FindPythonLibs.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. # Use the executable's path as a hint
  51. set(_Python_LIBRARY_PATH_HINT)
  52. if(PYTHON_EXECUTABLE)
  53. if(WIN32)
  54. get_filename_component(_PREFIX ${PYTHON_EXECUTABLE} PATH)
  55. if(_PREFIX)
  56. set(_Python_LIBRARY_PATH_HINT ${_PREFIX}/libs)
  57. endif()
  58. else()
  59. get_filename_component(_PREFIX ${PYTHON_EXECUTABLE} PATH)
  60. get_filename_component(_PREFIX ${_PREFIX} PATH)
  61. if(_PREFIX)
  62. set(_Python_LIBRARY_PATH_HINT ${_PREFIX}/lib)
  63. endif()
  64. endif()
  65. endif()
  66. include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindFrameworks.cmake)
  67. # Search for the python framework on Apple.
  68. CMAKE_FIND_FRAMEWORKS(Python)
  69. # Save CMAKE_FIND_FRAMEWORK
  70. if(DEFINED CMAKE_FIND_FRAMEWORK)
  71. set(_PythonLibs_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
  72. else()
  73. unset(_PythonLibs_CMAKE_FIND_FRAMEWORK)
  74. endif()
  75. # To avoid picking up the system Python.h pre-maturely.
  76. set(CMAKE_FIND_FRAMEWORK LAST)
  77. set(_PYTHON1_VERSIONS 1.6 1.5)
  78. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  79. set(_PYTHON3_VERSIONS 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
  80. if(PythonLibs_FIND_VERSION)
  81. if(PythonLibs_FIND_VERSION_COUNT GREATER 1)
  82. set(_PYTHON_FIND_MAJ_MIN "${PythonLibs_FIND_VERSION_MAJOR}.${PythonLibs_FIND_VERSION_MINOR}")
  83. unset(_PYTHON_FIND_OTHER_VERSIONS)
  84. if(PythonLibs_FIND_VERSION_EXACT)
  85. if(_PYTHON_FIND_MAJ_MIN STREQUAL PythonLibs_FIND_VERSION)
  86. set(_PYTHON_FIND_OTHER_VERSIONS "${PythonLibs_FIND_VERSION}")
  87. else()
  88. set(_PYTHON_FIND_OTHER_VERSIONS "${PythonLibs_FIND_VERSION}" "${_PYTHON_FIND_MAJ_MIN}")
  89. endif()
  90. else()
  91. foreach(_PYTHON_V ${_PYTHON${PythonLibs_FIND_VERSION_MAJOR}_VERSIONS})
  92. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  93. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  94. endif()
  95. endforeach()
  96. endif()
  97. unset(_PYTHON_FIND_MAJ_MIN)
  98. else()
  99. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonLibs_FIND_VERSION_MAJOR}_VERSIONS})
  100. endif()
  101. else()
  102. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  103. endif()
  104. # Set up the versions we know about, in the order we will search. Always add
  105. # the user supplied additional versions to the front.
  106. # If FindPythonInterp has already found the major and minor version,
  107. # insert that version between the user supplied versions and the stock
  108. # version list.
  109. set(_Python_VERSIONS ${Python_ADDITIONAL_VERSIONS})
  110. if(DEFINED PYTHON_VERSION_MAJOR AND DEFINED PYTHON_VERSION_MINOR)
  111. list(APPEND _Python_VERSIONS ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
  112. endif()
  113. list(APPEND _Python_VERSIONS ${_PYTHON_FIND_OTHER_VERSIONS})
  114. unset(_PYTHON_FIND_OTHER_VERSIONS)
  115. unset(_PYTHON1_VERSIONS)
  116. unset(_PYTHON2_VERSIONS)
  117. unset(_PYTHON3_VERSIONS)
  118. foreach(_CURRENT_VERSION ${_Python_VERSIONS})
  119. string(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
  120. if(WIN32)
  121. find_library(PYTHON_DEBUG_LIBRARY
  122. NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
  123. HINTS ${_Python_LIBRARY_PATH_HINT}
  124. PATHS
  125. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  126. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  127. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  128. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  129. )
  130. endif()
  131. set(PYTHON_FRAMEWORK_LIBRARIES)
  132. if(Python_FRAMEWORKS AND NOT PYTHON_LIBRARY)
  133. foreach(dir ${Python_FRAMEWORKS})
  134. list(APPEND PYTHON_FRAMEWORK_LIBRARIES
  135. ${dir}/Versions/${_CURRENT_VERSION}/lib)
  136. endforeach()
  137. endif()
  138. find_library(PYTHON_LIBRARY
  139. NAMES
  140. python${_CURRENT_VERSION_NO_DOTS}
  141. python${_CURRENT_VERSION}mu
  142. python${_CURRENT_VERSION}m
  143. python${_CURRENT_VERSION}u
  144. python${_CURRENT_VERSION}
  145. HINTS
  146. ${_Python_LIBRARY_PATH_HINT}
  147. PATHS
  148. ${PYTHON_FRAMEWORK_LIBRARIES}
  149. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  150. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  151. # Avoid finding the .dll in the PATH. We want the .lib.
  152. NO_SYSTEM_ENVIRONMENT_PATH
  153. )
  154. # Look for the static library in the Python config directory
  155. find_library(PYTHON_LIBRARY
  156. NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
  157. # Avoid finding the .dll in the PATH. We want the .lib.
  158. NO_SYSTEM_ENVIRONMENT_PATH
  159. # This is where the static library is usually located
  160. PATH_SUFFIXES python${_CURRENT_VERSION}/config
  161. )
  162. # Don't search for include dir until library location is known
  163. if(PYTHON_LIBRARY)
  164. # Use the library's install prefix as a hint
  165. set(_Python_INCLUDE_PATH_HINT)
  166. get_filename_component(_PREFIX ${PYTHON_LIBRARY} PATH)
  167. get_filename_component(_PREFIX ${_PREFIX} PATH)
  168. if(_PREFIX)
  169. set(_Python_INCLUDE_PATH_HINT ${_PREFIX}/include)
  170. endif()
  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()