Debian Codegraph @

cmake/modules/FindPythonLibrary.cmake

raw
1 # Find Python
2 # ~~~~~~~~~~~
3 # Find the Python interpreter and related Python directories.
4 #
5 # This file defines the following variables:
6 #
7 # PYTHON_EXECUTABLE - The path and filename of the Python interpreter.
8 #
9 # PYTHON_SHORT_VERSION - The version of the Python interpreter found,
10 # excluding the patch version number. (e.g. 2.5 and not 2.5.1))
11 #
12 # PYTHON_LONG_VERSION - The version of the Python interpreter found as a human
13 # readable string.
14 #
15 # PYTHON_SITE_PACKAGES_DIR - Location of the Python site-packages directory.
16 #
17 # PYTHON_INCLUDE_PATH, PYTHON_INCLUDE_DIRS - Directory holding the python.h include file.
18 #
19 # PYTHON_LIBRARY, PYTHON_LIBRARIES- Location of the Python library.
20
21 # SPDX-FileCopyrightText: 2007 Simon Edwards <[email protected]>
22 # SPDX-FileCopyrightText: 2012 Luca Beltrame <[email protected]>
23 #
24 # SPDX-License-Identifier: BSD-3-Clause
25 #
26
27 if(APPLE)
28 set(CMAKE_FRAMEWORK_PATH_OLD ${CMAKE_FRAMEWORK_PATH})
29 set(CMAKE_FRAMEWORK_PATH "${CMAKE_INSTALL_PREFIX}/lib" ${CMAKE_SYSTEM_FRAMEWORK_PATH})
30 endif(APPLE)
31
32 include(FindPackageHandleStandardArgs)
33
34 if (WIN32 OR APPLE)
35 set(Python_FIND_STRATEGY LOCATION)
36 # on APPLE we search python using Python_ROOT_DIR set by the toolchain file
37 find_package(Python 3.8 REQUIRED COMPONENTS Development Interpreter)
38 else()
39 find_package(Python 3.8 REQUIRED COMPONENTS Interpreter OPTIONAL_COMPONENTS Development)
40 endif()
41
42 message(STATUS "FindPythonLibrary: ${Python_Interpreter_FOUND}")
43
44 if (Python_Interpreter_FOUND)
45 set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
46
47 # Set the Python libraries to what we actually found for interpreters
48 set(Python_ADDITIONAL_VERSIONS "${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
49 # These are kept for compatibility
50 set(PYTHON_SHORT_VERSION "${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
51 set(PYTHON_LONG_VERSION ${PYTHON_VERSION_STRING})
52
53 if(Python_Development_FOUND)
54 set(PYTHON_INCLUDE_DIRS ${Python_INCLUDE_DIRS})
55 set(PYTHON_INCLUDE_PATH ${Python_INCLUDE_DIRS})
56 set(PYTHON_LIBRARY ${Python_LIBRARIES})
57 endif(Python_Development_FOUND)
58
59 # Auto detect Python site-packages directory
60 execute_process(COMMAND ${Python_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_path('platlib'))"
61 OUTPUT_VARIABLE PYTHON_SITE_PACKAGES_DIR
62 OUTPUT_STRIP_TRAILING_WHITESPACE
63 )
64
65 message(STATUS "Python system site-packages directory: ${PYTHON_SITE_PACKAGES_DIR}")
66
67 if (NOT CMAKE_PREFIX_PATH)
68 message (WARNING "CMAKE_PREFIX_PATH variable is not set, we might NOT be able to detect SIP modules")
69 # use install prefix as a fallback
70 set(_all_prefix_paths ${CMAKE_INSTALL_PREFIX})
71 else()
72 set(_all_prefix_paths ${CMAKE_PREFIX_PATH})
73 endif()
74
75 unset(KRITA_PYTHONPATH_V4 CACHE)
76 unset(KRITA_PYTHONPATH_V5 CACHE)
77
78 cmake_path(CONVERT "${_all_prefix_paths}" TO_CMAKE_PATH_LIST _python_prefix_path_v4 NORMALIZE)
79 list(REMOVE_ITEM _python_prefix_path_v4 "")
80 list(TRANSFORM _python_prefix_path_v4 APPEND "/lib/krita-python-libs")
81 cmake_path(CONVERT "${_python_prefix_path_v4}" TO_NATIVE_PATH_LIST KRITA_PYTHONPATH_V4 NORMALIZE)
82
83 cmake_path(CONVERT "${_all_prefix_paths}" TO_CMAKE_PATH_LIST _python_prefix_path_v5 NORMALIZE)
84 list(REMOVE_ITEM _python_prefix_path_v5 "")
85 list(TRANSFORM _python_prefix_path_v5 APPEND "/lib/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages")
86 cmake_path(CONVERT "${_python_prefix_path_v5}" TO_NATIVE_PATH_LIST KRITA_PYTHONPATH_V5 NORMALIZE)
87
88 message(STATUS "Krita site-packages directories for SIP v4: ${KRITA_PYTHONPATH_V4}")
89 message(STATUS "Krita site-packages directories for SIP v5+: ${KRITA_PYTHONPATH_V5}")
90 endif()
91
92 find_package_handle_standard_args(PythonLibrary DEFAULT_MSG PYTHON_LIBRARY PYTHON_INCLUDE_DIRS PYTHON_INCLUDE_PATH)
93
94 if(APPLE)
95 set(CMAKE_FRAMEWORK_PATH ${CMAKE_FRAMEWORK_PATH_OLD})
96 endif(APPLE)