Take this simple set up of CMake presets.
{
"version": 6,
"cmakeMinimumRequired": {
"major": 3,
"minor": 27,
"patch": 0
},
"configurePresets": [
{
"name": "base-preset",
"hidden": true,
"inherits": [],
"environment": {
"QT_ROOT": "/base/location/of/qt"
},
"cacheVariables": {
"CMAKE_PREFIX_PATH": "$env{QT_ROOT}/lib/cmake"
}
},
{
"name": "test-preset",
"inherits": ["base-preset"],
"environment": {
"QT_ROOT": "/actual/location/of/qt"
}
}
],
"buildPresets": []
}
I am wondering if there is a way to essentially “override” the environment variable QT_ROOT
via the command line. This would be useful as particular users would want to set QT_ROOT
differently than specified in test-preset
.
I know that the environment variables specified in the preset will always take precedence over the ones in the process’ environment (see https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#configure-preset).
In simple terms, executing QT_ROOT="/user-defined/location/of/qt" cmake --preset test-preset
, I would expect the following output:
# QT_ROOT="/user-defined/location/of/qt" cmake --preset test-preset
Preset CMake variables:
CMAKE_PREFIX_PATH="/user-defined/location/of/qt/lib/cmake"
Preset environment variables:
QT_ROOT="/user-defined/location/of/qt"
but I get
# QT_ROOT="/user-defined/location/of/qt" cmake --preset test-preset
Preset CMake variables:
CMAKE_PREFIX_PATH="/actual/location/of/qt/lib/cmake"
Preset environment variables:
QT_ROOT="/actual/location/of/qt"
I am also aware of using $penv{}
instead of $env()
in defining the cache variable, but I would want to retain the ability to define a sensible default as part of the environment
block in the preset.
Currently, I am making use of a user preset that inherits of test-preset
and overrides QT_ROOT
that way, however this solution becomes tedious when there are many presets.
In that case, I would need a user preset per preset I would want to use, just to change an environment variable.