One source to create multiple objects

Hello,

before I start with my question I’ll explain the background. In my C++ project I’ve one source file which is very heavy to compile (boost.spirit.x3) for a linker library of a project. Fortunately I can setup different compile units for low memory situations. Hence my CmakeLists.txt looks like:

project(testsuite_rules LANGUAGES CXX)
...
add_library(${PROJECT_NAME} "")
add_library(testsuite::rules ALIAS ${PROJECT_NAME})
...
if(localhost_RAM_MB GREATER 12000)
    set(SOURCE_LIST src/rules.cpp)
else()
    set(SOURCE_LIST 
        src/rules_api.cpp
        src/rules_a.cpp         src/rules_b.cpp         src/rules_c.cpp         src/rules_d.cpp 
        src/rules_e.cpp         src/rules_f.cpp         src/rules_g.cpp        src/rules_i.cpp 
        src/rules_l.cpp         src/rules_n.cpp         src/rules_p.cpp         src/rules_q.cpp 
        src/rules_r.cpp         src/rules_s.cpp         src/rules_t.cpp         src/rules_u.cpp 
        src/rules_v.cpp         src/rules_w.cpp 
    )
endif()
...
target_sources(${PROJECT_NAME} PRIVATE
    ${SOURCE_LIST}
)
...

The problem rise, that I have on changes different places to edit. So the wish rises to have only the
rules.cpp which can be compiled with different defines (e.g. COMPILE_RULE{A…Z}) into corresponding object files (rules_{a…z}.o) to be linked.

The first question is how to write this for CMake? What is the best/simplest solution for my source file, since it must be compiled as a whole without defines or even step-wise with defines.

Thanks

Maybe UNITY_GROUP is of interest? You can then tweak the group sized based on the memory size by setting the property on the appropriate rule source files. This would mean having separate sources still, but you wouldn’t also need a combined source file.

Other than that, I don’t think there’s a way to do this. I’d do it via:

#define COMPILE_RULE_A
#define COMPILE_RULE_B
#include "rules.cpp"

and then generate the files from CMake. But I think the unity build setup is likely to be way easier.