Back to home page

EIC code displayed by LXR

 
 

    


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

0001 # tools/pybind11NewTools.cmake -- Build system for the pybind11 modules
0002 #
0003 # Copyright (c) 2020 Wenzel Jakob <wenzel@inf.ethz.ch> and Henry Schreiner
0004 #
0005 # All rights reserved. Use of this source code is governed by a
0006 # BSD-style license that can be found in the LICENSE file.
0007 
0008 if(CMAKE_VERSION VERSION_LESS 3.12)
0009   message(FATAL_ERROR "You cannot use the new FindPython module with CMake < 3.12")
0010 endif()
0011 
0012 include_guard(DIRECTORY)
0013 
0014 get_property(
0015   is_config
0016   TARGET pybind11::headers
0017   PROPERTY IMPORTED)
0018 
0019 if(pybind11_FIND_QUIETLY)
0020   set(_pybind11_quiet QUIET)
0021 else()
0022   set(_pybind11_quiet "")
0023 endif()
0024 
0025 if(NOT Python_FOUND AND NOT Python3_FOUND)
0026   if(NOT DEFINED Python_FIND_IMPLEMENTATIONS)
0027     set(Python_FIND_IMPLEMENTATIONS CPython PyPy)
0028   endif()
0029 
0030   # GitHub Actions like activation
0031   if(NOT DEFINED Python_ROOT_DIR AND DEFINED ENV{pythonLocation})
0032     set(Python_ROOT_DIR "$ENV{pythonLocation}")
0033   endif()
0034 
0035   find_package(Python 3.6 REQUIRED COMPONENTS Interpreter Development ${_pybind11_quiet})
0036 
0037   # If we are in submodule mode, export the Python targets to global targets.
0038   # If this behavior is not desired, FindPython _before_ pybind11.
0039   if(NOT is_config)
0040     set_property(TARGET Python::Python PROPERTY IMPORTED_GLOBAL TRUE)
0041     set_property(TARGET Python::Interpreter PROPERTY IMPORTED_GLOBAL TRUE)
0042     if(TARGET Python::Module)
0043       set_property(TARGET Python::Module PROPERTY IMPORTED_GLOBAL TRUE)
0044     endif()
0045   endif()
0046 endif()
0047 
0048 if(Python_FOUND)
0049   set(_Python
0050       Python
0051       CACHE INTERNAL "" FORCE)
0052 elseif(Python3_FOUND)
0053   set(_Python
0054       Python3
0055       CACHE INTERNAL "" FORCE)
0056 endif()
0057 
0058 if(PYBIND11_MASTER_PROJECT)
0059   if(${_Python}_INTERPRETER_ID MATCHES "PyPy")
0060     message(STATUS "PyPy ${${_Python}_PyPy_VERSION} (Py ${${_Python}_VERSION})")
0061   else()
0062     message(STATUS "${_Python} ${${_Python}_VERSION}")
0063   endif()
0064 endif()
0065 
0066 # If a user finds Python, they may forget to include the Interpreter component
0067 # and the following two steps require it. It is highly recommended by CMake
0068 # when finding development libraries anyway, so we will require it.
0069 if(NOT DEFINED ${_Python}_EXECUTABLE)
0070   message(
0071     FATAL_ERROR
0072       "${_Python} was found without the Interpreter component. Pybind11 requires this component.")
0073 
0074 endif()
0075 
0076 if(NOT ${_Python}_EXECUTABLE STREQUAL PYBIND11_PYTHON_EXECUTABLE_LAST)
0077   # Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed
0078   unset(PYTHON_IS_DEBUG CACHE)
0079   unset(PYTHON_MODULE_EXTENSION CACHE)
0080   set(PYBIND11_PYTHON_EXECUTABLE_LAST
0081       "${${_Python}_EXECUTABLE}"
0082       CACHE INTERNAL "Python executable during the last CMake run")
0083 endif()
0084 
0085 if(NOT DEFINED PYTHON_IS_DEBUG)
0086   # Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
0087   execute_process(
0088     COMMAND "${${_Python}_EXECUTABLE}" "-c"
0089             "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
0090     RESULT_VARIABLE _PYTHON_IS_DEBUG)
0091   set(PYTHON_IS_DEBUG
0092       "${_PYTHON_IS_DEBUG}"
0093       CACHE INTERNAL "Python debug status")
0094 endif()
0095 
0096 # Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
0097 # required for PyPy3 (as of 7.3.1)
0098 if(NOT DEFINED PYTHON_MODULE_EXTENSION)
0099   execute_process(
0100     COMMAND
0101       "${${_Python}_EXECUTABLE}" "-c"
0102       "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'))"
0103     OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
0104     ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR
0105     OUTPUT_STRIP_TRAILING_WHITESPACE)
0106 
0107   if(_PYTHON_MODULE_EXTENSION STREQUAL "")
0108     message(
0109       FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'"
0110                   "package is not installed. Full error message:\n${_PYTHON_MODULE_EXTENSION_ERR}")
0111   endif()
0112 
0113   # This needs to be available for the pybind11_extension function
0114   set(PYTHON_MODULE_EXTENSION
0115       "${_PYTHON_MODULE_EXTENSION}"
0116       CACHE INTERNAL "")
0117 endif()
0118 
0119 # Python debug libraries expose slightly different objects before 3.8
0120 # https://docs.python.org/3.6/c-api/intro.html#debugging-builds
0121 # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
0122 if(PYTHON_IS_DEBUG)
0123   set_property(
0124     TARGET pybind11::pybind11
0125     APPEND
0126     PROPERTY INTERFACE_COMPILE_DEFINITIONS Py_DEBUG)
0127 endif()
0128 
0129 # Check on every access - since Python can change - do nothing in that case.
0130 
0131 if(DEFINED ${_Python}_INCLUDE_DIRS)
0132   # Only add Python for build - must be added during the import for config
0133   # since it has to be re-discovered.
0134   #
0135   # This needs to be a target to be included after the local pybind11
0136   # directory, just in case there there is an installed pybind11 sitting
0137   # next to Python's includes. It also ensures Python is a SYSTEM library.
0138   add_library(pybind11::python_headers INTERFACE IMPORTED)
0139   set_property(
0140     TARGET pybind11::python_headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
0141                                              "$<BUILD_INTERFACE:${${_Python}_INCLUDE_DIRS}>")
0142   set_property(
0143     TARGET pybind11::pybind11
0144     APPEND
0145     PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_headers)
0146   set(pybind11_INCLUDE_DIRS
0147       "${pybind11_INCLUDE_DIR}" "${${_Python}_INCLUDE_DIRS}"
0148       CACHE INTERNAL "Directories where pybind11 and possibly Python headers are located")
0149 endif()
0150 
0151 # In CMake 3.18+, you can find these separately, so include an if
0152 if(TARGET ${_Python}::Python)
0153   set_property(
0154     TARGET pybind11::embed
0155     APPEND
0156     PROPERTY INTERFACE_LINK_LIBRARIES ${_Python}::Python)
0157 endif()
0158 
0159 # CMake 3.15+ has this
0160 if(TARGET ${_Python}::Module)
0161   set_property(
0162     TARGET pybind11::module
0163     APPEND
0164     PROPERTY INTERFACE_LINK_LIBRARIES ${_Python}::Module)
0165 else()
0166   set_property(
0167     TARGET pybind11::module
0168     APPEND
0169     PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_link_helper)
0170 endif()
0171 
0172 # WITHOUT_SOABI and WITH_SOABI will disable the custom extension handling used by pybind11.
0173 # WITH_SOABI is passed on to python_add_library.
0174 function(pybind11_add_module target_name)
0175   cmake_parse_arguments(PARSE_ARGV 1 ARG
0176                         "STATIC;SHARED;MODULE;THIN_LTO;OPT_SIZE;NO_EXTRAS;WITHOUT_SOABI" "" "")
0177 
0178   if(ARG_STATIC)
0179     set(lib_type STATIC)
0180   elseif(ARG_SHARED)
0181     set(lib_type SHARED)
0182   else()
0183     set(lib_type MODULE)
0184   endif()
0185 
0186   if("${_Python}" STREQUAL "Python")
0187     python_add_library(${target_name} ${lib_type} ${ARG_UNPARSED_ARGUMENTS})
0188   elseif("${_Python}" STREQUAL "Python3")
0189     python3_add_library(${target_name} ${lib_type} ${ARG_UNPARSED_ARGUMENTS})
0190   else()
0191     message(FATAL_ERROR "Cannot detect FindPython version: ${_Python}")
0192   endif()
0193 
0194   target_link_libraries(${target_name} PRIVATE pybind11::headers)
0195 
0196   if(lib_type STREQUAL "MODULE")
0197     target_link_libraries(${target_name} PRIVATE pybind11::module)
0198   else()
0199     target_link_libraries(${target_name} PRIVATE pybind11::embed)
0200   endif()
0201 
0202   if(MSVC)
0203     target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
0204   endif()
0205 
0206   # -fvisibility=hidden is required to allow multiple modules compiled against
0207   # different pybind versions to work properly, and for some features (e.g.
0208   # py::module_local).  We force it on everything inside the `pybind11`
0209   # namespace; also turning it on for a pybind module compilation here avoids
0210   # potential warnings or issues from having mixed hidden/non-hidden types.
0211   if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
0212     set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
0213   endif()
0214 
0215   if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET)
0216     set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
0217   endif()
0218 
0219   # If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions
0220   if(NOT ARG_WITHOUT_SOABI AND NOT "WITH_SOABI" IN_LIST ARG_UNPARSED_ARGUMENTS)
0221     pybind11_extension(${target_name})
0222   endif()
0223 
0224   if(ARG_NO_EXTRAS)
0225     return()
0226   endif()
0227 
0228   if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
0229     if(ARG_THIN_LTO)
0230       target_link_libraries(${target_name} PRIVATE pybind11::thin_lto)
0231     else()
0232       target_link_libraries(${target_name} PRIVATE pybind11::lto)
0233     endif()
0234   endif()
0235 
0236   # Use case-insensitive comparison to match the result of $<CONFIG:cfgs>
0237   string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
0238   if(NOT MSVC AND NOT "${uppercase_CMAKE_BUILD_TYPE}" MATCHES DEBUG|RELWITHDEBINFO)
0239     # Strip unnecessary sections of the binary on Linux/macOS
0240     pybind11_strip(${target_name})
0241   endif()
0242 
0243   if(MSVC)
0244     target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
0245   endif()
0246 
0247   if(ARG_OPT_SIZE)
0248     target_link_libraries(${target_name} PRIVATE pybind11::opt_size)
0249   endif()
0250 endfunction()
0251 
0252 function(pybind11_extension name)
0253   # The extension is precomputed
0254   set_target_properties(${name} PROPERTIES PREFIX "" SUFFIX "${PYTHON_MODULE_EXTENSION}")
0255 
0256 endfunction()