From dd466e07c82f0d0919ea99868bac909943f0f855 Mon Sep 17 00:00:00 2001 From: Jamie Snape Date: Thu, 10 Jan 2019 12:03:15 -0500 Subject: [PATCH] Add support for Python 3 in CMake build --- CMakeLists.txt | 44 ++++++++++++++++--- doc/developers.rst | 1 - doc/python_bindings.rst | 6 ++- .../workspace/pybind11/package-create-cps.py | 12 +---- tools/workspace/python/repository.bzl | 2 + 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f05e656d158b..b190dd9690d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/doc/developers.rst b/doc/developers.rst index c518c4dbb7f5..90a2a15954c8 100644 --- a/doc/developers.rst +++ b/doc/developers.rst @@ -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: diff --git a/doc/python_bindings.rst b/doc/python_bindings.rst index dc78addc0ee5..a6a6bea6ec16 100644 --- a/doc/python_bindings.rst +++ b/doc/python_bindings.rst @@ -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 @@ -45,6 +43,9 @@ Next, ensure that your ``PYTHONPATH`` is properly configured: See :ref:`below ` for usage instructions. If using macOS, pay special attention to :ref:`this note `. +Python 2.7 is currently the only supported version for the bindings supplied +by the binary packages. + Building the Python Bindings ---------------------------- @@ -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 diff --git a/tools/workspace/pybind11/package-create-cps.py b/tools/workspace/pybind11/package-create-cps.py index fdb5450a7d05..8057f19dd8bd 100644 --- a/tools/workspace/pybind11/package-create-cps.py +++ b/tools/workspace/pybind11/package-create-cps.py @@ -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"], diff --git a/tools/workspace/python/repository.bzl b/tools/workspace/python/repository.bzl index 1cfab1e6787b..a1b029aa1aa5 100644 --- a/tools/workspace/python/repository.bzl +++ b/tools/workspace/python/repository.bzl @@ -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"],