-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmanifest.cppm
More file actions
1828 lines (1681 loc) · 75.3 KB
/
manifest.cppm
File metadata and controls
1828 lines (1681 loc) · 75.3 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.manifest — load and validate mcpp.toml
export module mcpp.manifest;
import std;
import mcpp.libs.toml;
import mcpp.pm.dep_spec; // M5.x pm/ subsystem refactor: DependencySpec lives here
import mcpp.pm.compat; // Legacy dependency-key compatibility helpers
import mcpp.pm.dependency_selector;
import mcpp.pm.index_spec; // IndexSpec for [indices] section
import mcpp.platform;
export namespace mcpp::manifest {
// PR-R1 transitional: the dependency data model has moved into
// `mcpp.pm.dep_spec`. The aliases below keep `mcpp::manifest::DependencySpec`
// and `mcpp::manifest::kDefaultNamespace` available as before so existing
// callers (`cli.cppm`, `fetcher.cppm`, ...) compile unchanged. A later
// refactor PR will migrate call sites to reference `mcpp::pm::` directly
// and these aliases can disappear.
using DependencySpec = mcpp::pm::DependencySpec;
inline constexpr auto kDefaultNamespace = mcpp::pm::kDefaultNamespace;
struct CppStandardConfig {
std::string canonical = "c++23";
std::string flag = "-std=c++23";
int level = 23;
bool gnuDialect = false;
};
struct Package {
std::string name;
std::string namespace_; // xpkg V1 namespace field (0.0.6+); empty = infer from name
std::string version;
std::string standard = "c++23"; // C++ standard (M5.0: moved from [language])
std::string description;
std::string license;
std::vector<std::string> authors;
std::string repo;
};
struct Language {
std::string standard = "c++23";
bool modules = true;
bool importStd = true;
};
struct Modules {
std::vector<std::string> sources; // glob patterns
std::vector<std::string> exports_; // declared module names (optional)
bool strict = false;
};
struct Target {
std::string name;
enum Kind { Library, Binary, SharedLibrary, TestBinary } kind;
std::string main; // for binary / test
};
// `DependencySpec` and `kDefaultNamespace` have moved to mcpp.pm.dep_spec.
// Aliases at the top of this file keep `mcpp::manifest::DependencySpec`
// resolvable for unchanged call sites.
// `[toolchain]` section per docs/21-toolchain-and-tools.md
// linux = "gcc@15.1.0"
// macos = "llvm@20"
// windows = "msvc@system"
// default = "gcc@15.1.0" (used when current platform isn't listed)
struct Toolchain {
std::map<std::string, std::string> byPlatform; // platform -> "pkg@ver"
// Returns the toolchain spec for a platform, falling back to "default".
std::optional<std::string> for_platform(std::string_view platform) const {
if (auto it = byPlatform.find(std::string(platform)); it != byPlatform.end()) {
return it->second;
}
if (auto it = byPlatform.find("default"); it != byPlatform.end()) {
return it->second;
}
return std::nullopt;
}
};
// `[build]` section — tunables for the build backend.
//
// M5.0: now also carries `sources` (moved from [modules]) and `include_dirs`
// (new). Defaults are injected by load() after parse if these are empty.
struct BuildConfig {
std::vector<std::string> sources; // glob patterns
std::vector<std::filesystem::path> includeDirs; // relative to package root
std::map<std::filesystem::path, std::string> generatedFiles; // Form B package-owned support files
bool staticStdlib = true;
// "" (default = dynamic), "static", "dynamic" — chosen at resolve
// time from --static / --target / [target.<triple>].linkage. Wired
// through to ninja backend as the `-static` link flag.
std::string linkage;
// M5.x C-language support. `cflags` / `cxxflags` are appended verbatim
// to the per-rule baseline (see `ninja_backend` cflags / cxxflags).
// `cStandard` controls -std= for the C compile rule (.c files).
// Empty cStandard → backend default ("c11" today).
std::vector<std::string> cflags;
std::vector<std::string> cxxflags;
std::vector<std::string> ldflags;
std::string cStandard;
};
// `[runtime]` — requirements needed when launching built binaries.
struct RuntimeConfig {
std::vector<std::filesystem::path> libraryDirs; // relative to package root
std::vector<std::string> dlopenLibs; // runtime-loaded sonames
std::vector<std::string> capabilities; // host/system capabilities
};
// `[target.<triple>]` — per-target overrides.
// Picked up when caller passes --target <triple> to build/run/test.
struct TargetEntry {
std::string toolchain; // e.g. "gcc@15.1.0-musl"; empty = inherit [toolchain]
std::string linkage; // "static" | "dynamic" | "" (= auto by libc)
};
// `[lib]` — library "root" interface convention.
//
// Convention-over-configuration: a library package's primary module
// interface lives at `src/<package-tail>.cppm`, where `<package-tail>` is
// the last dotted segment of `[package].name` (e.g. `mcpplibs.tinyhttps`
// → `src/tinyhttps.cppm`). That file declares `export module
// <full-package-name>;` and re-exports the public partitions. The lib
// root then drives:
// * `[modules].exports` default (the lib root's module = the only
// externally-visible base module),
// * `mcpp publish` xpkg generation (consumer just `import <name>;`),
// * downstream tooling (docs / explain) entry point.
//
// Override the convention with `[lib].path = "src/foo.cppm"` (cargo-style)
// — the file must still `export module <package-name>;` (no partition).
//
// Lib-root is only meaningful for projects that ship a `kind = "lib"`
// target. Pure-binary projects (mcpp itself, scaffolded `mcpp new`)
// don't trigger any lib-root checks.
struct LibConfig {
std::filesystem::path path; // explicit override; empty = use convention
};
// `[pack]` — `mcpp pack` configuration. See docs/35-pack-design.md.
//
// `default_mode` picks the bundling strategy when the user runs bare
// `mcpp pack` (no `--mode` flag):
// "static" — full musl static, no PT_INTERP / RUNPATH
// "bundle-project" — bundle only project's third-party .so (default)
// "bundle-all" — bundle every dynamic dep including libc / libstdc++
struct PackConfig {
std::string defaultMode; // empty → "bundle-project"
std::vector<std::string> include; // extra files/globs to ship
std::vector<std::string> exclude; // patterns to drop from include
// Mode C overrides — let the user expand or contract the PEP 600
// skip list when their target distros differ from the default
// assumption ("modern desktop Linux").
std::vector<std::string> alsoSkip; // libs to ALSO skip on top of PEP 600
std::vector<std::string> forceBundle; // libs to bundle even if PEP 600 says skip
};
// `[workspace]` — multi-package workspace support (0.0.11+).
//
// A workspace root mcpp.toml declares member packages. Members share
// a unified lock file, target directory, and can inherit dependency
// versions via `.workspace = true`.
//
// Virtual workspace (no [package]): pure management node.
// Rooted workspace ([package] + [workspace]): root is also a package.
struct WorkspaceConfig {
std::vector<std::string> members; // relative paths to member dirs
std::vector<std::string> exclude; // paths to exclude
std::map<std::string, DependencySpec> dependencies; // [workspace.dependencies]
bool present = false;
};
struct Manifest {
std::filesystem::path sourcePath; // mcpp.toml's filesystem path
Package package;
Language language;
Modules modules;
std::vector<Target> targets;
// version-string keyed dependencies (M2 short form only).
std::map<std::string, DependencySpec> dependencies;
std::map<std::string, DependencySpec> devDependencies;
std::map<std::string, DependencySpec> buildDependencies; // host-side tools (M5+ behavior)
Toolchain toolchain; // optional; empty == fallback
BuildConfig buildConfig;
RuntimeConfig runtimeConfig;
// [target.<triple>] tables — empty if user didn't declare any.
std::map<std::string, TargetEntry> targetOverrides;
// [pack] — `mcpp pack` config (see docs/35-pack-design.md).
PackConfig packConfig;
// [lib] — library root interface convention (M5.x+).
LibConfig lib;
// [workspace] — multi-package workspace.
WorkspaceConfig workspace;
// [indices] — custom package index repositories (index-name → IndexSpec).
std::map<std::string, mcpp::pm::IndexSpec> indices;
// M5.0: post-parse computed/inferred state
CppStandardConfig cppStandard;
bool usesModules = true; // refined by scanner
bool usesImportStd = true; // refined by scanner
std::vector<std::string> inferredNotes; // for `Inferred ...` banner
};
struct ManifestError {
std::string message;
std::filesystem::path file;
std::size_t line = 0;
std::size_t column = 0;
std::string format() const {
if (line)
return std::format("{}:{}:{}: error: {}", file.string(), line, column, message);
return std::format("{}: error: {}", file.string(), message);
}
};
std::expected<Manifest, ManifestError> parse_string(std::string_view content,
const std::filesystem::path& origin = "mcpp.toml");
std::expected<Manifest, ManifestError> load(const std::filesystem::path& path);
std::expected<CppStandardConfig, std::string> normalize_cpp_standard(std::string_view raw);
// For `mcpp new` scaffolding.
std::string default_template(std::string_view packageName);
// M6.x: `mcpp` field in xpkg.lua may be either:
// - a string (path to mcpp.toml inside the extracted tarball, glob-able)
// - a table (inline Form B descriptor)
// extract_mcpp_field discriminates and returns the right kind.
struct McppField {
enum Kind { Absent, StringPath, TableBody } kind = Absent;
std::string value; // glob path (StringPath) or table body (TableBody)
};
McppField extract_mcpp_field(std::string_view luaContent);
// Extract the list of available versions for `platform` (e.g. "linux", "macosx",
// "windows") from an xpkg .lua's xpm.<platform> = { ["X.Y.Z"] = {...}, ... }.
// Returns an empty vector if the platform table is missing or has no entries.
std::vector<std::string>
list_xpkg_versions(std::string_view luaContent, std::string_view platform);
// Extract the `namespace` field from an xpkg .lua's `package = { ... }` block.
// Returns empty string if the field is absent (legacy descriptors).
std::string extract_xpkg_namespace(std::string_view luaContent);
// Extract the `name` field from an xpkg .lua's `package = { ... }` block.
// Returns empty string if the field is absent.
std::string extract_xpkg_name(std::string_view luaContent);
// Resolve the lib-root path for a manifest:
// 1. `[lib].path` if explicitly set (cargo-style override),
// 2. otherwise the convention `src/<package-tail>.cppm`, where
// `<package-tail>` is the last `.`-segment of [package].name
// (e.g. `mcpplibs.tinyhttps` → `src/tinyhttps.cppm`).
// The returned path is relative to the package root unless the user
// passed an absolute path in `[lib].path`.
std::filesystem::path resolve_lib_root_path(const Manifest& manifest);
// True if the manifest declares at least one `kind = "lib"` target.
// Lib-root convention only applies when this returns true.
bool has_lib_target(const Manifest& manifest);
// Synthesize a Manifest from an xpkg .lua file's `mcpp = {}` segment.
// Used when a fetched dep has no source/mcpp.toml — the index entry's
// `mcpp = {}` workaround block carries the missing build info.
//
// The resulting Manifest is in-memory only; sourcePath is set to the
// supplied package name + version so error messages can refer to it.
std::expected<Manifest, ManifestError>
synthesize_from_xpkg_lua(std::string_view luaContent,
std::string_view packageName,
std::string_view packageVersion);
} // namespace mcpp::manifest
// =====================================================================
// Implementation
// =====================================================================
namespace mcpp::manifest {
namespace t = mcpp::libs::toml;
namespace {
bool starts_with_std_flag(std::string_view flag) {
return flag == "-std" || flag.starts_with("-std=");
}
ManifestError error(const std::filesystem::path& origin,
const std::string& msg,
t::Position pos = {0, 0}) {
return ManifestError{msg, origin, pos.line, pos.column};
}
} // namespace
std::expected<CppStandardConfig, std::string> normalize_cpp_standard(std::string_view raw) {
auto trim_copy = [](std::string_view input) {
std::size_t begin = 0;
while (begin < input.size()
&& std::isspace(static_cast<unsigned char>(input[begin]))) {
++begin;
}
std::size_t end = input.size();
while (end > begin
&& std::isspace(static_cast<unsigned char>(input[end - 1]))) {
--end;
}
return std::string(input.substr(begin, end - begin));
};
std::string s = trim_copy(raw);
for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
CppStandardConfig out;
if (s.empty() || s == "c++23" || s == "c++2b") {
out.canonical = "c++23";
out.flag = "-std=c++23";
out.level = 23;
out.gnuDialect = false;
return out;
}
if (s == "gnu++23" || s == "gnu++2b") {
out.canonical = "gnu++23";
out.flag = "-std=gnu++23";
out.level = 23;
out.gnuDialect = true;
return out;
}
if (s == "c++26" || s == "c++2c") {
out.canonical = "c++26";
out.flag = "-std=c++26";
out.level = 26;
out.gnuDialect = false;
return out;
}
if (s == "gnu++26" || s == "gnu++2c") {
out.canonical = "gnu++26";
out.flag = "-std=gnu++26";
out.level = 26;
out.gnuDialect = true;
return out;
}
if (s == "c++latest") {
out.canonical = "c++latest";
out.flag = "-std=c++26";
out.level = 999;
out.gnuDialect = false;
return out;
}
return std::unexpected(std::format(
"unsupported C++ standard '{}'; expected c++23, c++26, c++2c, gnu++23, gnu++26, or c++latest",
raw));
}
std::expected<Manifest, ManifestError> parse_string(std::string_view content,
const std::filesystem::path& origin) {
auto doc = t::parse(content);
if (!doc) {
return std::unexpected(error(origin, doc.error().message, doc.error().where));
}
Manifest m;
m.sourcePath = origin;
// [package] — required unless [workspace] is present (virtual workspace).
auto* pkg_t = doc->get_table("package");
bool has_workspace = (doc->get_table("workspace") != nullptr);
if (!pkg_t && !has_workspace)
return std::unexpected(error(origin, "missing required [package] section"));
auto name = doc->get_string("package.name");
if (!name && !has_workspace)
return std::unexpected(error(origin, "missing required field 'package.name'"));
if (name) m.package.name = *name;
// 0.0.6+: explicit namespace field (xpkg V1 style).
// If present, [package].name is the short name.
// If absent, compat.cppm::resolve_package_name infers from dotted name.
if (auto v = doc->get_string("package.namespace")) m.package.namespace_ = *v;
auto version = doc->get_string("package.version");
if (!version && !has_workspace)
return std::unexpected(error(origin, "missing required field 'package.version'"));
if (version) m.package.version = *version;
if (auto v = doc->get_string("package.description")) m.package.description = *v;
if (auto v = doc->get_string("package.license")) m.package.license = *v;
if (auto v = doc->get_string("package.repo")) m.package.repo = *v;
if (auto v = doc->get_string_array("package.authors")) m.package.authors = *v;
// [package].standard (M5.0 new home)
if (auto v = doc->get_string("package.standard")) m.package.standard = *v;
// [language] (M5.0: deprecated, kept for backward compat — drop in M6)
// Reads to old fields AND mirrors to new package.standard if [package].standard not set.
bool had_language_section = (doc->get_table("language") != nullptr);
if (auto v = doc->get_string("language.standard")) {
m.language.standard = *v;
// mirror to new home only if [package].standard wasn't explicitly set
if (!doc->get_string("package.standard")) m.package.standard = *v;
} else {
m.language.standard = m.package.standard; // keep old field consistent with new
}
if (auto v = doc->get_bool("language.modules")) m.language.modules = *v;
if (auto v = doc->get_bool("language.import_std")) m.language.importStd = *v;
// Validation on the unified standard. Store the canonical spelling so all
// downstream build surfaces consume one active value.
auto stdCfg = normalize_cpp_standard(m.package.standard);
if (!stdCfg) return std::unexpected(error(origin, stdCfg.error()));
m.cppStandard = *stdCfg;
m.package.standard = m.cppStandard.canonical;
m.language.standard = m.cppStandard.canonical;
if (had_language_section && !m.language.modules) {
return std::unexpected(error(origin,
"language.modules must be true (mcpp is modules-only)"));
}
// [build].sources (M5.0 new home) + [modules].sources (deprecated, compat)
if (auto v = doc->get_string_array("build.sources")) m.buildConfig.sources = *v;
if (auto v = doc->get_string_array("modules.sources")) {
m.modules.sources = *v;
// If [build].sources wasn't set, mirror legacy field into new field.
if (m.buildConfig.sources.empty()) m.buildConfig.sources = *v;
}
// Mirror new → legacy so existing code reading manifest.modules.sources keeps working.
if (m.modules.sources.empty()) m.modules.sources = m.buildConfig.sources;
if (auto v = doc->get_string_array("modules.exports")) m.modules.exports_ = *v;
if (auto v = doc->get_bool("modules.strict")) m.modules.strict = *v;
// [build].include_dirs (M5.0 new field)
if (auto v = doc->get_string_array("build.include_dirs")) {
for (auto& s : *v) m.buildConfig.includeDirs.emplace_back(s);
}
// [targets.*] — M5.0: now optional. If absent, defer to auto-inference (in load()).
auto* targets_table = doc->get_table("targets");
if (targets_table && !targets_table->empty()) {
for (auto& [tname, tval] : *targets_table) {
if (!tval.is_table()) {
return std::unexpected(error(origin,
std::format("[targets.{}] must be a table", tname)));
}
Target t;
t.name = tname;
auto& tt = tval.as_table();
auto kit = tt.find("kind");
if (kit == tt.end() || !kit->second.is_string()) {
return std::unexpected(error(origin,
std::format("targets.{}.kind missing or not a string", tname)));
}
const auto& kind_s = kit->second.as_string();
if (kind_s == "lib" || kind_s == "library") t.kind = Target::Library;
else if (kind_s == "bin" || kind_s == "binary") t.kind = Target::Binary;
else if (kind_s == "shared" || kind_s == "dylib"
|| kind_s == "so" || kind_s == "shlib") t.kind = Target::SharedLibrary;
else return std::unexpected(error(origin,
std::format("targets.{}.kind must be 'bin', 'lib' or 'shared'; got '{}'", tname, kind_s)));
if (t.kind == Target::Binary) {
auto mit = tt.find("main");
if (mit == tt.end() || !mit->second.is_string()) {
return std::unexpected(error(origin,
std::format("targets.{} (kind=bin) requires 'main' field", tname)));
}
t.main = mit->second.as_string();
}
m.targets.push_back(std::move(t));
}
} // close `if (targets_table && !targets_table->empty())`
// [dependencies] / [dev-dependencies]
//
// Three accepted forms (M5.x):
//
// (1) flat / default-ns
// [dependencies]
// gtest = "1.15.2" ⇒ (mcpp, gtest)
// frob = { path = "..." } ⇒ (mcpp, frob) inline spec
//
// (2) namespaced subtable (TOML-native, no quotes)
// [dependencies.mcpplibs]
// cmdline = "0.0.2" ⇒ (mcpplibs, cmdline)
// tmpl = { version = "0.0.1", features = [...] }
//
// (3) legacy quoted dotted form (deprecated, still parsed)
// [dependencies]
// "mcpplibs.cmdline" = "0.0.2" ⇒ (mcpplibs, cmdline) + warning
//
// The map key remains the fully-qualified `<ns>.<name>` for non-default
// namespaces (so existing fetcher / lockfile lookups by composite name
// keep working) and the bare `<name>` for the default namespace (so the
// common case stays unchanged).
auto is_dep_spec_key = [](std::string_view k) {
return k == "path" || k == "version" || k == "git"
|| k == "rev" || k == "tag" || k == "branch"
|| k == "features" || k == "workspace" || k == "visibility";
};
auto looks_like_inline_dep_spec = [&](const t::Table& sub) {
if (sub.empty()) return false;
for (auto& [sk, sv] : sub) {
if (!is_dep_spec_key(sk)) return false;
}
return true;
};
auto fill_inline_spec = [&](DependencySpec& spec,
std::string_view section,
std::string_view fqName,
const t::Table& sub) -> std::expected<void, ManifestError>
{
if (auto it = sub.find("path"); it != sub.end() && it->second.is_string()) spec.path = it->second.as_string();
if (auto it = sub.find("version"); it != sub.end() && it->second.is_string()) spec.version = it->second.as_string();
if (auto it = sub.find("git"); it != sub.end() && it->second.is_string()) spec.git = it->second.as_string();
if (auto it = sub.find("visibility"); it != sub.end() && it->second.is_string()) {
spec.visibility = it->second.as_string();
if (spec.visibility != "public"
&& spec.visibility != "private"
&& spec.visibility != "interface") {
return std::unexpected(error(origin, std::format(
"[{}.\"{}\"] visibility must be 'public', 'private', or 'interface'",
section, fqName)));
}
}
if (auto it = sub.find("rev"); it != sub.end() && it->second.is_string()) {
spec.gitRev = it->second.as_string();
spec.gitRefKind = "rev";
} else if (auto it = sub.find("tag"); it != sub.end() && it->second.is_string()) {
spec.gitRev = it->second.as_string();
spec.gitRefKind = "tag";
} else if (auto it = sub.find("branch"); it != sub.end() && it->second.is_string()) {
spec.gitRev = it->second.as_string();
spec.gitRefKind = "branch";
}
if (auto it = sub.find("workspace"); it != sub.end() && it->second.is_bool() && it->second.as_bool()) {
spec.inheritWorkspace = true;
return {}; // version will be filled in by workspace merge
}
if (spec.path.empty() && spec.version.empty() && spec.git.empty()) {
return std::unexpected(error(origin, std::format(
"[{}.\"{}\"] must specify 'path', 'version', or 'git'", section, fqName)));
}
if (!spec.git.empty() && spec.gitRev.empty()) {
return std::unexpected(error(origin, std::format(
"[{}.\"{}\"] git dep requires one of: rev / tag / branch", section, fqName)));
}
return {};
};
auto assign_dep = [&](std::string_view section,
std::map<std::string, DependencySpec>& out,
const mcpp::pm::DependencySelector& selector,
const t::Value& value,
bool legacyDottedKey)
-> std::expected<void, ManifestError>
{
if (selector.candidates.empty()) {
return std::unexpected(error(origin, std::format(
"[{}] dependency selector '{}' has no candidates",
section, selector.stableMapKey)));
}
DependencySpec spec;
spec.namespace_ = selector.candidates.front().namespace_;
spec.shortName = selector.candidates.front().shortName;
spec.candidates = selector.candidates;
spec.legacyDottedKey = legacyDottedKey;
auto key = selector.stableMapKey;
if (value.is_string()) {
spec.version = value.as_string();
} else if (value.is_table()) {
auto& sub = value.as_table();
if (!looks_like_inline_dep_spec(sub)) {
return std::unexpected(error(origin, std::format(
"[{}.{}] must be a version string or table of "
"(path/version/git/rev/tag/branch/features/visibility)",
section, key)));
}
if (auto r = fill_inline_spec(spec, section, key, sub); !r) return r;
} else {
return std::unexpected(error(origin, std::format(
"[{}].{} must be a string (version) or table (path/version/...)",
section, key)));
}
out[key] = std::move(spec);
return {};
};
auto is_namespace_table = [&](std::string_view section,
std::string_view key) {
auto path = std::format("{}.{}", section, key);
return doc->has_explicit_table(path)
|| key == kDefaultNamespace;
};
std::function<std::expected<void, ManifestError>(
std::string_view,
std::map<std::string, DependencySpec>&,
std::string,
std::string,
const t::Table&)> load_nested_dep_table;
load_nested_dep_table =
[&](std::string_view section,
std::map<std::string, DependencySpec>& out,
std::string ns,
std::string mapPrefix,
const t::Table& table) -> std::expected<void, ManifestError>
{
for (auto& [k, v] : table) {
if (v.is_string() ||
(v.is_table() && looks_like_inline_dep_spec(v.as_table()))) {
auto mapKey = mapPrefix.empty()
? k
: std::format("{}.{}", mapPrefix, k);
auto selector = mcpp::pm::make_direct_dependency_selector(
ns, k, mapKey);
if (auto r = assign_dep(section, out, selector, v, false); !r)
return r;
continue;
}
if (!v.is_table()) {
return std::unexpected(error(origin, std::format(
"[{}].{}.{} must be a string, inline dep table, or nested table",
section, ns, k)));
}
auto childNs = std::format("{}.{}", ns, k);
auto childMapPrefix = mapPrefix.empty()
? k
: std::format("{}.{}", mapPrefix, k);
if (auto r = load_nested_dep_table(
section, out, childNs, childMapPrefix, v.as_table()); !r)
return r;
}
return {};
};
std::function<std::expected<void, ManifestError>(
std::string_view,
std::map<std::string, DependencySpec>&,
std::string,
const t::Table&)> load_selector_dep_table;
load_selector_dep_table =
[&](std::string_view section,
std::map<std::string, DependencySpec>& out,
std::string selectorPrefix,
const t::Table& table) -> std::expected<void, ManifestError>
{
for (auto& [k, v] : table) {
auto selectorText = selectorPrefix.empty()
? k
: std::format("{}.{}", selectorPrefix, k);
if (v.is_string() ||
(v.is_table() && looks_like_inline_dep_spec(v.as_table()))) {
auto selector = mcpp::pm::resolve_dependency_selector(
selectorText,
mcpp::pm::DependencySelectorMode::OmittedMcpplibsPriority);
if (auto r = assign_dep(section, out, selector, v, false); !r)
return r;
continue;
}
if (!v.is_table()) {
return std::unexpected(error(origin, std::format(
"[{}].{} must be a string, inline dep table, or nested table",
section, selectorText)));
}
if (auto r = load_selector_dep_table(
section, out, selectorText, v.as_table()); !r)
return r;
}
return {};
};
auto load_deps = [&](std::string_view section, std::map<std::string, DependencySpec>& out)
-> std::expected<void, ManifestError>
{
auto* tt = doc->get_table(section);
if (!tt) return {};
for (auto& [k, v] : *tt) {
// (1) string value → flat default-ns short version, or
// (3) legacy "ns.name" = "ver" (dotted key).
if (v.is_string()) {
if (k.find('.') != std::string::npos) {
auto legacyKey = mcpp::pm::compat::split_legacy_dependency_key(k);
auto selector = mcpp::pm::make_direct_dependency_selector(
legacyKey.namespace_, legacyKey.shortName, k);
if (auto r = assign_dep(section, out, selector, v,
legacyKey.legacyDottedKey); !r)
return r;
continue;
}
auto selector = mcpp::pm::resolve_dependency_selector(
k, mcpp::pm::DependencySelectorMode::OmittedMcpplibsPriority);
if (auto r = assign_dep(section, out, selector, v, false); !r)
return r;
continue;
}
if (!v.is_table()) {
return std::unexpected(error(origin, std::format(
"[{}].{} must be a string (version) or table (path/version/...)", section, k)));
}
auto& sub = v.as_table();
// (1') inline dep spec under the default namespace, e.g.
// frob = { path = "..." } or
// "mcpplibs.cmdline" = { version = "0.0.2" }
// The latter is the legacy dotted-key form; same treatment as (3).
if (looks_like_inline_dep_spec(sub)) {
if (k.find('.') != std::string::npos) {
auto legacyKey = mcpp::pm::compat::split_legacy_dependency_key(k);
auto selector = mcpp::pm::make_direct_dependency_selector(
legacyKey.namespace_, legacyKey.shortName, k);
if (auto r = assign_dep(section, out, selector, v,
legacyKey.legacyDottedKey); !r)
return r;
continue;
}
auto selector = mcpp::pm::resolve_dependency_selector(
k, mcpp::pm::DependencySelectorMode::OmittedMcpplibsPriority);
if (auto r = assign_dep(section, out, selector, v, false); !r)
return r;
continue;
}
// (2) namespaced or nested subtable.
//
// Explicit tables such as `[dependencies.acme]` are namespace
// roots. Dotted keys written inside the single dependency table,
// such as `[dependencies] capi.lua = "0.0.3"`, are ordered
// selectors: mcpplibs.capi/lua first, then capi/lua.
if (is_namespace_table(section, k)) {
if (auto r = load_nested_dep_table(section, out, k, k, sub); !r)
return r;
} else if (auto r = load_selector_dep_table(section, out, k, sub); !r) {
return r;
}
}
return {};
};
if (auto r = load_deps("dependencies", m.dependencies); !r) return std::unexpected(r.error());
if (auto r = load_deps("dev-dependencies", m.devDependencies); !r) return std::unexpected(r.error());
if (auto r = load_deps("build-dependencies", m.buildDependencies); !r) return std::unexpected(r.error());
// [toolchain] — platform → "pkg@version" map (docs/21)
if (auto* tt = doc->get_table("toolchain")) {
for (auto& [platform, val] : *tt) {
if (!val.is_string()) {
return std::unexpected(error(origin,
std::format("[toolchain].{} must be a string like \"gcc@15.1.0\"", platform)));
}
m.toolchain.byPlatform[platform] = val.as_string();
}
}
// [build] — backend tunables
if (auto v = doc->get_bool("build.static_stdlib")) m.buildConfig.staticStdlib = *v;
if (auto v = doc->get_string_array("build.cflags")) m.buildConfig.cflags = *v;
if (auto v = doc->get_string_array("build.cxxflags")) m.buildConfig.cxxflags = *v;
if (auto v = doc->get_string_array("build.ldflags")) m.buildConfig.ldflags = *v;
if (auto v = doc->get_string("build.c_standard")) m.buildConfig.cStandard = *v;
for (auto const& flag : m.buildConfig.cxxflags) {
if (starts_with_std_flag(flag)) {
return std::unexpected(error(origin,
std::format("build.cxxflags contains '{}'; use [package].standard to configure the C++ language standard",
flag)));
}
}
// [runtime] — launch-time requirements.
if (auto v = doc->get_string_array("runtime.library_dirs")) {
for (auto& s : *v) m.runtimeConfig.libraryDirs.emplace_back(s);
}
if (auto v = doc->get_string_array("runtime.dlopen_libs"))
m.runtimeConfig.dlopenLibs = *v;
if (auto v = doc->get_string_array("runtime.capabilities"))
m.runtimeConfig.capabilities = *v;
// [lib] — library root convention (cargo-style).
if (auto v = doc->get_string("lib.path")) {
m.lib.path = *v;
}
// [pack] — `mcpp pack` configuration. See docs/35-pack-design.md.
if (auto v = doc->get_string("pack.default_mode")) {
const auto& s = *v;
if (s != "static" && s != "bundle-project" && s != "bundle-all") {
return std::unexpected(error(origin, std::format(
"[pack].default_mode = '{}' invalid; expected "
"'static' | 'bundle-project' | 'bundle-all'", s)));
}
m.packConfig.defaultMode = s;
}
if (auto v = doc->get_string_array("pack.include"))
m.packConfig.include = *v;
if (auto v = doc->get_string_array("pack.exclude"))
m.packConfig.exclude = *v;
// [pack.bundle-project] sub-table for fine-grained PEP 600 overrides.
if (auto v = doc->get_string_array("pack.bundle-project.also_skip"))
m.packConfig.alsoSkip = *v;
if (auto v = doc->get_string_array("pack.bundle-project.force_bundle"))
m.packConfig.forceBundle = *v;
// [target.<triple>] — per-target overrides. We accept both GCC
// (x86_64-linux-musl) and Rust-style (x86_64-unknown-linux-musl)
// triple forms; the latter is canonicalised by stripping the
// `-unknown-` segment so both keys map to the same entry.
auto canon_triple = [](std::string s) {
constexpr std::string_view kUnknown = "-unknown-";
if (auto p = s.find(kUnknown); p != std::string::npos)
s.replace(p, kUnknown.size(), "-");
return s;
};
if (auto* tt = doc->get_table("target")) {
for (auto& [triple, val] : *tt) {
if (!val.is_table()) continue;
auto& body = val.as_table();
TargetEntry e;
if (auto it = body.find("toolchain"); it != body.end() && it->second.is_string())
e.toolchain = it->second.as_string();
if (auto it = body.find("linkage"); it != body.end() && it->second.is_string()) {
e.linkage = it->second.as_string();
if (e.linkage != "static" && e.linkage != "dynamic") {
return std::unexpected(error(origin, std::format(
"[target.{}].linkage = '{}' is invalid; expected 'static' or 'dynamic'",
triple, e.linkage)));
}
}
m.targetOverrides[canon_triple(triple)] = std::move(e);
}
}
// [workspace] — multi-package workspace support (0.0.11+).
if (doc->get_table("workspace")) {
m.workspace.present = true;
if (auto v = doc->get_string_array("workspace.members"))
m.workspace.members = *v;
if (auto v = doc->get_string_array("workspace.exclude"))
m.workspace.exclude = *v;
// [workspace.dependencies] — versions that members inherit via .workspace = true.
if (auto* wdeps = doc->get_table("workspace.dependencies")) {
for (auto& [k, v] : *wdeps) {
if (v.is_string()) {
if (k.find('.') != std::string::npos) {
auto depKey = mcpp::pm::compat::split_legacy_dependency_key(k);
auto selector = mcpp::pm::make_direct_dependency_selector(
depKey.namespace_, depKey.shortName, k);
if (auto r = assign_dep("workspace.dependencies",
m.workspace.dependencies,
selector, v,
depKey.legacyDottedKey); !r) {
return std::unexpected(r.error());
}
continue;
}
auto selector = mcpp::pm::resolve_dependency_selector(
k, mcpp::pm::DependencySelectorMode::OmittedMcpplibsPriority);
if (auto r = assign_dep("workspace.dependencies",
m.workspace.dependencies,
selector, v, false); !r) {
return std::unexpected(r.error());
}
continue;
}
if (!v.is_table()) continue;
if (is_namespace_table("workspace.dependencies", k)) {
if (auto r = load_nested_dep_table("workspace.dependencies",
m.workspace.dependencies,
k, k, v.as_table()); !r) {
return std::unexpected(r.error());
}
} else {
if (auto r = load_selector_dep_table("workspace.dependencies",
m.workspace.dependencies,
k, v.as_table()); !r) {
return std::unexpected(r.error());
}
}
}
}
}
// [indices] — custom package index repositories.
//
// Accepted forms:
// acme = "git@gitlab.example.com:platform/mcpp-index.git" # short: value = url
// acme-stable = { url = "git@...", tag = "v2.0" } # long: inline table
// local-dev = { path = "<path>/my-packages" } # local path
// mcpplibs = { url = "https://...", rev = "abc123" } # pin built-in
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()) {
// Short form: key = "url"
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()) {
return std::unexpected(error(origin, std::format(
"[indices].{} must specify 'url' or 'path'", k)));
}
} else {
return std::unexpected(error(origin, std::format(
"[indices].{} must be a string (url) or inline table", k)));
}
m.indices[k] = std::move(spec);
}
}
return m;
}
// M5.0: inject defaults and auto-infer targets when fields are absent.
// Mutates manifest in-place; called from load() with the project root.
namespace {
void apply_defaults_and_infer(Manifest& m, const std::filesystem::path& root) {
// Default sources glob (covers .cppm/.cpp/.cc/.c under src/).
if (m.buildConfig.sources.empty()) {
m.buildConfig.sources = {
"src/**/*.cppm",
"src/**/*.cpp",
"src/**/*.cc",
"src/**/*.c",
};
m.modules.sources = m.buildConfig.sources; // legacy mirror
m.inferredNotes.push_back("sources [src/**/*.{cppm,cpp,cc,c}]");
}
// Default include_dirs: ["include"] iff <root>/include/ exists.
if (m.buildConfig.includeDirs.empty()) {
std::error_code ec;
if (std::filesystem::is_directory(root / "include", ec)) {
m.buildConfig.includeDirs.push_back("include");
m.inferredNotes.push_back("include_dirs [include]");
}
}
// Auto-target inference (only when no [targets] declared).
if (m.targets.empty()) {
std::error_code ec;
auto mainCpp = root / "src" / "main.cpp";
bool hasMain = std::filesystem::exists(mainCpp, ec);
bool hasCppm = false;
if (std::filesystem::is_directory(root / "src", ec)) {
for (auto& e : std::filesystem::recursive_directory_iterator(root / "src", ec)) {
if (ec) break;
if (e.is_regular_file(ec) && !ec
&& e.path().extension() == ".cppm") {
hasCppm = true; break;
}
}
}
if (hasMain) {
Target t;
t.name = m.package.name;
t.kind = Target::Binary;
t.main = "src/main.cpp";
m.targets.push_back(std::move(t));
m.inferredNotes.push_back(
std::format("target {} (bin from src/main.cpp)", m.package.name));
} else if (hasCppm) {
Target t;
t.name = m.package.name;
t.kind = Target::Library;
m.targets.push_back(std::move(t));
m.inferredNotes.push_back(
std::format("target {} (lib from .cppm in src/)", m.package.name));
}