1
0

FindPythonLibs.cmake 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindPythonLibs
  5. --------------
  6. .. versionchanged:: 3.27
  7. This module is available only if policy :policy:`CMP0148` is not set to ``NEW``.
  8. .. deprecated:: 3.12
  9. Use :module:`FindPython3`, :module:`FindPython2`, or :module:`FindPython`
  10. instead.
  11. Finds the Python installation and determines the location of its include
  12. directories and libraries, as well as the name of the Python library to
  13. link against:
  14. .. code-block:: cmake
  15. find_package(PythonLibs [<version>] [...])
  16. .. note::
  17. When using both this and the :module:`FindPythonInterp` module, call
  18. ``find_package(PythonInterp)`` before ``find_package(PythonLibs)``. This
  19. ensures that the detected interpreter version is used to guide the selection
  20. of compatible libraries, resulting in a consistent ``PYTHON_LIBRARIES`` value.
  21. Result Variables
  22. ^^^^^^^^^^^^^^^^
  23. This module defines the following variables:
  24. ``PythonLibs_FOUND``
  25. Boolean indicating whether the (requested version of) Python libraries have
  26. been found. For backward compatibility, the ``PYTHONLIBS_FOUND`` variable is
  27. also set to the same value.
  28. ``PYTHONLIBS_VERSION_STRING``
  29. The version of the Python libraries found.
  30. ``PYTHON_LIBRARIES``
  31. Libraries needed to link against to use Python.
  32. ``PYTHON_INCLUDE_DIRS``
  33. Include directories needed to use Python.
  34. Cache Variables
  35. ^^^^^^^^^^^^^^^
  36. The following cache variables may also be set to specify the Python installation
  37. to use:
  38. ``PYTHON_LIBRARY``
  39. The path to the Python library.
  40. ``PYTHON_INCLUDE_DIR``
  41. The directory containing the ``Python.h`` header file.
  42. Hints
  43. ^^^^^
  44. This module accepts the following variables before calling
  45. ``find_package(PythonLibs)``:
  46. ``Python_ADDITIONAL_VERSIONS``
  47. This variable can be used to specify a list of version numbers that should be
  48. taken into account when searching for Python.
  49. Deprecated Variables
  50. ^^^^^^^^^^^^^^^^^^^^
  51. These variables are provided for backward compatibility:
  52. ``PYTHON_DEBUG_LIBRARIES``
  53. .. deprecated:: 2.8.8
  54. Use ``PYTHON_LIBRARIES`` instead.
  55. Result variable that holds the path to the debug library.
  56. ``PYTHON_INCLUDE_PATH``
  57. .. deprecated:: 2.8.0
  58. Use ``PYTHON_INCLUDE_DIR`` or ``PYTHON_INCLUDE_DIRS`` instead.
  59. Result variable that holds the path to the directory containing the
  60. ``Python.h`` header file.
  61. Examples
  62. ^^^^^^^^
  63. In earlier versions of CMake, Python libraries were found and used in a project
  64. like this:
  65. .. code-block:: cmake
  66. find_package(PythonLibs)
  67. target_link_libraries(app PRIVATE ${PYTHON_LIBRARIES})
  68. target_include_directories(app PRIVATE ${PYTHON_INCLUDE_DIRS})
  69. Starting with CMake 3.12, Python libraries can be found using the
  70. :module:`FindPython` module. The equivalent example using the modern approach
  71. with an imported target is:
  72. .. code-block:: cmake
  73. find_package(Python COMPONENTS Development)
  74. target_link_libraries(app PRIVATE Python::Python)
  75. #]=======================================================================]
  76. cmake_policy(PUSH)
  77. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  78. cmake_policy(GET CMP0148 _FindPythonLibs_CMP0148)
  79. if(_FindPythonLibs_CMP0148 STREQUAL "NEW")
  80. message(FATAL_ERROR "The FindPythonLibs module has been removed by policy CMP0148.")
  81. endif()
  82. if(_FindPythonLibs_testing)
  83. set(_FindPythonLibs_included TRUE)
  84. cmake_policy(POP)
  85. return()
  86. endif()
  87. # Use the executable's path as a hint
  88. set(_Python_LIBRARY_PATH_HINT)
  89. if(IS_ABSOLUTE "${PYTHON_EXECUTABLE}")
  90. if(WIN32)
  91. get_filename_component(_Python_PREFIX "${PYTHON_EXECUTABLE}" PATH)
  92. if(_Python_PREFIX)
  93. set(_Python_LIBRARY_PATH_HINT ${_Python_PREFIX}/libs)
  94. endif()
  95. unset(_Python_PREFIX)
  96. else()
  97. get_filename_component(_Python_PREFIX "${PYTHON_EXECUTABLE}" PATH)
  98. get_filename_component(_Python_PREFIX "${_Python_PREFIX}" PATH)
  99. if(_Python_PREFIX)
  100. set(_Python_LIBRARY_PATH_HINT ${_Python_PREFIX}/lib)
  101. endif()
  102. unset(_Python_PREFIX)
  103. endif()
  104. endif()
  105. block(SCOPE_FOR POLICIES)
  106. cmake_policy(SET CMP0173 OLD)
  107. include(${CMAKE_CURRENT_LIST_DIR}/CMakeFindFrameworks.cmake)
  108. endblock()
  109. # Search for the python framework on Apple.
  110. CMAKE_FIND_FRAMEWORKS(Python)
  111. # Save CMAKE_FIND_FRAMEWORK
  112. if(DEFINED CMAKE_FIND_FRAMEWORK)
  113. set(_PythonLibs_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
  114. else()
  115. unset(_PythonLibs_CMAKE_FIND_FRAMEWORK)
  116. endif()
  117. # To avoid picking up the system Python.h pre-maturely.
  118. set(CMAKE_FIND_FRAMEWORK LAST)
  119. set(_PYTHON1_VERSIONS 1.6 1.5)
  120. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  121. set(_PYTHON3_VERSIONS 3.14 3.13 3.12 3.11 3.10 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
  122. if(PythonLibs_FIND_VERSION)
  123. if(PythonLibs_FIND_VERSION_COUNT GREATER 1)
  124. set(_PYTHON_FIND_MAJ_MIN "${PythonLibs_FIND_VERSION_MAJOR}.${PythonLibs_FIND_VERSION_MINOR}")
  125. unset(_PYTHON_FIND_OTHER_VERSIONS)
  126. if(PythonLibs_FIND_VERSION_EXACT)
  127. if(_PYTHON_FIND_MAJ_MIN STREQUAL PythonLibs_FIND_VERSION)
  128. set(_PYTHON_FIND_OTHER_VERSIONS "${PythonLibs_FIND_VERSION}")
  129. else()
  130. set(_PYTHON_FIND_OTHER_VERSIONS "${PythonLibs_FIND_VERSION}" "${_PYTHON_FIND_MAJ_MIN}")
  131. endif()
  132. else()
  133. foreach(_PYTHON_V ${_PYTHON${PythonLibs_FIND_VERSION_MAJOR}_VERSIONS})
  134. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  135. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  136. endif()
  137. endforeach()
  138. endif()
  139. unset(_PYTHON_FIND_MAJ_MIN)
  140. else()
  141. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonLibs_FIND_VERSION_MAJOR}_VERSIONS})
  142. endif()
  143. else()
  144. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  145. endif()
  146. # Set up the versions we know about, in the order we will search. Always add
  147. # the user supplied additional versions to the front.
  148. # If FindPythonInterp has already found the major and minor version,
  149. # insert that version between the user supplied versions and the stock
  150. # version list.
  151. set(_Python_VERSIONS ${Python_ADDITIONAL_VERSIONS})
  152. if(DEFINED PYTHON_VERSION_MAJOR AND DEFINED PYTHON_VERSION_MINOR)
  153. list(APPEND _Python_VERSIONS ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
  154. endif()
  155. list(APPEND _Python_VERSIONS ${_PYTHON_FIND_OTHER_VERSIONS})
  156. unset(_PYTHON_FIND_OTHER_VERSIONS)
  157. unset(_PYTHON1_VERSIONS)
  158. unset(_PYTHON2_VERSIONS)
  159. unset(_PYTHON3_VERSIONS)
  160. # Python distribution: define which architectures can be used
  161. if (CMAKE_SIZEOF_VOID_P)
  162. # In this case, search only for 64bit or 32bit
  163. math (EXPR _PYTHON_ARCH "${CMAKE_SIZEOF_VOID_P} * 8")
  164. set (_PYTHON_ARCH2 _PYTHON_PREFIX_ARCH})
  165. else()
  166. if (PYTHON_EXECUTABLE)
  167. # determine interpreter architecture
  168. execute_process (COMMAND "${PYTHON_EXECUTABLE}" -c "import sys; print(sys.maxsize > 2**32)"
  169. RESULT_VARIABLE _PYTHON_RESULT
  170. OUTPUT_VARIABLE _PYTHON_IS64BIT
  171. ERROR_VARIABLE _PYTHON_IS64BIT)
  172. if (NOT _PYTHON_RESULT)
  173. if (_PYTHON_IS64BIT)
  174. set (_PYTHON_ARCH 64)
  175. set (_PYTHON_ARCH2 64)
  176. else()
  177. set (_PYTHON_ARCH 32)
  178. set (_PYTHON_ARCH2 32)
  179. endif()
  180. endif()
  181. else()
  182. # architecture unknown, search for both 64bit and 32bit
  183. set (_PYTHON_ARCH 64)
  184. set (_PYTHON_ARCH2 32)
  185. endif()
  186. endif()
  187. foreach(_CURRENT_VERSION ${_Python_VERSIONS})
  188. string(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
  189. if(WIN32)
  190. find_library(PYTHON_DEBUG_LIBRARY
  191. NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
  192. NAMES_PER_DIR
  193. HINTS ${_Python_LIBRARY_PATH_HINT}
  194. PATHS
  195. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  196. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/libs/Debug
  197. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/libs/Debug
  198. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  199. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/libs/Debug
  200. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/libs/Debug
  201. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  202. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/libs
  203. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/libs
  204. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  205. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/libs
  206. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/libs
  207. )
  208. endif()
  209. set(PYTHON_FRAMEWORK_LIBRARIES)
  210. if(Python_FRAMEWORKS AND NOT PYTHON_LIBRARY)
  211. foreach(dir ${Python_FRAMEWORKS})
  212. list(APPEND PYTHON_FRAMEWORK_LIBRARIES
  213. ${dir}/Versions/${_CURRENT_VERSION}/lib)
  214. endforeach()
  215. endif()
  216. find_library(PYTHON_LIBRARY
  217. NAMES
  218. python${_CURRENT_VERSION_NO_DOTS}
  219. python${_CURRENT_VERSION}mu
  220. python${_CURRENT_VERSION}m
  221. python${_CURRENT_VERSION}u
  222. python${_CURRENT_VERSION}
  223. NAMES_PER_DIR
  224. HINTS
  225. ${_Python_LIBRARY_PATH_HINT}
  226. PATHS
  227. ${PYTHON_FRAMEWORK_LIBRARIES}
  228. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  229. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/libs
  230. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/libs
  231. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  232. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/libs
  233. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/libs
  234. )
  235. # Look for the static library in the Python config directory
  236. find_library(PYTHON_LIBRARY
  237. NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
  238. NAMES_PER_DIR
  239. # This is where the static library is usually located
  240. PATH_SUFFIXES python${_CURRENT_VERSION}/config
  241. )
  242. # Don't search for include dir until library location is known
  243. if(PYTHON_LIBRARY)
  244. # Use the library's install prefix as a hint
  245. set(_Python_INCLUDE_PATH_HINT)
  246. # PYTHON_LIBRARY may contain a list because of SelectLibraryConfigurations
  247. # which may have been run previously. If it is the case, the list can be:
  248. # optimized;<FILEPATH_TO_RELEASE_LIBRARY>;debug;<FILEPATH_TO_DEBUG_LIBRARY>
  249. foreach(lib ${PYTHON_LIBRARY} ${PYTHON_DEBUG_LIBRARY})
  250. if(IS_ABSOLUTE "${lib}")
  251. get_filename_component(_Python_PREFIX "${lib}" PATH)
  252. get_filename_component(_Python_PREFIX "${_Python_PREFIX}" PATH)
  253. if(_Python_PREFIX)
  254. list(APPEND _Python_INCLUDE_PATH_HINT ${_Python_PREFIX}/include)
  255. endif()
  256. unset(_Python_PREFIX)
  257. endif()
  258. endforeach()
  259. # Add framework directories to the search paths
  260. set(PYTHON_FRAMEWORK_INCLUDES)
  261. if(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
  262. foreach(dir ${Python_FRAMEWORKS})
  263. list(APPEND PYTHON_FRAMEWORK_INCLUDES
  264. ${dir}/Versions/${_CURRENT_VERSION}/include)
  265. endforeach()
  266. endif()
  267. find_path(PYTHON_INCLUDE_DIR
  268. NAMES Python.h
  269. HINTS
  270. ${_Python_INCLUDE_PATH_HINT}
  271. PATHS
  272. ${PYTHON_FRAMEWORK_INCLUDES}
  273. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  274. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/include
  275. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/include
  276. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  277. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH}\\InstallPath]/include
  278. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-${_PYTHON_ARCH2}\\InstallPath]/include
  279. PATH_SUFFIXES
  280. python${_CURRENT_VERSION}mu
  281. python${_CURRENT_VERSION}m
  282. python${_CURRENT_VERSION}u
  283. python${_CURRENT_VERSION}
  284. )
  285. endif()
  286. # For backward compatibility, set PYTHON_INCLUDE_PATH.
  287. set(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")
  288. if(PYTHON_INCLUDE_DIR AND EXISTS "${PYTHON_INCLUDE_DIR}/patchlevel.h")
  289. file(STRINGS "${PYTHON_INCLUDE_DIR}/patchlevel.h" python_version_str
  290. REGEX "^#define[ \t]+PY_VERSION[ \t]+\"[^\"]+\"")
  291. string(REGEX REPLACE "^#define[ \t]+PY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
  292. PYTHONLIBS_VERSION_STRING "${python_version_str}")
  293. unset(python_version_str)
  294. endif()
  295. if(PYTHON_LIBRARY AND PYTHON_INCLUDE_DIR)
  296. break()
  297. endif()
  298. endforeach()
  299. unset(_Python_INCLUDE_PATH_HINT)
  300. unset(_Python_LIBRARY_PATH_HINT)
  301. mark_as_advanced(
  302. PYTHON_DEBUG_LIBRARY
  303. PYTHON_LIBRARY
  304. PYTHON_INCLUDE_DIR
  305. )
  306. # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
  307. # cache entries because they are meant to specify the location of a single
  308. # library. We now set the variables listed by the documentation for this
  309. # module.
  310. set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
  311. set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
  312. # These variables have been historically named in this module different from
  313. # what select_library_configurations() expects.
  314. set(PYTHON_LIBRARY_DEBUG "${PYTHON_DEBUG_LIBRARY}")
  315. set(PYTHON_LIBRARY_RELEASE "${PYTHON_LIBRARY}")
  316. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  317. select_library_configurations(PYTHON)
  318. # select_library_configurations() sets ${PREFIX}_FOUND if it has a library.
  319. # Unset this, this prefix doesn't match the module prefix, they are different
  320. # for historical reasons.
  321. unset(PYTHON_FOUND)
  322. # Restore CMAKE_FIND_FRAMEWORK
  323. if(DEFINED _PythonLibs_CMAKE_FIND_FRAMEWORK)
  324. set(CMAKE_FIND_FRAMEWORK ${_PythonLibs_CMAKE_FIND_FRAMEWORK})
  325. unset(_PythonLibs_CMAKE_FIND_FRAMEWORK)
  326. else()
  327. unset(CMAKE_FIND_FRAMEWORK)
  328. endif()
  329. include(FindPackageHandleStandardArgs)
  330. find_package_handle_standard_args(PythonLibs
  331. REQUIRED_VARS PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS
  332. VERSION_VAR PYTHONLIBS_VERSION_STRING)
  333. # PYTHON_ADD_MODULE(<name> src1 src2 ... srcN) is used to build modules for python.
  334. # PYTHON_WRITE_MODULES_HEADER(<filename>) writes a header file you can include
  335. # in your sources to initialize the static python modules
  336. function(PYTHON_ADD_MODULE _NAME )
  337. get_property(_TARGET_SUPPORTS_SHARED_LIBS
  338. GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
  339. option(PYTHON_ENABLE_MODULE_${_NAME} "Add module ${_NAME}" TRUE)
  340. option(PYTHON_MODULE_${_NAME}_BUILD_SHARED
  341. "Add module ${_NAME} shared" ${_TARGET_SUPPORTS_SHARED_LIBS})
  342. # Mark these options as advanced
  343. mark_as_advanced(PYTHON_ENABLE_MODULE_${_NAME}
  344. PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  345. if(PYTHON_ENABLE_MODULE_${_NAME})
  346. if(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  347. set(PY_MODULE_TYPE MODULE)
  348. else()
  349. set(PY_MODULE_TYPE STATIC)
  350. set_property(GLOBAL APPEND PROPERTY PY_STATIC_MODULES_LIST ${_NAME})
  351. endif()
  352. set_property(GLOBAL APPEND PROPERTY PY_MODULES_LIST ${_NAME})
  353. add_library(${_NAME} ${PY_MODULE_TYPE} ${ARGN})
  354. # target_link_libraries(${_NAME} ${PYTHON_LIBRARIES})
  355. if(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  356. set_target_properties(${_NAME} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
  357. if(WIN32 AND NOT CYGWIN)
  358. set_target_properties(${_NAME} PROPERTIES SUFFIX ".pyd")
  359. endif()
  360. endif()
  361. endif()
  362. endfunction()
  363. function(PYTHON_WRITE_MODULES_HEADER _filename)
  364. get_property(PY_STATIC_MODULES_LIST GLOBAL PROPERTY PY_STATIC_MODULES_LIST)
  365. get_filename_component(_name "${_filename}" NAME)
  366. string(REPLACE "." "_" _name "${_name}")
  367. string(TOUPPER ${_name} _nameUpper)
  368. set(_filename ${CMAKE_CURRENT_BINARY_DIR}/${_filename})
  369. set(_filenameTmp "${_filename}.in")
  370. file(WRITE ${_filenameTmp} "/*Created by cmake, do not edit, changes will be lost*/\n")
  371. file(APPEND ${_filenameTmp}
  372. "#ifndef ${_nameUpper}
  373. #define ${_nameUpper}
  374. #include <Python.h>
  375. #ifdef __cplusplus
  376. extern \"C\" {
  377. #endif /* __cplusplus */
  378. ")
  379. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  380. file(APPEND ${_filenameTmp} "extern void init${PYTHON_MODULE_PREFIX}${_currentModule}(void);\n\n")
  381. endforeach()
  382. file(APPEND ${_filenameTmp}
  383. "#ifdef __cplusplus
  384. }
  385. #endif /* __cplusplus */
  386. ")
  387. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  388. 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")
  389. endforeach()
  390. file(APPEND ${_filenameTmp} "void ${_name}_LoadAllPythonModules(void)\n{\n")
  391. foreach(_currentModule ${PY_STATIC_MODULES_LIST})
  392. file(APPEND ${_filenameTmp} " ${_name}_${_currentModule}();\n")
  393. endforeach()
  394. file(APPEND ${_filenameTmp} "}\n\n")
  395. file(APPEND ${_filenameTmp} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n ${_name}_LoadAllPythonModules();\n}\n#endif\n\n#endif\n")
  396. # with configure_file() cmake complains that you may not use a file created using file(WRITE) as input file for configure_file()
  397. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_filenameTmp}" "${_filename}" OUTPUT_QUIET ERROR_QUIET)
  398. endfunction()
  399. cmake_policy(POP)