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