FindPythonLibs.cmake 13 KB

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