|
1 |
| -# Like cmake's FindPython but allows the user to override; should also |
2 |
| -# work (to some degree) with older cmake |
| 1 | +# Like cmake's FindPython but allows the user to override |
3 | 2 | function(imp_find_python)
|
4 |
| - set(USE_PYTHON2 off CACHE BOOL |
5 |
| - "Force use of Python2 (by default Python3 is used if available)") |
6 |
| - |
7 |
| - if (${CMAKE_VERSION} VERSION_LESS "3.14.0") |
8 |
| - message(WARNING "Using old Python detection logic. Recommended to upgrade to cmake 3.14.0 or later") |
9 |
| - if(NOT DEFINED PYTHON_INCLUDE_DIRS) |
10 |
| - if (USE_PYTHON2) |
11 |
| - set(_SEARCH_PYTHON_BINARIES python2 python) |
12 |
| - else() |
13 |
| - set(_SEARCH_PYTHON_BINARIES python3 python2 python) |
14 |
| - endif() |
15 |
| - |
16 |
| - foreach(pybinary ${_SEARCH_PYTHON_BINARIES}) |
17 |
| - execute_process(COMMAND ${pybinary} -c "import sys; print(sys.executable)" |
18 |
| - RESULT_VARIABLE retval |
19 |
| - WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
20 |
| - OUTPUT_VARIABLE python_full_path |
21 |
| - OUTPUT_STRIP_TRAILING_WHITESPACE) |
22 |
| - if(${retval} EQUAL 0) |
23 |
| - break() |
24 |
| - endif() |
25 |
| - endforeach() |
26 |
| - |
27 |
| - if(NOT ${retval} EQUAL 0) |
28 |
| - message(FATAL_ERROR "Could not find a suitable Python binary - looked for ${_SEARCH_PYTHON_BINARIES}") |
29 |
| - endif() |
30 |
| - set(PYTHON_EXECUTABLE ${python_full_path} CACHE INTERNAL "" FORCE) |
31 |
| - set(PYTHON_TEST_EXECUTABLE ${python_full_path} CACHE STRING "") |
32 |
| - endif() |
33 |
| - execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; print('%d.%d.%d' % sys.version_info[:3])" |
34 |
| - WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
35 |
| - OUTPUT_VARIABLE python_full_version |
36 |
| - OUTPUT_STRIP_TRAILING_WHITESPACE) |
37 |
| - string(REGEX REPLACE "^([0-9])+\\.[0-9]+.*" "\\1" major |
38 |
| - "${python_full_version}") |
39 |
| - string(REGEX REPLACE "^[0-9]+\\.([0-9]+).*" "\\1" minor |
40 |
| - "${python_full_version}") |
41 |
| - string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" patch |
42 |
| - "${python_full_version}") |
43 |
| - set(PYTHON_VERSION ${python_full_version} CACHE INTERNAL "" FORCE) |
44 |
| - set(PYTHON_VERSION_MAJOR ${major} CACHE INTERNAL "" FORCE) |
45 |
| - set(PYTHON_VERSION_MINOR ${minor} CACHE INTERNAL "" FORCE) |
46 |
| - set(PYTHON_VERSION_PATCH ${patch} CACHE INTERNAL "" FORCE) |
47 |
| - message(STATUS "Python binary is " ${PYTHON_EXECUTABLE} " (version " ${python_full_version} ")") |
48 |
| - if(NOT DEFINED PYTHON_INCLUDE_DIRS) |
49 |
| - find_package(PythonLibs ${python_full_version} EXACT REQUIRED) |
50 |
| - # Make sure PYTHON_INCLUDE_DIRS is in the cache so it can be |
51 |
| - # used elsewhere |
52 |
| - set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "") |
53 |
| - endif() |
54 |
| - if(NOT DEFINED PYTHON_NUMPY_INCLUDE_DIR) |
55 |
| - execute_process(COMMAND ${PYTHON_EXECUTABLE} -c |
56 |
| - "try: import numpy; print(numpy.get_include());\nexcept: pass" |
57 |
| - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
58 |
| - OUTPUT_VARIABLE __numpy_path |
59 |
| - OUTPUT_STRIP_TRAILING_WHITESPACE) |
60 |
| - find_path(PYTHON_NUMPY_INCLUDE_DIR numpy/arrayobject.h |
61 |
| - HINTS "${__numpy_path}" "${PYTHON_INCLUDE_PATH}" |
62 |
| - NO_DEFAULT_PATH) |
63 |
| - if(PYTHON_NUMPY_INCLUDE_DIR) |
64 |
| - set(PYTHON_NUMPY_FOUND 1 CACHE INTERNAL "Python numpy found") |
65 |
| - endif() |
66 |
| - include(FindPackageHandleStandardArgs) |
67 |
| - find_package_handle_standard_args(NumPy |
68 |
| - REQUIRED_VARS PYTHON_NUMPY_INCLUDE_DIR |
69 |
| - VERSION_VAR __numpy_version) |
70 |
| - endif() |
| 3 | + find_package(Python3 COMPONENTS Interpreter Development NumPy) |
71 | 4 |
|
| 5 | + if(Python3_Interpreter_FOUND AND Python3_Development_FOUND) |
| 6 | + # Use Python 3 tools |
| 7 | + set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE} CACHE STRING "") |
| 8 | + set(PYTHON_TEST_EXECUTABLE ${Python3_EXECUTABLE} CACHE STRING "") |
| 9 | + set(PYTHON_LIBRARIES ${Python3_LIBRARIES} CACHE STRING "") |
| 10 | + set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS} CACHE STRING "") |
| 11 | + set(PYTHON_LIBRARY_DIRS ${Python3_LIBRARY_DIRS} CACHE STRING "") |
| 12 | + set(PYTHON_NUMPY_FOUND ${Python3_NumPy_FOUND} CACHE STRING "") |
| 13 | + set(PYTHON_NUMPY_INCLUDE_DIR ${Python3_NumPy_INCLUDE_DIRS} |
| 14 | + CACHE STRING "") |
| 15 | + set(PYTHON_VERSION ${Python3_VERSION} CACHE STRING "") |
| 16 | + set(PYTHON_VERSION_MAJOR ${Python3_VERSION_MAJOR} CACHE STRING "") |
| 17 | + set(PYTHON_VERSION_MINOR ${Python3_VERSION_MINOR} CACHE STRING "") |
| 18 | + set(PYTHON_VERSION_PATCH ${Python3_VERSION_PATCH} CACHE STRING "") |
72 | 19 | else()
|
73 |
| - if (NOT USE_PYTHON2) |
74 |
| - find_package(Python3 COMPONENTS Interpreter Development NumPy) |
75 |
| - endif() |
76 |
| - |
77 |
| - if(NOT USE_PYTHON2 |
78 |
| - AND Python3_Interpreter_FOUND AND Python3_Development_FOUND) |
79 |
| - # Use Python 3 tools |
80 |
| - set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE} CACHE INTERNAL "" FORCE) |
81 |
| - set(PYTHON_TEST_EXECUTABLE ${Python3_EXECUTABLE} CACHE STRING "") |
82 |
| - set(PYTHON_LIBRARIES ${Python3_LIBRARIES} CACHE INTERNAL "" FORCE) |
83 |
| - set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS} CACHE INTERNAL "" FORCE) |
84 |
| - set(PYTHON_LIBRARY_DIRS ${Python3_LIBRARY_DIRS} CACHE INTERNAL "" FORCE) |
85 |
| - set(PYTHON_NUMPY_FOUND ${Python3_NumPy_FOUND} CACHE INTERNAL "" FORCE) |
86 |
| - set(PYTHON_NUMPY_INCLUDE_DIR ${Python3_NumPy_INCLUDE_DIRS} |
87 |
| - CACHE INTERNAL "" FORCE) |
88 |
| - set(PYTHON_VERSION ${Python3_VERSION} CACHE INTERNAL "" FORCE) |
89 |
| - set(PYTHON_VERSION_MAJOR ${Python3_VERSION_MAJOR} CACHE INTERNAL "" FORCE) |
90 |
| - set(PYTHON_VERSION_MINOR ${Python3_VERSION_MINOR} CACHE INTERNAL "" FORCE) |
91 |
| - set(PYTHON_VERSION_PATCH ${Python3_VERSION_PATCH} CACHE INTERNAL "" FORCE) |
92 |
| - else() |
93 |
| - find_package(Python2 COMPONENTS Interpreter Development NumPy) |
94 |
| - if(Python2_Interpreter_FOUND AND Python2_Development_FOUND) |
95 |
| - set(PYTHON_EXECUTABLE ${Python2_EXECUTABLE} CACHE INTERNAL "" FORCE) |
96 |
| - set(PYTHON_TEST_EXECUTABLE ${Python2_EXECUTABLE} CACHE STRING "") |
97 |
| - set(PYTHON_LIBRARIES ${Python2_LIBRARIES} CACHE INTERNAL "" FORCE) |
98 |
| - set(PYTHON_INCLUDE_DIRS ${Python2_INCLUDE_DIRS} CACHE INTERNAL "" FORCE) |
99 |
| - set(PYTHON_LIBRARY_DIRS ${Python2_LIBRARY_DIRS} CACHE INTERNAL "" FORCE) |
100 |
| - set(PYTHON_NUMPY_FOUND ${Python2_NumPy_FOUND} CACHE INTERNAL "" FORCE) |
101 |
| - set(PYTHON_NUMPY_INCLUDE_DIR ${Python2_NumPy_INCLUDE_DIRS} |
102 |
| - CACHE INTERNAL "" FORCE) |
103 |
| - set(PYTHON_VERSION ${Python2_VERSION} CACHE INTERNAL "" FORCE) |
104 |
| - set(PYTHON_VERSION_MAJOR ${Python2_VERSION_MAJOR} |
105 |
| - CACHE INTERNAL "" FORCE) |
106 |
| - set(PYTHON_VERSION_MINOR ${Python2_VERSION_MINOR} |
107 |
| - CACHE INTERNAL "" FORCE) |
108 |
| - set(PYTHON_VERSION_PATCH ${Python2_VERSION_PATCH} |
109 |
| - CACHE INTERNAL "" FORCE) |
110 |
| - else() |
111 |
| - message(FATAL_ERROR "Could not find a Python interpreter and matching headers/libraries. Python is required to build.") |
112 |
| - endif() |
113 |
| - endif() |
| 20 | + message(FATAL_ERROR "Could not find a Python interpreter and matching headers/libraries. Python is required to build.") |
114 | 21 | endif()
|
115 | 22 | endfunction(imp_find_python)
|
0 commit comments