Skip to content

Commit

Permalink
[cmake] Create FindPythonInterpreter for host information
Browse files Browse the repository at this point in the history
This splits out host find information from the regular target find module.
This allows us to not mix and check for host executable status, and only explicitly
search for the host interpreter when required (eg ENABLE_EVENTCLIENTS=ON)
  • Loading branch information
fuzzard committed Oct 24, 2023
1 parent 5423d9b commit a5450d7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
12 changes: 0 additions & 12 deletions cmake/modules/FindPython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#
# PYTHON_FOUND - system has PYTHON
# PYTHON_VERSION - Python version number (Major.Minor)
# PYTHON_EXECUTABLE - Python interpreter binary
# PYTHON_INCLUDE_DIRS - the python include directory
# PYTHON_LIBRARIES - The python libraries
# PYTHON_LDFLAGS - Python provided link options
Expand Down Expand Up @@ -76,23 +75,12 @@ if(KODI_DEPENDSBUILD)
endif()

list(APPEND Python3_LIBRARIES ${LZMA_LIBRARY} ${FFI_LIBRARY} ${EXPAT_LIBRARY} ${INTL_LIBRARY} ${GMP_LIBRARY} ${PYTHON_DEP_LIBRARIES})
else()
if(CORE_SYSTEM_NAME STREQUAL linux)
if(HOST_CAN_EXECUTE_TARGET)
find_package(Python3 ${VERSION} ${EXACT_VER} COMPONENTS Interpreter)
else()
find_package(Python3 COMPONENTS Interpreter)
endif()
endif()
endif()

if(Python3_FOUND)
list(APPEND PYTHON_DEFINITIONS -DHAS_PYTHON=1)
# These are all set for easy integration with the rest of our build system
set(PYTHON_FOUND ${Python3_FOUND})
if(NOT PYTHON_EXECUTABLE)
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE} CACHE FILEPATH "Python interpreter" FORCE)
endif()
set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
set(PYTHON_VERSION "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}" CACHE INTERNAL "" FORCE)
Expand Down
48 changes: 48 additions & 0 deletions cmake/modules/buildtools/FindPythonInterpreter.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# FindPython
# --------
# Finds Python3 Interpreter
#
# This module will search for a Python3 Interpreter
#
# --------
#
# the following variables influence behaviour:
#
# PYTHON_INTERPRETER_PATH - use external python not found in system paths
# usage: -DPYTHON_INTERPRETER_PATH=/path/to/python3
#
# --------
#
# This will define the following variable:
#
# PYTHON_EXECUTABLE - The HOST python executable
#

# We limit search paths to rule out TARGET paths that will populate the default cmake/package paths
# Note: we do not do a find_package call as it will populate targets based on HOST
# information and pollute the TARGET python searches when required for actual target platform.
find_program(PYTHON3_INTERPRETER_EXECUTABLE NAMES python3 python
HINTS ${PYTHON_INTERPRETER_PATH} ${NATIVEPREFIX}/bin
NO_CACHE
NO_PACKAGE_ROOT_PATH
NO_CMAKE_PATH
NO_CMAKE_ENVIRONMENT_PATH
NO_CMAKE_INSTALL_PREFIX)

if(PYTHON3_INTERPRETER_EXECUTABLE)
execute_process(COMMAND "${PYTHON3_INTERPRETER_EXECUTABLE}" --version
OUTPUT_VARIABLE PYTHON3_INTERPRETER_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REGEX REPLACE "^Python (.*)" "\\1" PYTHON3_INTERPRETER_VERSION "${PYTHON3_INTERPRETER_VERSION}")

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PythonInterpreter
REQUIRED_VARS PYTHON3_INTERPRETER_EXECUTABLE PYTHON3_INTERPRETER_VERSION
VERSION_VAR PYTHON3_INTERPRETER_VERSION)

if(PythonInterpreter_FOUND)
# We explicitly use a CACHE variable instead of a TARGET as execute_command is not
# able to use a TARGET - https://gitlab.kitware.com/cmake/cmake/-/issues/18364
set(PYTHON_EXECUTABLE ${PYTHON3_INTERPRETER_EXECUTABLE} CACHE FILEPATH "Host Python interpreter" FORCE)
endif()
endif()
1 change: 1 addition & 0 deletions cmake/scripts/linux/Install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ install(FILES ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/scripts/${APP_NAME}Config.cm
COMPONENT kodi-addon-dev)

if(ENABLE_EVENTCLIENTS)
find_package(PythonInterpreter REQUIRED)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(prefix=''))"
OUTPUT_VARIABLE PYTHON_LIB_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
# Install kodi-eventclients-common BT python files
Expand Down

0 comments on commit a5450d7

Please sign in to comment.