Nikolaus Ammann
Nikolaus Ammann

Reputation: 667

Automatically build binaries for all combinations of preprocessor defines

I have a project with multiple features, which can be enabled by preprocessor defines like #define ENABLE_FEATURE_A. To test the project I use GoogleTest. Therefore I define which features should be enabled, build the GoogleTest binary and run it.

To test all possible combinations I want to automatically build the project with different preprocessor defines and run the resulting applications.

For example I have only 2 features which can be enabled or disabled. Therefore I have four possible combinations:

make test
make test CFLAGS=-DENABLE_FEATURE_A
make test CFLAGS=-DENABLE_FEATURE_B
make test CFLAGS=-DENABLE_FEATURE_A -DENABLE_FEATURE_B

Can I use make or cmake to do this automatically so I just have to call make tests. Or do other tools exist which could help me with that task?

Upvotes: 1

Views: 234

Answers (2)

Mike Kinghan
Mike Kinghan

Reputation: 61327

Here is a toy C++ program we can pretend is your test source that can be built in 4 variants for features A, B:

#include <iostream>

using namespace std;

int main()
{
#ifdef ENABLE_FEATURE_A
    cout << "With Feature A" << endl;
#endif
#ifdef ENABLE_FEATURE_B
    cout << "With Feature B" << endl;
#endif
    return 0;
}

And here is a GNU makefile that builds all variants (on Linux), when run in the same directory as main.cpp:

variants = tester tester_A tester_B tester_AB
objs = main.o

tests:
    $(foreach variant,$(variants),$(MAKE) clean_obj $(variant);)  

tester_A: CXXFLAGS += -DENABLE_FEATURE_A
tester_B: CXXFLAGS += -DENABLE_FEATURE_B
tester_AB: CXXFLAGS += -DENABLE_FEATURE_A -DENABLE_FEATURE_B

$(variants): $(objs)
    g++ $(LDFLAGS) -o $@ $<

clean_obj:
    rm -f $(objs)

clean: clean_obj
    rm -f $(variants)

The 4 resulting executables are those listed in variants.

Upvotes: 2

Arkady
Arkady

Reputation: 2207

Using CMake there exists, for example, config.h.in (that will be transformed to config.h) file. For each your combination, you can create config.h.in1, config.h.in2 and etc, and then write batch file that will repeatedly do next steps: 1) copy config.h.inX to config.h.in 2) make && make install.

At config.h.inX you may choose destination for your binaries (or put them to one folder, but change their names for each combination), so, as result you will have list of binaries you want to.

That doesn't look like good solution, more like spike, but that can help, if there will be no other advices.

Upvotes: 1

Related Questions