-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.cppm
More file actions
683 lines (600 loc) · 27.5 KB
/
config.cppm
File metadata and controls
683 lines (600 loc) · 27.5 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
// mcpp.config — global config + paths + xlings binary acquisition.
//
// Layout (per docs/14-data-layout.md):
// $MCPP_HOME/ default ~/.mcpp/
// bin/mcpp mcpp binary (self-contained mode)
// registry/ XLINGS_HOME for mcpp's xlings
// bin/xlings vendored xlings binary (= <XLINGS_HOME>/bin/xlings)
// .xlings.json seeded with index_repos = [mcpplibs]
// bmi/<fp>/ BMI cache (existing)
// cache/ metadata caches
// config.toml this module's input
//
// Initialization is silent and idempotent: every Config::load() ensures
// the directory tree exists and seeds .xlings.json if missing.
module;
#include <cstdio>
#include <cstdlib>
#if defined(_WIN32)
#include <windows.h>
#define popen _popen
#define pclose _pclose
#elif defined(__APPLE__)
#include <mach-o/dyld.h> // _NSGetExecutablePath
#endif
export module mcpp.config;
import std;
import mcpp.libs.toml;
import mcpp.pm.index_spec;
import mcpp.xlings;
export namespace mcpp::config {
inline constexpr std::string_view kXlingsPinnedVersion = mcpp::xlings::pinned::kXlingsVersion;
struct IndexRepo {
std::string name;
std::string url;
};
struct GlobalConfig {
// Resolved paths
std::filesystem::path mcppHome; // ~/.mcpp/
std::filesystem::path binDir; // mcppHome/bin
std::filesystem::path xlingsBinary; // mcppHome/registry/bin/xlings
std::filesystem::path registryDir; // mcppHome/registry
std::filesystem::path bmiCacheDir; // mcppHome/bmi
std::filesystem::path metaCacheDir; // mcppHome/cache
std::filesystem::path logDir; // mcppHome/log
std::filesystem::path configFile; // mcppHome/config.toml
// From config.toml [xlings]
std::string xlingsBinaryMode; // "bundled" | "system" | absolute path
std::filesystem::path xlingsHomeOverride; // empty = use registryDir
// From config.toml [index]
std::string defaultIndex; // "mcpplibs"
std::vector<IndexRepo> indexRepos;
// From config.toml [indices] — custom index repositories (new schema).
// Merged: project mcpp.toml > global config.toml > built-in default.
std::map<std::string, mcpp::pm::IndexSpec> indices;
// From config.toml [cache]
std::int64_t searchTtlSeconds = 3600;
// From config.toml [build]
std::int64_t defaultJobs = 0;
std::string defaultBackend = "ninja";
// From config.toml [toolchain] (M5.5)
// default = "<compiler>@<version>" e.g. "gcc@15.1.0"
// Empty means no global default; mcpp will hard-error unless the project
// mcpp.toml declares its own [toolchain].
std::string defaultToolchain;
// Resolved xlings home (registryDir unless overridden)
std::filesystem::path xlingsHome() const {
return xlingsHomeOverride.empty() ? registryDir : xlingsHomeOverride;
}
};
// Create an xlings::Env from the resolved GlobalConfig.
mcpp::xlings::Env make_xlings_env(const GlobalConfig& cfg) {
#if defined(_WIN32)
// On Windows, the copied xlings binary in the sandbox may not function
// correctly for large package installs (missing runtime environment).
// When MCPP_VENDORED_XLINGS is set, use the original xlings binary
// directly — it has the full xlings runtime. The XLINGS_HOME env var
// ensures packages are installed into the mcpp sandbox.
if (auto* e = std::getenv("MCPP_VENDORED_XLINGS"); e && *e) {
std::filesystem::path vendored{e};
if (std::filesystem::exists(vendored))
return { vendored, cfg.xlingsHome() };
}
#endif
return { cfg.xlingsBinary, cfg.xlingsHome() };
}
// Create an xlings::Env that targets the project-level .mcpp/ directory.
// Used when custom (non-builtin) indices are configured in mcpp.toml.
mcpp::xlings::Env make_project_xlings_env(const GlobalConfig& cfg,
const std::filesystem::path& projectDir) {
return { cfg.xlingsBinary, cfg.xlingsHome(), projectDir / ".mcpp" };
}
// Ensure the project-level .mcpp/ directory exists and contains a
// .xlings.json seeded with the custom (non-builtin, non-local) index
// entries. Returns true if a .mcpp/ directory was created/updated.
bool ensure_project_index_dir(
const GlobalConfig& cfg,
const std::filesystem::path& projectDir,
const std::map<std::string, mcpp::pm::IndexSpec>& indices);
struct ConfigError { std::string message; };
// Load (or create) the global config. Idempotent. Performs:
// 1. Resolve $MCPP_HOME (env or ~/.mcpp)
// 2. Create directory tree if missing
// 3. Seed config.toml if missing
// 4. Seed registry/.xlings.json if missing (so xlings won't auto-add awesome)
// 5. Acquire xlings binary if missing (system copy → release download → fail)
// Streaming download-progress info for the sandbox bootstrap step (patchelf,
// ninja). cli.cppm wraps a ui::ProgressBar around this so the user sees the
// same percent / MB / s display they get for `mcpp toolchain install`.
//
// We can't use mcpp.fetcher's EventHandler here because fetcher imports
// config — the dependency would be cyclic.
using BootstrapFile = mcpp::xlings::BootstrapFile;
using BootstrapProgress = mcpp::xlings::BootstrapProgress;
using BootstrapProgressCallback = mcpp::xlings::BootstrapProgressCallback;
std::expected<GlobalConfig, ConfigError> load_or_init(
bool quiet = false,
BootstrapProgressCallback onBootstrapProgress = {});
// Pretty-print resolved config for `mcpp env` command.
void print_env(const GlobalConfig& cfg);
// M5.5: persist [toolchain].default to config.toml.
std::expected<void, ConfigError>
write_default_toolchain(const GlobalConfig& cfg, std::string_view spec);
} // namespace mcpp::config
namespace mcpp::config {
namespace t = mcpp::libs::toml;
namespace {
// Resolve MCPP_HOME, in priority order:
// 1. $MCPP_HOME env var (explicit override — used by CI / dev / multi-instance)
// 2. <binary-dir>/.. — self-contained mode, when mcpp lives at
// <root>/bin/mcpp. Release tarballs and `xlings install mcpp`
// use this layout; the unpacked tree IS the home. Dev builds
// live under target/<triple>/<fp>/bin/mcpp, which is the same
// "in a bin/ dir" shape — so we additionally exclude any path
// with a "target" ancestor as mcpp's own dev convention.
// 3. fallback to $HOME/.mcpp.
// Right-pad a verb to 12 columns, matching mcpp::ui::verb_padded so the
// bootstrap status lines line up under the cyan "Downloading …" lines
// produced via the BootstrapProgressCallback. We can't import mcpp.ui
// from here (cyclic dep), so this is a tiny duplicate of that helper —
// no color, no fanciness.
void print_status(std::string_view verb, std::string_view msg) {
constexpr std::size_t W = 12;
if (verb.size() >= W) {
std::println("{} {}", verb, msg);
} else {
std::println("{}{} {}", std::string(W - verb.size(), ' '), verb, msg);
}
}
std::filesystem::path home_dir() {
if (auto* e = std::getenv("MCPP_HOME"); e && *e)
return std::filesystem::path(e);
std::error_code ec;
#if defined(_WIN32)
char _exe_buf[MAX_PATH];
DWORD _exe_len = GetModuleFileNameA(NULL, _exe_buf, MAX_PATH);
std::filesystem::path exe;
if (_exe_len > 0 && _exe_len < MAX_PATH)
exe = std::filesystem::canonical(_exe_buf, ec);
#elif defined(__APPLE__)
char _exe_buf[4096];
uint32_t _exe_size = sizeof(_exe_buf);
std::filesystem::path exe;
if (_NSGetExecutablePath(_exe_buf, &_exe_size) == 0)
exe = std::filesystem::canonical(_exe_buf, ec);
#else
auto exe = std::filesystem::canonical("/proc/self/exe", ec);
#endif
if (!ec && exe.parent_path().filename() == "bin") {
// Dev builds emit binaries at target/<triple>/<fp>/bin/<exe>,
// matching the bin/ shape. Any ancestor literally named
// "target" disqualifies self-contained mode and falls through
// to $HOME/.mcpp — so first-run on a dev binary doesn't drop
// a half-populated sandbox into target/.
bool isDevPath = false;
for (auto p = exe.parent_path();
!p.empty() && p != p.parent_path();
p = p.parent_path()) {
if (p.filename() == "target") { isDevPath = true; break; }
}
if (!isDevPath)
return exe.parent_path().parent_path();
}
if (auto* e = std::getenv("HOME"); e && *e)
return std::filesystem::path(e) / ".mcpp";
return std::filesystem::current_path() / ".mcpp";
}
std::expected<std::string, std::string> run_capture(const std::string& cmd) {
return mcpp::xlings::run_capture(cmd);
}
void write_file(const std::filesystem::path& p, std::string_view content) {
std::error_code ec;
std::filesystem::create_directories(p.parent_path(), ec);
std::ofstream os(p);
os << content;
}
bool write_default_config_toml(const std::filesystem::path& path) {
constexpr auto tmpl = R"(# mcpp global config — auto-generated; safe to edit.
[xlings]
# binary: "bundled" (use $MCPP_HOME/registry/bin/xlings) | "system" | absolute path
binary = "bundled"
# home: empty = use $MCPP_HOME/registry; can override
home = ""
[index]
default = "mcpplibs"
[index.repos."mcpplibs"]
url = "https://github.com/mcpp-community/mcpp-index.git"
# xlings auto-adds xim / awesome / scode / d2x as defaults.
[cache]
search_ttl_seconds = 3600
[build]
default_jobs = 0
default_backend = "ninja"
)";
write_file(path, tmpl);
return std::filesystem::exists(path);
}
bool write_default_xlings_json(const std::filesystem::path& path,
const std::vector<IndexRepo>& repos)
{
// Delegate to xlings module. Convert IndexRepo vec to pair span.
std::vector<std::pair<std::string,std::string>> pairs;
pairs.reserve(repos.size());
for (auto& r : repos) pairs.emplace_back(r.name, r.url);
// seed_xlings_json writes to env.home / ".xlings.json", so we
// construct a temporary Env with home = path.parent_path().
mcpp::xlings::Env env;
env.home = path.parent_path();
mcpp::xlings::seed_xlings_json(env, pairs);
return std::filesystem::exists(path);
}
bool replace_all(std::string& text, std::string_view from, std::string_view to) {
bool changed = false;
for (std::size_t pos = 0;
(pos = text.find(from, pos)) != std::string::npos;) {
text.replace(pos, from.size(), to);
pos += to.size();
changed = true;
}
return changed;
}
bool write_text_if_changed(const std::filesystem::path& path,
const std::string& original,
const std::string& updated) {
if (updated == original) return false;
write_file(path, updated);
return true;
}
bool migrate_legacy_config_toml_index_names(const std::filesystem::path& path) {
std::ifstream is(path);
if (!is) return false;
std::stringstream ss;
ss << is.rdbuf();
auto original = ss.str();
auto updated = original;
replace_all(updated, "default = \"mcpp-index\"", "default = \"mcpplibs\"");
replace_all(updated, "[index.repos.\"mcpp-index\"]", "[index.repos.\"mcpplibs\"]");
return write_text_if_changed(path, original, updated);
}
bool migrate_legacy_xlings_json_index_names(const std::filesystem::path& path) {
std::ifstream is(path);
if (!is) return false;
std::stringstream ss;
ss << is.rdbuf();
auto original = ss.str();
auto updated = original;
// Older mcpp sandboxes seeded the package index under the repository
// name. Keep the URL and all xlings state intact; only rename the index
// key so xlings config/list output matches mcpp's default namespace.
replace_all(updated, "\"name\": \"mcpp-index\"", "\"name\": \"mcpplibs\"");
replace_all(updated, "\"name\":\"mcpp-index\"", "\"name\":\"mcpplibs\"");
return write_text_if_changed(path, original, updated);
}
void canonicalize_legacy_index_names(GlobalConfig& cfg) {
if (cfg.defaultIndex == "mcpp-index")
cfg.defaultIndex = "mcpplibs";
std::vector<IndexRepo> normalized;
for (auto r : cfg.indexRepos) {
if (r.name == "mcpp-index"
&& r.url == "https://github.com/mcpp-community/mcpp-index.git") {
r.name = "mcpplibs";
}
bool duplicate = std::any_of(normalized.begin(), normalized.end(),
[&](const IndexRepo& existing) {
return existing.name == r.name && existing.url == r.url;
});
if (!duplicate) normalized.push_back(std::move(r));
}
cfg.indexRepos = std::move(normalized);
}
// Try to acquire xlings binary. Returns the path if successful.
std::expected<std::filesystem::path, std::string>
acquire_xlings_binary(const std::filesystem::path& destBin, bool quiet)
{
if (std::filesystem::exists(destBin)) return destBin;
std::error_code ec;
std::filesystem::create_directories(destBin.parent_path(), ec);
// 1. Explicit override
if (auto* e = std::getenv("MCPP_VENDORED_XLINGS"); e && *e) {
std::filesystem::path src{e};
if (std::filesystem::exists(src)) {
std::filesystem::copy_file(src, destBin,
std::filesystem::copy_options::overwrite_existing, ec);
if (!ec) {
std::filesystem::permissions(destBin,
std::filesystem::perms::owner_exec
| std::filesystem::perms::group_exec
| std::filesystem::perms::others_exec,
std::filesystem::perm_options::add, ec);
if (!quiet) print_status("Bundled",
std::format("xlings v{} (from MCPP_VENDORED_XLINGS)", kXlingsPinnedVersion));
return destBin;
}
}
}
// 2. Copy from system (`which xlings`)
#if defined(_WIN32)
auto sys = run_capture("where xlings.exe 2>nul");
#else
auto sys = run_capture("command -v xlings 2>/dev/null");
#endif
if (sys) {
std::string p = *sys;
while (!p.empty() && (p.back() == '\n' || p.back() == '\r')) p.pop_back();
if (!p.empty() && std::filesystem::exists(p)) {
std::filesystem::copy_file(p, destBin,
std::filesystem::copy_options::overwrite_existing, ec);
if (!ec) {
std::filesystem::permissions(destBin,
std::filesystem::perms::owner_exec
| std::filesystem::perms::group_exec
| std::filesystem::perms::others_exec,
std::filesystem::perm_options::add, ec);
if (!quiet) print_status("Bundled",
std::format("xlings (copied from system: {})", p));
return destBin;
}
}
}
// 3. Download from GitHub Release (placeholder — real impl uses curl)
// We delegate to curl/wget in the bash bootstrap; for in-process robustness
// we just instruct the user.
return std::unexpected(std::format(
"xlings binary not found. Either:\n"
" - install via: curl -fsSL https://raw.githubusercontent.com/d2learn/xlings/refs/heads/main/tools/other/quick_install.sh | bash\n"
" - export MCPP_VENDORED_XLINGS=/abs/path/to/xlings\n"
" - set [xlings].binary = \"system\" in {}",
(destBin.parent_path().parent_path() / "config.toml").string()));
}
// Run `xlings self init` against the sandbox to create the standard
// directory layout (subos/default/{bin,lib,usr,generations}, data/, config/,
// shell profiles, and the empty workspace .xlings.json). Idempotent.
//
// TODO(xlings-upstream): Once xlings ships a `xlings sandbox bootstrap` /
// `xlings self init --copy-self-bin` API that does init + binary placement
// + patchelf in one shot, this function and ensure_sandbox_xlings_binary +
// ensure_sandbox_patchelf below can collapse into a single call.
void ensure_sandbox_init(const GlobalConfig& cfg, bool quiet) noexcept {
mcpp::xlings::ensure_init(make_xlings_env(cfg), quiet);
}
// With the 0.0.4 layout change (xlings binary at <MCPP_HOME>/registry/bin/
// = <XLINGS_HOME>/bin/), the bundled xlings IS already at the path xlings's
// shim-creation guard checks (`paths.homeDir / "bin" / "xlings"`).
// No mirroring / hardlinking needed — this function is now a no-op.
void ensure_sandbox_xlings_binary(const GlobalConfig& /*cfg*/, bool /*quiet*/) noexcept {
// Intentional no-op: xlingsBinary == xlingsHome()/bin/xlings.
}
// Bootstrap install: delegated to mcpp.xlings module.
void ensure_sandbox_ninja(const GlobalConfig& cfg, bool quiet,
const BootstrapProgressCallback& cb) noexcept
{
mcpp::xlings::ensure_ninja(make_xlings_env(cfg), quiet, cb);
}
void ensure_sandbox_patchelf(const GlobalConfig& cfg, bool quiet,
const BootstrapProgressCallback& cb) noexcept
{
mcpp::xlings::ensure_patchelf(make_xlings_env(cfg), quiet, cb);
}
} // namespace
std::expected<GlobalConfig, ConfigError> load_or_init(
bool quiet,
BootstrapProgressCallback onBootstrapProgress)
{
GlobalConfig cfg;
// 1. Resolve paths
cfg.mcppHome = home_dir();
cfg.binDir = cfg.mcppHome / "bin";
cfg.registryDir = cfg.mcppHome / "registry";
// xlings lives under registry/, not bin/ — it's a registry tool,
// not a user-facing binary. This also places it exactly at
// <XLINGS_HOME>/bin/xlings, which satisfies xlings's own shim-
// creation guard (`if fs::exists(homeDir/"bin"/"xlings")`),
// making ensure_sandbox_xlings_binary() a no-op.
#if defined(_WIN32)
cfg.xlingsBinary = cfg.registryDir / "bin" / "xlings.exe";
#else
cfg.xlingsBinary = cfg.registryDir / "bin" / "xlings";
#endif
cfg.bmiCacheDir = cfg.mcppHome / "bmi";
cfg.metaCacheDir = cfg.mcppHome / "cache";
cfg.logDir = cfg.mcppHome / "log";
cfg.configFile = cfg.mcppHome / "config.toml";
// 2. Create directory tree
std::error_code ec;
for (auto& d : { cfg.binDir, cfg.registryDir, cfg.bmiCacheDir,
cfg.metaCacheDir, cfg.logDir }) {
std::filesystem::create_directories(d, ec);
if (ec) return std::unexpected(ConfigError{
std::format("cannot create '{}': {}", d.string(), ec.message())});
}
// 3. Seed config.toml if missing
bool fresh_config = !std::filesystem::exists(cfg.configFile);
if (fresh_config) write_default_config_toml(cfg.configFile);
else migrate_legacy_config_toml_index_names(cfg.configFile);
// 4. Load config.toml
auto doc = t::parse_file(cfg.configFile);
if (!doc) {
return std::unexpected(ConfigError{
std::format("invalid config.toml: {}", doc.error().message)});
}
cfg.xlingsBinaryMode = doc->get_string("xlings.binary").value_or("bundled");
if (auto h = doc->get_string("xlings.home"); h && !h->empty())
cfg.xlingsHomeOverride = *h;
cfg.defaultIndex = doc->get_string("index.default").value_or("mcpplibs");
cfg.searchTtlSeconds = doc->get_int("cache.search_ttl_seconds").value_or(3600);
cfg.defaultJobs = doc->get_int("build.default_jobs").value_or(0);
cfg.defaultBackend = doc->get_string("build.default_backend").value_or("ninja");
cfg.defaultToolchain = doc->get_string("toolchain.default").value_or("");
// [index.repos.NAME] tables
if (auto* repos = doc->get_table("index.repos")) {
for (auto& [name, val] : *repos) {
if (!val.is_table()) continue;
auto& tt = val.as_table();
auto it = tt.find("url");
if (it == tt.end() || !it->second.is_string()) continue;
cfg.indexRepos.push_back({ name, it->second.as_string() });
}
}
// [indices] — new-schema custom index repositories.
// Accepts the same short/long/path forms as mcpp.toml [indices].
if (auto* indices_t = doc->get_table("indices")) {
for (auto& [k, v] : *indices_t) {
mcpp::pm::IndexSpec spec;
spec.name = k;
if (v.is_string()) {
spec.url = v.as_string();
} else if (v.is_table()) {
auto& sub = v.as_table();
if (auto it = sub.find("url"); it != sub.end() && it->second.is_string()) spec.url = it->second.as_string();
if (auto it = sub.find("rev"); it != sub.end() && it->second.is_string()) spec.rev = it->second.as_string();
if (auto it = sub.find("tag"); it != sub.end() && it->second.is_string()) spec.tag = it->second.as_string();
if (auto it = sub.find("branch"); it != sub.end() && it->second.is_string()) spec.branch = it->second.as_string();
if (auto it = sub.find("path"); it != sub.end() && it->second.is_string()) spec.path = it->second.as_string();
}
if (!spec.url.empty() || !spec.path.empty())
cfg.indices[k] = std::move(spec);
}
}
// Defaults: only mcpplibs. xlings auto-adds its own standard
// defaults (xim / awesome / scode / d2x) because globalIndexRepos_
// is non-empty (per xlings/src/core/config.cppm). Explicitly listing
// them ourselves can cause cross-index name conflicts during
// dependency resolution (e.g. linux-headers existing in both
// scode and xim). See docs/21 §VII.
auto add_default = [&](std::string_view name, std::string_view url) {
for (auto& r : cfg.indexRepos) if (r.name == name) return;
cfg.indexRepos.push_back({ std::string(name), std::string(url) });
};
add_default("mcpplibs", "https://github.com/mcpp-community/mcpp-index.git");
canonicalize_legacy_index_names(cfg);
// 5. Seed registry/.xlings.json if missing; migrate legacy cached
// sandboxes in place so CI/user caches move from mcpp-index to mcpplibs
// without losing xlings' active subos or version bindings.
auto xjson = cfg.xlingsHome() / ".xlings.json";
if (!std::filesystem::exists(xjson)) {
write_default_xlings_json(xjson, cfg.indexRepos);
} else {
migrate_legacy_xlings_json_index_names(xjson);
}
// 6. Acquire xlings binary if needed
if (cfg.xlingsBinaryMode == "bundled") {
auto xbin = acquire_xlings_binary(cfg.xlingsBinary, quiet);
if (!xbin) return std::unexpected(ConfigError{xbin.error()});
} else if (cfg.xlingsBinaryMode == "system") {
#if defined(_WIN32)
auto sys = run_capture("where xlings.exe 2>nul");
#else
auto sys = run_capture("command -v xlings 2>/dev/null");
#endif
if (!sys || sys->empty())
return std::unexpected(ConfigError{"system xlings not found in PATH"});
std::string p = *sys;
while (!p.empty() && (p.back() == '\n' || p.back() == '\r')) p.pop_back();
cfg.xlingsBinary = p;
} else {
cfg.xlingsBinary = cfg.xlingsBinaryMode;
if (!std::filesystem::exists(cfg.xlingsBinary))
return std::unexpected(ConfigError{std::format(
"configured xlings binary not found: {}", cfg.xlingsBinary.string())});
}
// 7. Sandbox bootstrap (mcpp self-contained xlings environment).
// Order matters:
// a. Mirror xlings binary into sandbox so shim creation works.
// b. xlings self init: creates subos/default/{bin,lib,usr} skeleton.
// c. Install patchelf so xim install hooks can patch ELF binaries.
//
// TODO(xlings-upstream): collapse into a single
// `xlings sandbox bootstrap --home <X>` once that command exists
// upstream (see docs/short-term-vs-long-track plan).
ensure_sandbox_xlings_binary(cfg, quiet);
ensure_sandbox_init(cfg, quiet);
#if !defined(__APPLE__) && !defined(_WIN32)
// patchelf is ELF-only; macOS uses Mach-O and Windows uses PE.
ensure_sandbox_patchelf(cfg, quiet, onBootstrapProgress);
#endif
ensure_sandbox_ninja(cfg, quiet, onBootstrapProgress);
return cfg;
}
void print_env(const GlobalConfig& cfg) {
std::println("MCPP_HOME = {}", cfg.mcppHome.string());
std::println("xlings binary = {}", cfg.xlingsBinary.string());
std::println("xlings home = {}", cfg.xlingsHome().string());
std::println("config = {}", cfg.configFile.string());
std::println("BMI cache = {}", cfg.bmiCacheDir.string());
std::println("meta cache = {}", cfg.metaCacheDir.string());
if (!cfg.defaultToolchain.empty())
std::println("default toolchain = {}", cfg.defaultToolchain);
else
std::println("default toolchain = (none — run `mcpp toolchain install gcc 16.1.0`)");
std::println("");
std::println("Index repos:");
for (auto& r : cfg.indexRepos) {
bool isDefault = (r.name == cfg.defaultIndex);
std::println(" {} {}{}",
r.name, r.url, isDefault ? " (default)" : "");
}
}
bool ensure_project_index_dir(
const GlobalConfig& cfg,
const std::filesystem::path& projectDir,
const std::map<std::string, mcpp::pm::IndexSpec>& indices)
{
// Collect custom (non-builtin, non-local) indices that need xlings cloning.
std::vector<std::pair<std::string,std::string>> customRepos;
for (auto& [name, spec] : indices) {
if (spec.is_builtin()) continue;
if (spec.is_local()) continue; // local path, mcpp reads directly
customRepos.emplace_back(name, spec.url);
}
if (customRepos.empty()) return false; // nothing to do
auto dotMcpp = projectDir / ".mcpp";
std::error_code ec;
std::filesystem::create_directories(dotMcpp, ec);
// Seed .xlings.json with the custom index entries.
mcpp::xlings::Env env;
env.home = dotMcpp;
mcpp::xlings::seed_xlings_json(env, customRepos);
return true;
}
// M5.5: persist [toolchain].default into config.toml without disturbing
// other fields. Naive: read text, replace/insert one line.
std::expected<void, ConfigError>
write_default_toolchain(const GlobalConfig& cfg, std::string_view spec) {
std::ifstream is(cfg.configFile);
if (!is) return std::unexpected(ConfigError{
std::format("cannot open '{}'", cfg.configFile.string())});
std::stringstream ss; ss << is.rdbuf();
std::string text = ss.str();
std::string line = std::format("default = \"{}\"\n", spec);
auto sectionPos = text.find("[toolchain]");
if (sectionPos == std::string::npos) {
// Append a [toolchain] block at end.
if (!text.empty() && text.back() != '\n') text += '\n';
text += std::format("\n[toolchain]\n{}", line);
} else {
// Locate existing `default = ...` within [toolchain]. If absent,
// insert just after the section header.
auto eol = text.find('\n', sectionPos);
if (eol == std::string::npos) eol = text.size();
auto bodyStart = (eol == text.size()) ? text.size() : eol + 1;
auto nextSec = text.find("\n[", bodyStart);
auto bodyEnd = (nextSec == std::string::npos) ? text.size() : nextSec;
auto body = std::string_view(text).substr(bodyStart, bodyEnd - bodyStart);
auto k = body.find("default");
if (k != std::string_view::npos) {
// replace that whole line
auto kAbs = bodyStart + k;
auto lineEnd = text.find('\n', kAbs);
if (lineEnd == std::string::npos) lineEnd = text.size();
text.replace(kAbs, lineEnd - kAbs + 1, line);
} else {
text.insert(bodyStart, line);
}
}
std::ofstream os(cfg.configFile);
if (!os) return std::unexpected(ConfigError{
std::format("cannot write '{}'", cfg.configFile.string())});
os << text;
return {};
}
} // namespace mcpp::config