Björn Pollex
Björn Pollex

Reputation: 76856

How to create a CMake configuration type that inherits from Release

This answer describes how to create a custom configuration type from scratch. How can I make a configuration type that exactly matches the builtin Release, only with some added flags? I'm using this right now:

set(CMAKE_CONFIGURATION_TYPES "Debug;Release;ReleaseWithAssertions" CACHE STRING
    "Available build-types: Debug, Release and ReleaseWithAssertions")

set(CMAKE_CXX_FLAGS_RELEASEWITHASSERTIONS "${CMAKE_CXX_FLAGS_RELEASE} 
    -DENABLE_ASSERTIONS=1")

This seems to do what I want, but I'm only copying the value of CMAKE_CXX_FLAGS_RELEASE, so I'm wondering if there is anything I'm missing that users might expect?

Upvotes: 4

Views: 3588

Answers (3)

scx
scx

Reputation: 3947

From https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-can-i-extend-the-build-modes-with-a-custom-made-one-

It seems you would want to do the same for all of these.

CMAKE_CXX_FLAGS_RELEASEWITHASSERTIONS
CMAKE_C_FLAGS_RELEASEWITHASSERTIONS
CMAKE_EXE_LINKER_FLAGS_RELEASEWITHASSERTIONS
CMAKE_SHARED_LINKER_FLAGS_RELEASEWITHASSERTIONS

Upvotes: 0

Florian
Florian

Reputation: 43020

No, not in respect of adding a custom "configuration type that exactly matches the builtin Release". That's more like a feature request for CMake.

Edit: Just have seen that there actually is a "Creating new configurations for MSVC" feature request you could give support.

Here is some background information what's possible and what's not:

  1. There are potentially many configuration specific variables. You could copy those with a script:

    get_directory_property(_vars VARIABLES)
    foreach(_var IN LISTS _vars)
        if (_var MATCHES "_RELEASE$")
            string(REPLACE "_RELEASE" "_RELEASEWITHASSERTIONS" _var_new "${_var}")
            set(${_var_new} "${${_var}}")
        endif()
    endforeach()
    
  2. You need to map imported targets to for your new configuration with setting CMAKE_MAP_IMPORTED_CONFIG_<CONFIG>:

    list(APPEND CMAKE_MAP_IMPORTED_CONFIG_RELEASEWITHASSERTIONS "Release" "")
    
  3. You can't do anything about $<CONFIG:cfg> type generator expressions checking for specific configuration names

  4. You have to check directory/target/source file and configuration specific changes in properties

References

Upvotes: 6

dlasalle
dlasalle

Reputation: 1725

The only other one you might want would be CMAKE_C_FLAGS_RELEASE in case you're compiling any C files.

See cmake's documentation:

None (CMAKE_C_FLAGS or CMAKE_CXX_FLAGS used)
Debug (CMAKE_C_FLAGS_DEBUG or CMAKE_CXX_FLAGS_DEBUG)
Release (CMAKE_C_FLAGS_RELEASE or CMAKE_CXX_FLAGS_RELEASE)
RelWithDebInfo (CMAKE_C_FLAGS_RELWITHDEBINFO or CMAKE_CXX_FLAGS_RELWITHDEBINFO
MinSizeRel (CMAKE_C_FLAGS_MINSIZEREL or CMAKE_CXX_FLAGS_MINSIZEREL)

Upvotes: 1

Related Questions