jjcasmar
jjcasmar

Reputation: 1675

Mix build flags for several configurations in CMake

According to CMake documentation here, we can use the build type to specify our own build types, adding flags to our custom build depending on the CMAKE_BUILD_TYPE option.

For example, if CMAKE_BUILD_TYPE == Profile, CMake will use CMAKE_CXX_FLAGS_PROFILE for the build flags.

I would like to know if there is any way to "inherit" build flags from another build type. For example, I want a trace and debug build and a trace and release build. Is it possible to do something like CMAKE_BUILD_TYPE=Trace_Debug which adds CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_TRACE to the build? I guess this can have some problems as it would allow the project to have contradictory build flags, but nothing prohibits to add -O1 and -O3 to our flags now, so that problem already exists.

Upvotes: 0

Views: 394

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66118

There is no such thing in CMake like "inheriting" a build type.

Is it possible to do something like CMAKE_BUILD_TYPE=Trace_Debug which adds CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_TRACE to the build?

Just define a (new) variable CMAKE_CXX_FLAGS_TRACE_DEBUG and set its value appropriately. When setting the value of the variable you may use values from other variables:

# Combine values of two variables into the single one.
set(CMAKE_CXX_FLAGS_TRACE_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_TRACE}")

It is up to you to remove conflicting flags from the resulted variable. CMake has no knowledge about conflicting flags, it just passes them to the compiler tool.

Upvotes: 2

Related Questions