Back to home page

EIC code displayed by LXR

 
 

    


Warning, /jana2/src/python/externals/pybind11-2.10.3/tests/CMakeLists.txt is written in an unsupported language. File is not indexed.

0001 # CMakeLists.txt -- Build system for the pybind11 test suite
0002 #
0003 # Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
0004 #
0005 # All rights reserved. Use of this source code is governed by a
0006 # BSD-style license that can be found in the LICENSE file.
0007 
0008 cmake_minimum_required(VERSION 3.4)
0009 
0010 # The `cmake_minimum_required(VERSION 3.4...3.18)` syntax does not work with
0011 # some versions of VS that have a patched CMake 3.11. This forces us to emulate
0012 # the behavior using the following workaround:
0013 if(${CMAKE_VERSION} VERSION_LESS 3.21)
0014   cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
0015 else()
0016   cmake_policy(VERSION 3.21)
0017 endif()
0018 
0019 # Only needed for CMake < 3.5 support
0020 include(CMakeParseArguments)
0021 
0022 # Filter out items; print an optional message if any items filtered. This ignores extensions.
0023 #
0024 # Usage:
0025 #   pybind11_filter_tests(LISTNAME file1.cpp file2.cpp ... MESSAGE "")
0026 #
0027 macro(pybind11_filter_tests LISTNAME)
0028   cmake_parse_arguments(ARG "" "MESSAGE" "" ${ARGN})
0029   set(PYBIND11_FILTER_TESTS_FOUND OFF)
0030   # Make a list of the test without any extensions, for easier filtering.
0031   set(_TMP_ACTUAL_LIST "${${LISTNAME}};") # enforce ';' at the end to allow matching last item.
0032   string(REGEX REPLACE "\\.[^.;]*;" ";" LIST_WITHOUT_EXTENSIONS "${_TMP_ACTUAL_LIST}")
0033   foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS)
0034     string(REGEX REPLACE "\\.[^.]*$" "" filename_no_ext ${filename})
0035     # Search in the list without extensions.
0036     list(FIND LIST_WITHOUT_EXTENSIONS ${filename_no_ext} _FILE_FOUND)
0037     if(_FILE_FOUND GREATER -1)
0038       list(REMOVE_AT ${LISTNAME} ${_FILE_FOUND}) # And remove from the list with extensions.
0039       list(REMOVE_AT LIST_WITHOUT_EXTENSIONS ${_FILE_FOUND}
0040       )# And our search list, to ensure it is in sync.
0041       set(PYBIND11_FILTER_TESTS_FOUND ON)
0042     endif()
0043   endforeach()
0044   if(PYBIND11_FILTER_TESTS_FOUND AND ARG_MESSAGE)
0045     message(STATUS "${ARG_MESSAGE}")
0046   endif()
0047 endmacro()
0048 
0049 macro(possibly_uninitialized)
0050   foreach(VARNAME ${ARGN})
0051     if(NOT DEFINED "${VARNAME}")
0052       set("${VARNAME}" "")
0053     endif()
0054   endforeach()
0055 endmacro()
0056 
0057 # Function to add additional targets if any of the provided tests are found.
0058 # Needles; Specifies the test names to look for.
0059 # Additions; Specifies the additional test targets to add when any of the needles are found.
0060 macro(tests_extra_targets needles additions)
0061   # Add the index for this relation to the index extra targets map.
0062   list(LENGTH PYBIND11_TEST_EXTRA_TARGETS PYBIND11_TEST_EXTRA_TARGETS_LEN)
0063   list(APPEND PYBIND11_TEST_EXTRA_TARGETS ${PYBIND11_TEST_EXTRA_TARGETS_LEN})
0064   # Add the test names to look for, and the associated test target additions.
0065   set(PYBIND11_TEST_EXTRA_TARGETS_NEEDLES_${PYBIND11_TEST_EXTRA_TARGETS_LEN} ${needles})
0066   set(PYBIND11_TEST_EXTRA_TARGETS_ADDITION_${PYBIND11_TEST_EXTRA_TARGETS_LEN} ${additions})
0067 endmacro()
0068 
0069 # New Python support
0070 if(DEFINED Python_EXECUTABLE)
0071   set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}")
0072   set(PYTHON_VERSION "${Python_VERSION}")
0073 endif()
0074 
0075 # There's no harm in including a project in a project
0076 project(pybind11_tests CXX)
0077 
0078 # Access FindCatch and more
0079 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../tools")
0080 
0081 option(PYBIND11_WERROR "Report all warnings as errors" OFF)
0082 option(DOWNLOAD_EIGEN "Download EIGEN (requires CMake 3.11+)" OFF)
0083 option(PYBIND11_CUDA_TESTS "Enable building CUDA tests (requires CMake 3.12+)" OFF)
0084 set(PYBIND11_TEST_OVERRIDE
0085     ""
0086     CACHE STRING "Tests from ;-separated list of *.cpp files will be built instead of all tests")
0087 set(PYBIND11_TEST_FILTER
0088     ""
0089     CACHE STRING "Tests from ;-separated list of *.cpp files will be removed from all tests")
0090 
0091 if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
0092   # We're being loaded directly, i.e. not via add_subdirectory, so make this
0093   # work as its own project and load the pybind11Config to get the tools we need
0094   find_package(pybind11 REQUIRED CONFIG)
0095 endif()
0096 
0097 if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
0098   message(STATUS "Setting tests build type to MinSizeRel as none was specified")
0099   set(CMAKE_BUILD_TYPE
0100       MinSizeRel
0101       CACHE STRING "Choose the type of build." FORCE)
0102   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel"
0103                                                "RelWithDebInfo")
0104 endif()
0105 
0106 if(PYBIND11_CUDA_TESTS)
0107   enable_language(CUDA)
0108   if(DEFINED CMAKE_CXX_STANDARD)
0109     set(CMAKE_CUDA_STANDARD ${CMAKE_CXX_STANDARD})
0110   endif()
0111   set(CMAKE_CUDA_STANDARD_REQUIRED ON)
0112 endif()
0113 
0114 # Full set of test files (you can override these; see below, overrides ignore extension)
0115 # Any test that has no extension is both .py and .cpp, so 'foo' will add 'foo.cpp' and 'foo.py'.
0116 # Any test that has an extension is exclusively that and handled as such.
0117 set(PYBIND11_TEST_FILES
0118     test_async
0119     test_buffers
0120     test_builtin_casters
0121     test_call_policies
0122     test_callbacks
0123     test_chrono
0124     test_class
0125     test_const_name
0126     test_constants_and_functions
0127     test_copy_move
0128     test_custom_type_casters
0129     test_custom_type_setup
0130     test_docstring_options
0131     test_eigen_matrix
0132     test_eigen_tensor
0133     test_enum
0134     test_eval
0135     test_exceptions
0136     test_factory_constructors
0137     test_gil_scoped
0138     test_iostream
0139     test_kwargs_and_defaults
0140     test_local_bindings
0141     test_methods_and_attributes
0142     test_modules
0143     test_multiple_inheritance
0144     test_numpy_array
0145     test_numpy_dtypes
0146     test_numpy_vectorize
0147     test_opaque_types
0148     test_operator_overloading
0149     test_pickling
0150     test_pytypes
0151     test_sequences_and_iterators
0152     test_smart_ptr
0153     test_stl
0154     test_stl_binders
0155     test_tagbased_polymorphic
0156     test_thread
0157     test_union
0158     test_virtual_functions)
0159 
0160 # Invoking cmake with something like:
0161 #     cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp" ..
0162 # lets you override the tests that get compiled and run.  You can restore to all tests with:
0163 #     cmake -DPYBIND11_TEST_OVERRIDE= ..
0164 if(PYBIND11_TEST_OVERRIDE)
0165   # Instead of doing a direct override here, we iterate over the overrides without extension and
0166   # match them against entries from the PYBIND11_TEST_FILES, anything that not matches goes into the filter list.
0167   string(REGEX REPLACE "\\.[^.;]*;" ";" TEST_OVERRIDE_NO_EXT "${PYBIND11_TEST_OVERRIDE};")
0168   string(REGEX REPLACE "\\.[^.;]*;" ";" TEST_FILES_NO_EXT "${PYBIND11_TEST_FILES};")
0169   # This allows the override to be done with extensions, preserving backwards compatibility.
0170   foreach(test_name ${TEST_FILES_NO_EXT})
0171     if(NOT ${test_name} IN_LIST TEST_OVERRIDE_NO_EXT
0172     )# If not in the whitelist, add to be filtered out.
0173       list(APPEND PYBIND11_TEST_FILTER ${test_name})
0174     endif()
0175   endforeach()
0176 endif()
0177 
0178 # You can also filter tests:
0179 if(PYBIND11_TEST_FILTER)
0180   pybind11_filter_tests(PYBIND11_TEST_FILES ${PYBIND11_TEST_FILTER})
0181 endif()
0182 
0183 # Skip tests for CUDA check:
0184 # /pybind11/tests/test_constants_and_functions.cpp(125):
0185 #   error: incompatible exception specifications
0186 if(PYBIND11_CUDA_TESTS)
0187   pybind11_filter_tests(
0188     PYBIND11_TEST_FILES test_constants_and_functions.cpp MESSAGE
0189     "Skipping test_constants_and_functions due to incompatible exception specifications")
0190 endif()
0191 
0192 # Now that the test filtering is complete, we need to split the list into the test for PYTEST
0193 # and the list for the cpp targets.
0194 set(PYBIND11_CPPTEST_FILES "")
0195 set(PYBIND11_PYTEST_FILES "")
0196 
0197 foreach(test_name ${PYBIND11_TEST_FILES})
0198   if(test_name MATCHES "\\.py$") # Ends in .py, purely python test.
0199     list(APPEND PYBIND11_PYTEST_FILES ${test_name})
0200   elseif(test_name MATCHES "\\.cpp$") # Ends in .cpp, purely cpp test.
0201     list(APPEND PYBIND11_CPPTEST_FILES ${test_name})
0202   elseif(NOT test_name MATCHES "\\.") # No extension specified, assume both, add extension.
0203     list(APPEND PYBIND11_PYTEST_FILES ${test_name}.py)
0204     list(APPEND PYBIND11_CPPTEST_FILES ${test_name}.cpp)
0205   else()
0206     message(WARNING "Unhanded test extension in test: ${test_name}")
0207   endif()
0208 endforeach()
0209 set(PYBIND11_TEST_FILES ${PYBIND11_CPPTEST_FILES})
0210 list(SORT PYBIND11_PYTEST_FILES)
0211 
0212 # Contains the set of test files that require pybind11_cross_module_tests to be
0213 # built; if none of these are built (i.e. because TEST_OVERRIDE is used and
0214 # doesn't include them) the second module doesn't get built.
0215 tests_extra_targets("test_exceptions.py;test_local_bindings.py;test_stl.py;test_stl_binders.py"
0216                     "pybind11_cross_module_tests")
0217 
0218 # And add additional targets for other tests.
0219 tests_extra_targets("test_exceptions.py" "cross_module_interleaved_error_already_set")
0220 tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils")
0221 
0222 set(PYBIND11_EIGEN_REPO
0223     "https://gitlab.com/libeigen/eigen.git"
0224     CACHE STRING "Eigen repository to use for tests")
0225 # Always use a hash for reconfigure speed and security reasons
0226 # Include the version number for pretty printing (keep in sync)
0227 set(PYBIND11_EIGEN_VERSION_AND_HASH
0228     "3.4.0;929bc0e191d0927b1735b9a1ddc0e8b77e3a25ec"
0229     CACHE STRING "Eigen version to use for tests, format: VERSION;HASH")
0230 
0231 list(GET PYBIND11_EIGEN_VERSION_AND_HASH 0 PYBIND11_EIGEN_VERSION_STRING)
0232 list(GET PYBIND11_EIGEN_VERSION_AND_HASH 1 PYBIND11_EIGEN_VERSION_HASH)
0233 
0234 # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
0235 # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
0236 # skip message).
0237 list(FIND PYBIND11_TEST_FILES test_eigen_matrix.cpp PYBIND11_TEST_FILES_EIGEN_I)
0238 if(PYBIND11_TEST_FILES_EIGEN_I EQUAL -1)
0239   list(FIND PYBIND11_TEST_FILES test_eigen_tensor.cpp PYBIND11_TEST_FILES_EIGEN_I)
0240 endif()
0241 if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
0242   # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
0243   # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
0244   # produces a fatal error if loaded from a pre-3.0 cmake.
0245   if(DOWNLOAD_EIGEN)
0246     if(CMAKE_VERSION VERSION_LESS 3.11)
0247       message(FATAL_ERROR "CMake 3.11+ required when using DOWNLOAD_EIGEN")
0248     endif()
0249 
0250     include(FetchContent)
0251     FetchContent_Declare(
0252       eigen
0253       GIT_REPOSITORY "${PYBIND11_EIGEN_REPO}"
0254       GIT_TAG "${PYBIND11_EIGEN_VERSION_HASH}")
0255 
0256     FetchContent_GetProperties(eigen)
0257     if(NOT eigen_POPULATED)
0258       message(
0259         STATUS
0260           "Downloading Eigen ${PYBIND11_EIGEN_VERSION_STRING} (${PYBIND11_EIGEN_VERSION_HASH}) from ${PYBIND11_EIGEN_REPO}"
0261       )
0262       FetchContent_Populate(eigen)
0263     endif()
0264 
0265     set(EIGEN3_INCLUDE_DIR ${eigen_SOURCE_DIR})
0266     set(EIGEN3_FOUND TRUE)
0267     # When getting locally, the version is not visible from a superprojet,
0268     # so just force it.
0269     set(EIGEN3_VERSION "${PYBIND11_EIGEN_VERSION_STRING}")
0270 
0271   else()
0272     find_package(Eigen3 3.2.7 QUIET CONFIG)
0273 
0274     if(NOT EIGEN3_FOUND)
0275       # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
0276       # tools/FindEigen3.cmake
0277       find_package(Eigen3 3.2.7 QUIET)
0278     endif()
0279   endif()
0280 
0281   if(EIGEN3_FOUND)
0282     if(NOT TARGET Eigen3::Eigen)
0283       add_library(Eigen3::Eigen IMPORTED INTERFACE)
0284       set_property(TARGET Eigen3::Eigen PROPERTY INTERFACE_INCLUDE_DIRECTORIES
0285                                                  "${EIGEN3_INCLUDE_DIR}")
0286     endif()
0287 
0288     # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed
0289     # rather than looking it up in the cmake script); older versions, and the
0290     # tools/FindEigen3.cmake, set EIGEN3_VERSION instead.
0291     if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING)
0292       set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
0293     endif()
0294     message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
0295 
0296     if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0))
0297       tests_extra_targets("test_eigen_tensor.py" "eigen_tensor_avoid_stl_array")
0298     endif()
0299 
0300   else()
0301     list(FIND PYBIND11_TEST_FILES test_eigen_matrix.cpp PYBIND11_TEST_FILES_EIGEN_I)
0302     if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
0303       list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
0304     endif()
0305 
0306     list(FIND PYBIND11_TEST_FILES test_eigen_tensor.cpp PYBIND11_TEST_FILES_EIGEN_I)
0307     if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
0308       list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
0309     endif()
0310     message(
0311       STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON on CMake 3.11+ to download")
0312   endif()
0313 endif()
0314 
0315 # Some code doesn't support gcc 4
0316 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
0317   list(FIND PYBIND11_TEST_FILES test_eigen_tensor.cpp PYBIND11_TEST_FILES_EIGEN_I)
0318   if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
0319     list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
0320   endif()
0321 endif()
0322 
0323 # Optional dependency for some tests (boost::variant is only supported with version >= 1.56)
0324 find_package(Boost 1.56)
0325 
0326 if(Boost_FOUND)
0327   if(NOT TARGET Boost::headers)
0328     add_library(Boost::headers IMPORTED INTERFACE)
0329     if(TARGET Boost::boost)
0330       # Classic FindBoost
0331       set_property(TARGET Boost::boost PROPERTY INTERFACE_LINK_LIBRARIES Boost::boost)
0332     else()
0333       # Very old FindBoost, or newer Boost than CMake in older CMakes
0334       set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
0335                                                   ${Boost_INCLUDE_DIRS})
0336     endif()
0337   endif()
0338 endif()
0339 
0340 # Check if we need to add -lstdc++fs or -lc++fs or nothing
0341 if(DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD LESS 17)
0342   set(STD_FS_NO_LIB_NEEDED TRUE)
0343 elseif(MSVC)
0344   set(STD_FS_NO_LIB_NEEDED TRUE)
0345 else()
0346   file(
0347     WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
0348     "#include <filesystem>\nint main(int argc, char ** argv) {\n  std::filesystem::path p(argv[0]);\n  return p.string().length();\n}"
0349   )
0350   try_compile(
0351     STD_FS_NO_LIB_NEEDED ${CMAKE_CURRENT_BINARY_DIR}
0352     SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
0353     COMPILE_DEFINITIONS -std=c++17)
0354   try_compile(
0355     STD_FS_NEEDS_STDCXXFS ${CMAKE_CURRENT_BINARY_DIR}
0356     SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
0357     COMPILE_DEFINITIONS -std=c++17
0358     LINK_LIBRARIES stdc++fs)
0359   try_compile(
0360     STD_FS_NEEDS_CXXFS ${CMAKE_CURRENT_BINARY_DIR}
0361     SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
0362     COMPILE_DEFINITIONS -std=c++17
0363     LINK_LIBRARIES c++fs)
0364 endif()
0365 
0366 if(${STD_FS_NEEDS_STDCXXFS})
0367   set(STD_FS_LIB stdc++fs)
0368 elseif(${STD_FS_NEEDS_CXXFS})
0369   set(STD_FS_LIB c++fs)
0370 elseif(${STD_FS_NO_LIB_NEEDED})
0371   set(STD_FS_LIB "")
0372 else()
0373   message(WARNING "Unknown C++17 compiler - not passing -lstdc++fs")
0374   set(STD_FS_LIB "")
0375 endif()
0376 
0377 # Compile with compiler warnings turned on
0378 function(pybind11_enable_warnings target_name)
0379   if(MSVC)
0380     target_compile_options(${target_name} PRIVATE /W4 /wd4189)
0381   elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS)
0382     target_compile_options(
0383       ${target_name}
0384       PRIVATE -Wall
0385               -Wextra
0386               -Wconversion
0387               -Wcast-qual
0388               -Wdeprecated
0389               -Wundef
0390               -Wnon-virtual-dtor)
0391   endif()
0392 
0393   if(PYBIND11_WERROR)
0394     if(MSVC)
0395       target_compile_options(${target_name} PRIVATE /WX)
0396     elseif(PYBIND11_CUDA_TESTS)
0397       target_compile_options(${target_name} PRIVATE "SHELL:-Werror all-warnings")
0398     elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang|IntelLLVM)")
0399       target_compile_options(${target_name} PRIVATE -Werror)
0400     elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
0401       if(CMAKE_CXX_STANDARD EQUAL 17) # See PR #3570
0402         target_compile_options(${target_name} PRIVATE -Wno-conversion)
0403       endif()
0404       target_compile_options(
0405         ${target_name}
0406         PRIVATE
0407           -Werror-all
0408           # "Inlining inhibited by limit max-size", "Inlining inhibited by limit max-total-size"
0409           -diag-disable 11074,11076)
0410     endif()
0411   endif()
0412 endfunction()
0413 
0414 set(test_targets pybind11_tests)
0415 
0416 # Check if any tests need extra targets by iterating through the mappings registered.
0417 foreach(i ${PYBIND11_TEST_EXTRA_TARGETS})
0418   foreach(needle ${PYBIND11_TEST_EXTRA_TARGETS_NEEDLES_${i}})
0419     if(needle IN_LIST PYBIND11_PYTEST_FILES)
0420       # Add all the additional targets to the test list. List join in newer cmake.
0421       foreach(extra_target ${PYBIND11_TEST_EXTRA_TARGETS_ADDITION_${i}})
0422         list(APPEND test_targets ${extra_target})
0423       endforeach()
0424       break() # Breaks out of the needle search, continues with the next mapping.
0425     endif()
0426   endforeach()
0427 endforeach()
0428 
0429 # Support CUDA testing by forcing the target file to compile with NVCC
0430 if(PYBIND11_CUDA_TESTS)
0431   set_property(SOURCE ${PYBIND11_TEST_FILES} PROPERTY LANGUAGE CUDA)
0432 endif()
0433 
0434 foreach(target ${test_targets})
0435   set(test_files ${PYBIND11_TEST_FILES})
0436   if(NOT "${target}" STREQUAL "pybind11_tests")
0437     set(test_files "")
0438   endif()
0439 
0440   # Support CUDA testing by forcing the target file to compile with NVCC
0441   if(PYBIND11_CUDA_TESTS)
0442     set_property(SOURCE ${target}.cpp PROPERTY LANGUAGE CUDA)
0443   endif()
0444 
0445   # Create the binding library
0446   pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS})
0447   pybind11_enable_warnings(${target})
0448 
0449   if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
0450     get_property(
0451       suffix
0452       TARGET ${target}
0453       PROPERTY SUFFIX)
0454     set(source_output "${CMAKE_CURRENT_SOURCE_DIR}/${target}${suffix}")
0455     if(suffix AND EXISTS "${source_output}")
0456       message(WARNING "Output file also in source directory; "
0457                       "please remove to avoid confusion: ${source_output}")
0458     endif()
0459   endif()
0460 
0461   if(MSVC)
0462     target_compile_options(${target} PRIVATE /utf-8)
0463   endif()
0464 
0465   if(EIGEN3_FOUND)
0466     target_link_libraries(${target} PRIVATE Eigen3::Eigen)
0467     target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_EIGEN)
0468   endif()
0469 
0470   if(Boost_FOUND)
0471     target_link_libraries(${target} PRIVATE Boost::headers)
0472     target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST)
0473   endif()
0474 
0475   target_link_libraries(${target} PRIVATE ${STD_FS_LIB})
0476 
0477   # Always write the output file directly into the 'tests' directory (even on MSVC)
0478   if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
0479     set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
0480                                                "${CMAKE_CURRENT_BINARY_DIR}")
0481 
0482     if(DEFINED CMAKE_CONFIGURATION_TYPES)
0483       foreach(config ${CMAKE_CONFIGURATION_TYPES})
0484         string(TOUPPER ${config} config)
0485         set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
0486                                                    "${CMAKE_CURRENT_BINARY_DIR}")
0487       endforeach()
0488     endif()
0489   endif()
0490 endforeach()
0491 
0492 # Provide nice organisation in IDEs
0493 if(NOT CMAKE_VERSION VERSION_LESS 3.8)
0494   source_group(
0495     TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include"
0496     PREFIX "Header Files"
0497     FILES ${PYBIND11_HEADERS})
0498 endif()
0499 
0500 # Make sure pytest is found or produce a warning
0501 pybind11_find_import(pytest VERSION 3.1)
0502 
0503 if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
0504   # This is not used later in the build, so it's okay to regenerate each time.
0505   configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pytest.ini" "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
0506                  COPYONLY)
0507   file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
0508        "\ntestpaths = \"${CMAKE_CURRENT_SOURCE_DIR}\"")
0509 
0510 endif()
0511 
0512 # cmake 3.12 added list(transform <list> prepend
0513 # but we can't use it yet
0514 string(REPLACE "test_" "${CMAKE_CURRENT_SOURCE_DIR}/test_" PYBIND11_ABS_PYTEST_FILES
0515                "${PYBIND11_PYTEST_FILES}")
0516 
0517 set(PYBIND11_TEST_PREFIX_COMMAND
0518     ""
0519     CACHE STRING "Put this before pytest, use for checkers and such")
0520 
0521 # A single command to compile and run the tests
0522 add_custom_target(
0523   pytest
0524   COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest
0525           ${PYBIND11_ABS_PYTEST_FILES}
0526   DEPENDS ${test_targets}
0527   WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
0528   USES_TERMINAL)
0529 
0530 if(PYBIND11_TEST_OVERRIDE)
0531   add_custom_command(
0532     TARGET pytest
0533     POST_BUILD
0534     COMMAND ${CMAKE_COMMAND} -E echo
0535             "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
0536 endif()
0537 
0538 # cmake-format: off
0539 add_custom_target(
0540   memcheck
0541   COMMAND
0542     PYTHONMALLOC=malloc
0543     valgrind
0544     --leak-check=full
0545     --show-leak-kinds=definite,indirect
0546     --errors-for-leak-kinds=definite,indirect
0547     --error-exitcode=1
0548     --read-var-info=yes
0549     --track-origins=yes
0550     --suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-python.supp"
0551     --suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-numpy-scipy.supp"
0552     --gen-suppressions=all
0553     ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_ABS_PYTEST_FILES}
0554   DEPENDS ${test_targets}
0555   WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
0556   USES_TERMINAL)
0557 # cmake-format: on
0558 
0559 # Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
0560 add_custom_target(check DEPENDS pytest)
0561 
0562 # The remaining tests only apply when being built as part of the pybind11 project, but not if the
0563 # tests are being built independently.
0564 if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
0565   return()
0566 endif()
0567 
0568 # Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it:
0569 add_custom_command(
0570   TARGET pybind11_tests
0571   POST_BUILD
0572   COMMAND
0573     ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../tools/libsize.py
0574     $<TARGET_FILE:pybind11_tests>
0575     ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
0576 
0577 if(NOT PYBIND11_CUDA_TESTS)
0578   # Test embedding the interpreter. Provides the `cpptest` target.
0579   add_subdirectory(test_embed)
0580 
0581   # Test CMake build using functions and targets from subdirectory or installed location
0582   add_subdirectory(test_cmake_build)
0583 endif()