Skip to content

Instantly share code, notes, and snippets.

@RLovelett
Created April 1, 2020 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RLovelett/f80691e5b2535e04bcfb8ee61a595a8d to your computer and use it in GitHub Desktop.
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
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