FindPythonLibs.cmake 12 KB

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