diff options
author | Brad King <brad.king@kitware.com> | 2023-04-27 13:04:54 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2023-04-27 13:05:13 (GMT) |
commit | d5095028ea2d7ceda2a42384a62bd9c70fbca27e (patch) | |
tree | 97200b3aa23db3fd764c142a62c7d58ddbaf1a06 /Source | |
parent | 7d62611a977e58253144b23d507bf04a49a83097 (diff) | |
parent | f552ba6e6d4571213afdc8afdd063637ac0cbee7 (diff) | |
download | CMake-d5095028ea2d7ceda2a42384a62bd9c70fbca27e.zip CMake-d5095028ea2d7ceda2a42384a62bd9c70fbca27e.tar.gz CMake-d5095028ea2d7ceda2a42384a62bd9c70fbca27e.tar.bz2 |
Merge topic 'preset-includes-macro-expansion'
f552ba6e6d presets: add support for macro expansion to includes
1df24df01f presets: Fix encoding of env/penv macro expansion on Windows
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !8255
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmCMakePresetsGraph.cxx | 29 | ||||
-rw-r--r-- | Source/cmCMakePresetsGraphInternal.h | 10 | ||||
-rw-r--r-- | Source/cmCMakePresetsGraphReadJSON.cxx | 37 |
3 files changed, 57 insertions, 19 deletions
diff --git a/Source/cmCMakePresetsGraph.cxx b/Source/cmCMakePresetsGraph.cxx index 13e8bad..13eddbe 100644 --- a/Source/cmCMakePresetsGraph.cxx +++ b/Source/cmCMakePresetsGraph.cxx @@ -4,7 +4,6 @@ #include <algorithm> #include <cassert> -#include <cstdlib> #include <functional> #include <iostream> #include <iterator> @@ -49,6 +48,7 @@ template <typename T> using PresetPair = cmCMakePresetsGraph::PresetPair<T>; using ExpandMacroResult = cmCMakePresetsGraphInternal::ExpandMacroResult; using MacroExpander = cmCMakePresetsGraphInternal::MacroExpander; +using cmCMakePresetsGraphInternal::ExpandMacros; void InheritString(std::string& child, const std::string& parent) { @@ -204,14 +204,6 @@ bool IsValidMacroNamespace(const std::string& str) ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status, const std::vector<MacroExpander>& macroExpanders, int version); -ExpandMacroResult ExpandMacros( - std::string& out, const std::vector<MacroExpander>& macroExpanders, - int version); -ExpandMacroResult ExpandMacro(std::string& out, - const std::string& macroNamespace, - const std::string& macroName, - const std::vector<MacroExpander>& macroExpanders, - int version); bool ExpandMacros(const cmCMakePresetsGraph& graph, const ConfigurePreset& preset, @@ -448,9 +440,9 @@ bool ExpandMacros(cmCMakePresetsGraph& graph, const T& preset, if (macroName.empty()) { return ExpandMacroResult::Error; } - const char* value = std::getenv(macroName.c_str()); - if (value) { - result += value; + if (cm::optional<std::string> value = + cmSystemTools::GetEnvVar(macroName)) { + result += *value; } return ExpandMacroResult::Ok; } @@ -515,8 +507,9 @@ ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status, status = CycleStatus::Verified; return ExpandMacroResult::Ok; } +} -ExpandMacroResult ExpandMacros( +ExpandMacroResult cmCMakePresetsGraphInternal::ExpandMacros( std::string& out, const std::vector<MacroExpander>& macroExpanders, int version) { @@ -595,11 +588,10 @@ ExpandMacroResult ExpandMacros( return ExpandMacroResult::Ok; } -ExpandMacroResult ExpandMacro(std::string& out, - const std::string& macroNamespace, - const std::string& macroName, - const std::vector<MacroExpander>& macroExpanders, - int version) +ExpandMacroResult cmCMakePresetsGraphInternal::ExpandMacro( + std::string& out, const std::string& macroNamespace, + const std::string& macroName, + const std::vector<MacroExpander>& macroExpanders, int version) { for (auto const& macroExpander : macroExpanders) { auto result = macroExpander(macroNamespace, macroName, out, version); @@ -615,6 +607,7 @@ ExpandMacroResult ExpandMacro(std::string& out, return ExpandMacroResult::Error; } +namespace { template <typename T> bool SetupWorkflowConfigurePreset(const T& preset, const ConfigurePreset*& configurePreset, diff --git a/Source/cmCMakePresetsGraphInternal.h b/Source/cmCMakePresetsGraphInternal.h index db784c3..f133efb 100644 --- a/Source/cmCMakePresetsGraphInternal.h +++ b/Source/cmCMakePresetsGraphInternal.h @@ -28,6 +28,16 @@ enum class ExpandMacroResult using MacroExpander = std::function<ExpandMacroResult( const std::string&, const std::string&, std::string&, int version)>; + +ExpandMacroResult ExpandMacros( + std::string& out, const std::vector<MacroExpander>& macroExpanders, + int version); + +ExpandMacroResult ExpandMacro(std::string& out, + const std::string& macroNamespace, + const std::string& macroName, + const std::vector<MacroExpander>& macroExpanders, + int version); } class cmCMakePresetsGraph::Condition diff --git a/Source/cmCMakePresetsGraphReadJSON.cxx b/Source/cmCMakePresetsGraphReadJSON.cxx index bc829f3..54fea40 100644 --- a/Source/cmCMakePresetsGraphReadJSON.cxx +++ b/Source/cmCMakePresetsGraphReadJSON.cxx @@ -33,6 +33,9 @@ using PackagePreset = cmCMakePresetsGraph::PackagePreset; using WorkflowPreset = cmCMakePresetsGraph::WorkflowPreset; using ArchToolsetStrategy = cmCMakePresetsGraph::ArchToolsetStrategy; using JSONHelperBuilder = cmJSONHelperBuilder; +using ExpandMacroResult = cmCMakePresetsGraphInternal::ExpandMacroResult; +using MacroExpander = cmCMakePresetsGraphInternal::MacroExpander; +using cmCMakePresetsGraphInternal::ExpandMacros; constexpr int MIN_VERSION = 1; constexpr int MAX_VERSION = 7; @@ -688,7 +691,39 @@ bool cmCMakePresetsGraph::ReadJSONFile(const std::string& filename, return true; }; - for (auto include : presets.Include) { + std::vector<MacroExpander> macroExpanders; + + MacroExpander environmentMacroExpander = + [](const std::string& macroNamespace, const std::string& macroName, + std::string& expanded, int /*version*/) -> ExpandMacroResult { + if (macroNamespace == "penv") { + if (macroName.empty()) { + return ExpandMacroResult::Error; + } + if (cm::optional<std::string> value = + cmSystemTools::GetEnvVar(macroName)) { + expanded += *value; + } + return ExpandMacroResult::Ok; + } + + return ExpandMacroResult::Ignore; + }; + + macroExpanders.push_back(environmentMacroExpander); + + for (Json::ArrayIndex i = 0; i < presets.Include.size(); ++i) { + auto include = presets.Include[i]; + + // Support for macro expansion in includes added in version 7 + if (v >= 7) { + if (ExpandMacros(include, macroExpanders, v) != ExpandMacroResult::Ok) { + cmCMakePresetErrors::INVALID_INCLUDE(&root["include"][i], + &this->parseState); + return false; + } + } + if (!cmSystemTools::FileIsFullPath(include)) { auto directory = cmSystemTools::GetFilenamePath(filename); include = cmStrCat(directory, '/', include); |