icinga2/cmake/DiscoverBoostTests.cmake
Johannes Schmidt b4681b10ec Discover Boost test cases automatically after build
This adds a global fixture that can parse an additional argument to
the test executables (`--generate_ctest_config`). When run by
CMake during build, this generates a CTest script containing all
the tests and their properties.

An additional decorator, that defines CTest properties for a test case
or suite that will be added to the tests during config generation.

This version needs no hacks, no huge CMake scripts, just a bit of
additional C++ code that iterates over all test-cases and collects
the information CTest needs.

One caveat is still that this does not work with cross-compilation,
which probably isn't an issue to begin with, but there are also ways
to fix that if necessary.
2025-09-10 13:48:24 +02:00

22 lines
904 B
CMake

# Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+
# - Discover tests defined in the Boost.Test executable target
#
# Boost.Test executables should be defined like any other CMake executable target.
# Then, this function can be used on the executable target to discover all the unit
# tests in that executable.
# This relies on the additional commandline argument added in 'test/test-ctest.hpp'
function(target_discover_boost_tests target)
set(testfile "${CMAKE_CURRENT_BINARY_DIR}/${target}_tests.cmake")
set(args -- --generate_ctest_config "${testfile}")
string(REPLACE ";" "$<SEMICOLON>" test "${args}")
add_custom_command(TARGET "${target}" POST_BUILD
COMMAND ${CMAKE_COMMAND} -DCMD=$<TARGET_FILE:${target}> -DARGS="${test}" -P "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/ExecuteCommandQuietly.cmake"
)
set_property(DIRECTORY
APPEND PROPERTY TEST_INCLUDE_FILES "${testfile}"
)
endfunction()