Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / cmake / modules / FindPython3Libs.cmake
1 #.rst:
2 # FindPython3Libs
3 # --------------
4 #
5 # Find python libraries
6 #
7 # This module finds if Python is installed and determines where the
8 # include files and libraries are.  It also determines what the name of
9 # the library is.  This code sets the following variables:
10 #
11 # ::
12 #
13 #   PYTHON3LIBS_FOUND           - have the Python libs been found
14 #   PYTHON3_LIBRARIES           - path to the python library
15 #   PYTHON3_INCLUDE_PATH        - path to where Python.h is found (deprecated)
16 #   PYTHON3_INCLUDE_DIRS        - path to where Python.h is found
17 #   PYTHON3_DEBUG_LIBRARIES     - path to the debug library (deprecated)
18 #   PYTHON3LIBS_VERSION_STRING  - version of the Python libs found (since CMake 2.8.8)
19 #
20 #
21 #
22 # The Python3_ADDITIONAL_VERSIONS variable can be used to specify a list
23 # of version numbers that should be taken into account when searching
24 # for Python.  You need to set this variable before calling
25 # find_package(Python3Libs).
26 #
27 # If you'd like to specify the installation of Python to use, you should
28 # modify the following cache variables:
29 #
30 # ::
31 #
32 #   PYTHON3_LIBRARY             - path to the python library
33 #   PYTHON3_INCLUDE_DIR         - path to where Python.h is found
34 #
35 # If calling both ``find_package(PythonInterp)`` and
36 # ``find_package(Python3Libs)``, call ``find_package(PythonInterp)`` first to
37 # get the currently active Python version by default with a consistent version
38 # of PYTHON3_LIBRARIES.
39
40 #=============================================================================
41 # Copyright 2001-2009 Kitware, Inc.
42 #
43 # Redistribution and use in source and binary forms, with or without
44 # modification, are permitted provided that the following conditions
45 # are met:
46 #
47 # * Redistributions of source code must retain the above copyright
48 #   notice, this list of conditions and the following disclaimer.
49 #
50 # * Redistributions in binary form must reproduce the above copyright
51 #   notice, this list of conditions and the following disclaimer in the
52 #   documentation and/or other materials provided with the distribution.
53 #
54 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
55 #   nor the names of their contributors may be used to endorse or promote
56 #   products derived from this software without specific prior written
57 #   permission.
58 #
59 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
60 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
61 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
62 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
63 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
64 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
65 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 #=============================================================================
71
72 # Use the executable's path as a hint
73 set(_Python3_LIBRARY_PATH_HINT)
74 if(PYTHON3_EXECUTABLE)
75   if(WIN32)
76     get_filename_component(_Python3_PREFIX ${PYTHON3_EXECUTABLE} PATH)
77     if(_Python3_PREFIX)
78       set(_Python3_LIBRARY_PATH_HINT ${_Python3_PREFIX}/libs)
79     endif()
80     unset(_Python3_PREFIX)
81   else()
82     get_filename_component(_Python3_PREFIX ${PYTHON3_EXECUTABLE} PATH)
83     get_filename_component(_Python3_PREFIX ${_Python3_PREFIX} PATH)
84     if(_Python3_PREFIX)
85       set(_Python3_LIBRARY_PATH_HINT ${_Python3_PREFIX}/lib)
86     endif()
87     unset(_Python3_PREFIX)
88   endif()
89 endif()
90
91 include(CMakeFindFrameworks)
92 # Search for the python framework on Apple.
93 CMAKE_FIND_FRAMEWORKS(Python)
94
95 # Save CMAKE_FIND_FRAMEWORK
96 if(DEFINED CMAKE_FIND_FRAMEWORK)
97   set(_Python3Libs_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
98 else()
99   unset(_Python3Libs_CMAKE_FIND_FRAMEWORK)
100 endif()
101 # To avoid picking up the system Python.h pre-maturely.
102 set(CMAKE_FIND_FRAMEWORK LAST)
103
104 set(_PYTHON3_VERSIONS 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
105
106 if(Python3Libs_FIND_VERSION)
107     if(Python3Libs_FIND_VERSION_COUNT GREATER 1)
108         set(_PYTHON3_FIND_MAJ_MIN "${Python3Libs_FIND_VERSION_MAJOR}.${Python3Libs_FIND_VERSION_MINOR}")
109         unset(_PYTHON3_FIND_OTHER_VERSIONS)
110         if(Python3Libs_FIND_VERSION_EXACT)
111             if(_PYTHON3_FIND_MAJ_MIN STREQUAL Python3Libs_FIND_VERSION)
112                 set(_PYTHON3_FIND_OTHER_VERSIONS "${Python3Libs_FIND_VERSION}")
113             else()
114                 set(_PYTHON3_FIND_OTHER_VERSIONS "${Python3Libs_FIND_VERSION}" "${_PYTHON3_FIND_MAJ_MIN}")
115             endif()
116         else()
117             foreach(_PYTHON3_V ${_PYTHON${Python3Libs_FIND_VERSION_MAJOR}_VERSIONS})
118                 if(NOT _PYTHON3_V VERSION_LESS _PYTHON3_FIND_MAJ_MIN)
119                     list(APPEND _PYTHON3_FIND_OTHER_VERSIONS ${_PYTHON3_V})
120                 endif()
121              endforeach()
122         endif()
123         unset(_PYTHON3_FIND_MAJ_MIN)
124     else()
125         set(_PYTHON3_FIND_OTHER_VERSIONS ${_PYTHON${Python3Libs_FIND_VERSION_MAJOR}_VERSIONS})
126     endif()
127 else()
128     set(_PYTHON3_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS})
129 endif()
130
131 # Set up the versions we know about, in the order we will search. Always add
132 # the user supplied additional versions to the front.
133 # If FindPythonInterp has already found the major and minor version,
134 # insert that version between the user supplied versions and the stock
135 # version list.
136 set(_Python3_VERSIONS ${Python3_ADDITIONAL_VERSIONS})
137 if(DEFINED PYTHON3_VERSION_MAJOR AND DEFINED PYTHON3_VERSION_MINOR)
138   list(APPEND _Python3_VERSIONS ${PYTHON3_VERSION_MAJOR}.${PYTHON3_VERSION_MINOR})
139 endif()
140 list(APPEND _Python3_VERSIONS ${_PYTHON3_FIND_OTHER_VERSIONS})
141
142 unset(_PYTHON3_FIND_OTHER_VERSIONS)
143 unset(_PYTHON3_VERSIONS)
144
145 foreach(_CURRENT_VERSION ${_Python3_VERSIONS})
146   string(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
147   if(WIN32)
148     find_library(PYTHON3_DEBUG_LIBRARY
149       NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
150       HINTS ${_Python3_LIBRARY_PATH_HINT}
151       PATHS
152       [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
153       [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
154       [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
155       [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
156       )
157   endif()
158
159   set(PYTHON3_FRAMEWORK_LIBRARIES)
160   if(Python3_FRAMEWORKS AND NOT PYTHON3_LIBRARY)
161     foreach(dir ${Python3_FRAMEWORKS})
162       list(APPEND PYTHON3_FRAMEWORK_LIBRARIES
163            ${dir}/Versions/${_CURRENT_VERSION}/lib)
164     endforeach()
165   endif()
166   find_library(PYTHON3_LIBRARY
167     NAMES
168       python${_CURRENT_VERSION_NO_DOTS}
169       python${_CURRENT_VERSION}mu
170       python${_CURRENT_VERSION}m
171       python${_CURRENT_VERSION}u
172       python${_CURRENT_VERSION}
173     HINTS
174       ${_Python3_LIBRARY_PATH_HINT}
175     PATHS
176       ${PYTHON3_FRAMEWORK_LIBRARIES}
177       [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
178       [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
179     # Avoid finding the .dll in the PATH.  We want the .lib.
180     NO_SYSTEM_ENVIRONMENT_PATH
181   )
182   # Look for the static library in the Python config directory
183   find_library(PYTHON3_LIBRARY
184     NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
185     # Avoid finding the .dll in the PATH.  We want the .lib.
186     NO_SYSTEM_ENVIRONMENT_PATH
187     # This is where the static library is usually located
188     PATH_SUFFIXES python${_CURRENT_VERSION}/config
189   )
190
191   # Don't search for include dir until library location is known
192   if(PYTHON3_LIBRARY)
193
194     # Use the library's install prefix as a hint
195     set(_Python3_INCLUDE_PATH_HINT)
196     get_filename_component(_Python3_PREFIX ${PYTHON3_LIBRARY} PATH)
197     get_filename_component(_Python3_PREFIX ${_Python3_PREFIX} PATH)
198     if(_Python3_PREFIX)
199       set(_Python3_INCLUDE_PATH_HINT ${_Python3_PREFIX}/include)
200     endif()
201     unset(_Python3_PREFIX)
202
203     # Add framework directories to the search paths
204     set(PYTHON3_FRAMEWORK_INCLUDES)
205     if(Python3_FRAMEWORKS AND NOT PYTHON3_INCLUDE_DIR)
206       foreach(dir ${Python3_FRAMEWORKS})
207         list(APPEND PYTHON3_FRAMEWORK_INCLUDES
208           ${dir}/Versions/${_CURRENT_VERSION}/include)
209       endforeach()
210     endif()
211
212     find_path(PYTHON3_INCLUDE_DIR
213       NAMES Python.h
214       HINTS
215         ${_Python3_INCLUDE_PATH_HINT}
216       PATHS
217         ${PYTHON3_FRAMEWORK_INCLUDES}
218         [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
219         [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
220       PATH_SUFFIXES
221         python${_CURRENT_VERSION}mu
222         python${_CURRENT_VERSION}m
223         python${_CURRENT_VERSION}u
224         python${_CURRENT_VERSION}
225     )
226   endif()
227
228   # For backward compatibility, set PYTHON3_INCLUDE_PATH.
229   set(PYTHON3_INCLUDE_PATH "${PYTHON3_INCLUDE_DIR}")
230
231   if(PYTHON3_INCLUDE_DIR AND EXISTS "${PYTHON3_INCLUDE_DIR}/patchlevel.h")
232     file(STRINGS "${PYTHON3_INCLUDE_DIR}/patchlevel.h" python3_version_str
233          REGEX "^#define[ \t]+PY_VERSION[ \t]+\"[^\"]+\"")
234     string(REGEX REPLACE "^#define[ \t]+PY_VERSION[ \t]+\"([^\"]+)\".*" "\\1"
235                          PYTHON3LIBS_VERSION_STRING "${python3_version_str}")
236     unset(python3_version_str)
237   endif()
238
239   if(PYTHON3_LIBRARY AND PYTHON3_INCLUDE_DIR)
240     break()
241   endif()
242 endforeach()
243
244 unset(_Python3_INCLUDE_PATH_HINT)
245 unset(_Python3_LIBRARY_PATH_HINT)
246
247 mark_as_advanced(
248   PYTHON3_DEBUG_LIBRARY
249   PYTHON3_LIBRARY
250   PYTHON3_INCLUDE_DIR
251 )
252
253 # We use PYTHON3_INCLUDE_DIR, PYTHON3_LIBRARY and PYTHON3_DEBUG_LIBRARY for the
254 # cache entries because they are meant to specify the location of a single
255 # library. We now set the variables listed by the documentation for this
256 # module.
257 set(PYTHON3_INCLUDE_DIRS "${PYTHON3_INCLUDE_DIR}")
258 set(PYTHON3_DEBUG_LIBRARIES "${PYTHON3_DEBUG_LIBRARY}")
259
260 # These variables have been historically named in this module different from
261 # what SELECT_LIBRARY_CONFIGURATIONS() expects.
262 set(PYTHON3_LIBRARY_DEBUG "${PYTHON3_DEBUG_LIBRARY}")
263 set(PYTHON3_LIBRARY_RELEASE "${PYTHON3_LIBRARY}")
264 include(SelectLibraryConfigurations)
265 SELECT_LIBRARY_CONFIGURATIONS(PYTHON3)
266 # SELECT_LIBRARY_CONFIGURATIONS() sets ${PREFIX}_FOUND if it has a library.
267 # Unset this, this prefix doesn't match the module prefix, they are different
268 # for historical reasons.
269 unset(PYTHON3_FOUND)
270
271 # Restore CMAKE_FIND_FRAMEWORK
272 if(DEFINED _Python3Libs_CMAKE_FIND_FRAMEWORK)
273   set(CMAKE_FIND_FRAMEWORK ${_Python3Libs_CMAKE_FIND_FRAMEWORK})
274   unset(_Python3Libs_CMAKE_FIND_FRAMEWORK)
275 else()
276   unset(CMAKE_FIND_FRAMEWORK)
277 endif()
278
279 include(FindPackageHandleStandardArgs)
280 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Python3Libs
281                                   REQUIRED_VARS PYTHON3_LIBRARIES PYTHON3_INCLUDE_DIRS
282                                   VERSION_VAR PYTHON3LIBS_VERSION_STRING)
283
284 # PYTHON3_ADD_MODULE(<name> src1 src2 ... srcN) is used to build modules for python.
285 # PYTHON3_WRITE_MODULES_HEADER(<filename>) writes a header file you can include
286 # in your sources to initialize the static python modules
287 function(PYTHON3_ADD_MODULE _NAME )
288   get_property(_TARGET_SUPPORTS_SHARED_LIBS
289     GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
290   option(PYTHON3_ENABLE_MODULE_${_NAME} "Add module ${_NAME}" TRUE)
291   option(PYTHON3_MODULE_${_NAME}_BUILD_SHARED
292     "Add module ${_NAME} shared" ${_TARGET_SUPPORTS_SHARED_LIBS})
293
294   # Mark these options as advanced
295   mark_as_advanced(PYTHON3_ENABLE_MODULE_${_NAME}
296     PYTHON3_MODULE_${_NAME}_BUILD_SHARED)
297
298   if(PYTHON3_ENABLE_MODULE_${_NAME})
299     if(PYTHON3_MODULE_${_NAME}_BUILD_SHARED)
300       set(PY_MODULE_TYPE MODULE)
301     else()
302       set(PY_MODULE_TYPE STATIC)
303       set_property(GLOBAL  APPEND  PROPERTY  PY_STATIC_MODULES_LIST ${_NAME})
304     endif()
305
306     set_property(GLOBAL  APPEND  PROPERTY  PY_MODULES_LIST ${_NAME})
307     add_library(${_NAME} ${PY_MODULE_TYPE} ${ARGN})
308 #    target_link_libraries(${_NAME} ${PYTHON3_LIBRARIES})
309
310     if(PYTHON3_MODULE_${_NAME}_BUILD_SHARED)
311       set_target_properties(${_NAME} PROPERTIES PREFIX "${PYTHON3_MODULE_PREFIX}")
312       if(WIN32 AND NOT CYGWIN)
313         set_target_properties(${_NAME} PROPERTIES SUFFIX ".pyd")
314       endif()
315     endif()
316
317   endif()
318 endfunction()
319
320 function(PYTHON3_WRITE_MODULES_HEADER _filename)
321
322   get_property(PY_STATIC_MODULES_LIST  GLOBAL  PROPERTY PY_STATIC_MODULES_LIST)
323
324   get_filename_component(_name "${_filename}" NAME)
325   string(REPLACE "." "_" _name "${_name}")
326   string(TOUPPER ${_name} _nameUpper)
327   set(_filename ${CMAKE_CURRENT_BINARY_DIR}/${_filename})
328
329   set(_filenameTmp "${_filename}.in")
330   file(WRITE ${_filenameTmp} "/*Created by cmake, do not edit, changes will be lost*/\n")
331   file(APPEND ${_filenameTmp}
332 "#ifndef ${_nameUpper}
333 #define ${_nameUpper}
334
335 #include <Python.h>
336
337 #ifdef __cplusplus
338 extern \"C\" {
339 #endif /* __cplusplus */
340
341 ")
342
343   foreach(_currentModule ${PY_STATIC_MODULES_LIST})
344     file(APPEND ${_filenameTmp} "extern void init${PYTHON3_MODULE_PREFIX}${_currentModule}(void);\n\n")
345   endforeach()
346
347   file(APPEND ${_filenameTmp}
348 "#ifdef __cplusplus
349 }
350 #endif /* __cplusplus */
351
352 ")
353
354
355   foreach(_currentModule ${PY_STATIC_MODULES_LIST})
356     file(APPEND ${_filenameTmp} "int ${_name}_${_currentModule}(void) \n{\n  static char name[]=\"${PYTHON3_MODULE_PREFIX}${_currentModule}\"; return PyImport_AppendInittab(name, init${PYTHON3_MODULE_PREFIX}${_currentModule});\n}\n\n")
357   endforeach()
358
359   file(APPEND ${_filenameTmp} "void ${_name}_LoadAllPythonModules(void)\n{\n")
360   foreach(_currentModule ${PY_STATIC_MODULES_LIST})
361     file(APPEND ${_filenameTmp} "  ${_name}_${_currentModule}();\n")
362   endforeach()
363   file(APPEND ${_filenameTmp} "}\n\n")
364   file(APPEND ${_filenameTmp} "#ifndef EXCLUDE_LOAD_ALL_FUNCTION\nvoid CMakeLoadAllPythonModules(void)\n{\n  ${_name}_LoadAllPythonModules();\n}\n#endif\n\n#endif\n")
365
366 # with configure_file() cmake complains that you may not use a file created using file(WRITE) as input file for configure_file()
367   execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_filenameTmp}" "${_filename}" OUTPUT_QUIET ERROR_QUIET)
368
369 endfunction()