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