[CMake] How to set a preprocessor define for all build configurations except one ?

Michael Hertling mhertling at online.de
Sat Jun 18 09:30:48 EDT 2011


On 06/16/2011 11:44 PM, Glenn Coombs wrote:
> On 16 June 2011 15:45, Michael Hertling <mhertling at online.de> wrote:
> 
>> IMO, the default should not need to be explicitly enabled but the
>> exception, and readability - though important - is subordinate to
>> functionality, but probably, this is a matter of personal taste.
>>
>> However, if you stick with the GEN_OUTFILES definition, be aware of
>> the single-config generator with an empty build type. In that case,
>> only the COMPILE_DEFINITIONS property is in effect, so you must add
>> the GEN_OUTFILES definition to it, but with a non-empty build type,
>> this config-less property is in effect, too, so you must *not* add
>> GEN_OUTFILE to it. Thus, you need to differentiate these cases:
>>
>> IF(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE)
>>    FOREACH(... IN LISTS ... ITEMS ...)
>>        # Set COMPILE_DEFINITIONS_<CONFIG>.
>>    ENDFOREACH()
>> ELSE()
>>    # Set COMPILE_DEFINITIONS.
>> ENDIF()
>>
>> Clearly, this cuts down the elegance of the FOREACH loop, while the
>> inverted approach with NO_GEN_OUTFILES would be unalteredly smooth.
>>
> 
> Luckily I already have this in my CMakeLists.txt:
> 
> if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
>     set(CMAKE_BUILD_TYPE Release)
> endif()
> 
> to force the default build to use the Release configuration.  Otherwise I
> would have fallen foul of the case you are suggesting.
> 
> Is COMPILE_DEFINITIONS only used in single-config generators if
> CMAKE_BUILD_TYPE is not set ?  That might explain why my initial attempts to
> do this failed.  I had added GEN_OUTFILES to COMPILE_DEFINITIONS thinking
> this would apply to all configs.  And then when I tried to remove that
> option from COMPILE_DEFINITIONS_RELEASENOOUTFILES it failed to have the
> desired effect.

COMPILE_DEFINITIONS is used *always*, i.e. for every generator in every
configuration, and COMPILE_DEFINITIONS_<CONFIG> is used *additionally*
in configuration <CONFIG>, so it can not be used to remove definitions
added to COMPILE_DEFINITIONS; in fact, these properties are completely
independent from each other.

> Would that approach have worked for the multi-config generators ?  Do they
> use the concatenation of COMPILE_DEFINITIONS and COMPILE_DEFINITIONS_<active
> config> ?

All generators use this concatenation, and that's the reason why you
must handle "NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE"
separately one way or another if the GEN_OUTFILES definition is to
be enabled always except for the RELEASENOOUTFILES configuration.

Regards,

Michael


More information about the CMake mailing list