You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
13 KiB

  1. # - Find python libraries
  2. # This module finds if Python is installed and determines where the
  3. # include files and libraries are. It also determines what the name of
  4. # the library is. This code sets the following variables:
  5. #
  6. # PYTHONLIBS_FOUND - have the Python libs been found
  7. # PYTHON_LIBRARIES - path to the python library
  8. # PYTHON_INCLUDE_PATH - path to where Python.h is found (deprecated)
  9. # PYTHON_INCLUDE_DIRS - path to where Python.h is found
  10. # PYTHON_DEBUG_LIBRARIES - path to the debug library (deprecated)
  11. # PYTHONLIBS_VERSION_STRING - version of the Python libs found (since CMake 2.8.8)
  12. #
  13. # The Python_ADDITIONAL_VERSIONS variable can be used to specify a list of
  14. # version numbers that should be taken into account when searching for Python.
  15. # You need to set this variable before calling find_package(PythonLibs).
  16. #
  17. # You can point to a preferred python install to use by setting the following
  18. # to the point at the root directory of the python install:
  19. #
  20. # PYTHON_ROOT_DIR - The root directory of the python install
  21. #
  22. # If you'd like to specify the installation of Python to use, you should modify
  23. # the following cache variables:
  24. # PYTHON_LIBRARY - path to the python library
  25. # PYTHON_INCLUDE_DIR - path to where Python.h is found
  26. #=============================================================================
  27. # Copyright 2001-2009 Kitware, Inc.
  28. #
  29. # Distributed under the OSI-approved BSD License (the "License");
  30. # see accompanying file Copyright.txt for details.
  31. #
  32. # This software is distributed WITHOUT ANY WARRANTY; without even the
  33. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  34. # See the License for more information.
  35. #=============================================================================
  36. # (To distribute this file outside of CMake, substitute the full
  37. # License text for the above reference.)
  38. include(CMakeFindFrameworks)
  39. # Search for the python framework on Apple.
  40. cmake_find_frameworks(Python)
  41. set(_PYTHON1_VERSIONS 1.6 1.5)
  42. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  43. set(_PYTHON3_VERSIONS 3.10 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
  44. if(PythonLibs_FIND_VERSION)
  45. if(PythonLibs_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  46. string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _PYTHON_FIND_MAJ_MIN "${PythonLibs_FIND_VERSION}")
  47. string(REGEX REPLACE "^([0-9]+).*" "\\1" _PYTHON_FIND_MAJ "${_PYTHON_FIND_MAJ_MIN}")
  48. unset(_PYTHON_FIND_OTHER_VERSIONS)
  49. if(PythonLibs_FIND_VERSION_EXACT)
  50. if(_PYTHON_FIND_MAJ_MIN STREQUAL PythonLibs_FIND_VERSION)
  51. set(_PYTHON_FIND_OTHER_VERSIONS "${PythonLibs_FIND_VERSION}")
  52. else()
  53. set(_PYTHON_FIND_OTHER_VERSIONS "${PythonLibs_FIND_VERSION}" "${_PYTHON_FIND_MAJ_MIN}")
  54. endif()
  55. else()
  56. foreach(_PYTHON_V ${_PYTHON${_PYTHON_FIND_MAJ}_VERSIONS})
  57. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  58. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  59. endif()
  60. endforeach()
  61. endif()
  62. unset(_PYTHON_FIND_MAJ_MIN)
  63. unset(_PYTHON_FIND_MAJ)
  64. else()
  65. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonLibs_FIND_VERSION}_VERSIONS})
  66. endif()
  67. else()
  68. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  69. endif()
  70. # Set up the versions we know about, in the order we will search. Always add
  71. # the user supplied additional versions to the front.
  72. set(_Python_VERSIONS
  73. ${Python_ADDITIONAL_VERSIONS}
  74. ${_PYTHON_FIND_OTHER_VERSIONS}
  75. )
  76. unset(_PYTHON_FIND_OTHER_VERSIONS)
  77. unset(_PYTHON1_VERSIONS)
  78. unset(_PYTHON2_VERSIONS)
  79. unset(_PYTHON3_VERSIONS)
  80. foreach(_CURRENT_VERSION ${_Python_VERSIONS})
  81. string(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
  82. if(WIN32)
  83. if(MINGW)
  84. find_library(PYTHON_DEBUG_LIBRARY
  85. NAMES python${_CURRENT_VERSION}_d
  86. PATHS
  87. "${PYTHON_ROOT_DIR}"
  88. "c:/python${_CURRENT_VERSION}"
  89. "c:/python${_CURRENT_VERSION_NO_DOTS}"
  90. NO_SYSTEM_ENVIRONMENT_PATH
  91. )
  92. elseif(VCPKG_TOOLCHAIN)
  93. find_library(PYTHON_DEBUG_LIBRARY
  94. NAMES python${_CURRENT_VERSION_NO_DOTS}_d
  95. PATHS
  96. "${PYTHON_ROOT_DIR}"
  97. NO_SYSTEM_ENVIRONMENT_PATH
  98. )
  99. else()
  100. find_library(PYTHON_DEBUG_LIBRARY
  101. NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
  102. PATHS
  103. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  104. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  105. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  106. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  107. )
  108. endif()
  109. endif()
  110. if(MINGW)
  111. find_library(PYTHON_LIBRARY
  112. NAMES python${_CURRENT_VERSION}
  113. PATHS
  114. "${PYTHON_ROOT_DIR}"
  115. "C:/python"
  116. PATH_SUFFIXES
  117. ${_CURRENT_VERSION}
  118. ${_CURRENT_VERSION_NO_DOTS}
  119. NO_SYSTEM_ENVIRONMENT_PATH
  120. )
  121. elseif(VCPKG_TOOLCHAIN)
  122. find_library(PYTHON_LIBRARY
  123. NAMES python${_CURRENT_VERSION_NO_DOTS}
  124. PATHS
  125. "${PYTHON_ROOT_DIR}"
  126. PATH_SUFFIXES
  127. ${_CURRENT_VERSION}
  128. ${_CURRENT_VERSION_NO_DOTS}
  129. NO_SYSTEM_ENVIRONMENT_PATH
  130. )
  131. else()
  132. find_library(PYTHON_LIBRARY
  133. NAMES
  134. python${_CURRENT_VERSION_NO_DOTS}
  135. python${_CURRENT_VERSION}mu
  136. python${_CURRENT_VERSION}m
  137. python${_CURRENT_VERSION}u
  138. python${_CURRENT_VERSION}
  139. PATHS
  140. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  141. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  142. # Avoid finding the .dll in the PATH. We want the .lib.
  143. NO_SYSTEM_ENVIRONMENT_PATH
  144. )
  145. endif()
  146. # Look for the static library in the Python config directory
  147. find_library(PYTHON_LIBRARY
  148. NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
  149. # Avoid finding the .dll in the PATH. We want the .lib.
  150. NO_SYSTEM_ENVIRONMENT_PATH
  151. # This is where the static library is usually located
  152. PATH_SUFFIXES python${_CURRENT_VERSION}/config
  153. )
  154. # For backward compatibility, honour value of PYTHON_INCLUDE_PATH, if
  155. # PYTHON_INCLUDE_DIR is not set.
  156. if(DEFINED PYTHON_INCLUDE_PATH AND NOT DEFINED PYTHON_INCLUDE_DIR)
  157. set(PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_PATH}" CACHE PATH
  158. "Path to where Python.h is found" FORCE)
  159. endif()
  160. set(PYTHON_FRAMEWORK_INCLUDES)
  161. if(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
  162. foreach(dir ${Python_FRAMEWORKS})
  163. set(PYTHON_FRAMEWORK_INCLUDES ${PYTHON_FRAMEWORK_INCLUDES}
  164. ${dir}/Versions/${_CURRENT_VERSION}/include/python${_CURRENT_VERSION})
  165. endforeach()
  166. endif()
  167. if(MINGW)
  168. find_path(PYTHON_INCLUDE_DIR
  169. NAMES Python.h
  170. PATHS
  171. "${PYTHON_ROOT_DIR}"
  172. "C:/python${_CURRENT_VERSION}"
  173. "C:/python${_CURRENT_VERSION_NOT_DOTS}"
  174. PATH_SUFFIXES
  175. include
  176. python${_CURRENT_VERSION}
  177. python${_CURRENT_VERSION_NOT_DOTS}
  178. )
  179. elseif(VCPKG_TOOLCHAIN)
  180. find_path(PYTHON_INCLUDE_DIR
  181. NAMES Python.h
  182. PATHS
  183. "${PYTHON_ROOT_DIR}"
  184. PATH_SUFFIXES
  185. include
  186. python${_CURRENT_VERSION}
  187. python${_CURRENT_VERSION_NOT_DOTS}
  188. NO_SYSTEM_ENVIRONMENT_PATH
  189. )
  190. else()
  191. find_path(PYTHON_INCLUDE_DIR
  192. NAMES Python.h
  193. PATHS
  194. ${PYTHON_FRAMEWORK_INCLUDES}
  195. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  196. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  197. PATH_SUFFIXES
  198. python${_CURRENT_VERSION}mu
  199. python${_CURRENT_VERSION}m
  200. python${_CURRENT_VERSION}u
  201. python${_CURRENT_VERSION}
  202. )
  203. endif()
  204. # For backward compatibility, set PYTHON_INCLUDE_PATH.
  205. set(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")
  206. if(PYTHON_INCLUDE_DIR AND EXISTS "${PYTHON_INCLUDE_DIR}/patchlevel.h")
  207. file(STRINGS "${PYTHON_INCLUDE_DIR}/patchlevel.h" python_version_str
  208. REGEX "^#define[ \t]+PY_VERSION[ \t]+\"[^\"]+\"")
  209. string(REGEX REPLACE "^#define[ \t]+PY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
  210. PYTHONLIBS_VERSION_STRING "${python_version_str}")
  211. unset(python_version_str)
  212. endif()
  213. if(PYTHON_LIBRARY AND PYTHON_INCLUDE_DIR)
  214. break()
  215. endif()
  216. endforeach()
  217. mark_as_advanced(
  218. PYTHON_DEBUG_LIBRARY
  219. PYTHON_LIBRARY
  220. PYTHON_INCLUDE_DIR
  221. )
  222. # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
  223. # cache entries because they are meant to specify the location of a single
  224. # library. We now set the variables listed by the documentation for this
  225. # module.
  226. set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
  227. set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
  228. # These variables have been historically named in this module different from
  229. # what SELECT_LIBRARY_CONFIGURATIONS() expects.
  230. set(PYTHON_LIBRARY_DEBUG "${PYTHON_DEBUG_LIBRARY}")
  231. set(PYTHON_LIBRARY_RELEASE "${PYTHON_LIBRARY}")
  232. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  233. select_library_configurations(PYTHON)
  234. # SELECT_LIBRARY_CONFIGURATIONS() sets ${PREFIX}_FOUND if it has a library.
  235. # Unset this, this prefix doesn't match the module prefix, they are different
  236. # for historical reasons.
  237. unset(PYTHON_FOUND)
  238. include(FindPackageHandleStandardArgs)
  239. find_package_handle_standard_args(PythonLibs
  240. REQUIRED_VARS PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS
  241. VERSION_VAR PYTHONLIBS_VERSION_STRING)
  242. # PYTHON_ADD_MODULE(<name> src1 src2 ... srcN) is used to build modules for python.
  243. # PYTHON_WRITE_MODULES_HEADER(<filename>) writes a header file you can include
  244. # in your sources to initialize the static python modules
  245. function(PYTHON_ADD_MODULE _NAME )
  246. get_property(_TARGET_SUPPORTS_SHARED_LIBS
  247. GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
  248. option(PYTHON_ENABLE_MODULE_${_NAME} "Add module ${_NAME}" TRUE)
  249. option(PYTHON_MODULE_${_NAME}_BUILD_SHARED
  250. "Add module ${_NAME} shared" ${_TARGET_SUPPORTS_SHARED_LIBS})
  251. # Mark these options as advanced
  252. mark_as_advanced(PYTHON_ENABLE_MODULE_${_NAME}
  253. PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  254. if(PYTHON_ENABLE_MODULE_${_NAME})
  255. if(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  256. set(PY_MODULE_TYPE MODULE)
  257. else()
  258. set(PY_MODULE_TYPE STATIC)
  259. set_property(GLOBAL APPEND PROPERTY PY_STATIC_MODULES_LIST ${_NAME})
  260. endif()
  261. set_property(GLOBAL APPEND PROPERTY PY_MODULES_LIST ${_NAME})
  262. add_library(${_NAME} ${PY_MODULE_TYPE} ${ARGN})
  263. # target_link_libraries(${_NAME} ${PYTHON_LIBRARIES})
  264. if(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  265. set_target_properties(${_NAME} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
  266. if(WIN32 AND NOT CYGWIN)
  267. set_target_properties(${_NAME} PROPERTIES SUFFIX ".pyd")
  268. endif()
  269. endif()
  270. endif()
  271. endfunction()
  272. function(PYTHON_WRITE_MODULES_HEADER _filename)
  273. get_property(PY_STATIC_MODULES_LIST GLOBAL PROPERTY PY_STATIC_MODULES_LIST)
  274. get_filename_component(_name "${_filename}" NAME)
  275. string(REPLACE "." "_" _name "${_name}")
  276. string(TOUPPER ${_name} _nameUpper)
  277. set(_filename ${CMAKE_CURRENT_BINARY_DIR}/${_filename})
  278. set(_filenameTmp "${_filename}.in")
  279. file(WRITE ${_filenameTmp} "/*Created by cmake, do not edit, changes will be lost*/\n")
  280. file(APPEND ${_filenameTmp}
  281. "#ifndef ${_nameUpper}
  282. #define ${_nameUpper}
  283. #include <Python.h>
  284. #ifdef __cplusplus
  285. extern \"C\" {
  286. #endif /* __cplusplus */
  287. ")
  288. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  289. file(APPEND ${_filenameTmp} "extern void init${PYTHON_MODULE_PREFIX}${_currentModule}(void);\n\n")
  290. endforeach()
  291. file(APPEND ${_filenameTmp}
  292. "#ifdef __cplusplus
  293. }
  294. #endif /* __cplusplus */
  295. ")
  296. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  297. file(APPEND ${_filenameTmp} "int ${_name}_${_currentModule}(void) \n{\n static char name[]=\"${PYTHON_MODULE_PREFIX}${_currentModule}\"; return PyImport_AppendInittab(name, init${PYTHON_MODULE_PREFIX}${_currentModule});\n}\n\n")
  298. endforeach()
  299. file(APPEND ${_filenameTmp} "void ${_name}_LoadAllPythonModules(void)\n{\n")
  300. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  301. file(APPEND ${_filenameTmp} " ${_name}_${_currentModule}();\n")
  302. endforeach()
  303. file(APPEND ${_filenameTmp} "}\n\n")
  304. file(APPEND ${_filenameTmp} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n ${_name}_LoadAllPythonModules();\n}\n#endif\n\n#endif\n")
  305. # with configure_file() cmake complains that you may not use a file created using file(WRITE) as input file for configure_file()
  306. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_filenameTmp}" "${_filename}" OUTPUT_QUIET ERROR_QUIET)
  307. endfunction()