Presets: Add support for sourceDirName macro
The current functionality available with 3.19.0-rc1 allows a presets file to conveniently define build directories that live under the source directory (once #21311 (closed) is fixed). They can have preset-specific names and any relative path is relative to the source directory. However, it isn't always desirable to have the build directory located under the source directory. This can be problematic for CI systems, for example, where build artefacts or caches need to be saved between build stages. Sometimes these directories can be hard to exclude or convince the CI system to ignore.
A common alternative is to place build directories outside of the source directory, often as a sibling. On developer machines, one might have a directory containing all your source trees and a dedicated build
directory in which the build directories are placed. In such an arrangement, you want a project-specific name under that build
directory to distinguish each project's build directories.
With the existing set of macros supported by presets, you have to hard-code the project name in the presets file. This is a blocker to being able to easily share user presets files between projects (example below). A macro that provided just the source directory name without the full path to it would make this possible. Consider a user presets file that makes use of such a (not-yet-supported) macro name, which I'll call sourceDirName
here:
{
"version": 1,
"cmakeMinimumRequired": {
"major": 3,
"minor": 19,
"patch": 0
},
"configurePresets": [
{
"name": "Base",
"hidden": true,
"binaryDir": "${sourceParentDir}/build/${sourceDirName}-${presetName}",
},
{
"name": "Debug",
"inherits": "Base",
"displayName": "Ninja Debug",
"description": "Debug build using Ninja generator",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": {
"type": "STRING",
"value": "Debug"
}
}
},
{
"name": "Release",
"inherits": "Base",
"displayName": "Ninja Release",
"description": "Release build using Ninja generator",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": {
"type": "STRING",
"value": "Release"
}
}
},
{
"name": "Ninja Multi-Config",
"inherits": "Base",
"displayName": "Ninja Multi-Config",
"generator": "Ninja Multi-Config",
},
{
"name": "Xcode",
"inherits": "Base",
"displayName": "Xcode",
"generator": "Xcode",
}
]
}
The above is just a skeleton, in reality the various presets would likely have further cache variables, etc. defined. The main point is to note what this is trying to achieve with the binaryDir
field in the hidden Base
preset. It is for use with a directory structure like this:
SomeDir
+-- build
| +-- projA-Debug
| +-- projA-Release
| +-- projB-Xcode
+-- projA
| +-- CMakeUserPresets.json --> ../CMakeUserPresets-common.json
+-- projB
| +-- CMakeUserPresets.json --> ../CMakeUserPresets-common.json
+-- /CMakeUserPresets-common.json
The projA
and projB
projects source directories each have a symlinked CMakeUserPresets.json
file that points back to the common one in the parent SomeDir
directory. I'm proposing we add sourceDirName
or some variation of that to support the above use case.