-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathninja_backend.cppm
More file actions
571 lines (504 loc) · 21.8 KB
/
Copy pathninja_backend.cppm
File metadata and controls
571 lines (504 loc) · 21.8 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
// mcpp.build.ninja — Ninja-backed implementation of Backend.
//
// Layout produced under plan.outputDir = target/<triple>/<fp>/:
// build.ninja
// gcm.cache/std.gcm (symlink/copy of plan.stdBmiPath)
// gcm.cache/<module>.gcm (created by GCC during compile)
// obj/<unit>.o
// obj/std.o (symlink/copy of plan.stdObjectPath)
// bin/<target>
//
// All compile commands are run with cwd = plan.outputDir, so GCC's implicit
// gcm.cache/ lookup finds both std and our package modules.
module;
#include <cstdio>
#include <cstdlib>
export module mcpp.build.ninja;
import std;
import mcpp.build.backend;
import mcpp.build.plan;
import mcpp.dyndep;
export namespace mcpp::build {
class NinjaBackend final : public Backend {
public:
std::string_view name() const override { return "ninja"; }
std::expected<BuildResult, BuildError>
build(const BuildPlan& plan, const BuildOptions& opts) override;
};
// Factory for this backend implementation.
std::unique_ptr<Backend> make_ninja_backend();
// Helper exposed for testing / debugging
std::string emit_ninja_string(const BuildPlan& plan);
} // namespace mcpp::build
namespace mcpp::build {
namespace {
std::string escape_ninja_path(const std::filesystem::path& p) {
// Ninja escapes: $ → $$, : → $:, space → $ (with leading space).
// For simplicity we wrap in case-by-case.
std::string s = p.string();
std::string out;
for (char c : s) {
if (c == '$') out += "$$";
else if (c == ':') out += "$:";
else if (c == ' ') out += "$ ";
else out.push_back(c);
}
return out;
}
void write_file(const std::filesystem::path& p, std::string_view content) {
std::filesystem::create_directories(p.parent_path());
std::ofstream os(p);
os << content;
}
bool run(const std::string& cmd, std::string& output_capture, bool capture = true) {
std::array<char, 8192> buf{};
output_capture.clear();
std::FILE* fp = ::popen(cmd.c_str(), "r");
if (!fp) return false;
while (std::fgets(buf.data(), buf.size(), fp) != nullptr) {
if (capture) output_capture += buf.data();
else std::fputs(buf.data(), stdout);
}
int rc = ::pclose(fp);
return rc == 0;
}
bool dyndep_mode_enabled() {
// M4 #7: dyndep is now the default. Set MCPP_NINJA_DYNDEP=0 to opt
// OUT and fall back to the static-deps emission path.
const char* v = std::getenv("MCPP_NINJA_DYNDEP");
if (!v) return true;
std::string_view sv(v);
return !(sv == "0" || sv == "off" || sv == "false");
}
std::filesystem::path mcpp_exe_path() {
std::error_code ec;
auto p = std::filesystem::read_symlink("/proc/self/exe", ec);
if (!ec) return p;
return "mcpp"; // fall back to PATH lookup
}
// Derive a sibling C compiler from a C++ compiler binary path. Used so .c
// sources can be compiled by the actual C frontend (cc1), not g++ which
// rejects implicit `void*` conversions and `restrict` etc.
// .../bin/g++ → .../bin/gcc
// .../bin/x86_64-linux-musl-g++ → .../bin/x86_64-linux-musl-gcc
// .../bin/clang++ → .../bin/clang
// .../bin/c++ → .../bin/cc
// If no sibling exists, return "gcc" so PATH lookup is the final fallback
// (this also keeps unit tests that don't touch a real toolchain happy).
std::filesystem::path derive_c_compiler(const std::filesystem::path& cxx) {
auto fname = cxx.filename().string();
auto try_replace = [&](std::string_view from, std::string_view to)
-> std::optional<std::filesystem::path>
{
auto pos = fname.rfind(from);
if (pos == std::string::npos) return std::nullopt;
std::string repl = fname;
repl.replace(pos, from.size(), to);
auto p = cxx.parent_path() / repl;
std::error_code ec;
if (std::filesystem::exists(p, ec)) return p;
return std::nullopt;
};
if (auto p = try_replace("clang++", "clang")) return *p;
if (auto p = try_replace("g++", "gcc")) return *p;
if (auto p = try_replace("c++", "cc")) return *p;
return "gcc";
}
bool is_c_source(const std::filesystem::path& src) {
return src.extension() == ".c";
}
} // namespace
std::string emit_ninja_string(const BuildPlan& plan) {
bool dyndep = dyndep_mode_enabled();
std::string out;
auto append = [&](std::string s) { out += std::move(s); };
append("# Auto-generated by mcpp v0.0.1. Do not edit by hand.\n");
append("ninja_required_version = 1.11\n\n");
// Detect whether any target needs PIC (shared library). If so, all
// objects are compiled with -fPIC so they can be linked into either
// shared libs or binaries. (Slight perf cost; far simpler than two
// compile graphs.)
bool need_pic = false;
for (auto& lu : plan.linkUnits) {
if (lu.kind == LinkUnit::SharedLibrary) { need_pic = true; break; }
}
const char* pic_flag = need_pic ? " -fPIC" : "";
// -static-libstdc++ default-on per docs/21 §VIII (decision #5):
// makes built binaries portable across machines with the same ABI.
// Users can disable via mcpp.toml `[build].static_stdlib = false`.
const char* static_stdlib = plan.manifest.buildConfig.staticStdlib
? " -static-libstdc++" : "";
// Full static linkage — set by --static or [target.<triple>].linkage = "static".
// `-static` subsumes -static-libstdc++ but emit both since gcc tolerates
// the duplication and CI logs make the intent obvious.
const char* full_static = (plan.manifest.buildConfig.linkage == "static")
? " -static" : "";
// M5.0: -I from [build].include_dirs (resolved to absolute paths).
std::string include_flags;
for (auto& inc : plan.manifest.buildConfig.includeDirs) {
auto abs = inc.is_absolute() ? inc : (plan.projectRoot / inc);
include_flags += " -I" + escape_ninja_path(abs);
}
// M5.5: --sysroot when probed (needed since we bypass any xlings wrapper).
std::string sysroot_flag;
if (!plan.toolchain.sysroot.empty()) {
sysroot_flag = " --sysroot=" + escape_ninja_path(plan.toolchain.sysroot);
}
// When the toolchain comes from mcpp's private sandbox, locate the
// binutils bin dir (where as/ld/ar/ranlib live). Used to:
// - emit absolute `ar` path in cxx_archive rule
// - add -B<binutils-bin> to cxxflags/ldflags so g++ finds as/ld
// internally without depending on PATH lookup
//
// This is the last piece making mcpp's build path fully self-contained:
// no PATH dependency for the build, no xlings shim involvement at all.
// The musl-gcc xpkg ships a complete prefixed binutils set under its
// own bin/ (`x86_64-linux-musl-{ar,as,ld,...}`); xim:binutils isn't on
// its dependency path and won't be present. For musl we leave -B
// unset so g++ resolves as/ld via its own libexec — that's what
// musl-gcc.lua expects.
bool isMuslTc = plan.toolchain.targetTriple.find("-musl") != std::string::npos;
std::filesystem::path binutilsBin;
if (!isMuslTc) {
auto bp = plan.toolchain.binaryPath;
std::filesystem::path xpkgsDir;
for (auto p = bp.parent_path();
p.has_parent_path() && p != p.root_path();
p = p.parent_path()) {
if (p.filename() == "xpkgs") { xpkgsDir = p; break; }
}
if (!xpkgsDir.empty()) {
auto root = xpkgsDir / "xim-x-binutils";
std::error_code ec;
if (std::filesystem::exists(root, ec)) {
for (auto& v : std::filesystem::directory_iterator(root, ec)) {
auto candidate = v.path() / "bin";
if (std::filesystem::exists(candidate / "ar", ec)) {
binutilsBin = candidate;
break;
}
}
}
}
}
std::string b_flag;
if (!binutilsBin.empty()) {
b_flag = " -B" + escape_ninja_path(binutilsBin);
}
// musl-gcc 15.1.0 ICEs in tree-ssa-ccp on libstdc++'s std::format
// (`__write_padded` template) at -O2. Drop to -Og for the musl path
// until either musl-gcc 16.1 lands or upstream fixes the pass.
// TODO(musl-gcc-upstream): remove once musl-gcc@16+ ships.
const char* opt_flag = isMuslTc ? " -Og" : " -O2";
// M5.x: any C sources in the plan? If so we emit a `cc` variable and a
// separate `c_object` rule so .c files are compiled by the C frontend
// (gcc / clang / cc) rather than g++. .c files compiled with g++ get
// routed to cc1plus which rejects C-only constructs (implicit void*
// conversion, `restrict` keyword, etc.) — fatal for libraries like
// mbedtls / openssl.
bool need_c_rule = false;
for (auto& cu : plan.compileUnits) {
if (is_c_source(cu.source)) { need_c_rule = true; break; }
}
// User-supplied flag tails — appended verbatim to per-rule baselines.
auto join_flags = [](const std::vector<std::string>& flags) {
std::string out;
for (auto& f : flags) { out += ' '; out += f; }
return out;
};
std::string user_cxxflags = join_flags(plan.manifest.buildConfig.cxxflags);
std::string user_cflags = join_flags(plan.manifest.buildConfig.cflags);
std::string c_standard = plan.manifest.buildConfig.cStandard.empty()
? std::string{"c11"} : plan.manifest.buildConfig.cStandard;
append(std::format("cxx = {}\n", escape_ninja_path(plan.toolchain.binaryPath)));
append(std::format("cxxflags = -std=c++23 -fmodules{}{}{}{}{}{}\n",
opt_flag, pic_flag, sysroot_flag, b_flag, include_flags,
user_cxxflags));
if (need_c_rule) {
auto cc_path = derive_c_compiler(plan.toolchain.binaryPath);
append(std::format("cc = {}\n", escape_ninja_path(cc_path)));
// C baseline: same opt/pic/sysroot/-B/include layout as cxxflags but
// no -fmodules and -std= goes to a C dialect.
append(std::format("cflags = -std={}{}{}{}{}{}{}\n",
c_standard, opt_flag, pic_flag, sysroot_flag,
b_flag, include_flags, user_cflags));
}
append(std::format("ldflags ={}{}{}{}\n",
full_static, static_stdlib, sysroot_flag, b_flag));
// `ar` for cxx_archive: prefer sandbox absolute path, fall back to PATH.
// For musl-gcc the prefixed binary lives next to the compiler itself.
if (!binutilsBin.empty()) {
append(std::format("ar = {}\n",
escape_ninja_path(binutilsBin / "ar")));
} else if (isMuslTc) {
auto muslAr = plan.toolchain.binaryPath.parent_path()
/ "x86_64-linux-musl-ar";
if (std::filesystem::exists(muslAr)) {
append(std::format("ar = {}\n", escape_ninja_path(muslAr)));
} else {
append("ar = ar\n");
}
} else {
append("ar = ar\n");
}
if (dyndep) {
append(std::format("mcpp = {}\n", escape_ninja_path(mcpp_exe_path())));
}
append("\n");
append("rule cp_bmi\n");
append(" command = mkdir -p $$(dirname $out) && cp -f $in $out\n");
append(" description = STAGE $out\n\n");
append("rule cxx_module\n");
append(" command = $cxx $cxxflags -c $in -o $out\n");
append(" description = MOD $out\n");
if (dyndep) append(" restat = 1\n");
append("\n");
append("rule cxx_object\n");
append(" command = $cxx $cxxflags -c $in -o $out\n");
append(" description = OBJ $out\n");
if (dyndep) append(" restat = 1\n");
append("\n");
if (need_c_rule) {
append("rule c_object\n");
append(" command = $cc $cflags -c $in -o $out\n");
append(" description = CC $out\n");
if (dyndep) append(" restat = 1\n");
append("\n");
}
append("rule cxx_link\n");
append(" command = $cxx $in -o $out $ldflags\n");
append(" description = LINK $out\n\n");
append("rule cxx_archive\n");
append(" command = $ar rcs $out $in\n");
append(" description = AR $out\n\n");
append("rule cxx_shared\n");
append(" command = $cxx -shared $in -o $out $ldflags\n");
append(" description = SHARED $out\n\n");
if (dyndep) {
// Scan rule: produce P1689 .ddi for one TU.
// -E -M -MM -MF gives us the dep file; -fdeps-* gives us the .ddi.
append("rule cxx_scan\n");
append(" command = $cxx $cxxflags -fdeps-format=p1689r5 "
"-fdeps-file=$out -fdeps-target=$compile_target "
"-M -MM -MF $out.dep -E $in -o $compile_target\n");
append(" description = SCAN $out\n\n");
// Aggregate .ddi files into a Ninja dyndep file.
append("rule cxx_collect\n");
append(" command = $mcpp dyndep --output $out $in\n");
append(" description = COLLECT $out\n");
append(" restat = 1\n\n");
}
// Stage prebuilt std artifacts into our gcm.cache/
auto std_bmi_dst = std::filesystem::path("gcm.cache") / "std.gcm";
auto std_o_dst = std::filesystem::path("obj") / "std.o";
append(std::format("build {} : cp_bmi {}\n",
escape_ninja_path(std_bmi_dst),
escape_ninja_path(plan.stdBmiPath)));
append(std::format("build {} : cp_bmi {}\n\n",
escape_ninja_path(std_o_dst),
escape_ninja_path(plan.stdObjectPath)));
auto bmi_path = [](std::string_view name) {
std::string s = "gcm.cache/";
for (char c : name) s.push_back(c == ':' ? '-' : c);
s += ".gcm";
return s;
};
auto pick_rule = [](const std::filesystem::path& src) -> std::string {
auto ext = src.extension();
if (ext == ".cppm") return "cxx_module";
if (ext == ".c") return "c_object";
return "cxx_object";
};
if (dyndep) {
// ── Phase 1: scan edges (one .ddi per TU). ──────────────────────
// .ddi is placed beside the object so multi-version mangling can
// namespace by package without producing two `build` rules with
// the same `.ddi` output (plan.cppm switches `cu.object` from
// `obj/<file>.o` to `obj/<pkg>/<file>.o` whenever a basename
// collides across packages — `.ddi` follows that placement).
// Skip .c files: they have no `import`s and don't need P1689 scan;
// running them through cxx_scan would route them through g++ /
// -fmodules which is exactly what C support is here to avoid.
std::vector<std::string> ddi_paths;
ddi_paths.reserve(plan.compileUnits.size());
for (auto& cu : plan.compileUnits) {
if (is_c_source(cu.source)) continue;
auto ddi = (cu.object.parent_path()
/ cu.source.filename()).string() + ".ddi";
ddi_paths.push_back(ddi);
append(std::format("build {} : cxx_scan {}\n",
escape_ninja_path(ddi),
escape_ninja_path(cu.source)));
append(std::format(" compile_target = {}\n",
escape_ninja_path(cu.object)));
}
append("\n");
// ── Phase 2: collect into dyndep file. ──────────────────────────
std::string ddi_inputs;
for (auto& d : ddi_paths) ddi_inputs += " " + d;
append("build build.ninja.dd : cxx_collect" + ddi_inputs + "\n\n");
// ── Phase 3: compile edges with dyndep. ─────────────────────────
// BMI implicit outputs are still declared statically (we know
// them from the plan); the dyndep file adds implicit BMI INPUTS
// (the requires) so ninja schedules in the right order.
for (auto& cu : plan.compileUnits) {
std::string rule = pick_rule(cu.source);
std::string out_line = "build " + escape_ninja_path(cu.object);
if (cu.providesModule) {
out_line += " | " + bmi_path(*cu.providesModule);
}
out_line += std::format(" : {} {}", rule,
escape_ninja_path(cu.source));
if (rule != "c_object") {
// build.ninja.dd is the dyndep file; ninja requires it as an
// implicit input (so it's built before the compile runs).
out_line += " | build.ninja.dd";
out_line += "\n dyndep = build.ninja.dd\n";
} else {
out_line += "\n";
}
append(std::move(out_line));
}
append("\n");
} else {
// ── Static-deps mode (M3.2 and earlier). ────────────────────────
for (auto& cu : plan.compileUnits) {
std::string rule = pick_rule(cu.source);
std::string implicit;
// .c files don't `import` modules; skip BMI implicit inputs.
if (rule != "c_object") {
for (auto& imp : cu.imports) {
if (imp == "std" || imp == "std.compat") {
implicit += " gcm.cache/std.gcm";
continue;
}
implicit += " " + bmi_path(imp);
}
}
std::string out_line = "build " + escape_ninja_path(cu.object);
if (cu.providesModule) {
out_line += " " + bmi_path(*cu.providesModule);
}
out_line += std::format(" : {} {}", rule,
escape_ninja_path(cu.source));
if (!implicit.empty()) out_line += " |" + implicit;
out_line += "\n";
append(std::move(out_line));
}
append("\n");
}
// Link units
for (auto& lu : plan.linkUnits) {
std::string ins;
for (auto& o : lu.objects) {
ins += " " + escape_ninja_path(o);
}
std::string rule;
switch (lu.kind) {
case LinkUnit::Binary:
case LinkUnit::TestBinary:
ins += " " + escape_ninja_path(std_o_dst);
rule = "cxx_link";
break;
case LinkUnit::StaticLibrary:
rule = "cxx_archive";
break;
case LinkUnit::SharedLibrary:
ins += " " + escape_ninja_path(std_o_dst);
rule = "cxx_shared";
break;
}
append(std::format("build {} : {}{}\n",
escape_ninja_path(lu.output), rule, ins));
}
append("\n");
if (!plan.linkUnits.empty()) {
std::string defaults;
for (auto& lu : plan.linkUnits) {
defaults += " " + escape_ninja_path(lu.output);
}
append("default" + defaults + "\n");
}
return out;
}
std::expected<BuildResult, BuildError>
NinjaBackend::build(const BuildPlan& plan, const BuildOptions& opts)
{
auto t0 = std::chrono::steady_clock::now();
std::error_code ec;
std::filesystem::create_directories(plan.outputDir, ec);
if (ec) return std::unexpected(BuildError{
std::format("cannot create output dir '{}': {}", plan.outputDir.string(), ec.message()),
plan.outputDir});
auto ninja_path = plan.outputDir / "build.ninja";
write_file(ninja_path, emit_ninja_string(plan));
if (opts.dryRun) {
BuildResult r;
r.exitCode = 0;
r.elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - t0);
return r;
}
// When the toolchain comes from mcpp's private sandbox, use the
// sandbox-local ninja absolute path (skip the system xlings ninja
// shim which requires per-tool version pin activation).
//
// The compiler's internal `as`/`ld` lookup is handled via the
// -B<binutils-bin> flag we emit into cxxflags/ldflags (see
// emit_ninja_string). No PATH injection needed here.
std::filesystem::path ninjaBin;
{
auto bp = plan.toolchain.binaryPath;
std::filesystem::path xpkgsDir;
for (auto p = bp.parent_path();
p.has_parent_path() && p != p.root_path();
p = p.parent_path())
{
if (p.filename() == "xpkgs") { xpkgsDir = p; break; }
}
if (!xpkgsDir.empty()) {
// xim's ninja xpkg puts the binary at <v>/ninja (no bin/ subdir).
auto root = xpkgsDir / "xim-x-ninja";
std::error_code ec;
if (std::filesystem::exists(root, ec)) {
for (auto& v : std::filesystem::directory_iterator(root, ec)) {
auto candidate = v.path() / "ninja";
if (std::filesystem::exists(candidate, ec)) {
ninjaBin = candidate;
break;
}
}
}
}
}
std::string ninjaProgram = !ninjaBin.empty()
? std::format("'{}'", ninjaBin.string()) : std::string{"ninja"};
std::string cmd = std::format("{} -C '{}'",
ninjaProgram, plan.outputDir.string());
if (opts.verbose) cmd += " -v";
if (opts.parallelJobs) cmd += std::format(" -j{}", opts.parallelJobs);
cmd += " 2>&1";
std::string out;
bool ok = run(cmd, out, /*capture=*/true);
if (opts.verbose || !ok) {
std::fputs(out.c_str(), stdout);
}
BuildResult r;
r.exitCode = ok ? 0 : 1;
r.elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - t0);
if (ok) {
for (auto& lu : plan.linkUnits) {
r.producedArtifacts.push_back(plan.outputDir / lu.output);
}
} else {
return std::unexpected(BuildError{
std::format("ninja failed (exit non-zero):\n{}", out),
plan.outputDir / "build.ninja"});
}
return r;
}
std::unique_ptr<Backend> make_ninja_backend() {
return std::make_unique<NinjaBackend>();
}
} // namespace mcpp::build