Coverage Report

Created: 2026-09-14 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 <cstdio>
7
#include <utility>
8
9
#include <cm/optional>
10
#include <cm/string_view>
11
#include <cmext/string_view>
12
13
#include <cm3p/json/value.h>
14
#include <cm3p/json/writer.h>
15
16
#include "cmsys/FStream.hxx"
17
#include "cmsys/String.h"
18
19
#include "cmGeneratedFileStream.h"
20
#include "cmJSONState.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 (cmsysString_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
  {
89
0
    cmJSONState parseState(arg_pp, &ppio);
90
0
    if (!parseState.errors.empty()) {
91
0
      cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
92
0
                                    arg_pp, parseState.GetErrorMessage()));
93
0
      return false;
94
0
    }
95
0
  }
96
97
0
  Json::Value const& version = ppi["version"];
98
0
  if (version.asUInt() > 1) {
99
0
    cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
100
0
                                  arg_pp, ": version ", version.asString()));
101
0
    return false;
102
0
  }
103
104
0
  Json::Value const& rules = ppi["rules"];
105
0
  if (rules.isArray()) {
106
0
    if (rules.size() != 1) {
107
0
      cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
108
0
                                    arg_pp, ": expected 1 source entry"));
109
0
      return false;
110
0
    }
111
112
0
    for (auto const& rule : rules) {
113
0
      cm::optional<std::string> work_directory;
114
0
      Json::Value const& workdir = rule["work-directory"];
115
0
      if (workdir.isString()) {
116
0
        std::string wd;
117
0
        PARSE_BLOB(workdir, wd);
118
0
        work_directory = std::move(wd);
119
0
      } else if (!workdir.isNull()) {
120
0
        cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
121
0
                                      arg_pp,
122
0
                                      ": work-directory is not a string"));
123
0
        return false;
124
0
      }
125
126
0
      if (rule.isMember("primary-output")) {
127
0
        Json::Value const& primary_output = rule["primary-output"];
128
0
        PARSE_FILENAME(primary_output, info->PrimaryOutput);
129
0
      }
130
131
0
      if (rule.isMember("outputs")) {
132
0
        Json::Value const& outputs = rule["outputs"];
133
0
        if (outputs.isArray()) {
134
0
          for (auto const& output : outputs) {
135
0
            std::string extra_output;
136
0
            PARSE_FILENAME(output, extra_output);
137
138
0
            info->ExtraOutputs.emplace_back(extra_output);
139
0
          }
140
0
        }
141
0
      }
142
143
0
      if (rule.isMember("provides")) {
144
0
        Json::Value const& provides = rule["provides"];
145
0
        if (!provides.isArray()) {
146
0
          cmSystemTools::Error(
147
0
            cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
148
0
                     ": provides is not an array"));
149
0
          return false;
150
0
        }
151
152
0
        for (auto const& provide : provides) {
153
0
          cmSourceReqInfo provide_info;
154
155
0
          Json::Value const& logical_name = provide["logical-name"];
156
0
          PARSE_BLOB(logical_name, provide_info.LogicalName);
157
158
0
          if (provide.isMember("compiled-module-path")) {
159
0
            Json::Value const& compiled_module_path =
160
0
              provide["compiled-module-path"];
161
0
            PARSE_FILENAME(compiled_module_path,
162
0
                           provide_info.CompiledModulePath);
163
0
          }
164
165
0
          if (provide.isMember("unique-on-source-path")) {
166
0
            Json::Value const& unique_on_source_path =
167
0
              provide["unique-on-source-path"];
168
0
            if (!unique_on_source_path.isBool()) {
169
0
              cmSystemTools::Error(
170
0
                cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
171
0
                         ": unique-on-source-path is not a boolean"));
172
0
              return false;
173
0
            }
174
0
            provide_info.UseSourcePath = unique_on_source_path.asBool();
175
0
          } else {
176
0
            provide_info.UseSourcePath = false;
177
0
          }
178
179
0
          if (provide.isMember("source-path")) {
180
0
            Json::Value const& source_path = provide["source-path"];
181
0
            PARSE_FILENAME(source_path, provide_info.SourcePath);
182
0
          } else if (provide_info.UseSourcePath) {
183
0
            cmSystemTools::Error(
184
0
              cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
185
0
                       ": source-path is missing"));
186
0
            return false;
187
0
          }
188
189
0
          if (provide.isMember("is-interface")) {
190
0
            Json::Value const& is_interface = provide["is-interface"];
191
0
            if (!is_interface.isBool()) {
192
0
              cmSystemTools::Error(
193
0
                cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
194
0
                         ": is-interface is not a boolean"));
195
0
              return false;
196
0
            }
197
0
            provide_info.IsInterface = is_interface.asBool();
198
0
          } else {
199
0
            provide_info.IsInterface = true;
200
0
          }
201
202
0
          info->Provides.push_back(provide_info);
203
0
        }
204
0
      }
205
206
0
      if (rule.isMember("requires")) {
207
0
        Json::Value const& reqs = rule["requires"];
208
0
        if (!reqs.isArray()) {
209
0
          cmSystemTools::Error(
210
0
            cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
211
0
                     ": requires is not an array"));
212
0
          return false;
213
0
        }
214
215
0
        for (auto const& require : reqs) {
216
0
          cmSourceReqInfo require_info;
217
218
0
          Json::Value const& logical_name = require["logical-name"];
219
0
          PARSE_BLOB(logical_name, require_info.LogicalName);
220
221
0
          if (require.isMember("compiled-module-path")) {
222
0
            Json::Value const& compiled_module_path =
223
0
              require["compiled-module-path"];
224
0
            PARSE_FILENAME(compiled_module_path,
225
0
                           require_info.CompiledModulePath);
226
0
          }
227
228
0
          if (require.isMember("unique-on-source-path")) {
229
0
            Json::Value const& unique_on_source_path =
230
0
              require["unique-on-source-path"];
231
0
            if (!unique_on_source_path.isBool()) {
232
0
              cmSystemTools::Error(
233
0
                cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
234
0
                         ": unique-on-source-path is not a boolean"));
235
0
              return false;
236
0
            }
237
0
            require_info.UseSourcePath = unique_on_source_path.asBool();
238
0
          } else {
239
0
            require_info.UseSourcePath = false;
240
0
          }
241
242
0
          if (require.isMember("source-path")) {
243
0
            Json::Value const& source_path = require["source-path"];
244
0
            PARSE_FILENAME(source_path, require_info.SourcePath);
245
0
          } else if (require_info.UseSourcePath) {
246
0
            cmSystemTools::Error(
247
0
              cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
248
0
                       ": source-path is missing"));
249
0
            return false;
250
0
          }
251
252
0
          if (require.isMember("lookup-method")) {
253
0
            Json::Value const& lookup_method = require["lookup-method"];
254
0
            if (!lookup_method.isString()) {
255
0
              cmSystemTools::Error(
256
0
                cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
257
0
                         ": lookup-method is not a string"));
258
0
              return false;
259
0
            }
260
261
0
            std::string lookup_method_str = lookup_method.asString();
262
0
            if (lookup_method_str == "by-name"_s) {
263
0
              require_info.Method = LookupMethod::ByName;
264
0
            } else if (lookup_method_str == "include-angle"_s) {
265
0
              require_info.Method = LookupMethod::IncludeAngle;
266
0
            } else if (lookup_method_str == "include-quote"_s) {
267
0
              require_info.Method = LookupMethod::IncludeQuote;
268
0
            } else {
269
0
              cmSystemTools::Error(cmStrCat(
270
0
                "-E cmake_ninja_dyndep failed to parse ", arg_pp,
271
0
                ": lookup-method is not a valid: ", lookup_method_str));
272
0
              return false;
273
0
            }
274
0
          } else if (require_info.UseSourcePath) {
275
0
            require_info.Method = LookupMethod::ByName;
276
0
          }
277
278
0
          info->Requires.push_back(require_info);
279
0
        }
280
0
      }
281
0
    }
282
0
  }
283
284
0
  return true;
285
0
}
286
287
bool cmScanDepFormat_P1689_Write(std::string const& path,
288
                                 cmScanDepInfo const& info)
289
0
{
290
0
  Json::Value ddi(Json::objectValue);
291
0
  ddi["version"] = 0;
292
0
  ddi["revision"] = 0;
293
294
0
  Json::Value& rules = ddi["rules"] = Json::arrayValue;
295
296
0
  Json::Value rule(Json::objectValue);
297
298
0
  rule["primary-output"] = EncodeFilename(info.PrimaryOutput);
299
300
0
  Json::Value& rule_outputs = rule["outputs"] = Json::arrayValue;
301
0
  for (auto const& output : info.ExtraOutputs) {
302
0
    rule_outputs.append(EncodeFilename(output));
303
0
  }
304
305
0
  Json::Value& provides = rule["provides"] = Json::arrayValue;
306
0
  for (auto const& provide : info.Provides) {
307
0
    Json::Value provide_obj(Json::objectValue);
308
0
    auto const encoded = EncodeFilename(provide.LogicalName);
309
0
    provide_obj["logical-name"] = encoded;
310
0
    if (!provide.CompiledModulePath.empty()) {
311
0
      provide_obj["compiled-module-path"] =
312
0
        EncodeFilename(provide.CompiledModulePath);
313
0
    }
314
315
0
    if (provide.UseSourcePath) {
316
0
      provide_obj["unique-on-source-path"] = true;
317
0
      provide_obj["source-path"] = EncodeFilename(provide.SourcePath);
318
0
    } else if (!provide.SourcePath.empty()) {
319
0
      provide_obj["source-path"] = EncodeFilename(provide.SourcePath);
320
0
    }
321
322
0
    provide_obj["is-interface"] = provide.IsInterface;
323
324
0
    provides.append(provide_obj);
325
0
  }
326
327
0
  Json::Value& reqs = rule["requires"] = Json::arrayValue;
328
0
  for (auto const& require : info.Requires) {
329
0
    Json::Value require_obj(Json::objectValue);
330
0
    auto const encoded = EncodeFilename(require.LogicalName);
331
0
    require_obj["logical-name"] = encoded;
332
0
    if (!require.CompiledModulePath.empty()) {
333
0
      require_obj["compiled-module-path"] =
334
0
        EncodeFilename(require.CompiledModulePath);
335
0
    }
336
337
0
    if (require.UseSourcePath) {
338
0
      require_obj["unique-on-source-path"] = true;
339
0
      require_obj["source-path"] = EncodeFilename(require.SourcePath);
340
0
    } else if (!require.SourcePath.empty()) {
341
0
      require_obj["source-path"] = EncodeFilename(require.SourcePath);
342
0
    }
343
344
0
    char const* lookup_method = nullptr;
345
0
    switch (require.Method) {
346
0
      case LookupMethod::ByName:
347
        // No explicit value needed for the default.
348
0
        break;
349
0
      case LookupMethod::IncludeAngle:
350
0
        lookup_method = "include-angle";
351
0
        break;
352
0
      case LookupMethod::IncludeQuote:
353
0
        lookup_method = "include-quote";
354
0
        break;
355
0
    }
356
0
    if (lookup_method) {
357
0
      require_obj["lookup-method"] = lookup_method;
358
0
    }
359
360
0
    reqs.append(require_obj);
361
0
  }
362
363
0
  rules.append(rule);
364
365
0
  cmGeneratedFileStream ddif(path);
366
0
  ddif << ddi;
367
368
0
  return !!ddif;
369
0
}