FindPythonLibs.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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
  9. # PYTHON_DEBUG_LIBRARIES = path to the debug library
  10. #
  11. INCLUDE(CMakeFindFrameworks)
  12. # Search for the python framework on Apple.
  13. CMAKE_FIND_FRAMEWORKS(Python)
  14. FOREACH(_CURRENT_VERSION 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.6 1.5)
  15. STRING(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
  16. IF(WIN32)
  17. FIND_LIBRARY(PYTHON_DEBUG_LIBRARY
  18. NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
  19. PATHS
  20. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
  21. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs )
  22. ENDIF(WIN32)
  23. FIND_LIBRARY(PYTHON_LIBRARY
  24. NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
  25. PATHS
  26. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
  27. PATH_SUFFIXES
  28. python${_CURRENT_VERSION}/config
  29. # Avoid finding the .dll in the PATH. We want the .lib.
  30. NO_SYSTEM_ENVIRONMENT_PATH
  31. )
  32. SET(PYTHON_FRAMEWORK_INCLUDES)
  33. IF(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_PATH)
  34. FOREACH(dir ${Python_FRAMEWORKS})
  35. SET(PYTHON_FRAMEWORK_INCLUDES ${PYTHON_FRAMEWORK_INCLUDES}
  36. ${dir}/Versions/${_CURRENT_VERSION}/include/python${_CURRENT_VERSION})
  37. ENDFOREACH(dir)
  38. ENDIF(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_PATH)
  39. FIND_PATH(PYTHON_INCLUDE_PATH
  40. NAMES Python.h
  41. PATHS
  42. ${PYTHON_FRAMEWORK_INCLUDES}
  43. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
  44. PATH_SUFFIXES
  45. python${_CURRENT_VERSION}
  46. )
  47. ENDFOREACH(_CURRENT_VERSION)
  48. MARK_AS_ADVANCED(
  49. PYTHON_DEBUG_LIBRARY
  50. PYTHON_LIBRARY
  51. PYTHON_INCLUDE_PATH
  52. )
  53. # Python Should be built and installed as a Framework on OSX
  54. IF(Python_FRAMEWORKS)
  55. # If a framework has been selected for the include path,
  56. # make sure "-framework" is used to link it.
  57. IF("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
  58. SET(PYTHON_LIBRARY "")
  59. SET(PYTHON_DEBUG_LIBRARY "")
  60. ENDIF("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
  61. IF(NOT PYTHON_LIBRARY)
  62. SET (PYTHON_LIBRARY "-framework Python" CACHE FILEPATH "Python Framework" FORCE)
  63. ENDIF(NOT PYTHON_LIBRARY)
  64. IF(NOT PYTHON_DEBUG_LIBRARY)
  65. SET (PYTHON_DEBUG_LIBRARY "-framework Python" CACHE FILEPATH "Python Framework" FORCE)
  66. ENDIF(NOT PYTHON_DEBUG_LIBRARY)
  67. ENDIF(Python_FRAMEWORKS)
  68. # We use PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the cache entries
  69. # because they are meant to specify the location of a single library.
  70. # We now set the variables listed by the documentation for this
  71. # module.
  72. SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
  73. SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
  74. INCLUDE(FindPackageHandleStandardArgs)
  75. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibs DEFAULT_MSG PYTHON_LIBRARIES PYTHON_INCLUDE_PATH)
  76. GET_TARGET_PROPERTY(_FIND_PYTHONLIBS_ALREADY_INCLUDED __FindPythonLibsHelper TYPE)
  77. IF(NOT _FIND_PYTHONLIBS_ALREADY_INCLUDED)
  78. ADD_CUSTOM_TARGET(__FindPythonLibsHelper) # so if this file is included several times the next time GET_TARGET_PROPERTIES() will work
  79. SET(PY_STATIC_MODULES_LIST "" CACHE STRING "Collect a list of all added static python modules" FORCE)
  80. SET(PY_MODULES_LIST "" CACHE STRING "Collect a list of all added python modules" FORCE)
  81. ENDIF(NOT _FIND_PYTHONLIBS_ALREADY_INCLUDED)
  82. GET_PROPERTY(_TARGET_SUPPORTS_SHARED_LIBS GLOBAL TARGET_SUPPORTS_SHARED_LIBS)
  83. MACRO(PYTHON_ADD_MODULE _NAME )
  84. OPTION(PYTHON_ENABLE_MODULE_${_NAME} "Add module ${_NAME}" TRUE)
  85. OPTION(PYTHON_MODULE_${_NAME}_BUILD_SHARED "Add module ${_NAME} shared" ${_TARGET_SUPPORTS_SHARED_LIBS})
  86. IF(PYTHON_ENABLE_MODULE_${_NAME})
  87. IF(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  88. SET(PY_MODULE_TYPE MODULE)
  89. ELSE(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  90. SET(PY_MODULE_TYPE STATIC)
  91. SET(PY_STATIC_MODULES_LIST ${PY_STATIC_MODULES_LIST} ${_NAME} CACHE STRING "Collect a list of all added static python modules" FORCE)
  92. ENDIF(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
  93. SET(PY_MODULES_LIST ${PY_MODULES_LIST} ${_NAME} CACHE STRING "Collect a list of all added python modules" FORCE)
  94. ADD_LIBRARY(${_NAME} ${PY_MODULE_TYPE} ${ARGN})
  95. # TARGET_LINK_LIBRARIES(${_NAME} ${PYTHON_LIBRARIES})
  96. ENDIF(PYTHON_ENABLE_MODULE_${_NAME})
  97. ENDMACRO(PYTHON_ADD_MODULE)
  98. MACRO(PYTHON_WRITE_MODULES_HEADER _filename)
  99. GET_FILENAME_COMPONENT(_name "${_filename}" NAME)
  100. STRING(REPLACE "." "_" _name "${_name}")
  101. STRING(TOUPPER ${_name} _name)
  102. SET(_filenameTmp "${_filename}.in")
  103. FILE(WRITE ${_filenameTmp} "/*Created by cmake, do not edit, changes will be lost*/\n")
  104. FILE(APPEND ${_filenameTmp}
  105. "#ifndef ${_name}
  106. #define ${_name}
  107. #include <Python.h>
  108. #ifdef __cplusplus
  109. extern \"C\" {
  110. #endif /* __cplusplus */
  111. ")
  112. FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
  113. FILE(APPEND ${_filenameTmp} "extern void init${PYTHON_MODULE_PREFIX}${_currentModule}(void);\n\n")
  114. ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
  115. FILE(APPEND ${_filenameTmp}
  116. "#ifdef __cplusplus
  117. }
  118. #endif /* __cplusplus */
  119. ")
  120. FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
  121. FILE(APPEND ${_filenameTmp} "int CMakeLoadPythonModule_${_currentModule}(void) \n{\n static char name[]=\"${PYTHON_MODULE_PREFIX}${_currentModule}\"; return PyImport_AppendInittab(name, init${PYTHON_MODULE_PREFIX}${_currentModule});\n}\n\n")
  122. ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
  123. FILE(APPEND ${_filenameTmp} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n")
  124. FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
  125. FILE(APPEND ${_filenameTmp} " CMakeLoadPythonModule_${_currentModule}();\n")
  126. ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
  127. FILE(APPEND ${_filenameTmp} "}\n#endif\n\n#endif\n")
  128. # with CONFIGURE_FILE() cmake complains that you may not use a file created using FILE(WRITE) as input file for CONFIGURE_FILE()
  129. EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_filenameTmp}" "${_filename}" OUTPUT_QUIET ERROR_QUIET)
  130. ENDMACRO(PYTHON_WRITE_MODULES_HEADER)