-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathprepare.cppm
More file actions
2500 lines (2308 loc) · 115 KB
/
Copy pathprepare.cppm
File metadata and controls
2500 lines (2308 loc) · 115 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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// mcpp.build.prepare — BuildContext + prepare_build: the build-orchestration
// core (workspace -> toolchain -> dependency resolution -> features ->
// modgraph -> fingerprint -> plan -> lockfile).
// Bodies moved verbatim from the CLI layer. Zero behavior change.
module;
#include <cstdio>
#include <cstdlib>
export module mcpp.build.prepare;
import std;
import mcpp.libs.json;
import mcpp.manifest;
import mcpp.modgraph.graph;
import mcpp.modgraph.scanner;
import mcpp.modgraph.validate;
import mcpp.toolchain.clang;
import mcpp.toolchain.detect;
import mcpp.toolchain.fingerprint;
import mcpp.toolchain.registry;
import mcpp.toolchain.stdmod;
import mcpp.toolchain.post_install;
import mcpp.build.plan;
import mcpp.lockfile;
import mcpp.config;
import mcpp.xlings;
import mcpp.platform;
import mcpp.fetcher;
import mcpp.fetcher.progress;
import mcpp.pm.resolver;
import mcpp.pm.index_spec;
import mcpp.pm.mangle;
import mcpp.pm.compat;
import mcpp.pm.dep_spec;
import mcpp.version_req;
import mcpp.ui;
import mcpp.log;
import mcpp.fallback.install_integrity;
import mcpp.bmi_cache;
import mcpp.project;
namespace mcpp::build {
export std::filesystem::path target_dir(const mcpp::toolchain::Toolchain& tc,
const mcpp::toolchain::Fingerprint& fp,
const std::filesystem::path& root)
{
auto triple = tc.targetTriple.empty() ? std::string{"unknown"} : tc.targetTriple;
return root / "target" / triple / fp.hex;
}
// Compose a stable canonical compile-flags string for fingerprinting.
std::string canonical_compile_flags(const mcpp::manifest::Manifest& m) {
std::string s;
s += "-std="; s += m.package.standard;
s += " -fmodules";
// macOS deployment target changes the effective compile triple
// (arm64-apple-macosxNN) — a std.pcm built for one target cannot be
// loaded by a TU compiled for another. Fold the resolved value
// (env override > [build] macos_deployment_target manifest default)
// into the fingerprint so switching targets rebuilds the BMI cache
// instead of dying with a module config mismatch.
//
// The built-in default floor (rustc-style) lives in the single
// resolver (platform::macos::deployment_target), so this rule, the
// flags and the std-module prebuild always agree — the 0.0.50-era
// attempt to inject a default here alone left the test build's
// std.pcm unstaged (import std failed wholesale on macos CI).
if constexpr (mcpp::platform::is_macos) {
auto dtv = mcpp::platform::macos::deployment_target(
m.buildConfig.macosDeploymentTarget);
if (!dtv.empty()) {
s += " macos_deployment_target=";
s += dtv;
}
}
if (!m.buildConfig.cStandard.empty()) {
s += " c_standard=";
s += m.buildConfig.cStandard;
}
for (auto const& flag : m.buildConfig.cflags) {
s += " cflag:";
s += flag;
}
for (auto const& flag : m.buildConfig.cxxflags) {
s += " cxxflag:";
s += flag;
}
for (auto const& flag : m.buildConfig.ldflags) {
s += " ldflag:";
s += flag;
}
return s;
}
std::string canonical_package_build_metadata(
const std::vector<mcpp::modgraph::PackageRoot>& packages)
{
std::string s;
for (auto const& pkg : packages) {
s += "\npackage:";
s += pkg.manifest.package.namespace_;
s += "/";
s += pkg.manifest.package.name;
s += "@";
s += pkg.manifest.package.version;
if (!pkg.manifest.buildConfig.cStandard.empty()) {
s += " c_standard=";
s += pkg.manifest.buildConfig.cStandard;
}
for (auto const& flag : pkg.manifest.buildConfig.cflags) {
s += " cflag:";
s += flag;
}
for (auto const& flag : pkg.manifest.buildConfig.cxxflags) {
s += " cxxflag:";
s += flag;
}
for (auto const& flag : pkg.manifest.buildConfig.ldflags) {
s += " ldflag:";
s += flag;
}
if (pkg.usageResolved) {
for (auto const& dir : pkg.privateBuild.includeDirs) {
s += " private_include:";
s += dir.generic_string();
}
for (auto const& dir : pkg.publicUsage.includeDirs) {
s += " public_include:";
s += dir.generic_string();
}
}
for (auto const& [path, content] : pkg.manifest.buildConfig.generatedFiles) {
s += " genfile:";
s += path.generic_string();
s += "=";
s += content;
}
}
return s;
}
std::expected<void, std::string>
materialize_generated_files(const std::filesystem::path& root,
const mcpp::manifest::Manifest& manifest)
{
for (auto const& [relPath, content] : manifest.buildConfig.generatedFiles) {
if (relPath.empty()) {
return std::unexpected("generated_files contains an empty path");
}
if (relPath.is_absolute()) {
return std::unexpected(std::format(
"generated_files path '{}' must be relative", relPath.generic_string()));
}
auto const genericPath = relPath.generic_string();
for (std::size_t begin = 0; begin <= genericPath.size();) {
auto const end = genericPath.find('/', begin);
auto const part = genericPath.substr(begin, end == std::string::npos
? std::string::npos
: end - begin);
if (part == "..") {
return std::unexpected(std::format(
"generated_files path '{}' must not escape the package root",
relPath.generic_string()));
}
if (end == std::string::npos) {
break;
}
begin = end + 1;
}
auto out = root / relPath.lexically_normal();
std::error_code ec;
std::filesystem::create_directories(out.parent_path(), ec);
if (ec) {
return std::unexpected(std::format(
"cannot create directory for generated file '{}': {}",
out.string(), ec.message()));
}
std::ofstream os(out, std::ios::binary);
if (!os) {
return std::unexpected(std::format(
"cannot write generated file '{}'", out.string()));
}
os << content;
if (!os) {
return std::unexpected(std::format(
"failed while writing generated file '{}'", out.string()));
}
}
return {};
}
bool is_std_module(std::string_view name) {
return name == "std" || name == "std.compat";
}
std::string trim_copy(std::string s) {
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.front())))
s.erase(0, 1);
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.back())))
s.pop_back();
return s;
}
bool source_file_imports_std(const std::filesystem::path& path) {
std::ifstream is(path);
if (!is) return false;
std::string line;
while (std::getline(is, line)) {
line = trim_copy(std::move(line));
std::size_t i = std::string::npos;
if (line.starts_with("import ")) {
i = 7;
} else if (line.starts_with("export import ")) {
i = 14;
}
if (i == std::string::npos) continue;
while (i < line.size() && std::isspace(static_cast<unsigned char>(line[i])))
++i;
std::string name;
while (i < line.size()
&& (std::isalnum(static_cast<unsigned char>(line[i]))
|| line[i] == '_' || line[i] == '.' || line[i] == ':')) {
name.push_back(line[i]);
++i;
}
if (is_std_module(name)) return true;
}
return false;
}
bool graph_or_targets_import_std(const mcpp::modgraph::Graph& graph,
const mcpp::manifest::Manifest& manifest,
const std::filesystem::path& projectRoot) {
for (auto& u : graph.units) {
for (auto& req : u.requires_) {
if (is_std_module(req.logicalName))
return true;
}
}
// Some target entry files can be added to the plan after the package scan.
// Check them here so std BMI setup matches what make_plan will compile.
for (auto& t : manifest.targets) {
if (!t.main.empty() && source_file_imports_std(projectRoot / t.main))
return true;
}
return false;
}
export struct BuildContext {
mcpp::manifest::Manifest manifest;
mcpp::toolchain::Toolchain tc;
mcpp::toolchain::Fingerprint fp;
std::filesystem::path projectRoot;
std::filesystem::path outputDir;
std::filesystem::path stdBmi;
std::filesystem::path stdObject;
mcpp::build::BuildPlan plan;
// M3.2 BMI cache: deps that did NOT hit cache and therefore need
// populate_from(...) AFTER backend.build succeeds.
struct CacheTask {
mcpp::bmi_cache::CacheKey key;
mcpp::bmi_cache::DepArtifacts artifacts;
};
std::vector<CacheTask> depsToPopulate;
// Names of deps that DID hit cache (for ui status output).
std::vector<std::string> cachedDepLabels; // "mcpplibs.cmdline v0.0.1"
};
// Command-level overrides (--target / --static).
// Empty defaults preserve pre-existing behaviour exactly.
export struct BuildOverrides {
std::string target_triple; // empty = host triple, fall through to [toolchain]
bool force_static = false; // --static (or implied by musl target)
std::string package_filter; // -p <name>: only build this workspace member
std::string profile; // --profile <name> (default "release")
std::string features; // --features a,b,c (root package activation)
bool strict = false; // --strict: schema warnings become errors
};
// `prepare_build` builds the BuildContext for any verb that compiles.
// includeDevDeps: when true, dev-dependencies are also fetched + scanned
// into the modgraph. mcpp test passes true; build/run pass false.
// extraTargets: additional Target entries (e.g. synthetic test targets)
// appended to the manifest before the modgraph runs.
// overrides: --target / --static.
export std::expected<BuildContext, std::string>
prepare_build(bool print_fingerprint,
bool includeDevDeps = false,
std::vector<mcpp::manifest::Target> extraTargets = {},
BuildOverrides overrides = {}) {
auto root = mcpp::project::find_manifest_root(std::filesystem::current_path());
if (!root) {
return std::unexpected("no mcpp.toml found in current directory or any parent");
}
auto m = mcpp::manifest::load(*root / "mcpp.toml");
if (!m) return std::unexpected(m.error().format());
// ─── Workspace handling ────────────────────────────────────────────
// If the manifest has [workspace] and is a virtual workspace (no [package]),
// or if -p filter is set, switch to the target member's manifest.
std::optional<mcpp::manifest::Manifest> wsManifest; // keep workspace manifest alive
if (m->workspace.present) {
std::string targetMember;
if (!overrides.package_filter.empty()) {
// -p <name>: find matching member by directory basename or path
for (auto& mp : m->workspace.members) {
auto basename = std::filesystem::path(mp).filename().string();
if (basename == overrides.package_filter || mp == overrides.package_filter) {
targetMember = mp;
break;
}
}
if (targetMember.empty()) {
return std::unexpected(std::format(
"workspace member '{}' not found in [workspace].members",
overrides.package_filter));
}
} else if (m->package.name.empty()) {
// Virtual workspace: find a member with a binary target, or use last member.
for (auto& mp : m->workspace.members) {
auto memberDir = *root / mp;
auto mm = mcpp::manifest::load(memberDir / "mcpp.toml");
if (!mm) continue;
for (auto& t : mm->targets) {
if (t.kind == mcpp::manifest::Target::Binary) {
targetMember = mp;
break;
}
}
if (!targetMember.empty()) break;
}
if (targetMember.empty() && !m->workspace.members.empty()) {
targetMember = m->workspace.members.back();
}
}
// else: rooted workspace with [package] — build root normally.
if (!targetMember.empty()) {
auto memberDir = *root / targetMember;
if (!std::filesystem::exists(memberDir / "mcpp.toml")) {
return std::unexpected(std::format(
"workspace member '{}' has no mcpp.toml", targetMember));
}
wsManifest = std::move(*m); // preserve workspace manifest
m = mcpp::manifest::load(memberDir / "mcpp.toml");
if (!m) return std::unexpected(std::format(
"workspace member '{}': {}", targetMember, m.error().format()));
// Merge workspace dependency versions
mcpp::project::merge_workspace_deps(*m, *wsManifest);
// Inherit workspace toolchain if member doesn't define one
if (m->toolchain.byPlatform.empty()) {
m->toolchain = wsManifest->toolchain;
}
// Inherit workspace target overrides
for (auto& [triple, entry] : wsManifest->targetOverrides) {
if (!m->targetOverrides.contains(triple)) {
m->targetOverrides[triple] = entry;
}
}
// Inherit workspace indices if member doesn't define any
if (m->indices.empty() && !wsManifest->indices.empty()) {
m->indices = wsManifest->indices;
}
mcpp::ui::status("Workspace", std::format("building member '{}'", targetMember));
root = memberDir;
}
} else {
// Not at workspace root — check if we're inside a workspace
auto wsRoot = mcpp::project::find_workspace_root(*root);
if (!wsRoot.empty()) {
auto wsm = mcpp::manifest::load(wsRoot / "mcpp.toml");
if (wsm && wsm->workspace.present) {
mcpp::project::merge_workspace_deps(*m, *wsm);
if (m->toolchain.byPlatform.empty()) {
m->toolchain = wsm->toolchain;
}
for (auto& [triple, entry] : wsm->targetOverrides) {
if (!m->targetOverrides.contains(triple)) {
m->targetOverrides[triple] = entry;
}
}
// Inherit workspace indices if member doesn't define any
if (m->indices.empty() && !wsm->indices.empty()) {
m->indices = wsm->indices;
}
}
}
}
// Inject synthetic targets (e.g. test binaries from `mcpp test`).
for (auto& t : extraTargets) m->targets.push_back(t);
// ─── Toolchain resolution (docs/21) ────────────────────────────────
// Priority chain:
// 1. mcpp.toml [toolchain].<platform> → resolve_xpkg_path → abs path
// 2. $CXX env var
// 3. PATH g++ (with warning)
std::filesystem::path explicit_compiler;
std::optional<mcpp::config::GlobalConfig> cfg_opt;
bool bootstrap_checked = false;
auto get_cfg = [&](bool requireBootstrap = true) -> std::expected<mcpp::config::GlobalConfig*, std::string> {
if (!cfg_opt) {
auto c = mcpp::config::load_or_init(/*quiet=*/false,
mcpp::fetcher::make_bootstrap_progress_callback());
if (!c) return std::unexpected(c.error().message);
cfg_opt = std::move(*c);
}
// Commands that need bootstrap tools (build, run, toolchain install)
// pass requireBootstrap=true to get an early, clear error.
if (requireBootstrap && !bootstrap_checked) {
bootstrap_checked = true;
auto problem = mcpp::config::check_base_init(*cfg_opt);
if (!problem.empty()) {
return std::unexpected(std::format(
"{}\n hint: run `mcpp self init --force` to reset and re-initialize",
problem));
}
}
return &*cfg_opt;
};
constexpr std::string_view kCurrentPlatform = mcpp::platform::name;
// M5.5: toolchain resolution priority:
// 0. --target X / --static, looked up in [target.<triple>]
// 1. project mcpp.toml [toolchain].<platform> or .default
// 2. global ~/.mcpp/config.toml [toolchain].default
// 3. hard error (no system fallback)
// Resolve the build profile: --profile (default "release") → built-in
// defaults, overlaid by any [profile.<name>] from the manifest → buildConfig.
{
std::string pname = overrides.profile.empty() ? "release" : overrides.profile;
mcpp::manifest::Profile pr;
if (pname == "dev" || pname == "debug") { pr.optLevel = "0"; pr.debug = true; }
else if (pname == "dist") { pr.optLevel = "3"; pr.strip = true; }
// (built-in dist intentionally leaves lto off: several packaged gcc
// payloads ship without the LTO plugin; enable via [profile.dist].)
else { pr.optLevel = "2"; } // release
if (auto it = m->profiles.find(pname); it != m->profiles.end()) pr = it->second;
m->buildConfig.optLevel = pr.optLevel;
m->buildConfig.debug = pr.debug;
m->buildConfig.lto = pr.lto;
m->buildConfig.strip = pr.strip;
m->buildConfig.cflags.insert(m->buildConfig.cflags.end(),
pr.cflags.begin(), pr.cflags.end());
m->buildConfig.cxxflags.insert(m->buildConfig.cxxflags.end(),
pr.cxxflags.begin(), pr.cxxflags.end());
m->buildConfig.ldflags.insert(m->buildConfig.ldflags.end(),
pr.ldflags.begin(), pr.ldflags.end());
}
// [package] platforms — fixed vocabulary owned by mcpp (it owns the
// target/triple system). Unknown values: warning, or error under --strict.
for (auto& pf : m->package.platforms) {
if (pf != "linux" && pf != "macos" && pf != "windows") {
auto msg = std::format(
"[package] platforms contains unknown platform '{}' "
"(expected: linux | macos | windows)", pf);
if (overrides.strict) return std::unexpected(msg);
std::println(stderr, "warning: {}", msg);
}
}
auto tcSpec = m->toolchain.for_platform(kCurrentPlatform);
if (!tcSpec.has_value()) {
auto cfg = get_cfg();
if (cfg && !(*cfg)->defaultToolchain.empty()) {
tcSpec = (*cfg)->defaultToolchain;
}
}
// ─── --target / --static overrides ──────────────────────────────────
// Look up [target.<triple>] from manifest; fall back to convention
// (anything ending with "-musl" → gcc@<inherited-version>-musl + static).
auto endswith = [](std::string_view s, std::string_view suf) {
return s.size() >= suf.size()
&& s.compare(s.size() - suf.size(), suf.size(), suf) == 0;
};
if (!overrides.target_triple.empty()) {
auto it = m->targetOverrides.find(overrides.target_triple);
if (it != m->targetOverrides.end()) {
if (!it->second.toolchain.empty()) tcSpec = it->second.toolchain;
if (!it->second.linkage.empty()) m->buildConfig.linkage = it->second.linkage;
}
// Convention: "*-musl" target without an explicit `[target.X]`
// override gets the canonical musl-gcc spec the rest of mcpp
// uses internally. We can't just append "-musl" to the inherited
// toolchain version because xim doesn't have a `musl-gcc@<host
// gcc version>` for every gcc release — gcc 16.1 has no musl
// variant yet, only 9.4 / 11.5 / 13.3 / 15.1 do. Picking 15.1.0
// as the static default matches what mcpp itself uses for
// `mcpp build --target x86_64-linux-musl` (see mcpp.toml).
if (endswith(overrides.target_triple, "-musl")
&& (it == m->targetOverrides.end() || it->second.toolchain.empty()))
{
tcSpec = "gcc@15.1.0-musl";
}
if (endswith(overrides.target_triple, "-musl")
&& m->buildConfig.linkage.empty()) {
m->buildConfig.linkage = "static";
}
}
if (overrides.force_static) m->buildConfig.linkage = "static";
if (tcSpec.has_value() && *tcSpec != "system") {
auto spec = mcpp::toolchain::parse_toolchain_spec(*tcSpec);
if (!spec || spec->version.empty()) {
return std::unexpected(std::format(
"[toolchain].{} = '{}' is invalid; expected '<pkg>@<version>'",
kCurrentPlatform, *tcSpec));
}
auto pkg = mcpp::toolchain::to_xim_package(*spec);
auto cfg = get_cfg();
if (!cfg) return std::unexpected(cfg.error());
mcpp::fetcher::Fetcher fetcher(**cfg);
mcpp::ui::info("Resolving", "toolchain");
mcpp::fetcher::InstallProgressHandler progress;
auto payload = fetcher.resolve_xpkg_path(pkg.target(), /*autoInstall=*/true, &progress);
if (!payload) {
return std::unexpected(std::format(
"toolchain '{}': {}", *tcSpec, payload.error().message));
}
explicit_compiler = mcpp::toolchain::toolchain_frontend(payload->binDir, pkg);
if (!std::filesystem::exists(explicit_compiler)) {
return std::unexpected(std::format(
"toolchain payload '{}' has no known C++ frontend in {}",
pkg.target(), payload->binDir.string()));
}
mcpp::ui::info("Resolved",
std::format("{} → {}", *tcSpec,
mcpp::ui::shorten_path(explicit_compiler,
mcpp::fetcher::make_path_ctx(&**get_cfg(), *root))));
} else if (tcSpec.has_value() && *tcSpec == "system") {
// Explicit user opt-in to system PATH compiler — kept as escape hatch.
} else if (auto* opt = std::getenv("MCPP_NO_AUTO_INSTALL"); opt && *opt && *opt != '0') {
// CI / offline / test opt-out: hard-error instead of silently
// pulling ~800 MB of toolchain. Preserves the original M5.5
// contract for environments that need it.
if constexpr (mcpp::platform::is_macos || mcpp::platform::is_windows) {
return std::unexpected(
"no toolchain configured.\n"
" run one of:\n"
" mcpp toolchain install llvm 20.1.7\n"
" mcpp toolchain default llvm@20.1.7\n"
" or unset MCPP_NO_AUTO_INSTALL to let mcpp auto-install.");
} else {
return std::unexpected(
"no toolchain configured.\n"
" run one of:\n"
" mcpp toolchain install gcc 15.1.0-musl\n"
" mcpp toolchain default gcc@15.1.0-musl\n"
" or unset MCPP_NO_AUTO_INSTALL to let mcpp auto-install.");
}
} else {
// First-run UX: no project-level [toolchain], no global default,
// and the user just ran `mcpp build` (or similar). Auto-install
// the platform's canonical default so the user gets a working
// binary out of the box without any config. We pin it as the
// global default so the next invocation is silent.
// Users can switch any time via `mcpp toolchain default <spec>`.
//
// macOS: LLVM/Clang — Apple doesn't ship GCC; upstream LLVM with
// bundled libc++ is the self-contained choice.
// Linux: glibc gcc — the platform-native ABI. A musl-static default
// cannot link the glibc world (X11/GL/system libs), so it
// breaks GUI/native packages out of the box. musl-static stays
// opt-in via `mcpp build --target x86_64-linux-musl` for users
// who explicitly want portable static binaries.
std::string defaultSpec = (mcpp::platform::is_macos || mcpp::platform::is_windows)
? "llvm@20.1.7" : "gcc@16.1.0";
auto defaultParsed = mcpp::toolchain::parse_toolchain_spec(defaultSpec);
auto defaultPkg = mcpp::toolchain::to_xim_package(*defaultParsed);
if constexpr (mcpp::platform::is_macos || mcpp::platform::is_windows) {
mcpp::ui::info("First run",
std::format("no toolchain configured — installing {} (LLVM/Clang) as default",
defaultSpec));
} else {
mcpp::ui::info("First run",
std::format("no toolchain configured — installing {} (glibc, native ABI) as default",
defaultSpec));
}
auto cfg = get_cfg();
if (!cfg) return std::unexpected(cfg.error());
mcpp::fetcher::Fetcher fetcher(**cfg);
mcpp::fetcher::InstallProgressHandler progress;
// The glibc default toolchain needs the sysroot payloads (C library +
// kernel headers), exactly like `mcpp toolchain install` provides.
// The old musl-static default was self-contained, which masked this.
if constexpr (!mcpp::platform::is_macos && !mcpp::platform::is_windows) {
for (auto dep : {"xim:glibc", "xim:linux-headers"}) {
(void)fetcher.resolve_xpkg_path(dep, /*autoInstall=*/true, &progress);
}
}
auto payload = fetcher.resolve_xpkg_path(defaultPkg.target(),
/*autoInstall=*/true, &progress);
if (!payload) {
return std::unexpected(std::format(
"auto-installing default toolchain {} failed: {}\n"
" you can install it manually with:\n"
" mcpp toolchain install {} {}",
defaultSpec, payload.error().message,
defaultParsed->compiler, defaultParsed->version));
}
explicit_compiler = mcpp::toolchain::toolchain_frontend(payload->binDir, defaultPkg);
if (!std::filesystem::exists(explicit_compiler)) {
return std::unexpected(std::format(
"default toolchain payload {} has no known C++ frontend in {}",
defaultPkg.target(), payload->binDir.string()));
}
// The freshly-installed glibc gcc needs the SAME post-install fixup
// (patchelf + specs wiring against the sandbox glibc) that
// `mcpp toolchain install` performs — without it a fresh sandbox
// cannot find the C library (stdlib.h: No such file or directory).
if (defaultPkg.needsGccPostInstallFixup) {
mcpp::toolchain::gcc_post_install_fixup(**cfg, payload->root);
}
// Persist the default so we don't ask again next time.
if (auto wr = mcpp::config::write_default_toolchain(**cfg, defaultSpec); wr) {
(*cfg)->defaultToolchain = defaultSpec;
mcpp::ui::status("Default", std::format("set to {}", defaultSpec));
} // best-effort: a failed config write only loses the persistence,
// not the running build.
tcSpec = defaultSpec;
}
auto tc = mcpp::toolchain::detect(explicit_compiler);
if (!tc) return std::unexpected(tc.error().message);
// For musl-gcc the toolchain is fully self-contained
// (`<root>/x86_64-linux-musl/{include,lib}` is its own sysroot).
// musl-gcc's `-dumpmachine` reports `x86_64-linux-musl`.
bool isMuslTc = tc->targetTriple.find("-musl") != std::string::npos;
// A musl toolchain only really makes sense with static linkage —
// dynamic-musl binaries depend on a system /lib/ld-musl-x86_64.so.1
// that most distros don't ship. Default linkage to "static" when
// the resolved toolchain is musl, unless the user has already opted
// out via [build].linkage / [target.<triple>].linkage.
if (isMuslTc && m->buildConfig.linkage.empty()) {
m->buildConfig.linkage = "static";
}
// Sysroot comes from the toolchain payload itself (GCC -print-sysroot,
// Clang clang++.cfg). mcpp does not override it — the payload is
// self-describing. See docs: 2026-05-21-linux-sysroot-missing-kernel-headers.md
// Resolve dependencies: walk the **transitive** graph from the main
// manifest, BFS-style. Each unique `(namespace, shortName)` is fetched
// once, its `[build].include_dirs` are propagated to the main
// manifest, and its own `[dependencies]` are queued for processing
// (its `[dev-dependencies]` are NOT — those are private to the dep's
// own test runs).
//
// Conflict policy: C++ modules require globally-unique module names
// and ODR-respecting symbols, so the same `(ns, name)` resolved to
// two different exact versions is an error — mcpp prints both
// requesting parents and asks the user to align them.
// Auto-refresh the builtin package index only when a version dependency
// is actually routed there. Local/remote project indices are handled by
// the project-scoped setup below; refreshing the global index for those
// packages is both unnecessary and can make offline/local-index builds
// block on unrelated remote repositories.
if (!m->dependencies.empty()) {
auto usesBuiltinIndex = [&](const mcpp::manifest::DependencySpec& spec) {
if (spec.isPath() || spec.isGit()) return false;
auto ns = spec.namespace_.empty()
? std::string(mcpp::pm::kDefaultNamespace)
: spec.namespace_;
if (ns == mcpp::pm::kDefaultNamespace) return true;
auto it = m->indices.find(ns);
if (it == m->indices.end()) return true;
return it->second.is_builtin();
};
bool needsBuiltinIndexRefresh = false;
for (auto& [_, spec] : m->dependencies) {
if (usesBuiltinIndex(spec)) {
needsBuiltinIndexRefresh = true;
break;
}
}
if (needsBuiltinIndexRefresh) {
auto cfg2 = get_cfg();
if (cfg2) {
auto xlEnv = mcpp::config::make_xlings_env(**cfg2);
if (!mcpp::xlings::is_index_fresh(xlEnv, (*cfg2)->searchTtlSeconds)) {
mcpp::ui::status("Updating", "package index (auto-refresh)");
mcpp::xlings::ensure_index_fresh(
xlEnv, (*cfg2)->searchTtlSeconds, /*quiet=*/true);
}
}
}
}
// Set up project-level .mcpp/ directory for custom indices.
// This creates .mcpp/.xlings.json with custom non-builtin index
// entries so xlings can clone them into the project-scoped data dir.
if (!m->indices.empty()) {
auto cfg2 = get_cfg();
if (cfg2) {
mcpp::config::ensure_project_index_dir(**cfg2, *root, m->indices);
// On first build, the project index data root may be empty because
// ensure_project_index_dir only writes .xlings.json but does not
// trigger clone/link creation. Local path indices are read directly;
// remote custom indices are synced quietly before dependency resolution.
bool hasCustomIndices = false;
for (auto& [idxName, spec] : m->indices) {
if (!spec.is_builtin()) {
hasCustomIndices = true;
break;
}
}
if (hasCustomIndices) {
bool needsClone = !mcpp::config::project_index_data_initialized(*root);
if (needsClone) {
bool needsRemoteUpdate = false;
for (auto& [idxName, spec] : m->indices) {
if (spec.is_builtin() || spec.is_local()) continue;
needsRemoteUpdate = true;
break;
}
if (needsRemoteUpdate) {
mcpp::ui::status("Fetching", "custom index repos (first use)");
auto projEnv = mcpp::config::make_project_xlings_env(**cfg2, *root);
int rc = mcpp::xlings::update_index(projEnv, /*quiet=*/true);
if (rc != 0) {
return std::unexpected(
"project custom index update failed; run `mcpp index update` for details");
}
}
}
}
}
}
std::vector<mcpp::modgraph::PackageRoot> packages;
packages.push_back({*root, *m});
// dep_manifests is kept around purely so the build plan can move it
// out at the end (PackageRoot stores a `Manifest` by value, so the
// unique_ptr is not load-bearing for liveness — it's a leftover from
// an earlier design and harmless).
std::vector<std::unique_ptr<mcpp::manifest::Manifest>> dep_manifests;
auto cache_index_name = [](std::string_view ns) {
if (ns.empty()) return std::string(mcpp::pm::kDefaultNamespace);
return std::string(ns);
};
struct DepCacheIdentity {
std::string indexName;
std::string packageName;
std::string version;
};
std::vector<DepCacheIdentity> dep_cache_identities;
struct GitLockIdentity {
std::string source;
std::string hash;
};
std::map<std::string, GitLockIdentity> root_git_lock_identities;
struct ResolvedKey {
std::string ns;
std::string shortName;
auto operator<=>(const ResolvedKey&) const = default;
};
struct ResolvedRecord {
std::string version; // empty for path/git deps
std::string constraint; // AND-combined original constraints (version src only)
std::string requestedBy; // human-readable for error messages
std::string source; // "version" | "path" | "git" — for type-clash check
std::size_t depIndex = 0; // index into dep_manifests/packages-1 (for in-place re-fetch)
std::vector<std::string> linkFlagsAdded; // entries appended to m->buildConfig.ldflags by this dep
};
std::map<ResolvedKey, ResolvedRecord> resolved;
// Sentinel for "the consumer is the main package" (no dep_manifests entry).
constexpr std::size_t kMainConsumer = static_cast<std::size_t>(-1);
struct WorkItem {
std::string name; // dep map key as written
mcpp::manifest::DependencySpec spec; // copy (we may mutate version)
std::string requestedBy; // who asked for it
std::string originalConstraint; // spec.version BEFORE pinning (for SemVer merge)
std::size_t consumerDepIndex; // dep_manifests slot of who pushed this child; kMainConsumer for main
std::filesystem::path resolveRoot; // base dir for relative path deps (empty = use project root)
};
std::deque<WorkItem> worklist;
// SemVer constraint resolver, shared across the worklist so transitive
// deps with caret/range constraints (`^1.0`) also get pinned to a
// concrete version before fetch.
auto resolveSemver = [&](mcpp::manifest::DependencySpec& s,
const std::string& depName)
-> std::expected<void, std::string>
{
if (s.isPath() || s.isGit()) return {};
if (!mcpp::pm::is_version_constraint(s.version)) return {};
auto cfg = get_cfg();
if (!cfg) return std::unexpected(cfg.error());
mcpp::fetcher::Fetcher fetcher(**cfg);
// 0.0.10+: use structured namespace from DependencySpec.
auto resolved = mcpp::pm::resolve_semver(
s.namespace_, s.shortName.empty() ? depName : s.shortName,
s.version, fetcher);
if (!resolved) return std::unexpected(resolved.error());
mcpp::ui::info("Resolved",
std::format("{} {} → v{}", depName, s.version, *resolved));
s.version = std::move(*resolved);
return {};
};
// Acquire a version-source dep at a specific pinned version. Used both
// by the first-time walk and by the SemVer merger when a re-fetch at a
// different version is needed. Returns the dep's effective root (where
// mcpp.toml lives) and a fully loaded manifest.
using LoadedDep = std::pair<std::filesystem::path, mcpp::manifest::Manifest>;
// Helper: find the IndexSpec for a namespace from the manifest's [indices].
// Returns nullptr if the namespace maps to the default/builtin index.
auto findIndexForNs = [&](const std::string& ns)
-> const mcpp::pm::IndexSpec*
{
if (ns.empty() || ns == std::string(mcpp::pm::kDefaultNamespace)) return nullptr;
if (auto it = m->indices.find(ns); it != m->indices.end()) {
return &it->second;
}
auto root = ns.substr(0, ns.find('.'));
for (auto& [idxName, spec] : m->indices) {
if (idxName == ns) return &spec;
if (idxName == root) return &spec;
}
return nullptr;
};
auto canonicalXpkgLuaFilename =
[](std::string_view ns, std::string_view shortName) {
if (ns.empty() || ns == mcpp::pm::kDefaultNamespace) {
return std::string(shortName) + ".lua";
}
return std::format("{}.{}.lua", ns, shortName);
};
auto readStrictLuaFromPkgsDir =
[&](const std::filesystem::path& pkgsDir,
std::string_view ns,
std::string_view shortName) -> std::optional<std::string>
{
auto fname = canonicalXpkgLuaFilename(ns, shortName);
if (fname.empty()) return std::nullopt;
char first = static_cast<char>(std::tolower(
static_cast<unsigned char>(fname.front())));
auto candidate = pkgsDir / std::string(1, first) / fname;
if (!std::filesystem::exists(candidate)) return std::nullopt;
std::ifstream is(candidate);
std::stringstream ss;
ss << is.rdbuf();
return ss.str();
};
auto readStrictLuaForCandidate =
[&](const mcpp::pm::DependencyCoordinate& coord)
-> std::optional<std::string>
{
auto cfg = get_cfg();
if (!cfg) return std::nullopt;
auto* idxSpec = findIndexForNs(coord.namespace_);
if (idxSpec && idxSpec->is_local()) {
auto indexPath = mcpp::config::resolve_project_index_path(*root, *idxSpec);
return readStrictLuaFromPkgsDir(indexPath / "pkgs",
coord.namespace_,
coord.shortName);
}
if (idxSpec && !idxSpec->is_builtin()) {
std::error_code ec;
for (auto& data : mcpp::config::project_xlings_data_roots(*root)) {
if (!std::filesystem::exists(data)) continue;
for (auto& entry : std::filesystem::directory_iterator(data, ec)) {
if (!entry.is_directory()) continue;
auto pkgsDir = entry.path() / "pkgs";
if (auto lua = readStrictLuaFromPkgsDir(
pkgsDir, coord.namespace_, coord.shortName)) {
return lua;
}
}
}
return std::nullopt;
}
auto data = (*cfg)->xlingsHome() / "data";
if (!std::filesystem::exists(data)) return std::nullopt;
std::error_code ec;
for (auto& entry : std::filesystem::directory_iterator(data, ec)) {
if (!entry.is_directory()) continue;
auto pkgsDir = entry.path() / "pkgs";
if (auto lua = readStrictLuaFromPkgsDir(
pkgsDir, coord.namespace_, coord.shortName)) {
return lua;
}
}
return std::nullopt;
};
auto candidateQualifiedName =
[](std::string_view ns, std::string_view shortName) {
if (ns.empty()) return std::string(shortName);
return std::format("{}.{}", ns, shortName);
};
auto xpkgLuaMatchesCandidate =
[&](const mcpp::pm::DependencyCoordinate& coord,
std::string_view luaContent,
bool allowLegacyBareDefault) {
auto luaName = mcpp::manifest::extract_xpkg_name(luaContent);
if (luaName.empty()) return true;
auto luaNs = mcpp::manifest::extract_xpkg_namespace(luaContent);
auto qname = candidateQualifiedName(coord.namespace_, coord.shortName);
if (coord.namespace_.empty()) {
return luaNs.empty() && luaName == coord.shortName;
}
if (coord.namespace_ == mcpp::pm::kDefaultNamespace) {
if (luaNs == coord.namespace_) {
return luaName == coord.shortName || luaName == qname;
}
if (luaNs.empty() && luaName == qname) return true;
return allowLegacyBareDefault
&& luaNs.empty()
&& luaName == coord.shortName;
}
if (luaNs == coord.namespace_) {
return luaName == coord.shortName || luaName == qname;
}
return luaNs.empty() && luaName == qname;
};
auto dependencyCoordinates =
[](const mcpp::manifest::DependencySpec& spec,
const std::string& depName) {
if (!spec.candidates.empty()) return spec.candidates;
std::vector<mcpp::pm::DependencyCoordinate> out;
out.push_back({
.namespace_ = spec.namespace_.empty()
? std::string(mcpp::pm::kDefaultNamespace)
: spec.namespace_,
.shortName = spec.shortName.empty() ? depName : spec.shortName,
});
return out;
};
auto selectDependencyCandidate =
[&](mcpp::manifest::DependencySpec& spec,
const std::string& depName) -> std::expected<void, std::string>
{
auto candidates = dependencyCoordinates(spec, depName);
if (candidates.empty()) {
return std::unexpected(
std::format("dependency '{}' has no lookup candidates", depName));
}
auto selected = candidates.front();
if (spec.isVersion() && candidates.size() > 1) {
for (auto& candidate : candidates) {
auto lua = readStrictLuaForCandidate(candidate);
if (lua && xpkgLuaMatchesCandidate(
candidate, *lua, /*allowLegacyBareDefault=*/false)) {
selected = candidate;
break;
}
}
}