Unity Build: Per-config sources broken in multi-config generators
Consider the CMakeLists.txt
:
cmake_minimum_required(VERSION 3.21)
project(UnityPerConfig C)
add_executable(main main.c
"$<$<CONFIG:Debug>:debug.c>"
"$<$<NOT:$<CONFIG:Debug>>:other.c>"
)
set_property(TARGET main PROPERTY UNITY_BUILD ON)
target_compile_definitions(main PRIVATE
"$<$<CONFIG:Debug>:CFG_DEBUG>"
"$<$<NOT:$<CONFIG:Debug>>:CFG_OTHER>"
)
with sources main.c, debug.c, and other.c as follows:
main.c
:
#ifdef CFG_DEBUG
extern void debug(void);
#endif
#ifdef CFG_OTHER
extern void other(void);
#endif
int main(void)
{
#ifdef CFG_DEBUG
debug();
#endif
#ifdef CFG_OTHER
other();
#endif
return 0;
}
debug.c
:
#if !defined(CFG_DEBUG) || defined(CFG_OTHER)
# error "debug source in other config"
#endif
void debug(void)
{
}
other.c
:
#if !defined(CFG_OTHER) || defined(CFG_DEBUG)
# error "other source in debug config"
#endif
void other(void)
{
}
Using the Ninja Multi-Config
generator fails to build the Debug
config:
$ cmake .. -G "Ninja Multi-Config"
$ ninja -f build-Debug.ninja
...
2 | # error "other source in debug config"
...
The problem is that the generated CMakeFiles/main.dir/Unity/unity_0_c.c
unity source includes other.c
even in the Debug
configuration.
Additionally, debug.c
is not included by the unity source.
Edited by Brad King