FindPythonLibs.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. set(PYTHON_FRAMEWORK_INCLUDES)
  144. if(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
  145. foreach(dir ${Python_FRAMEWORKS})
  146. list(APPEND PYTHON_FRAMEWORK_INCLUDES
  147. ${dir}/Versions/${_CURRENT_VERSION}/include)
  148. endforeach()
  149. endif()
  150. find_path(PYTHON_INCLUDE_DIR
  151. NAMES Python.h
  152. PATHS
  153. ${PYTHON_FRAMEWORK_INCLUDES}
  154. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  155. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  156. PATH_SUFFIXES
  157. python${_CURRENT_VERSION}mu
  158. python${_CURRENT_VERSION}m
  159. python${_CURRENT_VERSION}u
  160. python${_CURRENT_VERSION}
  161. )
  162. # For backward compatibility, set PYTHON_INCLUDE_PATH.
  163. set(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")
  164. if(PYTHON_INCLUDE_DIR AND EXISTS "${PYTHON_INCLUDE_DIR}/patchlevel.h")
  165. file(STRINGS "${PYTHON_INCLUDE_DIR}/patchlevel.h" python_version_str
  166. REGEX "^#define[ \t]+PY_VERSION[ \t]+\"[^\"]+\"")
  167. string(REGEX REPLACE "^#define[ \t]+PY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
  168. PYTHONLIBS_VERSION_STRING "${python_version_str}")
  169. unset(python_version_str)
  170. endif()
  171. if(PYTHON_LIBRARY AND PYTHON_INCLUDE_DIR)
  172. break()
  173. endif()
  174. endforeach()
  175. mark_as_advanced(
  176. PYTHON_DEBUG_LIBRARY
  177. PYTHON_LIBRARY
  178. PYTHON_INCLUDE_DIR
  179. )
  180. # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
  181. # cache entries because they are meant to specify the location of a single
  182. # library. We now set the variables listed by the documentation for this
  183. # module.
  184. set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
  185. set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
  186. # These variables have been historically named in this module different from
  187. # what SELECT_LIBRARY_CONFIGURATIONS() expects.
  188. set(PYTHON_LIBRARY_DEBUG "${PYTHON_DEBUG_LIBRARY}")
  189. set(PYTHON_LIBRARY_RELEASE "${PYTHON_LIBRARY}")
  190. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  191. SELECT_LIBRARY_CONFIGURATIONS(PYTHON)
  192. # SELECT_LIBRARY_CONFIGURATIONS() sets ${PREFIX}_FOUND if it has a library.
  193. # Unset this, this prefix doesn't match the module prefix, they are different
  194. # for historical reasons.
  195. unset(PYTHON_FOUND)
  196. # Restore CMAKE_FIND_FRAMEWORK
  197. if(DEFINED _PythonLibs_CMAKE_FIND_FRAMEWORK)
  198. set(CMAKE_FIND_FRAMEWORK ${_PythonLibs_CMAKE_FIND_FRAMEWORK})
  199. unset(_PythonLibs_CMAKE_FIND_FRAMEWORK)
  200. else()
  201. unset(CMAKE_FIND_FRAMEWORK)
  202. endif()
  203. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  204. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibs
  205. REQUIRED_VARS PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS
  206. VERSION_VAR PYTHONLIBS_VERSION_STRING)
  207. # PYTHON_ADD_MODULE(<name> src1 src2 ... srcN) is used to build modules for python.
  208. # PYTHON_WRITE_MODULES_HEADER(<filename>) writes a header file you can include
  209. # in your sources to initialize the static python modules
  210. function(PYTHON_ADD_MODULE _NAME )
  211. get_property(_TARGET_SUPPORTS_SHARED_LIBS
  212. GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
  213. option(PYTHON_ENABLE_MODULE_${_NAME} "Add module ${_NAME}" TRUE)
  214. option(PYTHON_MODULE_${_NAME}_BUILD_SHARED
  215. "Add module ${_NAME} shared" ${_TARGET_SUPPORTS_SHARED_LIBS})
  216. # Mark these options as advanced
  217. mark_as_advanced(PYTHON_ENABLE_MODULE_${_NAME}
  218. PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  219. if(PYTHON_ENABLE_MODULE_${_NAME})
  220. if(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  221. set(PY_MODULE_TYPE MODULE)
  222. else()
  223. set(PY_MODULE_TYPE STATIC)
  224. set_property(GLOBAL APPEND PROPERTY PY_STATIC_MODULES_LIST ${_NAME})
  225. endif()
  226. set_property(GLOBAL APPEND PROPERTY PY_MODULES_LIST ${_NAME})
  227. add_library(${_NAME} ${PY_MODULE_TYPE} ${ARGN})
  228. # target_link_libraries(${_NAME} ${PYTHON_LIBRARIES})
  229. if(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  230. set_target_properties(${_NAME} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
  231. if(WIN32 AND NOT CYGWIN)
  232. set_target_properties(${_NAME} PROPERTIES SUFFIX ".pyd")
  233. endif()
  234. endif()
  235. endif()
  236. endfunction()
  237. function(PYTHON_WRITE_MODULES_HEADER _filename)
  238. get_property(PY_STATIC_MODULES_LIST GLOBAL PROPERTY PY_STATIC_MODULES_LIST)
  239. get_filename_component(_name "${_filename}" NAME)
  240. string(REPLACE "." "_" _name "${_name}")
  241. string(TOUPPER ${_name} _nameUpper)
  242. set(_filename ${CMAKE_CURRENT_BINARY_DIR}/${_filename})
  243. set(_filenameTmp "${_filename}.in")
  244. file(WRITE ${_filenameTmp} "/*Created by cmake, do not edit, changes will be lost*/\n")
  245. file(APPEND ${_filenameTmp}
  246. "#ifndef ${_nameUpper}
  247. #define ${_nameUpper}
  248. #include <Python.h>
  249. #ifdef __cplusplus
  250. extern \"C\" {
  251. #endif /* __cplusplus */
  252. ")
  253. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  254. file(APPEND ${_filenameTmp} "extern void init${PYTHON_MODULE_PREFIX}${_currentModule}(void);\n\n")
  255. endforeach()
  256. file(APPEND ${_filenameTmp}
  257. "#ifdef __cplusplus
  258. }
  259. #endif /* __cplusplus */
  260. ")
  261. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  262. 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")
  263. endforeach()
  264. file(APPEND ${_filenameTmp} "void ${_name}_LoadAllPythonModules(void)\n{\n")
  265. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  266. file(APPEND ${_filenameTmp} " ${_name}_${_currentModule}();\n")
  267. endforeach()
  268. file(APPEND ${_filenameTmp} "}\n\n")
  269. file(APPEND ${_filenameTmp} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n ${_name}_LoadAllPythonModules();\n}\n#endif\n\n#endif\n")
  270. # with configure_file() cmake complains that you may not use a file created using file(WRITE) as input file for configure_file()
  271. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_filenameTmp}" "${_filename}" OUTPUT_QUIET ERROR_QUIET)
  272. endfunction()