cmake: FindPython3: Adjust python3 program search
Fixes: #24340 Using find_package(Python3 3.6) will select the highest available python3. This means that in case user has: /usr/bin/python --> python2.7 /usr/bin/python3 --> python3.6 /usr/bin/python3.6 /usr/bin/python3.8 then CMake will choose python 3.8. This commit changes that behavior, so that in the above scenario, then Python 3.6 will be preferred. It also adds the following, python will be preferred over python3, if both meets the minimum requirement. For example: /usr/bin/python --> python3.6 /usr/bin/python3 --> python3.7 then Python 3.6 is prefered. It further introduces PYTHON_PREFER variable, which can be used to further control the behavior. As example -DPYTHON_PREFER=python3.7 will choose Python 3.7 if installed. Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
parent
9aefdaff2e
commit
94de3e9f60
1 changed files with 25 additions and 1 deletions
|
@ -9,5 +9,29 @@ if (WIN32)
|
|||
set(ENV{PYTHONIOENCODING} "utf-8")
|
||||
endif()
|
||||
|
||||
find_package(Python3 3.6 REQUIRED)
|
||||
set(PYTHON_MINIMUM_REQUIRED 3.6)
|
||||
|
||||
# We are using foreach here, instead of find_program(PYTHON_EXECUTABLE_SYSTEM_DEFAULT "python" "python3")
|
||||
# cause just using find_program directly could result in a python2.7 as python, and not finding a valid python3.
|
||||
foreach(PYTHON_PREFER ${PYTHON_PREFER} "python" "python3")
|
||||
find_program(PYTHON_PREFER_EXECUTABLE ${PYTHON_PREFER})
|
||||
if(PYTHON_PREFER_EXECUTABLE)
|
||||
execute_process (COMMAND "${PYTHON_PREFER_EXECUTABLE}" -c
|
||||
"import sys; sys.stdout.write('.'.join([str(x) for x in sys.version_info[:2]]))"
|
||||
RESULT_VARIABLE result
|
||||
OUTPUT_VARIABLE version
|
||||
ERROR_QUIET
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
if(version VERSION_LESS PYTHON_MINIMUM_REQUIRED)
|
||||
set_property (CACHE PYTHON_PREFER_EXECUTABLE PROPERTY VALUE "PYTHON_PREFER_EXECUTABLE-NOTFOUND")
|
||||
else()
|
||||
set(PYTHON_MINIMUM_REQUIRED ${version})
|
||||
set(PYTHON_EXACT EXACT)
|
||||
break()
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
find_package(Python3 ${PYTHON_MINIMUM_REQUIRED} REQUIRED ${PYTHON_EXACT})
|
||||
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue