-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_targetside.cpp
More file actions
767 lines (683 loc) · 35.4 KB
/
Copy pathtest_targetside.cpp
File metadata and controls
767 lines (683 loc) · 35.4 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
// Target-side resolution: which layer comes from where.
//
// These tests exist because the decision they cover used to be three
// independent derivations with three different criteria, and the bug was that
// they disagreed on a case none of them had been written for: a C program whose
// system comes from the dependency graph. So the assertions below are about the
// TABLE's totality — every combination the ecosystem can produce gets a row,
// including the ones that were unrepresentable before.
//
// The module under test is a pure function of plain data on purpose. The
// capability it replaces (`hosted-standard-library`) drove seven behaviours
// from inside a 7000-line translation unit and had zero test coverage, because
// asserting it required running a whole build. Nothing here runs a build.
#include <gtest/gtest.h>
import std;
import mcpp.targetside;
namespace ts = mcpp::targetside;
namespace {
// The traditional stack: a payload supplies every layer.
ts::Inputs payload_linux() {
ts::Inputs in;
in.llvmTriple = "x86_64-unknown-linux-gnu";
in.targetOs = "linux";
in.targetEnv = "gnu";
in.payloadSystemRef = "xim-x-linux-headers@5.11.1";
in.payloadLibcRef = "xim-x-glibc@2.44";
in.payloadCxxRef = "xim-x-llvm@22.1.8";
in.payloadCxxInterface = "libc++";
return in;
}
ts::Provider provider(std::string name, std::string version,
std::string iface, bool stdModule = false) {
ts::Provider p;
p.name = std::move(name);
p.version = std::move(version);
p.interfaceName = std::move(iface);
p.hasStdModule = stdModule;
return p;
}
} // namespace
// ── The capability grammar ───────────────────────────────────────────────────
TEST(TargetSideCapability, ThreeLayerNamesAreAccepted) {
for (auto [entry, layer] : std::initializer_list<
std::pair<std::string_view, ts::CapLayer>>{
{"mcpp:kernel-abi", ts::CapLayer::KernelAbi},
{"mcpp:c-abi", ts::CapLayer::CAbi},
{"mcpp:c++-abi", ts::CapLayer::CxxAbi}}) {
auto r = ts::parse_capability(entry);
ASSERT_TRUE(r.has_value()) << entry;
ASSERT_TRUE(r->has_value()) << entry;
EXPECT_EQ((*r)->layer, layer) << entry;
EXPECT_TRUE((*r)->interfaceName.empty()) << entry;
}
}
TEST(TargetSideCapability, InterfaceNameIsCarriedByTheDeclaration) {
auto r = ts::parse_capability("mcpp:kernel-abi=openkal");
ASSERT_TRUE(r.has_value());
ASSERT_TRUE(r->has_value());
EXPECT_EQ((*r)->layer, ts::CapLayer::KernelAbi);
EXPECT_EQ((*r)->interfaceName, "openkal");
}
// The engine must not reject names it does not own. `provides` serves the
// feature system too, and a closed set over the whole array would break the
// allocator selection that already ships.
TEST(TargetSideCapability, NamesOutsideTheReservedPrefixPassThrough) {
for (std::string_view entry : {"freestanding-allocator",
"hosted-standard-library",
"anything-a-package-invents"}) {
auto r = ts::parse_capability(entry);
ASSERT_TRUE(r.has_value()) << entry;
EXPECT_FALSE(r->has_value()) << entry;
}
}
// And the point of having a prefix at all: inside it, a typo is an error rather
// than a silently disabled behaviour.
TEST(TargetSideCapability, UnknownNameInsideTheReservedPrefixIsAnError) {
for (std::string_view entry : {"mcpp:kernel_abi", // underscore, not hyphen
"mcpp:cabi",
"mcpp:c++abi",
"mcpp:target-system"}) {
auto r = ts::parse_capability(entry);
EXPECT_FALSE(r.has_value()) << entry;
if (!r.has_value())
EXPECT_NE(r.error().find("mcpp:kernel-abi"), std::string::npos)
<< "the diagnostic must list the layers that do exist";
}
}
// ── The resolution table ─────────────────────────────────────────────────────
TEST(TargetSideResolve, TraditionalStackTakesEveryLayerFromThePayload) {
auto r = ts::resolve(payload_linux());
EXPECT_EQ(r.kernelAbi.origin, ts::Origin::Payload);
EXPECT_EQ(r.cAbi.origin, ts::Origin::Payload);
EXPECT_EQ(r.cxx.origin, ts::Origin::Payload);
EXPECT_EQ(r.kernelAbi.interfaceName, "linux");
EXPECT_EQ(r.cAbi.interfaceName, "glibc");
EXPECT_EQ(r.cxx.interfaceName, "libc++");
EXPECT_FALSE(r.system_from_graph());
}
TEST(TargetSideResolve, OpenkalCxxTakesEveryLayerFromTheGraph) {
auto in = payload_linux();
in.llvmTriple = "arm64-apple-macos14.0";
in.targetOs = "macos";
in.kernelAbi = provider("openkal-macos", "0.3.1", "openkal");
in.cAbi = provider("openkal-musl", "0.3.1", "musl");
in.cxxAbi = provider("openkal-llvm-runtime", "0.1.0", "libc++", /*stdModule=*/true);
auto r = ts::resolve(in);
EXPECT_EQ(r.kernelAbi.origin, ts::Origin::Graph);
EXPECT_EQ(r.cAbi.origin, ts::Origin::Graph);
EXPECT_EQ(r.cxx.origin, ts::Origin::Graph);
EXPECT_EQ(r.kernelAbi.impl, "openkal-macos@0.3.1");
EXPECT_EQ(r.cAbi.impl, "openkal-musl@0.3.1");
EXPECT_FALSE(r.cxx.subset) << "a package that declares a std module supplies the whole library";
EXPECT_TRUE(r.system_from_graph());
}
// THE CASE THAT WAS UNREPRESENTABLE, AND THE MEASURED DEFECT.
//
// A C program over openkal has a kernel ABI and a C library from the graph and
// no C++ runtime at all. Before this module the absence of the third layer was
// read as "the target side is not from the graph", the payload's own libc++
// stayed on the link line, and a macOS cross ended in
// `libc++.so: unhandled file type`.
TEST(TargetSideResolve, PureCOverOpenkalStillHasItsSystemFromTheGraph) {
auto in = payload_linux();
in.llvmTriple = "arm64-apple-macos14.0";
in.targetOs = "macos";
in.kernelAbi = provider("openkal-macos", "0.3.1", "openkal");
in.cAbi = provider("openkal-musl", "0.3.1", "musl");
// no cxxAbi provider
auto r = ts::resolve(in);
EXPECT_EQ(r.cxx.origin, ts::Origin::None)
<< "a C program needs no C++ runtime, and that is not a failure to find one";
EXPECT_TRUE(r.system_from_graph())
<< "the system still comes from the graph, which is the whole defect";
EXPECT_NE(r.cxx.origin, ts::Origin::Payload)
<< "the payload's C++ runtime must not be selected over a graph C library";
}
// Only the platform implementation, and nothing above it.
TEST(TargetSideResolve, RawOpenkalHasOnlyAKernelAbi) {
auto in = payload_linux();
in.kernelAbi = provider("openkal-linux", "0.5.1", "openkal");
in.sysrootDeclaredEmpty = true;
auto r = ts::resolve(in);
EXPECT_EQ(r.kernelAbi.origin, ts::Origin::Graph);
EXPECT_TRUE(r.cAbi.absent());
EXPECT_TRUE(r.cxx.absent());
}
TEST(TargetSideResolve, BareMetalWithPicolibcHasNoKernelAbi) {
ts::Inputs in;
in.llvmTriple = "riscv64-none-elf";
in.freestandingTarget = true;
in.sysrootXpkg = "xim:picolibc-riscv@1.8.12";
auto r = ts::resolve(in);
EXPECT_TRUE(r.kernelAbi.absent())
<< "a bare machine has no kernel, and saying so is the information";
EXPECT_EQ(r.cAbi.origin, ts::Origin::Xpkg);
// The package name, not a prettier form of it. Stripping the `-riscv`
// suffix would mean the engine knows how this ecosystem names its
// packages, which is exactly the knowledge this design keeps out of it.
EXPECT_EQ(r.cAbi.interfaceName, "picolibc-riscv");
EXPECT_EQ(r.cAbi.impl, "xim:picolibc-riscv@1.8.12");
EXPECT_TRUE(r.cxx.absent());
EXPECT_FALSE(r.system_from_graph());
}
// THE MIXED CASE. It already ships, and no single boolean can express it.
TEST(TargetSideResolve, PrebuiltCLibraryUnderAGraphSuppliedCxxSubset) {
ts::Inputs in;
in.llvmTriple = "riscv64-none-elf";
in.freestandingTarget = true;
in.sysrootXpkg = "xim:picolibc-riscv@1.8.12";
in.cxxAbi = provider("std-freestanding", "0.5.0", "freestanding subset");
auto r = ts::resolve(in);
EXPECT_EQ(r.cAbi.origin, ts::Origin::Xpkg) << "prebuilt";
EXPECT_EQ(r.cxx.origin, ts::Origin::Graph) << "composed at build time";
EXPECT_TRUE(r.cxx.subset) << "no std module declared, so the library is a subset";
}
// A hosted target whose C library is the payload's (a located SDK, or glibc)
// while a package supplies the whole standard library: the iOS rows over
// `llvm.libcxx` (mcpp#630). The C++ layer is the graph's and is not a subset,
// and the C library stays the payload's; nothing about one decides the other.
TEST(TargetSideResolve, PrebuiltCLibraryUnderAGraphSuppliedLibcxx) {
auto in = payload_linux();
in.cxxAbi = provider("libcxx", "22.1.8.1", "libc++", /*stdModule=*/true);
auto r = ts::resolve(in);
EXPECT_EQ(r.cAbi.origin, ts::Origin::Payload) << "the SDK's or glibc's, prebuilt";
EXPECT_TRUE(r.cAbi.prebuilt());
EXPECT_EQ(r.cxx.origin, ts::Origin::Graph);
EXPECT_TRUE(r.cxx.fromGraph());
EXPECT_FALSE(r.cxx.subset) << "a std module is declared, so this is the whole library";
EXPECT_FALSE(r.system_from_graph()) << "the system is still the payload's";
}
// The payload has no compiler runtime for this platform and the graph
// declares none: the layer is absent, and the report says so rather than
// naming the compiler's family for an archive that does not exist. With a
// graph provider the same input resolves to the graph.
TEST(TargetSideResolve, APayloadWithoutACompilerRuntimeForThePlatform) {
auto in = payload_linux();
in.llvmTriple = "arm64-apple-ios18.0-simulator";
in.targetOs = "ios";
in.targetEnv = "sim";
in.compilerFamily = "llvm";
in.payloadCompilerRuntimeAbsent = true;
auto absent = ts::resolve(in);
EXPECT_TRUE(absent.compilerRuntime.absent());
in.compilerRuntime = provider("compiler-rt-builtins", "22.1.8.3", "compiler-rt");
auto supplied = ts::resolve(in);
EXPECT_EQ(supplied.compilerRuntime.origin, ts::Origin::Graph);
EXPECT_FALSE(supplied.compilerRuntime.absent());
// Negative direction: a payload that has the archive reports its own.
in.compilerRuntime.reset();
in.payloadCompilerRuntimeAbsent = false;
auto own = ts::resolve(in);
EXPECT_EQ(own.compilerRuntime.origin, ts::Origin::Payload);
}
TEST(TargetSideResolve, ZeroLibcTierHasNothingAtAll) {
ts::Inputs in;
in.llvmTriple = "x86_64-none-elf";
in.freestandingTarget = true;
in.sysrootDeclaredEmpty = true;
auto r = ts::resolve(in);
EXPECT_TRUE(r.kernelAbi.absent());
EXPECT_TRUE(r.cAbi.absent());
EXPECT_TRUE(r.cxx.absent());
}
// ── The layering rule ────────────────────────────────────────────────────────
//
// An implementation must have been configured for the layer beneath it. The
// resolver enforces it by construction; this asserts that the construction
// really does, rather than that a later check catches it.
TEST(TargetSideResolve, PayloadCxxIsNotSelectedOverANonPayloadCLibrary) {
for (auto make : std::initializer_list<std::function<ts::Inputs()>>{
[] { auto in = payload_linux(); in.sysrootXpkg = "xim:picolibc-riscv@1.8.12"; return in; },
[] { auto in = payload_linux();
in.cAbi = provider("openkal-musl", "0.3.1", "musl"); return in; }}) {
auto r = ts::resolve(make());
EXPECT_NE(r.cxx.origin, ts::Origin::Payload);
EXPECT_EQ(ts::check_layering(r), std::nullopt)
<< "the default path must not be able to construct the violation";
}
}
TEST(TargetSideResolve, TheLayeringRuleIsStatedForTheOverridePath) {
ts::TargetSide bad;
bad.cAbi = { ts::Origin::Graph, "musl", "openkal-musl@0.3.1", false };
bad.cxx = { ts::Origin::Payload, "libc++", "xim-x-llvm@22.1.8", false };
auto why = ts::check_layering(bad);
ASSERT_TRUE(why.has_value());
EXPECT_NE(why->find("never configured for this one"), std::string::npos)
<< "the diagnostic must state the reason, not merely the combination";
}
// ── The report ───────────────────────────────────────────────────────────────
TEST(TargetSideReport, ShowsTheTranslatedTripleWhenItDiffers) {
auto in = payload_linux();
in.llvmTriple = "arm64-apple-macos14.0";
auto text = ts::format_report(ts::resolve(in), "aarch64-macos");
EXPECT_NE(text.find("aarch64-macos → arm64-apple-macos14.0"), std::string::npos)
<< "the translation is load bearing: the untranslated form emits a Mach-O "
"whose MinVersion carries no platform";
}
TEST(TargetSideReport, AbsentLayersReadAsADashRatherThanBeingOmitted) {
ts::Inputs in;
in.llvmTriple = "x86_64-none-elf";
in.freestandingTarget = true;
in.sysrootDeclaredEmpty = true;
in.compilerFamily = "llvm";
auto text = ts::format_report(ts::resolve(in), "x86_64-none-elf",
/*verbose=*/true);
EXPECT_NE(text.find("kernel-abi"), std::string::npos);
EXPECT_NE(text.find("c-abi"), std::string::npos);
EXPECT_NE(text.find("c++-abi"), std::string::npos);
EXPECT_NE(text.find("—"), std::string::npos);
}
TEST(TargetSideReport, NamesInterfaceAndImplementationSeparately) {
auto in = payload_linux();
in.kernelAbi = provider("openkal-opensbi", "0.1.0", "openkal");
auto text = ts::format_report(ts::resolve(in), "riscv64-none-elf");
EXPECT_NE(text.find("openkal"), std::string::npos);
EXPECT_NE(text.find("openkal-opensbi@0.1.0"), std::string::npos)
<< "four packages answer to one interface name; collapsing them would "
"hide why one source reaches four machines";
EXPECT_NE(text.find("graph"), std::string::npos);
}
// ── What earns a line ────────────────────────────────────────────────────────
//
// A zero-configuration build resolves every layer from one compiler payload,
// and five lines reading `(payload)` answer a question nobody asked. What earns
// a line is a layer that came from somewhere else.
TEST(TargetSideReport, AZeroConfigurationBuildPrintsOnlyTheTarget) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.compilerVersion = "22.1.8";
auto text = ts::format_report(ts::resolve(in), "x86_64-linux-gnu");
EXPECT_EQ(text.find('\n'), std::string::npos)
<< "every layer came from the payload, so none of them is news:\n" << text;
}
TEST(TargetSideReport, VerbosePrintsAllFiveIncludingThePayloadOnes) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.compilerVersion = "22.1.8";
auto text = ts::format_report(ts::resolve(in), "x86_64-linux-gnu",
/*verbose=*/true);
for (auto* label : {"compiler", "compiler-runtime", "kernel-abi", "c-abi",
"c++-abi"})
EXPECT_NE(text.find(label), std::string::npos) << label;
}
// An absent layer is a statement, and whether the stack is shown at all must not
// depend on where that statement sits among the rows. A bare-metal build has an
// absent kernel interface ABOVE a prebuilt C library; deciding per row swallowed
// the first and printed the second.
TEST(TargetSideReport, AnAbsentLayerAboveAPrintedOneIsStillPrinted) {
ts::Inputs in;
in.llvmTriple = "riscv64-none-elf";
in.freestandingTarget = true;
in.compilerFamily = "llvm";
in.sysrootXpkg = "xim:picolibc-riscv@1.8.12";
auto text = ts::format_report(ts::resolve(in), "riscv64-none-elf");
EXPECT_NE(text.find("c-abi"), std::string::npos) << text;
EXPECT_NE(text.find("kernel-abi"), std::string::npos)
<< "the kernel interface is absent, which is the information:\n" << text;
}
TEST(TargetSideReport, OnlyTheLayersThatCameFromElsewhereEarnALine) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.cxxAbi = provider("openkal-llvm-runtime", "0.1.1", "libc++");
auto text = ts::format_report(ts::resolve(in), "x86_64-linux-gnu");
EXPECT_NE(text.find("c++-abi"), std::string::npos);
EXPECT_EQ(text.find("kernel-abi"), std::string::npos)
<< "the kernel interface is the payload's, which is not news";
}
// ── The five layers ──────────────────────────────────────────────────────────
// A payload's C library is named by the triple's env field where the triple has
// one. macOS has none, and falling back to `glibc` named a library that does not
// exist on the platform — invisible while the report printed only the layers a
// build had something to say about.
TEST(TargetSideResolve, ThePayloadCLibraryIsNamedForItsPlatform) {
auto mac = payload_linux();
mac.targetOs = "macos";
mac.targetEnv = "";
EXPECT_EQ(ts::resolve(mac).cAbi.interfaceName, "libSystem");
auto win = payload_linux();
win.targetOs = "windows";
win.targetEnv = "";
EXPECT_EQ(ts::resolve(win).cAbi.interfaceName, "ucrt");
// #540: the layer names the LIBRARY, never the triple's env segment. `musl`
// is both spellings at once and hid the difference; `gnu` is not the name of
// any C library, and on Windows it does not even mean the same one.
EXPECT_EQ(ts::resolve(payload_linux()).cAbi.interfaceName, "glibc")
<< "the env segment `gnu` asks for a C library; the layer answers glibc";
auto winGnu = payload_linux();
winGnu.targetOs = "windows";
winGnu.targetEnv = "gnu";
EXPECT_EQ(ts::resolve(winGnu).cAbi.interfaceName, "ucrt")
<< "on Windows `-gnu` names the MinGW toolchain flavour, not glibc";
auto musl = payload_linux();
musl.targetEnv = "musl";
EXPECT_EQ(ts::resolve(musl).cAbi.interfaceName, "musl")
<< "the one env segment that IS a C library name is unchanged";
}
// The rename above must not turn every ordinary `-gnu` build into a reported
// request mismatch: the request keeps the triple's spelling by definition
// (docs/specs/target-side.md §3.4) and the answer now names the library.
TEST(TargetSideResolve, TheEnvRequestIsSatisfiedByTheLibraryItNames) {
EXPECT_TRUE(ts::c_abi_request_satisfied("gnu", "glibc"));
EXPECT_TRUE(ts::c_abi_request_satisfied("gnu", "ucrt"));
EXPECT_TRUE(ts::c_abi_request_satisfied("musl", "musl"));
EXPECT_TRUE(ts::c_abi_request_satisfied("glibc", "gnu"));
EXPECT_FALSE(ts::c_abi_request_satisfied("gnu", "musl"));
EXPECT_FALSE(ts::c_abi_request_satisfied("musl", "glibc"));
}
TEST(TargetSideResolve, TheCompilerIsALayerAndItIsAlwaysThePayloads) {
auto in = payload_linux();
in.compilerFamily = "gcc";
in.compilerVersion = "16.1.0";
auto r = ts::resolve(in);
EXPECT_EQ(r.compiler.origin, ts::Origin::Payload);
EXPECT_EQ(r.compiler.interfaceName, "gcc");
EXPECT_EQ(r.compiler.impl, "16.1.0");
}
// The builtins are what a PURE C PROGRAM needs. A package supplies them only
// when the payload's own are wrong for the target; absent from the graph means
// the payload's, not absent.
TEST(TargetSideResolve, TheCompilerRuntimeDefaultsToTheCompilersOwn) {
auto in = payload_linux();
in.compilerFamily = "llvm";
auto r = ts::resolve(in);
EXPECT_EQ(r.compilerRuntime.origin, ts::Origin::Payload);
EXPECT_EQ(r.compilerRuntime.interfaceName, "llvm");
in.compilerRuntime = provider("openkal-llvm-runtime", "0.1.1", "compiler-rt");
auto g = ts::resolve(in);
EXPECT_EQ(g.compilerRuntime.origin, ts::Origin::Graph);
EXPECT_EQ(g.compilerRuntime.interfaceName, "compiler-rt");
}
TEST(TargetSideCapability, TheCompilerIsALayerNoPackageMaySupply) {
EXPECT_FALSE(ts::layer_is_suppliable_by_package(ts::CapLayer::Compiler));
for (auto l : {ts::CapLayer::CompilerRuntime, ts::CapLayer::KernelAbi,
ts::CapLayer::CAbi, ts::CapLayer::CxxAbi})
EXPECT_TRUE(ts::layer_is_suppliable_by_package(l));
}
TEST(TargetSideCapability, TheGrammarKnowsAllFiveLayers) {
struct Row { const char* entry; ts::CapLayer layer; const char* iface; };
for (auto const& row : std::initializer_list<Row>{
{"mcpp:compiler=llvm", ts::CapLayer::Compiler, "llvm"},
{"mcpp:compiler-runtime=compiler-rt", ts::CapLayer::CompilerRuntime, "compiler-rt"},
{"mcpp:kernel-abi=openkal", ts::CapLayer::KernelAbi, "openkal"},
{"mcpp:c-abi=musl", ts::CapLayer::CAbi, "musl"},
{"mcpp:c++-abi=libc++", ts::CapLayer::CxxAbi, "libc++"}}) {
auto parsed = ts::parse_capability(row.entry);
ASSERT_TRUE(parsed.has_value()) << row.entry;
ASSERT_TRUE(parsed->has_value()) << row.entry;
EXPECT_EQ((*parsed)->layer, row.layer) << row.entry;
EXPECT_EQ((*parsed)->interfaceName, row.iface) << row.entry;
}
// Outside the reserved prefix the feature system owns the name.
auto own = ts::parse_capability("freestanding-allocator");
ASSERT_TRUE(own.has_value());
EXPECT_FALSE(own->has_value());
// Inside it, a misspelling is an error rather than a silently dead check.
EXPECT_FALSE(ts::parse_capability("mcpp:c_abi=musl").has_value());
}
// ── The triple is a request; the target side is the fact ────────────────────
TEST(TargetSideRequest, AFilledEnvSegmentStatesNothing) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.cAbi = provider("openkal-musl", "0.3.3", "musl");
in.envAxis = ts::EnvAxis::CLibrary;
// `--target x86_64-linux` — the project declined to name a C library, so
// the parser's fill must not become a claim it can be held to.
in.requestedCAbi.clear();
EXPECT_EQ(ts::check_request(ts::resolve(in)), std::nullopt);
}
// REPORTED, NOT REFUSED. The graph supplies the C library either way, so the
// segment is ignored rather than violated and the artifact is identical with or
// without it. Refusing was tried and broke every project spelling the host
// target `x86_64-linux-gnu` — which is what `mcpp toolchain list` prints.
TEST(TargetSideRequest, AWrittenEnvSegmentThatDisagreesIsReported) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.cAbi = provider("openkal-musl", "0.3.3", "musl");
in.requestedCAbi = "gnu";
in.requestFreeTarget = "x86_64-linux";
in.envAxis = ts::EnvAxis::CLibrary;
auto why = ts::check_request(ts::resolve(in));
ASSERT_TRUE(why.has_value());
EXPECT_NE(why->find("`gnu`"), std::string::npos);
EXPECT_NE(why->find("musl"), std::string::npos);
EXPECT_NE(why->find("--target x86_64-linux"), std::string::npos)
<< "telling someone their target name is wrong is only useful once "
"there is a right one to give them";
}
TEST(TargetSideRequest, APrebuiltCLibraryIsWhatTheRequestSelected) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.requestedCAbi = "musl";
in.envAxis = ts::EnvAxis::CLibrary;
// No graph supplier: the payload's C library IS the request's answer, so
// there is nothing to contradict even when the names differ.
EXPECT_EQ(ts::check_request(ts::resolve(in)), std::nullopt);
}
// On Windows the same segment names the OBJECT ABI — `gnu` is PE with the
// GNU ABI, `msvc` is PE with Microsoft's — and both are compatible with more
// than one C library. Reporting such a build as "asking for the `gnu` C ABI"
// describes an axis the name never addressed, and the correction it suggested
// named a target that does not exist.
// AND THE OBJECT-ABI AXIS REPORTS TOO, WHICH IS THE REVERSAL.
//
// It was exempted on the grounds that `gnu` on Windows names the Itanium C++
// ABI rather than a C library. True, and incomplete: the segment bundles the
// object ABI, which IS honoured, with MinGW's C runtime, which a graph-supplied
// C library replaces. The second half is a name/fact disagreement of exactly
// the shape this function reports, and it went unreported — while the identical
// shape on Linux warned.
TEST(TargetSideRequest, TheObjectAbiAxisReportsTheCLibraryHalfToo) {
auto in = payload_linux();
in.targetOs = "windows";
in.compilerFamily = "llvm";
in.cAbi = provider("openkal-musl", "0.3.3", "musl");
in.requestedCAbi = "gnu";
in.requestFreeTarget = "x86_64-windows";
in.envAxis = ts::EnvAxis::ObjectAbi;
auto why = ts::check_request(ts::resolve(in));
ASSERT_NE(why, std::nullopt);
EXPECT_NE(why->find("musl"), std::string::npos) << *why;
// And it says the ABI half was honoured, so the reader does not conclude
// the object ABI changed as well.
EXPECT_NE(why->find("object ABI"), std::string::npos) << *why;
EXPECT_NE(why->find("--target x86_64-windows"), std::string::npos) << *why;
}
// The object-FORMAT axis stays exempt, and not for symmetry: `elf` names no C
// library on any platform, so "asks for the `elf` C ABI" would be nonsense
// rather than merely noisy.
TEST(TargetSideRequest, TheObjectFormatAxisStaysExempt) {
auto in = payload_linux();
in.targetOs = "none";
in.compilerFamily = "llvm";
in.cAbi = provider("openkal-musl", "0.3.3", "musl");
in.requestedCAbi = "elf";
in.requestFreeTarget = "riscv64-none";
in.envAxis = ts::EnvAxis::ObjectFormat;
EXPECT_EQ(ts::check_request(ts::resolve(in)), std::nullopt);
}
// AND WHEN IT DOES NOT NAME ONE, THE REPORT SAYS WHAT IT DOES NAME.
//
// Staying silent is correct as a DIAGNOSTIC and insufficient as a REPORT. The
// reader sees `x86_64-windows-gnu` above a line reading `c-abi musl`, finds no
// row called `gnu`, and maps it to the nearest thing that looks like a C
// library name. Measured twice, by the same reader, on two different days.
// THE GLOSS IS NOW THE OBJECT-FORMAT AXIS ALONE. The object-ABI axis warns
// instead (see `TheObjectAbiAxisReportsTheCLibraryHalfToo`), and leaving both
// in place would state one finding twice — once as an aside, once as a warning.
TEST(TargetSideReport, TheObjectAbiAxisIsNoLongerGlossed) {
auto in = payload_linux();
in.targetOs = "windows";
in.compilerFamily = "llvm";
in.cAbi = provider("openkal-musl", "0.3.3", "musl");
in.requestedCAbi = "gnu";
in.requestFreeTarget = "x86_64-windows";
in.envAxis = ts::EnvAxis::ObjectAbi;
auto r = ts::format_report(ts::resolve(in), "x86_64-windows-gnu");
EXPECT_EQ(r.find("not a C library"), std::string::npos) << r;
}
// A bare-metal target's segment names the object FORMAT, and the same gloss
// applies with a different noun. One enum, three platforms, no `#if`.
TEST(TargetSideReport, OnBareMetalTheSegmentNamesTheObjectFormat) {
auto in = payload_linux();
in.targetOs = "none";
in.compilerFamily = "llvm";
in.cAbi = provider("openkal-musl", "0.3.3", "musl");
in.requestedCAbi = "elf";
in.requestFreeTarget = "riscv64-none";
in.envAxis = ts::EnvAxis::ObjectFormat;
auto r = ts::format_report(ts::resolve(in), "riscv64-none-elf");
EXPECT_NE(r.find("elf selects the object format, not a C library"),
std::string::npos) << r;
}
// AND IT DOES NOT FIRE WHEN THE C LIBRARY CAME FROM A PAYLOAD.
//
// A payload C library IS what the triple selected — the triple is how it was
// selected — so `gnu → ucrt` follows visibly and a gloss would be noise on
// every ordinary Windows build.
TEST(TargetSideReport, NoGlossWhenTheTripleItselfChoseTheCLibrary) {
auto in = payload_linux();
in.targetOs = "windows";
in.compilerFamily = "llvm";
in.requestedCAbi = "gnu";
in.requestFreeTarget = "x86_64-windows";
in.envAxis = ts::EnvAxis::ObjectAbi;
// cAbi left as the payload's — not from the graph.
auto r = ts::format_report(ts::resolve(in), "x86_64-windows-gnu");
EXPECT_EQ(r.find("not a C library"), std::string::npos) << r;
}
// On Linux the segment DOES name a C library, so there is nothing to gloss —
// the disagreement is reported as a warning instead, which is a different
// mechanism and must not double up.
TEST(TargetSideReport, NoGlossWhereTheSegmentNamesACLibrary) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.cAbi = provider("openkal-musl", "0.3.3", "musl");
in.requestedCAbi = "gnu";
in.requestFreeTarget = "x86_64-linux";
in.envAxis = ts::EnvAxis::CLibrary;
auto r = ts::format_report(ts::resolve(in), "x86_64-linux-gnu");
EXPECT_EQ(r.find("not a C library"), std::string::npos) << r;
EXPECT_NE(ts::check_request(ts::resolve(in)), std::nullopt);
}
// ── Rule two: declared requirements ──────────────────────────────────────────
TEST(TargetSideRequirements, ARequirementIsCheckedAgainstWhatResolved) {
auto in = payload_linux();
in.compilerFamily = "gcc";
in.cxxAbi = provider("acme-runtime", "0.1.0", "libc++");
auto r = ts::resolve(in);
std::vector<ts::Requirement> reqs{
{"acme-runtime@0.1.0", ts::CapLayer::Compiler, "llvm"}};
auto why = ts::check_requirements(r, reqs);
ASSERT_TRUE(why.has_value());
EXPECT_NE(why->find("requires the compiler to be `llvm`"), std::string::npos);
EXPECT_NE(why->find("default = \"llvm\""), std::string::npos)
<< "a diagnostic that names no next step is a diagnostic the reader "
"must still go and research";
// AND THE STEP IT NAMES MUST NOT BE A GLOBAL ONE. Until 2026.8.26.2 the
// first remedy offered was `mcpp toolchain default llvm` — the default for
// every project on the machine, changed because ONE project's dependency
// asked. mcpp now applies the graph's requirement itself wherever its own
// answer was revisable, so a global change is both unnecessary and, in the
// only case that still reaches here, ineffective.
EXPECT_EQ(why->find("mcpp toolchain default"), std::string::npos)
<< "the remedy must stay inside the project that has the problem";
// With the caller naming where the compiler was stated, the advice points
// at that statement instead of at the generic pair of spellings.
auto stated = ts::check_requirements(r, reqs, "[toolchain] in mcpp.toml");
ASSERT_TRUE(stated.has_value());
EXPECT_NE(stated->find("[toolchain] in mcpp.toml"), std::string::npos);
EXPECT_NE(stated->find("remove it"), std::string::npos)
<< "removing the statement is what lets mcpp take the graph's compiler";
in.compilerFamily = "llvm";
EXPECT_EQ(ts::check_requirements(ts::resolve(in), reqs), std::nullopt);
}
TEST(TargetSideRequirements, AnUnsuppliedLayerIsNamedAsSuch) {
auto in = payload_linux();
in.compilerFamily = "llvm";
in.freestandingTarget = true;
in.sysrootDeclaredEmpty = true;
std::vector<ts::Requirement> reqs{
{"acme-board@0.1.0", ts::CapLayer::CAbi, "picolibc"}};
auto why = ts::check_requirements(ts::resolve(in), reqs);
ASSERT_TRUE(why.has_value());
EXPECT_NE(why->find("nothing supplies that layer"), std::string::npos);
EXPECT_NE(why->find("mcpp:c-abi=picolibc"), std::string::npos);
}
// ── Rule one: one supplier per layer ─────────────────────────────────────────
TEST(TargetSideConflict, TwoSuppliersAreNamedTogetherWithHowEachArrived) {
ts::Conflict c;
c.layer = ts::CapLayer::CAbi;
c.first = "mcpplibs/openkal-musl@0.3.3";
c.firstVia = "a transitive dependency";
c.second = "acme/tinylibc@0.2.0";
auto text = ts::format_conflict(c);
EXPECT_NE(text.find("openkal-musl@0.3.3"), std::string::npos);
EXPECT_NE(text.find("tinylibc@0.2.0"), std::string::npos);
EXPECT_NE(text.find("a transitive dependency"), std::string::npos);
EXPECT_NE(text.find("a direct dependency"), std::string::npos);
EXPECT_NE(text.find("choice rather than a contribution"), std::string::npos)
<< "the reason matters: choosing wrong does not fail the link, it "
"produces a program that runs and occasionally does not";
}
// ── Which layer decides the payload's C-library flags ───────────────────────
//
// THE PREDICATE THIS REPLACES WAS AN `OR` OVER TWO LAYERS, AND SHIPPED.
//
// bool system_from_graph() const {
// return kernelAbi.fromGraph() || cAbi.fromGraph();
// }
//
// The link side replaced `f.ld` when that was true, dropping the `-B` that
// lets a driver find startup files. The two layers move together for an
// openkal target and come apart for a backend that implements openkal on top
// of a platform whose C library the program still uses. Measured, on three
// lines of manifest:
//
// error: hermetic link check failed
// crt1.o (bare name — the linker cannot resolve it)
//
// ONE TEST PER `Origin`, BECAUSE THE QUESTION HAS ONE ANSWER PER VALUE.
// A predicate written as a list of cases answers the ones its author thought
// of; four tests against a four-valued enum make the fifth value's absence
// visible when someone adds it.
namespace {
mcpp::targetside::TargetSide side_with(mcpp::targetside::Origin cAbiOrigin,
mcpp::targetside::Origin kernelOrigin
= mcpp::targetside::Origin::Graph) {
using namespace mcpp::targetside;
TargetSide ts;
ts.kernelAbi = { kernelOrigin, "openkal", "openkal-linux@0.5.4", false };
ts.cAbi = { cAbiOrigin,
cAbiOrigin == Origin::None ? "" : "glibc", "", false };
return ts;
}
}
TEST(TargetSideCLibrary, ThePayloadsCLibraryIsPrebuilt) {
// The shape that failed: the graph supplies the kernel interface and the C
// library is still the payload's, so the payload's flags must survive.
EXPECT_TRUE(side_with(mcpp::targetside::Origin::Payload).cAbi.prebuilt());
}
TEST(TargetSideCLibrary, AnXpkgSysrootIsPrebuiltToo) {
// The value a hand-written `fromGraph() || absent()` was silent about. A
// sysroot from an xpkg is a directory that existed before resolution, like
// a payload and unlike a package.
EXPECT_TRUE(side_with(mcpp::targetside::Origin::Xpkg).cAbi.prebuilt());
}
TEST(TargetSideCLibrary, AGraphBuiltCLibraryIsNotPrebuilt) {
EXPECT_FALSE(side_with(mcpp::targetside::Origin::Graph).cAbi.prebuilt());
}
TEST(TargetSideCLibrary, NoCLibraryIsNotPrebuilt) {
// Nothing to reach, and the link line adds `-nostdlib -static` for it.
EXPECT_FALSE(side_with(mcpp::targetside::Origin::None).cAbi.prebuilt());
EXPECT_TRUE(side_with(mcpp::targetside::Origin::None).cAbi.absent());
}
TEST(TargetSideCLibrary, TheKernelInterfaceDoesNotDecideIt) {
// The defect itself, as an assertion: every kernel-interface origin leaves
// the answer to the C-ABI layer alone.
using namespace mcpp::targetside;
for (auto k : { Origin::Payload, Origin::Xpkg, Origin::Graph, Origin::None }) {
EXPECT_TRUE(side_with(Origin::Payload, k).cAbi.prebuilt());
EXPECT_FALSE(side_with(Origin::Graph, k).cAbi.prebuilt());
}
}