diff --git a/.appveyor.yml b/.appveyor.yml index fbcf8a3784..b33a4ccf4e 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -26,7 +26,7 @@ install: $env:CMAKE_INCLUDE_PATH = "eigen-eigen-67e894c6cd8f;$env:CMAKE_INCLUDE_PATH" build_script: - cmake -G "%CMAKE_GENERATOR%" -A "%CMAKE_ARCH%" - -DPYBIND11_CPP_STANDARD=/std:c++14 + -DCMAKE_CXX_STANDARD=14 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DCMAKE_SUPPRESS_REGENERATION=1 diff --git a/.cmake-format.yaml b/.cmake-format.yaml new file mode 100644 index 0000000000..a2a69f3f89 --- /dev/null +++ b/.cmake-format.yaml @@ -0,0 +1,73 @@ +parse: + additional_commands: + pybind11_add_module: + flags: + - THIN_LTO + - MODULE + - SHARED + - NO_EXTRAS + - EXCLUDE_FROM_ALL + - SYSTEM + +format: + line_width: 99 + tab_size: 2 + + # If an argument group contains more than this many sub-groups + # (parg or kwarg groups) then force it to a vertical layout. + max_subgroups_hwrap: 2 + + # If a positional argument group contains more than this many + # arguments, then force it to a vertical layout. + max_pargs_hwrap: 6 + + # If a cmdline positional group consumes more than this many + # lines without nesting, then invalidate the layout (and nest) + max_rows_cmdline: 2 + separate_ctrl_name_with_space: false + separate_fn_name_with_space: false + dangle_parens: false + + # If the trailing parenthesis must be 'dangled' on its on + # 'line, then align it to this reference: `prefix`: the start' + # 'of the statement, `prefix-indent`: the start of the' + # 'statement, plus one indentation level, `child`: align to' + # the column of the arguments + dangle_align: prefix + # If the statement spelling length (including space and + # parenthesis) is smaller than this amount, then force reject + # nested layouts. + min_prefix_chars: 4 + + # If the statement spelling length (including space and + # parenthesis) is larger than the tab width by more than this + # amount, then force reject un-nested layouts. + max_prefix_chars: 10 + + # If a candidate layout is wrapped horizontally but it exceeds + # this many lines, then reject the layout. + max_lines_hwrap: 2 + + line_ending: unix + + # Format command names consistently as 'lower' or 'upper' case + command_case: canonical + + # Format keywords consistently as 'lower' or 'upper' case + # unchanged is valid too + keyword_case: 'upper' + + # A list of command names which should always be wrapped + always_wrap: [] + + # If true, the argument lists which are known to be sortable + # will be sorted lexicographically + enable_sort: true + + # If true, the parsers may infer whether or not an argument + # list is sortable (without annotation). + autosort: false + +# Causes a few issues - can be solved later, possibly. +markup: + enable_markup: false diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9673485dd5..fc9d04c924 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -95,7 +95,6 @@ jobs: - name: Configure C++11 shell: bash run: > - cmake --version && cmake -S . -B build -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON @@ -107,10 +106,10 @@ jobs: run: cmake --build build -j 2 - name: Python tests C++11 - run: cmake --build build --target pytest -j 2 -v + run: cmake --build build --target pytest -j 2 - name: C++11 tests - run: cmake --build build --target cpptest -j 2 -v + run: cmake --build build --target cpptest -j 2 - name: Interface test C++11 run: cmake --build build --target test_cmake_build @@ -119,7 +118,7 @@ jobs: shell: bash run: > cmake -S . -B build2 - -DPYBIND17_WERROR=ON + -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=${{ matrix.max-cxx-std }} @@ -163,7 +162,6 @@ jobs: - name: Configure shell: bash run: > - cmake --version && cmake -S . -B build -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON @@ -210,7 +208,6 @@ jobs: - name: Configure shell: bash run: > - cmake --version && cmake -S . -B build -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON @@ -256,7 +253,6 @@ jobs: - name: Configure shell: bash run: > - cmake --version && cmake -S . -B build -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON diff --git a/.github/workflows/configure.yml b/.github/workflows/configure.yml index a23276a346..96904341de 100644 --- a/.github/workflows/configure.yml +++ b/.github/workflows/configure.yml @@ -34,33 +34,31 @@ jobs: - name: Make build directories run: | - mkdir build2.8 mkdir build3.7 + mkdir build3.11 mkdir build3.18 - - name: Setup CMake 2.8 + - name: Setup CMake 3.7 uses: jwlawson/actions-setup-cmake@v1.3 with: - cmake-version: 2.8 + cmake-version: 3.7 - - name: Configure 2.8 - working-directory: build2.8 + - name: Configure 3.7 + working-directory: build3.7 run: > - cmake --version && cmake .. -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)") - - name: Setup CMake 3.7 + - name: Setup CMake 3.11 uses: jwlawson/actions-setup-cmake@v1.3 with: - cmake-version: 3.7 + cmake-version: 3.11 - - name: Configure 3.7 - working-directory: build3.7 + - name: Configure 3.11 + working-directory: build3.11 run: > - cmake --version && cmake .. -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON @@ -74,7 +72,6 @@ jobs: - name: Configure 3.18 working-directory: build3.18 run: > - cmake --version && cmake .. -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3e68d34f07..7fb3a13b70 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,6 +26,12 @@ repos: additional_dependencies: [flake8-bugbear, pep8-naming] exclude: ^(docs/.*|tools/.*)$ +- repo: https://github.com/cheshirekow/cmake-format-precommit + rev: v0.6.10 + hooks: + - id: cmake-format + additional_dependencies: [pyyaml] + - repo: local hooks: - id: check-style diff --git a/CMakeLists.txt b/CMakeLists.txt index 85ecd9028f..31e253f9dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,153 +5,244 @@ # All rights reserved. Use of this source code is governed by a # BSD-style license that can be found in the LICENSE file. -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.7) -if (POLICY CMP0048) - # cmake warns if loaded from a min-3.0-required parent dir, so silence the warning: - cmake_policy(SET CMP0048 NEW) +# VERSION 3.7...3.18, but some versions of MCVS have a patched CMake 3.11 +# that do not work properly with this syntax, so using the following workaround: +if(${CMAKE_VERSION} VERSION_LESS 3.18) + cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) +else() + cmake_policy(VERSION 3.18) endif() -# CMake versions < 3.4.0 do not support try_compile/pthread checks without C as active language. -if(CMAKE_VERSION VERSION_LESS 3.4.0) - project(pybind11) -else() - project(pybind11 CXX) +# Extract project version from source +file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h" + pybind11_version_defines REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ") + +foreach(ver ${pybind11_version_defines}) + if(ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]]) + set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}") + endif() +endforeach() + +if(PYBIND11_VERSION_PATCH MATCHES [[([a-zA-Z]+)]]) + set(PYBIND11_VERSION_TYPE "${CMAKE_MATCH_1}") endif() +string(REGEX MATCH "[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH}") + +project( + pybind11 + LANGUAGES CXX + VERSION "${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}") + +# Standard includes +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) +include(CMakeDependentOption) + +message(STATUS "pybind11 v${pybind11_VERSION} ${PYBIND11_VERSION_TYPE}") # Check if pybind11 is being used directly or via add_subdirectory -set(PYBIND11_MASTER_PROJECT OFF) -if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(PYBIND11_MASTER_PROJECT ON) + message(STATUS "CMake ${CMAKE_VERSION}") + + if(CMAKE_CXX_STANDARD) + set(CMAKE_CXX_EXTENSIONS OFF) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + endif() +else() + set(PYBIND11_MASTER_PROJECT OFF) + set(pybind11_system SYSTEM) endif() option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) -option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) - -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools") +option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) +option(PYBIND11_CLASSIC_LTO "Use the classic LTO flag algorithm, even on CMake 3.9+" OFF) +cmake_dependent_option( + USE_PYTHON_INCLUDE_DIR + "Install pybind11 headers in Python include directory instead of default installation prefix" + OFF "PYBIND11_INSTALL" OFF) +# NB: when adding a header don't forget to also add it to setup.py +set(PYBIND11_HEADERS + include/pybind11/detail/class.h + include/pybind11/detail/common.h + include/pybind11/detail/descr.h + include/pybind11/detail/init.h + include/pybind11/detail/internals.h + include/pybind11/detail/typeid.h + include/pybind11/attr.h + include/pybind11/buffer_info.h + include/pybind11/cast.h + include/pybind11/chrono.h + include/pybind11/common.h + include/pybind11/complex.h + include/pybind11/options.h + include/pybind11/eigen.h + include/pybind11/embed.h + include/pybind11/eval.h + include/pybind11/functional.h + include/pybind11/numpy.h + include/pybind11/operators.h + include/pybind11/pybind11.h + include/pybind11/pytypes.h + include/pybind11/stl.h + include/pybind11/stl_bind.h) + +# TODO: compare with grep and warn if missmatched + +# cmake 3.12 added list(TRANSFORM PREPEND +# But we can't use it yet +string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS + "${PYBIND11_HEADERS}") + +# Classic mode + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools") include(pybind11Tools) # Cache variables so pybind11_add_module can be used in parent projects -set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "") -set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "") -set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "") -set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "") -set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "") -set(PYTHON_VERSION_MAJOR ${PYTHON_VERSION_MAJOR} CACHE INTERNAL "") -set(PYTHON_VERSION_MINOR ${PYTHON_VERSION_MINOR} CACHE INTERNAL "") - -# NB: when adding a header don't forget to also add it to setup.py -set(PYBIND11_HEADERS - include/pybind11/detail/class.h - include/pybind11/detail/common.h - include/pybind11/detail/descr.h - include/pybind11/detail/init.h - include/pybind11/detail/internals.h - include/pybind11/detail/typeid.h - include/pybind11/attr.h - include/pybind11/buffer_info.h - include/pybind11/cast.h - include/pybind11/chrono.h - include/pybind11/common.h - include/pybind11/complex.h - include/pybind11/options.h - include/pybind11/eigen.h - include/pybind11/embed.h - include/pybind11/eval.h - include/pybind11/functional.h - include/pybind11/numpy.h - include/pybind11/operators.h - include/pybind11/pybind11.h - include/pybind11/pytypes.h - include/pybind11/stl.h - include/pybind11/stl_bind.h -) -string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" - PYBIND11_HEADERS "${PYBIND11_HEADERS}") - -if (PYBIND11_TEST) +set(PYBIND11_INCLUDE_DIR + "${CMAKE_CURRENT_LIST_DIR}/include" + CACHE INTERNAL "") +set(PYTHON_INCLUDE_DIRS + ${PYTHON_INCLUDE_DIRS} + CACHE INTERNAL "") +set(PYTHON_LIBRARIES + ${PYTHON_LIBRARIES} + CACHE INTERNAL "") +set(PYTHON_MODULE_PREFIX + ${PYTHON_MODULE_PREFIX} + CACHE INTERNAL "") +set(PYTHON_MODULE_EXTENSION + ${PYTHON_MODULE_EXTENSION} + CACHE INTERNAL "") +set(PYTHON_VERSION_MAJOR + ${PYTHON_VERSION_MAJOR} + CACHE INTERNAL "") +set(PYTHON_VERSION_MINOR + ${PYTHON_VERSION_MINOR} + CACHE INTERNAL "") +set(PYTHON_IS_DEBUG + "${PYTHON_IS_DEBUG}" + CACHE INTERNAL "") + +if(PYBIND11_TEST OR (BUILD_TESTING AND PYBIND11_MASTER_PROJECT)) add_subdirectory(tests) endif() -include(GNUInstallDirs) -include(CMakePackageConfigHelpers) +if(USE_PYTHON_INCLUDE_DIR) + file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS}) +endif() -# extract project version from source -file(STRINGS "${PYBIND11_INCLUDE_DIR}/pybind11/detail/common.h" pybind11_version_defines - REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ") -foreach(ver ${pybind11_version_defines}) - if (ver MATCHES "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$") - set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "") - endif() -endforeach() -set(${PROJECT_NAME}_VERSION ${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}) -message(STATUS "pybind11 v${${PROJECT_NAME}_VERSION}") +# Note: when creating targets, you cannot use if statements at configure time - +# you need generator expressions, because those will be placed in the target file. +# You can also place ifs *in* the Config.in, but not here. + +# Build an interface library target: +add_library(pybind11 INTERFACE) +add_library(pybind11::pybind11 ALIAS pybind11) # to match exported target -option (USE_PYTHON_INCLUDE_DIR "Install pybind11 headers in Python include directory instead of default installation prefix" OFF) -if (USE_PYTHON_INCLUDE_DIR) - file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS}) +target_include_directories( + pybind11 ${pybind11_system} INTERFACE $ + $) +# Only add Python for build - must be added during the import for config since it has to be re-discovered. +target_include_directories(pybind11 SYSTEM INTERFACE $) + +if(CMAKE_VERSION VERSION_LESS 3.13) + target_compile_features(pybind11 INTERFACE cxx_inheriting_constructors cxx_user_literals + cxx_right_angle_brackets) +else() + # This was added in CMake 3.8, but we are keeping a consistent breaking + # point for the config file at 3.13. A config generated by CMake 3.13+ + # can only be read in 3.13+ due to the SHELL usage later, so this is safe to do. + target_compile_features(pybind11 INTERFACE cxx_std_11) endif() -if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0 - # Build an interface library target: - add_library(pybind11 INTERFACE) - add_library(pybind11::pybind11 ALIAS pybind11) # to match exported target - target_include_directories(pybind11 INTERFACE $ - $ - $) - target_compile_options(pybind11 INTERFACE $) - - add_library(module INTERFACE) - add_library(pybind11::module ALIAS module) - if(NOT MSVC) - target_compile_options(module INTERFACE -fvisibility=hidden) - endif() - target_link_libraries(module INTERFACE pybind11::pybind11) - if(WIN32 OR CYGWIN) - target_link_libraries(module INTERFACE $) - elseif(APPLE) - target_link_libraries(module INTERFACE "-undefined dynamic_lookup") - endif() +add_library(module INTERFACE) +add_library(pybind11::module ALIAS module) + +target_link_libraries(module INTERFACE pybind11::pybind11) - add_library(embed INTERFACE) - add_library(pybind11::embed ALIAS embed) - target_link_libraries(embed INTERFACE pybind11::pybind11 $) +# See https://github.com/Kitware/CMake/blob/master/Modules/CMakePlatformId.h.in for platform IDs +# Note: CMake 3.15 allows $ +target_link_libraries( + module + INTERFACE + "$<$,$>:$>") + +if(CMAKE_VERSION VERSION_LESS 3.13) + target_link_libraries(module INTERFACE "$<$:-undefined dynamic_lookup>") +else() + # SHELL (3.12+) forces this to remain together, and link_options was added in 3.13+ + # This is safer, because you are ensured the deduplication pass in CMake will not consider + # these separate and remove one but not the other. + target_link_options(module INTERFACE "$<$:SHELL:-undefined dynamic_lookup>") endif() -if (PYBIND11_INSTALL) +# Workaround for Python 2.7 and C++17 (C++14 as a warning) incompatibility +# This adds the flags -Wno-register and -Wno-deprecated-register if the compiler +# is Clang 3.9+ or AppleClang and the compile language is CXX, or /wd5033 for MSVC (all languages, +# since MSVC didn't recognize COMPILE_LANGUAGE until CMake 3.11+). +set(clang_4plus + "$,$,3.9>>>") +set(no_register "$>") +set(cxx_no_register "$,${no_register}>") +set(msvc "$") +target_compile_options( + pybind11 INTERFACE "$<${cxx_no_register}:-Wno-register;-Wno-deprecated-register>" + "$<${msvc}:/wd5033>") + +add_library(embed INTERFACE) +add_library(pybind11::embed ALIAS embed) +target_link_libraries(embed INTERFACE pybind11::pybind11 $) + +if(PYBIND11_INSTALL) install(DIRECTORY ${PYBIND11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share". - set(PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") - - configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) - # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does - # not depend on architecture specific settings or libraries. - set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) - unset(CMAKE_SIZEOF_VOID_P) - write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake - VERSION ${${PROJECT_NAME}_VERSION} - COMPATIBILITY AnyNewerVersion) - set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P}) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake - ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake - tools/FindPythonLibsNew.cmake - tools/pybind11Tools.cmake - DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) - - if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) - if(NOT PYBIND11_EXPORT_NAME) - set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets") - endif() - - install(TARGETS pybind11 module embed - EXPORT "${PYBIND11_EXPORT_NAME}") - if(PYBIND11_MASTER_PROJECT) - install(EXPORT "${PYBIND11_EXPORT_NAME}" - NAMESPACE "${PROJECT_NAME}::" - DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) - endif() + set(PYBIND11_CMAKECONFIG_INSTALL_DIR + "share/cmake/${PROJECT_NAME}" + CACHE STRING "install path for pybind11Config.cmake") + + configure_package_config_file( + tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) + + if(CMAKE_VERSION VERSION_LESS 3.14) + # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does + # not depend on architecture specific settings or libraries. + set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) + unset(CMAKE_SIZEOF_VOID_P) + + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion) + + set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P}) + else() + # CMake 3.14+ natively supports header-only libraries + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT) endif() + + install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + tools/FindPythonLibsNew.cmake tools/pybind11Tools.cmake + DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) + + if(NOT PYBIND11_EXPORT_NAME) + set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets") + endif() + + install(TARGETS pybind11 module embed EXPORT "${PYBIND11_EXPORT_NAME}") + + install( + EXPORT "${PYBIND11_EXPORT_NAME}" + NAMESPACE "pybind11::" + DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) endif() diff --git a/docs/compiling.rst b/docs/compiling.rst index bfb1cd8056..07f93e7cc5 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -33,7 +33,7 @@ extension module can be created with just a few lines of code: .. code-block:: cmake - cmake_minimum_required(VERSION 2.8.12) + cmake_minimum_required(VERSION 3.7) project(example) add_subdirectory(pybind11) @@ -59,7 +59,7 @@ function with the following signature: .. code-block:: cmake pybind11_add_module( [MODULE | SHARED] [EXCLUDE_FROM_ALL] - [NO_EXTRAS] [SYSTEM] [THIN_LTO] source1 [source2 ...]) + [NO_EXTRAS] [THIN_LTO] source1 [source2 ...]) This function behaves very much like CMake's builtin ``add_library`` (in fact, it's a wrapper function around that command). It will add a library target @@ -86,10 +86,6 @@ latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is given, they will always be disabled, even in ``Release`` mode. However, this will result in code bloat and is generally not recommended. -By default, pybind11 and Python headers will be included with ``-I``. In order -to include pybind11 as system library, e.g. to avoid warnings in downstream -code with warn-levels outside of pybind11's scope, set the option ``SYSTEM``. - As stated above, LTO is enabled by default. Some newer compilers also support different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause the function to prefer this flavor if available. The function falls back to @@ -100,25 +96,22 @@ regular LTO if ``-flto=thin`` is not available. Configuration variables ----------------------- -By default, pybind11 will compile modules with the C++14 standard, if available -on the target compiler, falling back to C++11 if C++14 support is not -available. Note, however, that this default is subject to change: future -pybind11 releases are expected to migrate to newer C++ standards as they become -available. To override this, the standard flag can be given explicitly in -`CMAKE_CXX_STANDARD `_: +By default, pybind11 will compile modules with the compiler default or the +minimum standard required by pybind11, whichever is higher. You can set the +standard explicitly with +`CMAKE_CXX_STANDARD `_: .. code-block:: cmake - # Use just one of these: - set(CMAKE_CXX_STANDARD 11) - set(CMAKE_CXX_STANDARD 14) - set(CMAKE_CXX_STANDARD 17) # Experimental C++17 support + set(CMAKE_CXX_STANDARD 14) # or 11, 14, 17, 20 + set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported + set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensionsn off - add_subdirectory(pybind11) # or find_package(pybind11) -Note that this and all other configuration variables must be set **before** the -call to ``add_subdirectory`` or ``find_package``. The variables can also be set -when calling CMake from the command line using the ``-D=`` flag. +The variables can also be set when calling CMake from the command line using +the ``-D=`` flag. You can also manually set ``CXX_STANDARD`` +on a target or use ``target_compile_features`` on your targets - anything that +CMake supports. The target Python version can be selected by setting ``PYBIND11_PYTHON_VERSION`` or an exact Python installation can be specified with ``PYTHON_EXECUTABLE``. @@ -127,8 +120,12 @@ For example: .. code-block:: bash cmake -DPYBIND11_PYTHON_VERSION=3.6 .. - # or - cmake -DPYTHON_EXECUTABLE=path/to/python .. + + # Another method: + cmake -DPYTHON_EXECUTABLE=/path/to/python .. + + # This often is a good way to get the current Python, works in environments: + cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") .. find_package vs. add_subdirectory --------------------------------- @@ -139,7 +136,7 @@ See the `Config file`_ docstring for details of relevant CMake variables. .. code-block:: cmake - cmake_minimum_required(VERSION 2.8.12) + cmake_minimum_required(VERSION 3.7) project(example) find_package(pybind11 REQUIRED) @@ -151,12 +148,19 @@ the pybind11 repository : .. code-block:: bash + # Classic CMake cd pybind11 mkdir build cd build cmake .. make install + # CMake 3.15+ + cd pybind11 + cmake -S . -B build + cmake --build build -j 2 # Build on 2 cores + cmake --install build + Once detected, the aforementioned ``pybind11_add_module`` can be employed as before. The function usage and configuration variables are identical no matter if pybind11 is added as a subdirectory or found as an installed package. You @@ -171,13 +175,13 @@ Advanced: interface library target When using a version of CMake greater than 3.0, pybind11 can additionally be used as a special *interface library* . The target ``pybind11::module`` is available with pybind11 headers, Python headers and libraries as needed, -and C++ compile definitions attached. This target is suitable for linking +and C++ compile features attached. This target is suitable for linking to an independently constructed (through ``add_library``, not ``pybind11_add_module``) target in the consuming project. .. code-block:: cmake - cmake_minimum_required(VERSION 3.0) + cmake_minimum_required(VERSION 3.7) project(example) find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) @@ -201,6 +205,17 @@ to an independently constructed (through ``add_library``, not Studio (``/bigobj``). The :ref:`FAQ ` contains an explanation on why these are needed. + If you want to add these in yourself, you can use: + + .. code-block:: cmake + + set(CMAKE_CXX_VISIBILITY_PRESET hidden) + set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) # CMake 3.9+ required + + or set the corresponding property (without the ``CMAKE_``) on the targets + manually. + Embedding the Python interpreter -------------------------------- @@ -213,7 +228,7 @@ information about usage in C++, see :doc:`/advanced/embedding`. .. code-block:: cmake - cmake_minimum_required(VERSION 3.0) + cmake_minimum_required(VERSION 3.7) project(example) find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 33805e0f91..8923faef76 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -9,6 +9,10 @@ #pragma once +#define PYBIND11_VERSION_MAJOR 2 +#define PYBIND11_VERSION_MINOR 6 +#define PYBIND11_VERSION_PATCH dev0 + #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } @@ -96,10 +100,6 @@ # define PYBIND11_MAYBE_UNUSED __attribute__ ((__unused__)) #endif -#define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 5 -#define PYBIND11_VERSION_PATCH dev1 - /* Don't let Python.h #define (v)snprintf as macro because they are implemented properly in Visual Studio since 2015. */ #if defined(_MSC_VER) && _MSC_VER >= 1900 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fda6aac8a9..5dea63acd1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,79 +5,96 @@ # All rights reserved. Use of this source code is governed by a # BSD-style license that can be found in the LICENSE file. -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.7) -option(PYBIND11_WERROR "Report all warnings as errors" OFF) -set(PYBIND11_TEST_OVERRIDE "" CACHE STRING "Tests from ;-separated list of *.cpp files will be built instead of all tests") +# VERSION 3.7...3.18, but some versions of VS have a patched CMake 3.11 +# that do not work properly with this syntax, so using the following workaround: +if(${CMAKE_VERSION} VERSION_LESS 3.18) + cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) +else() + cmake_policy(VERSION 3.18) +endif() -if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - # We're being loaded directly, i.e. not via add_subdirectory, so make this - # work as its own project and load the pybind11Config to get the tools we need - project(pybind11_tests CXX) +# There's no harm in including a project in a project +project(pybind11_tests CXX) - find_package(pybind11 REQUIRED CONFIG) +option(PYBIND11_WERROR "Report all warnings as errors" OFF) +option(DOWNLOAD_EIGEN "Download EIGEN (requires CMake 3.11+)" OFF) +set(PYBIND11_TEST_OVERRIDE + "" + CACHE STRING "Tests from ;-separated list of *.cpp files will be built instead of all tests") + +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + # We're being loaded directly, i.e. not via add_subdirectory, so make this + # work as its own project and load the pybind11Config to get the tools we need + find_package(pybind11 REQUIRED CONFIG) endif() if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "Setting tests build type to MinSizeRel as none was specified") - set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE) - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" - "MinSizeRel" "RelWithDebInfo") + set(CMAKE_BUILD_TYPE + MinSizeRel + CACHE STRING "Choose the type of build." FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" + "RelWithDebInfo") endif() # Full set of test files (you can override these; see below) set(PYBIND11_TEST_FILES - test_async.cpp - test_buffers.cpp - test_builtin_casters.cpp - test_call_policies.cpp - test_callbacks.cpp - test_chrono.cpp - test_class.cpp - test_constants_and_functions.cpp - test_copy_move.cpp - test_custom_type_casters.cpp - test_docstring_options.cpp - test_eigen.cpp - test_enum.cpp - test_eval.cpp - test_exceptions.cpp - test_factory_constructors.cpp - test_gil_scoped.cpp - test_iostream.cpp - test_kwargs_and_defaults.cpp - test_local_bindings.cpp - test_methods_and_attributes.cpp - test_modules.cpp - test_multiple_inheritance.cpp - test_numpy_array.cpp - test_numpy_dtypes.cpp - test_numpy_vectorize.cpp - test_opaque_types.cpp - test_operator_overloading.cpp - test_pickling.cpp - test_pytypes.cpp - test_sequences_and_iterators.cpp - test_smart_ptr.cpp - test_stl.cpp - test_stl_binders.cpp - test_tagbased_polymorphic.cpp - test_union.cpp - test_virtual_functions.cpp -) + test_async.cpp + test_buffers.cpp + test_builtin_casters.cpp + test_call_policies.cpp + test_callbacks.cpp + test_chrono.cpp + test_class.cpp + test_constants_and_functions.cpp + test_copy_move.cpp + test_custom_type_casters.cpp + test_docstring_options.cpp + test_eigen.cpp + test_enum.cpp + test_eval.cpp + test_exceptions.cpp + test_factory_constructors.cpp + test_gil_scoped.cpp + test_iostream.cpp + test_kwargs_and_defaults.cpp + test_local_bindings.cpp + test_methods_and_attributes.cpp + test_modules.cpp + test_multiple_inheritance.cpp + test_numpy_array.cpp + test_numpy_dtypes.cpp + test_numpy_vectorize.cpp + test_opaque_types.cpp + test_operator_overloading.cpp + test_pickling.cpp + test_pytypes.cpp + test_sequences_and_iterators.cpp + test_smart_ptr.cpp + test_stl.cpp + test_stl_binders.cpp + test_tagbased_polymorphic.cpp + test_union.cpp + test_virtual_functions.cpp) # Invoking cmake with something like: # cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_picking.cpp" .. # lets you override the tests that get compiled and run. You can restore to all tests with: # cmake -DPYBIND11_TEST_OVERRIDE= .. -if (PYBIND11_TEST_OVERRIDE) +if(PYBIND11_TEST_OVERRIDE) set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE}) endif() # Skip test_async for Python < 3.5 list(FIND PYBIND11_TEST_FILES test_async.cpp PYBIND11_TEST_FILES_ASYNC_I) -if((PYBIND11_TEST_FILES_ASYNC_I GREATER -1) AND ("${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" VERSION_LESS 3.5)) - message(STATUS "Skipping test_async because Python version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} < 3.5") +if((PYBIND11_TEST_FILES_ASYNC_I GREATER -1) AND ("${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" + VERSION_LESS 3.5)) + message( + STATUS + "Skipping test_async because Python version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} < 3.5" + ) list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_ASYNC_I}) endif() @@ -86,18 +103,10 @@ string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}") # Contains the set of test files that require pybind11_cross_module_tests to be # built; if none of these are built (i.e. because TEST_OVERRIDE is used and # doesn't include them) the second module doesn't get built. -set(PYBIND11_CROSS_MODULE_TESTS - test_exceptions.py - test_local_bindings.py - test_stl.py - test_stl_binders.py -) - -set(PYBIND11_CROSS_MODULE_GIL_TESTS - test_gil_scoped.py -) +set(PYBIND11_CROSS_MODULE_TESTS test_exceptions.py test_local_bindings.py test_stl.py + test_stl_binders.py) -option(DOWNLOAD_EIGEN "Download EIGEN (requires CMake 3.11+)" OFF) +set(PYBIND11_CROSS_MODULE_GIL_TESTS test_gil_scoped.py) # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed" @@ -118,8 +127,7 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) FetchContent_Declare( eigen GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git - GIT_TAG ${EIGEN3_VERSION_STRING} - ) + GIT_TAG ${EIGEN3_VERSION_STRING}) FetchContent_GetProperties(eigen) if(NOT eigen_POPULATED) @@ -130,15 +138,14 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) set(EIGEN3_INCLUDE_DIR ${eigen_SOURCE_DIR}) set(EIGEN3_FOUND TRUE) else() - if (NOT CMAKE_VERSION VERSION_LESS 3.0) - find_package(Eigen3 3.2.7 QUIET CONFIG) - if (EIGEN3_FOUND) - if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1) - set(PYBIND11_EIGEN_VIA_TARGET TRUE) - endif() + find_package(Eigen3 3.2.7 QUIET CONFIG) + if(EIGEN3_FOUND) + if(EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1) + set(PYBIND11_EIGEN_VIA_TARGET TRUE) endif() endif() - if (NOT EIGEN3_FOUND) + + if(NOT EIGEN3_FOUND) # Couldn't load via target, so fall back to allowing module mode finding, which will pick up # tools/FindEigen3.cmake find_package(Eigen3 3.2.7 QUIET) @@ -167,7 +174,8 @@ function(pybind11_enable_warnings target_name) if(MSVC) target_compile_options(${target_name} PRIVATE /W4) elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)") - target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated) + target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual + -Wdeprecated) endif() if(PYBIND11_WERROR) @@ -178,7 +186,10 @@ function(pybind11_enable_warnings target_name) endif() endif() - if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) + # Needs to be readded since the ordering requires these to be after the ones above + if(CMAKE_CXX_STANDARD + AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" + AND PYTHON_VERSION VERSION_LESS 3.0) if(CMAKE_CXX_STANDARD LESS 17) target_compile_options(${target_name} PUBLIC -Wno-deprecated-register) else() @@ -192,7 +203,7 @@ set(test_targets pybind11_tests) # Build pybind11_cross_module_tests if any test_whatever.py are being built that require it foreach(t ${PYBIND11_CROSS_MODULE_TESTS}) list(FIND PYBIND11_PYTEST_FILES ${t} i) - if (i GREATER -1) + if(i GREATER -1) list(APPEND test_targets pybind11_cross_module_tests) break() endif() @@ -200,7 +211,7 @@ endforeach() foreach(t ${PYBIND11_CROSS_MODULE_GIL_TESTS}) list(FIND PYBIND11_PYTEST_FILES ${t} i) - if (i GREATER -1) + if(i GREATER -1) list(APPEND test_targets cross_module_gil_utils) break() endif() @@ -209,7 +220,7 @@ endforeach() set(testdir ${CMAKE_CURRENT_SOURCE_DIR}) foreach(target ${test_targets}) set(test_files ${PYBIND11_TEST_FILES}) - if(NOT target STREQUAL "pybind11_tests") + if(NOT "${target}" STREQUAL "pybind11_tests") set(test_files "") endif() @@ -222,7 +233,7 @@ foreach(target ${test_targets}) endif() if(EIGEN3_FOUND) - if (PYBIND11_EIGEN_VIA_TARGET) + if(PYBIND11_EIGEN_VIA_TARGET) target_link_libraries(${target} PRIVATE Eigen3::Eigen) else() target_include_directories(${target} PRIVATE ${EIGEN3_INCLUDE_DIR}) @@ -247,8 +258,11 @@ endforeach() # Make sure pytest is found or produce a fatal error if(NOT PYBIND11_PYTEST_FOUND) - execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)" - RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET) + execute_process( + COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)" + RESULT_VARIABLE pytest_not_found + OUTPUT_VARIABLE pytest_version + ERROR_QUIET) if(pytest_not_found) message(FATAL_ERROR "Running the tests requires pytest. Please install it manually" " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)") @@ -256,22 +270,25 @@ if(NOT PYBIND11_PYTEST_FOUND) message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}" "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)") endif() - set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "") -endif() - -if(CMAKE_VERSION VERSION_LESS 3.2) - set(PYBIND11_USES_TERMINAL "") -else() - set(PYBIND11_USES_TERMINAL "USES_TERMINAL") + set(PYBIND11_PYTEST_FOUND + TRUE + CACHE INTERNAL "") endif() # A single command to compile and run the tests -add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES} - DEPENDS ${test_targets} WORKING_DIRECTORY ${testdir} ${PYBIND11_USES_TERMINAL}) +add_custom_target( + pytest + COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES} + DEPENDS ${test_targets} + WORKING_DIRECTORY ${testdir} + USES_TERMINAL) if(PYBIND11_TEST_OVERRIDE) - add_custom_command(TARGET pytest POST_BUILD - COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect") + add_custom_command( + TARGET pytest + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E echo + "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect") endif() # Add a check target to run all the tests, starting with pytest (we add dependencies to this below) @@ -279,14 +296,18 @@ add_custom_target(check DEPENDS pytest) # The remaining tests only apply when being built as part of the pybind11 project, but not if the # tests are being built independently. -if (NOT PROJECT_NAME STREQUAL "pybind11") +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) return() endif() # Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it: -add_custom_command(TARGET pybind11_tests POST_BUILD - COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py - $ ${CMAKE_CURRENT_BINARY_DIR}/sosize-$.txt) +add_custom_command( + TARGET pybind11_tests + POST_BUILD + COMMAND + ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../tools/libsize.py + $ + ${CMAKE_CURRENT_BINARY_DIR}/sosize-$.txt) # Test embedding the interpreter. Provides the `cpptest` target. add_subdirectory(test_embed) diff --git a/tests/test_cmake_build/CMakeLists.txt b/tests/test_cmake_build/CMakeLists.txt index cf9a9ca9ff..53228f0eb4 100644 --- a/tests/test_cmake_build/CMakeLists.txt +++ b/tests/test_cmake_build/CMakeLists.txt @@ -1,36 +1,40 @@ add_custom_target(test_cmake_build) -if(CMAKE_VERSION VERSION_LESS 3.1) - # 3.0 needed for interface library for subdirectory_target/installed_target - # 3.1 needed for cmake -E env for testing - return() -endif() - -include(CMakeParseArguments) function(pybind11_add_build_test name) cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN}) - set(build_options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/mock_install" - "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" - "-DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}" - "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" - "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}") + set(build_options + "-DCMAKE_PREFIX_PATH=${pybind11_BINARY_DIR}/mock_install" + "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" + "-DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}") + + if(CMAKE_CXX_STANDARD) + list(APPEND build_options "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}") + endif() + if(NOT ARG_INSTALL) - list(APPEND build_options "-DPYBIND11_PROJECT_DIR=${PROJECT_SOURCE_DIR}") + list(APPEND build_options "-DPYBIND11_PROJECT_DIR=${pybind11_SOURCE_DIR}") endif() - add_custom_target(test_${name} ${CMAKE_CTEST_COMMAND} - --quiet --output-log ${name}.log - --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/${name}" - "${CMAKE_CURRENT_BINARY_DIR}/${name}" - --build-config Release + add_custom_target( + test_${name} + ${CMAKE_CTEST_COMMAND} + --build-and-test + "${CMAKE_CURRENT_SOURCE_DIR}/${name}" + "${CMAKE_CURRENT_BINARY_DIR}/${name}" + --build-config + Release --build-noclean - --build-generator ${CMAKE_GENERATOR} - $<$:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM} - --build-makeprogram ${CMAKE_MAKE_PROGRAM} - --build-target check - --build-options ${build_options} - ) + --build-generator + ${CMAKE_GENERATOR} + $<$:--build-generator-platform> + ${CMAKE_GENERATOR_PLATFORM} + --build-makeprogram + ${CMAKE_MAKE_PROGRAM} + --build-target + check + --build-options + ${build_options}) if(ARG_INSTALL) add_dependencies(test_${name} mock_install) endif() @@ -44,10 +48,9 @@ if(NOT ${PYTHON_MODULE_EXTENSION} MATCHES "pypy") endif() if(PYBIND11_INSTALL) - add_custom_target(mock_install ${CMAKE_COMMAND} - "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/mock_install" - -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" - ) + add_custom_target( + mock_install ${CMAKE_COMMAND} "-DCMAKE_INSTALL_PREFIX=${pybind11_BINARY_DIR}/mock_install" -P + "${pybind11_BINARY_DIR}/cmake_install.cmake") pybind11_add_build_test(installed_function INSTALL) pybind11_add_build_test(installed_target INSTALL) diff --git a/tests/test_cmake_build/installed_embed/CMakeLists.txt b/tests/test_cmake_build/installed_embed/CMakeLists.txt index 6ab3efd470..78855afa26 100644 --- a/tests/test_cmake_build/installed_embed/CMakeLists.txt +++ b/tests/test_cmake_build/installed_embed/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.7) project(test_installed_embed CXX) set(CMAKE_MODULE_PATH "") @@ -8,17 +8,8 @@ message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}") add_executable(test_cmake_build ../embed.cpp) target_link_libraries(test_cmake_build PRIVATE pybind11::embed) - # Do not treat includes from IMPORTED target as SYSTEM (Python headers in pybind11::embed). # This may be needed to resolve header conflicts, e.g. between Python release and debug headers. set_target_properties(test_cmake_build PROPERTIES NO_SYSTEM_FROM_IMPORTED ON) add_custom_target(check $ ${PROJECT_SOURCE_DIR}/../test.py) - -if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) - if(CMAKE_CXX_STANDARD LESS 17) - target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) - else() - target_compile_options(test_cmake_build PUBLIC -Wno-register) - endif() -endif() diff --git a/tests/test_cmake_build/installed_function/CMakeLists.txt b/tests/test_cmake_build/installed_function/CMakeLists.txt index e0c20a8a36..3ad5445e3f 100644 --- a/tests/test_cmake_build/installed_function/CMakeLists.txt +++ b/tests/test_cmake_build/installed_function/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.7) project(test_installed_module CXX) set(CMAKE_MODULE_PATH "") @@ -8,5 +8,12 @@ message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}") pybind11_add_module(test_cmake_build SHARED NO_EXTRAS ../main.cpp) -add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ - ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py ${PROJECT_NAME}) +add_custom_target( + check + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${PYTHON_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME}) diff --git a/tests/test_cmake_build/installed_target/CMakeLists.txt b/tests/test_cmake_build/installed_target/CMakeLists.txt index 38520fa9c0..348c419cd3 100644 --- a/tests/test_cmake_build/installed_target/CMakeLists.txt +++ b/tests/test_cmake_build/installed_target/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.7) project(test_installed_target CXX) set(CMAKE_MODULE_PATH "") @@ -18,13 +18,12 @@ set_target_properties(test_cmake_build PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX # This may be needed to resolve header conflicts, e.g. between Python release and debug headers. set_target_properties(test_cmake_build PROPERTIES NO_SYSTEM_FROM_IMPORTED ON) -add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ - ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py ${PROJECT_NAME}) - -if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) - if(CMAKE_CXX_STANDARD LESS 17) - target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) - else() - target_compile_options(test_cmake_build PUBLIC -Wno-register) - endif() -endif() +add_custom_target( + check + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${PYTHON_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME}) diff --git a/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt b/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt index 003e93c22e..eea0eeea3d 100644 --- a/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt @@ -1,7 +1,9 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.7) project(test_subdirectory_embed CXX) -set(PYBIND11_INSTALL ON CACHE BOOL "") +set(PYBIND11_INSTALL + ON + CACHE BOOL "") set(PYBIND11_EXPORT_NAME test_export) add_subdirectory(${PYBIND11_PROJECT_DIR} pybind11) @@ -16,20 +18,10 @@ add_custom_target(check $ ${PROJECT_SOURCE_DIR}/.. add_library(test_embed_lib ../embed.cpp) target_link_libraries(test_embed_lib PRIVATE pybind11::embed) -install(TARGETS test_embed_lib - EXPORT test_export - ARCHIVE DESTINATION bin - LIBRARY DESTINATION lib - RUNTIME DESTINATION lib) -install(EXPORT test_export - DESTINATION lib/cmake/test_export/test_export-Targets.cmake) - -if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) - if(CMAKE_CXX_STANDARD LESS 17) - target_compile_options(test_embed_lib PUBLIC -Wno-deprecated-register) - target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) - else() - target_compile_options(test_embed_lib PUBLIC -Wno-register) - target_compile_options(test_cmake_build PUBLIC -Wno-register) - endif() -endif() +install( + TARGETS test_embed_lib + EXPORT test_export + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION lib) +install(EXPORT test_export DESTINATION lib/cmake/test_export/test_export-Targets.cmake) diff --git a/tests/test_cmake_build/subdirectory_function/CMakeLists.txt b/tests/test_cmake_build/subdirectory_function/CMakeLists.txt index 278007aebd..e4518044ed 100644 --- a/tests/test_cmake_build/subdirectory_function/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_function/CMakeLists.txt @@ -1,8 +1,15 @@ -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.7) project(test_subdirectory_module CXX) add_subdirectory(${PYBIND11_PROJECT_DIR} pybind11) pybind11_add_module(test_cmake_build THIN_LTO ../main.cpp) -add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ - ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py ${PROJECT_NAME}) +add_custom_target( + check + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${PYTHON_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME}) diff --git a/tests/test_cmake_build/subdirectory_target/CMakeLists.txt b/tests/test_cmake_build/subdirectory_target/CMakeLists.txt index 507cb8b99a..f84140ce04 100644 --- a/tests/test_cmake_build/subdirectory_target/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_target/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.7) project(test_subdirectory_target CXX) add_subdirectory(${PYBIND11_PROJECT_DIR} pybind11) @@ -11,13 +11,12 @@ target_link_libraries(test_cmake_build PRIVATE pybind11::module) set_target_properties(test_cmake_build PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}" SUFFIX "${PYTHON_MODULE_EXTENSION}") -add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ - ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py ${PROJECT_NAME}) - -if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) - if(CMAKE_CXX_STANDARD LESS 17) - target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) - else() - target_compile_options(test_cmake_build PUBLIC -Wno-register) - endif() -endif() +add_custom_target( + check + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${PYTHON_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME}) diff --git a/tests/test_embed/CMakeLists.txt b/tests/test_embed/CMakeLists.txt index 1e0ebde745..25972701fc 100644 --- a/tests/test_embed/CMakeLists.txt +++ b/tests/test_embed/CMakeLists.txt @@ -1,5 +1,5 @@ if(${PYTHON_MODULE_EXTENSION} MATCHES "pypy") - add_custom_target(cpptest) # Dummy target on PyPy. Embedding is not supported. + add_custom_target(cpptest) # Dummy target on PyPy. Embedding is not supported. set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") return() endif() @@ -13,43 +13,28 @@ else() return() endif() -add_executable(test_embed - catch.cpp - test_interpreter.cpp -) +add_executable(test_embed catch.cpp test_interpreter.cpp) target_include_directories(test_embed PRIVATE "${CATCH_INCLUDE_DIR}") pybind11_enable_warnings(test_embed) -if(NOT CMAKE_VERSION VERSION_LESS 3.0) - target_link_libraries(test_embed PRIVATE pybind11::embed) -else() - target_include_directories(test_embed PRIVATE "${PYBIND11_INCLUDE_DIR}" "${PYTHON_INCLUDE_DIRS}") - target_compile_options(test_embed PRIVATE "${PYBIND11_CPP_STANDARD}") - target_link_libraries(test_embed PRIVATE "${PYTHON_LIBRARIES}") -endif() +target_link_libraries(test_embed PRIVATE pybind11::embed) find_package(Threads REQUIRED) -target_link_libraries(test_embed PUBLIC ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(test_embed PUBLIC Threads::Threads) -add_custom_target(cpptest COMMAND $ - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_custom_target( + cpptest + COMMAND "$" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") pybind11_add_module(external_module THIN_LTO external_module.cpp) -set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY + "${CMAKE_CURRENT_SOURCE_DIR}") foreach(config ${CMAKE_CONFIGURATION_TYPES}) string(TOUPPER ${config} config) - set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} "${CMAKE_CURRENT_SOURCE_DIR}") + set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} + "${CMAKE_CURRENT_SOURCE_DIR}") endforeach() add_dependencies(cpptest external_module) add_dependencies(check cpptest) - -if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) - if(CMAKE_CXX_STANDARD LESS 17) - target_compile_options(test_embed PUBLIC -Wno-deprecated-register) - target_compile_options(external_module PUBLIC -Wno-deprecated-register) - else() - target_compile_options(test_embed PUBLIC -Wno-register) - target_compile_options(external_module PUBLIC -Wno-register) - endif() -endif() diff --git a/tools/FindCatch.cmake b/tools/FindCatch.cmake index ad4a4a5938..ade66c79e6 100644 --- a/tools/FindCatch.cmake +++ b/tools/FindCatch.cmake @@ -19,9 +19,14 @@ endif() # Extract the version number from catch.hpp function(_get_catch_version) - file(STRINGS "${CATCH_INCLUDE_DIR}/catch.hpp" version_line REGEX "Catch v.*" LIMIT_COUNT 1) + file( + STRINGS "${CATCH_INCLUDE_DIR}/catch.hpp" version_line + REGEX "Catch v.*" + LIMIT_COUNT 1) if(version_line MATCHES "Catch v([0-9]+)\\.([0-9]+)\\.([0-9]+)") - set(CATCH_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE) + set(CATCH_VERSION + "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" + PARENT_SCOPE) endif() endfunction() @@ -34,11 +39,16 @@ function(_download_catch version destination_dir) if(error) message(FATAL_ERROR "Could not download ${url}") endif() - set(CATCH_INCLUDE_DIR "${destination_dir}" CACHE INTERNAL "") + set(CATCH_INCLUDE_DIR + "${destination_dir}" + CACHE INTERNAL "") endfunction() # Look for catch locally -find_path(CATCH_INCLUDE_DIR NAMES catch.hpp PATH_SUFFIXES catch2) +find_path( + CATCH_INCLUDE_DIR + NAMES catch.hpp + PATH_SUFFIXES catch2) if(CATCH_INCLUDE_DIR) _get_catch_version() endif() diff --git a/tools/FindEigen3.cmake b/tools/FindEigen3.cmake index 66ffe8e1e5..98ab43d9e6 100644 --- a/tools/FindEigen3.cmake +++ b/tools/FindEigen3.cmake @@ -26,17 +26,21 @@ if(NOT Eigen3_FIND_VERSION) set(Eigen3_FIND_VERSION_PATCH 0) endif(NOT Eigen3_FIND_VERSION_PATCH) - set(Eigen3_FIND_VERSION "${Eigen3_FIND_VERSION_MAJOR}.${Eigen3_FIND_VERSION_MINOR}.${Eigen3_FIND_VERSION_PATCH}") + set(Eigen3_FIND_VERSION + "${Eigen3_FIND_VERSION_MAJOR}.${Eigen3_FIND_VERSION_MINOR}.${Eigen3_FIND_VERSION_PATCH}") endif(NOT Eigen3_FIND_VERSION) macro(_eigen3_check_version) file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen3_version_header) - string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen3_world_version_match "${_eigen3_version_header}") + string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen3_world_version_match + "${_eigen3_version_header}") set(EIGEN3_WORLD_VERSION "${CMAKE_MATCH_1}") - string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen3_major_version_match "${_eigen3_version_header}") + string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen3_major_version_match + "${_eigen3_version_header}") set(EIGEN3_MAJOR_VERSION "${CMAKE_MATCH_1}") - string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen3_minor_version_match "${_eigen3_version_header}") + string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen3_minor_version_match + "${_eigen3_version_header}") set(EIGEN3_MINOR_VERSION "${CMAKE_MATCH_1}") set(EIGEN3_VERSION ${EIGEN3_WORLD_VERSION}.${EIGEN3_MAJOR_VERSION}.${EIGEN3_MINOR_VERSION}) @@ -53,20 +57,19 @@ macro(_eigen3_check_version) endif(NOT EIGEN3_VERSION_OK) endmacro(_eigen3_check_version) -if (EIGEN3_INCLUDE_DIR) +if(EIGEN3_INCLUDE_DIR) # in cache already _eigen3_check_version() set(EIGEN3_FOUND ${EIGEN3_VERSION_OK}) -else (EIGEN3_INCLUDE_DIR) +else(EIGEN3_INCLUDE_DIR) - find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library - PATHS - ${CMAKE_INSTALL_PREFIX}/include - ${KDE4_INCLUDE_DIR} - PATH_SUFFIXES eigen3 eigen - ) + find_path( + EIGEN3_INCLUDE_DIR + NAMES signature_of_eigen3_matrix_library + PATHS ${CMAKE_INSTALL_PREFIX}/include ${KDE4_INCLUDE_DIR} + PATH_SUFFIXES eigen3 eigen) if(EIGEN3_INCLUDE_DIR) _eigen3_check_version() diff --git a/tools/FindPythonLibsNew.cmake b/tools/FindPythonLibsNew.cmake index 994bb1e03b..cf2a13f67b 100644 --- a/tools/FindPythonLibsNew.cmake +++ b/tools/FindPythonLibsNew.cmake @@ -52,23 +52,23 @@ # Checking for the extension makes sure that `LibsNew` was found and not just `Libs`. if(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION) - return() + return() endif() # Use the Python interpreter to find the libs. if(NOT PythonLibsNew_FIND_VERSION) - set(PythonLibsNew_FIND_VERSION "") + set(PythonLibsNew_FIND_VERSION "") endif() if(PythonLibsNew_FIND_REQUIRED) - find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED) + find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED) else() - find_package(PythonInterp ${PythonLibsNew_FIND_VERSION}) + find_package(PythonInterp ${PythonLibsNew_FIND_VERSION}) endif() if(NOT PYTHONINTERP_FOUND) - set(PYTHONLIBS_FOUND FALSE) - set(PythonLibsNew_FOUND FALSE) - return() + set(PYTHONLIBS_FOUND FALSE) + set(PythonLibsNew_FOUND FALSE) + return() endif() # According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter @@ -77,8 +77,9 @@ endif() # # The library suffix is from the config var LDVERSION sometimes, otherwise # VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows. -execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" - "from distutils import sysconfig as s;import sys;import struct; +execute_process( + COMMAND + "${PYTHON_EXECUTABLE}" "-c" "from distutils import sysconfig as s;import sys;import struct; print('.'.join(str(v) for v in sys.version_info)); print(sys.prefix); print(s.get_python_inc(plat_specific=True)); @@ -90,23 +91,22 @@ print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION')); print(s.get_config_var('LIBDIR') or ''); print(s.get_config_var('MULTIARCH') or ''); " - RESULT_VARIABLE _PYTHON_SUCCESS - OUTPUT_VARIABLE _PYTHON_VALUES - ERROR_VARIABLE _PYTHON_ERROR_VALUE) + RESULT_VARIABLE _PYTHON_SUCCESS + OUTPUT_VARIABLE _PYTHON_VALUES + ERROR_VARIABLE _PYTHON_ERROR_VALUE) if(NOT _PYTHON_SUCCESS MATCHES 0) - if(PythonLibsNew_FIND_REQUIRED) - message(FATAL_ERROR - "Python config failure:\n${_PYTHON_ERROR_VALUE}") - endif() - set(PYTHONLIBS_FOUND FALSE) - set(PythonLibsNew_FOUND FALSE) - return() + if(PythonLibsNew_FIND_REQUIRED) + message(FATAL_ERROR "Python config failure:\n${_PYTHON_ERROR_VALUE}") + endif() + set(PYTHONLIBS_FOUND FALSE) + set(PythonLibsNew_FOUND FALSE) + return() endif() # Convert the process output into a list if(WIN32) - string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES}) + string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES}) endif() string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES}) string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES}) @@ -124,16 +124,15 @@ list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH) # Make sure the Python has the same pointer-size as the chosen compiler # Skip if CMAKE_SIZEOF_VOID_P is not defined if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}")) - if(PythonLibsNew_FIND_REQUIRED) - math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8") - math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8") - message(FATAL_ERROR - "Python config failure: Python is ${_PYTHON_BITS}-bit, " - "chosen compiler is ${_CMAKE_BITS}-bit") - endif() - set(PYTHONLIBS_FOUND FALSE) - set(PythonLibsNew_FOUND FALSE) - return() + if(PythonLibsNew_FIND_REQUIRED) + math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8") + math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8") + message(FATAL_ERROR "Python config failure: Python is ${_PYTHON_BITS}-bit, " + "chosen compiler is ${_CMAKE_BITS}-bit") + endif() + set(PYTHONLIBS_FOUND FALSE) + set(PythonLibsNew_FOUND FALSE) + return() endif() # The built-in FindPython didn't always give the version numbers @@ -148,79 +147,76 @@ string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}") string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES "${PYTHON_SITE_PACKAGES}") if(CMAKE_HOST_WIN32) - set(PYTHON_LIBRARY - "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib") - - # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the - # original python installation. They may be found relative to PYTHON_INCLUDE_DIR. - if(NOT EXISTS "${PYTHON_LIBRARY}") - get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY) - set(PYTHON_LIBRARY - "${_PYTHON_ROOT}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib") - endif() - - # if we are in MSYS & MINGW, and we didn't find windows python lib, look for system python lib - if(DEFINED ENV{MSYSTEM} AND MINGW AND NOT EXISTS "${PYTHON_LIBRARY}") - if(PYTHON_MULTIARCH) - set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}") - else() - set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}") - endif() - unset(PYTHON_LIBRARY) - find_library(PYTHON_LIBRARY - NAMES "python${PYTHON_LIBRARY_SUFFIX}" - PATHS ${_PYTHON_LIBS_SEARCH} - NO_DEFAULT_PATH) - endif() - - # raise an error if the python libs are still not found. - if(NOT EXISTS "${PYTHON_LIBRARY}") - message(FATAL_ERROR "Python libraries not found") - endif() - -else() + set(PYTHON_LIBRARY "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib") + + # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the + # original python installation. They may be found relative to PYTHON_INCLUDE_DIR. + if(NOT EXISTS "${PYTHON_LIBRARY}") + get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY) + set(PYTHON_LIBRARY "${_PYTHON_ROOT}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib") + endif() + + # if we are in MSYS & MINGW, and we didn't find windows python lib, look for system python lib + if(DEFINED ENV{MSYSTEM} + AND MINGW + AND NOT EXISTS "${PYTHON_LIBRARY}") if(PYTHON_MULTIARCH) - set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}") + set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}") else() - set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}") - endif() - #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}") - # Probably this needs to be more involved. It would be nice if the config - # information the python interpreter itself gave us were more complete. - find_library(PYTHON_LIBRARY - NAMES "python${PYTHON_LIBRARY_SUFFIX}" - PATHS ${_PYTHON_LIBS_SEARCH} - NO_DEFAULT_PATH) - - # If all else fails, just set the name/version and let the linker figure out the path. - if(NOT PYTHON_LIBRARY) - set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX}) + set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}") endif() + unset(PYTHON_LIBRARY) + find_library( + PYTHON_LIBRARY + NAMES "python${PYTHON_LIBRARY_SUFFIX}" + PATHS ${_PYTHON_LIBS_SEARCH} + NO_DEFAULT_PATH) + endif() + + # raise an error if the python libs are still not found. + if(NOT EXISTS "${PYTHON_LIBRARY}") + message(FATAL_ERROR "Python libraries not found") + endif() + +else() + if(PYTHON_MULTIARCH) + set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}") + else() + set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}") + endif() + #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}") + # Probably this needs to be more involved. It would be nice if the config + # information the python interpreter itself gave us were more complete. + find_library( + PYTHON_LIBRARY + NAMES "python${PYTHON_LIBRARY_SUFFIX}" + PATHS ${_PYTHON_LIBS_SEARCH} NO_DEFAULT_PATH) + + # If all else fails, just set the name/version and let the linker figure out the path. + if(NOT PYTHON_LIBRARY) + set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX}) + endif() endif() -MARK_AS_ADVANCED( - PYTHON_LIBRARY - PYTHON_INCLUDE_DIR -) +mark_as_advanced(PYTHON_LIBRARY PYTHON_INCLUDE_DIR) # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the # cache entries because they are meant to specify the location of a single # library. We now set the variables listed by the documentation for this # module. -SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}") -SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}") +set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}") +set(PYTHON_LIBRARIES "${PYTHON_LIBRARY}") if(NOT PYTHON_DEBUG_LIBRARY) - SET(PYTHON_DEBUG_LIBRARY "") + set(PYTHON_DEBUG_LIBRARY "") endif() -SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}") +set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}") -find_package_message(PYTHON - "Found PythonLibs: ${PYTHON_LIBRARY}" - "${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}") +find_package_message(PYTHON "Found PythonLibs: ${PYTHON_LIBRARY}" + "${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}") set(PYTHONLIBS_FOUND TRUE) set(PythonLibsNew_FOUND TRUE) if(NOT PYTHON_MODULE_PREFIX) - SET(PYTHON_MODULE_PREFIX "") + set(PYTHON_MODULE_PREFIX "") endif() diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 58426887a5..bcc74d6bfb 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -60,45 +60,37 @@ @PACKAGE_INIT@ -set(PN pybind11) +# Location of pybind11/pybind11.h +set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@") -# location of pybind11/pybind11.h -set(${PN}_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@") +set(pybind11_LIBRARY "") +set(pybind11_DEFINITIONS USING_pybind11) -set(${PN}_LIBRARY "") -set(${PN}_DEFINITIONS USING_${PN}) +check_required_components(pybind11) -check_required_components(${PN}) - -# make detectable the FindPythonLibsNew.cmake module +# Make the FindPythonLibsNew.cmake module available list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}) include(pybind11Tools) -if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) #----------------------------------------------------------------------------- # Don't include targets if this file is being picked up by another # project which has already built this as a subproject #----------------------------------------------------------------------------- -if(NOT TARGET ${PN}::pybind11) - include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake") +if(NOT TARGET pybind11::pybind11) + include("${CMAKE_CURRENT_LIST_DIR}/pybind11Targets.cmake") find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED) - set_property(TARGET ${PN}::pybind11 APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${PYTHON_INCLUDE_DIRS}) - set_property(TARGET ${PN}::embed APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${PYTHON_LIBRARIES}) - if(WIN32 OR CYGWIN) - set_property(TARGET ${PN}::module APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${PYTHON_LIBRARIES}) - endif() - if(CMAKE_VERSION VERSION_LESS 3.3) - set_property(TARGET ${PN}::pybind11 APPEND PROPERTY INTERFACE_COMPILE_OPTIONS "${PYBIND11_CPP_STANDARD}") - else() - set_property(TARGET ${PN}::pybind11 APPEND PROPERTY INTERFACE_COMPILE_OPTIONS $<$:${PYBIND11_CPP_STANDARD}>) - endif() + set_property(TARGET pybind11::pybind11 APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${PYTHON_INCLUDE_DIRS}) + set_property(TARGET pybind11::pybind11 APPEND PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${PYTHON_INCLUDE_DIRS}) - get_property(_iid TARGET ${PN}::pybind11 PROPERTY INTERFACE_INCLUDE_DIRECTORIES) - get_property(_ill TARGET ${PN}::module PROPERTY INTERFACE_LINK_LIBRARIES) - set(${PN}_INCLUDE_DIRS ${_iid}) - set(${PN}_LIBRARIES ${_ico} ${_ill}) -endif() + set_property(TARGET pybind11::embed APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${PYTHON_LIBRARIES}) + set_property(TARGET pybind11::module APPEND PROPERTY INTERFACE_LINK_LIBRARIES + "$<$,$>:$>") + + get_property(_iid TARGET pybind11::pybind11 PROPERTY INTERFACE_INCLUDE_DIRECTORIES) + get_property(_ill TARGET pybind11::module PROPERTY INTERFACE_LINK_LIBRARIES) + set(pybind11_INCLUDE_DIRS ${_iid}) + set(pybind11_LIBRARIES ${_ico} ${_ill}) endif() diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake index be536e6b6f..49876eb9f6 100644 --- a/tools/pybind11Tools.cmake +++ b/tools/pybind11Tools.cmake @@ -5,110 +5,108 @@ # All rights reserved. Use of this source code is governed by a # BSD-style license that can be found in the LICENSE file. -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.7) + +# VERSION 3.7...3.18, but some versions of VS have a patched CMake 3.11 +# that do not work properly with this syntax, so using the following workaround: +if(${CMAKE_VERSION} VERSION_LESS 3.18) + cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) +else() + cmake_policy(VERSION 3.18) +endif() # Add a CMake parameter for choosing a desired Python version if(NOT PYBIND11_PYTHON_VERSION) - set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules") + set(PYBIND11_PYTHON_VERSION + "" + CACHE STRING "Python version to use for compiling modules") endif() -set(Python_ADDITIONAL_VERSIONS 3.9 3.8 3.7 3.6 3.5 3.4) +# A user can set versions manually too +set(Python_ADDITIONAL_VERSIONS + "3.9;3.8;3.7;3.6;3.5;3.4" + CACHE INTERNAL "") find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED) include(CheckCXXCompilerFlag) include(CMakeParseArguments) -# Use the language standards abstraction if CMake supports it with the current compiler -if(NOT CMAKE_VERSION VERSION_LESS 3.1) +# Warn or error if old variable name used +if(PYBIND11_CPP_STANDARD) if(NOT CMAKE_CXX_STANDARD) - if(CMAKE_CXX14_STANDARD_COMPILE_OPTION) - set(CMAKE_CXX_STANDARD 14) - elseif(CMAKE_CXX11_STANDARD_COMPILE_OPTION) - set(CMAKE_CXX_STANDARD 11) - endif() - endif() - if(CMAKE_CXX_STANDARD) - set(CMAKE_CXX_EXTENSIONS OFF) - set(CMAKE_CXX_STANDARD_REQUIRED ON) - endif() -endif() - -# Fall back to heuristics -if(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD) - if(MSVC) - set(PYBIND11_CPP_STANDARD /std:c++14) - else() - check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG) - if(HAS_CPP14_FLAG) - set(PYBIND11_CPP_STANDARD -std=c++14) + string(REGEX MATCH [=[..^]=] VAL "${PYBIND11_CPP_STANDARD}") + set(supported_standards 11 14 17 20) + if("${VAL}" IN_LIST supported_standards) + message(WARNING "USE -DCMAKE_CXX_STANDARD=${VAL} instead of PYBIND11_PYTHON_VERSION") + set(CMAKE_CXX_STANDARD ${VAL}) else() - check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG) - if(HAS_CPP11_FLAG) - set(PYBIND11_CPP_STANDARD -std=c++11) - endif() + message(FATAL_ERROR "PYBIND11_CPP_STANDARD should be replaced with CMAKE_CXX_STANDARD") endif() endif() - - if(NOT PYBIND11_CPP_STANDARD) - message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!") - endif() - set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING - "C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE) endif() # Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and # linkerflags are lists of flags to use. The result variable is a unique variable name for each set # of flags: the compilation result will be cached base on the result variable. If the flags work, # sets them in cxxflags_out/linkerflags_out internal cache variables (in addition to ${result}). -function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerflags cxxflags_out linkerflags_out) +function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerflags cxxflags_out + linkerflags_out) set(CMAKE_REQUIRED_LIBRARIES ${linkerflags}) check_cxx_compiler_flag("${cxxflags}" ${result}) - if (${result}) - set(${cxxflags_out} "${cxxflags}" CACHE INTERNAL "" FORCE) - set(${linkerflags_out} "${linkerflags}" CACHE INTERNAL "" FORCE) + if(${result}) + set(${cxxflags_out} + "${cxxflags}" + CACHE INTERNAL "" FORCE) + set(${linkerflags_out} + "${linkerflags}" + CACHE INTERNAL "" FORCE) endif() endfunction() # Internal: find the appropriate link time optimization flags for this compiler function(_pybind11_add_lto_flags target_name prefer_thin_lto) - if (NOT DEFINED PYBIND11_LTO_CXX_FLAGS) - set(PYBIND11_LTO_CXX_FLAGS "" CACHE INTERNAL "") - set(PYBIND11_LTO_LINKER_FLAGS "" CACHE INTERNAL "") + if(NOT DEFINED PYBIND11_LTO_CXX_FLAGS) + set(PYBIND11_LTO_CXX_FLAGS + "" + CACHE INTERNAL "") + set(PYBIND11_LTO_LINKER_FLAGS + "" + CACHE INTERNAL "") if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") set(cxx_append "") set(linker_append "") - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it set(linker_append ";$<$:-O3>") elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") set(cxx_append ";-fno-fat-lto-objects") endif() - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND prefer_thin_lto) - _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO_THIN - "-flto=thin${cxx_append}" "-flto=thin${linker_append}" + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND prefer_thin_lto) + _pybind11_return_if_cxx_and_linker_flags_work( + HAS_FLTO_THIN "-flto=thin${cxx_append}" "-flto=thin${linker_append}" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) endif() - if (NOT HAS_FLTO_THIN) - _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO - "-flto${cxx_append}" "-flto${linker_append}" - PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) + if(NOT HAS_FLTO_THIN) + _pybind11_return_if_cxx_and_linker_flags_work( + HAS_FLTO "-flto${cxx_append}" "-flto${linker_append}" PYBIND11_LTO_CXX_FLAGS + PYBIND11_LTO_LINKER_FLAGS) endif() - elseif (CMAKE_CXX_COMPILER_ID MATCHES "Intel") + elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel") # Intel equivalent to LTO is called IPO - _pybind11_return_if_cxx_and_linker_flags_work(HAS_INTEL_IPO - "-ipo" "-ipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) + _pybind11_return_if_cxx_and_linker_flags_work( + HAS_INTEL_IPO "-ipo" "-ipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) elseif(MSVC) # cmake only interprets libraries as linker flags when they start with a - (otherwise it # converts /LTCG to \LTCG as if it was a Windows path). Luckily MSVC supports passing flags # with - instead of /, even if it is a bit non-standard: - _pybind11_return_if_cxx_and_linker_flags_work(HAS_MSVC_GL_LTCG - "/GL" "-LTCG" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) + _pybind11_return_if_cxx_and_linker_flags_work( + HAS_MSVC_GL_LTCG "/GL" "-LTCG" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) endif() - if (PYBIND11_LTO_CXX_FLAGS) + if(PYBIND11_LTO_CXX_FLAGS) message(STATUS "LTO enabled") else() message(STATUS "LTO disabled (not supported by the compiler and/or linker)") @@ -116,17 +114,20 @@ function(_pybind11_add_lto_flags target_name prefer_thin_lto) endif() # Enable LTO flags if found, except for Debug builds - if (PYBIND11_LTO_CXX_FLAGS) - target_compile_options(${target_name} PRIVATE "$<$>:${PYBIND11_LTO_CXX_FLAGS}>") + if(PYBIND11_LTO_CXX_FLAGS) + set(not_debug "$>") + set(cxx_lang "$") + target_compile_options(${target_name} + PRIVATE "$<$:${PYBIND11_LTO_CXX_FLAGS}>") endif() - if (PYBIND11_LTO_LINKER_FLAGS) - target_link_libraries(${target_name} PRIVATE "$<$>:${PYBIND11_LTO_LINKER_FLAGS}>") + if(PYBIND11_LTO_LINKER_FLAGS) + target_link_libraries(${target_name} PRIVATE "$<${not_debug}:${PYBIND11_LTO_LINKER_FLAGS}>") endif() endfunction() # Build a Python extension module: # pybind11_add_module( [MODULE | SHARED] [EXCLUDE_FROM_ALL] -# [NO_EXTRAS] [SYSTEM] [THIN_LTO] source1 [source2 ...]) +# [NO_EXTRAS] [THIN_LTO] source1 [source2 ...]) # function(pybind11_add_module target_name) set(options MODULE SHARED EXCLUDE_FROM_ALL NO_EXTRAS SYSTEM THIN_LTO) @@ -148,29 +149,16 @@ function(pybind11_add_module target_name) add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS}) - if(ARG_SYSTEM) - set(inc_isystem SYSTEM) - else() - set(inc_isystem "") - endif() + target_link_libraries(${target_name} PRIVATE pybind11::module) - set(PYBIND11_INCLUDE_DIR_SELECTED "") - if(PYBIND11_INCLUDE_DIR) - # from project CMakeLists.txt - set(PYBIND11_INCLUDE_DIR_SELECTED ${PYBIND11_INCLUDE_DIR}) - elseif(pybind11_INCLUDE_DIR) - # from pybind11Config - set(PYBIND11_INCLUDE_DIR_SELECTED ${pybind11_INCLUDE_DIR}) - else() - message(FATAL "No pybind11_INCLUDE_DIR available. Use " - "find_package(pybind11) before calling pybind11_add_module.") + if(ARG_SYSTEM) + message( + STATUS + "Warning: this does not have an effect - use NO_SYSTEM_FROM_IMPORTED if using imported targets" + ) endif() - target_include_directories(${target_name} ${inc_isystem} - PRIVATE ${PYBIND11_INCLUDE_DIR_SELECTED} - PRIVATE ${PYTHON_INCLUDE_DIRS}) - - # Python debug libraries expose slightly different objects + # Python debug libraries expose slightly different objects before 3.8 # https://docs.python.org/3.6/c-api/intro.html#debugging-builds # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib if(PYTHON_IS_DEBUG) @@ -189,64 +177,33 @@ function(pybind11_add_module target_name) set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden") set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden") - if(WIN32 OR CYGWIN) - # Link against the Python shared library on Windows - target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES}) - elseif(APPLE) - # It's quite common to have multiple copies of the same Python version - # installed on one's system. E.g.: one copy from the OS and another copy - # that's statically linked into an application like Blender or Maya. - # If we link our plugin library against the OS Python here and import it - # into Blender or Maya later on, this will cause segfaults when multiple - # conflicting Python instances are active at the same time (even when they - # are of the same version). - - # Windows is not affected by this issue since it handles DLL imports - # differently. The solution for Linux and Mac OS is simple: we just don't - # link against the Python library. The resulting shared library will have - # missing symbols, but that's perfectly fine -- they will be resolved at - # import time. - - target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup") - - if(ARG_SHARED) - # Suppress CMake >= 3.0 warning for shared libraries - set_target_properties(${target_name} PROPERTIES MACOSX_RPATH ON) - endif() - endif() - - # Make sure C++11/14 are enabled - if(PYBIND11_CPP_STANDARD) - if(CMAKE_VERSION VERSION_LESS 3.3) - target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD}) - else() - target_compile_options(${target_name} PUBLIC $<$:${PYBIND11_CPP_STANDARD}>) - endif() - endif() - - # Python 2 doesn't really support C++17, Clang warns/errors over it - if(CMAKE_CXX_STANDARD - AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" - AND PYTHON_VERSION VERSION_LESS 3.0 - AND NOT CMAKE_CXX_STANDARD LESS 17) - target_compile_options(${target_name} PUBLIC -Wno-register) - endif() - if(ARG_NO_EXTRAS) return() endif() - _pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO}) + if(CMAKE_VERSION VERSION_LESS 3.9 OR PYBIND11_CLASSIC_LTO) + _pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO}) + else() + include(CheckIPOSupported) + check_ipo_supported(RESULT supported OUTPUT error) + if(supported) + set_property(TARGET ${target_name} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() + endif() - if (NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) + if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) # Strip unnecessary sections of the binary on Linux/Mac OS if(CMAKE_STRIP) if(APPLE) - add_custom_command(TARGET ${target_name} POST_BUILD - COMMAND ${CMAKE_STRIP} -x $) + add_custom_command( + TARGET ${target_name} + POST_BUILD + COMMAND ${CMAKE_STRIP} -x $) else() - add_custom_command(TARGET ${target_name} POST_BUILD - COMMAND ${CMAKE_STRIP} $) + add_custom_command( + TARGET ${target_name} + POST_BUILD + COMMAND ${CMAKE_STRIP} $) endif() endif() endif() @@ -262,7 +219,8 @@ function(pybind11_add_module target_name) # instance, projects that include other types of source files like CUDA # .cu files don't get these options propagated to nvcc since that would # cause the build to fail. - target_compile_options(${target_name} PRIVATE $<$>:$<$:/MP>>) + target_compile_options(${target_name} + PRIVATE $<$>:$<$:/MP>>) endif() endif() endfunction()