/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 <cm/optional> |
11 | | |
12 | | #include <cm3p/json/value.h> |
13 | | |
14 | | #include "cmCMakePresetsGraph.h" |
15 | | #include "cmJSONHelpers.h" |
16 | | #include "cmSystemTools.h" |
17 | | |
18 | | #define CHECK_OK(expr) \ |
19 | 0 | do { \ |
20 | 0 | auto _result = expr; \ |
21 | 0 | if (_result != true) \ |
22 | 0 | return _result; \ |
23 | 0 | } while (false) |
24 | | |
25 | | namespace cmCMakePresetsGraphInternal { |
26 | | enum class ExpandMacroResult |
27 | | { |
28 | | Ok, |
29 | | Ignore, |
30 | | Defer, |
31 | | Error, |
32 | | }; |
33 | | |
34 | | class MacroExpander |
35 | | { |
36 | | public: |
37 | | virtual ExpandMacroResult operator()(std::string const& macroNamespace, |
38 | | std::string const& macroName, |
39 | | std::string& macroOut, |
40 | | int version) const = 0; |
41 | 0 | virtual ~MacroExpander() = default; |
42 | | }; |
43 | | using MacroExpanderVector = std::vector<std::unique_ptr<MacroExpander>>; |
44 | | |
45 | | ExpandMacroResult ExpandMacros(std::string& out, |
46 | | MacroExpanderVector const& macroExpanders, |
47 | | int version); |
48 | | |
49 | | ExpandMacroResult ExpandMacro(std::string& out, |
50 | | std::string const& macroNamespace, |
51 | | std::string const& macroName, |
52 | | MacroExpanderVector const& macroExpanders, |
53 | | int version); |
54 | | } |
55 | | |
56 | | class cmCMakePresetsGraph::Condition |
57 | | { |
58 | | public: |
59 | 0 | virtual ~Condition() = default; |
60 | | |
61 | | virtual bool Evaluate( |
62 | | cmCMakePresetsGraphInternal::MacroExpanderVector const& expanders, |
63 | | int version, cm::optional<bool>& out) const = 0; |
64 | 0 | virtual bool IsNull() const { return false; } |
65 | | }; |
66 | | |
67 | | namespace cmCMakePresetsGraphInternal { |
68 | | class BaseMacroExpander : public MacroExpander |
69 | | { |
70 | | cmCMakePresetsGraph const& Graph; |
71 | | cm::optional<std::string> File; |
72 | | |
73 | | public: |
74 | | BaseMacroExpander(cmCMakePresetsGraph const& graph) |
75 | 0 | : Graph(graph) |
76 | 0 | { |
77 | 0 | } |
78 | | BaseMacroExpander(cmCMakePresetsGraph const& graph, std::string const& file) |
79 | 0 | : Graph(graph) |
80 | 0 | , File(file) |
81 | 0 | { |
82 | 0 | } |
83 | | ExpandMacroResult operator()(std::string const& macroNamespace, |
84 | | std::string const& macroName, |
85 | | std::string& macroOut, |
86 | | int version) const override; |
87 | | }; |
88 | | |
89 | | template <typename T> |
90 | | class PresetMacroExpander : public MacroExpander |
91 | | { |
92 | | cmCMakePresetsGraph const& Graph; |
93 | | T const& Preset; |
94 | | |
95 | | public: |
96 | | PresetMacroExpander(cmCMakePresetsGraph const& graph, T const& preset) |
97 | 0 | : Graph(graph) |
98 | 0 | , Preset(preset) |
99 | 0 | { |
100 | 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&) |
101 | | ExpandMacroResult operator()(std::string const& macroNamespace, |
102 | | std::string const& macroName, |
103 | | std::string& macroOut, |
104 | | int version) const override |
105 | 0 | { |
106 | 0 | if (macroNamespace.empty()) { |
107 | 0 | if (macroName == "presetName") { |
108 | 0 | macroOut += Preset.Name; |
109 | 0 | return ExpandMacroResult::Ok; |
110 | 0 | } |
111 | 0 | if (macroName == "generator") { |
112 | | // Generator only makes sense if preset is not hidden. |
113 | 0 | if (!Preset.Hidden) { |
114 | 0 | macroOut += Graph.GetGeneratorForPreset(Preset.Name); |
115 | 0 | } |
116 | 0 | return ExpandMacroResult::Ok; |
117 | 0 | } |
118 | 0 | if (macroName == "fileDir") { |
119 | 0 | if (version < 4) { |
120 | 0 | return ExpandMacroResult::Error; |
121 | 0 | } |
122 | 0 | macroOut += |
123 | 0 | cmSystemTools::GetParentDirectory(Preset.OriginFile->Filename); |
124 | 0 | return ExpandMacroResult::Ok; |
125 | 0 | } |
126 | 0 | } |
127 | 0 | return ExpandMacroResult::Ignore; |
128 | 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 |
129 | | }; |
130 | | |
131 | | template <class T> |
132 | | class ImmediateMacroExpander : public MacroExpander |
133 | | { |
134 | | T const& Preset; |
135 | | |
136 | | public: |
137 | | ImmediateMacroExpander(T const& preset) |
138 | 0 | : Preset(preset) |
139 | 0 | { |
140 | 0 | } Unexecuted instantiation: cmCMakePresetsGraphInternal::ImmediateMacroExpander<cmCMakePresetsGraph::ConfigurePreset>::ImmediateMacroExpander(cmCMakePresetsGraph::ConfigurePreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::ImmediateMacroExpander<cmCMakePresetsGraph::BuildPreset>::ImmediateMacroExpander(cmCMakePresetsGraph::BuildPreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::ImmediateMacroExpander<cmCMakePresetsGraph::TestPreset>::ImmediateMacroExpander(cmCMakePresetsGraph::TestPreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::ImmediateMacroExpander<cmCMakePresetsGraph::PackagePreset>::ImmediateMacroExpander(cmCMakePresetsGraph::PackagePreset const&) Unexecuted instantiation: cmCMakePresetsGraphInternal::ImmediateMacroExpander<cmCMakePresetsGraph::WorkflowPreset>::ImmediateMacroExpander(cmCMakePresetsGraph::WorkflowPreset const&) |
141 | | ExpandMacroResult operator()(std::string const& macroNamespace, |
142 | | std::string const& macroName, |
143 | | std::string& macroOut, |
144 | | int version) const override |
145 | 0 | { |
146 | 0 | if (macroNamespace.empty()) { |
147 | 0 | if (macroName == "fileDir") { |
148 | 0 | if (version < 12) { |
149 | 0 | return ExpandMacroResult::Defer; |
150 | 0 | } |
151 | 0 | macroOut += |
152 | 0 | cmSystemTools::GetParentDirectory(Preset.OriginFile->Filename); |
153 | 0 | return ExpandMacroResult::Ok; |
154 | 0 | } |
155 | 0 | } |
156 | 0 | return ExpandMacroResult::Defer; |
157 | 0 | } Unexecuted instantiation: cmCMakePresetsGraphInternal::ImmediateMacroExpander<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::ImmediateMacroExpander<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::ImmediateMacroExpander<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::ImmediateMacroExpander<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::ImmediateMacroExpander<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 |
158 | | }; |
159 | | |
160 | | template <typename T> |
161 | | bool ExpandImmediateMacros(T& preset); |
162 | | |
163 | | extern template bool ExpandImmediateMacros< |
164 | | cmCMakePresetsGraph::ConfigurePreset>(cmCMakePresetsGraph::ConfigurePreset&); |
165 | | extern template bool ExpandImmediateMacros<cmCMakePresetsGraph::BuildPreset>( |
166 | | cmCMakePresetsGraph::BuildPreset&); |
167 | | extern template bool ExpandImmediateMacros<cmCMakePresetsGraph::TestPreset>( |
168 | | cmCMakePresetsGraph::TestPreset&); |
169 | | extern template bool ExpandImmediateMacros<cmCMakePresetsGraph::PackagePreset>( |
170 | | cmCMakePresetsGraph::PackagePreset&); |
171 | | extern template bool ExpandImmediateMacros< |
172 | | cmCMakePresetsGraph::WorkflowPreset>(cmCMakePresetsGraph::WorkflowPreset&); |
173 | | |
174 | | class NullCondition : public cmCMakePresetsGraph::Condition |
175 | | { |
176 | | bool Evaluate(MacroExpanderVector const& /*expanders*/, int /*version*/, |
177 | | cm::optional<bool>& out) const override |
178 | 0 | { |
179 | 0 | out = true; |
180 | 0 | return true; |
181 | 0 | } |
182 | | |
183 | 0 | bool IsNull() const override { return true; } |
184 | | }; |
185 | | |
186 | | class ConstCondition : public cmCMakePresetsGraph::Condition |
187 | | { |
188 | | public: |
189 | | bool Evaluate(MacroExpanderVector const& /*expanders*/, int /*version*/, |
190 | | cm::optional<bool>& out) const override |
191 | 0 | { |
192 | 0 | out = this->Value; |
193 | 0 | return true; |
194 | 0 | } |
195 | | |
196 | | bool Value; |
197 | | }; |
198 | | |
199 | | class EqualsCondition : public cmCMakePresetsGraph::Condition |
200 | | { |
201 | | public: |
202 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
203 | | cm::optional<bool>& out) const override; |
204 | | |
205 | | std::string Lhs; |
206 | | std::string Rhs; |
207 | | }; |
208 | | |
209 | | class InListCondition : public cmCMakePresetsGraph::Condition |
210 | | { |
211 | | public: |
212 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
213 | | cm::optional<bool>& out) const override; |
214 | | |
215 | | std::string String; |
216 | | std::vector<std::string> List; |
217 | | }; |
218 | | |
219 | | class MatchesCondition : public cmCMakePresetsGraph::Condition |
220 | | { |
221 | | public: |
222 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
223 | | cm::optional<bool>& out) const override; |
224 | | |
225 | | std::string String; |
226 | | std::string Regex; |
227 | | }; |
228 | | |
229 | | class AnyAllOfCondition : public cmCMakePresetsGraph::Condition |
230 | | { |
231 | | public: |
232 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
233 | | cm::optional<bool>& out) const override; |
234 | | |
235 | | std::vector<std::unique_ptr<Condition>> Conditions; |
236 | | bool StopValue; |
237 | | }; |
238 | | |
239 | | class NotCondition : public cmCMakePresetsGraph::Condition |
240 | | { |
241 | | public: |
242 | | bool Evaluate(MacroExpanderVector const& expanders, int version, |
243 | | cm::optional<bool>& out) const override; |
244 | | |
245 | | std::unique_ptr<Condition> SubCondition; |
246 | | }; |
247 | | |
248 | | bool PresetStringHelper(std::string& out, Json::Value const* value, |
249 | | cmJSONState* state); |
250 | | |
251 | | bool PresetNameHelper(std::string& out, Json::Value const* value, |
252 | | cmJSONState* state); |
253 | | |
254 | | bool PresetVectorStringHelper(std::vector<std::string>& out, |
255 | | Json::Value const* value, cmJSONState* state); |
256 | | |
257 | | bool PresetBoolHelper(bool& out, Json::Value const* value, cmJSONState* state); |
258 | | |
259 | | bool PresetOptionalBoolHelper(cm::optional<bool>& out, |
260 | | Json::Value const* value, cmJSONState* state); |
261 | | |
262 | | template <typename K> |
263 | | bool PresetMapToBoolHelper(std::map<K, bool>& out, Json::Value const* value, |
264 | | K key, cmJSONState* state) |
265 | 0 | { |
266 | 0 | cm::optional<bool> temp; |
267 | 0 | if (!PresetOptionalBoolHelper(temp, value, state)) { |
268 | 0 | return false; |
269 | 0 | } |
270 | | |
271 | 0 | if (temp) { |
272 | 0 | out[key] = *temp; |
273 | 0 | } |
274 | |
|
275 | 0 | return true; |
276 | 0 | } |
277 | | |
278 | | bool PresetIntHelper(int& out, Json::Value const* value, cmJSONState* state); |
279 | | |
280 | | bool PresetOptionalIntHelper(cm::optional<int>& out, Json::Value const* value, |
281 | | cmJSONState* state); |
282 | | |
283 | | bool PresetUIntHelper(unsigned int& out, Json::Value const* value, |
284 | | cmJSONState* state); |
285 | | |
286 | | bool PresetOptionalUIntHelper(cm::optional<unsigned int>& out, |
287 | | Json::Value const* value, cmJSONState* state); |
288 | | |
289 | | bool PresetVectorIntHelper(std::vector<int>& out, Json::Value const* value, |
290 | | cmJSONState* state); |
291 | | |
292 | | bool ConfigurePresetsHelper( |
293 | | std::vector<cmCMakePresetsGraph::ConfigurePreset>& out, |
294 | | Json::Value const* value, cmJSONState* state); |
295 | | |
296 | | bool BuildPresetsHelper(std::vector<cmCMakePresetsGraph::BuildPreset>& out, |
297 | | Json::Value const* value, cmJSONState* state); |
298 | | |
299 | | bool TestPresetsHelper(std::vector<cmCMakePresetsGraph::TestPreset>& out, |
300 | | Json::Value const* value, cmJSONState* state); |
301 | | |
302 | | bool PackagePresetsHelper(std::vector<cmCMakePresetsGraph::PackagePreset>& out, |
303 | | Json::Value const* value, cmJSONState* state); |
304 | | |
305 | | bool WorkflowPresetsHelper( |
306 | | std::vector<cmCMakePresetsGraph::WorkflowPreset>& out, |
307 | | Json::Value const* value, cmJSONState* state); |
308 | | |
309 | | cmJSONHelper<std::nullptr_t> VendorHelper(ErrorGenerator const& error); |
310 | | |
311 | | bool PresetConditionHelper( |
312 | | std::shared_ptr<cmCMakePresetsGraph::Condition>& out, |
313 | | Json::Value const* value, cmJSONState* state); |
314 | | |
315 | | bool PresetVectorOneOrMoreStringHelper(std::vector<std::string>& out, |
316 | | Json::Value const* value, |
317 | | cmJSONState* state); |
318 | | |
319 | | bool EnvironmentMapHelper( |
320 | | std::map<std::string, cm::optional<std::string>>& out, |
321 | | Json::Value const* value, cmJSONState* state); |
322 | | |
323 | | cmJSONHelper<std::nullptr_t> SchemaHelper(); |
324 | | |
325 | | bool CheckDiagnostics(cmJSONState* state, int version, |
326 | | cmCMakePresetsGraph::ConfigurePreset& preset); |
327 | | } |