CMakePresets.json: Add ${fileDir} macro

Fixes: #23214
This commit is contained in:
Kyle Edwards
2022-03-07 17:48:18 -05:00
parent 7a8536e0ea
commit f54507c2f6
10 changed files with 65 additions and 1 deletions

View File

@@ -1053,6 +1053,11 @@ Recognized macros include:
:variable:`CMAKE_HOST_SYSTEM_NAME`. This is allowed in preset files
specifying version ``3`` or above.
``${fileDir}``
Path to the directory containing the preset file which contains the macro.
This is allowed in preset files specifying version ``4`` or above.
``${dollar}``
A literal dollar sign (``$``).

View File

@@ -18,6 +18,9 @@ Presets
* :manual:`cmake-presets(7)` files now have an optional ``include`` field,
which allows the files to include other files.
* :manual:`cmake-presets(7)` files now support a ``${fileDir}`` macro, which
contains the directory containing the preset file.
* :manual:`cmake-presets(7)` gained support for specifying the
``resolvePackageReferences`` command line option in a build preset to control
restoration behavior of package references from external package managers.

View File

@@ -353,6 +353,14 @@ bool ExpandMacros(const cmCMakePresetsGraph& graph, const T& preset,
macroOut += cmSystemTools::GetSystemName();
return ExpandMacroResult::Ok;
}
if (macroName == "fileDir") {
if (version < 4) {
return ExpandMacroResult::Error;
}
macroOut +=
cmSystemTools::GetParentDirectory(preset.OriginFile->Filename);
return ExpandMacroResult::Ok;
}
}
return ExpandMacroResult::Ignore;

View File

@@ -0,0 +1,3 @@
include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake)
test_variable(TEST_FILE_DIR "" "${CMAKE_CURRENT_SOURCE_DIR}/subdir")

View File

@@ -0,0 +1,6 @@
{
"version": 4,
"include": [
"subdir/FileDir.json"
]
}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,2 @@
^CMake Error: Could not read presets from [^
]*/Tests/RunCMake/CMakePresets/FileDirFuture: Invalid macro expansion$

View File

@@ -0,0 +1,13 @@
{
"version": 3,
"configurePresets": [
{
"name": "FileDirFuture",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"TEST_FILE_DIR": "${fileDir}"
}
}
]
}

View File

@@ -53,7 +53,7 @@ function(run_cmake_presets name)
)
string(REGEX REPLACE "\\.in$" "" _extra_file_out_relative "${_extra_file_relative}")
set(_extra_file_out "${RunCMake_TEST_SOURCE_DIR}/${_extra_file_out_relative}")
configure_file("${_extra_file}" "${_extra_file_out}")
configure_file("${_extra_file}" "${_extra_file_out}" @ONLY)
list(APPEND _CMakePresets_EXTRA_FILES_OUT "${_extra_file_out}")
list(APPEND _CMakePresets_EXTRA_FILES_SCHEMA_EXPECTED_RESULTS 0)
endforeach()
@@ -317,6 +317,16 @@ run_cmake_presets(HostSystemName)
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/HostSystemNameFuture.json.in")
run_cmake_presets(HostSystemNameFuture)
# Test ${fileDir} macro
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/FileDir.json.in")
set(CMakePresets_EXTRA_FILES
"${RunCMake_SOURCE_DIR}/subdir/FileDir.json.in"
)
run_cmake_presets(FileDir)
unset(CMakePresets_EXTRA_FILES)
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/FileDirFuture.json.in")
run_cmake_presets(FileDirFuture)
# Test conditions
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/Conditions.json.in")
run_cmake_presets(ListConditions --list-presets)

View File

@@ -0,0 +1,13 @@
{
"version": 4,
"configurePresets": [
{
"name": "FileDir",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"TEST_FILE_DIR": "${fileDir}"
}
}
]
}