/src/CMake/Source/cmCMakePresetsGraphInternal.h
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #pragma once |
4 | | |
5 | | #include <cstddef> |
6 | | #include <memory> |
7 | | #include <string> |
8 | | #include <vector> |
9 | | |
10 | | #include <cm3p/json/value.h> |
11 | | |
12 | | #include "cmCMakePresetsGraph.h" |
13 | | #include "cmJSONHelpers.h" |
14 | | #include "cmSystemTools.h" |
15 | | |
16 | | #define CHECK_OK(expr) \ |
17 | 0 | do { \ |
18 | 0 | auto _result = expr; \ |
19 | 0 | if (_result != true) \ |
20 | 0 | return _result; \ |
21 | 0 | } while (false) |
22 | | |
23 | | namespace cmCMakePresetsGraphInternal { |
24 | | enum class ExpandMacroResult |
25 | | { |
26 | | Ok, |
27 | | Ignore, |
28 | | Error, |
29 | | }; |
30 | | |
31 | | class MacroExpander |
32 | | { |
33 | | public: |
34 | | virtual ExpandMacroResult operator()(std::string const& macroNamespace, |
35 | | std::string const& macroName, |
36 | | std::string& macroOut, |
37 | | int version) const = 0; |
38 | 0 | virtual ~MacroExpander() = default; |
39 | | }; |
40 | | using MacroExpanderVector = std::vector<std::unique_ptr<MacroExpander>>; |
41 | | |
42 | | ExpandMacroResult ExpandMacros(std::string& out, |
43 | | MacroExpanderVector const& macroExpanders, |
44 | | int version); |
45 | | |
46 | | ExpandMacroResult ExpandMacro(std::string& out, |
47 | | std::string const& macroNamespace, |
48 | | std::string const& macroName, |
49 | | MacroExpanderVector const& macroExpanders, |
50 | | int version); |
51 | | } |
52 | | |
53 | | class cmCMakePresetsGraph::Condition |
54 | | { |
55 | | public: |
56 | 0 | virtual ~Condition() = default; |
57 | | |
58 | | virtual bool Evaluate( |
59 | | cmCMakePresetsGraphInternal::MacroExpanderVector const& expanders, |
60 | | int version, cm::optional<bool>& out) const = 0; |
61 | 0 | virtual bool IsNull() const { return false; } |
62 | | }; |
63 | | |
64 | | namespace cmCMakePresetsGraphInternal { |
65 | | class BaseMacroExpander : public MacroExpander |
66 | | { |
67 | | cmCMakePresetsGraph const& Graph; |
68 | | cm::optional<std::string> File; |
69 | | |
70 | | public: |
71 | | BaseMacroExpander(cmCMakePresetsGraph const& graph) |
72 | 0 | : Graph(graph) |
73 | 0 | { |
74 | 0 | } |
75 | | BaseMacroExpander(cmCMakePresetsGraph const& graph, std::string const& file) |
76 | 0 | : Graph(graph) |
77 | 0 | , File(file) |
78 | 0 | { |
79 | 0 | } |
80 | | ExpandMacroResult operator()(std::string const& macroNamespace, |
81 | | std::string const& macroName, |
82 | | std::string& macroOut, |
83 | | int version) const override; |
84 | | }; |
85 | | |
86 | | template <class T> |
87 | | class PresetMacroExpander : public MacroExpander |
88 | | { |
89 | | cmCMakePresetsGraph const& Graph; |
90 | | T const& Preset; |
91 | | |
92 | | public: |
93 | | PresetMacroExpander(cmCMakePresetsGraph const& graph, T const& preset) |
94 | 0 | : Graph(graph) |
95 | 0 | , Preset(preset) |
96 | 0 | { |
97 | 0 | } Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::ConfigurePreset>::PresetMacroExpander(cmCMakePresetsGraph const&, cmCMakePresetsGraph::ConfigurePreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::BuildPreset>::PresetMacroExpander(cmCMakePresetsGraph const&, cmCMakePresetsGraph::BuildPreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::TestPreset>::PresetMacroExpander(cmCMakePresetsGraph const&, cmCMakePresetsGraph::TestPreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::PackagePreset>::PresetMacroExpander(cmCMakePresetsGraph const&, cmCMakePresetsGraph::PackagePreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::WorkflowPreset>::PresetMacroExpander(cmCMakePresetsGraph const&, cmCMakePresetsGraph::WorkflowPreset const&) |
98 | | ExpandMacroResult operator()(std::string const& macroNamespace, |
99 | | std::string const& macroName, |
100 | | std::string& macroOut, |
101 | | int version) const override |
102 | 0 | { |
103 | 0 | if (macroNamespace.empty()) { |
104 | 0 | if (macroName == "presetName") { |
105 | 0 | macroOut += Preset.Name; |
106 | 0 | return ExpandMacroResult::Ok; |
107 | 0 | } |
108 | 0 | if (macroName == "generator") { |
109 | | // Generator only makes sense if preset is not hidden. |
110 | 0 | if (!Preset.Hidden) { |
111 | 0 | macroOut += Graph.GetGeneratorForPreset(Preset.Name); |
112 | 0 | } |
113 | 0 | return ExpandMacroResult::Ok; |
114 | 0 | } |
115 | 0 | if (macroName == "fileDir") { |
116 | 0 | if (version < 4) { |
117 | 0 | return ExpandMacroResult::Error; |
118 | 0 | } |
119 | 0 | macroOut += |
120 | 0 | cmSystemTools::GetParentDirectory(Preset.OriginFile->Filename); |
121 | 0 | return ExpandMacroResult::Ok; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | return ExpandMacroResult::Ignore; |
125 | 0 | } Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::ConfigurePreset>::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, int) const Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::BuildPreset>::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, int) const Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::TestPreset>::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, int) const Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::PackagePreset>::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, int) const Unexecuted instantiation: cmCMakePresetsGraphInternal::PresetMacroExpander<cmCMakePresetsGraph::WorkflowPreset>::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, int) const |
126 | | }; |
127 | | |
128 | | class NullCondition : public cmCMakePresetsGraph::Condition |
129 | | { |
130 | | bool Evaluate(MacroExpanderVector const& /*expanders*/, int /*version*/, |
131 | | cm::optional<bool>& out) const override |
132 | 0 | { |
133 | 0 | out = true; |
134 | 0 | return true; |
135 | 0 | } |
136 | | |
137 | 0 | bool IsNull() const override { return true; } |
138 | | }; |
139 | | |
140 | | class ConstCondition : public cmCMakePresetsGraph::Condition |
141 | | { |
142 | | public: |
143 | | bool Evaluate(MacroExpanderVector const& /*expanders*/, int /*version*/, |
144 | | cm::optional<bool>& out) const override |
145 | 0 | { |
146 | 0 | out = this->Value; |
147 | 0 | return true; |
148 | 0 | } |
149 | | |
150 | | bool Value; |
151 | | }; |
152 | | |
153 | | class EqualsCondition : public cmCMakePresetsGraph::Condition |
154 | | { |
155 | | public: |
156 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
157 | | cm::optional<bool>& out) const override; |
158 | | |
159 | | std::string Lhs; |
160 | | std::string Rhs; |
161 | | }; |
162 | | |
163 | | class InListCondition : public cmCMakePresetsGraph::Condition |
164 | | { |
165 | | public: |
166 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
167 | | cm::optional<bool>& out) const override; |
168 | | |
169 | | std::string String; |
170 | | std::vector<std::string> List; |
171 | | }; |
172 | | |
173 | | class MatchesCondition : public cmCMakePresetsGraph::Condition |
174 | | { |
175 | | public: |
176 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
177 | | cm::optional<bool>& out) const override; |
178 | | |
179 | | std::string String; |
180 | | std::string Regex; |
181 | | }; |
182 | | |
183 | | class AnyAllOfCondition : public cmCMakePresetsGraph::Condition |
184 | | { |
185 | | public: |
186 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
187 | | cm::optional<bool>& out) const override; |
188 | | |
189 | | std::vector<std::unique_ptr<Condition>> Conditions; |
190 | | bool StopValue; |
191 | | }; |
192 | | |
193 | | class NotCondition : public cmCMakePresetsGraph::Condition |
194 | | { |
195 | | public: |
196 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
197 | | cm::optional<bool>& out) const override; |
198 | | |
199 | | std::unique_ptr<Condition> SubCondition; |
200 | | }; |
201 | | |
202 | | bool PresetStringHelper(std::string& out, Json::Value const* value, |
203 | | cmJSONState* state); |
204 | | |
205 | | bool PresetNameHelper(std::string& out, Json::Value const* value, |
206 | | cmJSONState* state); |
207 | | |
208 | | bool PresetVectorStringHelper(std::vector<std::string>& out, |
209 | | Json::Value const* value, cmJSONState* state); |
210 | | |
211 | | bool PresetBoolHelper(bool& out, Json::Value const* value, cmJSONState* state); |
212 | | |
213 | | bool PresetOptionalBoolHelper(cm::optional<bool>& out, |
214 | | Json::Value const* value, cmJSONState* state); |
215 | | |
216 | | bool PresetIntHelper(int& out, Json::Value const* value, cmJSONState* state); |
217 | | |
218 | | bool PresetOptionalIntHelper(cm::optional<int>& out, Json::Value const* value, |
219 | | cmJSONState* state); |
220 | | |
221 | | bool PresetUIntHelper(unsigned int& out, Json::Value const* value, |
222 | | cmJSONState* state); |
223 | | |
224 | | bool PresetOptionalUIntHelper(cm::optional<unsigned int>& out, |
225 | | Json::Value const* value, cmJSONState* state); |
226 | | |
227 | | bool PresetVectorIntHelper(std::vector<int>& out, Json::Value const* value, |
228 | | cmJSONState* state); |
229 | | |
230 | | bool ConfigurePresetsHelper( |
231 | | std::vector<cmCMakePresetsGraph::ConfigurePreset>& out, |
232 | | Json::Value const* value, cmJSONState* state); |
233 | | |
234 | | bool BuildPresetsHelper(std::vector<cmCMakePresetsGraph::BuildPreset>& out, |
235 | | Json::Value const* value, cmJSONState* state); |
236 | | |
237 | | bool TestPresetsHelper(std::vector<cmCMakePresetsGraph::TestPreset>& out, |
238 | | Json::Value const* value, cmJSONState* state); |
239 | | |
240 | | bool PackagePresetsHelper(std::vector<cmCMakePresetsGraph::PackagePreset>& out, |
241 | | Json::Value const* value, cmJSONState* state); |
242 | | |
243 | | bool WorkflowPresetsHelper( |
244 | | std::vector<cmCMakePresetsGraph::WorkflowPreset>& out, |
245 | | Json::Value const* value, cmJSONState* state); |
246 | | |
247 | | cmJSONHelper<std::nullptr_t> VendorHelper(ErrorGenerator const& error); |
248 | | |
249 | | bool PresetConditionHelper( |
250 | | std::shared_ptr<cmCMakePresetsGraph::Condition>& out, |
251 | | Json::Value const* value, cmJSONState* state); |
252 | | |
253 | | bool PresetVectorOneOrMoreStringHelper(std::vector<std::string>& out, |
254 | | Json::Value const* value, |
255 | | cmJSONState* state); |
256 | | |
257 | | bool EnvironmentMapHelper( |
258 | | std::map<std::string, cm::optional<std::string>>& out, |
259 | | Json::Value const* value, cmJSONState* state); |
260 | | |
261 | | cmJSONHelper<std::nullptr_t> SchemaHelper(); |
262 | | } |