-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_ninja_backend.cpp
More file actions
2248 lines (2014 loc) · 100 KB
/
Copy pathtest_ninja_backend.cpp
File metadata and controls
2248 lines (2014 loc) · 100 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
#include <gtest/gtest.h>
import std;
import mcpp.source_kind;
import mcpp.build.compile_commands;
import mcpp.build.flags;
import mcpp.build.ninja;
import mcpp.build.plan;
import mcpp.libs.json;
import mcpp.manifest;
import mcpp.toolchain.dialect;
import mcpp.toolchain.model;
import mcpp.platform;
import mcpp.platform.runtime_search;
import mcpp.targetside;
using namespace mcpp::build;
namespace {
std::size_t count_occurrences(std::string_view haystack, std::string_view needle) {
std::size_t count = 0;
std::size_t pos = 0;
while ((pos = haystack.find(needle, pos)) != std::string_view::npos) {
++count;
pos += needle.size();
}
return count;
}
std::string escaped_include_flag(const std::filesystem::path& path) {
auto s = path.string();
std::string escaped;
escaped.reserve(s.size());
for (char c : s) {
if (c == ' ' || c == '$' || c == ':')
escaped.push_back('$');
escaped.push_back(c);
}
return "-I" + escaped;
}
BuildPlan minimal_plan() {
BuildPlan plan;
plan.projectRoot = std::filesystem::temp_directory_path() / "mcpp-ninja-test";
plan.outputDir = plan.projectRoot / "target" / "test";
plan.manifest.package.name = "objc_rule_test";
plan.manifest.buildConfig.cStandard = "c11";
plan.toolchain.compiler = mcpp::toolchain::CompilerId::GCC;
plan.toolchain.version = "test";
plan.toolchain.binaryPath = "/usr/bin/g++";
plan.toolchain.targetTriple = "x86_64-linux-gnu";
// AND THE TARGET SIDE, WHICH THIS FIXTURE USED TO LEAVE DEFAULT.
//
// A default-constructed `TargetSide` has every layer at `Origin::None`,
// which is not a native build — it is "nothing has been resolved". No
// production path reaches the flag builder with one: `resolve` gives a
// plain native build `cAbi = { Payload, … }`, from its final branch.
//
// The fixture got away with it while the link line was gated on
// `kernelAbi.fromGraph() || cAbi.fromGraph()`, which is false for all-None
// and false for a payload build alike. Asking the question the link line
// actually has — did the C library come from a directory that existed
// before resolution — separates them, and the fixture then described a
// target with no C library while every assertion in this file is about one
// that has the payload's.
plan.targetSide.compiler = { mcpp::targetside::Origin::Payload, "gcc", "", false };
plan.targetSide.kernelAbi = { mcpp::targetside::Origin::Payload, "linux", "", false };
plan.targetSide.cAbi = { mcpp::targetside::Origin::Payload, "glibc", "", false };
plan.targetSide.cxx = { mcpp::targetside::Origin::Payload, "libstdc++", "", false };
return plan;
}
} // namespace
TEST(NinjaBackend, ObjectiveCSourceUsesCObjectRuleAndCFlags) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/cocoa.m",
.kind = mcpp::SourceKind::C,
.object = "obj/cocoa.o",
.packageName = "objc_rule_test",
.packageCflags = {"-DOBJ_C_BUILD=1"},
.packageCxxflags = {"-DWRONG_CXX_FLAG=1"},
});
auto ninja = emit_ninja_string(plan);
EXPECT_NE(ninja.find("build obj/cocoa.o : c_object src/cocoa.m"), std::string::npos)
<< ninja;
EXPECT_EQ(ninja.find("build obj/cocoa.o : cxx_object src/cocoa.m"), std::string::npos)
<< ninja;
EXPECT_NE(ninja.find("unit_cflags = -DOBJ_C_BUILD=1"), std::string::npos)
<< ninja;
EXPECT_EQ(ninja.find("unit_cxxflags = -DWRONG_CXX_FLAG=1"), std::string::npos)
<< ninja;
}
// mcpp#235: cxx_module/cxx_object must track header/purview/GMF includes via
// a GNU-style depfile on non-MSVC toolchains (this test's plan uses GCC on a
// non-Windows host, so posixDepfile is true). Before the fix, neither rule
// had ANY depfile outside the msvcDeps branch, so editing a file #include'd
// inside a module's purview (or a header pulled in by a .cpp) never
// invalidated the compile edge. The depfile is routed through a scratch
// `$out.d.raw` + `awk` filter (not written directly to `$out.d`) because
// GCC's `-fmodules` bolts non-standard "reversed" module rules onto the raw
// -MMD output that ninja's depfile loader rejects — see the long comment at
// the definition site for the empirically-confirmed failure mode.
TEST(NinjaBackend, CxxModuleAndCxxObjectRulesTrackHeaderDepsViaGccDepfile) {
// The filtered gcc depfile (#235) is POSIX-only: `posixDepfile =
// !msvcDeps && !is_windows` (awk isn't available on native Windows, and
// MSVC uses `deps = msvc` instead). This asserts the POSIX emission.
if constexpr (mcpp::platform::is_windows)
GTEST_SKIP() << "gcc depfile filter is POSIX-only (Windows uses deps=msvc)";
auto plan = minimal_plan();
auto ninja = emit_ninja_string(plan);
auto module_rule_start = ninja.find("rule cxx_module");
auto object_rule_start = ninja.find("rule cxx_object");
ASSERT_NE(module_rule_start, std::string::npos) << ninja;
ASSERT_NE(object_rule_start, std::string::npos) << ninja;
ASSERT_LT(module_rule_start, object_rule_start) << ninja;
auto module_rule = ninja.substr(module_rule_start, object_rule_start - module_rule_start);
auto object_rule = ninja.substr(object_rule_start);
for (auto const& rule : {module_rule, object_rule}) {
EXPECT_NE(rule.find("-MMD -MF $out.d.raw"), std::string::npos) << ninja;
EXPECT_NE(rule.find("deps = gcc"), std::string::npos) << ninja;
EXPECT_NE(rule.find("depfile = $out.d\n"), std::string::npos) << ninja;
// The raw compiler depfile (with GCC's module-specific reversed
// rules) must never be bound directly as ninja's depfile.
EXPECT_EQ(rule.find("depfile = $out.d.raw"), std::string::npos) << ninja;
}
}
TEST(NinjaBackend, UsesPackageCppStandardForCxxFlags) {
auto plan = minimal_plan();
plan.manifest.package.standard = "c++26";
plan.manifest.language.standard = "c++26";
plan.cppStandard = "c++26";
plan.cppStandardFlag = "-std=c++26";
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "cpp26_test",
});
auto ninja = emit_ninja_string(plan);
EXPECT_NE(ninja.find("cxxflags = -std=c++26"), std::string::npos)
<< ninja;
EXPECT_EQ(ninja.find("-std=c++23"), std::string::npos)
<< ninja;
}
TEST(NinjaBackend, CompileCommandsUsesSameCppStandard) {
auto plan = minimal_plan();
plan.manifest.package.standard = "c++26";
plan.manifest.language.standard = "c++26";
plan.cppStandard = "c++26";
plan.cppStandardFlag = "-std=c++26";
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "cpp26_test",
});
auto flags = compute_flags(plan);
auto cdb = emit_compile_commands(plan, flags);
EXPECT_NE(cdb.find("\"-std=c++26\""), std::string::npos)
<< cdb;
EXPECT_EQ(cdb.find("\"-std=c++23\""), std::string::npos)
<< cdb;
}
TEST(NinjaBackend, CxxFlagsIncludeBuildIncludeDirs) {
auto plan = minimal_plan();
plan.manifest.buildConfig.includeDirs = {"include", "third_party/imgui"};
auto flags = compute_flags(plan);
EXPECT_NE(flags.cxx.find(escaped_include_flag(plan.projectRoot / "include")),
std::string::npos)
<< flags.cxx;
// #390: a multi-segment entry is normalized to NATIVE separators before
// the -I token is built (a mixed `...\third_party/imgui` used to reach
// the CDB through f.cxx). Build the expected path natively too.
auto imgui = plan.projectRoot / "third_party" / "imgui";
EXPECT_NE(flags.cxx.find(escaped_include_flag(imgui)),
std::string::npos)
<< flags.cxx;
}
// #390: the NASM include list is built from the SAME `[build] include_dirs`
// key as the C/C++ one, so it must absolutize and spell entries identically —
// it used to re-derive the join on its own (and with a different "already
// rooted?" predicate). Nothing here is nasm-specific except the channel: the
// point is that one manifest key cannot produce two different paths.
TEST(NinjaBackend, NasmIncludeDirsMatchTheCxxChannelSpelling) {
auto plan = minimal_plan();
plan.nasmPath = "/usr/bin/nasm";
plan.manifest.buildConfig.includeDirs = {"third_party/imgui"};
plan.manifest.buildConfig.includeDirsAfter = {"generated/inc"};
auto flags = compute_flags(plan);
auto native = [](std::filesystem::path p) { p.make_preferred(); return p; };
auto imgui = native(plan.projectRoot / "third_party" / "imgui");
auto gen = native(plan.projectRoot / "generated" / "inc");
// Absolutized against projectRoot, natively spelt, and -I for BOTH keys
// (nasm has no system-header chain to defer to, so after-dirs degrade).
EXPECT_NE(flags.nasm.find(escaped_include_flag(imgui)), std::string::npos)
<< flags.nasm;
EXPECT_NE(flags.nasm.find(escaped_include_flag(gen)), std::string::npos)
<< flags.nasm;
EXPECT_EQ(flags.nasm.find("-idirafter"), std::string::npos) << flags.nasm;
}
// #249: a compile unit's localIncludeDirsAfter emit as -idirafter into the
// same $local_includes variable, APPENDED after the -I entries. -idirafter
// dirs are searched after the toolchain's system dirs (gcc+clang), so a dep
// source root containing a file named like a standard header (ffmpeg's
// VERSION vs libc++'s <version> on case-insensitive macOS) can't shadow it
// while the dep's real headers stay findable. The flag's semantics — not
// its position — carry the priority; the -I-then-idirafter ordering is for
// readability.
TEST(NinjaBackend, LocalIncludeDirsAfterEmitIdirafterAppendedAfterDashI) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "after_test",
.localIncludeDirs = {"/dep/include"},
.localIncludeDirsAfter = {"/dep/tarball-root"},
});
auto ninja = emit_ninja_string(plan);
auto line_start = ninja.find("local_includes =");
ASSERT_NE(line_start, std::string::npos) << ninja;
auto line_end = ninja.find('\n', line_start);
auto line = ninja.substr(line_start, line_end - line_start);
auto i_pos = line.find("-I/dep/include");
auto after_pos = line.find("-idirafter/dep/tarball-root");
ASSERT_NE(i_pos, std::string::npos) << line;
ASSERT_NE(after_pos, std::string::npos) << line;
// After-dirs come after the -I entries within $local_includes.
EXPECT_LT(i_pos, after_pos) << line;
// Never upgraded to -I.
EXPECT_EQ(line.find("-I/dep/tarball-root"), std::string::npos) << line;
}
// #249 MSVC degradation: cl.exe has no -idirafter, so under the msvc
// dialect after-dirs are emitted as regular /I at the END of the include
// list (clang targeting MSVC uses the gnu dialect and gets -idirafter).
TEST(NinjaBackend, MsvcDialectEmitsIncludeDirsAfterAsTrailingSlashI) {
auto plan = minimal_plan();
plan.toolchain.compiler = mcpp::toolchain::CompilerId::MSVC;
plan.toolchain.binaryPath = "cl.exe";
plan.toolchain.targetTriple = "x86_64-pc-windows-msvc";
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "after_test",
.localIncludeDirs = {"/dep/include"},
.localIncludeDirsAfter = {"/dep/tarball-root"},
});
auto ninja = emit_ninja_string(plan);
auto line_start = ninja.find("local_includes =");
ASSERT_NE(line_start, std::string::npos) << ninja;
auto line_end = ninja.find('\n', line_start);
auto line = ninja.substr(line_start, line_end - line_start);
// Both halves take the dialect's prefix. The plain half used to hardcode
// `-I` even here — cl.exe accepts it, so it never broke anything, but it
// meant local_include_flags derived the prefix twice and only agreed with
// the dialect on one of them. Converging both on include_token fixed it.
auto i_pos = line.find("/I/dep/include");
auto after_pos = line.find("/I/dep/tarball-root");
ASSERT_NE(i_pos, std::string::npos) << line;
ASSERT_NE(after_pos, std::string::npos) << line;
EXPECT_LT(i_pos, after_pos) << line;
EXPECT_EQ(line.find("-idirafter"), std::string::npos) << line;
EXPECT_EQ(line.find("-I/dep"), std::string::npos) << line;
}
// #331: the per-TU include channel applied only ninja's `$` escaping, while
// the global channel in flags.cppm shell-quoted. Same manifest include_dirs,
// two derivations — so a directory with a space in it survived one path and
// split into separate shell words on the other, which is what every Windows
// user hits the moment a dependency lands under `C:\Program Files`.
TEST(NinjaBackend, LocalIncludeDirsWithSpacesAreShellQuoted) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "spaced",
.localIncludeDirs = {"/opt/my dep/include"},
.localIncludeDirsAfter = {"/opt/my dep/after"},
});
auto ninja = emit_ninja_string(plan);
auto line_start = ninja.find("local_includes =");
ASSERT_NE(line_start, std::string::npos) << ninja;
auto line = ninja.substr(line_start, ninja.find('\n', line_start) - line_start);
// Two escaping layers, in order: ninja's (`$ ` for a literal space, so
// ninja does not treat it as a field separator) and then the shell's
// (quotes, so what ninja hands to sh stays one word). The old code had
// only the first, which is why the path survived ninja and then split in
// the shell. The prefix must be INSIDE the quotes — quoting the path
// alone would leave `-I` as its own word and reintroduce the split.
//
// The quote character is the host shell's, not a fixed one: POSIX sh
// wants single quotes, cmd.exe double. Hardcoding `'` passed on Linux
// and failed on the Windows runner for a difference that is correct.
const std::string q = mcpp::platform::is_windows ? "\"" : "'";
EXPECT_NE(line.find(q + "-I/opt/my$ dep/include" + q), std::string::npos) << line;
EXPECT_NE(line.find(q + "-idirafter/opt/my$ dep/after" + q), std::string::npos) << line;
}
// #261: on Windows $local_includes is copied into a RESPONSE FILE, which the
// drivers tokenize GNU-style — a backslash is an escape character there, and
// quoting does not exempt it. A native-separator token like C:\src\inc loses
// its separators and every dependency header goes missing, with nothing in
// the error pointing at the include flag.
//
// The separator distinction is real only on Windows: POSIX treats '\' as an
// ordinary filename character, so generic_string() leaves it alone and this
// assertion cannot be made from a Linux host. Guarded rather than weakened —
// a test that passes for the wrong reason everywhere is worse than one that
// says where it applies. Windows CI is the enforcement point.
TEST(NinjaBackend, LocalIncludeTokensUseGenericSeparators) {
const auto& gnu = mcpp::toolchain::gnu_dialect();
auto sub = std::filesystem::path("src") / "inc";
auto tok = mcpp::build::include_token(gnu, sub, {},
mcpp::build::PathForm::Generic);
// True on every host: the generic form never uses the native separator.
EXPECT_NE(tok.find("src/inc"), std::string::npos) << tok;
if constexpr (mcpp::platform::is_windows) {
auto abs = mcpp::build::include_token(
gnu, std::filesystem::path("C:\\src\\inc"), {},
mcpp::build::PathForm::Generic);
EXPECT_EQ(abs.find('\\'), std::string::npos) << abs;
// The command-line channel keeps native separators — a backslash is
// just a character there, and rewriting those paths would be a change
// nobody asked for.
auto native = mcpp::build::include_token(
gnu, std::filesystem::path("C:\\src\\inc"), {},
mcpp::build::PathForm::Native);
EXPECT_NE(native.find('\\'), std::string::npos) << native;
}
}
// #249 NASM degradation: nasm_object edges share $local_includes, but NASM
// would parse `-idirafter<p>` as its `-i` option with value `dirafter<p>` —
// a silently wrong search dir. Only the C/C++ frontends have a system-header
// chain to protect, so a nasm unit's after-dirs degrade to plain -I.
TEST(NinjaBackend, NasmUnitEmitsIncludeDirsAfterAsPlainDashI) {
auto plan = minimal_plan();
plan.nasmPath = "/usr/bin/nasm";
plan.compileUnits.push_back({
.source = "src/scale.asm",
.kind = mcpp::SourceKind::NasmAsm,
.object = "obj/scale.asm.o",
.packageName = "after_test",
.localIncludeDirs = {"/dep/x86"},
.localIncludeDirsAfter = {"/dep/tarball-root"},
});
auto ninja = emit_ninja_string(plan);
auto edge_start = ninja.find("build obj/scale.asm.o : nasm_object");
ASSERT_NE(edge_start, std::string::npos) << ninja;
auto line_start = ninja.find("local_includes =", edge_start);
ASSERT_NE(line_start, std::string::npos) << ninja;
auto line = ninja.substr(line_start, ninja.find('\n', line_start) - line_start);
EXPECT_NE(line.find("-I/dep/x86"), std::string::npos) << line;
EXPECT_NE(line.find("-I/dep/tarball-root"), std::string::npos) << line;
EXPECT_EQ(line.find("-idirafter"), std::string::npos) << line;
}
// Cluster A review fix (#226/#234 follow-up): `[build] include_dirs` is a
// TYPED PATH channel — bare paths from the manifest, dialect prefix applied
// at emission (-I under GNU, /I under MSVC) — not the FLAG-STRING channel
// that normalize_include_flags serves (cflags/cxxflags, where the prefix is
// already embedded in the string by the scanner). Routing dialect-prefixed
// include tokens through normalize_include_flags (whose prefix table only
// knows GNU spellings: -I/-iquote/-isystem/-idirafter/-iprefix/-L) silently
// no-ops under MSVC: "/Iinclude" matches no table entry and is never
// rewritten against plan.projectRoot, so it survives as a *relative* path —
// but ninja runs with cwd = the output dir, so the include stops resolving.
// The fix absolutizes the path directly (dialect-agnostic) before
// prepending the dialect prefix. This test would FAIL before the fix
// (emitting the literal, unrewritten "/Iinclude") and passes after.
TEST(NinjaBackend, MsvcIncludeDirsAreAbsolutizedNotGnuNormalized) {
// The MSVC-dialect logic under test is host-independent; run it on POSIX
// where the test's temp projectRoot has no drive letter. On Windows the
// runner's `C:\...` temp path gets its `:` ninja-escaped (`C$:`), which
// would need escape-aware matching unrelated to what this test verifies.
if constexpr (mcpp::platform::is_windows)
GTEST_SKIP() << "MSVC-dialect path check runs on POSIX (avoids Windows drive-colon ninja escaping)";
auto plan = minimal_plan();
plan.toolchain.compiler = mcpp::toolchain::CompilerId::MSVC;
plan.toolchain.binaryPath = "cl.exe";
plan.toolchain.targetTriple = "x86_64-pc-windows-msvc";
plan.manifest.buildConfig.includeDirs = {"include"};
auto flags = compute_flags(plan);
auto expected = "/I" + (plan.projectRoot / "include").string();
EXPECT_NE(flags.cxx.find(expected), std::string::npos) << flags.cxx;
// The un-rewritten, still-relative token must never appear.
EXPECT_EQ(flags.cxx.find("/Iinclude"), std::string::npos) << flags.cxx;
}
// ── assembly sources (.S/.s → asm_object via $cc, .asm → nasm_object) ────────
TEST(NinjaBackend, GasSourceUsesAsmObjectRule) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/copy.S",
.kind = mcpp::SourceKind::GasAsm,
.object = "obj/copy.S.o",
.packageName = "asm_rule_test",
// Only the -D/-U/-I subset may reach the assembler: -std/-O/-w on an
// asm command line are driver noise (or errors).
.packageCflags = {"-DHAVE_ASM=1", "-std=c99", "-O2", "-w"},
.packageCxxflags = {"-DWRONG_CXX_FLAG=1"},
});
auto ninja = emit_ninja_string(plan);
EXPECT_NE(ninja.find("rule asm_object"), std::string::npos) << ninja;
EXPECT_NE(ninja.find("build obj/copy.S.o : asm_object src/copy.S"),
std::string::npos) << ninja;
EXPECT_NE(ninja.find("cc = "), std::string::npos) << ninja;
EXPECT_NE(ninja.find("unit_asmflags = -DHAVE_ASM=1\n"), std::string::npos)
<< ninja;
EXPECT_EQ(ninja.find("unit_asmflags = -DHAVE_ASM=1 -std=c99"), std::string::npos)
<< ninja;
// The global asm flag string must not carry a C standard or opt level.
auto asmline_pos = ninja.find("asmflags =");
ASSERT_NE(asmline_pos, std::string::npos) << ninja;
auto asmline = ninja.substr(asmline_pos, ninja.find('\n', asmline_pos) - asmline_pos);
EXPECT_EQ(asmline.find("-std="), std::string::npos) << asmline;
EXPECT_EQ(asmline.find("-O"), std::string::npos) << asmline;
}
TEST(NinjaBackend, NasmSourceUsesNasmRuleWithDerivedFormat) {
auto plan = minimal_plan();
plan.nasmPath = "/opt/bin/nasm";
plan.nasmFormat = "elf64";
plan.compileUnits.push_back({
.source = "src/simd.asm",
.kind = mcpp::SourceKind::NasmAsm,
.object = "obj/simd.asm.o",
.packageName = "nasm_rule_test",
.packageCflags = {"-DHAVE_AVX2=1", "-O2"},
});
auto ninja = emit_ninja_string(plan);
EXPECT_NE(ninja.find("rule nasm_object"), std::string::npos) << ninja;
EXPECT_NE(ninja.find("nasm = /opt/bin/nasm"), std::string::npos) << ninja;
EXPECT_NE(ninja.find("nasmfmt = elf64"), std::string::npos) << ninja;
EXPECT_NE(ninja.find("build obj/simd.asm.o : nasm_object src/simd.asm"),
std::string::npos) << ninja;
EXPECT_NE(ninja.find("unit_asmflags = -DHAVE_AVX2=1\n"), std::string::npos)
<< ninja;
}
TEST(NinjaBackend, NoAsmRulesWithoutAsmSources) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "plain_test",
});
auto ninja = emit_ninja_string(plan);
EXPECT_EQ(ninja.find("rule asm_object"), std::string::npos) << ninja;
EXPECT_EQ(ninja.find("rule nasm_object"), std::string::npos) << ninja;
}
TEST(NinjaBackend, CompileCommandsSkipNasmAndCoverGas) {
auto plan = minimal_plan();
plan.nasmPath = "/opt/bin/nasm";
plan.nasmFormat = "elf64";
plan.compileUnits.push_back({
.source = "src/simd.asm",
.kind = mcpp::SourceKind::NasmAsm,
.object = "obj/simd.asm.o",
.packageName = "cdb_test",
});
plan.compileUnits.push_back({
.source = "src/copy.S",
.kind = mcpp::SourceKind::GasAsm,
.object = "obj/copy.S.o",
.packageName = "cdb_test",
});
auto flags = compute_flags(plan);
auto cdb = emit_compile_commands(plan, flags);
// NASM command lines are meaningless to CDB consumers (clangd) — excluded.
EXPECT_EQ(cdb.find("simd.asm"), std::string::npos) << cdb;
// GAS units ride the C driver and stay in the CDB.
EXPECT_NE(cdb.find("copy.S"), std::string::npos) << cdb;
EXPECT_EQ(cdb.find("\"-std=c11\""), std::string::npos) << cdb; // asm-safe flags, no C std
}
// mcpp#234: each packageCflags/packageCxxflags element is already one argv
// token — apply_glob_flags pushes a define like `T=long long` as the single
// element `-DT=long long`. join_flags previously joined tokens with a bare
// space and zero quoting, so once ninja resolved the command line and handed
// it to the shell, the embedded space split `-DT=long long` into TWO words
// (`-DT=long` and a bare `long`). The emitted unit_cflags line must carry the
// define as a single shell-quoted token.
TEST(NinjaBackend, QuotesFlagValueWithSpace) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/main.c",
.kind = mcpp::SourceKind::C,
.object = "obj/main.o",
.packageName = "quote_test",
.packageCflags = {"-DT=long long"},
});
auto ninja = emit_ninja_string(plan);
// shell_quote_arg wraps in single quotes on POSIX, double quotes on
// Windows — assert the platform-appropriate spelling.
const std::string quoted = mcpp::platform::is_windows
? "unit_cflags = \"-DT=long long\""
: "unit_cflags = '-DT=long long'";
EXPECT_NE(ninja.find(quoted), std::string::npos) << ninja;
// Must NOT appear as two bare, unquoted words split on the space.
EXPECT_EQ(ninja.find("unit_cflags = -DT=long long"), std::string::npos)
<< ninja;
}
// Plain framework-shaped flags with nothing shell-significant must pass
// through byte-for-byte unquoted (no over-quoting regression).
TEST(NinjaBackend, PlainFlagsPassThroughUnquoted) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "plain_flag_test",
.packageCxxflags = {"-DFOO=1", "-O2"},
});
auto ninja = emit_ninja_string(plan);
EXPECT_NE(ninja.find("unit_cxxflags = -DFOO=1 -O2"), std::string::npos)
<< ninja;
}
// The SubOS farm must reach DT_RPATH AFTER the artifact's own directory.
//
// It did not, and the composition is why: the farm was appended to the GLOBAL
// `$ldflags` while `$ORIGIN` rides the PER-UNIT `$unit_ldflags`, and every link
// rule renders `$ldflags $unit_ldflags`. Each site read correctly on its own —
// flags.cppm even commented "so it is LAST" — and the artifact loaded a
// different build of libX11 than it had just been linked against.
//
// The assertion is on the EFFECTIVE line (global then unit, exactly as the
// rule expands it), not on either variable alone: checking one of them is what
// made the original mistake invisible.
TEST(NinjaBackend, SubosFarmRpathFollowsTheArtifactsOwnDirectory) {
if constexpr (!mcpp::platform::is_linux)
GTEST_SKIP() << "the SubOS farm rpath is emitted for ELF targets only";
const std::string farm = "/tmp/mcpp-ninja-test-farm/subos/default/lib";
auto plan = minimal_plan();
plan.runtimeSearch.push_back(
{farm, mcpp::platform::search::Origin::SubosFarm});
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "farm_order_test",
});
plan.linkUnits.push_back({
.targetName = "app",
.kind = mcpp::build::LinkUnit::Binary,
.objects = {"obj/main.o"},
// What `shared_library_link_flags` produces for a shared-lib consumer.
.linkFlags = {"-Lbin", "-Wl,-rpath,'$$ORIGIN'", "-lgreet"},
.output = "bin/app",
.entryMain = "src/main.cpp",
});
auto ninja = emit_ninja_string(plan);
auto line_after = [&](std::string_view prefix) -> std::string {
auto at = ninja.find(prefix);
if (at == std::string::npos) return {};
at += prefix.size();
return ninja.substr(at, ninja.find('\n', at) - at);
};
// `$cxx $in -o $out $ldflags $unit_ldflags` — reproduce that expansion.
const std::string effective =
line_after("\nldflags =") + " " + line_after("\n unit_ldflags =");
auto origin = effective.find("$$ORIGIN");
auto fallback = effective.find(farm);
ASSERT_NE(origin, std::string::npos) << effective;
ASSERT_NE(fallback, std::string::npos) << effective;
EXPECT_LT(origin, fallback)
<< "the SubOS farm outranks $ORIGIN, so the artifact can load a "
"different build of a library than it linked against:\n" << effective;
// And it must have left the global channel entirely — leaving a copy there
// would restore the old order no matter what the unit tail says.
EXPECT_EQ(line_after("\nldflags =").find(farm), std::string::npos) << ninja;
}
// An archive is produced by `ar`, whose rule never expands `$unit_ldflags`.
// Emitting run-time search flags there would be dead bytes in every graph.
TEST(NinjaBackend, StaticLibraryCarriesNoRuntimeFallback) {
if constexpr (!mcpp::platform::is_linux)
GTEST_SKIP() << "the SubOS farm rpath is emitted for ELF targets only";
const std::string farm = "/tmp/mcpp-ninja-test-farm/subos/default/lib";
auto plan = minimal_plan();
plan.runtimeSearch.push_back(
{farm, mcpp::platform::search::Origin::SubosFarm});
plan.compileUnits.push_back({
.source = "src/lib.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/lib.o",
.packageName = "farm_order_test",
});
plan.linkUnits.push_back({
.targetName = "greet",
.kind = mcpp::build::LinkUnit::StaticLibrary,
.objects = {"obj/lib.o"},
.output = "lib/libgreet.a",
});
auto ninja = emit_ninja_string(plan);
auto at = ninja.find("build lib/libgreet.a");
ASSERT_NE(at, std::string::npos) << ninja;
auto stanza = ninja.substr(at, ninja.find("\nbuild ", at + 1) - at);
EXPECT_EQ(stanza.find(farm), std::string::npos) << stanza;
}
// Regression: mcpp-GENERATED per-unit LINK flags are already correctly
// shell-quoted + ninja-escaped at construction — e.g. the shared-dep rpath
// token `-Wl,-rpath,'$$ORIGIN'` (single quotes stop shell $-expansion, `$$`
// is ninja's literal `$`). join_flags for link flags must NOT re-run
// shell_quote_arg over them: doing so double-quotes the token, baking a
// literal `'$ORIGIN'` (quotes included) into the binary's RUNPATH so the
// dynamic linker can't find dependency .so's next to the exe (e2e 55-57/64).
TEST(NinjaBackend, LinkFlagsAreNotReQuoted) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "rpath_test",
});
plan.linkUnits.push_back({
.targetName = "app",
.kind = mcpp::build::LinkUnit::Binary,
.objects = {"obj/main.o"},
.linkFlags = {"-Wl,-rpath,'$$ORIGIN'"},
.output = "bin/app",
.entryMain = "src/main.cpp",
});
auto ninja = emit_ninja_string(plan);
// The rpath token passes through verbatim (ninja `$$` = literal `$`).
EXPECT_NE(ninja.find("-Wl,-rpath,'$$ORIGIN'"), std::string::npos) << ninja;
// Must NOT be double-quoted: shell_quote_arg escaping an embedded `'`
// produces the `'\''` sequence, which only appears if it re-quoted.
EXPECT_EQ(ninja.find("'\\''"), std::string::npos) << ninja;
}
// Regression (#234 follow-up): a raw descriptor flag that packs two argv
// tokens into one string — e.g. compat.lua's `-include <header>` — must pass
// through VERBATIM so the shell splits it back into `-include` + the header.
// Blanket shell-quoting wrapped it into one malformed arg, so gcc looked for a
// file literally named "<space>header" → "No such file" → aarch64/macos/windows
// cross-builds failed. Only `-D`/`/D` define tokens with an intra-value space
// get quoted; `-include foo.h` does not.
TEST(NinjaBackend, RawMultiTokenFlagIsNotQuoted) {
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/main.c",
.kind = mcpp::SourceKind::C,
.object = "obj/main.o",
.packageName = "include_flag_test",
.packageCflags = {"-include mcpp_lua_platform_config.h"},
});
auto ninja = emit_ninja_string(plan);
// Verbatim, so the shell re-splits into two args.
EXPECT_NE(ninja.find("unit_cflags = -include mcpp_lua_platform_config.h"),
std::string::npos) << ninja;
// Must NOT be wrapped in quotes (which would make it one malformed arg).
EXPECT_EQ(ninja.find("'-include mcpp_lua_platform_config.h'"),
std::string::npos) << ninja;
}
// mcpp#247 + #344: link/archive/shared route the object list through a response
// file on EVERY platform. The number of objects on one link edge is unbounded —
// an ffmpeg/opencv-class source package contributes thousands — and every way of
// spawning a command has a ceiling:
//
// Windows CreateProcess, 32 KiB command line
// POSIX ninja runs `sh -c "<whole command>"`, so the command is a SINGLE
// argv entry and hits MAX_ARG_STRLEN (32 pages, 128 KiB) long before
// the 2 MiB ARG_MAX anyone would think to check
//
// This test used to assert the OPPOSITE for POSIX ("ARG_MAX is ample"), pinning
// an assumption that was both about the wrong limit and false: mcpp-index's
// opencv-module link line was already 56 840 bytes inline, and #344's per-package
// object directories took it to 161 687 — at which point ninja dies with
// `posix_spawn: Argument list too long`, naming no edge and no cause. A ceiling
// nothing watches is not a ceiling anyone can stay under, so there isn't one now.
TEST(NinjaBackend, DriverStyleLinkRulesAlwaysUseRspfile) {
auto plan = minimal_plan(); // GCC → gnu dialect → driver-style branch
auto ninja = emit_ninja_string(plan);
for (std::string_view rule : {"rule cxx_link\n", "rule cxx_archive\n",
"rule cxx_shared\n"}) {
auto start = ninja.find(rule);
ASSERT_NE(start, std::string::npos) << ninja;
auto end = ninja.find("\n\n", start);
ASSERT_NE(end, std::string::npos) << ninja;
auto body = ninja.substr(start, end - start);
EXPECT_NE(body.find("@$out.rsp"), std::string::npos) << body;
EXPECT_NE(body.find("rspfile = $out.rsp"), std::string::npos) << body;
EXPECT_NE(body.find("rspfile_content = $in"), std::string::npos) << body;
// And the object list must no longer be inlined into the command: that
// is the whole point, so `$in` may appear only as rspfile_content.
EXPECT_EQ(count_occurrences(body, "$in"), 1u) << body;
}
}
TEST(NinjaBackend, RootPackageCxxflagsAreEmittedOncePerUnit) {
auto plan = minimal_plan();
plan.manifest.buildConfig.cxxflags = {"-DROOT_FLAG=1"};
plan.compileUnits.push_back({
.source = "src/main.cpp",
.kind = mcpp::SourceKind::Cxx,
.object = "obj/main.o",
.packageName = "root_flag_test",
.packageCxxflags = {"-DROOT_FLAG=1"},
});
auto ninja = emit_ninja_string(plan);
EXPECT_EQ(count_occurrences(ninja, "unit_cxxflags = -DROOT_FLAG=1"), 2u)
<< ninja;
EXPECT_EQ(ninja.find("cxxflags = -std=c++23 -O2 -DROOT_FLAG=1"), std::string::npos)
<< ninja;
}
// mcpp#261: the clang scan rule used shell redirection (`> $out`), which on
// Windows forced a `cmd /c` wrapper and with it cmd.exe's 8191-char command
// line ceiling — a quarter of what ninja's CreateProcess path allows. Any
// package with a large include-dir list overran it. clang-scan-deps has -o
// (LLVM 17+), so the redirect and the wrapper are both unnecessary.
TEST(NinjaBackend, ClangScanRuleWritesViaDashOWithoutShellRedirection) {
auto plan = minimal_plan();
plan.toolchain.compiler = mcpp::toolchain::CompilerId::Clang;
plan.toolchain.binaryPath = "/usr/bin/clang++";
plan.scanDepsPath = "/usr/bin/clang-scan-deps";
plan.compileUnits.push_back({
.source = "src/m.cppm",
.kind = mcpp::SourceKind::ModuleInterface,
.object = "obj/m.o",
.packageName = "objc_rule_test",
.providesModule = "m",
});
auto ninja = emit_ninja_string(plan);
auto scanRule = ninja.find("rule cxx_scan");
ASSERT_NE(scanRule, std::string::npos) << ninja;
auto scanEnd = ninja.find("description = SCAN", scanRule);
ASSERT_NE(scanEnd, std::string::npos) << ninja;
auto rule = ninja.substr(scanRule, scanEnd - scanRule);
EXPECT_NE(rule.find("-format=p1689 -o $out --"), std::string::npos) << rule;
EXPECT_EQ(rule.find("> $out"), std::string::npos)
<< "scan rule must not use shell redirection: " << rule;
}
// The `cmd /c` wrapper was the only place mcpp put a shell between ninja and
// a compiler invocation. Keep it gone: it is what re-imposes the 8191 ceiling.
TEST(NinjaBackend, NoRuleWrapsItsCommandInCmdSlashC) {
for (auto compiler : {mcpp::toolchain::CompilerId::GCC,
mcpp::toolchain::CompilerId::Clang}) {
auto plan = minimal_plan();
plan.toolchain.compiler = compiler;
if (compiler == mcpp::toolchain::CompilerId::Clang) {
plan.toolchain.binaryPath = "/usr/bin/clang++";
plan.scanDepsPath = "/usr/bin/clang-scan-deps";
}
plan.compileUnits.push_back({
.source = "src/m.cppm",
.kind = mcpp::SourceKind::ModuleInterface,
.object = "obj/m.o",
.packageName = "objc_rule_test",
.providesModule = "m",
});
auto ninja = emit_ninja_string(plan);
EXPECT_EQ(ninja.find("cmd /c"), std::string::npos)
<< "compiler=" << static_cast<int>(compiler) << "\n" << ninja;
}
}
// mcpp#261: the compile and scan rules carry an UNBOUNDED flag payload —
// one -I per dependency include dir — and on Windows ninja spawns through
// CreateProcess (32767-char ceiling). They now route that payload through a
// response file, the same mitigation #247 gave the link rules for the same
// reason. Reachable from a POSIX test host via the msvc dialect, which only
// ever runs on Windows. NASM is excluded on purpose (it spells response
// files `-@ file`), and POSIX keeps the inline form byte-identical.
TEST(NinjaBackend, CompileAndScanRulesRouteFlagsThroughRspfileUnderMsvcDialect) {
auto plan = minimal_plan();
plan.toolchain.compiler = mcpp::toolchain::CompilerId::MSVC;
plan.toolchain.binaryPath = "cl.exe";
plan.compileUnits.push_back({
.source = "src/m.cppm",
.kind = mcpp::SourceKind::ModuleInterface,
.object = "obj/m.o",
.packageName = "objc_rule_test",
.providesModule = "m",
});
plan.compileUnits.push_back({
.source = "src/a.c",
.kind = mcpp::SourceKind::C,
.object = "obj/a.o",
.packageName = "objc_rule_test",
});
auto ninja = emit_ninja_string(plan);
// cxx_module and cxx_object pick their Windows shape through
// `if constexpr (is_windows)`, so their response-file form is only
// reachable on a Windows host; c_object and cxx_scan have no such
// compile-time branch and are assertable everywhere.
std::vector<std::string_view> rules{"rule c_object\n", "rule cxx_scan\n"};
if constexpr (mcpp::platform::is_windows) {
rules.push_back("rule cxx_module\n");
rules.push_back("rule cxx_object\n");
}
for (std::string_view rule : rules) {
auto start = ninja.find(rule);
ASSERT_NE(start, std::string::npos) << rule << "\n" << ninja;
auto end = ninja.find("\n\n", start);
ASSERT_NE(end, std::string::npos) << ninja;
auto body = ninja.substr(start, end - start);
EXPECT_NE(body.find("@$out.rsp"), std::string::npos) << body;
EXPECT_NE(body.find("rspfile = $out.rsp"), std::string::npos) << body;
// ONLY $local_includes may live in the response file: its content
// is tokenized GNU-style (backslash = escape), and those are the
// only paths this file forward-slashes itself. $cxxflags carries
// native-separated paths from flags.cppm and must stay inline —
// routing it through the rsp ate the separators of the std.pcm path
// and broke every `import std;` on Windows.
EXPECT_NE(body.find("rspfile_content = $local_includes\n"),
std::string::npos) << body;
// The payload must not ALSO remain inline, or the ceiling stands.
auto cmdStart = body.find("command = ");
auto cmdEnd = body.find('\n', cmdStart);
auto cmd = body.substr(cmdStart, cmdEnd - cmdStart);
EXPECT_EQ(cmd.find("$local_includes"), std::string::npos) << cmd;
// ...and the flags that must NOT move off the command line stay there.
if (rule != "rule c_object\n")
EXPECT_NE(cmd.find("$cxxflags"), std::string::npos) << cmd;
else
EXPECT_NE(cmd.find("$cflags"), std::string::npos) << cmd;
}
}
// POSIX must keep the inline form: ARG_MAX is ample and an inline command is
// far easier to re-run by hand. This is the byte-identity guard for the
// #261 change on the platform where it must be a no-op.
TEST(NinjaBackend, CompileRulesStayInlineOnPosixDrivers) {
if constexpr (mcpp::platform::is_windows) {
GTEST_SKIP() << "inline form is Windows-exempt by design";
} else {
auto plan = minimal_plan(); // GCC → gnu dialect, non-msvc deps
plan.compileUnits.push_back({
.source = "src/a.c",
.kind = mcpp::SourceKind::C,
.object = "obj/a.o",
.packageName = "objc_rule_test",
});
auto ninja = emit_ninja_string(plan);
for (std::string_view rule : {"rule cxx_module\n", "rule cxx_object\n",
"rule c_object\n"}) {
auto start = ninja.find(rule);
ASSERT_NE(start, std::string::npos) << rule << "\n" << ninja;
auto end = ninja.find("\n\n", start);
ASSERT_NE(end, std::string::npos) << ninja;
auto body = ninja.substr(start, end - start);
EXPECT_EQ(body.find("rspfile"), std::string::npos) << body;
EXPECT_NE(body.find("$local_includes"), std::string::npos) << body;
}
}
}
// mcpp#257: "emit a depfile" and "strip GCC's reversed module rules" are two
// decisions; 0.0.97 conflated them and left Clang with no include tracking
// at all. Clang emits a single plain make rule (measured on 20.1.7/22.1.8),
// so it takes the depfile WITHOUT the awk filter, writing -MF straight to
// $out.d.
TEST(NinjaBackend, ClangGetsDepfileWithoutTheGccModuleRuleFilter) {
if constexpr (mcpp::platform::is_windows)
GTEST_SKIP() << "POSIX depfile shape only";
auto plan = minimal_plan();
plan.toolchain.compiler = mcpp::toolchain::CompilerId::Clang;
plan.toolchain.binaryPath = "/usr/bin/clang++";
auto ninja = emit_ninja_string(plan);
auto module_rule_start = ninja.find("rule cxx_module");
auto object_rule_start = ninja.find("rule cxx_object");
ASSERT_NE(module_rule_start, std::string::npos) << ninja;
ASSERT_NE(object_rule_start, std::string::npos) << ninja;
auto module_rule = ninja.substr(module_rule_start,
object_rule_start - module_rule_start);
EXPECT_NE(module_rule.find("-MMD -MF $out.d"), std::string::npos) << ninja;
EXPECT_NE(module_rule.find("deps = gcc"), std::string::npos) << ninja;
EXPECT_NE(module_rule.find("depfile = $out.d\n"), std::string::npos) << ninja;
// No scratch file and no filter: there is nothing to strip.
EXPECT_EQ(module_rule.find("$out.d.raw"), std::string::npos) << ninja;
EXPECT_EQ(module_rule.find("awk"), std::string::npos) << ninja;
}
// The other half of the same asymmetry: C and GAS edges include headers too
// and had no depfile on ANY toolchain.
TEST(NinjaBackend, CAndAsmRulesAlsoTrackHeaderDeps) {
if constexpr (mcpp::platform::is_windows)
GTEST_SKIP() << "POSIX depfile shape only";
auto plan = minimal_plan();
plan.compileUnits.push_back({
.source = "src/a.c",
.kind = mcpp::SourceKind::C,
.object = "obj/a.o",
.packageName = "objc_rule_test",
});
plan.compileUnits.push_back({
.source = "src/b.S",
.kind = mcpp::SourceKind::GasAsm,
.object = "obj/b.o",
.packageName = "objc_rule_test",
});
auto ninja = emit_ninja_string(plan);
for (std::string_view rule : {"rule c_object\n", "rule asm_object\n"}) {
// NOTE: asm_object is the `.S` rule. `.s` uses asm_object_raw and
// deliberately has no depfile — see below.
auto start = ninja.find(rule);
ASSERT_NE(start, std::string::npos) << rule << "\n" << ninja;
auto end = ninja.find("\n\n", start);
ASSERT_NE(end, std::string::npos) << ninja;
auto body = ninja.substr(start, end - start);
EXPECT_NE(body.find("-MMD -MF $out.d"), std::string::npos) << body;
EXPECT_NE(body.find("deps = gcc"), std::string::npos) << body;