ollaw
ollaw

Reputation: 2193

CMake : different configure_file() for each target


I'm not very familiar with cmake but as the title says what i'd like to do is the following:

I've an header file of configuration (eg config.h.in), in which I'd like to specify all my parameters depending on which target I'm currently calling. So my header file is something like:

#cmakedefine TEST @TEST@
#cmakedefine PINK @PINK@

#ifndef TEST
#define MY_A 10    
#endif
#ifdef PINK
#define MY_A 20
#endif

Now, in my CMakeLists.txt I would like to have multiple targets, so for example (actually the config.hfile is just included from other .c files)

add_executable(FirstTarget
    something.c
    somethingelse.c
    config.h
)

add_executable(SecondTarget
    something.c
    somethingother.c
    config.h
)

And what i would really like to do is that FirstTarget And SecondTarget have different configuration file, so what I'm asking is if it possible to run something like

set(TEST Test)
configure_file(config.h.in config.h)

just for target FirstTarget, and then someway running for SecondTarget

set(PINK Test2)
configure_file(config.h.in config.h)

so that if i call make FirstTarget and make SecondTarget they've different configuration parameters each.

Thanks!

Upvotes: 2

Views: 2425

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65928

The command configure_file creates a "real" file: it has the same content for each target.

However, the created file may contain #ifdef (or other conditional statements), so its interpretation may differ for different target.

Following example uses target_compile_definitions command for "assign" compile definitions for a specific target.

config.h:

#ifndef TEST
#define MY_A 10    
#endif
#ifdef PINK
#define MY_A 20
#endif

CMakeLists.txt:

add_executable(FirstTarget ...)
# When "config.h" will be included into this target, it will use "PINK" branch.
target_compile_definitions(FirstTarget PRIVATE PINK)

add_executable(SecondTarget ...)
# When "config.h" will be included into this target, it will use "TEST" branch.
target_compile_definitions(FirstTarget PRIVATE TEST)

Upvotes: 3

Related Questions