From 5c2182d11eae9513e694fc48790a0d5228f640f8 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 1 Oct 2023 14:32:30 +0800 Subject: [PATCH 1/4] Add add_test wrapper module --- CMakeLists.txt | 29 ++- cmake/AddTest.cmake | 292 +++++++++++++++++++++++++++ my_exe/test/CMakeLists.txt | 30 +-- my_header_lib/test/CMakeLists.txt | 24 +-- my_lib/test/CMakeLists.txt | 23 +-- my_lib/test/constexpr/CMakeLists.txt | 37 ++-- 6 files changed, 337 insertions(+), 98 deletions(-) create mode 100644 cmake/AddTest.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 55c4526..cb924fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,13 +51,28 @@ if(FEATURE_TESTS) ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY) - # generate a main function for the test executable - enable_testing() - add_library(catch2_test_common INTERFACE) - target_find_dependencies(catch2_test_common INTERFACE_CONFIG Catch2) - target_link_libraries(catch2_test_common INTERFACE Catch2::Catch2) - target_compile_definitions(catch2_test_common INTERFACE CATCH_CONFIG_MAIN DO_NOT_USE_WMAIN) - include(Catch) + include(CTest) + # common settings for library test + include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/AddTest.cmake) + if(${ENABLE_COVERAGE}) + set(COVERAGE_ARGS --reporter xml) + endif() + add_test_config(common + DEPENDENCIES_CONFIG + Catch2 + + LIBRARIES + my_project_options + my_project_warnings + Catch2::Catch2 + + COMPILE_DEFINITIONS + CATCH_CONFIG_MAIN + DO_NOT_USE_WMAIN + + EXECUTE_ARGS + ${COVERAGE_ARGS} + ) endif() if(FEATURE_DOCS) diff --git a/cmake/AddTest.cmake b/cmake/AddTest.cmake new file mode 100644 index 0000000..8868a42 --- /dev/null +++ b/cmake/AddTest.cmake @@ -0,0 +1,292 @@ +# - Handy functions to create tests +# This module provides handy add_test wrappers and configs. +# +# These conifg functions will generate a test config target that can be used by linking it. +# +# add_test_config( # target will be named as `test_config.${config_name}` +# [SOURCES ] +# [INCLUDES ] +# [SYSTEM_INCLUDES ] +# [DEPENDENCIES_CONFIG ] +# [DEPENDENCIES ] +# [LIBRARIES ] +# [SYSTEM_LIBRARIES ] +# [COMPILE_DEFINITIONS ] +# [COMPILE_OPTIONS ] +# [COMPILE_FEATURES ] +# [EXECUTE_ARGS ] +# ) +# +# These add_test wrappers will generate a test target and register it in CTest. +# +# add_library_test( +# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` +# [SOURCES ] +# [INCLUDES ] +# [SYSTEM_INCLUDES ] +# [DEPENDENCIES_CONFIG ] +# [DEPENDENCIES ] +# [LIBRARIES ] +# [SYSTEM_LIBRARIES ] +# [COMPILE_DEFINITIONS ] +# [COMPILE_OPTIONS ] +# [COMPILE_FEATURES ] +# [EXECUTE_ARGS ] +# ) +# +# add_executable_test( +# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` +# [EXECUTE_ARGS ] +# ) + +include_guard() + +function(_Configure_target target_name type) + set(options) + set(one_value_args) + set(multi_value_args + SOURCES + INCLUDES + SYSTEM_INCLUDES + DEPENDENCIES_CONFIG + DEPENDENCIES + LIBRARIES + SYSTEM_LIBRARIES + COMPILE_DEFINITIONS + COMPILE_OPTIONS + COMPILE_FEATURES + ) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + if(${type} STREQUAL "library_test") + add_executable(${target_name}) + set(scope PRIVATE) + elseif(${type} STREQUAL "test_config") + add_library(${target_name} INTERFACE) + set(scope INTERFACE) + endif() + + target_sources(${target_name} + ${scope} + ${args_SOURCES} + ) + target_include_directories(${target_name} + ${scope} + ${args_INCLUDES} + ) + target_link_libraries(${target_name} + ${scope} + ${args_LIBRARIES} + ) + target_include_system_directories(${target_name} + ${scope} + ${args_SYSTEM_INCLUDES} + ) + target_find_dependencies(${target_name} + "${scope}_CONFIG" + ${args_DEPENDENCIES_CONFIG} + + ${scope} + ${args_DEPENDENCIES} + ) + target_link_system_libraries(${target_name} + ${scope} + ${args_SYSTEM_LIBRARIES} + ) + target_compile_definitions(${target_name} + ${scope} + ${args_COMPILE_DEFINITIONS} + ) + target_compile_options(${target_name} + ${scope} + ${args_COMPILE_OPTIONS} + ) + target_compile_features(${target_name} + ${scope} + ${args_COMPILE_FEATURES} + ) +endfunction() + +function(_Set_config_execute_args target_name execute_args) + set_property(TARGET ${target_name} PROPERTY PROJECT_OPTIONS_EXECUTE_ARGS ${execute_args}) +endfunction() + +# add_test_config( # target will be named as `test_config.${config_name}` +# [SOURCES ] +# [INCLUDES ] +# [SYSTEM_INCLUDES ] +# [DEPENDENCIES_CONFIG ] +# [DEPENDENCIES ] +# [LIBRARIES ] +# [SYSTEM_LIBRARIES ] +# [COMPILE_DEFINITIONS ] +# [COMPILE_OPTIONS ] +# [COMPILE_FEATURES ] +# [EXECUTE_ARGS ] +# ) +# +# Add a library test config called test_config.${config_name} +function(add_test_config config_name) + set(options) + set(one_value_args) + set(multi_value_args + EXECUTE_ARGS + + SOURCES + INCLUDES + SYSTEM_INCLUDES + DEPENDENCIES_CONFIG + DEPENDENCIES + LIBRARIES + SYSTEM_LIBRARIES + COMPILE_DEFINITIONS + COMPILE_OPTIONS + COMPILE_FEATURES + ) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + set(target_name "test_config.${config_name}") + _Configure_target(${target_name} test_config + SOURCES + ${args_SOURCES} + INCLUDES + ${args_INCLUDES} + SYSTEM_INCLUDES + ${args_SYSTEM_INCLUDES} + DEPENDENCIES_CONFIG + ${args_DEPENDENCIES_CONFIG} + DEPENDENCIES + ${args_DEPENDENCIES} + LIBRARIES + ${args_LIBRARIES} + SYSTEM_LIBRARIES + ${args_SYSTEM_LIBRARIES} + COMPILE_DEFINITIONS + ${args_COMPILE_DEFINITIONS} + COMPILE_OPTIONS + ${args_COMPILE_OPTIONS} + COMPILE_FEATURES + ${args_COMPILE_FEATURES} + ) + + _Set_config_execute_args(${target_name} "${args_EXECUTE_ARGS}") +endfunction() + +function(_Get_configs_execute_args variable_name) + set(value) + foreach(config IN LISTS ARGN) + get_target_property(execute_args ${config} PROJECT_OPTIONS_EXECUTE_ARGS) + list(APPEND variable_name ${execute_args}) + endforeach() + set(${variable_name} ${value} PARENT_SCOPE) +endfunction() + +function(_Add_configs_prefix variable_name) + set(value) + foreach(config IN LISTS ARGN) + if (${config} MATCHES "test_config\..*") + list(APPEND value "${config}") + else() + list(APPEND value "test_config.${config}") + endif() + endforeach() + set(${variable_name} ${value} PARENT_SCOPE) +endfunction() + +# add_library_test( +# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` +# [SOURCES ] +# [INCLUDES ] +# [SYSTEM_INCLUDES ] +# [DEPENDENCIES_CONFIG ] +# [DEPENDENCIES ] +# [LIBRARIES ] +# [SYSTEM_LIBRARIES ] +# [COMPILE_DEFINITIONS ] +# [COMPILE_OPTIONS ] +# [COMPILE_FEATURES ] +# [EXECUTE_ARGS ] +# ) +# +# Add a library test called test.${library}.${test_name} +function(add_library_test library test_name) + set(options) + set(one_value_args) + set(multi_value_args + CONFIGS + EXECUTE_ARGS + + SOURCES + INCLUDES + SYSTEM_INCLUDES + DEPENDENCIES_CONFIG + DEPENDENCIES + LIBRARIES + SYSTEM_LIBRARIES + COMPILE_DEFINITIONS + COMPILE_OPTIONS + COMPILE_FEATURES + ) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + _Add_configs_prefix(prefixed_configs ${args_CONFIGS}) + + set(target_name "test.${library}.${test_name}") + _Configure_target(${target_name} library_test + SOURCES + ${args_SOURCES} + INCLUDES + ${args_INCLUDES} + SYSTEM_INCLUDES + ${args_SYSTEM_INCLUDES} + DEPENDENCIES_CONFIG + ${args_DEPENDENCIES_CONFIG} + DEPENDENCIES + ${args_DEPENDENCIES} + LIBRARIES + ${args_LIBRARIES} + SYSTEM_LIBRARIES + ${args_SYSTEM_LIBRARIES} + COMPILE_DEFINITIONS + ${args_COMPILE_DEFINITIONS} + COMPILE_OPTIONS + ${args_COMPILE_OPTIONS} + COMPILE_FEATURES + ${args_COMPILE_FEATURES} + ) + + target_link_libraries(${target_name} + PRIVATE + ${prefixed_configs} + ${library} + ) + + _Get_configs_execute_args(configs_execute_args ${prefixed_configs}) + add_test( + NAME ${target_name} + COMMAND ${target_name} ${configs_execute_args} ${args_EXECUTE_ARGS} + ) +endfunction() + +# add_executable_test( +# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` +# [EXECUTE_ARGS ] +# ) +# +# Add an executable test called test.${executable}.${test_name} +function(add_executable_test executable test_name) + set(options) + set(one_value_args) + set(multi_value_args CONFIGS EXECUTE_ARGS) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + _Add_configs_prefix(prefixed_configs ${args_CONFIGS}) + + set(target_name "test.${executable}.${test_name}") + + _Get_configs_execute_args(configs_execute_args ${prefixed_configs}) + add_test( + NAME ${target_name} + COMMAND ${executable} ${configs_execute_args} ${args_EXECUTE_ARGS} + ) +endfunction() \ No newline at end of file diff --git a/my_exe/test/CMakeLists.txt b/my_exe/test/CMakeLists.txt index e12a641..ca3e9f3 100644 --- a/my_exe/test/CMakeLists.txt +++ b/my_exe/test/CMakeLists.txt @@ -1,28 +1,2 @@ -# test dependencies -include(CTest) - -# ---------------------------------------------------------------------------------------------------------------------- -# test the executable part -add_test( - # calling my_exe executable directly - NAME my_exe_test - COMMAND my_exe "" - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - -# ---------------------------------------------------------------------------------------------------------------------- -# test the library part -add_executable(my_exe_lib_tests "./tests.cpp") -target_link_libraries( - my_exe_lib_tests - PRIVATE my_project_warnings - my_project_options - catch2_test_common - my_exe_lib) - -# use xml reporter if coverage is enabled -if(${ENABLE_COVERAGE}) - set(COVERAGE_ARGS REPORTER xml) -endif() - -# automatically discover tests that are defined in catch based test files you can modify the tests -catch_discover_tests(my_exe_lib_tests ${COVERAGE_ARGS}) +add_library_test(my_exe_lib tests CONFIGS common SOURCES tests.cpp) +add_executable_test(my_exe no_arg) \ No newline at end of file diff --git a/my_header_lib/test/CMakeLists.txt b/my_header_lib/test/CMakeLists.txt index fb008c6..cb8efb2 100644 --- a/my_header_lib/test/CMakeLists.txt +++ b/my_header_lib/test/CMakeLists.txt @@ -1,23 +1 @@ -# test dependencies -include(CTest) - -# test executable -add_executable(my_header_lib_tests "./tests.cpp") - -target_link_libraries( - my_header_lib_tests - PRIVATE my_header_lib - my_project_warnings - my_project_options - catch2_test_common) - -# use xml reporter if coverage is enabled -if(${ENABLE_COVERAGE}) - set(COVERAGE_ARGS REPORTER xml) -endif() - -# automatically discover tests that are defined in catch based test files you can modify the tests -catch_discover_tests(my_header_lib_tests ${COVERAGE_ARGS}) - -# constexpr tests -add_subdirectory("./constexpr") +add_library_test(my_header_lib tests CONFIGS common SOURCES tests.cpp) \ No newline at end of file diff --git a/my_lib/test/CMakeLists.txt b/my_lib/test/CMakeLists.txt index f13bc54..8c377d9 100644 --- a/my_lib/test/CMakeLists.txt +++ b/my_lib/test/CMakeLists.txt @@ -1,23 +1,4 @@ -# test dependencies -include(CTest) - -# test executable -add_executable(my_lib_tests "./tests.cpp") - -target_link_libraries( - my_lib_tests - PRIVATE my_lib - my_project_warnings - my_project_options - catch2_test_common) - -# use xml reporter if coverage is enabled -if(${ENABLE_COVERAGE}) - set(COVERAGE_ARGS REPORTER xml) -endif() - -# automatically discover tests that are defined in catch based test files you can modify the tests -catch_discover_tests(my_lib_tests ${COVERAGE_ARGS}) +add_library_test(my_lib tests CONFIGS common SOURCES tests.cpp) # constexpr tests -add_subdirectory("./constexpr") +add_subdirectory("./constexpr") \ No newline at end of file diff --git a/my_lib/test/constexpr/CMakeLists.txt b/my_lib/test/constexpr/CMakeLists.txt index 541ad9f..f756a0f 100644 --- a/my_lib/test/constexpr/CMakeLists.txt +++ b/my_lib/test/constexpr/CMakeLists.txt @@ -1,25 +1,24 @@ # Add a file containing a set of constexpr tests -add_executable(my_lib_constexpr_tests "./constexpr_tests.cpp") - -target_link_libraries( - my_lib_constexpr_tests - PRIVATE my_lib - my_project_warnings - my_project_options - catch2_test_common) - -catch_discover_tests(my_lib_constexpr_tests ${COVERAGE_ARGS}) +add_library_test(my_lib constexpr_tests CONFIGS common SOURCES constexpr_tests.cpp) # Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when # things go wrong with the constexpr testing -add_executable(my_lib_relaxed_constexpr_tests "./constexpr_tests.cpp") +add_library_test(my_lib relaxed_constexpr_tests + CONFIGS + common + + SOURCES + constexpr_tests.cpp -target_link_libraries( - my_lib_relaxed_constexpr_tests - PRIVATE my_lib - my_project_warnings - my_project_options - catch2_test_common) -target_compile_definitions(my_lib_relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE) + COMPILE_DEFINITIONS + -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE +) -catch_discover_tests(my_lib_relaxed_constexpr_tests ${COVERAGE_ARGS}) +# Or for reuse: +# +# add_test_config(relaxed_constexpr +# COMPILE_DEFINITIONS +# -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE +# ) +# +# add_library_test(my_lib constexpr_tests CONFIGS common relaxed_constexpr SOURCES constexpr_tests.cpp) \ No newline at end of file From 01937db1c0a97d07972838639dd669b43fc200a3 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 1 Oct 2023 14:47:12 +0800 Subject: [PATCH 2/4] typo: constexpr_tests -> relaxed_constexpr_tests --- my_lib/test/constexpr/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/my_lib/test/constexpr/CMakeLists.txt b/my_lib/test/constexpr/CMakeLists.txt index f756a0f..d94b700 100644 --- a/my_lib/test/constexpr/CMakeLists.txt +++ b/my_lib/test/constexpr/CMakeLists.txt @@ -21,4 +21,4 @@ add_library_test(my_lib relaxed_constexpr_tests # -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE # ) # -# add_library_test(my_lib constexpr_tests CONFIGS common relaxed_constexpr SOURCES constexpr_tests.cpp) \ No newline at end of file +# add_library_test(my_lib relaxed_constexpr_tests CONFIGS common relaxed_constexpr SOURCES constexpr_tests.cpp) \ No newline at end of file From 69d4e5ee6f151f3f038eb21c6c69dbb8795d1454 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Sun, 1 Oct 2023 14:51:28 +0800 Subject: [PATCH 3/4] Insert newline at eof --- my_exe/test/CMakeLists.txt | 2 +- my_header_lib/test/CMakeLists.txt | 2 +- my_lib/test/CMakeLists.txt | 2 +- my_lib/test/constexpr/CMakeLists.txt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/my_exe/test/CMakeLists.txt b/my_exe/test/CMakeLists.txt index ca3e9f3..de9a87c 100644 --- a/my_exe/test/CMakeLists.txt +++ b/my_exe/test/CMakeLists.txt @@ -1,2 +1,2 @@ add_library_test(my_exe_lib tests CONFIGS common SOURCES tests.cpp) -add_executable_test(my_exe no_arg) \ No newline at end of file +add_executable_test(my_exe no_arg) diff --git a/my_header_lib/test/CMakeLists.txt b/my_header_lib/test/CMakeLists.txt index cb8efb2..7cf10f2 100644 --- a/my_header_lib/test/CMakeLists.txt +++ b/my_header_lib/test/CMakeLists.txt @@ -1 +1 @@ -add_library_test(my_header_lib tests CONFIGS common SOURCES tests.cpp) \ No newline at end of file +add_library_test(my_header_lib tests CONFIGS common SOURCES tests.cpp) diff --git a/my_lib/test/CMakeLists.txt b/my_lib/test/CMakeLists.txt index 8c377d9..47db2a0 100644 --- a/my_lib/test/CMakeLists.txt +++ b/my_lib/test/CMakeLists.txt @@ -1,4 +1,4 @@ add_library_test(my_lib tests CONFIGS common SOURCES tests.cpp) # constexpr tests -add_subdirectory("./constexpr") \ No newline at end of file +add_subdirectory("./constexpr") diff --git a/my_lib/test/constexpr/CMakeLists.txt b/my_lib/test/constexpr/CMakeLists.txt index d94b700..050b01b 100644 --- a/my_lib/test/constexpr/CMakeLists.txt +++ b/my_lib/test/constexpr/CMakeLists.txt @@ -21,4 +21,4 @@ add_library_test(my_lib relaxed_constexpr_tests # -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE # ) # -# add_library_test(my_lib relaxed_constexpr_tests CONFIGS common relaxed_constexpr SOURCES constexpr_tests.cpp) \ No newline at end of file +# add_library_test(my_lib relaxed_constexpr_tests CONFIGS common relaxed_constexpr SOURCES constexpr_tests.cpp) From 1332870671946cfdd3e4c0e5b0882d8a6058ad71 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 11:14:15 +0800 Subject: [PATCH 4/4] Remove AddTest.cmake handled by project_options --- CMakeLists.txt | 1 - cmake/AddTest.cmake | 292 -------------------------------------------- 2 files changed, 293 deletions(-) delete mode 100644 cmake/AddTest.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index cb924fd..6f42770 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,6 @@ if(FEATURE_TESTS) include(CTest) # common settings for library test - include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/AddTest.cmake) if(${ENABLE_COVERAGE}) set(COVERAGE_ARGS --reporter xml) endif() diff --git a/cmake/AddTest.cmake b/cmake/AddTest.cmake deleted file mode 100644 index 8868a42..0000000 --- a/cmake/AddTest.cmake +++ /dev/null @@ -1,292 +0,0 @@ -# - Handy functions to create tests -# This module provides handy add_test wrappers and configs. -# -# These conifg functions will generate a test config target that can be used by linking it. -# -# add_test_config( # target will be named as `test_config.${config_name}` -# [SOURCES ] -# [INCLUDES ] -# [SYSTEM_INCLUDES ] -# [DEPENDENCIES_CONFIG ] -# [DEPENDENCIES ] -# [LIBRARIES ] -# [SYSTEM_LIBRARIES ] -# [COMPILE_DEFINITIONS ] -# [COMPILE_OPTIONS ] -# [COMPILE_FEATURES ] -# [EXECUTE_ARGS ] -# ) -# -# These add_test wrappers will generate a test target and register it in CTest. -# -# add_library_test( -# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` -# [SOURCES ] -# [INCLUDES ] -# [SYSTEM_INCLUDES ] -# [DEPENDENCIES_CONFIG ] -# [DEPENDENCIES ] -# [LIBRARIES ] -# [SYSTEM_LIBRARIES ] -# [COMPILE_DEFINITIONS ] -# [COMPILE_OPTIONS ] -# [COMPILE_FEATURES ] -# [EXECUTE_ARGS ] -# ) -# -# add_executable_test( -# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` -# [EXECUTE_ARGS ] -# ) - -include_guard() - -function(_Configure_target target_name type) - set(options) - set(one_value_args) - set(multi_value_args - SOURCES - INCLUDES - SYSTEM_INCLUDES - DEPENDENCIES_CONFIG - DEPENDENCIES - LIBRARIES - SYSTEM_LIBRARIES - COMPILE_DEFINITIONS - COMPILE_OPTIONS - COMPILE_FEATURES - ) - cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) - - if(${type} STREQUAL "library_test") - add_executable(${target_name}) - set(scope PRIVATE) - elseif(${type} STREQUAL "test_config") - add_library(${target_name} INTERFACE) - set(scope INTERFACE) - endif() - - target_sources(${target_name} - ${scope} - ${args_SOURCES} - ) - target_include_directories(${target_name} - ${scope} - ${args_INCLUDES} - ) - target_link_libraries(${target_name} - ${scope} - ${args_LIBRARIES} - ) - target_include_system_directories(${target_name} - ${scope} - ${args_SYSTEM_INCLUDES} - ) - target_find_dependencies(${target_name} - "${scope}_CONFIG" - ${args_DEPENDENCIES_CONFIG} - - ${scope} - ${args_DEPENDENCIES} - ) - target_link_system_libraries(${target_name} - ${scope} - ${args_SYSTEM_LIBRARIES} - ) - target_compile_definitions(${target_name} - ${scope} - ${args_COMPILE_DEFINITIONS} - ) - target_compile_options(${target_name} - ${scope} - ${args_COMPILE_OPTIONS} - ) - target_compile_features(${target_name} - ${scope} - ${args_COMPILE_FEATURES} - ) -endfunction() - -function(_Set_config_execute_args target_name execute_args) - set_property(TARGET ${target_name} PROPERTY PROJECT_OPTIONS_EXECUTE_ARGS ${execute_args}) -endfunction() - -# add_test_config( # target will be named as `test_config.${config_name}` -# [SOURCES ] -# [INCLUDES ] -# [SYSTEM_INCLUDES ] -# [DEPENDENCIES_CONFIG ] -# [DEPENDENCIES ] -# [LIBRARIES ] -# [SYSTEM_LIBRARIES ] -# [COMPILE_DEFINITIONS ] -# [COMPILE_OPTIONS ] -# [COMPILE_FEATURES ] -# [EXECUTE_ARGS ] -# ) -# -# Add a library test config called test_config.${config_name} -function(add_test_config config_name) - set(options) - set(one_value_args) - set(multi_value_args - EXECUTE_ARGS - - SOURCES - INCLUDES - SYSTEM_INCLUDES - DEPENDENCIES_CONFIG - DEPENDENCIES - LIBRARIES - SYSTEM_LIBRARIES - COMPILE_DEFINITIONS - COMPILE_OPTIONS - COMPILE_FEATURES - ) - cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) - - set(target_name "test_config.${config_name}") - _Configure_target(${target_name} test_config - SOURCES - ${args_SOURCES} - INCLUDES - ${args_INCLUDES} - SYSTEM_INCLUDES - ${args_SYSTEM_INCLUDES} - DEPENDENCIES_CONFIG - ${args_DEPENDENCIES_CONFIG} - DEPENDENCIES - ${args_DEPENDENCIES} - LIBRARIES - ${args_LIBRARIES} - SYSTEM_LIBRARIES - ${args_SYSTEM_LIBRARIES} - COMPILE_DEFINITIONS - ${args_COMPILE_DEFINITIONS} - COMPILE_OPTIONS - ${args_COMPILE_OPTIONS} - COMPILE_FEATURES - ${args_COMPILE_FEATURES} - ) - - _Set_config_execute_args(${target_name} "${args_EXECUTE_ARGS}") -endfunction() - -function(_Get_configs_execute_args variable_name) - set(value) - foreach(config IN LISTS ARGN) - get_target_property(execute_args ${config} PROJECT_OPTIONS_EXECUTE_ARGS) - list(APPEND variable_name ${execute_args}) - endforeach() - set(${variable_name} ${value} PARENT_SCOPE) -endfunction() - -function(_Add_configs_prefix variable_name) - set(value) - foreach(config IN LISTS ARGN) - if (${config} MATCHES "test_config\..*") - list(APPEND value "${config}") - else() - list(APPEND value "test_config.${config}") - endif() - endforeach() - set(${variable_name} ${value} PARENT_SCOPE) -endfunction() - -# add_library_test( -# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` -# [SOURCES ] -# [INCLUDES ] -# [SYSTEM_INCLUDES ] -# [DEPENDENCIES_CONFIG ] -# [DEPENDENCIES ] -# [LIBRARIES ] -# [SYSTEM_LIBRARIES ] -# [COMPILE_DEFINITIONS ] -# [COMPILE_OPTIONS ] -# [COMPILE_FEATURES ] -# [EXECUTE_ARGS ] -# ) -# -# Add a library test called test.${library}.${test_name} -function(add_library_test library test_name) - set(options) - set(one_value_args) - set(multi_value_args - CONFIGS - EXECUTE_ARGS - - SOURCES - INCLUDES - SYSTEM_INCLUDES - DEPENDENCIES_CONFIG - DEPENDENCIES - LIBRARIES - SYSTEM_LIBRARIES - COMPILE_DEFINITIONS - COMPILE_OPTIONS - COMPILE_FEATURES - ) - cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) - - _Add_configs_prefix(prefixed_configs ${args_CONFIGS}) - - set(target_name "test.${library}.${test_name}") - _Configure_target(${target_name} library_test - SOURCES - ${args_SOURCES} - INCLUDES - ${args_INCLUDES} - SYSTEM_INCLUDES - ${args_SYSTEM_INCLUDES} - DEPENDENCIES_CONFIG - ${args_DEPENDENCIES_CONFIG} - DEPENDENCIES - ${args_DEPENDENCIES} - LIBRARIES - ${args_LIBRARIES} - SYSTEM_LIBRARIES - ${args_SYSTEM_LIBRARIES} - COMPILE_DEFINITIONS - ${args_COMPILE_DEFINITIONS} - COMPILE_OPTIONS - ${args_COMPILE_OPTIONS} - COMPILE_FEATURES - ${args_COMPILE_FEATURES} - ) - - target_link_libraries(${target_name} - PRIVATE - ${prefixed_configs} - ${library} - ) - - _Get_configs_execute_args(configs_execute_args ${prefixed_configs}) - add_test( - NAME ${target_name} - COMMAND ${target_name} ${configs_execute_args} ${args_EXECUTE_ARGS} - ) -endfunction() - -# add_executable_test( -# [CONFIGS ] # accepts both `${config_name}` and `test_config.${config_name}` -# [EXECUTE_ARGS ] -# ) -# -# Add an executable test called test.${executable}.${test_name} -function(add_executable_test executable test_name) - set(options) - set(one_value_args) - set(multi_value_args CONFIGS EXECUTE_ARGS) - cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) - - _Add_configs_prefix(prefixed_configs ${args_CONFIGS}) - - set(target_name "test.${executable}.${test_name}") - - _Get_configs_execute_args(configs_execute_args ${prefixed_configs}) - add_test( - NAME ${target_name} - COMMAND ${executable} ${configs_execute_args} ${args_EXECUTE_ARGS} - ) -endfunction() \ No newline at end of file