-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbuild_database.cppm
More file actions
616 lines (569 loc) · 29.1 KB
/
Copy pathbuild_database.cppm
File metadata and controls
616 lines (569 loc) · 29.1 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// mcpp.build.build_database — the plan, rendered as an S1 build database.
//
// S1 ("C++ Build Database: IDE Profile", profile 0.2.0) is a profile of the
// WG21 P2977R2 build database format; docs/specs/build-database.md (SPEC-005)
// states what mcpp guarantees as its producer. The document is a projection of
// what prepare_build planned: every field is read from a BuildContext, and
// nothing in this module compiles, runs or writes anything.
//
// Three rules shape the projection, and each one is a statement about the
// engine rather than about the format:
//
// - `arguments` come from the record the compile database renders
// (mcpp.build.compile_commands::unit_invocations), so the two databases
// cannot list different command lines for one unit.
// - `visible-sets` is every other set of the planned member. The engine
// resolves imports over one graph per invocation; a narrower closure would
// describe a rule the build does not enforce.
// - The standard library modules are translation units of their own set,
// carrying the command mcpp runs to build them. That is one rule for every
// toolchain and for a package that ships its own `std.cppm`, and S1 lets
// units outrank a module manifest.
export module mcpp.build.build_database;
import std;
import mcpp.build.build_program;
import mcpp.build.compile_commands;
import mcpp.build.flags;
import mcpp.build.plan;
import mcpp.build.prepare;
import mcpp.home;
import mcpp.libs.json;
import mcpp.manifest;
import mcpp.modgraph.graph;
import mcpp.modgraph.scanner;
import mcpp.platform;
import mcpp.toolchain.fingerprint;
import mcpp.toolchain.linkmodel;
import mcpp.toolchain.model;
import mcpp.toolchain.stdmod;
export namespace mcpp::build::database {
// 0.3.0: a set carries `generated` (S1 section 7.2, mcpp#724).
inline constexpr std::string_view kProfileVersion = "0.3.0";
inline constexpr std::string_view kStdSetName = "mcpp:std";
// One planned member of the document.
struct Member {
const BuildContext* ctx = nullptr;
// Prepended to every set name: empty for a single package, `<member>/` for
// a member of a workspace document.
std::string setPrefix;
// Where the planning pass wrote (BuildOverrides::work_dir): the build
// programs' caches, and so their declared inputs, are under it.
std::filesystem::path workDir;
// Test discovery (`[test] discover`, relative to `testRoot`): a new file
// matching it is a new test target, and so a new unit in the document.
std::filesystem::path testRoot;
std::vector<std::string> testDiscover;
};
struct Rendered {
nlohmann::json database;
// The same units as a JSON Compilation Database: the entries `mcpp build
// --configure-only` writes to compile_commands.json, from the same record.
nlohmann::json compileCommands = nlohmann::json::array();
std::vector<std::string> watch;
std::string inputsFingerprint;
// Conditions found while rendering, at the severity the command reports
// them: most are warnings the document is still complete despite, and a
// failed build program (#699 item 2, E3) is an error whose `path` names
// its `build.mcpp`, rewritten here to the workspace-relative form.
std::vector<PlanNote> notes;
};
// The S1 document for `members`, and the inputs whose change changes it.
// `failedMemberRoots` is every selected workspace member whose planning
// failed (#699 item 1, E1): it contributes no set, but its `mcpp.toml` and
// `build.mcpp` (when present) still join `watch`, exactly as a planned
// member's do — fixing either file is what should make a consumer ask again.
// `workspaceRoot` anchors the relative `watch` patterns; `selector` is the
// command's own selection (target, toolchain, profile, members), which enters
// the fingerprint because the same files answer differently under another one.
Rendered render(std::span<const Member> members,
std::span<const std::filesystem::path> failedMemberRoots,
const std::filesystem::path& workspaceRoot,
std::string_view selector);
// ── Exported for tests: each is a rule SPEC-005 states. ──────────────────
// The S1 role of a declaration form (S1 §8.2).
std::string_view role_name(mcpp::modgraph::ModuleDeclaration declaration);
// The S1 `stdlib.name` for mcpp's standard library identifier.
std::string_view stdlib_name(std::string_view stdlibId);
// `<family>-<version>-<target triple>`, with mcpp's family name (`llvm`, as in
// `[email protected]`) and the triple as the compiler spells it: stable within a
// document, and opaque to a consumer (S1 section 5.1).
std::string toolchain_id(const mcpp::toolchain::Toolchain& tc,
std::string_view compilerTriple);
// The standard-library units' recovery (mcpp.build.plan::recover_invocation)
// now runs once, in prepare.cppm, onto BuildPlan::stdModuleUnits (design
// 2026-09-26 §3.5, D5a/D5b: the plan carries the standard-library
// description, so this document and compile_commands.json render the exact
// same record and cannot disagree, P1). Re-exported here under the names
// this file's tests use.
using Invocation = mcpp::build::RecoveredInvocation;
using mcpp::build::recover_invocation;
using mcpp::build::split_command_words;
} // namespace mcpp::build::database
namespace mcpp::build::database {
namespace {
std::string native_string(const std::filesystem::path& p) {
auto n = p;
n.make_preferred();
return n.string();
}
std::string qualified_name(const mcpp::manifest::Manifest& m) {
return m.package.namespace_.empty()
? m.package.name
: m.package.namespace_ + "." + m.package.name;
}
bool names_path(std::string_view word, const std::filesystem::path& path,
const std::filesystem::path& cwd) {
std::filesystem::path w{std::string(word)};
const auto want = path.lexically_normal();
if (w.lexically_normal() == want) return true;
if (!w.is_absolute() && !cwd.empty() && (cwd / w).lexically_normal() == want)
return true;
return false;
}
// S1 `config-files`: the configuration files the driver reads without being
// named on a command line. A clang driver reads the `.cfg` beside it unless a
// unit passes `--no-default-config`, which mcpp does whenever that file exists
// (mcpp.toolchain.linkmodel::resolve_clang_driver). A GCC driver reads the
// `specs` file in its library directory for its own machine and version, which
// a distribution may spell with the major version only. Found from the layout
// the driver searches; no driver is run.
nlohmann::json config_files(const mcpp::toolchain::Toolchain& tc,
const std::vector<mcpp::build::UnitInvocation>& invocations) {
nlohmann::json out = nlohmann::json::array();
using C = mcpp::toolchain::CompilerId;
std::error_code ec;
if (tc.compiler == C::Clang) {
const auto dm = mcpp::toolchain::resolve_clang_driver(tc);
const bool read = std::ranges::any_of(invocations, [](auto const& inv) {
return std::ranges::find(inv.arguments, "--no-default-config") == inv.arguments.end();
});
if (dm.hasCfg && read) out.push_back(native_string(dm.cfgPath));
} else if (tc.compiler == C::GCC && !tc.targetTriple.empty()) {
const auto machine = tc.binaryPath.parent_path().parent_path()
/ "lib" / "gcc" / tc.targetTriple;
const auto major = tc.version.substr(0, tc.version.find('.'));
for (auto const& version : {tc.version, major}) {
const auto specs = machine / version / "specs";
if (!version.empty() && std::filesystem::is_regular_file(specs, ec)) {
out.push_back(native_string(specs));
break;
}
}
}
return out;
}
// S1 `baseline-arguments` and `local-arguments`. A unit's arguments are its
// driver, then the set's baseline, then its local arguments, then its own
// trailing `-c <source> -o <object>` when it has one: the baseline is the longest
// prefix that every unit of the set shares after the driver and without that
// tail. A prefix keeps the order of the arguments, which decides include search
// and macro definitions; a common subset would not.
void split_baseline(nlohmann::json& set) {
std::vector<std::vector<std::string>> semantic;
for (auto const& u : set["translation-units"]) {
auto args = u["arguments"].get<std::vector<std::string>>();
std::vector<std::string> rest(args.size() > 1 ? args.begin() + 1 : args.end(), args.end());
const std::filesystem::path cwd{u["work-directory"].get<std::string>()};
const auto n = rest.size();
if (n >= 4 && rest[n - 4] == "-c" && rest[n - 2] == "-o"
&& names_path(rest[n - 3], u["source"].get<std::string>(), cwd)
&& names_path(rest[n - 1], u["object"].get<std::string>(), cwd))
rest.resize(n - 4);
semantic.push_back(std::move(rest));
}
std::vector<std::string> baseline;
if (!semantic.empty()) {
baseline = semantic.front();
for (auto const& unit : semantic) {
std::size_t k = 0;
while (k < baseline.size() && k < unit.size() && baseline[k] == unit[k]) ++k;
baseline.resize(k);
}
}
std::size_t i = 0;
for (auto& u : set["translation-units"]) {
const auto& unit = semantic[i++];
u["local-arguments"] = std::vector<std::string>(
unit.begin() + static_cast<std::ptrdiff_t>(baseline.size()), unit.end());
}
set["baseline-arguments"] = std::move(baseline);
}
// D5b, S1 §6: "the compiler's build revision ... equal versions do not imply
// compatible BMIs" (S1-11.2-3 gates the authoritative reuse of a build BMI on
// this value matching exactly). mcpp already computes it, as one field of the
// toolchain fingerprint (mcpp.toolchain.fingerprint::compute_fingerprint,
// field 3): the compiler's normalized `--version` banner when the toolchain
// probe recorded one (`Toolchain::driverIdent`), which changes with a build
// commit even when the reported version string does not, and a hash of the
// driver binary otherwise. Deriving build-id from the SAME computation,
// rather than a second one, is what keeps the two stable together and each
// stable across two runs of one toolchain.
std::string build_id(const mcpp::toolchain::Toolchain& tc) {
return !tc.driverIdent.empty()
? mcpp::toolchain::hash_string(tc.driverIdent)
: (tc.binaryPath.empty() ? std::string{}
: mcpp::toolchain::hash_file(tc.binaryPath));
}
nlohmann::json toolchain_json(const mcpp::toolchain::Toolchain& tc,
std::string_view compilerTriple,
const std::vector<mcpp::build::UnitInvocation>& invocations) {
nlohmann::json j{
{"family", std::string(tc.compiler_name())},
{"version", tc.version},
{"driver", tc.binaryPath.string()},
{"target", std::string(compilerTriple)},
{"config-files", config_files(tc, invocations)},
};
if (!tc.sysroot.empty()) j["sysroot"] = native_string(tc.sysroot);
if (!tc.stdlibId.empty()) {
nlohmann::json stdlib{{"name", std::string(stdlib_name(tc.stdlibId))}};
if (!tc.stdlibVersion.empty()) stdlib["version"] = tc.stdlibVersion;
j["stdlib"] = std::move(stdlib);
}
if (auto id = build_id(tc); !id.empty()) j["build-id"] = id;
return j;
}
std::string target_kind(const mcpp::manifest::Manifest& m) {
bool library = false, program = false;
for (auto const& t : m.targets) {
using K = mcpp::manifest::Target::Kind;
if (t.kind == K::Library || t.kind == K::SharedLibrary) library = true;
if (t.kind == K::Binary || t.kind == K::Application) program = true;
}
return library ? "library" : program ? "executable" : "other";
}
// The `watch` spelling of a path: relative to the workspace root, with `/`, when
// it is under it; absolute and native otherwise (S2 accepts no absolute glob).
std::optional<std::string> relative_to(const std::filesystem::path& p,
const std::filesystem::path& root) {
auto rel = p.lexically_normal().lexically_relative(root.lexically_normal());
if (rel.empty()) return std::nullopt;
auto first = rel.begin();
if (first != rel.end() && *first == "..") return std::nullopt;
auto s = rel.generic_string();
return s == "." ? std::string{} : s;
}
struct SetData {
std::string familyName;
std::string kind;
nlohmann::json units = nlohmann::json::array();
// S1 section 7.2: the files this plan generates and the set's units
// compile or include, and the generated include directories they name.
nlohmann::json generated = nlohmann::json::array();
std::set<std::string> generatedDirs;
};
} // namespace
std::string_view role_name(mcpp::modgraph::ModuleDeclaration declaration) {
using D = mcpp::modgraph::ModuleDeclaration;
switch (declaration) {
case D::None: return "non-module";
case D::Interface: return "module-interface";
case D::InterfacePartition: return "module-partition-interface";
case D::ImplementationPartition: return "module-partition-implementation";
case D::Implementation: return "module-implementation";
case D::Unknown: return "unknown";
}
return "unknown";
}
std::string_view stdlib_name(std::string_view stdlibId) {
if (stdlibId == "libstdc++") return "libstdc++";
if (stdlibId == "libc++") return "libc++";
if (stdlibId.starts_with("msvc")) return "msvc-stl";
return "other";
}
std::string toolchain_id(const mcpp::toolchain::Toolchain& tc,
std::string_view compilerTriple) {
return std::format("{}-{}-{}", tc.compiler_family(), tc.version, compilerTriple);
}
Rendered render(std::span<const Member> members,
std::span<const std::filesystem::path> failedMemberRoots,
const std::filesystem::path& workspaceRoot,
std::string_view selector) {
Rendered r;
nlohmann::json toolchains = nlohmann::json::object();
nlohmann::json sets = nlohmann::json::array();
std::set<std::string> watch;
std::set<std::filesystem::path> inputFiles;
auto watch_file = [&](const std::filesystem::path& p) {
const auto normal = p.lexically_normal();
std::error_code ec;
if (std::filesystem::is_regular_file(normal, ec)) inputFiles.insert(normal);
if (auto rel = relative_to(normal, workspaceRoot); rel && !rel->empty())
watch.insert(*rel);
else
watch.insert(native_string(normal));
};
auto watch_glob = [&](const std::filesystem::path& root, std::string_view glob) {
if (glob.empty() || glob.starts_with('!')) return;
const auto matches = mcpp::modgraph::expand_glob(root, glob);
for (auto const& f : matches) inputFiles.insert(f.lexically_normal());
if (auto rel = relative_to(root, workspaceRoot)) {
watch.insert(rel->empty() ? std::string(glob)
: *rel + "/" + std::string(glob));
} else {
for (auto const& f : matches) watch.insert(native_string(f.lexically_normal()));
}
};
watch_file(workspaceRoot / "mcpp.toml");
watch_file(mcpp::home::root() / "config.toml");
for (auto const& member : members) {
const auto& ctx = *member.ctx;
// The triple as the compiler spells it (`x86_64-pc-windows-msvc`),
// which is what S1 asks for; mcpp's own spelling when none was resolved.
const std::string compilerTriple = ctx.plan.targetSide.llvmTriple.empty()
? ctx.tc.targetTriple : ctx.plan.targetSide.llvmTriple;
const auto tcId = toolchain_id(ctx.tc, compilerTriple);
const auto rootName = qualified_name(ctx.manifest);
std::set<std::filesystem::path> testSources;
for (auto const& t : ctx.manifest.targets) {
if (t.kind != mcpp::manifest::Target::TestBinary || t.main.empty()) continue;
std::filesystem::path main{t.main};
testSources.insert((main.is_absolute() ? main : ctx.projectRoot / main)
.lexically_normal());
}
std::vector<std::string> order;
std::map<std::string, SetData> groups;
auto set_for = [&](const std::string& name, const std::string& family,
const std::string& kind) -> SetData& {
auto [it, inserted] = groups.try_emplace(name);
if (inserted) {
order.push_back(name);
it->second.familyName = family;
it->second.kind = kind;
}
return it->second;
};
// GENERATED FILES (S1 0.3.0 section 7.2, mcpp#724). A planning pass
// runs no action (SPEC-005 R2.5), so a file a rule generates is absent
// from the directory this document names until a build writes it. The
// plan knows each generating step; the document states it, together
// with the path the build of the same configuration writes, so that a
// reader can tell "not built yet" from "missing" without knowing how
// the planning directory maps onto the project's own `target/`.
const auto planRoot = member.workDir.empty() ? ctx.projectRoot : member.workDir;
const auto generatedTree = (planRoot / "target" / ".build-mcpp").lexically_normal();
auto build_path = [&](const std::filesystem::path& p) {
if (!member.workDir.empty())
if (auto rel = relative_to(p, member.workDir); rel && !rel->empty())
return native_string(ctx.projectRoot / std::filesystem::path(*rel));
return native_string(p);
};
const auto flags = mcpp::build::compute_flags(ctx.plan);
auto invocations = mcpp::build::unit_invocations(ctx.plan, flags);
if (!toolchains.contains(tcId))
toolchains[tcId] = toolchain_json(ctx.tc, compilerTriple, invocations);
for (auto& inv : invocations) {
const auto& cu = *inv.unit;
const bool isTest = testSources.contains(cu.source.lexically_normal());
const std::string package = cu.packageName.empty() ? rootName : cu.packageName;
const std::string kind = isTest ? "test"
: package == rootName ? target_kind(ctx.manifest)
: "library";
auto& set = set_for(member.setPrefix + package + (isTest ? ":test" : ""),
package, kind);
for (auto const* dirs : {&cu.localIncludeDirs, &cu.localIncludeDirsAfter})
for (auto const& dir : *dirs)
if (relative_to(dir, generatedTree))
set.generatedDirs.insert(native_string(dir.lexically_normal()));
nlohmann::json provides = nlohmann::json::object();
if (!cu.providesModule.empty()) provides[cu.providesModule] = "";
nlohmann::json requires_ = nlohmann::json::array();
for (auto const& name : cu.imports) requires_.push_back(name);
r.compileCommands.push_back(nlohmann::json{
{"directory", inv.directory},
{"file", inv.file},
{"arguments", inv.arguments},
{"output", inv.output},
});
set.units.push_back(nlohmann::json{
{"source", std::move(inv.file)},
{"work-directory", std::move(inv.directory)},
{"arguments", std::move(inv.arguments)},
{"object", std::move(inv.output)},
{"private", false},
{"provides", std::move(provides)},
{"requires", std::move(requires_)},
{"ide", {{"role", std::string(role_name(cu.declaration))}}},
});
}
// D5a/D5b (design 2026-09-26 §3.5): the standard-library units,
// already recovered onto the plan (prepare.cppm, from the same
// derivation `ensure_built` and `describe_std_module` both read), so
// this rendering cannot list a command or a BMI path that
// compile_commands.json (mcpp.build.compile_commands) disagrees with
// (P1). S1-12-1: they are translation units of the build like any
// other, so `--spec compile-commands` lists them too.
if (!ctx.plan.stdModuleUnits.empty()) {
auto& set = set_for(member.setPrefix + std::string(kStdSetName),
std::string(kStdSetName), "library");
for (auto const& unit : ctx.plan.stdModuleUnits) {
const auto sourceStr = native_string(unit.source);
const auto objectStr = native_string(unit.object);
const auto workDirStr = native_string(unit.workDirectory);
r.compileCommands.push_back(nlohmann::json{
{"directory", workDirStr},
{"file", sourceStr},
{"arguments", unit.arguments},
{"output", objectStr},
});
nlohmann::json requires_ = nlohmann::json::array();
for (auto const& name : unit.requiresModules) requires_.push_back(name);
set.units.push_back(nlohmann::json{
{"source", sourceStr},
{"work-directory", workDirStr},
{"arguments", unit.arguments},
{"object", objectStr},
{"private", false},
// D5b, S1-8-6: the path of the BMI the build writes, in
// the shared std cache that `emit` and the build share.
// The planning pass never compiles (SPEC-005 R2.2), but
// this path is the cache key's, not a compiler output it
// would have to run to learn.
{"provides", {{unit.module, native_string(unit.bmi)}}},
{"requires", std::move(requires_)},
{"ide", {{"role", "module-interface"}}},
});
}
}
// A recovery failure is now reported where the recovery runs
// (prepare.cppm, onto BuildContext::planNotes) and reaches `r.notes`
// through the unconditional copy below, with every other plan note.
{
using Role = mcpp::manifest::BuildAction::Role;
for (auto const& a : ctx.plan.actions) {
if (a.role != Role::Source || a.outputs.empty()) continue;
const std::string package = a.packageName.empty() ? rootName : a.packageName;
nlohmann::json inputs = nlohmann::json::array();
for (auto const& in : a.inputs)
inputs.push_back(native_string(std::filesystem::path(in).lexically_normal()));
const nlohmann::json generator{
{"id", a.id},
{"inputs", std::move(inputs)},
{"arguments", a.command},
{"work-directory", native_string(a.cwd.empty()
? ctx.plan.outputDir
: std::filesystem::path(a.cwd))},
};
// SPEC-005 R3.12: every set of the package lists what its build
// program generates, its test set included, since the units
// that include a header are not known without preprocessing.
// `kind` is per set: `source` where a unit of that set is
// compiled from the file.
for (auto& [name, set] : groups) {
if (set.familyName != package) continue;
for (auto const& out : a.outputs) {
const auto p = std::filesystem::path(out).lexically_normal();
const auto path = native_string(p);
const bool isUnit = std::ranges::any_of(set.units, [&](auto const& u) {
return u.value("source", "") == path;
});
set.generated.push_back(nlohmann::json{
{"path", path},
{"build-path", build_path(p)},
{"kind", isUnit ? "source" : "header"},
{"generator", generator},
});
}
}
}
for (auto& [name, set] : groups)
for (auto const& dir : set.generatedDirs)
set.generated.push_back(nlohmann::json{
{"path", dir},
{"build-path", build_path(std::filesystem::path(dir))},
{"kind", "directory"},
});
}
for (auto const& name : order) {
auto& set = groups.at(name);
nlohmann::json visible = nlohmann::json::array();
for (auto const& other : order)
if (other != name) visible.push_back(other);
nlohmann::json setJson{
{"name", name},
{"family-name", set.familyName},
{"visible-sets", std::move(visible)},
{"translation-units", std::move(set.units)},
{"ide", {
{"toolchain", tcId},
{"configuration", ctx.profile},
{"kind", set.kind},
}},
};
if (!set.generated.empty())
setJson["ide"]["generated"] = std::move(set.generated);
split_baseline(setJson);
sets.push_back(std::move(setJson));
}
watch_file(ctx.projectRoot / "mcpp.lock");
for (auto const& sp : ctx.sourcePackages) {
watch_file(sp.root / "mcpp.toml");
std::error_code ec;
if (std::filesystem::exists(sp.root / "build.mcpp", ec))
watch_file(sp.root / "build.mcpp");
for (auto const& g : sp.sources) watch_glob(sp.root, g);
}
for (auto const& g : member.testDiscover) watch_glob(member.testRoot, g);
// Only an editable package's build program inputs can change: a store
// package's are fixed by the version its manifest and lock name.
std::set<std::filesystem::path> editableRoots;
for (auto const& sp : ctx.sourcePackages) editableRoots.insert(sp.root.lexically_normal());
for (auto const& declared : mcpp::build::declared_program_inputs(member.workDir)) {
if (!editableRoots.contains(declared.root.lexically_normal())) continue;
for (auto const& f : declared.files) watch_file(f);
for (auto const& g : declared.globs) watch_glob(declared.root, g);
}
for (auto note : ctx.planNotes) {
// `note.path`, when set, is the absolute path a `plan_only`
// branch recorded (#699 item 2, E3); every other `path` in this
// document is relative to the workspace root, and a note's is
// rewritten to match before it reaches the command.
if (!note.path.empty()) {
const std::filesystem::path abs{note.path};
if (auto rel = relative_to(abs, workspaceRoot); rel && !rel->empty())
note.path = *rel;
else
note.path = native_string(abs.lexically_normal());
}
r.notes.push_back(std::move(note));
}
}
// Every member whose planning failed still has its likely cause watched
// (#699 item 1, E1): editing its `mcpp.toml`, or the `build.mcpp` that may
// have failed, is what should make a consumer ask again for the members
// that could not be described this time.
for (auto const& failedRoot : failedMemberRoots) {
watch_file(failedRoot / "mcpp.toml");
std::error_code ec;
if (std::filesystem::exists(failedRoot / "build.mcpp", ec))
watch_file(failedRoot / "build.mcpp");
}
r.database = nlohmann::json{
{"version", 1},
{"revision", 0},
{"ide", {
{"profile-version", std::string(kProfileVersion)},
{"generator", {
{"name", "mcpp"},
{"version", std::string(mcpp::toolchain::MCPP_VERSION)},
}},
{"toolchains", std::move(toolchains)},
}},
{"sets", std::move(sets)},
};
r.watch.assign(watch.begin(), watch.end());
std::string digest = "mcpp-build-database-inputs-v1\x1f";
digest += mcpp::toolchain::MCPP_VERSION;
digest += '\x1f';
digest += selector;
digest += '\n';
for (auto const& f : inputFiles) {
digest += f.generic_string();
digest += '\x1f';
digest += mcpp::toolchain::hash_file(f);
digest += '\n';
}
r.inputsFingerprint = "fnv1a:" + mcpp::toolchain::hash_string(digest);
return r;
}
} // namespace mcpp::build::database