Created
April 1, 2020 17:59
-
-
Save RLovelett/f80691e5b2535e04bcfb8ee61a595a8d to your computer and use it in GitHub Desktop.
Example of preferring Python 2 over Python3 with new FindPython2 and FindPython3 CMake modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.12.4) | |
find_package(Python2 COMPONENTS Interpreter) | |
find_package(Python3 COMPONENTS Interpreter) | |
if (NOT Python2_Interpreter_FOUND AND NOT Python3_Interpreter_FOUND) | |
message(FATAL_ERROR "Could NOT find Python2 or Python3") | |
endif() | |
# https://github.com/Kitware/CMake/blob/cfc92b483fbd3695d4a67843977e709ba4d7ea47/Modules/FindPython/Support.cmake#L2433-L2439 | |
if (Python2_Interpreter_FOUND) | |
add_executable(Python::Interpreter IMPORTED) | |
set_property(TARGET Python::Interpreter PROPERTY IMPORTED_LOCATION "${Python2_EXECUTABLE}") | |
elseif (Python3_Interpreter_FOUND) | |
add_executable(Python::Interpreter IMPORTED) | |
set_property(TARGET Python::Interpreter PROPERTY IMPORTED_LOCATION "${Python3_EXECUTABLE}") | |
else() | |
message(FATAL_ERROR "Could NOT find Python2 or Python3") | |
endif() | |
if (TARGET Python::Interpreter) | |
get_target_property(_location Python::Interpreter IMPORTED_LOCATION) | |
message(STATUS "Set Python::Interpreter = ${_location}") | |
endif() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment