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

     1  # tools/pybind11Tools.cmake -- Build system for the pybind11 modules
     2  #
     3  # Copyright (c) 2020 Wenzel Jakob <wenzel.jakob@epfl.ch>
     4  #
     5  # All rights reserved. Use of this source code is governed by a
     6  # BSD-style license that can be found in the LICENSE file.
     7  
     8  # include_guard(global) (pre-CMake 3.10)
     9  if(TARGET pybind11::python_headers)
    10    return()
    11  endif()
    12  
    13  # Built-in in CMake 3.5+
    14  include(CMakeParseArguments)
    15  
    16  if(pybind11_FIND_QUIETLY)
    17    set(_pybind11_quiet QUIET)
    18  else()
    19    set(_pybind11_quiet "")
    20  endif()
    21  
    22  # If this is the first run, PYTHON_VERSION can stand in for PYBIND11_PYTHON_VERSION
    23  if(NOT DEFINED PYBIND11_PYTHON_VERSION AND DEFINED PYTHON_VERSION)
    24    message(WARNING "Set PYBIND11_PYTHON_VERSION to search for a specific version, not "
    25                    "PYTHON_VERSION (which is an output). Assuming that is what you "
    26                    "meant to do and continuing anyway.")
    27    set(PYBIND11_PYTHON_VERSION
    28        "${PYTHON_VERSION}"
    29        CACHE STRING "Python version to use for compiling modules")
    30    unset(PYTHON_VERSION)
    31    unset(PYTHON_VERSION CACHE)
    32  elseif(DEFINED PYBIND11_PYTHON_VERSION)
    33    # If this is set as a normal variable, promote it
    34    set(PYBIND11_PYTHON_VERSION
    35        "${PYBIND11_PYTHON_VERSION}"
    36        CACHE STRING "Python version to use for compiling modules")
    37  else()
    38    # Make an empty cache variable.
    39    set(PYBIND11_PYTHON_VERSION
    40        ""
    41        CACHE STRING "Python version to use for compiling modules")
    42  endif()
    43  
    44  # A user can set versions manually too
    45  set(Python_ADDITIONAL_VERSIONS
    46      "3.11;3.10;3.9;3.8;3.7;3.6"
    47      CACHE INTERNAL "")
    48  
    49  list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
    50  find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED ${_pybind11_quiet})
    51  list(REMOVE_AT CMAKE_MODULE_PATH -1)
    52  
    53  # Makes a normal variable a cached variable
    54  macro(_PYBIND11_PROMOTE_TO_CACHE NAME)
    55    set(_tmp_ptc "${${NAME}}")
    56    # CMake 3.21 complains if a cached variable is shadowed by a normal one
    57    unset(${NAME})
    58    set(${NAME}
    59        "${_tmp_ptc}"
    60        CACHE INTERNAL "")
    61  endmacro()
    62  
    63  # Cache variables so pybind11_add_module can be used in parent projects
    64  _pybind11_promote_to_cache(PYTHON_INCLUDE_DIRS)
    65  _pybind11_promote_to_cache(PYTHON_LIBRARIES)
    66  _pybind11_promote_to_cache(PYTHON_MODULE_PREFIX)
    67  _pybind11_promote_to_cache(PYTHON_MODULE_EXTENSION)
    68  _pybind11_promote_to_cache(PYTHON_VERSION_MAJOR)
    69  _pybind11_promote_to_cache(PYTHON_VERSION_MINOR)
    70  _pybind11_promote_to_cache(PYTHON_VERSION)
    71  _pybind11_promote_to_cache(PYTHON_IS_DEBUG)
    72  
    73  if(PYBIND11_MASTER_PROJECT)
    74    if(PYTHON_MODULE_EXTENSION MATCHES "pypy")
    75      if(NOT DEFINED PYPY_VERSION)
    76        execute_process(
    77          COMMAND ${PYTHON_EXECUTABLE} -c
    78                  [=[import sys; sys.stdout.write(".".join(map(str, sys.pypy_version_info[:3])))]=]
    79          OUTPUT_VARIABLE pypy_version)
    80        set(PYPY_VERSION
    81            ${pypy_version}
    82            CACHE INTERNAL "")
    83      endif()
    84      message(STATUS "PYPY ${PYPY_VERSION} (Py ${PYTHON_VERSION})")
    85    else()
    86      message(STATUS "PYTHON ${PYTHON_VERSION}")
    87    endif()
    88  endif()
    89  
    90  # Only add Python for build - must be added during the import for config since
    91  # it has to be re-discovered.
    92  #
    93  # This needs to be an target to it is included after the local pybind11
    94  # directory, just in case there are multiple versions of pybind11, we want the
    95  # one we expect.
    96  add_library(pybind11::python_headers INTERFACE IMPORTED)
    97  set_property(TARGET pybind11::python_headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
    98                                                        "$<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>")
    99  set_property(
   100    TARGET pybind11::pybind11
   101    APPEND
   102    PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_headers)
   103  
   104  set(pybind11_INCLUDE_DIRS
   105      "${pybind11_INCLUDE_DIR}" "${PYTHON_INCLUDE_DIRS}"
   106      CACHE INTERNAL "Directories where pybind11 and possibly Python headers are located")
   107  
   108  # Python debug libraries expose slightly different objects before 3.8
   109  # https://docs.python.org/3.6/c-api/intro.html#debugging-builds
   110  # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
   111  if(PYTHON_IS_DEBUG)
   112    set_property(
   113      TARGET pybind11::pybind11
   114      APPEND
   115      PROPERTY INTERFACE_COMPILE_DEFINITIONS Py_DEBUG)
   116  endif()
   117  
   118  # The <3.11 code here does not support release/debug builds at the same time, like on vcpkg
   119  if(CMAKE_VERSION VERSION_LESS 3.11)
   120    set_property(
   121      TARGET pybind11::module
   122      APPEND
   123      PROPERTY
   124        INTERFACE_LINK_LIBRARIES
   125        pybind11::python_link_helper
   126        "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES}>>"
   127    )
   128  
   129    set_property(
   130      TARGET pybind11::embed
   131      APPEND
   132      PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
   133  else()
   134    # The IMPORTED INTERFACE library here is to ensure that "debug" and "release" get processed outside
   135    # of a generator expression - https://gitlab.kitware.com/cmake/cmake/-/issues/18424, as they are
   136    # target_link_library keywords rather than real libraries.
   137    add_library(pybind11::_ClassicPythonLibraries IMPORTED INTERFACE)
   138    target_link_libraries(pybind11::_ClassicPythonLibraries INTERFACE ${PYTHON_LIBRARIES})
   139    target_link_libraries(
   140      pybind11::module
   141      INTERFACE
   142        pybind11::python_link_helper
   143        "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:pybind11::_ClassicPythonLibraries>")
   144  
   145    target_link_libraries(pybind11::embed INTERFACE pybind11::pybind11
   146                                                    pybind11::_ClassicPythonLibraries)
   147  endif()
   148  
   149  function(pybind11_extension name)
   150    # The prefix and extension are provided by FindPythonLibsNew.cmake
   151    set_target_properties(${name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
   152                                             SUFFIX "${PYTHON_MODULE_EXTENSION}")
   153  endfunction()
   154  
   155  # Build a Python extension module:
   156  # pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
   157  #                     [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...])
   158  #
   159  function(pybind11_add_module target_name)
   160    set(options "MODULE;SHARED;EXCLUDE_FROM_ALL;NO_EXTRAS;SYSTEM;THIN_LTO;OPT_SIZE")
   161    cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})
   162  
   163    if(ARG_MODULE AND ARG_SHARED)
   164      message(FATAL_ERROR "Can't be both MODULE and SHARED")
   165    elseif(ARG_SHARED)
   166      set(lib_type SHARED)
   167    else()
   168      set(lib_type MODULE)
   169    endif()
   170  
   171    if(ARG_EXCLUDE_FROM_ALL)
   172      set(exclude_from_all EXCLUDE_FROM_ALL)
   173    else()
   174      set(exclude_from_all "")
   175    endif()
   176  
   177    add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS})
   178  
   179    target_link_libraries(${target_name} PRIVATE pybind11::module)
   180  
   181    if(ARG_SYSTEM)
   182      message(
   183        STATUS
   184          "Warning: this does not have an effect - use NO_SYSTEM_FROM_IMPORTED if using imported targets"
   185      )
   186    endif()
   187  
   188    pybind11_extension(${target_name})
   189  
   190    # -fvisibility=hidden is required to allow multiple modules compiled against
   191    # different pybind versions to work properly, and for some features (e.g.
   192    # py::module_local).  We force it on everything inside the `pybind11`
   193    # namespace; also turning it on for a pybind module compilation here avoids
   194    # potential warnings or issues from having mixed hidden/non-hidden types.
   195    if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
   196      set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
   197    endif()
   198  
   199    if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET)
   200      set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
   201    endif()
   202  
   203    if(ARG_NO_EXTRAS)
   204      return()
   205    endif()
   206  
   207    if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
   208      if(ARG_THIN_LTO)
   209        target_link_libraries(${target_name} PRIVATE pybind11::thin_lto)
   210      else()
   211        target_link_libraries(${target_name} PRIVATE pybind11::lto)
   212      endif()
   213    endif()
   214  
   215    # Use case-insensitive comparison to match the result of $<CONFIG:cfgs>
   216    string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
   217    if(NOT MSVC AND NOT "${uppercase_CMAKE_BUILD_TYPE}" MATCHES DEBUG|RELWITHDEBINFO)
   218      pybind11_strip(${target_name})
   219    endif()
   220  
   221    if(MSVC)
   222      target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
   223    endif()
   224  
   225    if(ARG_OPT_SIZE)
   226      target_link_libraries(${target_name} PRIVATE pybind11::opt_size)
   227    endif()
   228  endfunction()
   229  
   230  # Provide general way to call common Python commands in "common" file.
   231  set(_Python
   232      PYTHON
   233      CACHE INTERNAL "" FORCE)