FindPythonLibs.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. HINTS ${_Python_LIBRARY_PATH_HINT}
  117. PATHS
  118. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  119. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  120. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  121. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  122. )
  123. endif()
  124. set(PYTHON_FRAMEWORK_LIBRARIES)
  125. if(Python_FRAMEWORKS AND NOT PYTHON_LIBRARY)
  126. foreach(dir ${Python_FRAMEWORKS})
  127. list(APPEND PYTHON_FRAMEWORK_LIBRARIES
  128. ${dir}/Versions/${_CURRENT_VERSION}/lib)
  129. endforeach()
  130. endif()
  131. find_library(PYTHON_LIBRARY
  132. NAMES
  133. python${_CURRENT_VERSION_NO_DOTS}
  134. python${_CURRENT_VERSION}mu
  135. python${_CURRENT_VERSION}m
  136. python${_CURRENT_VERSION}u
  137. python${_CURRENT_VERSION}
  138. HINTS
  139. ${_Python_LIBRARY_PATH_HINT}
  140. PATHS
  141. ${PYTHON_FRAMEWORK_LIBRARIES}
  142. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  143. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  144. # Avoid finding the .dll in the PATH. We want the .lib.
  145. NO_SYSTEM_ENVIRONMENT_PATH
  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. # Avoid finding the .dll in the PATH. We want the .lib.
  151. NO_SYSTEM_ENVIRONMENT_PATH
  152. # This is where the static library is usually located
  153. PATH_SUFFIXES python${_CURRENT_VERSION}/config
  154. )
  155. # Don't search for include dir until library location is known
  156. if(PYTHON_LIBRARY)
  157. # Use the library's install prefix as a hint
  158. set(_Python_INCLUDE_PATH_HINT)
  159. # PYTHON_LIBRARY may contain a list because of SelectLibraryConfigurations
  160. # which may have been run previously. If it is the case, the list can be:
  161. # optimized;<FILEPATH_TO_RELEASE_LIBRARY>;debug;<FILEPATH_TO_DEBUG_LIBRARY>
  162. foreach(lib ${PYTHON_LIBRARY} ${PYTHON_DEBUG_LIBRARY})
  163. if(IS_ABSOLUTE "${lib}")
  164. get_filename_component(_Python_PREFIX "${lib}" PATH)
  165. get_filename_component(_Python_PREFIX "${_Python_PREFIX}" PATH)
  166. if(_Python_PREFIX)
  167. list(APPEND _Python_INCLUDE_PATH_HINT ${_Python_PREFIX}/include)
  168. endif()
  169. unset(_Python_PREFIX)
  170. endif()
  171. endforeach()
  172. # Add framework directories to the search paths
  173. set(PYTHON_FRAMEWORK_INCLUDES)
  174. if(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
  175. foreach(dir ${Python_FRAMEWORKS})
  176. list(APPEND PYTHON_FRAMEWORK_INCLUDES
  177. ${dir}/Versions/${_CURRENT_VERSION}/include)
  178. endforeach()
  179. endif()
  180. find_path(PYTHON_INCLUDE_DIR
  181. NAMES Python.h
  182. HINTS
  183. ${_Python_INCLUDE_PATH_HINT}
  184. PATHS
  185. ${PYTHON_FRAMEWORK_INCLUDES}
  186. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  187. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  188. PATH_SUFFIXES
  189. python${_CURRENT_VERSION}mu
  190. python${_CURRENT_VERSION}m
  191. python${_CURRENT_VERSION}u
  192. python${_CURRENT_VERSION}
  193. )
  194. endif()
  195. # For backward compatibility, set PYTHON_INCLUDE_PATH.
  196. set(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")
  197. if(PYTHON_INCLUDE_DIR AND EXISTS "${PYTHON_INCLUDE_DIR}/patchlevel.h")
  198. file(STRINGS "${PYTHON_INCLUDE_DIR}/patchlevel.h" python_version_str
  199. REGEX "^#define[ \t]+PY_VERSION[ \t]+\"[^\"]+\"")
  200. string(REGEX REPLACE "^#define[ \t]+PY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
  201. PYTHONLIBS_VERSION_STRING "${python_version_str}")
  202. unset(python_version_str)
  203. endif()
  204. if(PYTHON_LIBRARY AND PYTHON_INCLUDE_DIR)
  205. break()
  206. endif()
  207. endforeach()
  208. unset(_Python_INCLUDE_PATH_HINT)
  209. unset(_Python_LIBRARY_PATH_HINT)
  210. mark_as_advanced(
  211. PYTHON_DEBUG_LIBRARY
  212. PYTHON_LIBRARY
  213. PYTHON_INCLUDE_DIR
  214. )
  215. # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
  216. # cache entries because they are meant to specify the location of a single
  217. # library. We now set the variables listed by the documentation for this
  218. # module.
  219. set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
  220. set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
  221. # These variables have been historically named in this module different from
  222. # what SELECT_LIBRARY_CONFIGURATIONS() expects.
  223. set(PYTHON_LIBRARY_DEBUG "${PYTHON_DEBUG_LIBRARY}")
  224. set(PYTHON_LIBRARY_RELEASE "${PYTHON_LIBRARY}")
  225. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  226. SELECT_LIBRARY_CONFIGURATIONS(PYTHON)
  227. # SELECT_LIBRARY_CONFIGURATIONS() sets ${PREFIX}_FOUND if it has a library.
  228. # Unset this, this prefix doesn't match the module prefix, they are different
  229. # for historical reasons.
  230. unset(PYTHON_FOUND)
  231. # Restore CMAKE_FIND_FRAMEWORK
  232. if(DEFINED _PythonLibs_CMAKE_FIND_FRAMEWORK)
  233. set(CMAKE_FIND_FRAMEWORK ${_PythonLibs_CMAKE_FIND_FRAMEWORK})
  234. unset(_PythonLibs_CMAKE_FIND_FRAMEWORK)
  235. else()
  236. unset(CMAKE_FIND_FRAMEWORK)
  237. endif()
  238. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  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()