From caa9d44cc78e78c560352279b483de27fc8eb804 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Sun, 17 Jan 2016 22:36:34 +0100 Subject: [PATCH] cmake: robustified search for python --- CMakeLists.txt | 17 ++++++++++++----- docs/cmake.rst | 11 +++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9f7f878b..df08a397e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ option(PYBIND11_INSTALL "Install pybind11 header files?" ON) # Add a CMake parameter for choosing a desired Python version set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application") +include(CheckCXXCompilerFlag) + # Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "Setting build type to 'MinSizeRel' as none was specified.") @@ -23,11 +25,16 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) endif() string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) -set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6) -find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED) -find_package(PythonInterp ${PYBIND11_PYTHON_VERSION} REQUIRED) - -include(CheckCXXCompilerFlag) +set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7) +if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "") + find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT) + if (NOT PythonLibs_FOUND) + find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED) + endif() +else() + find_package(PythonLibs REQUIRED) +endif() +find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") # Enable C++11 mode on C++ / Clang diff --git a/docs/cmake.rst b/docs/cmake.rst index 5952920a5..348c54a64 100644 --- a/docs/cmake.rst +++ b/docs/cmake.rst @@ -28,8 +28,15 @@ and that the pybind11 repository is located in a subdirectory named :file:`pybin string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) # Try to autodetect Python (can be overridden manually if needed) - set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6) - find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} REQUIRED) + set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7) + if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "") + find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT REQUIRED) + else() + find_package(PythonLibs REQUIRED) + endif() + + # Uncomment the following line if you will also require a matching Python interpreter + # find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") # Enable C++11 mode on C++ / Clang