/src/CMake/Source/cmScanDepFormat.cxx
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 | | |
4 | | #include "cmScanDepFormat.h" |
5 | | |
6 | | #include <cctype> |
7 | | #include <cstdio> |
8 | | #include <utility> |
9 | | |
10 | | #include <cm/optional> |
11 | | #include <cm/string_view> |
12 | | #include <cmext/string_view> |
13 | | |
14 | | #include <cm3p/json/reader.h> |
15 | | #include <cm3p/json/value.h> |
16 | | #include <cm3p/json/writer.h> |
17 | | |
18 | | #include "cmsys/FStream.hxx" |
19 | | |
20 | | #include "cmGeneratedFileStream.h" |
21 | | #include "cmStringAlgorithms.h" |
22 | | #include "cmSystemTools.h" |
23 | | |
24 | | static bool ParseFilename(Json::Value const& val, std::string& result) |
25 | 0 | { |
26 | 0 | if (val.isString()) { |
27 | 0 | result = val.asString(); |
28 | 0 | } else { |
29 | 0 | return false; |
30 | 0 | } |
31 | | |
32 | 0 | return true; |
33 | 0 | } |
34 | | |
35 | | static Json::Value EncodeFilename(std::string const& path) |
36 | 0 | { |
37 | 0 | std::string data; |
38 | 0 | data.reserve(path.size()); |
39 | |
|
40 | 0 | for (auto const& byte : path) { |
41 | 0 | if (std::iscntrl(byte)) { |
42 | | // Control characters. |
43 | 0 | data.append("\\u"); |
44 | 0 | char buf[5]; |
45 | 0 | std::snprintf(buf, sizeof(buf), "%04x", byte); |
46 | 0 | data.append(buf); |
47 | 0 | } else if (byte == '"' || byte == '\\') { |
48 | | // Special JSON characters. |
49 | 0 | data.push_back('\\'); |
50 | 0 | data.push_back(byte); |
51 | 0 | } else { |
52 | | // Other data. |
53 | 0 | data.push_back(byte); |
54 | 0 | } |
55 | 0 | } |
56 | |
|
57 | 0 | return data; |
58 | 0 | } |
59 | | |
60 | | #define PARSE_BLOB(val, res) \ |
61 | 0 | do { \ |
62 | 0 | if (!ParseFilename(val, res)) { \ |
63 | 0 | cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ", \ |
64 | 0 | arg_pp, ": invalid blob")); \ |
65 | 0 | return false; \ |
66 | 0 | } \ |
67 | 0 | } while (0) |
68 | | |
69 | | #define PARSE_FILENAME(val, res) \ |
70 | 0 | do { \ |
71 | 0 | if (!ParseFilename(val, res)) { \ |
72 | 0 | cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ", \ |
73 | 0 | arg_pp, ": invalid filename")); \ |
74 | 0 | return false; \ |
75 | 0 | } \ |
76 | 0 | \ |
77 | 0 | if (work_directory && !work_directory->empty() && \ |
78 | 0 | !cmSystemTools::FileIsFullPath(res)) { \ |
79 | 0 | res = cmStrCat(*work_directory, '/', res); \ |
80 | 0 | } \ |
81 | 0 | } while (0) |
82 | | |
83 | | bool cmScanDepFormat_P1689_Parse(std::string const& arg_pp, |
84 | | cmScanDepInfo* info) |
85 | 0 | { |
86 | 0 | Json::Value ppio; |
87 | 0 | Json::Value const& ppi = ppio; |
88 | 0 | cmsys::ifstream ppf(arg_pp.c_str(), std::ios::in | std::ios::binary); |
89 | 0 | { |
90 | 0 | Json::Reader reader; |
91 | 0 | if (!reader.parse(ppf, ppio, false)) { |
92 | 0 | cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ", |
93 | 0 | arg_pp, |
94 | 0 | reader.getFormattedErrorMessages())); |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | 0 | Json::Value const& version = ppi["version"]; |
100 | 0 | if (version.asUInt() > 1) { |
101 | 0 | cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ", |
102 | 0 | arg_pp, ": version ", version.asString())); |
103 | 0 | return false; |
104 | 0 | } |
105 | | |
106 | 0 | Json::Value const& rules = ppi["rules"]; |
107 | 0 | if (rules.isArray()) { |
108 | 0 | if (rules.size() != 1) { |
109 | 0 | cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ", |
110 | 0 | arg_pp, ": expected 1 source entry")); |
111 | 0 | return false; |
112 | 0 | } |
113 | | |
114 | 0 | for (auto const& rule : rules) { |
115 | 0 | cm::optional<std::string> work_directory; |
116 | 0 | Json::Value const& workdir = rule["work-directory"]; |
117 | 0 | if (workdir.isString()) { |
118 | 0 | std::string wd; |
119 | 0 | PARSE_BLOB(workdir, wd); |
120 | 0 | work_directory = std::move(wd); |
121 | 0 | } else if (!workdir.isNull()) { |
122 | 0 | cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ", |
123 | 0 | arg_pp, |
124 | 0 | ": work-directory is not a string")); |
125 | 0 | return false; |
126 | 0 | } |
127 | | |
128 | 0 | if (rule.isMember("primary-output")) { |
129 | 0 | Json::Value const& primary_output = rule["primary-output"]; |
130 | 0 | PARSE_FILENAME(primary_output, info->PrimaryOutput); |
131 | 0 | } |
132 | | |
133 | 0 | if (rule.isMember("outputs")) { |
134 | 0 | Json::Value const& outputs = rule["outputs"]; |
135 | 0 | if (outputs.isArray()) { |
136 | 0 | for (auto const& output : outputs) { |
137 | 0 | std::string extra_output; |
138 | 0 | PARSE_FILENAME(output, extra_output); |
139 | | |
140 | 0 | info->ExtraOutputs.emplace_back(extra_output); |
141 | 0 | } |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | 0 | if (rule.isMember("provides")) { |
146 | 0 | Json::Value const& provides = rule["provides"]; |
147 | 0 | if (!provides.isArray()) { |
148 | 0 | cmSystemTools::Error( |
149 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
150 | 0 | ": provides is not an array")); |
151 | 0 | return false; |
152 | 0 | } |
153 | | |
154 | 0 | for (auto const& provide : provides) { |
155 | 0 | cmSourceReqInfo provide_info; |
156 | |
|
157 | 0 | Json::Value const& logical_name = provide["logical-name"]; |
158 | 0 | PARSE_BLOB(logical_name, provide_info.LogicalName); |
159 | | |
160 | 0 | if (provide.isMember("compiled-module-path")) { |
161 | 0 | Json::Value const& compiled_module_path = |
162 | 0 | provide["compiled-module-path"]; |
163 | 0 | PARSE_FILENAME(compiled_module_path, |
164 | 0 | provide_info.CompiledModulePath); |
165 | 0 | } |
166 | | |
167 | 0 | if (provide.isMember("unique-on-source-path")) { |
168 | 0 | Json::Value const& unique_on_source_path = |
169 | 0 | provide["unique-on-source-path"]; |
170 | 0 | if (!unique_on_source_path.isBool()) { |
171 | 0 | cmSystemTools::Error( |
172 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
173 | 0 | ": unique-on-source-path is not a boolean")); |
174 | 0 | return false; |
175 | 0 | } |
176 | 0 | provide_info.UseSourcePath = unique_on_source_path.asBool(); |
177 | 0 | } else { |
178 | 0 | provide_info.UseSourcePath = false; |
179 | 0 | } |
180 | | |
181 | 0 | if (provide.isMember("source-path")) { |
182 | 0 | Json::Value const& source_path = provide["source-path"]; |
183 | 0 | PARSE_FILENAME(source_path, provide_info.SourcePath); |
184 | 0 | } else if (provide_info.UseSourcePath) { |
185 | 0 | cmSystemTools::Error( |
186 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
187 | 0 | ": source-path is missing")); |
188 | 0 | return false; |
189 | 0 | } |
190 | | |
191 | 0 | if (provide.isMember("is-interface")) { |
192 | 0 | Json::Value const& is_interface = provide["is-interface"]; |
193 | 0 | if (!is_interface.isBool()) { |
194 | 0 | cmSystemTools::Error( |
195 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
196 | 0 | ": is-interface is not a boolean")); |
197 | 0 | return false; |
198 | 0 | } |
199 | 0 | provide_info.IsInterface = is_interface.asBool(); |
200 | 0 | } else { |
201 | 0 | provide_info.IsInterface = true; |
202 | 0 | } |
203 | | |
204 | 0 | info->Provides.push_back(provide_info); |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | 0 | if (rule.isMember("requires")) { |
209 | 0 | Json::Value const& reqs = rule["requires"]; |
210 | 0 | if (!reqs.isArray()) { |
211 | 0 | cmSystemTools::Error( |
212 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
213 | 0 | ": requires is not an array")); |
214 | 0 | return false; |
215 | 0 | } |
216 | | |
217 | 0 | for (auto const& require : reqs) { |
218 | 0 | cmSourceReqInfo require_info; |
219 | |
|
220 | 0 | Json::Value const& logical_name = require["logical-name"]; |
221 | 0 | PARSE_BLOB(logical_name, require_info.LogicalName); |
222 | | |
223 | 0 | if (require.isMember("compiled-module-path")) { |
224 | 0 | Json::Value const& compiled_module_path = |
225 | 0 | require["compiled-module-path"]; |
226 | 0 | PARSE_FILENAME(compiled_module_path, |
227 | 0 | require_info.CompiledModulePath); |
228 | 0 | } |
229 | | |
230 | 0 | if (require.isMember("unique-on-source-path")) { |
231 | 0 | Json::Value const& unique_on_source_path = |
232 | 0 | require["unique-on-source-path"]; |
233 | 0 | if (!unique_on_source_path.isBool()) { |
234 | 0 | cmSystemTools::Error( |
235 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
236 | 0 | ": unique-on-source-path is not a boolean")); |
237 | 0 | return false; |
238 | 0 | } |
239 | 0 | require_info.UseSourcePath = unique_on_source_path.asBool(); |
240 | 0 | } else { |
241 | 0 | require_info.UseSourcePath = false; |
242 | 0 | } |
243 | | |
244 | 0 | if (require.isMember("source-path")) { |
245 | 0 | Json::Value const& source_path = require["source-path"]; |
246 | 0 | PARSE_FILENAME(source_path, require_info.SourcePath); |
247 | 0 | } else if (require_info.UseSourcePath) { |
248 | 0 | cmSystemTools::Error( |
249 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
250 | 0 | ": source-path is missing")); |
251 | 0 | return false; |
252 | 0 | } |
253 | | |
254 | 0 | if (require.isMember("lookup-method")) { |
255 | 0 | Json::Value const& lookup_method = require["lookup-method"]; |
256 | 0 | if (!lookup_method.isString()) { |
257 | 0 | cmSystemTools::Error( |
258 | 0 | cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp, |
259 | 0 | ": lookup-method is not a string")); |
260 | 0 | return false; |
261 | 0 | } |
262 | | |
263 | 0 | std::string lookup_method_str = lookup_method.asString(); |
264 | 0 | if (lookup_method_str == "by-name"_s) { |
265 | 0 | require_info.Method = LookupMethod::ByName; |
266 | 0 | } else if (lookup_method_str == "include-angle"_s) { |
267 | 0 | require_info.Method = LookupMethod::IncludeAngle; |
268 | 0 | } else if (lookup_method_str == "include-quote"_s) { |
269 | 0 | require_info.Method = LookupMethod::IncludeQuote; |
270 | 0 | } else { |
271 | 0 | cmSystemTools::Error(cmStrCat( |
272 | 0 | "-E cmake_ninja_dyndep failed to parse ", arg_pp, |
273 | 0 | ": lookup-method is not a valid: ", lookup_method_str)); |
274 | 0 | return false; |
275 | 0 | } |
276 | 0 | } else if (require_info.UseSourcePath) { |
277 | 0 | require_info.Method = LookupMethod::ByName; |
278 | 0 | } |
279 | | |
280 | 0 | info->Requires.push_back(require_info); |
281 | 0 | } |
282 | 0 | } |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | 0 | return true; |
287 | 0 | } |
288 | | |
289 | | bool cmScanDepFormat_P1689_Write(std::string const& path, |
290 | | cmScanDepInfo const& info) |
291 | 0 | { |
292 | 0 | Json::Value ddi(Json::objectValue); |
293 | 0 | ddi["version"] = 0; |
294 | 0 | ddi["revision"] = 0; |
295 | |
|
296 | 0 | Json::Value& rules = ddi["rules"] = Json::arrayValue; |
297 | |
|
298 | 0 | Json::Value rule(Json::objectValue); |
299 | |
|
300 | 0 | rule["primary-output"] = EncodeFilename(info.PrimaryOutput); |
301 | |
|
302 | 0 | Json::Value& rule_outputs = rule["outputs"] = Json::arrayValue; |
303 | 0 | for (auto const& output : info.ExtraOutputs) { |
304 | 0 | rule_outputs.append(EncodeFilename(output)); |
305 | 0 | } |
306 | |
|
307 | 0 | Json::Value& provides = rule["provides"] = Json::arrayValue; |
308 | 0 | for (auto const& provide : info.Provides) { |
309 | 0 | Json::Value provide_obj(Json::objectValue); |
310 | 0 | auto const encoded = EncodeFilename(provide.LogicalName); |
311 | 0 | provide_obj["logical-name"] = encoded; |
312 | 0 | if (!provide.CompiledModulePath.empty()) { |
313 | 0 | provide_obj["compiled-module-path"] = |
314 | 0 | EncodeFilename(provide.CompiledModulePath); |
315 | 0 | } |
316 | |
|
317 | 0 | if (provide.UseSourcePath) { |
318 | 0 | provide_obj["unique-on-source-path"] = true; |
319 | 0 | provide_obj["source-path"] = EncodeFilename(provide.SourcePath); |
320 | 0 | } else if (!provide.SourcePath.empty()) { |
321 | 0 | provide_obj["source-path"] = EncodeFilename(provide.SourcePath); |
322 | 0 | } |
323 | |
|
324 | 0 | provide_obj["is-interface"] = provide.IsInterface; |
325 | |
|
326 | 0 | provides.append(provide_obj); |
327 | 0 | } |
328 | |
|
329 | 0 | Json::Value& reqs = rule["requires"] = Json::arrayValue; |
330 | 0 | for (auto const& require : info.Requires) { |
331 | 0 | Json::Value require_obj(Json::objectValue); |
332 | 0 | auto const encoded = EncodeFilename(require.LogicalName); |
333 | 0 | require_obj["logical-name"] = encoded; |
334 | 0 | if (!require.CompiledModulePath.empty()) { |
335 | 0 | require_obj["compiled-module-path"] = |
336 | 0 | EncodeFilename(require.CompiledModulePath); |
337 | 0 | } |
338 | |
|
339 | 0 | if (require.UseSourcePath) { |
340 | 0 | require_obj["unique-on-source-path"] = true; |
341 | 0 | require_obj["source-path"] = EncodeFilename(require.SourcePath); |
342 | 0 | } else if (!require.SourcePath.empty()) { |
343 | 0 | require_obj["source-path"] = EncodeFilename(require.SourcePath); |
344 | 0 | } |
345 | |
|
346 | 0 | char const* lookup_method = nullptr; |
347 | 0 | switch (require.Method) { |
348 | 0 | case LookupMethod::ByName: |
349 | | // No explicit value needed for the default. |
350 | 0 | break; |
351 | 0 | case LookupMethod::IncludeAngle: |
352 | 0 | lookup_method = "include-angle"; |
353 | 0 | break; |
354 | 0 | case LookupMethod::IncludeQuote: |
355 | 0 | lookup_method = "include-quote"; |
356 | 0 | break; |
357 | 0 | } |
358 | 0 | if (lookup_method) { |
359 | 0 | require_obj["lookup-method"] = lookup_method; |
360 | 0 | } |
361 | |
|
362 | 0 | reqs.append(require_obj); |
363 | 0 | } |
364 | | |
365 | 0 | rules.append(rule); |
366 | |
|
367 | 0 | cmGeneratedFileStream ddif(path); |
368 | 0 | ddif << ddi; |
369 | |
|
370 | 0 | return !!ddif; |
371 | 0 | } |