FindPythonLibs.cmake 13 KB

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