Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dont copy the old doc in the tarball, even if it still exists on disk
[simgrid.git] / tools / cmake / Modules / FindPythonLibsNew.cmake
1 # - Find python libraries
2 # This module finds the libraries corresponding to the Python interpreter
3 # FindPythonInterp provides.
4 # This code sets the following variables:
5 #
6 #  PYTHONLIBS_FOUND           - have the Python libs been found
7 #  PYTHON_PREFIX              - path to the Python installation
8 #  PYTHON_LIBRARIES           - path to the python library
9 #  PYTHON_INCLUDE_DIRS        - path to where Python.h is found
10 #  PYTHON_MODULE_EXTENSION    - lib extension, e.g. '.so' or '.pyd'
11 #  PYTHON_MODULE_PREFIX       - lib name prefix: usually an empty string
12 #  PYTHON_SITE_PACKAGES       - path to installation site-packages
13 #  PYTHON_IS_DEBUG            - whether the Python interpreter is a debug build
14 #
15 # Thanks to talljimbo for the patch adding the 'LDVERSION' config
16 # variable usage.
17
18 #=============================================================================
19 # Copyright 2001-2009 Kitware, Inc.
20 # Copyright 2012 Continuum Analytics, Inc.
21 #
22 # All rights reserved.
23 #
24 # Redistribution and use in source and binary forms, with or without
25 # modification, are permitted provided that the following conditions
26 # are met:
27 #
28 # * Redistributions of source code must retain the above copyright
29 # notice, this list of conditions and the following disclaimer.
30 #
31 # * Redistributions in binary form must reproduce the above copyright
32 # notice, this list of conditions and the following disclaimer in the
33 # documentation and/or other materials provided with the distribution.
34 #
35 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
36 # nor the names of their contributors may be used to endorse or promote
37 # products derived from this software without specific prior written
38 # permission.
39 #
40 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43 # # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #=============================================================================
52
53 # Checking for the extension makes sure that `LibsNew` was found and not just `Libs`.
54 if(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION)
55     return()
56 endif()
57
58 # Use the Python interpreter to find the libs.
59 if(PythonLibsNew_FIND_REQUIRED)
60     find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED)
61 else()
62     find_package(PythonInterp ${PythonLibsNew_FIND_VERSION})
63 endif()
64
65 if(NOT PYTHONINTERP_FOUND)
66     set(PYTHONLIBS_FOUND FALSE)
67     return()
68 endif()
69
70 # According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
71 # testing whether sys has the gettotalrefcount function is a reliable, cross-platform
72 # way to detect a CPython debug interpreter.
73 #
74 # The library suffix is from the config var LDVERSION sometimes, otherwise
75 # VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
76 execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
77     "from distutils import sysconfig as s;import sys;import struct;
78 print('.'.join(str(v) for v in sys.version_info));
79 print(sys.prefix);
80 print(s.get_python_inc(plat_specific=True));
81 print(s.get_python_lib(plat_specific=True));
82 print(s.get_config_var('SO'));
83 print(hasattr(sys, 'gettotalrefcount')+0);
84 print(struct.calcsize('@P'));
85 print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
86 print(s.get_config_var('LIBDIR') or '');
87 print(s.get_config_var('MULTIARCH') or '');
88 "
89     RESULT_VARIABLE _PYTHON_SUCCESS
90     OUTPUT_VARIABLE _PYTHON_VALUES
91     ERROR_VARIABLE _PYTHON_ERROR_VALUE)
92
93 if(NOT _PYTHON_SUCCESS MATCHES 0)
94     if(PythonLibsNew_FIND_REQUIRED)
95         message(FATAL_ERROR
96             "Python config failure:\n${_PYTHON_ERROR_VALUE}")
97     endif()
98     set(PYTHONLIBS_FOUND FALSE)
99     return()
100 endif()
101
102 # Convert the process output into a list
103 string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
104 string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
105 list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
106 list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
107 list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
108 list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
109 list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
110 list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
111 list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
112 list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
113 list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
114 list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
115
116 # Make sure the Python has the same pointer-size as the chosen compiler
117 # Skip if CMAKE_SIZEOF_VOID_P is not defined
118 if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
119     if(PythonLibsNew_FIND_REQUIRED)
120         math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
121         math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
122         message(FATAL_ERROR
123             "Python config failure: Python is ${_PYTHON_BITS}-bit, "
124             "chosen compiler is  ${_CMAKE_BITS}-bit")
125     endif()
126     set(PYTHONLIBS_FOUND FALSE)
127     return()
128 endif()
129
130 # The built-in FindPython didn't always give the version numbers
131 string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
132 list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
133 list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
134 list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
135
136 # Make sure all directory separators are '/'
137 string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX ${PYTHON_PREFIX})
138 string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
139 string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES})
140
141 if(CMAKE_HOST_WIN32)
142     set(PYTHON_LIBRARY
143         "${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
144
145     # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
146     # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
147     if(NOT EXISTS "${PYTHON_LIBRARY}")
148         get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
149         set(PYTHON_LIBRARY
150             "${_PYTHON_ROOT}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
151     endif()
152
153     # raise an error if the python libs are still not found.
154     if(NOT EXISTS "${PYTHON_LIBRARY}")
155         message(FATAL_ERROR "Python libraries not found")
156     endif()
157
158 else()
159     if(PYTHON_MULTIARCH)
160         set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
161     else()
162         set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
163     endif()
164     #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
165     # Probably this needs to be more involved. It would be nice if the config
166     # information the python interpreter itself gave us were more complete.
167     find_library(PYTHON_LIBRARY
168         NAMES "python${PYTHON_LIBRARY_SUFFIX}"
169         PATHS ${_PYTHON_LIBS_SEARCH}
170         NO_DEFAULT_PATH)
171
172     # If all else fails, just set the name/version and let the linker figure out the path.
173     if(NOT PYTHON_LIBRARY)
174         set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
175     endif()
176 endif()
177
178 MARK_AS_ADVANCED(
179   PYTHON_LIBRARY
180   PYTHON_INCLUDE_DIR
181 )
182
183 # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
184 # cache entries because they are meant to specify the location of a single
185 # library. We now set the variables listed by the documentation for this
186 # module.
187 SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
188 SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
189 SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
190
191 find_package_message(PYTHON
192     "Found PythonLibs: ${PYTHON_LIBRARY}"
193     "${PYTHON_EXECUTABLE}${PYTHON_VERSION}")
194
195 set(PYTHONLIBS_FOUND TRUE)