FindPythonLibs.cmake 18 KB

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