Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #[======================================================[.rst
0002 
0003 Adds the following targets::
0004 
0005     pybind11::pybind11 - link to headers and pybind11
0006     pybind11::module - Adds module links
0007     pybind11::embed - Adds embed links
0008     pybind11::lto - Link time optimizations (manual selection)
0009     pybind11::thin_lto - Link time optimizations (manual selection)
0010     pybind11::python_link_helper - Adds link to Python libraries
0011     pybind11::windows_extras - MSVC bigobj and mp for building multithreaded
0012     pybind11::opt_size - avoid optimizations that increase code size
0013 
0014 Adds the following functions::
0015 
0016     pybind11_strip(target) - strip target after building on linux/macOS
0017     pybind11_find_import(module) - See if a module is installed.
0018 
0019 #]======================================================]
0020 
0021 # CMake 3.10 has an include_guard command, but we can't use that yet
0022 # include_guard(global) (pre-CMake 3.10)
0023 if(TARGET pybind11::lto)
0024   return()
0025 endif()
0026 
0027 # If we are in subdirectory mode, all IMPORTED targets must be GLOBAL. If we
0028 # are in CONFIG mode, they should be "normal" targets instead.
0029 # In CMake 3.11+ you can promote a target to global after you create it,
0030 # which might be simpler than this check.
0031 get_property(
0032   is_config
0033   TARGET pybind11::headers
0034   PROPERTY IMPORTED)
0035 if(NOT is_config)
0036   set(optional_global GLOBAL)
0037 endif()
0038 
0039 # If not run in Python mode, we still would like this to at least
0040 # include pybind11's include directory:
0041 set(pybind11_INCLUDE_DIRS
0042     "${pybind11_INCLUDE_DIR}"
0043     CACHE INTERNAL "Include directory for pybind11 (Python not requested)")
0044 
0045 # --------------------- Shared targets ----------------------------
0046 
0047 # Build an interface library target:
0048 add_library(pybind11::pybind11 IMPORTED INTERFACE ${optional_global})
0049 set_property(
0050   TARGET pybind11::pybind11
0051   APPEND
0052   PROPERTY INTERFACE_LINK_LIBRARIES pybind11::headers)
0053 
0054 # Build a module target:
0055 add_library(pybind11::module IMPORTED INTERFACE ${optional_global})
0056 set_property(
0057   TARGET pybind11::module
0058   APPEND
0059   PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11)
0060 
0061 # Build an embed library target:
0062 add_library(pybind11::embed IMPORTED INTERFACE ${optional_global})
0063 set_property(
0064   TARGET pybind11::embed
0065   APPEND
0066   PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11)
0067 
0068 # --------------------------- link helper ---------------------------
0069 
0070 add_library(pybind11::python_link_helper IMPORTED INTERFACE ${optional_global})
0071 
0072 if(CMAKE_VERSION VERSION_LESS 3.13)
0073   # In CMake 3.11+, you can set INTERFACE properties via the normal methods, and
0074   # this would be simpler.
0075   set_property(
0076     TARGET pybind11::python_link_helper
0077     APPEND
0078     PROPERTY INTERFACE_LINK_LIBRARIES "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>")
0079 else()
0080   # link_options was added in 3.13+
0081   # This is safer, because you are ensured the deduplication pass in CMake will not consider
0082   # these separate and remove one but not the other.
0083   set_property(
0084     TARGET pybind11::python_link_helper
0085     APPEND
0086     PROPERTY INTERFACE_LINK_OPTIONS "$<$<PLATFORM_ID:Darwin>:LINKER:-undefined,dynamic_lookup>")
0087 endif()
0088 
0089 # ------------------------ Windows extras -------------------------
0090 
0091 add_library(pybind11::windows_extras IMPORTED INTERFACE ${optional_global})
0092 
0093 if(MSVC) # That's also clang-cl
0094   # /bigobj is needed for bigger binding projects due to the limit to 64k
0095   # addressable sections
0096   set_property(
0097     TARGET pybind11::windows_extras
0098     APPEND
0099     PROPERTY INTERFACE_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:CXX>:/bigobj>)
0100 
0101   # /MP enables multithreaded builds (relevant when there are many files) for MSVC
0102   if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # no Clang no Intel
0103     if(CMAKE_VERSION VERSION_LESS 3.11)
0104       set_property(
0105         TARGET pybind11::windows_extras
0106         APPEND
0107         PROPERTY INTERFACE_COMPILE_OPTIONS $<$<NOT:$<CONFIG:Debug>>:/MP>)
0108     else()
0109       # Only set these options for C++ files.  This is important so that, for
0110       # instance, projects that include other types of source files like CUDA
0111       # .cu files don't get these options propagated to nvcc since that would
0112       # cause the build to fail.
0113       set_property(
0114         TARGET pybind11::windows_extras
0115         APPEND
0116         PROPERTY INTERFACE_COMPILE_OPTIONS
0117                  $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>)
0118     endif()
0119   endif()
0120 endif()
0121 
0122 # ----------------------- Optimize binary size --------------------------
0123 
0124 add_library(pybind11::opt_size IMPORTED INTERFACE ${optional_global})
0125 
0126 if(MSVC)
0127   set(PYBIND11_OPT_SIZE /Os)
0128 else()
0129   set(PYBIND11_OPT_SIZE -Os)
0130 endif()
0131 
0132 set_property(
0133   TARGET pybind11::opt_size
0134   APPEND
0135   PROPERTY INTERFACE_COMPILE_OPTIONS $<$<CONFIG:Release>:${PYBIND11_OPT_SIZE}>
0136            $<$<CONFIG:MinSizeRel>:${PYBIND11_OPT_SIZE}>
0137            $<$<CONFIG:RelWithDebInfo>:${PYBIND11_OPT_SIZE}>)
0138 
0139 # ----------------------- Legacy option --------------------------
0140 
0141 # Warn or error if old variable name used
0142 if(PYBIND11_CPP_STANDARD)
0143   string(REGEX MATCH [[..$]] VAL "${PYBIND11_CPP_STANDARD}")
0144   if(CMAKE_CXX_STANDARD)
0145     if(NOT CMAKE_CXX_STANDARD STREQUAL VAL)
0146       message(WARNING "CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} does not match "
0147                       "PYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}, "
0148                       "please remove PYBIND11_CPP_STANDARD from your cache")
0149     endif()
0150   else()
0151     set(supported_standards 11 14 17 20)
0152     if("${VAL}" IN_LIST supported_standards)
0153       message(WARNING "USE -DCMAKE_CXX_STANDARD=${VAL} instead of PYBIND11_CPP_STANDARD")
0154       set(CMAKE_CXX_STANDARD
0155           ${VAL}
0156           CACHE STRING "From PYBIND11_CPP_STANDARD")
0157     else()
0158       message(FATAL_ERROR "PYBIND11_CPP_STANDARD should be replaced with CMAKE_CXX_STANDARD "
0159                           "(last two chars: ${VAL} not understood as a valid CXX std)")
0160     endif()
0161   endif()
0162 endif()
0163 
0164 # --------------------- Python specifics -------------------------
0165 
0166 # Check to see which Python mode we are in, new, old, or no python
0167 if(PYBIND11_NOPYTHON)
0168   set(_pybind11_nopython ON)
0169 elseif(
0170   PYBIND11_FINDPYTHON
0171   OR Python_FOUND
0172   OR Python2_FOUND
0173   OR Python3_FOUND)
0174   # New mode
0175   include("${CMAKE_CURRENT_LIST_DIR}/pybind11NewTools.cmake")
0176 
0177 else()
0178 
0179   # Classic mode
0180   include("${CMAKE_CURRENT_LIST_DIR}/pybind11Tools.cmake")
0181 
0182 endif()
0183 
0184 # --------------------- pybind11_find_import -------------------------------
0185 
0186 if(NOT _pybind11_nopython)
0187   # Check to see if modules are importable. Use REQUIRED to force an error if
0188   # one of the modules is not found. <package_name>_FOUND will be set if the
0189   # package was found (underscores replace dashes if present). QUIET will hide
0190   # the found message, and VERSION will require a minimum version. A successful
0191   # find will cache the result.
0192   function(pybind11_find_import PYPI_NAME)
0193     # CMake variables need underscores (PyPI doesn't care)
0194     string(REPLACE "-" "_" NORM_PYPI_NAME "${PYPI_NAME}")
0195 
0196     # Return if found previously
0197     if(${NORM_PYPI_NAME}_FOUND)
0198       return()
0199     endif()
0200 
0201     set(options "REQUIRED;QUIET")
0202     set(oneValueArgs "VERSION")
0203     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "" ${ARGN})
0204 
0205     if(ARG_REQUIRED)
0206       set(status_level FATAL_ERROR)
0207     else()
0208       set(status_level WARNING)
0209     endif()
0210 
0211     execute_process(
0212       COMMAND
0213         ${${_Python}_EXECUTABLE} -c
0214         "from pkg_resources import get_distribution; print(get_distribution('${PYPI_NAME}').version)"
0215       RESULT_VARIABLE RESULT_PRESENT
0216       OUTPUT_VARIABLE PKG_VERSION
0217       ERROR_QUIET)
0218 
0219     string(STRIP "${PKG_VERSION}" PKG_VERSION)
0220 
0221     # If a result is present, this failed
0222     if(RESULT_PRESENT)
0223       set(${NORM_PYPI_NAME}_FOUND
0224           ${NORM_PYPI_NAME}-NOTFOUND
0225           CACHE INTERNAL "")
0226       # Always warn or error
0227       message(
0228         ${status_level}
0229         "Missing: ${PYPI_NAME} ${ARG_VERSION}\nTry: ${${_Python}_EXECUTABLE} -m pip install ${PYPI_NAME}"
0230       )
0231     else()
0232       if(ARG_VERSION AND PKG_VERSION VERSION_LESS ARG_VERSION)
0233         message(
0234           ${status_level}
0235           "Version incorrect: ${PYPI_NAME} ${PKG_VERSION} found, ${ARG_VERSION} required - try upgrading"
0236         )
0237       else()
0238         set(${NORM_PYPI_NAME}_FOUND
0239             YES
0240             CACHE INTERNAL "")
0241         set(${NORM_PYPI_NAME}_VERSION
0242             ${PKG_VERSION}
0243             CACHE INTERNAL "")
0244       endif()
0245       if(NOT ARG_QUIET)
0246         message(STATUS "Found ${PYPI_NAME} ${PKG_VERSION}")
0247       endif()
0248     endif()
0249     if(NOT ARG_VERSION OR (NOT PKG_VERSION VERSION_LESS ARG_VERSION))
0250       # We have successfully found a good version, cache to avoid calling again.
0251     endif()
0252   endfunction()
0253 endif()
0254 
0255 # --------------------- LTO -------------------------------
0256 
0257 include(CheckCXXCompilerFlag)
0258 
0259 # Checks whether the given CXX/linker flags can compile and link a cxx file.
0260 # cxxflags and linkerflags are lists of flags to use.  The result variable is a
0261 # unique variable name for each set of flags: the compilation result will be
0262 # cached base on the result variable.  If the flags work, sets them in
0263 # cxxflags_out/linkerflags_out internal cache variables (in addition to
0264 # ${result}).
0265 function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerflags cxxflags_out
0266          linkerflags_out)
0267   set(CMAKE_REQUIRED_LIBRARIES ${linkerflags})
0268   check_cxx_compiler_flag("${cxxflags}" ${result})
0269   if(${result})
0270     set(${cxxflags_out}
0271         "${cxxflags}"
0272         PARENT_SCOPE)
0273     set(${linkerflags_out}
0274         "${linkerflags}"
0275         PARENT_SCOPE)
0276   endif()
0277 endfunction()
0278 
0279 function(_pybind11_generate_lto target prefer_thin_lto)
0280   if(MINGW)
0281     message(STATUS "${target} disabled (problems with undefined symbols for MinGW for now)")
0282     return()
0283   endif()
0284 
0285   if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
0286     set(cxx_append "")
0287     set(linker_append "")
0288     if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE)
0289       # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it
0290       set(linker_append ";$<$<CONFIG:MinSizeRel>:-O3>")
0291     elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT MINGW)
0292       set(cxx_append ";-fno-fat-lto-objects")
0293     endif()
0294 
0295     if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le" OR CMAKE_SYSTEM_PROCESSOR MATCHES "mips64")
0296       set(NO_FLTO_ARCH TRUE)
0297     else()
0298       set(NO_FLTO_ARCH FALSE)
0299     endif()
0300 
0301     if(CMAKE_CXX_COMPILER_ID MATCHES "Clang"
0302        AND prefer_thin_lto
0303        AND NOT NO_FLTO_ARCH)
0304       _pybind11_return_if_cxx_and_linker_flags_work(
0305         HAS_FLTO_THIN "-flto=thin${cxx_append}" "-flto=thin${linker_append}"
0306         PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
0307     endif()
0308 
0309     if(NOT HAS_FLTO_THIN AND NOT NO_FLTO_ARCH)
0310       _pybind11_return_if_cxx_and_linker_flags_work(
0311         HAS_FLTO "-flto${cxx_append}" "-flto${linker_append}" PYBIND11_LTO_CXX_FLAGS
0312         PYBIND11_LTO_LINKER_FLAGS)
0313     endif()
0314   elseif(CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM")
0315     # IntelLLVM equivalent to LTO is called IPO; also IntelLLVM is WIN32/UNIX
0316     # WARNING/HELP WANTED: This block of code is currently not covered by pybind11 GitHub Actions!
0317     if(WIN32)
0318       _pybind11_return_if_cxx_and_linker_flags_work(
0319         HAS_INTEL_IPO "-Qipo" "-Qipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
0320     else()
0321       _pybind11_return_if_cxx_and_linker_flags_work(
0322         HAS_INTEL_IPO "-ipo" "-ipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
0323     endif()
0324   elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
0325     # Intel equivalent to LTO is called IPO
0326     _pybind11_return_if_cxx_and_linker_flags_work(HAS_INTEL_IPO "-ipo" "-ipo"
0327                                                   PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
0328   elseif(MSVC)
0329     # cmake only interprets libraries as linker flags when they start with a - (otherwise it
0330     # converts /LTCG to \LTCG as if it was a Windows path).  Luckily MSVC supports passing flags
0331     # with - instead of /, even if it is a bit non-standard:
0332     _pybind11_return_if_cxx_and_linker_flags_work(HAS_MSVC_GL_LTCG "/GL" "-LTCG"
0333                                                   PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
0334   endif()
0335 
0336   # Enable LTO flags if found, except for Debug builds
0337   if(PYBIND11_LTO_CXX_FLAGS)
0338     # CONFIG takes multiple values in CMake 3.19+, until then we have to use OR
0339     set(is_debug "$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>")
0340     set(not_debug "$<NOT:${is_debug}>")
0341     set(cxx_lang "$<COMPILE_LANGUAGE:CXX>")
0342     if(MSVC AND CMAKE_VERSION VERSION_LESS 3.11)
0343       set(genex "${not_debug}")
0344     else()
0345       set(genex "$<AND:${not_debug},${cxx_lang}>")
0346     endif()
0347     set_property(
0348       TARGET ${target}
0349       APPEND
0350       PROPERTY INTERFACE_COMPILE_OPTIONS "$<${genex}:${PYBIND11_LTO_CXX_FLAGS}>")
0351     if(CMAKE_PROJECT_NAME STREQUAL "pybind11")
0352       message(STATUS "${target} enabled")
0353     endif()
0354   else()
0355     if(CMAKE_PROJECT_NAME STREQUAL "pybind11")
0356       message(STATUS "${target} disabled (not supported by the compiler and/or linker)")
0357     endif()
0358   endif()
0359 
0360   if(PYBIND11_LTO_LINKER_FLAGS)
0361     if(CMAKE_VERSION VERSION_LESS 3.11)
0362       set_property(
0363         TARGET ${target}
0364         APPEND
0365         PROPERTY INTERFACE_LINK_LIBRARIES "$<${not_debug}:${PYBIND11_LTO_LINKER_FLAGS}>")
0366     else()
0367       set_property(
0368         TARGET ${target}
0369         APPEND
0370         PROPERTY INTERFACE_LINK_OPTIONS "$<${not_debug}:${PYBIND11_LTO_LINKER_FLAGS}>")
0371     endif()
0372   endif()
0373 endfunction()
0374 
0375 add_library(pybind11::lto IMPORTED INTERFACE ${optional_global})
0376 _pybind11_generate_lto(pybind11::lto FALSE)
0377 
0378 add_library(pybind11::thin_lto IMPORTED INTERFACE ${optional_global})
0379 _pybind11_generate_lto(pybind11::thin_lto TRUE)
0380 
0381 # ---------------------- pybind11_strip -----------------------------
0382 
0383 function(pybind11_strip target_name)
0384   # Strip unnecessary sections of the binary on Linux/macOS
0385   if(CMAKE_STRIP)
0386     if(APPLE)
0387       set(x_opt -x)
0388     endif()
0389 
0390     add_custom_command(
0391       TARGET ${target_name}
0392       POST_BUILD
0393       COMMAND ${CMAKE_STRIP} ${x_opt} $<TARGET_FILE:${target_name}>)
0394   endif()
0395 endfunction()