presets 'include': Cannot get current directory in an included file
General comments on include
in 3.23 RC1, related to https://cmake.org/cmake/help/v3.23/manual/cmake-presets.7.html#manual:cmake-presets(7):
- Object name should probably have been
includes
as it is a list - The example preset file in uses CMake 3.21 with version 4 (see !7028 (merged))
- There is no example for
include
(see !7028 (merged)) -
include
should be just after "cmakeMinimumRequired" (see !7028 (merged)) - Doc states "If the filenames are not absolute, they are considered relative to the current file." I expected that to be relative ${sourceFile}, but that is not the case. (See 3. below too.)
I still cannot use this functionality though, see use case in #21331 (comment 1108055)
The CMakePresets.json
files in app1 and app3 in the example have different folder depths.
The common include script in utilrepo need to know the path to commonlib, but I cannot get that propagated
(other than requiring the user to provide the information from the command line, for instance with an environment variable).
(Excluding version info in the example files)
-
No possibility to evaluate current path in included files. "sourceDir" is not the same in "app1" and "app3" so that macro cannot be used in "utilrepo". A variable like
currentDir
is needed to set the path to "commonlib" or some way to evaluate a path. This was known by me when I looked at the proposal for include, but I expected 2 or 3 to work. -
It is not possible to set a base environment with an env var to "commonlib" in the "app" CMakePrests.json that the "utilrepo" file reads, fails with
CMake Error: Could not read presets from <path>: Inherited preset is unreachable from preset's file
(Also if base_environment is added to CMakeUserPresets.json (that file should not be added to Git).)
"configurePresets": [
{
"name": "base_environment",
"hidden": true,
"environment": {
"COMMONLIB_DIR": "${sourceDir}/my_relative_path/commonlib"
}
}
],
"include": [
"utilrepo/utilPresets.json"
]
}
utilPresets.json
{
"configurePresets": [
{
"name": "environment",
"inherits": "base_environment",
"hidden": true,
"environment": {
- As a workaround, it could be possible to include a file in "app" from the "util". (So "app" need both CMakePresets.json and this CMakeBasePresets.json)
Error message is unclear (I have seen an issue that json parse errors are hard to improve, ok with this)
CMake Error: Could not read presets from <path>: File not found
- ${sourceDir} is not parsed in include array
- The path in include is relative "utilrepo file", not the sourceDir file (probably OK, but no workaround for me)
base file in app: CMakeBasePresets.json
"configurePresets": [
{
"name": "base_environment",
"hidden": true,
"environment": {
"COMMONLIB_DIR": "${sourceDir}/my_relative_path/commonlib"
}
}
],
app CMakePresets.json
"include": [
"utilrepo/utilPresets.json"
]
}
util preset
{
"include": [
"CMakeBasePresets.json"
],
"configurePresets": [
{
"name": "environment",
"inherits": "base_environment",
"hidden": true,
"environment": {
- Usable workaround: Duplicate utilPresets.json
If the app presets points to presets in the util repo where all but COMMONLIB_DIR is duplicated, this works. Then the number of duplicated files is limited at least. I expect that there will be a need of app-type1 and app-type2 multiplying the number of variants, but still kindof manageable.
If utilPresets_1.json could inherit a base_environment it would be OK, but that fails as in 1.
app1 CMakePresets.json
"include": [
"../util/utilPresets_1.json"
]
}
app3 CMakePresets.json
"include": [
"../../util/utilPresets_2.json"
]
}
utilPresets_1.json
{
"configurePresets": [
{
"name": "environment",
"hidden": true,
"environment": {
"COMMONLIB_DIR": "${sourceDir}/../my_relative_path/commonlib"
utilPresets_2.json
{
"configurePresets": [
{
"name": "environment",
"hidden": true,
"environment": {
"COMMONLIB_DIR": "${sourceDir}/../../my_relative_path/commonlib"