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:
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
12 INCLUDE(CMakeFindFrameworks)
13 # Search for the python framework on Apple.
14 CMAKE_FIND_FRAMEWORKS(Python)
16 FOREACH(_CURRENT_VERSION 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.6 1.5)
17 STRING(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
19 FIND_LIBRARY(PYTHON_DEBUG_LIBRARY
20 NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
22 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
23 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs )
26 FIND_LIBRARY(PYTHON_LIBRARY
27 NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
29 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
31 python${_CURRENT_VERSION}/config
32 # Avoid finding the .dll in the PATH. We want the .lib.
33 NO_SYSTEM_ENVIRONMENT_PATH
36 SET(PYTHON_FRAMEWORK_INCLUDES)
37 IF(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_PATH)
38 FOREACH(dir ${Python_FRAMEWORKS})
39 SET(PYTHON_FRAMEWORK_INCLUDES ${PYTHON_FRAMEWORK_INCLUDES}
40 ${dir}/Versions/${_CURRENT_VERSION}/include/python${_CURRENT_VERSION})
42 ENDIF(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_PATH)
44 FIND_PATH(PYTHON_INCLUDE_PATH
47 ${PYTHON_FRAMEWORK_INCLUDES}
48 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
50 python${_CURRENT_VERSION}
53 ENDFOREACH(_CURRENT_VERSION)
61 # Python Should be built and installed as a Framework on OSX
63 # If a framework has been selected for the include path,
64 # make sure "-framework" is used to link it.
65 IF("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
66 SET(PYTHON_LIBRARY "")
67 SET(PYTHON_DEBUG_LIBRARY "")
68 ENDIF("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
69 IF(NOT PYTHON_LIBRARY)
70 SET (PYTHON_LIBRARY "-framework Python" CACHE FILEPATH "Python Framework" FORCE)
71 ENDIF(NOT PYTHON_LIBRARY)
72 IF(NOT PYTHON_DEBUG_LIBRARY)
73 SET (PYTHON_DEBUG_LIBRARY "-framework Python" CACHE FILEPATH "Python Framework" FORCE)
74 ENDIF(NOT PYTHON_DEBUG_LIBRARY)
75 ENDIF(Python_FRAMEWORKS)
77 # We use PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the cache entries
78 # because they are meant to specify the location of a single library.
79 # We now set the variables listed by the documentation for this
81 SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
82 SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
85 INCLUDE(FindPackageHandleStandardArgs)
86 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibs DEFAULT_MSG PYTHON_LIBRARIES PYTHON_INCLUDE_PATH)
89 # PYTHON_ADD_MODULE(<name> src1 src2 ... srcN) is used to build modules for python.
90 # PYTHON_WRITE_MODULES_HEADER(<filename>) writes a header file you can include
91 # in your sources to initialize the static python modules
93 GET_PROPERTY(_TARGET_SUPPORTS_SHARED_LIBS
94 GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
96 FUNCTION(PYTHON_ADD_MODULE _NAME )
97 OPTION(PYTHON_ENABLE_MODULE_${_NAME} "Add module ${_NAME}" TRUE)
98 OPTION(PYTHON_MODULE_${_NAME}_BUILD_SHARED "Add module ${_NAME} shared" ${_TARGET_SUPPORTS_SHARED_LIBS})
100 IF(PYTHON_ENABLE_MODULE_${_NAME})
101 IF(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
102 SET(PY_MODULE_TYPE MODULE)
103 ELSE(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
104 SET(PY_MODULE_TYPE STATIC)
105 SET_PROPERTY(GLOBAL APPEND PROPERTY PY_STATIC_MODULES_LIST ${_NAME})
106 ENDIF(PYTHON_MODULE_${_NAME}_BUILD_SHARED)
108 SET_PROPERTY(GLOBAL APPEND PROPERTY PY_MODULES_LIST ${_NAME})
109 ADD_LIBRARY(${_NAME} ${PY_MODULE_TYPE} ${ARGN})
110 # TARGET_LINK_LIBRARIES(${_NAME} ${PYTHON_LIBRARIES})
112 ENDIF(PYTHON_ENABLE_MODULE_${_NAME})
113 ENDFUNCTION(PYTHON_ADD_MODULE)
115 FUNCTION(PYTHON_WRITE_MODULES_HEADER _filename)
117 GET_PROPERTY(PY_STATIC_MODULES_LIST GLOBAL PROPERTY PY_STATIC_MODULES_LIST)
119 GET_FILENAME_COMPONENT(_name "${_filename}" NAME)
120 STRING(REPLACE "." "_" _name "${_name}")
121 STRING(TOUPPER ${_name} _name)
123 SET(_filenameTmp "${_filename}.in")
124 FILE(WRITE ${_filenameTmp} "/*Created by cmake, do not edit, changes will be lost*/\n")
125 FILE(APPEND ${_filenameTmp}
133 #endif /* __cplusplus */
137 FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
138 FILE(APPEND ${_filenameTmp} "extern void init${PYTHON_MODULE_PREFIX}${_currentModule}(void);\n\n")
139 ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
141 FILE(APPEND ${_filenameTmp}
144 #endif /* __cplusplus */
149 FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
150 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")
151 ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
153 FILE(APPEND ${_filenameTmp} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n")
154 FOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
155 FILE(APPEND ${_filenameTmp} " CMakeLoadPythonModule_${_currentModule}();\n")
156 ENDFOREACH(_currentModule ${PY_STATIC_MODULES_LIST})
157 FILE(APPEND ${_filenameTmp} "}\n#endif\n\n#endif\n")
159 # with CONFIGURE_FILE() cmake complains that you may not use a file created using FILE(WRITE) as input file for CONFIGURE_FILE()
160 EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_filenameTmp}" "${_filename}" OUTPUT_QUIET ERROR_QUIET)
162 ENDFUNCTION(PYTHON_WRITE_MODULES_HEADER)