You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
12 KiB

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