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

     1  # tools/pybind11NewTools.cmake -- Build system for the pybind11 modules
     2  #
     3  # Copyright (c) 2020 Wenzel Jakob <wenzel@inf.ethz.ch> and Henry Schreiner
     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  if(CMAKE_VERSION VERSION_LESS 3.12)
     9    message(FATAL_ERROR "You cannot use the new FindPython module with CMake < 3.12")
    10  endif()
    11  
    12  include_guard(DIRECTORY)
    13  
    14  get_property(
    15    is_config
    16    TARGET pybind11::headers
    17    PROPERTY IMPORTED)
    18  
    19  if(pybind11_FIND_QUIETLY)
    20    set(_pybind11_quiet QUIET)
    21  else()
    22    set(_pybind11_quiet "")
    23  endif()
    24  
    25  if(NOT Python_FOUND AND NOT Python3_FOUND)
    26    if(NOT DEFINED Python_FIND_IMPLEMENTATIONS)
    27      set(Python_FIND_IMPLEMENTATIONS CPython PyPy)
    28    endif()
    29  
    30    # GitHub Actions like activation
    31    if(NOT DEFINED Python_ROOT_DIR AND DEFINED ENV{pythonLocation})
    32      set(Python_ROOT_DIR "$ENV{pythonLocation}")
    33    endif()
    34  
    35    find_package(Python 3.6 REQUIRED COMPONENTS Interpreter Development ${_pybind11_quiet})
    36  
    37    # If we are in submodule mode, export the Python targets to global targets.
    38    # If this behavior is not desired, FindPython _before_ pybind11.
    39    if(NOT is_config)
    40      set_property(TARGET Python::Python PROPERTY IMPORTED_GLOBAL TRUE)
    41      set_property(TARGET Python::Interpreter PROPERTY IMPORTED_GLOBAL TRUE)
    42      if(TARGET Python::Module)
    43        set_property(TARGET Python::Module PROPERTY IMPORTED_GLOBAL TRUE)
    44      endif()
    45    endif()
    46  endif()
    47  
    48  if(Python_FOUND)
    49    set(_Python
    50        Python
    51        CACHE INTERNAL "" FORCE)
    52  elseif(Python3_FOUND)
    53    set(_Python
    54        Python3
    55        CACHE INTERNAL "" FORCE)
    56  endif()
    57  
    58  if(PYBIND11_MASTER_PROJECT)
    59    if(${_Python}_INTERPRETER_ID MATCHES "PyPy")
    60      message(STATUS "PyPy ${${_Python}_PyPy_VERSION} (Py ${${_Python}_VERSION})")
    61    else()
    62      message(STATUS "${_Python} ${${_Python}_VERSION}")
    63    endif()
    64  endif()
    65  
    66  # If a user finds Python, they may forget to include the Interpreter component
    67  # and the following two steps require it. It is highly recommended by CMake
    68  # when finding development libraries anyway, so we will require it.
    69  if(NOT DEFINED ${_Python}_EXECUTABLE)
    70    message(
    71      FATAL_ERROR
    72        "${_Python} was found without the Interpreter component. Pybind11 requires this component.")
    73  
    74  endif()
    75  
    76  if(NOT ${_Python}_EXECUTABLE STREQUAL PYBIND11_PYTHON_EXECUTABLE_LAST)
    77    # Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed
    78    unset(PYTHON_IS_DEBUG CACHE)
    79    unset(PYTHON_MODULE_EXTENSION CACHE)
    80    set(PYBIND11_PYTHON_EXECUTABLE_LAST
    81        "${${_Python}_EXECUTABLE}"
    82        CACHE INTERNAL "Python executable during the last CMake run")
    83  endif()
    84  
    85  if(NOT DEFINED PYTHON_IS_DEBUG)
    86    # Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
    87    execute_process(
    88      COMMAND "${${_Python}_EXECUTABLE}" "-c"
    89              "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
    90      RESULT_VARIABLE _PYTHON_IS_DEBUG)
    91    set(PYTHON_IS_DEBUG
    92        "${_PYTHON_IS_DEBUG}"
    93        CACHE INTERNAL "Python debug status")
    94  endif()
    95  
    96  # Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
    97  # required for PyPy3 (as of 7.3.1)
    98  if(NOT DEFINED PYTHON_MODULE_EXTENSION)
    99    execute_process(
   100      COMMAND
   101        "${${_Python}_EXECUTABLE}" "-c"
   102        "import sys, importlib; s = importlib.import_module('distutils.sysconfig' if sys.version_info < (3, 10) else 'sysconfig'); print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))"
   103      OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
   104      ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR
   105      OUTPUT_STRIP_TRAILING_WHITESPACE)
   106  
   107    if(_PYTHON_MODULE_EXTENSION STREQUAL "")
   108      message(
   109        FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'"
   110                    "package is not installed. Full error message:\n${_PYTHON_MODULE_EXTENSION_ERR}")
   111    endif()
   112  
   113    # This needs to be available for the pybind11_extension function
   114    set(PYTHON_MODULE_EXTENSION
   115        "${_PYTHON_MODULE_EXTENSION}"
   116        CACHE INTERNAL "")
   117  endif()
   118  
   119  # Python debug libraries expose slightly different objects before 3.8
   120  # https://docs.python.org/3.6/c-api/intro.html#debugging-builds
   121  # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
   122  if(PYTHON_IS_DEBUG)
   123    set_property(
   124      TARGET pybind11::pybind11
   125      APPEND
   126      PROPERTY INTERFACE_COMPILE_DEFINITIONS Py_DEBUG)
   127  endif()
   128  
   129  # Check on every access - since Python can change - do nothing in that case.
   130  
   131  if(DEFINED ${_Python}_INCLUDE_DIRS)
   132    # Only add Python for build - must be added during the import for config
   133    # since it has to be re-discovered.
   134    #
   135    # This needs to be a target to be included after the local pybind11
   136    # directory, just in case there there is an installed pybind11 sitting
   137    # next to Python's includes. It also ensures Python is a SYSTEM library.
   138    add_library(pybind11::python_headers INTERFACE IMPORTED)
   139    set_property(
   140      TARGET pybind11::python_headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
   141                                               "$<BUILD_INTERFACE:${${_Python}_INCLUDE_DIRS}>")
   142    set_property(
   143      TARGET pybind11::pybind11
   144      APPEND
   145      PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_headers)
   146    set(pybind11_INCLUDE_DIRS
   147        "${pybind11_INCLUDE_DIR}" "${${_Python}_INCLUDE_DIRS}"
   148        CACHE INTERNAL "Directories where pybind11 and possibly Python headers are located")
   149  endif()
   150  
   151  # In CMake 3.18+, you can find these separately, so include an if
   152  if(TARGET ${_Python}::Python)
   153    set_property(
   154      TARGET pybind11::embed
   155      APPEND
   156      PROPERTY INTERFACE_LINK_LIBRARIES ${_Python}::Python)
   157  endif()
   158  
   159  # CMake 3.15+ has this
   160  if(TARGET ${_Python}::Module)
   161    set_property(
   162      TARGET pybind11::module
   163      APPEND
   164      PROPERTY INTERFACE_LINK_LIBRARIES ${_Python}::Module)
   165  else()
   166    set_property(
   167      TARGET pybind11::module
   168      APPEND
   169      PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_link_helper)
   170  endif()
   171  
   172  # WITHOUT_SOABI and WITH_SOABI will disable the custom extension handling used by pybind11.
   173  # WITH_SOABI is passed on to python_add_library.
   174  function(pybind11_add_module target_name)
   175    cmake_parse_arguments(PARSE_ARGV 1 ARG
   176                          "STATIC;SHARED;MODULE;THIN_LTO;OPT_SIZE;NO_EXTRAS;WITHOUT_SOABI" "" "")
   177  
   178    if(ARG_STATIC)
   179      set(lib_type STATIC)
   180    elseif(ARG_SHARED)
   181      set(lib_type SHARED)
   182    else()
   183      set(lib_type MODULE)
   184    endif()
   185  
   186    if("${_Python}" STREQUAL "Python")
   187      python_add_library(${target_name} ${lib_type} ${ARG_UNPARSED_ARGUMENTS})
   188    elseif("${_Python}" STREQUAL "Python3")
   189      python3_add_library(${target_name} ${lib_type} ${ARG_UNPARSED_ARGUMENTS})
   190    else()
   191      message(FATAL_ERROR "Cannot detect FindPython version: ${_Python}")
   192    endif()
   193  
   194    target_link_libraries(${target_name} PRIVATE pybind11::headers)
   195  
   196    if(lib_type STREQUAL "MODULE")
   197      target_link_libraries(${target_name} PRIVATE pybind11::module)
   198    else()
   199      target_link_libraries(${target_name} PRIVATE pybind11::embed)
   200    endif()
   201  
   202    if(MSVC)
   203      target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
   204    endif()
   205  
   206    # -fvisibility=hidden is required to allow multiple modules compiled against
   207    # different pybind versions to work properly, and for some features (e.g.
   208    # py::module_local).  We force it on everything inside the `pybind11`
   209    # namespace; also turning it on for a pybind module compilation here avoids
   210    # potential warnings or issues from having mixed hidden/non-hidden types.
   211    if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
   212      set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
   213    endif()
   214  
   215    if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET)
   216      set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
   217    endif()
   218  
   219    # If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions
   220    if(NOT ARG_WITHOUT_SOABI AND NOT "WITH_SOABI" IN_LIST ARG_UNPARSED_ARGUMENTS)
   221      pybind11_extension(${target_name})
   222    endif()
   223  
   224    if(ARG_NO_EXTRAS)
   225      return()
   226    endif()
   227  
   228    if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
   229      if(ARG_THIN_LTO)
   230        target_link_libraries(${target_name} PRIVATE pybind11::thin_lto)
   231      else()
   232        target_link_libraries(${target_name} PRIVATE pybind11::lto)
   233      endif()
   234    endif()
   235  
   236    # Use case-insensitive comparison to match the result of $<CONFIG:cfgs>
   237    string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
   238    if(NOT MSVC AND NOT "${uppercase_CMAKE_BUILD_TYPE}" MATCHES DEBUG|RELWITHDEBINFO)
   239      # Strip unnecessary sections of the binary on Linux/macOS
   240      pybind11_strip(${target_name})
   241    endif()
   242  
   243    if(MSVC)
   244      target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
   245    endif()
   246  
   247    if(ARG_OPT_SIZE)
   248      target_link_libraries(${target_name} PRIVATE pybind11::opt_size)
   249    endif()
   250  endfunction()
   251  
   252  function(pybind11_extension name)
   253    # The extension is precomputed
   254    set_target_properties(${name} PROPERTIES PREFIX "" SUFFIX "${PYTHON_MODULE_EXTENSION}")
   255  
   256  endfunction()