Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmake] Create FindPythonInterpreter for host information #23877

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
56 changes: 56 additions & 0 deletions cmake/modules/buildtools/FindPythonInterpreter.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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)
else()
if(PythonInterpreter_FIND_REQUIRED)
message(FATAL_ERROR "A python3 interpreter was not found. Consider providing path using -DPYTHON_INTERPRETER_PATH=<path/to/python3>")
endif()
endif()
else()
if(PythonInterpreter_FIND_REQUIRED)
message(FATAL_ERROR "A python3 interpreter was not found. Consider providing path using -DPYTHON_INTERPRETER_PATH=<path/to/python3>")
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