diff --git a/.appveyor.yml b/.appveyor.yml index 391cf1071c..c39ba88a38 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -26,7 +26,9 @@ build_script: - cmake -G "%CMAKE_GENERATOR%" -A "%CMAKE_ARCH%" -DCMAKE_CXX_STANDARD=14 -DPYBIND11_WERROR=ON + -DPYBIND11_CATCH_TESTS=ON -DDOWNLOAD_CATCH=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_SUPPRESS_REGENERATION=1 . - set MSBuildLogger="C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index a09b630a21..f565c69f12 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -87,7 +87,7 @@ system with CMake 3.15+: python3 -m venv .venv source .venv/bin/activate pip install -r tests/requirements.txt -cmake -S . -B build -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON +cmake -S . -B build -DPYBIND11_CATCH_TESTS=ON -DPYBIND11_EIGEN_TESTS=ON cmake --build build -j4 ``` @@ -149,8 +149,10 @@ The valid options are: classic, deprecated, custom FindPythonLibs * `-DPYBIND11_NOPYTHON=ON`: Disable all Python searching (disables tests) * `-DBUILD_TESTING=ON`: Enable the tests -* `-DDOWNLOAD_CATCH=ON`: Download catch to build the C++ tests -* `-DDOWNLOAD_EIGEN=ON`: Download Eigen for the NumPy tests +* `-DPYBIND11_CATCH_TESTS=ON`: Enable tests using catch + (Fetched automatically if missing) +* `-DPYBIND11_EIGEN_TESTS=ON`: Enable testing of Eigen for the NumPy tests + (Fetched automatically if missing) * `-DPYBIND11_INSTALL=ON/OFF`: Enable the install target (on by default for the master project) * `-DUSE_PYTHON_INSTALL_DIR=ON`: Try to install into the python dir @@ -295,7 +297,7 @@ recent CMake and Python): python3 -m venv venv . venv/bin/activate pip install pytest -cmake -S . -B build-intel -DCMAKE_CXX_COMPILER=$(which icpc) -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON -DPYBIND11_WERROR=ON +cmake -S . -B build-intel -DCMAKE_CXX_COMPILER=$(which icpc) -DPYBIND11_CATCH_TESTS=ON -DPYBIND11_EIGEN_TESTS=ON -DPYBIND11_WERROR=ON ``` This will test the PGI compilers: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 64b680494d..88a67ad093 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -205,8 +205,8 @@ jobs: -DPYBIND11_WERROR=ON -DPYBIND11_SIMPLE_GIL_MANAGEMENT=ON -DPYBIND11_PYTEST_ARGS=-v - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_STANDARD=14 -DPYBIND11_INTERNALS_VERSION=10000000 @@ -308,9 +308,11 @@ jobs: run: > cmake -S . -B build -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DPYBIND11_CATCH_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}" + -DPYBIND11_EIGEN_TESTS=ON + -DCMAKE_REQUIRE_FIND_PACKAGE_Eigen3=ON -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") - name: Build @@ -344,7 +346,7 @@ jobs: run: apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y cmake git python3-dev python3-pytest python3-numpy - name: Configure - run: cmake -S . -B build -DPYBIND11_CUDA_TESTS=ON -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON + run: cmake -S . -B build -DPYBIND11_CUDA_TESTS=ON -DPYBIND11_WERROR=ON -DPYBIND11_CATCH_TESTS=ON - name: Build run: cmake --build build -j2 --verbose @@ -382,7 +384,7 @@ jobs: # run: | # source /etc/profile.d/modules.sh # module load /opt/nvidia/hpc_sdk/modulefiles/nvhpc/20.11 -# cmake -S . -B build -DDOWNLOAD_CATCH=ON -DCMAKE_CXX_STANDARD=14 -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") +# cmake -S . -B build -DPYBIND11_CATCH_TESTS=ON -DCMAKE_CXX_STANDARD=14 -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") # # - name: Build # run: cmake --build build -j 2 --verbose @@ -437,7 +439,7 @@ jobs: run: | source /etc/profile.d/modules.sh module load /opt/nvidia/hpc_sdk/modulefiles/nvhpc/25.11 - cmake -S . -B build -DDOWNLOAD_CATCH=ON \ + cmake -S . -B build -DPYBIND11_CATCH_TESTS=ON \ -DCMAKE_CXX_STANDARD=17 \ -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") \ -DCMAKE_CXX_FLAGS="-Wc,--pending_instantiations=0" \ @@ -493,9 +495,11 @@ jobs: run: > cmake -S . -B build -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DPYBIND11_CATCH_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}" + -DPYBIND11_EIGEN_TESTS=ON + -DCMAKE_REQUIRE_FIND_PACKAGE_Eigen3=ON -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") - name: Build @@ -520,7 +524,7 @@ jobs: run: > cmake -S . -B build_partial -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DPYBIND11_CATCH_TESTS=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") "-DPYBIND11_TEST_OVERRIDE=test_call_policies.cpp;test_gil_scoped.cpp;test_thread.cpp" @@ -570,8 +574,8 @@ jobs: set +e; source /opt/intel/oneapi/setvars.sh; set -e cmake -S . -B build-11 \ -DPYBIND11_WERROR=ON \ - -DDOWNLOAD_CATCH=ON \ - -DDOWNLOAD_EIGEN=OFF \ + -DPYBIND11_CATCH_TESTS=ON \ + -DPYBIND11_EIGEN_TESTS=OFF \ -DCMAKE_CXX_STANDARD=11 \ -DCMAKE_CXX_COMPILER=$(which icpc) \ -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") @@ -608,8 +612,8 @@ jobs: set +e; source /opt/intel/oneapi/setvars.sh; set -e cmake -S . -B build-17 \ -DPYBIND11_WERROR=ON \ - -DDOWNLOAD_CATCH=ON \ - -DDOWNLOAD_EIGEN=OFF \ + -DPYBIND11_CATCH_TESTS=ON \ + -DPYBIND11_EIGEN_TESTS=OFF \ -DCMAKE_CXX_STANDARD=17 \ -DCMAKE_CXX_COMPILER=$(which icpc) \ -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") @@ -686,8 +690,8 @@ jobs: cmake -S . -B build -DCMAKE_BUILD_TYPE=MinSizeRel -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_STANDARD=11 -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") @@ -744,8 +748,10 @@ jobs: - name: Configure tests run: > cmake ../pybind11-tests - -DDOWNLOAD_CATCH=ON + -DPYBIND11_CATCH_TESTS=ON -DPYBIND11_WERROR=ON + -DPYBIND11_EIGEN_TESTS=ON + -DCMAKE_REQUIRE_FIND_PACKAGE_Eigen3=ON -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") working-directory: /build-tests @@ -838,8 +844,8 @@ jobs: cmake -S . -B build -G "Visual Studio 17 2022" -A Win32 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON ${{ matrix.args }} - name: Build C++11 run: cmake --build build -j 2 @@ -889,8 +895,8 @@ jobs: -G "Visual Studio 17 2022" -A Win32 -DCMAKE_BUILD_TYPE=Debug -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON ${{ matrix.args }} - name: Build C++11 run: cmake --build build --config Debug -j 2 @@ -929,8 +935,8 @@ jobs: run: > cmake -S . -B build -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_STANDARD=20 - name: Build C++20 @@ -953,8 +959,8 @@ jobs: run: > cmake -S . -B build_partial -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_STANDARD=20 "-DPYBIND11_TEST_OVERRIDE=test_call_policies.cpp;test_gil_scoped.cpp;test_thread.cpp" @@ -1006,7 +1012,7 @@ jobs: # LTO leads to many undefined reference like # `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&) run: >- - cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=11 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON + cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=11 -DPYBIND11_WERROR=ON -DPYBIND11_CATCH_TESTS=ON -DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)") -S . -B build @@ -1031,7 +1037,7 @@ jobs: - name: Configure C++14 run: >- - cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=14 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON + cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=14 -DPYBIND11_WERROR=ON -DPYBIND11_CATCH_TESTS=ON -DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)") -S . -B build2 @@ -1056,7 +1062,7 @@ jobs: - name: Configure C++17 run: >- - cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=17 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON + cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=17 -DPYBIND11_WERROR=ON -DPYBIND11_CATCH_TESTS=ON -DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)") -S . -B build3 @@ -1124,8 +1130,8 @@ jobs: cmake -G Ninja -S . -B . -DPYBIND11_WERROR=OFF -DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_STANDARD=17 @@ -1192,8 +1198,8 @@ jobs: cmake -G Ninja -S . -B . ` -DPYBIND11_WERROR=OFF ` -DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF ` - -DDOWNLOAD_CATCH=ON ` - -DDOWNLOAD_EIGEN=ON ` + -DPYBIND11_CATCH_TESTS=ON ` + -DPYBIND11_EIGEN_TESTS=ON ` -DCMAKE_CXX_COMPILER=clang++ ` -DCMAKE_CXX_STANDARD=20 ` -DPython_EXECUTABLE="$pythonExecutable" ` @@ -1267,8 +1273,8 @@ jobs: run: >- cmake -S . -B build -DPYBIND11_WERROR=OFF - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_STANDARD=20 -DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)") diff --git a/.github/workflows/configure.yml b/.github/workflows/configure.yml index fce9d14ce9..bc8ad8f451 100644 --- a/.github/workflows/configure.yml +++ b/.github/workflows/configure.yml @@ -71,7 +71,7 @@ jobs: # These steps use a directory with a space in it intentionally - name: Configure shell: bash - run: cmake -S. -B"build dir" -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON + run: cmake -S. -B"build dir" -DPYBIND11_WERROR=ON -DPYBIND11_CATCH_TESTS=ON # Only build and test if this was manually triggered in the GitHub UI - name: Build diff --git a/.github/workflows/reusable-standard.yml b/.github/workflows/reusable-standard.yml index 091ce6c8e8..819644c91c 100644 --- a/.github/workflows/reusable-standard.yml +++ b/.github/workflows/reusable-standard.yml @@ -72,8 +72,8 @@ jobs: cmake -S. -Bbuild -Werror=dev -DPYBIND11_WERROR=ON -DPYBIND11_PYTEST_ARGS=-v - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON ${{ inputs.cmake-args }} - name: Build diff --git a/.github/workflows/upstream.yml b/.github/workflows/upstream.yml index 51354d68c3..a937f7817d 100644 --- a/.github/workflows/upstream.yml +++ b/.github/workflows/upstream.yml @@ -54,8 +54,8 @@ jobs: run: > cmake -S . -B build11 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_STANDARD=11 -DCMAKE_BUILD_TYPE=Debug @@ -77,8 +77,8 @@ jobs: run: > cmake -S . -B build17 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_STANDARD=17 - name: Build C++17 @@ -96,8 +96,8 @@ jobs: run: > cmake -S . -B build17max -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON + -DPYBIND11_CATCH_TESTS=ON + -DPYBIND11_EIGEN_TESTS=ON -DCMAKE_CXX_STANDARD=17 -DPYBIND11_INTERNALS_VERSION=10000000 diff --git a/CMakePresets.json b/CMakePresets.json index 42bf3ade9d..7b6129c7ac 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -13,8 +13,8 @@ "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "CMAKE_EXPORT_COMPILE_COMMANDS": true, - "DOWNLOAD_CATCH": true, - "DOWNLOAD_EIGEN": true, + "PYBIND11_CATCH_TESTS": true, + "PYBIND11_EIGEN_TESTS": true, "PYBIND11_FINDPYTHON": "NEW", "PYBIND11_WERROR": true, "CMAKE_COLOR_DIAGNOSTICS": true diff --git a/noxfile.py b/noxfile.py index 778025229c..857d126f8b 100644 --- a/noxfile.py +++ b/noxfile.py @@ -43,8 +43,8 @@ def tests(session: nox.Session) -> None: "-S.", f"-B{tmpdir}", "-DPYBIND11_WERROR=ON", - "-DDOWNLOAD_CATCH=ON", - "-DDOWNLOAD_EIGEN=ON", + "-DPYBIND11_CATCH_TESTS=ON", + "-DPYBIND11_EIGEN_TESTS=ON", *session.posargs, ) session.run("cmake", "--build", tmpdir) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fc08a9056f..26fb7d4e9a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -67,8 +67,19 @@ project(pybind11_tests CXX) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../tools") option(PYBIND11_WERROR "Report all warnings as errors" OFF) -option(DOWNLOAD_CATCH "Download catch2 if not found" OFF) -option(DOWNLOAD_EIGEN "Download EIGEN" OFF) +option(PYBIND11_CATCH_TESTS "Enable catch tests" OFF) +option(PYBIND11_EIGEN_TESTS "Enable eigen tests" OFF) +if(CMAKE_VERSION VERSION_LESS 3.24) + option(DOWNLOAD_CATCH "Download catch2 if not found" OFF) + option(DOWNLOAD_EIGEN "Download EIGEN" OFF) +else() + if(DEFINED DOWNLOAD_CATCH OR DEFINED DOWNLOAD_EIGEN) + message( + WARNING "DOWNLOAD_* options have no effect for CMake >= 3.24.\n" + "Use instead the PYBIND11_*_TESTS and FETCHCONTENT_TRY_FIND_PACKAGE_MODE and co " + "to control if and how to pick up these dependencies.") + endif() +endif() option(PYBIND11_CUDA_TESTS "Enable building CUDA tests" OFF) option(PYBIND11_TEST_SMART_HOLDER "Change the default to smart holder" OFF) set(PYBIND11_TEST_OVERRIDE @@ -256,14 +267,22 @@ tests_extra_targets("test_standalone_enum_module.py" "standalone_enum_module") set(PYBIND11_EIGEN_REPO "https://gitlab.com/libeigen/eigen.git" CACHE STRING "Eigen repository to use for tests") +set(PYBIND11_CATCH_REPO + "https://github.com/catchorg/Catch2.git" + CACHE STRING "Catch repository to use for tests") # Always use a hash for reconfigure speed and security reasons # Include the version number for pretty printing (keep in sync) set(PYBIND11_EIGEN_VERSION_AND_HASH "3.4.0;929bc0e191d0927b1735b9a1ddc0e8b77e3a25ec" CACHE STRING "Eigen version to use for tests, format: VERSION;HASH") +set(PYBIND11_CATCH_VERSION_AND_HASH + "v2.13.10;182c910b4b63ff587a3440e08f84f70497e49a81" + CACHE STRING "Catch version to use for tests, format: VERSION;HASH") list(GET PYBIND11_EIGEN_VERSION_AND_HASH 0 PYBIND11_EIGEN_VERSION_STRING) list(GET PYBIND11_EIGEN_VERSION_AND_HASH 1 PYBIND11_EIGEN_VERSION_HASH) +list(GET PYBIND11_CATCH_VERSION_AND_HASH 0 PYBIND11_CATCH_VERSION_STRING) +list(GET PYBIND11_CATCH_VERSION_AND_HASH 1 PYBIND11_CATCH_VERSION_HASH) # 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" @@ -272,22 +291,25 @@ list(FIND PYBIND11_TEST_FILES test_eigen_matrix.cpp PYBIND11_TEST_FILES_EIGEN_I) if(PYBIND11_TEST_FILES_EIGEN_I EQUAL -1) list(FIND PYBIND11_TEST_FILES test_eigen_tensor.cpp PYBIND11_TEST_FILES_EIGEN_I) endif() -if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) +if(PYBIND11_EIGEN_TESTS AND PYBIND11_TEST_FILES_EIGEN_I GREATER -1) # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake). # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also - if(DOWNLOAD_EIGEN) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24 OR DOWNLOAD_EIGEN) if(CMAKE_VERSION VERSION_LESS 3.18) set(_opts) else() set(_opts SOURCE_SUBDIR no-cmake-build) endif() + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) + list(APPEND _opts FIND_PACKAGE_ARGS CONFIG) + endif() include(FetchContent) FetchContent_Declare( - eigen + Eigen3 GIT_REPOSITORY "${PYBIND11_EIGEN_REPO}" GIT_TAG "${PYBIND11_EIGEN_VERSION_HASH}" ${_opts}) - FetchContent_MakeAvailable(eigen) + FetchContent_MakeAvailable(Eigen3) if(NOT CMAKE_VERSION VERSION_LESS 3.18) set(EIGEN3_INCLUDE_DIR "${eigen_SOURCE_DIR}") endif() @@ -344,7 +366,6 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I}) endif() - message(STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON to download") endif() endif() @@ -356,6 +377,36 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_L endif() endif() +if(PYBIND11_CATCH_TESTS) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24 OR DOWNLOAD_CATCH) + set(_opts) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) + list(APPEND _opts FIND_PACKAGE_ARGS CONFIG) + endif() + + # Set the build options when doing FetchContent + # TODO: Not needed in more recent Catch versions + option(CATCH_BUILD_TESTING "Overridden by pybind11" OFF) + option(CATCH_ENABLE_WERROR "Overridden by pybind11" OFF) + + include(FetchContent) + FetchContent_Declare( + Catch2 + GIT_REPOSITORY "${PYBIND11_CATCH_REPO}" + GIT_TAG "${PYBIND11_CATCH_VERSION_HASH}" + ${_opts}) + FetchContent_MakeAvailable(Catch2) + # Fetchcontent-ed branch does not set CATCH_VERSION + if(NOT Catch2_VERSION) + set(Catch2_VERSION ${PYBIND11_CATCH_VERSION_STRING}) + endif() + else() + find_package(Catch2 2.13.10 REQUIRED) + endif() + + message(STATUS "Building interpreter tests using Catch v${CATCH_VERSION}") +endif() + # Optional dependency for some tests (boost::variant is only supported with version >= 1.56) find_package(Boost 1.56) @@ -680,15 +731,17 @@ add_custom_command( ${CMAKE_CURRENT_BINARY_DIR}/sosize-$.txt) if(NOT PYBIND11_CUDA_TESTS) - # Test pure C++ code (not depending on Python). Provides the `test_pure_cpp` target. - add_subdirectory(pure_cpp) + if(PYBIND11_CATCH_TESTS) + # Test pure C++ code (not depending on Python). Provides the `test_pure_cpp` target. + add_subdirectory(pure_cpp) + + # Test C++ code that depends on Python, such as embedding the interpreter. Provides the `cpptest` target. + add_subdirectory(test_with_catch) - # Test C++ code that depends on Python, such as embedding the interpreter. Provides the `cpptest` target. - add_subdirectory(test_with_catch) + # Test visibility of common symbols across shared libraries + add_subdirectory(test_cross_module_rtti) + endif() # Test CMake build using functions and targets from subdirectory or installed location add_subdirectory(test_cmake_build) - - # Test visibility of common symbols across shared libraries - add_subdirectory(test_cross_module_rtti) endif() diff --git a/tests/pure_cpp/CMakeLists.txt b/tests/pure_cpp/CMakeLists.txt index d2757db766..77515ca30a 100644 --- a/tests/pure_cpp/CMakeLists.txt +++ b/tests/pure_cpp/CMakeLists.txt @@ -1,12 +1,4 @@ -find_package(Catch 2.13.10) - -if(CATCH_FOUND) - message(STATUS "Building pure C++ tests (not depending on Python) using Catch v${CATCH_VERSION}") -else() - message(STATUS "Catch not detected. Interpreter tests will be skipped. Install Catch headers" - " manually or use `cmake -DDOWNLOAD_CATCH=ON` to fetch them automatically.") - return() -endif() +message(STATUS "Building pure C++ tests (not depending on Python) using Catch ${CATCH_VERSION}") add_executable(smart_holder_poc_test smart_holder_poc_test.cpp) pybind11_enable_warnings(smart_holder_poc_test) diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index 09720432cf..7f44aa433b 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -12,7 +12,7 @@ #endif #define CATCH_CONFIG_MAIN -#include "catch.hpp" +#include using pybind11::memory::guarded_delete; using pybind11::memory::smart_holder; diff --git a/tests/test_cross_module_rtti/CMakeLists.txt b/tests/test_cross_module_rtti/CMakeLists.txt index c9b95bfba1..3a3589e80c 100644 --- a/tests/test_cross_module_rtti/CMakeLists.txt +++ b/tests/test_cross_module_rtti/CMakeLists.txt @@ -8,26 +8,16 @@ if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" message(STATUS "Skipping visibility test on PyPy or GraalPy") add_custom_target(test_cross_module_rtti )# Dummy target on PyPy or GraalPy. Embedding is not supported. - set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") return() endif() if(TARGET Python::Module AND NOT TARGET Python::Python) message(STATUS "Skipping visibility test since no embed libs found") add_custom_target(test_cross_module_rtti) # Dummy target since embedding is not supported. - set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") return() endif() -find_package(Catch 2.13.10) - -if(CATCH_FOUND) - message(STATUS "Building interpreter tests using Catch v${CATCH_VERSION}") -else() - message(STATUS "Catch not detected. Interpreter tests will be skipped. Install Catch headers" - " manually or use `cmake -DDOWNLOAD_CATCH=ON` to fetch them automatically.") - return() -endif() +message(STATUS "Building interpreter tests using Catch ${CATCH_VERSION}") include(GenerateExportHeader) diff --git a/tests/test_cross_module_rtti/catch.cpp b/tests/test_cross_module_rtti/catch.cpp index 2debc5ff17..f9c4522016 100644 --- a/tests/test_cross_module_rtti/catch.cpp +++ b/tests/test_cross_module_rtti/catch.cpp @@ -13,7 +13,7 @@ PYBIND11_WARNING_DISABLE_MSVC(4996) #endif #define CATCH_CONFIG_RUNNER -#include +#include int main(int argc, char *argv[]) { pybind11::scoped_interpreter guard{}; diff --git a/tests/test_cross_module_rtti/test_cross_module_rtti.cpp b/tests/test_cross_module_rtti/test_cross_module_rtti.cpp index 64988b77a1..1dd3ba541a 100644 --- a/tests/test_cross_module_rtti/test_cross_module_rtti.cpp +++ b/tests/test_cross_module_rtti/test_cross_module_rtti.cpp @@ -2,9 +2,10 @@ #include #include -#include #include +#include + static constexpr auto script = R"( import test_cross_module_rtti_bindings diff --git a/tests/test_with_catch/CMakeLists.txt b/tests/test_with_catch/CMakeLists.txt index e6a9f67aa7..5bbf3039e5 100644 --- a/tests/test_with_catch/CMakeLists.txt +++ b/tests/test_with_catch/CMakeLists.txt @@ -5,26 +5,16 @@ if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${PYTHON_MODULE_EXTENSION}" MATCHES "graalpy") message(STATUS "Skipping embed test on PyPy or GraalPy") add_custom_target(cpptest) # Dummy target on PyPy or GraalPy. Embedding is not supported. - set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") return() endif() if(TARGET Python::Module AND NOT TARGET Python::Python) message(STATUS "Skipping embed test since no embed libs found") add_custom_target(cpptest) # Dummy target since embedding is not supported. - set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") return() endif() -find_package(Catch 2.13.10) - -if(CATCH_FOUND) - message(STATUS "Building interpreter tests using Catch v${CATCH_VERSION}") -else() - message(STATUS "Catch not detected. Interpreter tests will be skipped. Install Catch headers" - " manually or use `cmake -DDOWNLOAD_CATCH=ON` to fetch them automatically.") - return() -endif() +message(STATUS "Building interpreter tests using Catch ${CATCH_VERSION}") find_package(Threads REQUIRED) diff --git a/tests/test_with_catch/catch.cpp b/tests/test_with_catch/catch.cpp index 8959593184..78cf2c51f0 100644 --- a/tests/test_with_catch/catch.cpp +++ b/tests/test_with_catch/catch.cpp @@ -27,7 +27,7 @@ PYBIND11_WARNING_DISABLE_MSVC(4996) #define CATCH_CONFIG_DEFAULT_REPORTER "progress" #include "catch_skip.h" -#include +#include namespace py = pybind11; diff --git a/tests/test_with_catch/catch_skip.h b/tests/test_with_catch/catch_skip.h index 9e2954d3b6..ab42f154fe 100644 --- a/tests/test_with_catch/catch_skip.h +++ b/tests/test_with_catch/catch_skip.h @@ -6,7 +6,7 @@ #include -#include +#include #define PYBIND11_CATCH2_SKIP_IF(condition, reason) \ do { \ diff --git a/tests/test_with_catch/test_args_convert_vector.cpp b/tests/test_with_catch/test_args_convert_vector.cpp index 7ce2d713fa..060d4d0f48 100644 --- a/tests/test_with_catch/test_args_convert_vector.cpp +++ b/tests/test_with_catch/test_args_convert_vector.cpp @@ -1,5 +1,6 @@ #include "pybind11/pybind11.h" -#include "catch.hpp" + +#include namespace py = pybind11; diff --git a/tests/test_with_catch/test_argument_vector.cpp b/tests/test_with_catch/test_argument_vector.cpp index 9cf302a9b1..c7f21f98b7 100644 --- a/tests/test_with_catch/test_argument_vector.cpp +++ b/tests/test_with_catch/test_argument_vector.cpp @@ -1,5 +1,6 @@ #include "pybind11/pybind11.h" -#include "catch.hpp" + +#include namespace py = pybind11; diff --git a/tests/test_with_catch/test_interpreter.cpp b/tests/test_with_catch/test_interpreter.cpp index 4103c0f5ff..524436c417 100644 --- a/tests/test_with_catch/test_interpreter.cpp +++ b/tests/test_with_catch/test_interpreter.cpp @@ -7,13 +7,14 @@ PYBIND11_WARNING_DISABLE_MSVC(4996) #include "catch_skip.h" -#include #include #include #include #include #include +#include + namespace py = pybind11; using namespace py::literals; diff --git a/tests/test_with_catch/test_subinterpreter.cpp b/tests/test_with_catch/test_subinterpreter.cpp index 3af100f2a9..d357bbaed1 100644 --- a/tests/test_with_catch/test_subinterpreter.cpp +++ b/tests/test_with_catch/test_subinterpreter.cpp @@ -9,13 +9,14 @@ PYBIND11_WARNING_DISABLE_MSVC(4996) # include "catch_skip.h" -# include # include # include # include # include # include +# include + namespace py = pybind11; using namespace py::literals; diff --git a/tools/FindCatch.cmake b/tools/FindCatch.cmake deleted file mode 100644 index 967434cb73..0000000000 --- a/tools/FindCatch.cmake +++ /dev/null @@ -1,74 +0,0 @@ -# - Find the Catch test framework or download it (single header) -# -# This is a quick module for internal use. It assumes that Catch is -# REQUIRED and that a minimum version is provided (not EXACT). If -# a suitable version isn't found locally, the single header file -# will be downloaded and placed in the build dir: PROJECT_BINARY_DIR. -# -# This code sets the following variables: -# CATCH_INCLUDE_DIR - path to catch.hpp -# CATCH_VERSION - version number - -if(NOT Catch_FIND_VERSION) - message(FATAL_ERROR "A version number must be specified.") -elseif(Catch_FIND_REQUIRED) - message(FATAL_ERROR "This module assumes Catch is not required.") -elseif(Catch_FIND_VERSION_EXACT) - message(FATAL_ERROR "Exact version numbers are not supported, only minimum.") -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) - 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) - endif() -endfunction() - -# Download the single-header version of Catch -function(_download_catch version destination_dir) - message(STATUS "Downloading catch v${version}...") - set(url https://github.com/philsquared/Catch/releases/download/v${version}/catch.hpp) - file( - DOWNLOAD ${url} "${destination_dir}/catch.hpp" - STATUS status - LOG log) - list(GET status 0 error) - if(error) - string(REPLACE "\n" "\n " log " ${log}") - message(FATAL_ERROR "Could not download URL:\n" " ${url}\n" "Log:\n" "${log}") - endif() - set(CATCH_INCLUDE_DIR - "${destination_dir}" - CACHE INTERNAL "") -endfunction() - -# Look for catch locally -find_path( - CATCH_INCLUDE_DIR - NAMES catch.hpp - PATH_SUFFIXES catch2) -if(CATCH_INCLUDE_DIR) - _get_catch_version() -endif() - -# Download the header if it wasn't found or if it's outdated -if(NOT CATCH_VERSION OR CATCH_VERSION VERSION_LESS ${Catch_FIND_VERSION}) - if(DOWNLOAD_CATCH) - _download_catch(${Catch_FIND_VERSION} "${PROJECT_BINARY_DIR}/catch/") - _get_catch_version() - else() - set(CATCH_FOUND FALSE) - return() - endif() -endif() - -add_library(Catch2::Catch2 IMPORTED INTERFACE) -set_property(TARGET Catch2::Catch2 PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CATCH_INCLUDE_DIR}") - -set(CATCH_FOUND TRUE)