Is it possible to have multiple configuration presets that add compiler options to CMAKE_CXX_FLAGS?
For example,
{
"name": "common-presets",
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_FLAGS_INIT": "-Dproperty-1"
}
{
"name": "specific-presets",
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_FLAGS_INIT": "-Dproperty-2"
}
{
"name": "My-Target",
"description": "My Release target.",
"displayName": "My_Release_Target",
"inherits": [ "common-presets", "specific-presets" ],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_CXX_FLAGS_INIT": "-Dproperty-3"
}
}
-
I would expect to see -DCMAKE_CXX_FLAGS_INIT:STRING="-Dproperty-1 -Dproperty-2 -Dproperty-3"
But I see
-DCMAKE_CXX_FLAGS_INIT:STRING="-Dproperty-3"
Am I doing something wrong or are my expectations incorrect?
At this point, presets can only set individual cache variables to single values. There’s no way to accumulate values into a cache variable.
A workaround is to have your presets set separate variables specific to your CMakeLists.txt and to accumulate the flags yourself.
Another alternative is to set environment variables and combine them at the last preset into the variable. That way CMakeLists.txt doesn’t need to be changed.
With something like the following:
{
"name": "common-presets",
"hidden": true,
"environment": {
"PRESET_COMMON_CXX_FLAGS": "-Dproperty-1"
}
},
{
"name": "specific-presets",
"hidden": true,
"environment": {
"PRESET_SPECIFIC_CXX_FLAGS": "-Dproperty-2"
}
},
{
"name": "My-Target",
"description": "My Release target.",
"displayName": "My_Release_Target",
"inherits": [ "common-presets", "specific-presets" ],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_CXX_FLAGS_INIT": "$env{PRESET_COMMON_CXX_FLAGS} $env{PRESET_SPECIFIC_CXX_FLAGS} -Dproperty-3"
}
}