cuno-toolchain 0.3.0

Compiler abstraction, detection, per-compiler implementations, and flag translation.
Documentation
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
//! The link step's arguments, row by row from `docs/en/specs/build.md`,
//! "Settings and flags". Linking goes through the compiler driver, so each
//! driver adds its own C runtime libraries.

use std::ffi::OsString;
use std::path::{Path, PathBuf};

use super::{BuildSettings, OptLevel, ProfileSettings, Subsystem, TARGET_SWITCH};
use crate::Version;
use crate::compiler::{CompilerKind, Family};

/// What one link of the build is made from.
#[derive(Clone, Copy, Debug)]
pub struct LinkInput<'a> {
    /// The driver.
    pub kind: CompilerKind,
    /// Its version: GCC links `libstdc++exp` from 14 (`build.md`, "Settings
    /// and flags").
    pub version: Version,
    /// The resolved build settings (`links`, `lib-dirs`, `link-flags`).
    pub build: &'a BuildSettings,
    /// The resolved profile (`debug-info`, `lto`, and `opt-level` for LTO).
    pub profile: &'a ProfileSettings,
    /// What the link makes.
    pub output: Linked<'a>,
    /// Whether any of its objects is C++, which GCC links with `g++` and
    /// its C++ runtime.
    pub cxx: bool,
}

/// What a link makes (`docs/en/specs/build.md`, "Linking" and "Building a
/// library").
#[derive(Clone, Copy, Debug)]
pub enum Linked<'a> {
    /// An executable, started as its `[[bin]] subsystem` says.
    Executable {
        /// Its `[[bin]] subsystem`.
        subsystem: Subsystem,
        /// Whether it links the body of a shared library's package, which
        /// exports, so that it is told to write no import library of its
        /// own (`docs/en/specs/build.md`, "Linking").
        of_shared: bool,
    },
    /// A DLL, with its import library at `import` and, with debug
    /// information, its PDB at `pdb`: both beside the static libraries
    /// rather than the DLL, where an executable of the same name would write
    /// its own.
    Shared {
        /// The import library.
        import: &'a Path,
        /// The program database.
        pdb: &'a Path,
    },
}

/// The files the linker may take `lib` from, for a `links` entry built
/// with `kind`, in the order it tries them in each directory: `<lib>.lib`
/// for the compilers that build for the MSVC ABI (the MSVC and clang-cl
/// link commands name it, clang's `-l<lib>` is found as it by lld), and
/// the seven names MinGW's `ld` tries for `-l<lib>` (as `-Wl,--verbose`
/// lists its attempts). The
/// build looks for them in `lib-dirs` to relink when one changes
/// (`docs/en/specs/build.md`, "Deciding what changed"), so a compiler
/// naming libraries otherwise answers here.
pub fn library_files(kind: CompilerKind, lib: &str) -> Vec<String> {
    match kind {
        CompilerKind::Msvc | CompilerKind::ClangCl | CompilerKind::Clang => {
            vec![format!("{lib}.lib")]
        }
        CompilerKind::Gcc => vec![
            format!("lib{lib}.dll.a"),
            format!("{lib}.dll.a"),
            format!("lib{lib}.a"),
            format!("{lib}.lib"),
            format!("lib{lib}.lib"),
            format!("lib{lib}.dll"),
            format!("{lib}.dll"),
        ],
    }
}

/// What makes GCC link its own runtime and `winpthread` statically, for
/// either `runtime` (`docs/en/specs/build.md`, "Settings and flags"): their
/// DLLs are not part of Windows. `-lstdc++` comes before `-lwinpthread`,
/// which resolves what libstdc++ uses; `-Bdynamic` after them leaves the
/// libraries the driver adds (UCRT's) as they are. From GCC 14, C++ also
/// links `libstdc++exp`, where MinGW's libstdc++ keeps what `std::print`
/// writes to a terminal with; before 14 there is no `<print>` to need it.
fn gcc_runtime_args(cxx: bool, version: Version) -> Vec<&'static str> {
    let mut args = vec!["-static-libgcc"];
    if cxx {
        args.push("-static-libstdc++");
    }
    args.push("-Wl,-Bstatic");
    if cxx {
        if version.major >= 14 {
            args.push("-lstdc++exp");
        }
        args.push("-lstdc++");
    }
    args.extend(["-lwinpthread", "-Wl,-Bdynamic"]);
    args
}

/// The arguments for the driver to link `objects` into `exe` (or the DLL,
/// as `input.output` says): the driver's own switches, the objects and the
/// output, then the linker's arguments (after `/link` for MSVC and
/// clang-cl), with `link-flags` last. Arguments that carry a path go to the
/// linker with `-Xlinker` rather than `-Wl,`, which splits at commas.
pub fn link_args(input: &LinkInput<'_>, objects: &[PathBuf], exe: &Path) -> Vec<OsString> {
    let build = input.build;
    let profile = input.profile;
    // lld optimises at level 2 when it runs LTO unless told the level.
    let lto_level = match profile.opt_level {
        OptLevel::O0 => "0",
        OptLevel::O1 => "1",
        OptLevel::O2 | OptLevel::Size => "2",
        OptLevel::O3 => "3",
    };
    let mut args: Vec<OsString> = Vec::new();
    let push = |args: &mut Vec<OsString>, flags: &[&str]| {
        args.extend(flags.iter().map(OsString::from));
    };

    let shared = match input.output {
        Linked::Executable { .. } => None,
        Linked::Shared { import, pdb } => Some((import, pdb)),
    };
    match input.kind {
        CompilerKind::Msvc | CompilerKind::ClangCl if shared.is_some() => {
            push(&mut args, &["/LD"]);
        }
        CompilerKind::Clang | CompilerKind::Gcc if shared.is_some() => {
            push(&mut args, &["-shared"]);
        }
        CompilerKind::Msvc | CompilerKind::ClangCl | CompilerKind::Clang | CompilerKind::Gcc => {}
    }
    match input.kind {
        CompilerKind::Msvc => push(&mut args, &["/nologo"]),
        CompilerKind::ClangCl => {
            push(&mut args, &["/nologo", "-fuse-ld=lld"]);
            if profile.lto {
                push(&mut args, &["-flto"]);
            }
        }
        CompilerKind::Clang => {
            // `-fms-runtime-lib` is left out: the objects already say which C
            // runtime they use, and the linker follows them.
            push(&mut args, &[TARGET_SWITCH, "-fuse-ld=lld"]);
            if profile.debug_info {
                push(&mut args, &["-g"]);
            }
            if profile.lto {
                push(&mut args, &["-flto"]);
            }
        }
        CompilerKind::Gcc => {
            if profile.debug_info {
                push(&mut args, &["-g"]);
            }
            // At link time GCC optimises at the level the objects were
            // compiled at, so no level is given; `auto` runs it in
            // parallel, where plain `-flto` warns that it runs serially.
            if profile.lto {
                push(&mut args, &["-flto=auto"]);
            }
        }
    }

    args.extend(objects.iter().map(OsString::from));

    match input.kind {
        // The GNU-style drivers: clang linking with lld for the MSVC ABI,
        // and MinGW's GCC with its own ld.
        CompilerKind::Clang | CompilerKind::Gcc => {
            args.extend(["-o".into(), exe.into()]);
            for lib in &build.links {
                args.push(format!("-l{lib}").into());
            }
            for dir in &build.lib_dirs {
                let mut arg = OsString::from("-L");
                arg.push(dir);
                args.push(arg);
            }
            let gcc = input.kind == CompilerKind::Gcc;
            match (input.output, gcc) {
                (
                    Linked::Executable {
                        subsystem,
                        of_shared,
                    },
                    gcc,
                ) => {
                    push(
                        &mut args,
                        match (subsystem, gcc) {
                            (Subsystem::Console, false) => &["-Wl,/subsystem:console"][..],
                            (Subsystem::Windows, false) => {
                                &["-Wl,/subsystem:windows", "-Wl,/entry:mainCRTStartup"]
                            }
                            // MinGW's start-up code calls `main` for either.
                            (Subsystem::Console, true) => &["-mconsole"],
                            (Subsystem::Windows, true) => &["-mwindows"],
                        },
                    );
                    // lld would write an import library of the executable's
                    // name; `ld` writes none unless asked.
                    if of_shared && !gcc {
                        push(&mut args, &["-Wl,/noimplib"]);
                    }
                }
                (Linked::Shared { import, .. }, true) => {
                    args.extend(["-Xlinker".into(), "--out-implib".into()]);
                    args.extend(["-Xlinker".into(), import.into()]);
                }
                (Linked::Shared { import, pdb }, false) => {
                    let mut arg = OsString::from("/implib:");
                    arg.push(import);
                    args.extend(["-Xlinker".into(), arg]);
                    if profile.debug_info {
                        let mut arg = OsString::from("/pdb:");
                        arg.push(pdb);
                        args.extend(["-Xlinker".into(), arg]);
                    }
                }
            }
            if gcc {
                push(&mut args, &gcc_runtime_args(input.cxx, input.version));
            } else if profile.lto {
                args.push(format!("-Wl,/opt:lldlto={lto_level}").into());
            }
            // `-Xlinker`, not `-Wl,`, which would split a flag at its commas.
            for flag in &build.link_flags {
                args.extend(["-Xlinker".into(), flag.into()]);
            }
            return args;
        }
        CompilerKind::Msvc | CompilerKind::ClangCl => {}
    }

    let mut out = OsString::from("/Fe:");
    out.push(exe);
    args.push(out);
    // Everything after `/link` goes to the linker itself.
    args.push("/link".into());
    for lib in &build.links {
        // The first name is the one cl and clang-cl are given.
        args.push(library_files(input.kind, lib).remove(0).into());
    }
    for dir in &build.lib_dirs {
        let mut arg = OsString::from("/LIBPATH:");
        arg.push(dir);
        args.push(arg);
    }
    match input.output {
        Linked::Executable {
            subsystem,
            of_shared,
        } => {
            // Stated for console too: otherwise a program with `WinMain` and
            // no `main` is silently linked as a GUI program.
            push(
                &mut args,
                match subsystem {
                    Subsystem::Console => &["/SUBSYSTEM:CONSOLE"][..],
                    Subsystem::Windows => &["/SUBSYSTEM:WINDOWS", "/ENTRY:mainCRTStartup"],
                },
            );
            // An executable of a shared library's package links the body,
            // which exports; the linker would write, and link.exe announce,
            // an import library (and link.exe an `.exp`) of the executable's
            // name. Only there: elsewhere a user's `/IMPLIB` must still work,
            // and lld lets `/NOIMPLIB` win over it (build.md, "Linking").
            if of_shared {
                push(
                    &mut args,
                    match input.kind.family() {
                        Family::Msvc => &["/NOIMPLIB", "/NOEXP"][..],
                        Family::Llvm | Family::Gnu => &["/NOIMPLIB"],
                    },
                );
            }
        }
        Linked::Shared { import, pdb } => {
            let mut arg = OsString::from("/IMPLIB:");
            arg.push(import);
            args.push(arg);
            if profile.debug_info {
                let mut arg = OsString::from("/PDB:");
                arg.push(pdb);
                args.push(arg);
            }
        }
    }
    if profile.debug_info {
        push(&mut args, &["/DEBUG"]);
        match input.kind.family() {
            // link.exe links incrementally under /DEBUG; lld does not.
            Family::Msvc => push(&mut args, &["/INCREMENTAL:NO"]),
            Family::Llvm | Family::Gnu => {}
        }
    }
    if profile.lto {
        match input.kind.family() {
            Family::Msvc => push(&mut args, &["/LTCG"]),
            Family::Llvm | Family::Gnu => args.push(format!("/opt:lldlto={lto_level}").into()),
        }
    }

    args.extend(build.link_flags.iter().map(OsString::from));
    args
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The compilers for the MSVC ABI name a library `<lib>.lib`; GCC's
    /// `ld` tries seven names in each directory, in the order
    /// `-Wl,--verbose` lists its attempts.
    ///
    /// Mutation: name it otherwise for one compiler, or reorder GCC's
    /// names; this test fails.
    #[test]
    fn a_library_is_the_files_its_linker_tries() {
        for kind in CompilerKind::ALL {
            let want: Vec<&str> = match kind {
                CompilerKind::Msvc | CompilerKind::ClangCl | CompilerKind::Clang => {
                    vec!["foo.lib"]
                }
                CompilerKind::Gcc => vec![
                    "libfoo.dll.a",
                    "foo.dll.a",
                    "libfoo.a",
                    "foo.lib",
                    "libfoo.lib",
                    "libfoo.dll",
                    "foo.dll",
                ],
            };
            assert_eq!(library_files(kind, "foo"), want, "{kind:?}");
        }
    }
    use crate::flags::Runtime;

    fn build() -> BuildSettings {
        BuildSettings {
            warnings: crate::flags::Warnings::Default,
            warnings_as_errors: false,
            defines: vec![],
            include_dirs: vec![],
            links: vec![],
            lib_dirs: vec![],
            link_flags: vec![],
            flags: vec![],
            runtime: Runtime::Dynamic,
            exceptions: true,
            rtti: true,
        }
    }

    fn profile() -> ProfileSettings {
        ProfileSettings {
            opt_level: OptLevel::O0,
            debug_info: false,
            debug_assertions: true,
            lto: false,
        }
    }

    fn args(
        kind: CompilerKind,
        build: &BuildSettings,
        profile: &ProfileSettings,
        subsystem: Subsystem,
    ) -> Vec<String> {
        args_for(kind, build, profile, subsystem, true)
    }

    fn args_for(
        kind: CompilerKind,
        build: &BuildSettings,
        profile: &ProfileSettings,
        subsystem: Subsystem,
        cxx: bool,
    ) -> Vec<String> {
        args_linking(
            kind,
            build,
            profile,
            Linked::Executable {
                subsystem,
                of_shared: false,
            },
            cxx,
        )
    }

    fn args_linking(
        kind: CompilerKind,
        build: &BuildSettings,
        profile: &ProfileSettings,
        output: Linked<'_>,
        cxx: bool,
    ) -> Vec<String> {
        // A GCC from before libstdc++exp, so each case's expected arguments
        // need not say which GCC; the version's own test is below.
        let input = LinkInput {
            kind,
            version: Version {
                major: 13,
                minor: 2,
                patch: 0,
            },
            build,
            profile,
            output,
            cxx,
        };
        link_args(
            &input,
            &[PathBuf::from("a.obj"), PathBuf::from("b.obj")],
            Path::new("app.exe"),
        )
        .into_iter()
        .map(|a| a.into_string().unwrap())
        .collect()
    }

    /// Mutation: drop or reorder any default link argument of any driver, leave
    /// the console subsystem unstated, pass the C runtime at link time, or put
    /// the output after `/link`; this test fails.
    #[test]
    fn the_defaults_give_each_drivers_link_line() {
        let (b, p) = (build(), profile());
        assert_eq!(
            args(CompilerKind::Msvc, &b, &p, Subsystem::Console),
            [
                "/nologo",
                "a.obj",
                "b.obj",
                "/Fe:app.exe",
                "/link",
                "/SUBSYSTEM:CONSOLE",
            ]
        );
        assert_eq!(
            args(CompilerKind::ClangCl, &b, &p, Subsystem::Console),
            [
                "/nologo",
                "-fuse-ld=lld",
                "a.obj",
                "b.obj",
                "/Fe:app.exe",
                "/link",
                "/SUBSYSTEM:CONSOLE",
            ]
        );
        let static_runtime = BuildSettings {
            runtime: Runtime::Static,
            ..build()
        };
        for b in [&b, &static_runtime] {
            assert_eq!(
                args(CompilerKind::Clang, b, &p, Subsystem::Console),
                [
                    "--target=x86_64-pc-windows-msvc",
                    "-fuse-ld=lld",
                    "a.obj",
                    "b.obj",
                    "-o",
                    "app.exe",
                    "-Wl,/subsystem:console",
                ]
            );
        }
        // GCC links its runtime and winpthread statically under either
        // runtime; only C++ takes libstdc++.
        for b in [&b, &static_runtime] {
            assert_eq!(
                args_for(CompilerKind::Gcc, b, &p, Subsystem::Console, true),
                [
                    "a.obj",
                    "b.obj",
                    "-o",
                    "app.exe",
                    "-mconsole",
                    "-static-libgcc",
                    "-static-libstdc++",
                    "-Wl,-Bstatic",
                    "-lstdc++",
                    "-lwinpthread",
                    "-Wl,-Bdynamic"
                ]
            );
            assert_eq!(
                args_for(CompilerKind::Gcc, b, &p, Subsystem::Console, false),
                [
                    "a.obj",
                    "b.obj",
                    "-o",
                    "app.exe",
                    "-mconsole",
                    "-static-libgcc",
                    "-Wl,-Bstatic",
                    "-lwinpthread",
                    "-Wl,-Bdynamic"
                ]
            );
        }
    }

    /// A DLL links with each driver's switch for one, its import library
    /// named to the linker and, with debug information, its PDB beside it;
    /// no subsystem, and no `/NOIMPLIB`. Paths go to the GNU-style drivers
    /// with `-Xlinker`.
    ///
    /// Mutation: drop `/LD` or `-shared`, the import library or the PDB,
    /// pass a path with `-Wl,`, or keep the subsystem; this test fails.
    #[test]
    fn a_dll_links_with_its_import_library() {
        let b = build();
        let debug = ProfileSettings {
            debug_info: true,
            ..profile()
        };
        let shared = Linked::Shared {
            import: Path::new("lib/m.lib"),
            pdb: Path::new("lib/m.pdb"),
        };
        let cases: [(CompilerKind, &[&str]); 4] = [
            (
                CompilerKind::Msvc,
                &[
                    "/LD",
                    "/nologo",
                    "a.obj",
                    "b.obj",
                    "/Fe:app.exe",
                    "/link",
                    "/IMPLIB:lib/m.lib",
                    "/PDB:lib/m.pdb",
                    "/DEBUG",
                    "/INCREMENTAL:NO",
                ],
            ),
            (
                CompilerKind::ClangCl,
                &[
                    "/LD",
                    "/nologo",
                    "-fuse-ld=lld",
                    "a.obj",
                    "b.obj",
                    "/Fe:app.exe",
                    "/link",
                    "/IMPLIB:lib/m.lib",
                    "/PDB:lib/m.pdb",
                    "/DEBUG",
                ],
            ),
            (
                CompilerKind::Clang,
                &[
                    "-shared",
                    TARGET_SWITCH,
                    "-fuse-ld=lld",
                    "-g",
                    "a.obj",
                    "b.obj",
                    "-o",
                    "app.exe",
                    "-Xlinker",
                    "/implib:lib/m.lib",
                    "-Xlinker",
                    "/pdb:lib/m.pdb",
                ],
            ),
            (
                CompilerKind::Gcc,
                &[
                    "-shared",
                    "-g",
                    "a.obj",
                    "b.obj",
                    "-o",
                    "app.exe",
                    "-Xlinker",
                    "--out-implib",
                    "-Xlinker",
                    "lib/m.lib",
                    "-static-libgcc",
                    "-Wl,-Bstatic",
                    "-lwinpthread",
                    "-Wl,-Bdynamic",
                ],
            ),
        ];
        for (kind, want) in cases {
            assert_eq!(
                args_linking(kind, &b, &debug, shared, false),
                want,
                "{kind:?}"
            );
        }
        // Without debug information, no PDB is named.
        for kind in CompilerKind::ALL {
            let plain = args_linking(kind, &b, &profile(), shared, false);
            assert!(
                !plain
                    .iter()
                    .any(|a| a.to_ascii_lowercase().contains("/pdb")),
                "{kind:?}: {plain:?}"
            );
        }
    }

    /// An executable of a shared library's package is told to write no
    /// import library (link.exe no `.exp` either), right after its
    /// subsystem; any other executable is not, so a `/IMPLIB` in
    /// `link-flags` still makes one.
    ///
    /// Mutation: tell every executable, or none; this test fails.
    #[test]
    fn only_a_shared_packages_executable_writes_no_import_library() {
        let (b, p) = (build(), profile());
        let of_shared = Linked::Executable {
            subsystem: Subsystem::Console,
            of_shared: true,
        };
        let tail = |kind| {
            let args = args_linking(kind, &b, &p, of_shared, false);
            args[args.len() - 2..].to_vec()
        };
        assert_eq!(tail(CompilerKind::Msvc), ["/NOIMPLIB", "/NOEXP"]);
        assert_eq!(
            tail(CompilerKind::ClangCl),
            ["/SUBSYSTEM:CONSOLE", "/NOIMPLIB"]
        );
        assert_eq!(
            tail(CompilerKind::Clang),
            ["-Wl,/subsystem:console", "-Wl,/noimplib"]
        );
        let gcc = args_linking(CompilerKind::Gcc, &b, &p, of_shared, false);
        assert!(!gcc.iter().any(|a| a.contains("implib")), "{gcc:?}");
        for kind in CompilerKind::ALL {
            let plain = args(kind, &b, &p, Subsystem::Console);
            assert!(
                !plain
                    .iter()
                    .any(|a| a.to_ascii_lowercase().contains("noimplib")),
                "{kind:?}: {plain:?}"
            );
        }
    }

    /// Mutation: change or drop any row's link arguments for one driver
    /// (`docs/en/specs/build.md`, "Settings and flags"), reorder several
    /// libraries, paths or flags, split a flag at its comma, or put a linker
    /// argument before `/link`; this test fails.
    #[test]
    fn every_setting_gives_the_tables_link_arguments() {
        let full_build = BuildSettings {
            links: vec!["user32".into(), "gdi32".into()],
            lib_dirs: vec!["libs".into(), "more".into()],
            link_flags: vec!["/OPT:REF,ICF".into(), "/STACK:2097152".into()],
            ..build()
        };
        let full_profile = ProfileSettings {
            opt_level: OptLevel::O3,
            debug_info: true,
            lto: true,
            ..profile()
        };
        let w = Subsystem::Windows;
        assert_eq!(
            args(CompilerKind::Msvc, &full_build, &full_profile, w),
            [
                "/nologo",
                "a.obj",
                "b.obj",
                "/Fe:app.exe",
                "/link",
                "user32.lib",
                "gdi32.lib",
                "/LIBPATH:libs",
                "/LIBPATH:more",
                "/SUBSYSTEM:WINDOWS",
                "/ENTRY:mainCRTStartup",
                "/DEBUG",
                "/INCREMENTAL:NO",
                "/LTCG",
                "/OPT:REF,ICF",
                "/STACK:2097152"
            ]
        );
        assert_eq!(
            args(CompilerKind::ClangCl, &full_build, &full_profile, w),
            [
                "/nologo",
                "-fuse-ld=lld",
                "-flto",
                "a.obj",
                "b.obj",
                "/Fe:app.exe",
                "/link",
                "user32.lib",
                "gdi32.lib",
                "/LIBPATH:libs",
                "/LIBPATH:more",
                "/SUBSYSTEM:WINDOWS",
                "/ENTRY:mainCRTStartup",
                "/DEBUG",
                "/opt:lldlto=3",
                "/OPT:REF,ICF",
                "/STACK:2097152"
            ]
        );
        assert_eq!(
            args(CompilerKind::Clang, &full_build, &full_profile, w),
            [
                "--target=x86_64-pc-windows-msvc",
                "-fuse-ld=lld",
                "-g",
                "-flto",
                "a.obj",
                "b.obj",
                "-o",
                "app.exe",
                "-luser32",
                "-lgdi32",
                "-Llibs",
                "-Lmore",
                "-Wl,/subsystem:windows",
                "-Wl,/entry:mainCRTStartup",
                "-Wl,/opt:lldlto=3",
                "-Xlinker",
                "/OPT:REF,ICF",
                "-Xlinker",
                "/STACK:2097152"
            ]
        );
        assert_eq!(
            args(CompilerKind::Gcc, &full_build, &full_profile, w),
            [
                "-g",
                "-flto=auto",
                "a.obj",
                "b.obj",
                "-o",
                "app.exe",
                "-luser32",
                "-lgdi32",
                "-Llibs",
                "-Lmore",
                "-mwindows",
                "-static-libgcc",
                "-static-libstdc++",
                "-Wl,-Bstatic",
                "-lstdc++",
                "-lwinpthread",
                "-Wl,-Bdynamic",
                "-Xlinker",
                "/OPT:REF,ICF",
                "-Xlinker",
                "/STACK:2097152"
            ]
        );
    }

    /// Mutation: map any `opt-level` to another LTO level; this test fails.
    #[test]
    fn lto_optimises_at_the_profiles_level() {
        for (level, n) in [
            (OptLevel::O0, "0"),
            (OptLevel::O1, "1"),
            (OptLevel::O2, "2"),
            (OptLevel::O3, "3"),
            (OptLevel::Size, "2"),
        ] {
            let p = ProfileSettings {
                opt_level: level,
                lto: true,
                ..profile()
            };
            let clang_cl = args(CompilerKind::ClangCl, &build(), &p, Subsystem::Console);
            assert!(
                clang_cl.contains(&format!("/opt:lldlto={n}")),
                "{level:?} {clang_cl:?}"
            );
            let clang = args(CompilerKind::Clang, &build(), &p, Subsystem::Console);
            assert!(
                clang.contains(&format!("-Wl,/opt:lldlto={n}")),
                "{level:?} {clang:?}"
            );
            let msvc = args(CompilerKind::Msvc, &build(), &p, Subsystem::Console);
            assert!(!msvc.iter().any(|a| a.contains("lldlto")), "{msvc:?}");
            let gcc = args(CompilerKind::Gcc, &build(), &p, Subsystem::Console);
            assert!(!gcc.iter().any(|a| a.contains("lldlto")), "{gcc:?}");
        }
    }

    /// GCC 14 and later link `libstdc++exp` for C++, right before
    /// `libstdc++` and statically; GCC 13, which has none, and C do not.
    ///
    /// Mutation: give it whatever the version, after `-lstdc++`, or to C;
    /// this test fails.
    #[test]
    fn gcc_14_links_libstdcxx_exp_for_cxx() {
        let v = |major| Version {
            major,
            minor: 1,
            patch: 0,
        };
        assert_eq!(
            gcc_runtime_args(true, v(14)),
            [
                "-static-libgcc",
                "-static-libstdc++",
                "-Wl,-Bstatic",
                "-lstdc++exp",
                "-lstdc++",
                "-lwinpthread",
                "-Wl,-Bdynamic",
            ]
        );
        assert!(!gcc_runtime_args(true, v(13)).contains(&"-lstdc++exp"));
        assert!(!gcc_runtime_args(false, v(16)).contains(&"-lstdc++exp"));
    }
}