[cosmetic] separate c-lang format commit
[xbmc.git] / cmake / modules / buildtools / FindPythonInterpreter.cmake
blob1aced219526d8610f9e0e62a4f08e694f2ab408c
1 # FindPython
2 # --------
3 # Finds Python3 Interpreter
5 # This module will search for a Python3 Interpreter
7 # --------
9 # the following variables influence behaviour:
11 # PYTHON_INTERPRETER_PATH - use external python not found in system paths
12 #                           usage: -DPYTHON_INTERPRETER_PATH=/path/to/python3
14 # --------
16 # This will define the following variable:
18 #  PYTHON_EXECUTABLE - The HOST python executable
21 # We limit search paths to rule out TARGET paths that will populate the default cmake/package paths
22 # Note: we do not do a find_package call as it will populate targets based on HOST
23 # information and pollute the TARGET python searches when required for actual target platform.
24 find_program(PYTHON3_INTERPRETER_EXECUTABLE NAMES python3 python
25                                             HINTS ${PYTHON_INTERPRETER_PATH} ${NATIVEPREFIX}/bin
26                                             NO_CACHE
27                                             NO_PACKAGE_ROOT_PATH
28                                             NO_CMAKE_PATH
29                                             NO_CMAKE_ENVIRONMENT_PATH
30                                             NO_CMAKE_INSTALL_PREFIX)
32 if(PYTHON3_INTERPRETER_EXECUTABLE)
33   execute_process(COMMAND "${PYTHON3_INTERPRETER_EXECUTABLE}" --version
34                   OUTPUT_VARIABLE PYTHON3_INTERPRETER_VERSION
35                   OUTPUT_STRIP_TRAILING_WHITESPACE)
36   string(REGEX REPLACE "^Python (.*)" "\\1" PYTHON3_INTERPRETER_VERSION "${PYTHON3_INTERPRETER_VERSION}")
38   include(FindPackageHandleStandardArgs)
39   find_package_handle_standard_args(PythonInterpreter
40                                     REQUIRED_VARS PYTHON3_INTERPRETER_EXECUTABLE PYTHON3_INTERPRETER_VERSION
41                                     VERSION_VAR PYTHON3_INTERPRETER_VERSION)
43   if(PythonInterpreter_FOUND)
44     # We explicitly use a CACHE variable instead of a TARGET as execute_command is not
45     # able to use a TARGET - https://gitlab.kitware.com/cmake/cmake/-/issues/18364
46     set(PYTHON_EXECUTABLE ${PYTHON3_INTERPRETER_EXECUTABLE} CACHE FILEPATH "Host Python interpreter" FORCE)
47   else()
48     if(PythonInterpreter_FIND_REQUIRED)
49       message(FATAL_ERROR "A python3 interpreter was not found. Consider providing path using -DPYTHON_INTERPRETER_PATH=<path/to/python3>")
50     endif()
51   endif()
52 else()
53   if(PythonInterpreter_FIND_REQUIRED)
54     message(FATAL_ERROR "A python3 interpreter was not found. Consider providing path using -DPYTHON_INTERPRETER_PATH=<path/to/python3>")
55   endif()
56 endif()