3 # Finds Python3 libraries
5 # This module will search for the required python libraries on the system
6 # If multiple versions are found, the highest version will be used.
10 # the following variables influence behaviour:
12 # PYTHON_PATH - use external python not found in system paths
13 # usage: -DPYTHON_PATH=/path/to/python/lib
14 # PYTHON_VER - use exact python version, fail if not found
15 # usage: -DPYTHON_VER=3.8
19 # This module will define the following variables:
21 # PYTHON_FOUND - system has PYTHON
22 # PYTHON_VERSION - Python version number (Major.Minor)
23 # PYTHON_EXECUTABLE - Python interpreter binary
24 # PYTHON_INCLUDE_DIRS - the python include directory
25 # PYTHON_LIBRARIES - The python libraries
26 # PYTHON_LDFLAGS - Python provided link options
31 # for Depends/Windows builds, set search root dir to libdir path
33 OR CMAKE_SYSTEM_NAME STREQUAL WINDOWS
34 OR CMAKE_SYSTEM_NAME STREQUAL WindowsStore)
35 set(Python3_USE_STATIC_LIBS TRUE)
36 set(Python3_ROOT_DIR ${libdir})
39 # Force set to tools/depends python version
44 # Provide root dir to search for Python if provided
46 set(Python3_ROOT_DIR ${PYTHON_PATH})
48 # unset cache var so we can generate again with a different dir (or none) if desired
49 unset(PYTHON_PATH CACHE)
52 # Set specific version of Python to find if provided
54 set(VERSION ${PYTHON_VER})
55 set(EXACT_VER "EXACT")
57 # unset cache var so we can generate again with a different ver (or none) if desired
58 unset(PYTHON_VER CACHE)
61 find_package(Python3 ${VERSION} ${EXACT_VER} COMPONENTS Development)
64 find_library(FFI_LIBRARY ffi REQUIRED)
65 find_library(EXPAT_LIBRARY expat REQUIRED)
66 find_library(INTL_LIBRARY intl REQUIRED)
67 find_library(GMP_LIBRARY gmp REQUIRED)
68 find_library(LZMA_LIBRARY lzma REQUIRED)
70 if(NOT CORE_SYSTEM_NAME STREQUAL android)
71 set(PYTHON_DEP_LIBRARIES pthread dl util)
72 if(CORE_SYSTEM_NAME STREQUAL linux)
73 # python archive built via depends requires librt for _posixshmem library
74 list(APPEND PYTHON_DEP_LIBRARIES rt)
78 list(APPEND Python3_LIBRARIES ${LZMA_LIBRARY} ${FFI_LIBRARY} ${EXPAT_LIBRARY} ${INTL_LIBRARY} ${GMP_LIBRARY} ${PYTHON_DEP_LIBRARIES})
80 if(CORE_SYSTEM_NAME STREQUAL linux)
81 if(HOST_CAN_EXECUTE_TARGET)
82 find_package(Python3 ${VERSION} ${EXACT_VER} COMPONENTS Interpreter)
84 find_package(Python3 COMPONENTS Interpreter)
90 list(APPEND PYTHON_DEFINITIONS -DHAS_PYTHON=1)
91 # These are all set for easy integration with the rest of our build system
92 set(PYTHON_FOUND ${Python3_FOUND})
93 if(NOT PYTHON_EXECUTABLE)
94 set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE} CACHE FILEPATH "Python interpreter" FORCE)
96 set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
97 set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
98 set(PYTHON_VERSION "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}" CACHE INTERNAL "" FORCE)
99 set(PYTHON_LDFLAGS ${Python3_LINK_OPTIONS})
102 mark_as_advanced(PYTHON_EXECUTABLE PYTHON_VERSION PYTHON_INCLUDE_DIRS PYTHON_LDFLAGS LZMA_LIBRARY FFI_LIBRARY EXPAT_LIBRARY INTL_LIBRARY GMP_LIBRARY)