Skip to content
Snippets Groups Projects
TestSetup.cmake 1.25 KiB
Newer Older
#TODO: make this macro take an argument
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)")
    if(NOT TestInterface_EXISTS)
        add_library(TestInterface INTERFACE)
        target_link_libraries(TestInterface INTERFACE Catch2::Catch2WithMain)
        if(NOT ${Catch2_VERSION} VERSION_GREATER_EQUAL "3")
            target_compile_definitions(TestInterface INTERFACE TESTS_CATCH2_USE_OLD_HEADER=1)


        # Link against additional arguments given to the macro
        set(test_libraries ${ARGN})
        list(LENGTH test_libraries lib_count)
        if (${lib_count} GREATER 0)
          target_link_libraries(TestInterface INTERFACE ${test_libraries})
        endif()