Skip to content

Commit

Permalink
Add support for Python 3 in CMake build
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Jan 10, 2019
1 parent a67aac2 commit dd466e0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
44 changes: 38 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,46 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
)
endif()

if(APPLE)
# Ensure that the python2 executable or link is used in preference to the
# python and python2.7 executables or links.
find_program(PYTHON_EXECUTABLE NAMES python2)
# The supported Python major/minor versions should match those listed in both
# doc/developers.rst and tools/workspace/python/repository.bzl.
set(SUPPORTED_PYTHON_VERSIONS 2 2.7 3 3.6 3.7)

if(PYTHON_EXECUTABLE)
find_package(PythonInterp MODULE REQUIRED)
else()
string(REPLACE ";" " " SUPPORTED_PYTHON_VERSIONS_STRING
"${SUPPORTED_PYTHON_VERSIONS}"
)
set(WITH_PYTHON_VERSION 2 CACHE STRING
"Choose the version of Python to use, options are ${SUPPORTED_PYTHON_VERSIONS_STRING}"
)
set_property(CACHE WITH_PYTHON_VERSION PROPERTY
STRINGS "${SUPPORTED_PYTHON_VERSIONS}"
)

string(REPLACE "." ";" WITH_PYTHON_VERSION_LIST "${WITH_PYTHON_VERSION}")
list(GET WITH_PYTHON_VERSION_LIST 0 WITH_PYTHON_VERSION_MAJOR)

if(APPLE AND WITH_PYTHON_VERSION_MAJOR EQUAL 2)
# Ensure that the python2 executable or link is used in preference to the
# python and python2.7 executables or links, which correspond to the
# system version of Python installed on macOS. There is no system python3
# as of macOS Mojave 10.14.
find_program(PYTHON_EXECUTABLE NAMES python2)
endif()

find_package(PythonInterp ${WITH_PYTHON_VERSION} EXACT MODULE REQUIRED)
endif()

# TODO(jamiesnape) Ensure that we can switch between supported versions.
find_package(PythonInterp 2.7 EXACT MODULE REQUIRED)
set(PYTHON_VERSION_MAJOR_MINOR
"${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}"
)

if(NOT PYTHON_VERSION_MAJOR_MINOR IN_LIST SUPPORTED_PYTHON_VERSIONS)
message(WARNING
"Python ${PYTHON_VERSION_MAJOR_MINOR} is NOT supported. Python code in project drake_cxx_python may fail at runtime."
)
endif()

if(CMAKE_BUILD_TYPE STREQUAL Debug)
set(BAZEL_COMPILATION_MODE dbg)
Expand Down
1 change: 0 additions & 1 deletion doc/developers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ The following configurations are presently untested in continuous integration:

- macOS Mojave: C++, Python
- macOS, Ubuntu Bionic: MATLAB
- Ubuntu Bionic: Python 3

.. _configuration-management-non-determinism:

Expand Down
6 changes: 4 additions & 2 deletions doc/python_bindings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class which is exposed to C++ has been explicitly enumerated in one of the
source files inside the ``bindings/pydrake`` folder. These bindings are
installed as a single package called ``pydrake``.

Python 2.7 is currently the only supported version for these bindings.

.. _python-bindings-binary:

Binary Installation for Python
Expand Down Expand Up @@ -45,6 +43,9 @@ Next, ensure that your ``PYTHONPATH`` is properly configured:
See :ref:`below <using-python-bindings>` for usage instructions. If using
macOS, pay special attention to :ref:`this note <using-python-mac-os-path>`.

Python 2.7 is currently the only supported version for the bindings supplied
by the binary packages.

Building the Python Bindings
----------------------------

Expand All @@ -64,6 +65,7 @@ Please note the additional CMake options which affect the Python bindings:
* ``-DWITH_GUROBI={ON, [OFF]}`` - Build with Gurobi enabled.
* ``-DWITH_MOSEK={ON, [OFF]}`` - Build with MOSEK enabled.
* ``-DWITH_SNOPT={ON, [OFF]}`` - Build with SNOPT enabled.
* ``-DWITH_PYTHON_VERSION={[2], 3}`` - Build with a specific version of Python.

``{...}`` means a list of options, and the option surrounded by ``[...]`` is
the default option. An example of building ``pydrake`` with both Gurobi and
Expand Down
12 changes: 2 additions & 10 deletions tools/workspace/pybind11/package-create-cps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@
"Version": "%(VERSION_MAJOR)s.%(VERSION_MINOR)s.%(VERSION_PATCH)s",
"Requires": {
"PythonInterp": {
"Version": "2.7",
"X-CMake-Find-Args": [
"EXACT",
"MODULE"
]
"X-CMake-Find-Args": ["MODULE"]
},
"PythonLibs": {
"Version": "2.7",
"X-CMake-Find-Args": [
"EXACT",
"MODULE"
]
"X-CMake-Find-Args": ["MODULE"]
}
},
"Default-Components": [":pybind11"],
Expand Down
2 changes: 2 additions & 0 deletions tools/workspace/python/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Arguments:
load("@drake//tools/workspace:execute.bzl", "execute_or_fail", "which")
load("@drake//tools/workspace:os.bzl", "determine_os")

# The supported Python versions should match those listed in both the root
# CMakeLists.txt and doc/developers.rst.
_VERSION_SUPPORT_MATRIX = {
"ubuntu:16.04": ["2.7"],
"ubuntu:18.04": ["2.7", "3.6"],
Expand Down

0 comments on commit dd466e0

Please sign in to comment.