macro(find_catch2_and_define_test_target)
    if (TARGET TestInterface)
        set(TestInterface_EXISTS 1)
    endif()

    find_package(Catch2 REQUIRED)

    if(Catch2_FOUND)
        message(STATUS "Test setup: found Catch2, version: ${Catch2_VERSION}")
        if(NOT TARGET Catch2::Catch2WithMain)
            message(SEND_ERROR
                "Tests depend on Catch2::Catch2WithMain, but Catch2 was built without "
                "it. To fix this, build Catch2 with CATCH_BUILD_STATIC_LIBRARY=ON\n"
                "(see: https://github.com/catchorg/Catch2/releases/tag/v2.13.5)")
        endif()
    endif()

    if(NOT TestInterface_EXISTS)
        add_library(TestInterface INTERFACE)
        target_link_libraries(TestInterface INTERFACE ${PROJECT_NAME} Catch2::Catch2WithMain)
        if(NOT ${Catch2_VERSION} VERSION_GREATER_EQUAL "3")
            target_compile_definitions(TestInterface INTERFACE TESTS_CATCH2_USE_OLD_HEADER=1)
        endif()
    endif()
endmacro()