FindPythonLibs.cmake 11 KB

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