Back to home page

EIC code displayed by LXR

 
 

    


Warning, /jana2/src/python/externals/pybind11-2.10.3/tools/FindPythonLibsNew.cmake is written in an unsupported language. File is not indexed.

0001 # - Find python libraries
0002 # This module finds the libraries corresponding to the Python interpreter
0003 # FindPythonInterp provides.
0004 # This code sets the following variables:
0005 #
0006 #  PYTHONLIBS_FOUND           - have the Python libs been found
0007 #  PYTHON_PREFIX              - path to the Python installation
0008 #  PYTHON_LIBRARIES           - path to the python library
0009 #  PYTHON_INCLUDE_DIRS        - path to where Python.h is found
0010 #  PYTHON_MODULE_EXTENSION    - lib extension, e.g. '.so' or '.pyd'
0011 #  PYTHON_MODULE_PREFIX       - lib name prefix: usually an empty string
0012 #  PYTHON_SITE_PACKAGES       - path to installation site-packages
0013 #  PYTHON_IS_DEBUG            - whether the Python interpreter is a debug build
0014 #
0015 # Thanks to talljimbo for the patch adding the 'LDVERSION' config
0016 # variable usage.
0017 
0018 #=============================================================================
0019 # Copyright 2001-2009 Kitware, Inc.
0020 # Copyright 2012 Continuum Analytics, Inc.
0021 #
0022 # All rights reserved.
0023 #
0024 # Redistribution and use in source and binary forms, with or without
0025 # modification, are permitted provided that the following conditions
0026 # are met:
0027 #
0028 # * Redistributions of source code must retain the above copyright
0029 # notice, this list of conditions and the following disclaimer.
0030 #
0031 # * Redistributions in binary form must reproduce the above copyright
0032 # notice, this list of conditions and the following disclaimer in the
0033 # documentation and/or other materials provided with the distribution.
0034 #
0035 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
0036 # nor the names of their contributors may be used to endorse or promote
0037 # products derived from this software without specific prior written
0038 # permission.
0039 #
0040 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0041 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0042 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0043 # # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0044 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0045 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0046 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0047 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0048 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0049 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0050 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0051 #=============================================================================
0052 
0053 # Checking for the extension makes sure that `LibsNew` was found and not just `Libs`.
0054 if(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION)
0055   return()
0056 endif()
0057 
0058 if(PythonLibsNew_FIND_QUIETLY)
0059   set(_pythonlibs_quiet QUIET)
0060 else()
0061   set(_pythonlibs_quiet "")
0062 endif()
0063 
0064 if(PythonLibsNew_FIND_REQUIRED)
0065   set(_pythonlibs_required REQUIRED)
0066 endif()
0067 
0068 # Check to see if the `python` command is present and from a virtual
0069 # environment, conda, or GHA activation - if it is, try to use that.
0070 
0071 if(NOT DEFINED PYTHON_EXECUTABLE)
0072   if(DEFINED ENV{VIRTUAL_ENV})
0073     find_program(
0074       PYTHON_EXECUTABLE python
0075       PATHS "$ENV{VIRTUAL_ENV}" "$ENV{VIRTUAL_ENV}/bin"
0076       NO_DEFAULT_PATH)
0077   elseif(DEFINED ENV{CONDA_PREFIX})
0078     find_program(
0079       PYTHON_EXECUTABLE python
0080       PATHS "$ENV{CONDA_PREFIX}" "$ENV{CONDA_PREFIX}/bin"
0081       NO_DEFAULT_PATH)
0082   elseif(DEFINED ENV{pythonLocation})
0083     find_program(
0084       PYTHON_EXECUTABLE python
0085       PATHS "$ENV{pythonLocation}" "$ENV{pythonLocation}/bin"
0086       NO_DEFAULT_PATH)
0087   endif()
0088   if(NOT PYTHON_EXECUTABLE)
0089     unset(PYTHON_EXECUTABLE)
0090   endif()
0091 endif()
0092 
0093 # Use the Python interpreter to find the libs.
0094 if(NOT PythonLibsNew_FIND_VERSION)
0095   set(PythonLibsNew_FIND_VERSION "3.6")
0096 endif()
0097 
0098 find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} ${_pythonlibs_required}
0099              ${_pythonlibs_quiet})
0100 
0101 if(NOT PYTHONINTERP_FOUND)
0102   set(PYTHONLIBS_FOUND FALSE)
0103   set(PythonLibsNew_FOUND FALSE)
0104   return()
0105 endif()
0106 
0107 # According to https://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
0108 # testing whether sys has the gettotalrefcount function is a reliable, cross-platform
0109 # way to detect a CPython debug interpreter.
0110 #
0111 # The library suffix is from the config var LDVERSION sometimes, otherwise
0112 # VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
0113 execute_process(
0114   COMMAND
0115     "${PYTHON_EXECUTABLE}" "-c" "
0116 import sys;import struct;
0117 import sysconfig as s
0118 USE_SYSCONFIG = sys.version_info >= (3, 10)
0119 if not USE_SYSCONFIG:
0120     from distutils import sysconfig as ds
0121 print('.'.join(str(v) for v in sys.version_info));
0122 print(sys.prefix);
0123 if USE_SYSCONFIG:
0124     scheme = s.get_default_scheme()
0125     if scheme == 'posix_local':
0126         # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
0127         scheme = 'posix_prefix'
0128     print(s.get_path('platinclude', scheme))
0129     print(s.get_path('platlib'))
0130     print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))
0131 else:
0132     print(ds.get_python_inc(plat_specific=True));
0133     print(ds.get_python_lib(plat_specific=True));
0134     print(ds.get_config_var('EXT_SUFFIX') or ds.get_config_var('SO'));
0135 print(hasattr(sys, 'gettotalrefcount')+0);
0136 print(struct.calcsize('@P'));
0137 print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
0138 print(s.get_config_var('LIBDIR') or '');
0139 print(s.get_config_var('MULTIARCH') or '');
0140 "
0141   RESULT_VARIABLE _PYTHON_SUCCESS
0142   OUTPUT_VARIABLE _PYTHON_VALUES
0143   ERROR_VARIABLE _PYTHON_ERROR_VALUE)
0144 
0145 if(NOT _PYTHON_SUCCESS MATCHES 0)
0146   if(PythonLibsNew_FIND_REQUIRED)
0147     message(FATAL_ERROR "Python config failure:\n${_PYTHON_ERROR_VALUE}")
0148   endif()
0149   set(PYTHONLIBS_FOUND FALSE)
0150   set(PythonLibsNew_FOUND FALSE)
0151   return()
0152 endif()
0153 
0154 option(
0155   PYBIND11_PYTHONLIBS_OVERWRITE
0156   "Overwrite cached values read from Python library (classic search). Turn off if cross-compiling and manually setting these values."
0157   ON)
0158 # Can manually set values when cross-compiling
0159 macro(_PYBIND11_GET_IF_UNDEF lst index name)
0160   if(PYBIND11_PYTHONLIBS_OVERWRITE OR NOT DEFINED "${name}")
0161     list(GET "${lst}" "${index}" "${name}")
0162   endif()
0163 endmacro()
0164 
0165 # Convert the process output into a list
0166 if(WIN32)
0167   string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
0168 endif()
0169 string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
0170 string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
0171 _pybind11_get_if_undef(_PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
0172 _pybind11_get_if_undef(_PYTHON_VALUES 1 PYTHON_PREFIX)
0173 _pybind11_get_if_undef(_PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
0174 _pybind11_get_if_undef(_PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
0175 _pybind11_get_if_undef(_PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
0176 _pybind11_get_if_undef(_PYTHON_VALUES 5 PYTHON_IS_DEBUG)
0177 _pybind11_get_if_undef(_PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
0178 _pybind11_get_if_undef(_PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
0179 _pybind11_get_if_undef(_PYTHON_VALUES 8 PYTHON_LIBDIR)
0180 _pybind11_get_if_undef(_PYTHON_VALUES 9 PYTHON_MULTIARCH)
0181 
0182 # Make sure the Python has the same pointer-size as the chosen compiler
0183 # Skip if CMAKE_SIZEOF_VOID_P is not defined
0184 # This should be skipped for (non-Apple) cross-compiles (like EMSCRIPTEN)
0185 if(NOT CMAKE_CROSSCOMPILING
0186    AND CMAKE_SIZEOF_VOID_P
0187    AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
0188   if(PythonLibsNew_FIND_REQUIRED)
0189     math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
0190     math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
0191     message(FATAL_ERROR "Python config failure: Python is ${_PYTHON_BITS}-bit, "
0192                         "chosen compiler is  ${_CMAKE_BITS}-bit")
0193   endif()
0194   set(PYTHONLIBS_FOUND FALSE)
0195   set(PythonLibsNew_FOUND FALSE)
0196   return()
0197 endif()
0198 
0199 # The built-in FindPython didn't always give the version numbers
0200 string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
0201 list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
0202 list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
0203 list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
0204 set(PYTHON_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}.${PYTHON_VERSION_PATCH}")
0205 
0206 # Make sure all directory separators are '/'
0207 string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX "${PYTHON_PREFIX}")
0208 string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}")
0209 string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES "${PYTHON_SITE_PACKAGES}")
0210 
0211 if(DEFINED PYTHON_LIBRARY)
0212   # Don't write to PYTHON_LIBRARY if it's already set
0213 elseif(CMAKE_HOST_WIN32)
0214   set(PYTHON_LIBRARY "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
0215 
0216   # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
0217   # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
0218   if(NOT EXISTS "${PYTHON_LIBRARY}")
0219     get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
0220     set(PYTHON_LIBRARY "${_PYTHON_ROOT}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
0221   endif()
0222 
0223   # if we are in MSYS & MINGW, and we didn't find windows python lib, look for system python lib
0224   if(DEFINED ENV{MSYSTEM}
0225      AND MINGW
0226      AND NOT EXISTS "${PYTHON_LIBRARY}")
0227     if(PYTHON_MULTIARCH)
0228       set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
0229     else()
0230       set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
0231     endif()
0232     unset(PYTHON_LIBRARY)
0233     find_library(
0234       PYTHON_LIBRARY
0235       NAMES "python${PYTHON_LIBRARY_SUFFIX}"
0236       PATHS ${_PYTHON_LIBS_SEARCH}
0237       NO_DEFAULT_PATH)
0238   endif()
0239 
0240   # raise an error if the python libs are still not found.
0241   if(NOT EXISTS "${PYTHON_LIBRARY}")
0242     message(FATAL_ERROR "Python libraries not found")
0243   endif()
0244 
0245 else()
0246   if(PYTHON_MULTIARCH)
0247     set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
0248   else()
0249     set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
0250   endif()
0251   #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
0252   # Probably this needs to be more involved. It would be nice if the config
0253   # information the python interpreter itself gave us were more complete.
0254   find_library(
0255     PYTHON_LIBRARY
0256     NAMES "python${PYTHON_LIBRARY_SUFFIX}"
0257     PATHS ${_PYTHON_LIBS_SEARCH}
0258     NO_DEFAULT_PATH)
0259 
0260   # If all else fails, just set the name/version and let the linker figure out the path.
0261   if(NOT PYTHON_LIBRARY)
0262     set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
0263   endif()
0264 endif()
0265 
0266 mark_as_advanced(PYTHON_LIBRARY PYTHON_INCLUDE_DIR)
0267 
0268 # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
0269 # cache entries because they are meant to specify the location of a single
0270 # library. We now set the variables listed by the documentation for this
0271 # module.
0272 set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
0273 set(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
0274 if(NOT PYTHON_DEBUG_LIBRARY)
0275   set(PYTHON_DEBUG_LIBRARY "")
0276 endif()
0277 set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
0278 
0279 find_package_message(PYTHON "Found PythonLibs: ${PYTHON_LIBRARIES}"
0280                      "${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}")
0281 
0282 set(PYTHONLIBS_FOUND TRUE)
0283 set(PythonLibsNew_FOUND TRUE)
0284 
0285 if(NOT PYTHON_MODULE_PREFIX)
0286   set(PYTHON_MODULE_PREFIX "")
0287 endif()