Testing a lib with various build configurations

ach
Sun, 03 Dec 2023 14:53:58 +0000
Usage

Hello,

I’m currently trying to set-up a small embedded-C project using CMake. Part of the code is conditionally built using preprocessor guards. The CMake project uses an option() to enable/disable this. Using generator expressions, the define is passed from cmake to the compiler. This basically looks like this:

option(OPTION_A "Option A description")
add_library(my_target STATIC my_target.c)
target_compile_definitions(my_target PUBLIC $<$<BOOL:${OPTION_A}>:OPTION_A>)

Now I’m trying to use ctest to run various tests. Of-course I would like to test my code both when the option is enabled or disabled. For the moment I use add_test, but I can only test one version of the code at once.

add_executable(test_my_target test_my_target.c)
target_link_libraries(test_my_target PRIVATE my_target)
add_test(NAME test_my_target COMMAND test_my_target)

I would like to be able to define two tests i(one exploiting the option, the other not), that I could run at once. By that I mean that I don’t want to contantly reconfigure the project and rebuild then re-run the tests (I agree that at some point I’ll need to,build the lib and it’s dependency twice).

I started having a look at CTest “Build and Test Mode”, but for now I wasn’t able to achieve what I wanted. If that is a good direction to take, I’ll re-read the doc and persevere.

My questions are the following:

Is what I want to achieve a good idea ?

Is this achievable in a simple manner ?

Is CTest “Build and Test Mode” a way to achieve this ?

8 posts - 3 participants