3

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?

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283

3 Answers3

5

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

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Thanks, this is very useful information. The solution described by @dlasalle works in my current project, but your answer provides some useful resources to know what to look out for. – Björn Pollex May 09 '17 at 08:35
  • 1
    This is so bad(Cmake way). So much work to create a custom build type with one additional preprocessor macro for public release. It should be 2 lines: one to create new build type from currently existing, and the other one to add the property to it. Thanks for the solution, I guess I'll have to write all this just to add custom build type with one preprocessor macro. – KulaGGin Nov 30 '21 at 16:17
  • @KulaGGin yes, it's a lot of lines, but it could be factored into a `function()` for reuse. Besides, without knowing more of your context, what you're describing sounds more like it could/should be achieved with `option()` instead. – starball Oct 13 '22 at 01:49
1

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)
dlasalle
  • 1,615
  • 3
  • 19
  • 25
0

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
scx
  • 3,221
  • 1
  • 19
  • 37