From 37c3cdd723c618655164b2cfd444d0515e094e68 Mon Sep 17 00:00:00 2001 From: Nathan Whitehorn Date: Tue, 11 Sep 2018 13:25:21 -0700 Subject: [PATCH] Fix Python detection on Python 3. Python 3 uses somewhat different naming of the library, making the custom logic break. This just replaces all the custom Python-finding logic with the built-in CMake Python-finding logic, which works and can be relied upon to be updated as necessary. --- cmake/Packages/Python.cmake | 64 ++++--------------------------------- 1 file changed, 6 insertions(+), 58 deletions(-) diff --git a/cmake/Packages/Python.cmake b/cmake/Packages/Python.cmake index 9ac3af9..f5dabd8 100644 --- a/cmake/Packages/Python.cmake +++ b/cmake/Packages/Python.cmake @@ -1,60 +1,9 @@ -FIND_PROGRAM(PYTHON_EXECUTABLE python - PATHS ENV PATH # look in the PATH environment variable - NO_DEFAULT_PATH # do not look anywhere else... - ) +find_package(PythonInterp) +find_package(PythonLibs) -IF(PYTHON_EXECUTABLE) - EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -V - OUTPUT_VARIABLE STDOUT_VERSION - ERROR_VARIABLE PYTHON_VERSION - ERROR_STRIP_TRAILING_WHITESPACE) - - IF(STDOUT_VERSION MATCHES "Python") - set(PYTHON_VERSION "${STDOUT_VERSION}") - ENDIF() - - STRING(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.?([0-9]*)" - PYTHON_STRIPPED_VERSION - ${PYTHON_VERSION}) - STRING(REGEX MATCH "([0-9]+)\\.([0-9]+)" - PYTHON_STRIPPED_MAJOR_MINOR_VERSION - ${PYTHON_VERSION}) - - STRING(REPLACE "." "" PYTHON_VERSION_NO_DOTS ${PYTHON_STRIPPED_MAJOR_MINOR_VERSION}) - - EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; sys.stdout.write(sys.prefix)" - OUTPUT_VARIABLE PYTHON_ROOT) - - FIND_LIBRARY(PYTHON_LIBRARY - NAMES python${PYTHON_VERSION_NO_DOTS} python${PYTHON_STRIPPED_MAJOR_MINOR_VERSION} - PATHS ${PYTHON_ROOT} ${PYTHON_ROOT}/lib - PATH_SUFFIXES lib - NO_SYSTEM_ENVIRONMENT_PATH - NO_DEFAULT_PATH - ) - FIND_PATH(PYTHON_INCLUDE_DIR - NAMES Python.h - PATHS - ${PYTHON_ROOT}/include - PATH_SUFFIXES - python${PYTHON_STRIPPED_MAJOR_MINOR_VERSION} - NO_DEFAULT_PATH - ) - - IF(EXISTS "${PYTHON_INCLUDE_DIR}/Python.h" ) - SET(PYTHON_FOUND TRUE CACHE BOOL "Python found successfully" FORCE) - MESSAGE(STATUS "Python found") - MESSAGE (STATUS " * binary: ${PYTHON_EXECUTABLE}") - MESSAGE (STATUS " * version: ${PYTHON_VERSION}") - MESSAGE (STATUS " * includes: ${PYTHON_INCLUDE_DIR}") - MESSAGE (STATUS " * libs: ${PYTHON_LIBRARY}") - ELSE() - SET(PYTHON_FOUND FALSE CACHE BOOL "Python found successfully" FORCE) - ENDIF() - - IF(PYTHON_FOUND) +IF(PYTHON_FOUND) # Search for numpy - EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy; print numpy.__version__" + EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy; print(numpy.__version__)" RESULT_VARIABLE NUMPY_FOUND OUTPUT_VARIABLE NUMPY_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) # IF(NUMPY_FOUND EQUAL 0 AND NOT NUMPY_VERSION VERSION_LESS 1.7) # SET(NUMPY_FOUND TRUE) @@ -70,7 +19,7 @@ IF(PYTHON_EXECUTABLE) IF(NUMPY_FOUND) SET(NUMPY_FOUND TRUE CACHE BOOL "Numpy found successfully" FORCE) EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c - "import numpy; print numpy.get_include()" + "import numpy; print(numpy.get_include())" OUTPUT_VARIABLE NUMPY_INCLUDE_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) SET(NUMPY_INCLUDE_DIR ${NUMPY_INCLUDE_DIR} CACHE STRING "Numpy directory") @@ -78,5 +27,4 @@ IF(PYTHON_EXECUTABLE) ELSE() SET(NUMPY_FOUND FALSE CACHE BOOL "Numpy found successfully" FORCE) ENDIF() - ENDIF(PYTHON_FOUND) -ENDIF(PYTHON_EXECUTABLE) \ No newline at end of file +ENDIF(PYTHON_FOUND)