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