From f54507c2f6826e9b2b021dbf3b3fa75dcdce22fe Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Mon, 7 Mar 2022 17:48:18 -0500 Subject: CMakePresets.json: Add ${fileDir} macro Fixes: #23214 --- Help/manual/cmake-presets.7.rst | 5 +++++ Help/release/3.23.rst | 3 +++ Source/cmCMakePresetsGraph.cxx | 8 ++++++++ Tests/RunCMake/CMakePresets/FileDir.cmake | 3 +++ Tests/RunCMake/CMakePresets/FileDir.json.in | 6 ++++++ Tests/RunCMake/CMakePresets/FileDirFuture-result.txt | 1 + Tests/RunCMake/CMakePresets/FileDirFuture-stderr.txt | 2 ++ Tests/RunCMake/CMakePresets/FileDirFuture.json.in | 13 +++++++++++++ Tests/RunCMake/CMakePresets/RunCMakeTest.cmake | 12 +++++++++++- Tests/RunCMake/CMakePresets/subdir/FileDir.json.in | 13 +++++++++++++ 10 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Tests/RunCMake/CMakePresets/FileDir.cmake create mode 100644 Tests/RunCMake/CMakePresets/FileDir.json.in create mode 100644 Tests/RunCMake/CMakePresets/FileDirFuture-result.txt create mode 100644 Tests/RunCMake/CMakePresets/FileDirFuture-stderr.txt create mode 100644 Tests/RunCMake/CMakePresets/FileDirFuture.json.in create mode 100644 Tests/RunCMake/CMakePresets/subdir/FileDir.json.in diff --git a/Help/manual/cmake-presets.7.rst b/Help/manual/cmake-presets.7.rst index 4620984..03bb5aa 100644 --- a/Help/manual/cmake-presets.7.rst +++ b/Help/manual/cmake-presets.7.rst @@ -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 (``$``). diff --git a/Help/release/3.23.rst b/Help/release/3.23.rst index b2928db..c3bee02 100644 --- a/Help/release/3.23.rst +++ b/Help/release/3.23.rst @@ -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. diff --git a/Source/cmCMakePresetsGraph.cxx b/Source/cmCMakePresetsGraph.cxx index 705e2b0..dc14831 100644 --- a/Source/cmCMakePresetsGraph.cxx +++ b/Source/cmCMakePresetsGraph.cxx @@ -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; diff --git a/Tests/RunCMake/CMakePresets/FileDir.cmake b/Tests/RunCMake/CMakePresets/FileDir.cmake new file mode 100644 index 0000000..c7298cd --- /dev/null +++ b/Tests/RunCMake/CMakePresets/FileDir.cmake @@ -0,0 +1,3 @@ +include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake) + +test_variable(TEST_FILE_DIR "" "${CMAKE_CURRENT_SOURCE_DIR}/subdir") diff --git a/Tests/RunCMake/CMakePresets/FileDir.json.in b/Tests/RunCMake/CMakePresets/FileDir.json.in new file mode 100644 index 0000000..899e5f3 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/FileDir.json.in @@ -0,0 +1,6 @@ +{ + "version": 4, + "include": [ + "subdir/FileDir.json" + ] +} diff --git a/Tests/RunCMake/CMakePresets/FileDirFuture-result.txt b/Tests/RunCMake/CMakePresets/FileDirFuture-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CMakePresets/FileDirFuture-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CMakePresets/FileDirFuture-stderr.txt b/Tests/RunCMake/CMakePresets/FileDirFuture-stderr.txt new file mode 100644 index 0000000..ba85f0f --- /dev/null +++ b/Tests/RunCMake/CMakePresets/FileDirFuture-stderr.txt @@ -0,0 +1,2 @@ +^CMake Error: Could not read presets from [^ +]*/Tests/RunCMake/CMakePresets/FileDirFuture: Invalid macro expansion$ diff --git a/Tests/RunCMake/CMakePresets/FileDirFuture.json.in b/Tests/RunCMake/CMakePresets/FileDirFuture.json.in new file mode 100644 index 0000000..75d6622 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/FileDirFuture.json.in @@ -0,0 +1,13 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "FileDirFuture", + "generator": "@RunCMake_GENERATOR@", + "binaryDir": "${sourceDir}/build", + "cacheVariables": { + "TEST_FILE_DIR": "${fileDir}" + } + } + ] +} diff --git a/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake b/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake index 955cf53..449132a 100644 --- a/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake +++ b/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake @@ -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) diff --git a/Tests/RunCMake/CMakePresets/subdir/FileDir.json.in b/Tests/RunCMake/CMakePresets/subdir/FileDir.json.in new file mode 100644 index 0000000..00282a7 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/subdir/FileDir.json.in @@ -0,0 +1,13 @@ +{ + "version": 4, + "configurePresets": [ + { + "name": "FileDir", + "generator": "@RunCMake_GENERATOR@", + "binaryDir": "${sourceDir}/build", + "cacheVariables": { + "TEST_FILE_DIR": "${fileDir}" + } + } + ] +} -- cgit v0.12