|
1 | | -# This file provides a work-around to provide consistent |
2 | | -# python3 cmake variables for cmake versions pre/post 3.12.0 |
3 | | - |
4 | 1 | # If the cmake variable "PYTHON_DEVELOPMENT_REQUIRED" is set to ON |
5 | 2 | # then the development environments are found. |
6 | 3 |
|
7 | | -# CMake < 3.15 will use deprecated search by PythonInterp and PythonLibs. |
8 | | - |
9 | | -# CMake >= 3.15 will use FindPython3 and can be controlled accordingly (i.e. setting Python3_ROOT_DIR or |
| 4 | +# CMake will use FindPython3 and can be controlled accordingly (i.e. setting Python3_ROOT_DIR or |
10 | 5 | # Python3_FIND_REGISTRY (useful if using PATH to specify Python3 version on Windows). |
11 | 6 | # Additionally, setting Python3_EXECUTABLE can be used to set the Python version explicitly, but |
12 | 7 | # may become less reliable with newer versions of CMake (as opposed setting FindPython3 HINTS). Current |
13 | 8 | # implementation gives preference to active virtualenvs. |
14 | 9 |
|
15 | | -if("${CMAKE_VERSION}" VERSION_LESS_EQUAL "3.15.0") |
16 | | - # CMake < 3.15 will use deprecated search by PythonInterp and PythonLibs. |
17 | | - # Use of PythonInterp and PythonLibs is deprecated since cmake version 3.12.0 |
18 | | - # Unstable behavior of FindPython between CMake 3.12 and 3.15, so including in this |
19 | | - # Only use deprecated mechanisms for older versions of cmake |
20 | | - # |
21 | | - set(Python_ADDITIONAL_VERSIONS 3.9 3.8 3.7 3.6) |
22 | | - find_package(PythonInterp) |
23 | | - if(PYTHON_DEVELOPMENT_REQUIRED) |
24 | | - find_package(PythonLibs REQUIRED) |
25 | | - # check for version mismatch. |
26 | | - if(PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND |
27 | | - AND NOT(PYTHON_VERSION_STRING VERSION_EQUAL PYTHONLIBS_VERSION_STRING)) |
28 | | - message(FATAL_ERROR "Python executable (\"${PYTHON_VERSION_STRING}\") and library (\"${PYTHONLIBS_VERSION_STRING}\") version mismatch.") |
29 | | - endif() |
30 | | - endif() |
31 | | - if(PYTHON_VERSION_STRING VERSION_LESS 3.6) |
32 | | - # if python version is less than 3.6, unset so that it appears that no python version is found. |
33 | | - # to emulate the same behavior as find(Python3 ..) from cmake 3.12.0+ |
34 | | - unset(PYTHON_EXECUTABLE) |
35 | | - unset(PYTHONINTERP_FOUND) |
36 | | - unset(PYTHON_VERSION_STRING) |
37 | | - unset(PYTHON_INCLUDE_DIRS) |
38 | | - else() |
39 | | - ## For forward compatibility with cmake 3.12.0 or greater, emulate variable names from FindPython3.cmake |
40 | | - set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE}) |
41 | | - set(Python3_Interpreter_FOUND ${PYTHONINTERP_FOUND}) |
42 | | - set(Python3_VERSION ${PYTHON_VERSION_STRING}) |
43 | | - |
44 | | - set(Python3_Development_FOUND ${PYTHONLIBS_FOUND}) |
45 | | - set(Python3_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS}) |
46 | | - set(Python3_LIBRARIES ${PYTHON_LIBRARIES}) |
47 | | - endif() |
48 | | -else() |
49 | | - cmake_policy(SET CMP0094 NEW) # makes FindPython3 prefer activated virtualenv Python to latest version |
50 | | - if(PYTHON_DEVELOPMENT_REQUIRED) |
51 | | - if(DEFINED Python3_EXECUTABLE) # if already specified |
52 | | - set(_specified_Python3_EXECUTABLE ${Python3_EXECUTABLE}) |
53 | | - endif() |
54 | | - # set(Python3_FIND_REGISTRY LAST) # default is FIRST. Do we need/want this? |
55 | | - find_package(Python3 COMPONENTS Interpreter Development) |
56 | | - if(DEFINED _specified_Python3_EXECUTABLE) |
57 | | - set(Python3_EXECUTABLE ${_specified_Python3_EXECUTABLE} CACHE INTERNAL |
58 | | - "Path to the Python interpreter" FORCE) |
59 | | - endif() |
60 | | - else() # if not PYTHON_DEVELOPMENT_REQUIRED, just find some version of Python (don't need to be as specific) |
61 | | - find_package(Python3 COMPONENTS Interpreter) |
| 10 | +cmake_policy(SET CMP0094 NEW) # makes FindPython3 prefer activated virtualenv Python to latest version |
| 11 | +if(PYTHON_DEVELOPMENT_REQUIRED) |
| 12 | + if(DEFINED Python3_EXECUTABLE) # if already specified |
| 13 | + set(_specified_Python3_EXECUTABLE ${Python3_EXECUTABLE}) |
62 | 14 | endif() |
63 | | - if(NOT Python3_EXECUTABLE AND _Python3_EXECUTABLE) # workaround for cases where FindPython3 fails to set correctly |
64 | | - set(Python3_EXECUTABLE ${_Python3_EXECUTABLE} CACHE INTERNAL |
| 15 | + # set(Python3_FIND_REGISTRY LAST) # default is FIRST. Do we need/want this? |
| 16 | + find_package(Python3 COMPONENTS Interpreter Development) |
| 17 | + if(DEFINED _specified_Python3_EXECUTABLE) |
| 18 | + set(Python3_EXECUTABLE ${_specified_Python3_EXECUTABLE} CACHE INTERNAL |
65 | 19 | "Path to the Python interpreter" FORCE) |
66 | 20 | endif() |
| 21 | +else() # if not PYTHON_DEVELOPMENT_REQUIRED, just find some version of Python (don't need to be as specific) |
| 22 | + find_package(Python3 COMPONENTS Interpreter) |
| 23 | +endif() |
| 24 | +if(NOT Python3_EXECUTABLE AND _Python3_EXECUTABLE) # workaround for cases where FindPython3 fails to set correctly |
| 25 | + set(Python3_EXECUTABLE ${_Python3_EXECUTABLE} CACHE INTERNAL |
| 26 | + "Path to the Python interpreter" FORCE) |
67 | 27 | endif() |
0 commit comments