FindPythonLibs.cmake 11 KB

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