github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/pybind11/tools/FindPythonLibsNew.cmake (about)

     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  if(PythonLibsNew_FIND_QUIETLY)
    59    set(_pythonlibs_quiet QUIET)
    60  else()
    61    set(_pythonlibs_quiet "")
    62  endif()
    63  
    64  if(PythonLibsNew_FIND_REQUIRED)
    65    set(_pythonlibs_required REQUIRED)
    66  endif()
    67  
    68  # Check to see if the `python` command is present and from a virtual
    69  # environment, conda, or GHA activation - if it is, try to use that.
    70  
    71  if(NOT DEFINED PYTHON_EXECUTABLE)
    72    if(DEFINED ENV{VIRTUAL_ENV})
    73      find_program(
    74        PYTHON_EXECUTABLE python
    75        PATHS "$ENV{VIRTUAL_ENV}" "$ENV{VIRTUAL_ENV}/bin"
    76        NO_DEFAULT_PATH)
    77    elseif(DEFINED ENV{CONDA_PREFIX})
    78      find_program(
    79        PYTHON_EXECUTABLE python
    80        PATHS "$ENV{CONDA_PREFIX}" "$ENV{CONDA_PREFIX}/bin"
    81        NO_DEFAULT_PATH)
    82    elseif(DEFINED ENV{pythonLocation})
    83      find_program(
    84        PYTHON_EXECUTABLE python
    85        PATHS "$ENV{pythonLocation}" "$ENV{pythonLocation}/bin"
    86        NO_DEFAULT_PATH)
    87    endif()
    88    if(NOT PYTHON_EXECUTABLE)
    89      unset(PYTHON_EXECUTABLE)
    90    endif()
    91  endif()
    92  
    93  # Use the Python interpreter to find the libs.
    94  if(NOT PythonLibsNew_FIND_VERSION)
    95    set(PythonLibsNew_FIND_VERSION "3.6")
    96  endif()
    97  
    98  find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} ${_pythonlibs_required}
    99               ${_pythonlibs_quiet})
   100  
   101  if(NOT PYTHONINTERP_FOUND)
   102    set(PYTHONLIBS_FOUND FALSE)
   103    set(PythonLibsNew_FOUND FALSE)
   104    return()
   105  endif()
   106  
   107  # According to https://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
   108  # testing whether sys has the gettotalrefcount function is a reliable, cross-platform
   109  # way to detect a CPython debug interpreter.
   110  #
   111  # The library suffix is from the config var LDVERSION sometimes, otherwise
   112  # VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
   113  execute_process(
   114    COMMAND
   115      "${PYTHON_EXECUTABLE}" "-c" "
   116  import sys;import struct;
   117  import sysconfig as s
   118  USE_SYSCONFIG = sys.version_info >= (3, 10)
   119  if not USE_SYSCONFIG:
   120      from distutils import sysconfig as ds
   121  print('.'.join(str(v) for v in sys.version_info));
   122  print(sys.prefix);
   123  if USE_SYSCONFIG:
   124      scheme = s.get_default_scheme()
   125      if scheme == 'posix_local':
   126          # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
   127          scheme = 'posix_prefix'
   128      print(s.get_path('platinclude', scheme))
   129      print(s.get_path('platlib'))
   130      print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))
   131  else:
   132      print(ds.get_python_inc(plat_specific=True));
   133      print(ds.get_python_lib(plat_specific=True));
   134      print(ds.get_config_var('EXT_SUFFIX') or ds.get_config_var('SO'));
   135  print(hasattr(sys, 'gettotalrefcount')+0);
   136  print(struct.calcsize('@P'));
   137  print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
   138  print(s.get_config_var('LIBDIR') or '');
   139  print(s.get_config_var('MULTIARCH') or '');
   140  "
   141    RESULT_VARIABLE _PYTHON_SUCCESS
   142    OUTPUT_VARIABLE _PYTHON_VALUES
   143    ERROR_VARIABLE _PYTHON_ERROR_VALUE)
   144  
   145  if(NOT _PYTHON_SUCCESS MATCHES 0)
   146    if(PythonLibsNew_FIND_REQUIRED)
   147      message(FATAL_ERROR "Python config failure:\n${_PYTHON_ERROR_VALUE}")
   148    endif()
   149    set(PYTHONLIBS_FOUND FALSE)
   150    set(PythonLibsNew_FOUND FALSE)
   151    return()
   152  endif()
   153  
   154  option(
   155    PYBIND11_PYTHONLIBS_OVERWRITE
   156    "Overwrite cached values read from Python library (classic search). Turn off if cross-compiling and manually setting these values."
   157    ON)
   158  # Can manually set values when cross-compiling
   159  macro(_PYBIND11_GET_IF_UNDEF lst index name)
   160    if(PYBIND11_PYTHONLIBS_OVERWRITE OR NOT DEFINED "${name}")
   161      list(GET "${lst}" "${index}" "${name}")
   162    endif()
   163  endmacro()
   164  
   165  # Convert the process output into a list
   166  if(WIN32)
   167    string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
   168  endif()
   169  string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
   170  string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
   171  _pybind11_get_if_undef(_PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
   172  _pybind11_get_if_undef(_PYTHON_VALUES 1 PYTHON_PREFIX)
   173  _pybind11_get_if_undef(_PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
   174  _pybind11_get_if_undef(_PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
   175  _pybind11_get_if_undef(_PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
   176  _pybind11_get_if_undef(_PYTHON_VALUES 5 PYTHON_IS_DEBUG)
   177  _pybind11_get_if_undef(_PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
   178  _pybind11_get_if_undef(_PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
   179  _pybind11_get_if_undef(_PYTHON_VALUES 8 PYTHON_LIBDIR)
   180  _pybind11_get_if_undef(_PYTHON_VALUES 9 PYTHON_MULTIARCH)
   181  
   182  # Make sure the Python has the same pointer-size as the chosen compiler
   183  # Skip if CMAKE_SIZEOF_VOID_P is not defined
   184  # This should be skipped for (non-Apple) cross-compiles (like EMSCRIPTEN)
   185  if(NOT CMAKE_CROSSCOMPILING
   186     AND CMAKE_SIZEOF_VOID_P
   187     AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
   188    if(PythonLibsNew_FIND_REQUIRED)
   189      math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
   190      math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
   191      message(FATAL_ERROR "Python config failure: Python is ${_PYTHON_BITS}-bit, "
   192                          "chosen compiler is  ${_CMAKE_BITS}-bit")
   193    endif()
   194    set(PYTHONLIBS_FOUND FALSE)
   195    set(PythonLibsNew_FOUND FALSE)
   196    return()
   197  endif()
   198  
   199  # The built-in FindPython didn't always give the version numbers
   200  string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
   201  list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
   202  list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
   203  list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
   204  set(PYTHON_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}.${PYTHON_VERSION_PATCH}")
   205  
   206  # Make sure all directory separators are '/'
   207  string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX "${PYTHON_PREFIX}")
   208  string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}")
   209  string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES "${PYTHON_SITE_PACKAGES}")
   210  
   211  if(DEFINED PYTHON_LIBRARY)
   212    # Don't write to PYTHON_LIBRARY if it's already set
   213  elseif(CMAKE_HOST_WIN32)
   214    set(PYTHON_LIBRARY "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
   215  
   216    # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
   217    # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
   218    if(NOT EXISTS "${PYTHON_LIBRARY}")
   219      get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
   220      set(PYTHON_LIBRARY "${_PYTHON_ROOT}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
   221    endif()
   222  
   223    # if we are in MSYS & MINGW, and we didn't find windows python lib, look for system python lib
   224    if(DEFINED ENV{MSYSTEM}
   225       AND MINGW
   226       AND NOT EXISTS "${PYTHON_LIBRARY}")
   227      if(PYTHON_MULTIARCH)
   228        set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
   229      else()
   230        set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
   231      endif()
   232      unset(PYTHON_LIBRARY)
   233      find_library(
   234        PYTHON_LIBRARY
   235        NAMES "python${PYTHON_LIBRARY_SUFFIX}"
   236        PATHS ${_PYTHON_LIBS_SEARCH}
   237        NO_DEFAULT_PATH)
   238    endif()
   239  
   240    # raise an error if the python libs are still not found.
   241    if(NOT EXISTS "${PYTHON_LIBRARY}")
   242      message(FATAL_ERROR "Python libraries not found")
   243    endif()
   244  
   245  else()
   246    if(PYTHON_MULTIARCH)
   247      set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
   248    else()
   249      set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
   250    endif()
   251    #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
   252    # Probably this needs to be more involved. It would be nice if the config
   253    # information the python interpreter itself gave us were more complete.
   254    find_library(
   255      PYTHON_LIBRARY
   256      NAMES "python${PYTHON_LIBRARY_SUFFIX}"
   257      PATHS ${_PYTHON_LIBS_SEARCH}
   258      NO_DEFAULT_PATH)
   259  
   260    # If all else fails, just set the name/version and let the linker figure out the path.
   261    if(NOT PYTHON_LIBRARY)
   262      set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
   263    endif()
   264  endif()
   265  
   266  mark_as_advanced(PYTHON_LIBRARY PYTHON_INCLUDE_DIR)
   267  
   268  # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
   269  # cache entries because they are meant to specify the location of a single
   270  # library. We now set the variables listed by the documentation for this
   271  # module.
   272  set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
   273  set(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
   274  if(NOT PYTHON_DEBUG_LIBRARY)
   275    set(PYTHON_DEBUG_LIBRARY "")
   276  endif()
   277  set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
   278  
   279  find_package_message(PYTHON "Found PythonLibs: ${PYTHON_LIBRARIES}"
   280                       "${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}")
   281  
   282  set(PYTHONLIBS_FOUND TRUE)
   283  set(PythonLibsNew_FOUND TRUE)
   284  
   285  if(NOT PYTHON_MODULE_PREFIX)
   286    set(PYTHON_MODULE_PREFIX "")
   287  endif()