Skip to main content

rucc_driver/
link.rs

1//! Finding a linker and telling it what to link.
2//!
3//! Design: `spec/04-driver-and-cli.md` section 4.9. There is no linker of our own before 1.0, so
4//! this finds one on the machine and builds the command line it wants.
5//!
6//! The linker is invoked directly rather than through the system compiler driver. Going through
7//! `cc` would be shorter to write and would borrow that compiler's idea of where everything is,
8//! and it would also mean this compiler cannot link on a machine that has no other compiler on
9//! it, which is most of the machines a compiler ends up on. It would also make `-###` output a
10//! line that does not say what happens, since the interesting half would be inside the program
11//! being spawned.
12//!
13//! # What is not decided here
14//!
15//! The startup files and the library directories are looked for rather than configured, for the
16//! same reason `library` looks for the headers: gcc settles this when it is built because a gcc
17//! is built for the machine it will run on, and this is one binary that runs wherever it is
18//! copied. So the shape of the answer is a list of candidates per platform of which the ones
19//! that exist are taken, and a cross build says where the rest is with `--sysroot`.
20//!
21//! # The compiler's own runtime
22//!
23//! `crtbegin`, `crtend` and the runtime libraries are found the same way, on the machine rather
24//! than by configuration. Ours is `librucc_builtins.a`, looked for beside the compiler, and the
25//! machine's `libgcc` goes on after it for the parts we have not written, which today is the
26//! unwinder and its personality routine. The C library goes in front of both, so that on a target
27//! that has one its `memcpy` is the one that answers rather than ours. `-fno-builtins-lib` leaves
28//! ours off, for somebody who wants libgcc to answer for everything.
29//!
30//! On a static link the three archives go inside `--start-group`, because `libc.a` refers to the
31//! unwinder and the unwinder refers back to `libc.a`, and a linker walking a list once resolves
32//! whichever of the two it reaches first and leaves the other undefined. That circularity is the
33//! whole reason `-static` failed before this, and it is issue #277.
34//!
35//! # Linking for a machine that is not this one
36//!
37//! Everything above describes a link against the machine running the compiler, and it is what runs
38//! when the target is that machine. A target that is not is a different problem: there is no
39//! `crt1.o` for it in `/usr/lib`, the `libc.so` there is the wrong architecture, and a line built
40//! out of what is lying around either fails at the first input or, worse, links. So a cross link
41//! does not look at this machine at all. It is built by [`rucc_sysroot::argv`] out of the target
42//! and a sysroot under the cache directory, and `spec/cross-compile/11-linking.md` section 11.3 is
43//! the design. [`cross_sysroot`] is the one place that decides which of the two it is.
44//!
45//! Two conditions keep that out of the way of everything that works today. The target has to differ
46//! from the host, and `--sysroot` must not have been given: somebody who assembled a tree and named
47//! it is asking for the line above with their own root in front of every path, which is what a
48//! cross compile with a real distribution tree in it has always been.
49//!
50//! That second condition is also the escape hatch for a machine which has a distribution's own cross
51//! files installed, where `/usr/lib/aarch64-linux-gnu` really does hold an AArch64 `crt1.o`.
52//! `--sysroot=/` takes the line above, and then every directory it decides is that machine's again.
53//!
54//! # What is not here yet
55//!
56//! Windows in Microsoft's ABI. `lld-link` wants a `/`-style command line and an import library set
57//! out of an SDK nobody may redistribute, and a link to it is refused by name rather than
58//! approximated. A mingw-w64 target does have a line, because PE in that environment is written in
59//! the GNU style and the import libraries for it are ours to produce.
60//!
61//! Darwin has a line of its own, [`darwin_line`], and it is the same line on a Mac and anywhere
62//! else. Everything it links against is in the SDK, which is found the way the header search finds
63//! it, so a machine that is not a Mac links for one exactly when somebody has named an SDK with
64//! `-isysroot` or `SDKROOT` and there is an `ld64.lld` to hand it to.
65//!
66//! The headers are the other half of a cross compile and [`crate::library::header_dirs`] is where
67//! they are decided. It asks [`cross_sysroot`] the same question this file asks it, which is the
68//! point: a compile that took its libc from the sysroot and its declarations from this machine would
69//! be wrong in the quietest way available, and one function answering for both is what stops that
70//! being possible.
71
72use std::ffi::OsString;
73use std::fs;
74use std::path::{Path, PathBuf};
75use std::process::Command;
76
77use rucc_sysroot::layout::{Kernel, Sysroot};
78use rucc_sysroot::{LinkMode, argv};
79use rucc_target::{Arch, Env, Os, Triple};
80use rucc_tuple::TargetTuple;
81
82/// What the command line said about linking.
83///
84/// Kept apart from `Options` because none of it reaches the compilation. A flag here changes what
85/// the linker is told and changes nothing about the object files handed to it, which is why `-lm`
86/// on a `-c` line is a note rather than an error.
87#[derive(Debug, Default, Clone, PartialEq, Eq)]
88pub struct LinkOptions {
89    /// `-fuse-ld=<name>`, which names a linker rather than a path to one.
90    pub use_ld: Option<String>,
91    /// `-L<dir>`, in order, because the linker takes the first library it finds.
92    pub search: Vec<PathBuf>,
93    /// `-B<prefix>`, which is where to look for the linker before looking on the path.
94    pub prefixes: Vec<PathBuf>,
95    /// `--sysroot=<dir>`, which prefixes the directories this looks in.
96    pub sysroot: Option<PathBuf>,
97    /// Where the generated sysroots are, which is [`crate::cache::dir`] on a real command line.
98    ///
99    /// [`None`] is a caller that was not given one, which outside a test is nothing, and then there
100    /// is no cross link line and a foreign target is refused the way it was before there was one.
101    /// It is a field rather than a call inside this module because a link line that read the
102    /// environment could only be tested on a machine whose environment said the right thing.
103    pub cache: Option<PathBuf>,
104    /// Where a distribution's cross packages put the tree for another architecture, which is
105    /// `/usr` on a real command line.
106    ///
107    /// Debian and Ubuntu install `libc6-dev-arm64-cross` and its friends as `/usr/<multiarch>/include`
108    /// and `/usr/<multiarch>/lib`, and gcc's own files for that target under
109    /// `/usr/lib/gcc-cross/<multiarch>`. [`distro_cross`] reads it. A field for the reason
110    /// [`LinkOptions::cache`] is one, and [`None`] in a test is a machine with no such packages.
111    pub usr: Option<PathBuf>,
112    /// `-static`.
113    pub is_static: bool,
114    /// `-shared`.
115    pub shared: bool,
116    /// `-pie` or `-no-pie`, and the platform's default when neither was written.
117    pub pie: Option<bool>,
118    /// `-nostdlib`, which is `-nostartfiles` and `-nodefaultlibs` together.
119    pub no_stdlib: bool,
120    /// `-nostartfiles`.
121    pub no_startfiles: bool,
122    /// `-nodefaultlibs`.
123    pub no_defaultlibs: bool,
124    /// `-rdynamic`, which puts every symbol in the dynamic table so a program can look itself up.
125    pub export_dynamic: bool,
126    /// `-s`, which drops the symbol table.
127    pub strip: bool,
128    /// `-mwindows` rather than `-mconsole`, whichever came last. Only a Windows line reads it.
129    pub gui: bool,
130    /// `-municode`, which only a Windows line reads.
131    pub unicode: bool,
132    /// `-fno-builtins-lib`, which leaves our own runtime off the line so that the machine's
133    /// libgcc answers for everything instead.
134    pub no_builtins_lib: bool,
135    /// The whole ten field target when `--target=` spelled one, which is where a pinned libc
136    /// release is.
137    ///
138    /// [`None`] is a command line that named no target at all, and then there is nothing pinned and
139    /// this machine is the target. A `Triple` has room for an architecture, an OS and an
140    /// environment and nowhere to put a release, so the release arrives here instead of there, and
141    /// [`cross_sysroot`] reads it for both of the things it decides: whether this is a cross link
142    /// and which directory under the cache it is against.
143    pub pinned: Option<TargetTuple>,
144    /// `-pg`, which changes the link as well as the code.
145    ///
146    /// The counts a profiled program keeps have to be started before `main` runs and written out
147    /// after it returns, and what does both is a start file of its own. So a build that compiles
148    /// with the flag and links without it produces a program that calls the hook on every function
149    /// and never writes a profile.
150    pub profile: bool,
151    /// Whether `-Ofast`, `-ffast-math` or `-funsafe-math-optimizations` was in force at the end
152    /// of the command line, which links `crtfastmath.o` into anything that is not a shared object.
153    ///
154    /// That file is a constructor which sets flush to zero and denormals are zero before `main`,
155    /// so the mode is the process's rather than the unit's, and it is the half of fast math the
156    /// compiler cannot give from inside a function.
157    pub fast_math: bool,
158    /// `-mdaz-ftz` or `-mno-daz-ftz`, which decides the same file outright and for a shared
159    /// object as well.
160    pub daz_ftz: Option<bool>,
161    /// `-r`, which joins objects into one bigger object rather than into something that runs.
162    ///
163    /// Kbuild builds every directory into a `built-in.o` this way. The result is still an input to a
164    /// later link, so it takes no startup files, no libraries and nothing about how it will be
165    /// loaded, which is what gcc leaves off the line when it sees the flag.
166    pub relocatable: bool,
167    /// The deployment target on an Apple platform, from `-mmacosx-version-min=` and its friends or
168    /// from the tuple, which `ld64` is told in `-platform_version` and checks every object against.
169    ///
170    /// [`None`] is the platform's default, the same one the object writer puts in
171    /// `LC_BUILD_VERSION`, so that the two agree when neither was told anything.
172    pub os_version: Option<rucc_tuple::Version>,
173}
174
175impl LinkOptions {
176    /// Whether gcc's `crtfastmath.o` goes on the line, which is its end file spec on x86-64.
177    fn wants_fastmath(&self) -> bool {
178        self.daz_ftz.unwrap_or(self.fast_math && !self.shared)
179    }
180
181    /// Whether the startup files go on the line.
182    fn wants_startfiles(&self) -> bool {
183        !self.no_stdlib && !self.no_startfiles && !self.relocatable
184    }
185
186    /// Whether the library the program was written against goes on the line.
187    fn wants_defaultlibs(&self) -> bool {
188        !self.no_stdlib && !self.no_defaultlibs && !self.relocatable
189    }
190
191    /// Whether the compiler's own runtime goes on the line.
192    ///
193    /// The same switch as the C library, because `-nodefaultlibs` in GCC means the compiler's
194    /// runtime too, and a link that keeps `libgcc` while dropping `libc` is not a thing anyone
195    /// asks for on purpose.
196    fn wants_runtime(&self) -> bool {
197        !self.no_stdlib && !self.no_defaultlibs && !self.relocatable
198    }
199}
200
201/// One item on the link line, in the order it was written, because link order is semantic.
202///
203/// A library named before the object that needs it is not found on a static link, which is the
204/// oldest surprise in the toolchain and the reason this is one ordered list rather than a list of
205/// files and a list of libraries.
206#[derive(Debug, Clone, PartialEq, Eq)]
207pub enum Item {
208    /// A file: an object this compilation produced, or one named on the command line.
209    File(String),
210    /// `-l<name>`, which the linker resolves against its search path.
211    Library(String),
212    /// One word from `-Wl,` or `-Xlinker`, handed to the linker where the user wrote it.
213    ///
214    /// Here rather than in a list of its own because a great many of the linker's options are a
215    /// bracket around the files after them, and an option moved away from what it brackets means
216    /// something else or nothing at all. `--whole-archive` says that every member of every archive
217    /// named after it goes in whether anything referenced it or not, `--start-group` says that the
218    /// archives after it are searched again until nothing more comes out, and `-Bstatic` says which
219    /// half of a library that ships both is wanted. Collecting them and appending them to the end
220    /// leaves each of those pointing at nothing.
221    Linker(String),
222}
223
224impl std::fmt::Display for Item {
225    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
226        match self {
227            Item::File(path) => f.write_str(path),
228            Item::Library(name) => write!(f, "-l{name}"),
229            Item::Linker(arg) => write!(f, "-Wl,{arg}"),
230        }
231    }
232}
233
234/// Why a link could not be run.
235#[derive(Debug, Clone, PartialEq, Eq)]
236pub enum Error {
237    /// No linker was found, after looking everywhere there was to look.
238    NoLinker {
239        /// The names that were tried, in the order they were tried.
240        tried: Vec<String>,
241    },
242    /// `-fuse-ld=` named one that is not on this machine.
243    Named {
244        /// What it named.
245        name: String,
246    },
247    /// A target this does not know how to build a link line for.
248    Target {
249        /// The triple that was asked for.
250        triple: String,
251    },
252    /// A cross link this scheme cannot produce, which [`rucc_sysroot::argv`] has explained.
253    ///
254    /// The reason is carried as a sentence rather than as a variant per cause, because the causes
255    /// live in `rucc-sysroot` and a second enumeration here would be a second thing to keep in step
256    /// with them. What this adds is that the sentence came from a link rather than from a
257    /// compilation.
258    Cross {
259        /// Why, in full, ready to print.
260        why: String,
261    },
262    /// The sysroot a cross link needs is not on this machine.
263    Sysroot {
264        /// The target that was asked for.
265        target: String,
266        /// Where its sysroot would be.
267        dir: String,
268        /// Whether this release pins an artifact for that target, which decides whether the message
269        /// can name a command that would fix it.
270        pinned: bool,
271    },
272    /// The linker was found and cannot do this target's link.
273    ///
274    /// Separate from [`Error::NoLinker`] because the linker is there and runs, and separate from
275    /// [`Error::Refused`] because the refusal is ours rather than its own: this is the case the
276    /// linker would not complain about at all.
277    TooOld {
278        /// What it was found as, which is what to look for when replacing it.
279        name: String,
280        /// The major version it reported.
281        found: u32,
282        /// The target whose link it cannot do.
283        target: String,
284    },
285    /// The linker was found and could not be started.
286    Spawn {
287        /// Where it was.
288        path: String,
289        /// What the operating system said.
290        why: String,
291    },
292    /// The linker ran and said no.
293    Refused {
294        /// What it exited with, or a description when it was killed instead.
295        status: String,
296    },
297}
298
299impl std::fmt::Display for Error {
300    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
301        match self {
302            Error::NoLinker { tried } => {
303                write!(f, "no linker was found; tried {}", tried.join(", "))?;
304                // Only when lld was one of the names, because that is the linker every cross
305                // target here is linked with and the one there is a single answer for.
306                if tried.iter().any(|name| is_lld(name)) {
307                    write!(
308                        f,
309                        ". lld {LLD_EXPORTAS} or newer links for every target, and {}",
310                        lld_advice()
311                    )?;
312                }
313                Ok(())
314            }
315            Error::Named { name } => {
316                write!(f, "-fuse-ld={name} asks for a linker that is not on this machine")
317            }
318            Error::Target { triple } => {
319                write!(f, "there is no link line for {triple} in this compiler yet")
320            }
321            Error::Cross { why } => f.write_str(why),
322            // Two sentences and the second one changes, because a person whose link just failed
323            // wants the command that fixes it and there is only a command to name when this release
324            // pins an artifact for that target. Section 13.8's rule is that a compile which is
325            // missing a sysroot says what to run rather than running it, and this is where it says
326            // it.
327            Error::Sysroot { target, dir, pinned: true } => write!(
328                f,
329                "there is no sysroot for {target} at {dir}, so there is nothing to link it \
330                 against. `rucc --fetch {target}` gets the one this release pins, or pass \
331                 --sysroot=<dir> to name a tree you have already"
332            ),
333            Error::Sysroot { target, dir, pinned: false } => write!(
334                f,
335                "there is no sysroot for {target} at {dir}, so there is nothing to link it \
336                 against, and this release pins none for it to fetch. Pass --sysroot=<dir> to name \
337                 a tree you have already, or see spec/cross-compile/13-distribution.md section \
338                 13.2 for the cache that will hold one"
339            ),
340            // The whole message, because the person reading it has a linker that works, a link that
341            // succeeded on their last try, and no reason to suspect the thing that is wrong.
342            Error::TooOld { name, found, target } => write!(
343                f,
344                "{name} is lld {found} and cannot link for {target}. mingw-w64 writes a few hundred \
345                 of its aliases, `_crt_atexit == atexit` among them, as IMPORT_NAME_EXPORTAS \
346                 records in its import libraries, which lld learned to read in {LLD_EXPORTAS}. An \
347                 older one neither reads them nor says so: it writes an import by ordinal zero, the \
348                 link succeeds, and the program dies at startup. No newer lld was found either. \
349                 For lld {LLD_EXPORTAS} or newer, {}, or name one with -fuse-ld=",
350                lld_advice()
351            ),
352            Error::Spawn { path, why } => write!(f, "could not run the linker at {path}: {why}"),
353            Error::Refused { status } => write!(f, "the linker {status}"),
354        }
355    }
356}
357
358impl std::error::Error for Error {}
359
360/// A linker, found.
361#[derive(Debug, Clone, PartialEq, Eq)]
362pub struct Linker {
363    /// The name it is known by, which is what `--print-config` reports.
364    pub name: String,
365    /// Where it is, which is what gets spawned.
366    pub path: PathBuf,
367}
368
369/// The names to look for, in the order section 4.9 gives.
370///
371/// `mold` first because it is dramatically faster, and a compiler that is twice the speed of
372/// another one while the link takes twelve seconds has not helped anybody. Then `lld`, then the
373/// platform's own. Each is looked for under both the bare name and the `ld.` prefix, because a
374/// distribution installs `mold` under its own name and `ld.mold` for exactly this lookup.
375#[must_use]
376pub fn order(target: Triple, opts: &LinkOptions) -> Vec<String> {
377    if let Some(named) = &opts.use_ld {
378        // A name rather than a path, so `-fuse-ld=mold` finds a `mold` that is not `ld.mold`.
379        return vec![format!("ld.{named}"), named.clone()];
380    }
381    // Apple's `ld` and lld's Mach-O flavour, and nothing from the list below. mold and `ld.lld` write
382    // ELF, and a Mac with Homebrew's llvm on its `PATH` has an `ld.lld` that would take the line and
383    // fail on its first flag.
384    if target.os == Os::Darwin {
385        return vec!["ld".to_owned(), "ld64.lld".to_owned()];
386    }
387    if cross_sysroot(target, opts).is_some() {
388        let mut names = cross_order(target);
389        // The mingw-w64 sysroot this release fetches has import libraries that GNU ld cannot link
390        // against: the `==` aliases are IMPORT_NAME_EXPORTAS records, and binutils reports every
391        // one of them as an undefined reference to a symbol like `__imp__fmode`. So on this path
392        // lld is the only linker worth finding, and a machine without one is told how to get it
393        // instead of being handed a page of undefined references.
394        if (target.os, target.env) == (Os::Windows, Env::Gnu) {
395            names.retain(|name| is_lld(name));
396        }
397        return names;
398    }
399    if distro_cross(target, opts).is_some() {
400        return cross_order(target);
401    }
402    match target.os {
403        Os::Windows => vec!["lld-link".to_owned(), "link.exe".to_owned()],
404        _ => vec![
405            "ld.mold".to_owned(),
406            "mold".to_owned(),
407            "ld.lld".to_owned(),
408            "lld".to_owned(),
409            "ld".to_owned(),
410        ],
411    }
412}
413
414/// The names to look for when the target is not this machine.
415///
416/// A shorter list than the one above and a different one, because most of that list cannot do this.
417/// `spec/cross-compile/11-linking.md` section 11.2 settles it: `ld.lld` is the ELF cross linker,
418/// since one binary of it links for every architecture it was built with and that is all of them.
419/// mold is off the list because it links for the host and `wild` likewise, which is why section 11.2
420/// has them as `-fuse-ld=` choices for a native link rather than as defaults. The platform's own
421/// `ld` is off it for the same reason: a distribution's `/usr/bin/ld` is built for one architecture,
422/// and `-fuse-ld=` is still there for somebody whose is not.
423///
424/// A cross binutils under its prefixed name is last, because a machine that has
425/// `aarch64-linux-gnu-ld` installed has it on purpose. The prefix is a distribution convention and
426/// there are two of them: a Linux target is filed under its multiarch name and a mingw-w64 one under
427/// `<arch>-w64-mingw32`, which is what every distribution's mingw packages install. `ld.lld` is the
428/// same binary for both, because its MinGW mode is a mode of the one linker rather than a second one.
429fn cross_order(target: Triple) -> Vec<String> {
430    let mut names = vec!["ld.lld".to_owned(), "lld".to_owned()];
431    match (target.os, target.env) {
432        (Os::Linux, _) => names.push(format!("{}-ld", multiarch(target))),
433        (Os::Windows, Env::Gnu) => names.push(format!("{}-w64-mingw32-ld", target.arch.as_str())),
434        _ => {}
435    }
436    names
437}
438
439/// The sysroot a cross link would use, or [`None`] for a link against this machine.
440///
441/// The one place the two paths are told apart, so that the linker that is looked for and the line it
442/// is handed cannot disagree about which kind of link this is.
443///
444/// Three conditions, and two of them are about leaving working configurations alone. A target that
445/// is this machine is linked against this machine, which is what every native compile has always
446/// done and what the directories under `/usr/lib` are for. A `--sysroot` the user wrote is taken as
447/// the root of a tree they assembled, and the line above prefixes every path it decides with it,
448/// which is what cross compiling against a real distribution tree has always meant here. The third
449/// is that there has to be a cache directory to look in, which on a real command line there always
450/// is.
451///
452/// An unknown host counts as different from every target. A machine this compiler cannot name is a
453/// machine whose `/usr/lib` it should not be guessing at.
454///
455/// # A pinned release is a cross compile
456///
457/// The first of those three conditions is about the machine and not about the triple, and a target
458/// that names a libc release is not this machine even when it is this architecture. Somebody on a
459/// 2.44 box writing `--target=x86_64-linux-gnu.2.28` is asking for a binary that runs on a 2.28
460/// machine, and handing them their own headers and their own libc gives them a binary that does not.
461/// So the condition is the triple being the host *and* no release named, and what it costs is that a
462/// pin equal to this machine's own release also stops using this machine's libc. That is not a loss:
463/// the two should be the same text, and if they are not then this machine's copy is patched and the
464/// bundled tree is the one the pin asked for. tamnd/rucc#956.
465#[must_use]
466pub fn cross_sysroot(target: Triple, opts: &LinkOptions) -> Option<Sysroot> {
467    cross_for(target, opts, Triple::host())
468}
469
470/// The same answer with the host as a parameter, so that both branches are testable on one machine.
471fn cross_for(target: Triple, opts: &LinkOptions, host: Option<Triple>) -> Option<Sysroot> {
472    if opts.sysroot.is_some() {
473        return None;
474    }
475    let tuple = target_tuple(target, opts);
476    // Windows is left out because a Windows machine has no C library of its own to compile
477    // against. On an x86-64 Windows box the default target is the host, and the only headers
478    // it can have are the mingw-w64 tree `--fetch` put in the cache.
479    if host == Some(target) && target.os != Os::Windows && tuple.env_version().is_none() {
480        return None;
481    }
482    if distro_for(target, opts, host).is_some() {
483        return None;
484    }
485    let cache = opts.cache.as_deref()?;
486    Some(Sysroot::in_cache(cache, tuple))
487}
488
489/// A tree a distribution's cross packages installed for a Linux target that is not this machine.
490///
491/// What `apt install gcc-aarch64-linux-gnu` leaves behind: the C library's headers and files under
492/// `/usr/aarch64-linux-gnu`, and gcc's `crtbegin.o` and `libgcc.a` for that target under
493/// `/usr/lib/gcc-cross/aarch64-linux-gnu/<version>`. The library's `libc.so` script names its files
494/// by their full path, so the tree is linked where it is and not under a `--sysroot`.
495#[derive(Debug, Clone, PartialEq, Eq)]
496pub struct Distro {
497    /// `/usr/<multiarch>`, which has `include` and `lib` under it.
498    pub root: PathBuf,
499    /// gcc's directories for the target, newest version first, and empty when only the library
500    /// was installed.
501    pub gcc: Vec<PathBuf>,
502}
503
504impl Distro {
505    /// The C library's headers, and the kernel's, which the same packages put in the same place.
506    #[must_use]
507    pub fn include(&self) -> PathBuf {
508        self.root.join("include")
509    }
510
511    /// The C library's startup files and libraries.
512    #[must_use]
513    pub fn lib(&self) -> PathBuf {
514        self.root.join("lib")
515    }
516}
517
518/// The distribution's tree for this target, when a cross compile should use it.
519///
520/// Only when nothing better was asked for or is there. A `--sysroot` is a tree somebody named, a
521/// pinned release is a request for our tree cut at that release, and a sysroot already fetched into
522/// the cache is the one this release pins, so each of those wins. What is left is a machine that
523/// has the distribution's cross packages and nothing of ours, and there the packages are what a
524/// prefixed gcc on the same machine would use, so they are what this uses too.
525#[must_use]
526pub fn distro_cross(target: Triple, opts: &LinkOptions) -> Option<Distro> {
527    distro_for(target, opts, Triple::host())
528}
529
530/// The same answer with the host as a parameter, for the same reason as [`cross_for`].
531fn distro_for(target: Triple, opts: &LinkOptions, host: Option<Triple>) -> Option<Distro> {
532    if opts.sysroot.is_some() || target.os != Os::Linux || host == Some(target) {
533        return None;
534    }
535    let tuple = target_tuple(target, opts);
536    if tuple.env_version().is_some() {
537        return None;
538    }
539    if opts.cache.as_deref().is_some_and(|cache| Sysroot::in_cache(cache, tuple).lib().is_dir()) {
540        return None;
541    }
542    let usr = opts.usr.as_deref()?;
543    let name = multiarch(target);
544    let root = usr.join(&name);
545    if !root.join("include").is_dir() || !root.join("lib").is_dir() {
546        return None;
547    }
548    let gcc = newest_first(&usr.join("lib/gcc-cross").join(&name));
549    Some(Distro { root, gcc })
550}
551
552/// The target as the model that has room for a release, which is what names the cache directory.
553///
554/// The pinned spelling when there is one, because `x86_64-linux-gnu` and `x86_64-linux-gnu.2.28` are
555/// two sysroots and not one: the release is in the tuple for the reason
556/// `spec/cross-compile/03-target-model.md` section 3.2 admits a field at all, which is that it
557/// changes what is compiled. A command line that named no target, or one whose spelling the ten
558/// field parser did not take, falls back to what the three field one did.
559fn target_tuple(target: Triple, opts: &LinkOptions) -> TargetTuple {
560    opts.pinned.unwrap_or_else(|| target.tuple())
561}
562
563/// The kernel headers that go with [`cross_sysroot`], for the targets that have any.
564///
565/// The same three conditions, asked through the same function, because the two halves of one
566/// target's system headers have to be decided together or a compile could read glibc's `sys/stat.h`
567/// against this machine's `asm/stat.h`. A `None` here on a Linux target where the sysroot is `Some`
568/// means only one thing, which is that the cache has no kernel tree for that architecture, and the
569/// directory is still named for the reason [`crate::library::header_dirs`] gives.
570///
571/// Not under the sysroot, because `linux/` and `asm-generic/` are the same nine megabytes for every
572/// target that shares an architecture, and a copy per target is eight copies of one thing.
573#[must_use]
574pub fn cross_kernel(target: Triple, opts: &LinkOptions) -> Option<Kernel> {
575    kernel_for(target, opts, Triple::host())
576}
577
578/// The same answer with the host as a parameter, for the same reason as [`cross_for`].
579fn kernel_for(target: Triple, opts: &LinkOptions, host: Option<Triple>) -> Option<Kernel> {
580    cross_for(target, opts, host)?;
581    Kernel::for_target(opts.cache.as_deref()?, target.tuple())
582}
583
584/// How the result is linked, as the five cases a sysroot link line is written over.
585///
586/// Four booleans reach here and five cases leave, because static and position independent are not
587/// independent of each other and the start file differs in four of the five. The default for `pie`
588/// is the one the native line above uses, so that a command line that says neither gets the same
589/// answer whichever path it takes.
590fn mode(opts: &LinkOptions) -> LinkMode {
591    let pie = opts.pie.unwrap_or(!opts.is_static && !opts.shared);
592    if opts.shared {
593        LinkMode::Shared
594    } else if opts.is_static {
595        if pie { LinkMode::StaticPie } else { LinkMode::Static }
596    } else if pie {
597        LinkMode::Dynamic
598    } else {
599        LinkMode::DynamicNoPie
600    }
601}
602
603/// The line for a machine that is not this one, from the target and the sysroot and nothing else.
604///
605/// Everything this knows is already in `opts`, and all it does is say it in the shape
606/// [`rucc_sysroot::argv`] is written over. There is deliberately no decision here: a second place
607/// that decided what goes on a cross link line would be a second place to get it wrong, and the
608/// recorded lines under `tests/link-lines` would stop describing what this compiler does.
609fn cross_line(
610    target: Triple,
611    opts: &LinkOptions,
612    items: &[Item],
613    output: &str,
614    sysroot: &Sysroot,
615) -> Result<Vec<String>, Error> {
616    if opts.profile {
617        // `gcrt1.o` is a compiled object out of the C library's own sources, and a generated sysroot
618        // has the names a libc exports rather than the bodies behind them. Said here rather than
619        // left to the linker, because what the linker would say is that `main` is undefined.
620        return Err(Error::Cross {
621            why: format!(
622                "-pg needs gcrt1.o, or gcrt2.o on Windows, the startup file that starts and stops \
623                 the counting, and a generated sysroot for {target} does not have one. Profile on \
624                 the host, or pass --sysroot=<dir> naming a tree that has it"
625            ),
626        });
627    }
628    let inputs: Vec<argv::Item> = items
629        .iter()
630        .map(|item| match item {
631            Item::File(path) => argv::Item::File(PathBuf::from(path)),
632            Item::Library(name) => argv::Item::Library(name.clone()),
633            Item::Linker(arg) => argv::Item::Linker(arg.clone()),
634        })
635        .collect();
636    let output = PathBuf::from(output);
637    // Ours, from beside the compiler, because that is where `cargo xtask builtins` writes it and a
638    // fetched sysroot will never hold it. The cross line used to name it inside the sysroot, which
639    // is a file nothing puts there, so every cross link either failed at the linker or quietly ran
640    // against somebody else's `libgcc` copied in under the name. tamnd/rucc#1514.
641    let ours = builtins_archive(target, &opts.prefixes);
642    if ours.is_none() && opts.wants_runtime() && !opts.no_builtins_lib {
643        // Said here rather than left to the linker, which on a Windows target says `___chkstk_ms`
644        // is undefined and names mingw-w64's objects as the callers, and on a musl one says
645        // `__udivti3` is. Neither of those is a person's first guess at a missing archive.
646        let tuple = target.tuple().to_canonical_string();
647        return Err(Error::Cross {
648            why: format!(
649                "a cross link ends with librucc_builtins.a, this compiler's own runtime for \
650                 {tuple}, and there is none beside the compiler or under a -B prefix. A sysroot \
651                 does not carry it, because it is our output rather than the platform's. Build it \
652                 with `cargo xtask builtins --target={tuple}`, or pass -fno-builtins-lib to link \
653                 without it"
654            ),
655        });
656    }
657    let invocation = argv::Invocation {
658        inputs: &inputs,
659        output: Some(&output),
660        mode: mode(opts),
661        search: &opts.search,
662        no_startfiles: !opts.wants_startfiles(),
663        no_defaultlibs: !opts.wants_defaultlibs(),
664        no_builtins_lib: opts.no_builtins_lib,
665        builtins: ours.as_deref(),
666        export_dynamic: opts.export_dynamic,
667        strip: opts.strip,
668        gui: opts.gui,
669        unicode: opts.unicode,
670    };
671    argv::argv(target.tuple(), sysroot, &invocation)
672        .map_err(|why| Error::Cross { why: why.to_string() })
673}
674
675/// Whether this link can be run at all, asked before anything is compiled.
676///
677/// Two questions that have answers before the first object exists: whether there is a line for this
678/// target and mode at all, and whether the sysroot it would read is on the machine. Both are worth a
679/// second at the start rather than a message after a minute of compiling, which is the same reason
680/// the linker itself is looked for first.
681///
682/// The line is built rather than inspected, with no inputs and a name nothing will be written to,
683/// because the refusals belong to the one function that builds it. A link against this machine has
684/// nothing to answer here: its directories are looked for as the line is built and a missing one is
685/// simply a directory that is not offered.
686///
687/// # Errors
688///
689/// [`Error::Cross`] for a target or a mode that has no line, and [`Error::Sysroot`] when the sysroot
690/// it would be linked against is not there.
691pub fn preflight(target: Triple, opts: &LinkOptions) -> Result<(), Error> {
692    if opts.relocatable {
693        return relocatable_line(target, opts, &[], "a.out").map(drop);
694    }
695    // Whether there is an SDK, which is the one thing a Darwin link can be missing that is worth
696    // saying before everything has been compiled.
697    if target.os == Os::Darwin {
698        return darwin_line(target, opts, &[], "a.out").map(drop);
699    }
700    let Some(sysroot) = cross_sysroot(target, opts) else { return Ok(()) };
701    // Whether there is a line for this target and mode at all, asked with our own runtime left off
702    // it. Otherwise a target nothing here can link and a machine where nobody built the runtime
703    // report the same thing, and the archive is the smaller of the two problems by a long way.
704    let shape = LinkOptions { no_builtins_lib: true, ..opts.clone() };
705    cross_line(target, &shape, &[], "a.out", &sysroot)?;
706    // The library directory rather than the root, because the root of a cache directory that has
707    // been created and never populated is there and holds nothing. Section 11.6's rule is that
708    // suitable is checked and not assumed, and this is the cheapest form of that.
709    if !sysroot.lib().is_dir() {
710        let tuple = target_tuple(target, opts).to_canonical_string();
711        return Err(Error::Sysroot {
712            dir: sysroot.root().display().to_string(),
713            pinned: rucc_sysroot::pinned_for_target(target_tuple(target, opts)).is_some(),
714            target: tuple,
715        });
716    }
717    // And now the whole line, which is the sysroot's files plus ours, so that a missing runtime is
718    // said here rather than by the linker after everything has been compiled.
719    cross_line(target, opts, &[], "a.out", &sysroot)?;
720    Ok(())
721}
722
723/// Writes the stub libraries a glibc cross link reads, into [`Sysroot::stubs`].
724///
725/// `spec/cross-compile/09-libc-stubs.md` section 9.1: the stubs are generated on demand rather than
726/// shipped, out of the description `rucc-stub` carries, cut at the release the tuple names or at
727/// the bundled one when it names none. So there is nothing to fetch for them and nothing to go
728/// stale, and a pin is a different directory rather than a different download.
729///
730/// A file is written only when its bytes differ from what is there, and then through a temporary
731/// name and a rename, because two builds for one target run side by side all the time and a
732/// linker must never read a half written `libc.so`. The bytes are the same on every host, so two
733/// processes racing to write them race to write the same thing.
734///
735/// Nothing for a link against this machine, a `--sysroot` the user named, or a target that is not
736/// glibc. A glibc release newer than the bundled tree was already refused when the headers were
737/// chosen, so it is quietly nothing here too.
738///
739/// # Errors
740///
741/// [`Error::Cross`] when the stubs cannot be generated for this target or cannot be written.
742pub fn write_stubs(target: Triple, opts: &LinkOptions) -> Result<(), Error> {
743    let Some(sysroot) = cross_sysroot(target, opts) else { return Ok(()) };
744    let tuple = sysroot.target();
745    if rucc_stub::glibc::architecture(tuple).is_none() {
746        return Ok(());
747    }
748    let Ok(Some(minor)) = rucc_sysroot::bundled_glibc_minor(tuple) else { return Ok(()) };
749    let files = rucc_stub::glibc::stubs(tuple, minor).map_err(|why| Error::Cross {
750        why: format!("the glibc stubs for {}: {why}", tuple.to_canonical_string()),
751    })?;
752    let dir = sysroot.stubs();
753    let failed = |path: &Path, why: std::io::Error| Error::Cross {
754        why: format!("{} cannot be written: {why}", path.display()),
755    };
756    fs::create_dir_all(dir).map_err(|why| failed(dir, why))?;
757    for file in files {
758        let path = dir.join(&file.name);
759        if fs::read(&path).is_ok_and(|there| there == file.bytes) {
760            continue;
761        }
762        let temporary = dir.join(format!(".{}.{}", file.name, std::process::id()));
763        fs::write(&temporary, &file.bytes).map_err(|why| failed(&temporary, why))?;
764        fs::rename(&temporary, &path).map_err(|why| {
765            let _ = fs::remove_file(&temporary);
766            failed(&path, why)
767        })?;
768    }
769    Ok(())
770}
771
772/// The linker to use, looked for where a linker is.
773///
774/// `-B` prefixes first, since the point of one is to put a toolchain in front of the machine's,
775/// then the path. A name that contains a separator is a path and is taken as one, which is what
776/// gcc does with `-fuse-ld=/usr/bin/ld.gold` and what a build system relying on that expects.
777///
778/// # Errors
779///
780/// [`Error::Named`] when `-fuse-ld=` asked for one that is not here, and [`Error::NoLinker`] when
781/// nothing was, which name the candidates so that the message says what was looked for.
782pub fn find(target: Triple, opts: &LinkOptions) -> Result<Linker, Error> {
783    let tried = order(target, opts);
784    let places = lld_dirs(Path::new("/"), std::env::var_os("ProgramFiles").map(PathBuf::from));
785    // The first linker [`suitable`] turned down, which is the answer when nothing after it is any
786    // better. Ubuntu 24.04 has lld 18 on PATH and lld 19 under /usr/lib/llvm-19/bin once somebody
787    // installs `lld-19`, and the second is the one to use without asking them to change PATH.
788    let mut refused = None;
789    for name in &tried {
790        for path in linker_candidates(name, opts, &places) {
791            let linker = Linker { name: name.clone(), path };
792            match suitable(target, &linker) {
793                Ok(()) => return Ok(linker),
794                Err(why) => {
795                    refused.get_or_insert(why);
796                }
797            }
798        }
799    }
800    if let Some(why) = refused {
801        return Err(why);
802    }
803    match &opts.use_ld {
804        Some(name) => Err(Error::Named { name: name.clone() }),
805        None => Err(Error::NoLinker { tried }),
806    }
807}
808
809/// Every file a linker of this name could be, in the order they are asked.
810///
811/// A name with a separator in it is a path and is the only answer. Otherwise the `-B` prefixes,
812/// then every directory on `PATH` rather than the first hit, then the places an lld is installed
813/// without being put on `PATH`. All of them rather than the first, because the first may be an lld
814/// that [`suitable`] turns down and a later one may not be.
815fn linker_candidates(name: &str, opts: &LinkOptions, places: &[PathBuf]) -> Vec<PathBuf> {
816    if name.contains(std::path::MAIN_SEPARATOR) || name.contains('/') {
817        let path = PathBuf::from(name);
818        return if path.is_file() { vec![path] } else { Vec::new() };
819    }
820    let pathext = pathext();
821    let mut found: Vec<PathBuf> = opts
822        .prefixes
823        .iter()
824        .flat_map(|dir| spellings(&dir.join(name), &pathext))
825        .filter(|path| path.is_file())
826        .collect();
827    if let Some(path) = std::env::var_os("PATH") {
828        found.extend(
829            std::env::split_paths(&path)
830                .flat_map(|dir| spellings(&dir.join(name), &pathext))
831                .filter(|p| executable(p)),
832        );
833    }
834    // The same spellings as on PATH, so `ld.lld` here and `ld.lld.exe` on Windows. Only `.exe`
835    // was tried at first, which found nothing in /usr/lib/llvm-19/bin on the Linux machines this
836    // search was written for.
837    if is_lld(name) {
838        for dir in places {
839            for file in spellings(&dir.join(name), &pathext) {
840                if executable(&file) && !found.contains(&file) {
841                    found.push(file);
842                }
843            }
844        }
845    }
846    found
847}
848
849/// The extensions a program name is tried with, from `PATHEXT` on Windows and none elsewhere.
850///
851/// `ld.lld` on Windows is `ld.lld.exe`, and looking for the bare name finds nothing, which is how
852/// a Windows host with LLVM installed and on `PATH` used to be told there was no linker. `PATHEXT`
853/// is what `cmd.exe` uses for the same question, and when it is unset the list is the one
854/// Windows ships with.
855fn pathext() -> Vec<String> {
856    if !cfg!(windows) {
857        return Vec::new();
858    }
859    let list = std::env::var("PATHEXT").unwrap_or_else(|_| ".COM;.EXE;.BAT;.CMD".to_owned());
860    list.split(';').filter(|ext| ext.starts_with('.')).map(str::to_ascii_lowercase).collect()
861}
862
863/// A path with each extension added, or as given when there are none or it already ends in one.
864///
865/// `ld.lld` ends in `.lld`, which is not an extension anybody runs, so the check is against the
866/// list rather than against whether there is a dot in the name at all. The bare name is left out
867/// when there is a list, because a file called `ld.lld` in a Git Bash directory on Windows is a
868/// shell script that `CreateProcess` cannot start.
869fn spellings(path: &Path, exts: &[String]) -> Vec<PathBuf> {
870    let has = path
871        .extension()
872        .and_then(|ext| ext.to_str())
873        .is_some_and(|ext| exts.iter().any(|e| e[1..].eq_ignore_ascii_case(ext)));
874    if exts.is_empty() || has {
875        return vec![path.to_path_buf()];
876    }
877    exts.iter()
878        .map(|ext| {
879            let mut name = path.as_os_str().to_owned();
880            name.push(ext);
881            PathBuf::from(name)
882        })
883        .collect()
884}
885
886/// Whether a name is one of the spellings lld is installed under.
887fn is_lld(name: &str) -> bool {
888    matches!(name, "ld.lld" | "ld64.lld" | "lld" | "lld-link")
889}
890
891/// Where lld is installed without being on `PATH`, newest first where a version is in the name.
892///
893/// Homebrew's `lld` and `llvm` formulae, whose `llvm` is keg-only and so never on `PATH` unless
894/// somebody put it there. Debian and Ubuntu's `lld-<N>` packages, which put the real program in
895/// `/usr/lib/llvm-<N>/bin` and only a versioned name in `/usr/bin`. Fedora's compatibility packages,
896/// which do the same under `/usr/lib64/llvm<N>/bin`. And the LLVM installer for Windows, which puts
897/// everything in `%ProgramFiles%\LLVM\bin` and leaves adding it to `PATH` as a checkbox that is off.
898///
899/// `root` is `/` except in the tests, which build the same tree somewhere they are allowed to.
900fn lld_dirs(root: &Path, program_files: Option<PathBuf>) -> Vec<PathBuf> {
901    let mut dirs: Vec<PathBuf> = [
902        "opt/homebrew/opt/lld/bin",
903        "opt/homebrew/opt/llvm/bin",
904        "usr/local/opt/lld/bin",
905        "usr/local/opt/llvm/bin",
906        "home/linuxbrew/.linuxbrew/opt/lld/bin",
907        "home/linuxbrew/.linuxbrew/opt/llvm/bin",
908    ]
909    .iter()
910    .map(|dir| root.join(dir))
911    .collect();
912    for (parent, prefix) in [("usr/lib", "llvm-"), ("usr/lib64", "llvm")] {
913        let mut versions: Vec<(u32, PathBuf)> = fs::read_dir(root.join(parent))
914            .into_iter()
915            .flatten()
916            .flatten()
917            .filter_map(|entry| {
918                let name = entry.file_name();
919                let version = name.to_str()?.strip_prefix(prefix)?.parse().ok()?;
920                Some((version, entry.path().join("bin")))
921            })
922            .collect();
923        versions.sort_by_key(|(version, _)| std::cmp::Reverse(*version));
924        dirs.extend(versions.into_iter().map(|(_, dir)| dir));
925    }
926    if let Some(dir) = program_files {
927        dirs.push(dir.join("LLVM").join("bin"));
928    }
929    dirs
930}
931
932/// Where to get an lld on the machine this compiler is running on, as the end of a sentence.
933///
934/// One per host rather than a list of every package manager, because the person reading it has one
935/// machine and wants the one command for it. Each names a place [`lld_dirs`] looks, so that doing
936/// what it says is enough and nobody has to edit `PATH` afterwards.
937fn lld_advice() -> &'static str {
938    if cfg!(target_os = "macos") {
939        "`brew install lld` installs one, and rucc finds it in Homebrew's directory without a \
940         change to PATH"
941    } else if cfg!(windows) {
942        "the LLVM installer from https://github.com/llvm/llvm-project/releases, or `winget install \
943         LLVM.LLVM`, installs one in %ProgramFiles%\\LLVM\\bin, where rucc finds it without a \
944         change to PATH"
945    } else {
946        "on Debian and Ubuntu `apt install lld-19` installs one in /usr/lib/llvm-19/bin and on \
947         Fedora `dnf install lld` installs one on PATH, and rucc finds either without a change to \
948         PATH"
949    }
950}
951
952/// The first lld that reads `IMPORT_NAME_EXPORTAS`, which is what a windows-gnu link needs.
953///
954/// 18 does not read it and does not say so, so the number is not a convenience: below it the
955/// answer is wrong rather than absent. tamnd/rucc#1515.
956pub const LLD_EXPORTAS: u32 = 19;
957
958/// Whether a found linker can do this target's link, asked before it is handed anything.
959///
960/// Section 11.6's rule is that suitable is checked and not assumed, and this is the one check that
961/// cannot be made by looking at a file. A windows-gnu link reads import libraries that mingw-w64's
962/// `==` aliases compiled into `IMPORT_NAME_EXPORTAS` records, which lld reads from
963/// [`LLD_EXPORTAS`] on. An older lld writes an import by ordinal zero instead, without a warning
964/// and with a successful exit, so nothing later in the toolchain has anything to notice: the
965/// program is wrong at startup and the link that made it said nothing. Ubuntu 24.04 is the current
966/// LTS and ships 18, so the machine this happens on is an ordinary one.
967///
968/// Every other target is left alone, and so is anything that is not an lld, because this is the one
969/// version of the one linker that is known to answer wrongly rather than not at all.
970///
971/// A linker that will not run or whose version cannot be read is allowed through. What the check
972/// can establish is that a specific old lld is here, and it should not turn every unusual linker
973/// into a refusal on the strength of failing to recognise it.
974///
975/// # Errors
976///
977/// [`Error::TooOld`] when the linker is an lld older than [`LLD_EXPORTAS`] and the target is
978/// windows-gnu.
979pub fn suitable(target: Triple, linker: &Linker) -> Result<(), Error> {
980    if (target.os, target.env) != (Os::Windows, Env::Gnu) {
981        return Ok(());
982    }
983    let Some(found) = lld_major(&reported_version(&linker.path)) else { return Ok(()) };
984    if found >= LLD_EXPORTAS {
985        return Ok(());
986    }
987    Err(Error::TooOld {
988        name: linker.name.clone(),
989        found,
990        target: target.tuple().to_canonical_string(),
991    })
992}
993
994/// What `<linker> --version` prints, or an empty string when it will not say.
995///
996/// A linker that cannot be started is not this function's problem to report, because the link is
997/// about to start it again and say so properly. What this returns for such a one is nothing to
998/// read, which is the same as a linker that ran and said something unrecognisable.
999fn reported_version(path: &Path) -> String {
1000    let Ok(out) = Command::new(path).arg("--version").output() else { return String::new() };
1001    String::from_utf8_lossy(&out.stdout).into_owned()
1002}
1003
1004/// The major version in an lld's `--version`, when the program that printed it was an lld.
1005///
1006/// What lld prints is `LLD 18.1.8 (compatible with GNU linkers)`, with a distribution's own prefix
1007/// in front of it often enough that the word is looked for rather than the line starting with it:
1008/// Ubuntu's says `Ubuntu LLD 18.1.3`. Binutils prints `GNU ld (GNU Binutils for Ubuntu) 2.42` and
1009/// mold prints its own name, and neither has the word, so both come back as [`None`] and are left
1010/// alone.
1011fn lld_major(text: &str) -> Option<u32> {
1012    let mut words = text.split_whitespace();
1013    words.find(|word| *word == "LLD")?;
1014    words.next()?.split('.').next()?.parse().ok()
1015}
1016
1017/// Whether a path is a file this process could run.
1018#[cfg(unix)]
1019fn executable(path: &Path) -> bool {
1020    use std::os::unix::fs::PermissionsExt as _;
1021    path.metadata().is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
1022}
1023
1024/// Whether a path is a file this process could run.
1025///
1026/// Windows has no executable bit and decides by extension, and the names looked for above carry
1027/// theirs, so being a file is the whole of the question here.
1028#[cfg(not(unix))]
1029fn executable(path: &Path) -> bool {
1030    path.is_file()
1031}
1032
1033/// What the linker is told, in order, not counting the linker itself.
1034///
1035/// Two lines and [`cross_sysroot`] picks which: the one above for this machine, and
1036/// [`rucc_sysroot::argv`]'s for any other. Nothing about the machine is read on the second path, so
1037/// `-###` prints the same line on every host and prints it whether the sysroot has been built or
1038/// not, which is what makes it worth printing.
1039///
1040/// # Errors
1041///
1042/// [`Error::Target`] for a platform there is no native line for yet, which is every one but Linux,
1043/// and [`Error::Cross`] for a cross link that cannot be produced at all.
1044pub fn line(
1045    target: Triple,
1046    opts: &LinkOptions,
1047    items: &[Item],
1048    output: &str,
1049) -> Result<Vec<String>, Error> {
1050    if opts.relocatable {
1051        return relocatable_line(target, opts, items, output);
1052    }
1053    if target.os == Os::Darwin {
1054        return darwin_line(target, opts, items, output);
1055    }
1056    if let Some(sysroot) = cross_sysroot(target, opts) {
1057        return cross_line(target, opts, items, output, &sysroot);
1058    }
1059    if target.os != Os::Linux {
1060        return Err(Error::Target { triple: target.to_string() });
1061    }
1062    let machine = emulation(target);
1063    let root = opts.sysroot.as_deref();
1064    // A distribution's cross tree is linked by the same line, with its two directories in place of
1065    // this machine's, because it is laid out the way this machine's own library is.
1066    let distro = distro_cross(target, opts);
1067    let dirs = match &distro {
1068        Some(distro) => vec![distro.lib()],
1069        None => library_dirs(target, root),
1070    };
1071    // Where a gcc on this machine keeps its own runtime, which is a different place from where
1072    // the C library keeps its own, and where our runtime is if it was built for this target.
1073    let runtime = match distro {
1074        Some(distro) => distro.gcc,
1075        None => runtime_dirs(target, root),
1076    };
1077    let ours = if opts.no_builtins_lib { None } else { builtins_archive(target, &opts.prefixes) };
1078    let mut args = vec![
1079        "-o".to_owned(),
1080        output.to_owned(),
1081        // Which of the several formats one `ld` can write is meant. A linker built for more than
1082        // one machine guesses from its first input otherwise, and a link of no objects at all has
1083        // nothing to guess from.
1084        "-m".to_owned(),
1085        machine.to_owned(),
1086        // The table a program unwinds through, which a C program with no exceptions in it still
1087        // needs because `backtrace` and every crash handler read it.
1088        "--eh-frame-hdr".to_owned(),
1089        // The symbol hash a dynamic loader from this century reads. The old one is still written
1090        // alongside by default on some distributions, and asking for this one is what stops a link
1091        // from carrying a table nothing has needed since 2006.
1092        "--hash-style=gnu".to_owned(),
1093    ];
1094
1095    let pie = opts.pie.unwrap_or(!opts.is_static && !opts.shared);
1096    if opts.shared {
1097        args.push("-shared".to_owned());
1098    } else if opts.is_static {
1099        args.push("-static".to_owned());
1100    } else if pie {
1101        args.push("-pie".to_owned());
1102    } else {
1103        args.push("-no-pie".to_owned());
1104    }
1105    if !opts.is_static && !opts.shared {
1106        args.push("-dynamic-linker".to_owned());
1107        args.push(target_path(root, loader(target)));
1108    }
1109    if opts.export_dynamic {
1110        args.push("--export-dynamic".to_owned());
1111    }
1112    if opts.strip {
1113        args.push("-s".to_owned());
1114    }
1115
1116    if opts.wants_startfiles() {
1117        for name in startfile(opts, pie).into_iter().chain(["crti.o"]) {
1118            if let Some(path) = find_file(&dirs, name) {
1119                args.push(path.display().to_string());
1120            }
1121        }
1122        // The compiler's own startup file, which runs the static constructors. Three spellings
1123        // of the same thing, and which one is right is about how the code in it refers to
1124        // itself: `S` for a position independent result, `T` for a static one, plain for the
1125        // rest. Skipped when there is no gcc on the machine to take it from, because a program
1126        // with no constructor in it does not miss it.
1127        let begin = if opts.shared || pie {
1128            "crtbeginS.o"
1129        } else if opts.is_static {
1130            "crtbeginT.o"
1131        } else {
1132            "crtbegin.o"
1133        };
1134        if let Some(path) = find_file(&runtime, begin).or_else(|| find_file(&runtime, "crtbegin.o"))
1135        {
1136            args.push(path.display().to_string());
1137        }
1138    }
1139
1140    for dir in &opts.search {
1141        args.push(format!("-L{}", dir.display()));
1142    }
1143    for dir in &dirs {
1144        args.push(format!("-L{}", dir.display()));
1145    }
1146    // Where `libgcc.a` and `libgcc_eh.a` are, which is not where the C library is. Nothing is
1147    // added when there is no gcc on the machine, and then the `-l` names below are left off too.
1148    for dir in &runtime {
1149        args.push(format!("-L{}", dir.display()));
1150    }
1151
1152    for item in items {
1153        match item {
1154            Item::File(path) => args.push(path.clone()),
1155            Item::Library(name) => args.push(format!("-l{name}")),
1156            Item::Linker(arg) => args.push(arg.clone()),
1157        }
1158    }
1159    // After the objects, because a static archive is searched for what is undefined at the point
1160    // it is reached and a library named before the object that needs it contributes nothing.
1161    args.extend(runtime_items(opts, &runtime, ours.as_deref()));
1162
1163    if opts.wants_startfiles() {
1164        // The other end of `crtbegin`, and it goes before `crtn.o` for the same reason `crti.o`
1165        // goes before `crtbegin`: the four are two nested pairs and not four separate files.
1166        // The fast math constructor, ahead of `crtend` where gcc puts it. Skipped when there is
1167        // no gcc to take it from, as `crtbegin` is.
1168        if opts.wants_fastmath() {
1169            if let Some(path) = find_file(&runtime, "crtfastmath.o") {
1170                args.push(path.display().to_string());
1171            }
1172        }
1173        let end = if opts.shared || pie { "crtendS.o" } else { "crtend.o" };
1174        if let Some(path) = find_file(&runtime, end).or_else(|| find_file(&runtime, "crtend.o")) {
1175            args.push(path.display().to_string());
1176        }
1177        if let Some(path) = find_file(&dirs, "crtn.o") {
1178            args.push(path.display().to_string());
1179        }
1180    }
1181
1182    Ok(args)
1183}
1184
1185/// The line for `-r`, which is the objects and the machine and nothing else.
1186///
1187/// No sysroot is read, because a relocatable link takes nothing from the C library, so this is the
1188/// same line for this machine and for any other Linux target. The `-L` directories the command
1189/// line gave are kept for a `-l` written on it, which the linker still resolves against archives.
1190fn relocatable_line(
1191    target: Triple,
1192    opts: &LinkOptions,
1193    items: &[Item],
1194    output: &str,
1195) -> Result<Vec<String>, Error> {
1196    if target.os == Os::Darwin {
1197        // `ld64` joins objects with `-r` too, and needs only to be told the architecture, since
1198        // nothing from the SDK goes into an object that is still going to be linked.
1199        let mut args = vec![
1200            "-r".to_owned(),
1201            "-arch".to_owned(),
1202            darwin_arch(target)?.to_owned(),
1203            "-o".to_owned(),
1204            output.to_owned(),
1205        ];
1206        for dir in &opts.search {
1207            args.push(format!("-L{}", dir.display()));
1208        }
1209        push_items(&mut args, items);
1210        return Ok(args);
1211    }
1212    if target.os != Os::Linux {
1213        return Err(Error::Target { triple: target.to_string() });
1214    }
1215    let mut args = vec![
1216        "-o".to_owned(),
1217        output.to_owned(),
1218        "-m".to_owned(),
1219        emulation(target).to_owned(),
1220        "-r".to_owned(),
1221    ];
1222    if opts.strip {
1223        args.push("-s".to_owned());
1224    }
1225    for dir in &opts.search {
1226        args.push(format!("-L{}", dir.display()));
1227    }
1228    for item in items {
1229        match item {
1230            Item::File(path) => args.push(path.clone()),
1231            Item::Library(name) => args.push(format!("-l{name}")),
1232            Item::Linker(arg) => args.push(arg.clone()),
1233        }
1234    }
1235    Ok(args)
1236}
1237
1238/// The line `ld64` takes, which is Apple's `ld` or lld's Mach-O flavour.
1239///
1240/// Shorter than the ELF one, because the SDK carries everything a program starts with. There are no
1241/// start files to find: `libSystem` has the C library, the startup code and the runtime in it, and
1242/// `dyld` calls `main` itself. What the linker has to be told is the architecture, the platform with
1243/// the oldest release the program runs on and the SDK it was built against, and where the SDK is,
1244/// which `-syslibroot` puts in front of every library it looks for.
1245///
1246/// `-pie` and `-no-pie` are not passed on. Every arm64 program on a Mac is position independent and
1247/// `ld64` rejects `-no_pie` there, so the flag has nothing to change. `-static` is refused, because
1248/// Apple ships no static C library and a static executable is a kernel's business.
1249///
1250/// # Errors
1251///
1252/// [`Error::Cross`] when there is no SDK to link against, or for a static link, or an architecture
1253/// that has no Mach-O name.
1254pub fn darwin_line(
1255    target: Triple,
1256    opts: &LinkOptions,
1257    items: &[Item],
1258    output: &str,
1259) -> Result<Vec<String>, Error> {
1260    let arch = darwin_arch(target)?;
1261    if opts.is_static && !opts.shared {
1262        return Err(Error::Cross {
1263            why: "there is no static C library for Apple platforms, so -static cannot make a \
1264                  program for one"
1265                .to_owned(),
1266        });
1267    }
1268    let Some(sdk) = crate::library::sdk(opts.sysroot.as_deref()) else {
1269        return Err(Error::Cross {
1270            why: format!(
1271                "no SDK was found to link {} against. Install the command line tools with \
1272                 `xcode-select --install`, or name one with -isysroot <dir> or SDKROOT",
1273                target_tuple(target, opts).to_canonical_string()
1274            ),
1275        });
1276    };
1277    let tuple = target_tuple(target, opts);
1278    let (platform, default) = match (tuple.os(), tuple.env()) {
1279        (rucc_tuple::Os::IOs, rucc_tuple::Env::Simulator) => ("ios-simulator", "14.0"),
1280        (rucc_tuple::Os::IOs, rucc_tuple::Env::MacAbi) => ("mac-catalyst", "14.0"),
1281        (rucc_tuple::Os::IOs, _) => ("ios", "14.0"),
1282        _ => ("macos", "11.0"),
1283    };
1284    let minimum = opts
1285        .os_version
1286        .or_else(|| tuple.os_version())
1287        .map_or_else(|| default.to_owned(), |version| version.to_string());
1288    // What the SDK says it is, which `ld64` records so the loader can tell which behaviours the
1289    // program was built to expect. The deployment target when the SDK does not say, which is the
1290    // answer that asks for no behaviour newer than the program claims to run on.
1291    let version = sdk_version(&sdk).unwrap_or_else(|| minimum.clone());
1292
1293    let mut args = vec![
1294        "-arch".to_owned(),
1295        arch.to_owned(),
1296        "-platform_version".to_owned(),
1297        platform.to_owned(),
1298        minimum,
1299        version,
1300        "-syslibroot".to_owned(),
1301        sdk.display().to_string(),
1302        "-o".to_owned(),
1303        output.to_owned(),
1304    ];
1305    if opts.shared {
1306        args.push("-dylib".to_owned());
1307    }
1308    if opts.export_dynamic {
1309        args.push("-export_dynamic".to_owned());
1310    }
1311    if opts.strip {
1312        // The debug map and the local symbols, which between them are what `-s` leaves out of an
1313        // ELF program. `ld64` has no one flag for it.
1314        args.push("-S".to_owned());
1315        args.push("-x".to_owned());
1316    }
1317    for dir in &opts.search {
1318        args.push(format!("-L{}", dir.display()));
1319    }
1320    push_items(&mut args, items);
1321    if opts.wants_runtime() && !opts.no_builtins_lib {
1322        if let Some(ours) = builtins_archive(target, &opts.prefixes) {
1323            args.push(ours.display().to_string());
1324        }
1325    }
1326    if opts.wants_defaultlibs() {
1327        args.push("-lSystem".to_owned());
1328    }
1329    Ok(args)
1330}
1331
1332/// What `ld64` calls the architecture, which is not what the triple does.
1333fn darwin_arch(target: Triple) -> Result<&'static str, Error> {
1334    match target.arch {
1335        Arch::Aarch64 => Ok("arm64"),
1336        Arch::X86_64 => Ok("x86_64"),
1337        _ => Err(Error::Target { triple: target.to_string() }),
1338    }
1339}
1340
1341/// The version an SDK says it is, from the `SDKSettings.json` every SDK since Xcode 7 has at its
1342/// root, or from its directory name, which is `MacOSX15.2.sdk` when it is not the unversioned link.
1343///
1344/// Read by hand rather than parsed, because the one field wanted is a quoted string at the top
1345/// level and a JSON parser would be a dependency for one line.
1346fn sdk_version(sdk: &Path) -> Option<String> {
1347    let valid = |text: &str| {
1348        !text.is_empty()
1349            && text
1350                .split('.')
1351                .all(|part| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit()))
1352    };
1353    if let Ok(text) = fs::read_to_string(sdk.join("SDKSettings.json")) {
1354        if let Some(at) = text.find("\"Version\"") {
1355            let rest = &text[at + "\"Version\"".len()..];
1356            let rest = rest.trim_start().strip_prefix(':')?.trim_start().strip_prefix('"')?;
1357            let version = &rest[..rest.find('"')?];
1358            if valid(version) {
1359                return Some(version.to_owned());
1360            }
1361        }
1362    }
1363    let name = sdk.file_name()?.to_str()?.strip_suffix(".sdk")?;
1364    let version = name.trim_start_matches(|c: char| c.is_ascii_alphabetic());
1365    valid(version).then(|| version.to_owned())
1366}
1367
1368/// The objects, libraries and linker words, in the order they were written.
1369fn push_items(args: &mut Vec<String>, items: &[Item]) {
1370    for item in items {
1371        match item {
1372            Item::File(path) => args.push(path.clone()),
1373            Item::Library(name) => args.push(format!("-l{name}")),
1374            Item::Linker(arg) => args.push(arg.clone()),
1375        }
1376    }
1377}
1378
1379/// The startup file the C library brings, or `None` for a link that calls nothing.
1380///
1381/// This is what calls `main` and what passes it the arguments, so a shared object takes none of
1382/// them: nothing starts one and it has no `main` to be started at. `Scrt1.o` rather than `crt1.o`
1383/// when the result moves, because the two differ in whether the reference to `main` in them is one
1384/// a loader may relocate.
1385///
1386/// A profiled program gets a different one again, which does all of that and starts and stops the
1387/// counting around it. There are two of those rather than three: the one that relocates itself is
1388/// only needed by a static position independent link, and every other link takes the plain one,
1389/// which is what gcc does with the same flag.
1390fn startfile(opts: &LinkOptions, pie: bool) -> Option<&'static str> {
1391    if opts.shared {
1392        None
1393    } else if opts.profile {
1394        Some(if pie && opts.is_static { "grcrt1.o" } else { "gcrt1.o" })
1395    } else if pie {
1396        Some("Scrt1.o")
1397    } else {
1398        Some("crt1.o")
1399    }
1400}
1401
1402/// The libraries the compiler's own runtime contributes, in the order the linker wants them.
1403///
1404/// The C library first, then ours, then the machine's `libgcc`. Order inside this list is not
1405/// about whether a symbol resolves, it is about which archive supplies one that more than one of
1406/// them defines, and the two places that happens both have a right answer.
1407///
1408/// `memcpy` and its three neighbours are in the C library on a hosted target and in ours only for
1409/// a freestanding one, which is what `spec/12-abi-and-runtime.md` section 12.8 says they are for.
1410/// glibc's are written in assembly per microarchitecture and ours is a word at a time loop, so a
1411/// link that took ours over glibc's would be slower at the one routine every program reaches.
1412///
1413/// The wide arithmetic is in ours and in `libgcc` both, and the two are ABI-identical on purpose,
1414/// so which one answers is not a correctness question. Ours comes first because it is ours, and
1415/// `-fno-builtins-lib` leaves it off for somebody who would rather it were not.
1416///
1417/// A static link puts the whole list inside `--start-group`. `libc.a` refers to `_Unwind_Resume`,
1418/// and the unwinder refers back into `libc.a`, so a linker walking the list once resolves
1419/// whichever it reaches first and reports the other as undefined. That is exactly the failure
1420/// issue #277 describes and the group is the fix for it.
1421///
1422/// A dynamic link needs no group, because the shared `libc` resolves its own references inside
1423/// itself. `libgcc_s` is asked for `--as-needed` there, the way gcc asks for it, so a program that
1424/// never unwinds does not acquire a dependency on it.
1425fn runtime_items(opts: &LinkOptions, runtime: &[PathBuf], ours: Option<&Path>) -> Vec<String> {
1426    let mut args = Vec::new();
1427    if !opts.wants_defaultlibs() && !opts.wants_runtime() {
1428        return args;
1429    }
1430    // Only when there is a gcc to take them from. On a machine without one the names would be an
1431    // error about a library that was never going to be there, and a program that needs neither
1432    // the unwinder nor a wide divide links and runs without them.
1433    let has_gcc = find_file(runtime, "libgcc.a").is_some();
1434
1435    if opts.is_static {
1436        args.push("--start-group".to_owned());
1437    }
1438    if opts.wants_defaultlibs() {
1439        args.push("-lc".to_owned());
1440    }
1441    if opts.wants_runtime() {
1442        if let Some(path) = ours {
1443            args.push(path.display().to_string());
1444        }
1445        if has_gcc {
1446            args.push("-lgcc".to_owned());
1447            if opts.is_static {
1448                args.push("-lgcc_eh".to_owned());
1449            }
1450        }
1451    }
1452    if opts.is_static {
1453        args.push("--end-group".to_owned());
1454    } else if opts.wants_runtime() && has_gcc {
1455        // The shared half, and only if something still wants it after everything above.
1456        args.push("--as-needed".to_owned());
1457        args.push("-lgcc_s".to_owned());
1458        args.push("--no-as-needed".to_owned());
1459    }
1460    args
1461}
1462
1463/// Where a gcc on this machine keeps `crtbegin.o`, `crtend.o` and `libgcc.a`, newest first.
1464///
1465/// This is not where the C library's files are. A distribution puts them under a directory named
1466/// for the gcc version, and there may be several, so the answer is every one that exists with the
1467/// highest version in front. Newest first because a newer `libgcc` is a superset of an older one
1468/// and because that is the one the C library on the same machine was built against.
1469#[must_use]
1470pub fn runtime_dirs(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
1471    let libc = match target.env {
1472        Env::Musl => "musl",
1473        Env::None | Env::Gnu | Env::Msvc => "gnu",
1474    };
1475    let arch = target.arch.as_str();
1476    // The spellings the distributions use for the same triple. Debian and Ubuntu drop the vendor
1477    // field, the source builds and Arch keep `pc`, and Red Hat and SUSE write their own name in
1478    // it, so all of them are looked for and the ones that are there are taken.
1479    let names = [
1480        format!("{arch}-linux-{libc}"),
1481        format!("{arch}-pc-linux-{libc}"),
1482        format!("{arch}-redhat-linux"),
1483        format!("{arch}-suse-linux"),
1484        format!("{arch}-alpine-linux-{libc}"),
1485    ];
1486    let mut found = Vec::new();
1487    for base in ["/usr/lib/gcc", "/usr/lib64/gcc", "/usr/local/lib/gcc"] {
1488        for name in &names {
1489            found.extend(newest_first(&under(sysroot, &format!("{base}/{name}"))));
1490        }
1491    }
1492    found
1493}
1494
1495/// The version directories under one of gcc's, highest version first.
1496fn newest_first(dir: &Path) -> Vec<PathBuf> {
1497    let Ok(entries) = fs::read_dir(dir) else { return Vec::new() };
1498    let mut versions: Vec<(Vec<u64>, PathBuf)> = entries
1499        .flatten()
1500        .map(|e| e.path())
1501        .filter(|p| p.is_dir())
1502        .map(|p| (version_key(&p), p))
1503        .collect();
1504    // Descending, so the highest version is the first place `find_file` looks. Ties keep the order
1505    // the directory gave, which is arbitrary and does not matter because two directories that sort
1506    // the same hold the same version.
1507    versions.sort_by(|a, b| b.0.cmp(&a.0));
1508    versions.into_iter().map(|(_, path)| path).collect()
1509}
1510
1511/// A directory name read as a version, so that `13` sorts above `9` and `10.2` above `10`.
1512///
1513/// A name that is not a version at all sorts below every name that is, rather than being left
1514/// out, because a directory holding a `libgcc.a` is worth looking in whatever it is called.
1515fn version_key(dir: &Path) -> Vec<u64> {
1516    let name = dir.file_name().unwrap_or_default().to_string_lossy();
1517    name.split('.').map(|part| part.parse::<u64>().unwrap_or(0)).collect()
1518}
1519
1520/// Our own runtime library for this target, if it was built.
1521///
1522/// Looked for beside the compiler rather than at a path decided when the compiler was built, for
1523/// the same reason everything else here is looked for: one binary runs wherever it is copied. A
1524/// `-B` prefix is asked first, because that is what a `-B` prefix is for.
1525#[must_use]
1526pub fn builtins_archive(target: Triple, prefixes: &[PathBuf]) -> Option<PathBuf> {
1527    // The name from the crate that puts it on a line, rather than a second spelling of it here,
1528    // which is what that constant asks of anybody who needs the name.
1529    const NAME: &str = rucc_sysroot::link::BUILTINS;
1530    // The four field triple first and then the tuple the sysroots are named by, because `cargo
1531    // xtask builtins --target=aarch64-linux-musl` names its directory after what it was given, and
1532    // that shorter spelling is the one people type.
1533    let spellings = [target.to_string(), target.tuple().to_string()];
1534    let mut places: Vec<PathBuf> = Vec::new();
1535    for prefix in prefixes {
1536        for triple in &spellings {
1537            places.push(prefix.join(triple).join(NAME));
1538        }
1539        places.push(prefix.join(NAME));
1540    }
1541    if let Some(dir) =
1542        std::env::current_exe().ok().and_then(|exe| exe.parent().map(Path::to_path_buf))
1543    {
1544        // A release archive: the compiler at the top of the unpacked directory and the runtime
1545        // beside it in `lib/rucc/<triple>`, which is how `.github/package.sh` lays one out.
1546        for triple in &spellings {
1547            places.push(dir.join("lib").join("rucc").join(triple).join(NAME));
1548        }
1549        if let Some(up) = dir.parent() {
1550            for triple in &spellings {
1551                // An install: the compiler in `bin` and its runtime in `lib/rucc/<triple>`.
1552                places.push(up.join("lib").join("rucc").join(triple).join(NAME));
1553                // A build tree: the compiler in `target/release` and the runtime, which is built
1554                // for the target and not the host, in `target/<triple>/release`.
1555                for profile in ["release", "debug"] {
1556                    places.push(up.join(triple).join(profile).join(NAME));
1557                }
1558            }
1559        }
1560        places.push(dir.join(NAME));
1561    }
1562    places.into_iter().find(|path| path.is_file())
1563}
1564
1565/// Which output format this `ld` should write, in the name `ld` knows it by.
1566fn emulation(target: Triple) -> &'static str {
1567    match target.arch {
1568        Arch::X86_64 => "elf_x86_64",
1569        Arch::Aarch64 => "aarch64linux",
1570        Arch::Riscv64 => "elf64lriscv",
1571    }
1572}
1573
1574/// The program that starts a dynamically linked program, whose path is part of the file.
1575///
1576/// It is a per-target constant rather than something to look for, because the name is fixed by
1577/// the platform's ABI and a program naming a different one does not start.
1578fn loader(target: Triple) -> &'static str {
1579    match (target.arch, target.env) {
1580        (Arch::X86_64, Env::Musl) => "/lib/ld-musl-x86_64.so.1",
1581        (Arch::X86_64, _) => "/lib64/ld-linux-x86-64.so.2",
1582        (Arch::Aarch64, Env::Musl) => "/lib/ld-musl-aarch64.so.1",
1583        (Arch::Aarch64, _) => "/lib/ld-linux-aarch64.so.1",
1584        (Arch::Riscv64, Env::Musl) => "/lib/ld-musl-riscv64.so.1",
1585        (Arch::Riscv64, _) => "/lib/ld-linux-riscv64-lp64d.so.1",
1586    }
1587}
1588
1589/// Where the library's own files might be, in search order.
1590///
1591/// The multiarch directory first for the reason it comes first in the header search: it is where
1592/// a distribution that can hold two architectures at once puts the one being asked for, and a
1593/// distribution that cannot simply does not have it. `lib64` after it, which is what the
1594/// distributions that split by word size use instead, and `lib` last, which is every other one.
1595#[must_use]
1596pub fn candidates(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
1597    let multiarch = multiarch(target);
1598    [
1599        format!("/usr/lib/{multiarch}"),
1600        format!("/lib/{multiarch}"),
1601        "/usr/lib64".to_owned(),
1602        "/lib64".to_owned(),
1603        "/usr/lib".to_owned(),
1604        "/lib".to_owned(),
1605    ]
1606    .into_iter()
1607    .map(|dir| under(sysroot, &dir))
1608    .collect()
1609}
1610
1611/// The name a distribution that holds two architectures at once files this target under.
1612///
1613/// `x86_64-linux-gnu` and its friends, which is what `gcc -print-multiarch` prints and what a
1614/// build system pastes into a path when it is looking for a library itself.
1615#[must_use]
1616pub fn multiarch(target: Triple) -> String {
1617    let libc = match target.env {
1618        Env::Musl => "musl",
1619        Env::None | Env::Gnu | Env::Msvc => "gnu",
1620    };
1621    format!("{}-linux-{libc}", target.arch.as_str())
1622}
1623
1624/// The candidates that are there.
1625fn library_dirs(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
1626    candidates(target, sysroot).into_iter().filter(|dir| dir.is_dir()).collect()
1627}
1628
1629/// Where a library is looked for, in the order it is looked for in.
1630///
1631/// The command line first and the target's own after it, which is the order the linker is handed
1632/// and therefore the order `-print-search-dirs` has to print.
1633#[must_use]
1634pub fn search_dirs(link: &LinkOptions, target: Triple) -> Vec<PathBuf> {
1635    let mut dirs = link.search.clone();
1636    // A cross link searches the sysroot's directory and, for a libc that is a stub, the one its
1637    // stubs are written to, so this is those and not the machine's. What `-print-search-dirs` says is what a build
1638    // system pastes into a link line of its own, and an answer that named `/usr/lib` for a target
1639    // whose link line never goes near it would be worse than no answer at all.
1640    if let Some(sysroot) = cross_sysroot(target, link) {
1641        dirs.push(sysroot.lib());
1642        if rucc_sysroot::link::libc(sysroot.target()) == rucc_sysroot::link::Libc::Stub {
1643            dirs.push(sysroot.stubs().to_path_buf());
1644        }
1645        return dirs;
1646    }
1647    if let Some(distro) = distro_cross(target, link) {
1648        dirs.push(distro.lib());
1649        return dirs;
1650    }
1651    dirs.extend(candidates(target, link.sysroot.as_deref()));
1652    dirs
1653}
1654
1655/// The full path of a file with that name, when one of the search directories holds it.
1656///
1657/// What `-print-file-name=` answers. GCC prints the name back unchanged when it finds nothing,
1658/// which is what makes the flag safe to paste into a link line either way.
1659#[must_use]
1660pub fn find_in_search(link: &LinkOptions, target: Triple, name: &str) -> Option<PathBuf> {
1661    find_file(&search_dirs(link, target), name)
1662}
1663
1664/// The first of those directories holding a file of that name.
1665fn find_file(dirs: &[PathBuf], name: &str) -> Option<PathBuf> {
1666    dirs.iter().map(|dir| dir.join(name)).find(|path| path.is_file())
1667}
1668
1669/// A path under the sysroot, when there is one.
1670fn under(sysroot: Option<&Path>, path: &str) -> PathBuf {
1671    match sysroot {
1672        // `strip_prefix` because joining an absolute path replaces the root rather than extending
1673        // it, which would make every entry the unprefixed one.
1674        Some(root) => root.join(path.strip_prefix('/').unwrap_or(path)),
1675        None => PathBuf::from(path),
1676    }
1677}
1678
1679/// A path on the machine that will run the program, rather than on the one compiling it.
1680///
1681/// Written with the separator of the target and not of the host, which matters for the one path
1682/// that is not looked at here but stored in the file and read by something else later: the loader
1683/// a dynamic program names. A Windows host joining it would put a backslash in the middle of a
1684/// name that a Linux loader has to find, and the program would not start.
1685fn target_path(sysroot: Option<&Path>, path: &str) -> String {
1686    match sysroot {
1687        Some(root) => {
1688            let root = root.display().to_string();
1689            format!("{}/{}", root.trim_end_matches(['/', '\\']), path.trim_start_matches('/'))
1690        }
1691        None => path.to_owned(),
1692    }
1693}
1694
1695/// The whole invocation as one line, quoted the way `-###` prints it.
1696#[must_use]
1697pub fn render(linker: &Linker, args: &[String]) -> String {
1698    let mut out = linker.path.display().to_string();
1699    for arg in args {
1700        out.push(' ');
1701        if arg.is_empty() || arg.contains(char::is_whitespace) {
1702            out.push('"');
1703            out.push_str(arg);
1704            out.push('"');
1705        } else {
1706            out.push_str(arg);
1707        }
1708    }
1709    out
1710}
1711
1712/// How long a command line may be on Windows before the arguments go in a file instead.
1713///
1714/// `CreateProcess` takes 32767 characters, the program's own path and the quoting included, and
1715/// what is left for the arguments is not worth computing to the character.
1716const WINDOWS_LINE: usize = 30_000;
1717
1718/// The same for every other host, where the limit is a megabyte or more shared with the
1719/// environment, and a link this long is Kbuild's `-r` of a whole subsystem.
1720const UNIX_LINE: usize = 256 * 1024;
1721
1722/// Whether these arguments, joined with a space and a pair of quotes each, are over `limit`.
1723fn too_long(args: &[String], limit: usize) -> bool {
1724    args.iter().map(|arg| arg.len() + 3).sum::<usize>() > limit
1725}
1726
1727/// The arguments that stay on the command line and the ones that go in the file.
1728///
1729/// `-m` and its value stay, because they are what makes `ld.lld` pick its MinGW driver over its
1730/// ELF one, and it is better not to depend on that choice looking inside the file.
1731fn split_for_file(args: &[String]) -> (Vec<String>, Vec<String>) {
1732    let mut front = Vec::new();
1733    let mut rest = Vec::with_capacity(args.len());
1734    let mut words = args.iter();
1735    while let Some(arg) = words.next() {
1736        if arg == "-m" {
1737            front.push(arg.clone());
1738            front.extend(words.next().cloned());
1739        } else {
1740            rest.push(arg.clone());
1741        }
1742    }
1743    (front, rest)
1744}
1745
1746/// Whether this linker reads a response file with Windows quoting, where a backslash is an
1747/// ordinary character unless it comes before a quote.
1748///
1749/// lld does on a Windows host, both its ELF driver and its MinGW one, and the MinGW one has no
1750/// option to say otherwise. GNU ld reads the GNU way on every host, MSYS2's included.
1751fn windows_quoting(linker: &Linker) -> bool {
1752    let file = linker.path.file_stem().and_then(|stem| stem.to_str()).unwrap_or_default();
1753    cfg!(windows) && (is_lld(&linker.name) || file.starts_with("ld.lld") || file.starts_with("lld"))
1754}
1755
1756/// The words of a response file, one to a line, quoted so that the linker reads them back as
1757/// they were.
1758///
1759/// The GNU way is a backslash before every quote and every backslash. The Windows way leaves a
1760/// backslash alone unless a run of them ends at a quote, and then doubles the run and escapes the
1761/// quote, which is the rule `CommandLineToArgvW` reads with and so the rule LLVM reads with too.
1762fn response_text(args: &[String], windows: bool) -> String {
1763    let mut text = String::new();
1764    for arg in args {
1765        text.push('"');
1766        let mut slashes = 0;
1767        for c in arg.chars() {
1768            match c {
1769                '\\' if windows => slashes += 1,
1770                '"' if windows => {
1771                    text.extend(std::iter::repeat_n('\\', slashes * 2 + 1));
1772                    text.push('"');
1773                    slashes = 0;
1774                }
1775                _ if windows => {
1776                    text.extend(std::iter::repeat_n('\\', slashes));
1777                    text.push(c);
1778                    slashes = 0;
1779                }
1780                '"' | '\\' => {
1781                    text.push('\\');
1782                    text.push(c);
1783                }
1784                _ => text.push(c),
1785            }
1786        }
1787        text.extend(std::iter::repeat_n('\\', slashes * 2));
1788        text.push_str("\"\n");
1789    }
1790    text
1791}
1792
1793/// Runs the linker and waits for it.
1794///
1795/// A line too long for the host goes to the linker as a response file, which is what a Windows
1796/// build of a large program needs: a few hundred objects in a deep directory are past what
1797/// `CreateProcess` takes.
1798///
1799/// # Errors
1800///
1801/// [`Error::Spawn`] when it could not be started, which is a machine problem, and
1802/// [`Error::Refused`] when it ran and said no, which is a program problem and one the linker has
1803/// already explained on its own error output.
1804pub fn run(linker: &Linker, args: &[String]) -> Result<(), Error> {
1805    let spawn = |why: std::io::Error| Error::Spawn {
1806        path: linker.path.display().to_string(),
1807        why: why.to_string(),
1808    };
1809    let mut command = Command::new(&linker.path);
1810    let mut written = None;
1811    if too_long(args, if cfg!(windows) { WINDOWS_LINE } else { UNIX_LINE }) {
1812        let (front, rest) = split_for_file(args);
1813        let path = std::env::temp_dir().join(format!("rucc-link-{}.rsp", std::process::id()));
1814        fs::write(&path, response_text(&rest, windows_quoting(linker))).map_err(spawn)?;
1815        let mut at = OsString::from("@");
1816        at.push(&path);
1817        command.args(front).arg(at);
1818        written = Some(path);
1819    } else {
1820        command.args(args.iter().map(OsString::from));
1821    }
1822    let status = command.status();
1823    if let Some(path) = written {
1824        let _ = fs::remove_file(path);
1825    }
1826    let status = status.map_err(spawn)?;
1827    if status.success() {
1828        return Ok(());
1829    }
1830    // Nothing is added to what the linker printed. It has already named the symbol or the file,
1831    // and a second message from here saying that linking failed would only push the first one
1832    // further up the screen.
1833    Err(Error::Refused {
1834        status: match status.code() {
1835            Some(code) => format!("exited with status {code}"),
1836            None => "was killed before it finished".to_owned(),
1837        },
1838    })
1839}
1840
1841#[cfg(test)]
1842mod tests {
1843    use super::*;
1844
1845    fn linux() -> Triple {
1846        Triple::new(Arch::X86_64, Os::Linux, Env::Gnu)
1847    }
1848
1849    fn one(name: &str) -> Vec<Item> {
1850        vec![Item::File(name.to_owned())]
1851    }
1852
1853    #[test]
1854    fn the_fast_one_is_looked_for_first_and_the_platforms_own_last() {
1855        let names = order(linux(), &LinkOptions::default());
1856        assert_eq!(names.first().map(String::as_str), Some("ld.mold"));
1857        assert_eq!(names.last().map(String::as_str), Some("ld"));
1858    }
1859
1860    #[test]
1861    fn naming_one_is_the_whole_of_the_order() {
1862        let opts = LinkOptions { use_ld: Some("gold".to_owned()), ..LinkOptions::default() };
1863        assert_eq!(order(linux(), &opts), ["ld.gold", "gold"]);
1864    }
1865
1866    #[test]
1867    fn a_dynamic_program_names_the_loader_that_will_start_it() {
1868        let args = line(linux(), &LinkOptions::default(), &one("a.o"), "a.out").expect("a line");
1869        let at = args.iter().position(|a| a == "-dynamic-linker").expect("the flag");
1870        assert!(args[at + 1].ends_with("/lib64/ld-linux-x86-64.so.2"), "{args:?}");
1871    }
1872
1873    /// Kbuild's `built-in.o`, which is objects joined into an object and is linked again later.
1874    #[test]
1875    fn a_relocatable_link_is_the_objects_and_nothing_a_program_needs() {
1876        let opts = LinkOptions { relocatable: true, ..LinkOptions::default() };
1877        let args = line(linux(), &opts, &one("a.o"), "built-in.o").expect("a line");
1878        assert_eq!(args, ["-o", "built-in.o", "-m", "elf_x86_64", "-r", "a.o"]);
1879    }
1880
1881    #[test]
1882    fn a_static_program_names_no_loader_because_nothing_will_start_it() {
1883        let opts = LinkOptions { is_static: true, ..LinkOptions::default() };
1884        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1885        assert!(args.contains(&"-static".to_owned()), "{args:?}");
1886        assert!(!args.contains(&"-dynamic-linker".to_owned()), "{args:?}");
1887    }
1888
1889    #[test]
1890    fn the_startup_file_of_a_program_that_moves_is_not_the_one_of_a_program_that_does_not() {
1891        let moving = LinkOptions { pie: Some(true), ..LinkOptions::default() };
1892        let fixed = LinkOptions { pie: Some(false), ..LinkOptions::default() };
1893        let named = |opts: &LinkOptions| {
1894            line(linux(), opts, &one("a.o"), "a.out")
1895                .expect("a line")
1896                .iter()
1897                .filter_map(|a| Path::new(a).file_name().map(|n| n.to_string_lossy().into_owned()))
1898                .find(|n| n.ends_with("crt1.o"))
1899        };
1900        // Only when the machine running this has them, which is what makes this two assertions
1901        // rather than one: a machine with no glibc development files has neither to find.
1902        if let Some(name) = named(&moving) {
1903            assert_eq!(name, "Scrt1.o");
1904            assert_eq!(named(&fixed).as_deref(), Some("crt1.o"));
1905        }
1906    }
1907
1908    /// A profiled program is started by a startup file of its own.
1909    ///
1910    /// The counts it keeps have to be started before `main` runs and written out after it returns,
1911    /// and what does both is this file rather than anything the compiler wrote. So a build that
1912    /// compiles with the flag and links without it produces a program that calls the hook on every
1913    /// function and never writes a profile, which is the failure this is here to keep out.
1914    ///
1915    /// A shared object takes none of them either way, since nothing starts one.
1916    #[test]
1917    fn a_profiled_program_is_started_by_the_startup_file_that_counts() {
1918        let profile = LinkOptions { profile: true, ..LinkOptions::default() };
1919        assert_eq!(startfile(&profile, false), Some("gcrt1.o"));
1920        assert_eq!(startfile(&profile, true), Some("gcrt1.o"));
1921        let still = LinkOptions { is_static: true, ..profile.clone() };
1922        assert_eq!(startfile(&still, true), Some("grcrt1.o"));
1923        assert_eq!(startfile(&still, false), Some("gcrt1.o"));
1924        let shared = LinkOptions { shared: true, ..profile };
1925        assert_eq!(startfile(&shared, false), None);
1926    }
1927
1928    /// And a program that is not profiled is started by the one it always was.
1929    #[test]
1930    fn a_program_that_is_not_profiled_is_started_by_the_usual_one() {
1931        let plain = LinkOptions::default();
1932        assert_eq!(startfile(&plain, false), Some("crt1.o"));
1933        assert_eq!(startfile(&plain, true), Some("Scrt1.o"));
1934    }
1935
1936    #[test]
1937    fn asking_for_no_startup_files_leaves_out_both_ends_of_them() {
1938        let opts = LinkOptions { no_startfiles: true, ..LinkOptions::default() };
1939        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1940        assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1941        assert!(!args.iter().any(|a| a.ends_with("crtn.o")), "{args:?}");
1942        // And still links against the library, because that is the other flag.
1943        assert!(args.contains(&"-lc".to_owned()), "{args:?}");
1944    }
1945
1946    #[test]
1947    fn asking_for_no_library_at_all_leaves_out_the_startup_files_too() {
1948        let opts = LinkOptions { no_stdlib: true, ..LinkOptions::default() };
1949        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1950        assert!(!args.contains(&"-lc".to_owned()), "{args:?}");
1951        assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1952    }
1953
1954    #[test]
1955    fn the_library_comes_after_the_objects_that_need_it() {
1956        let items = vec![Item::File("a.o".to_owned()), Item::Library("m".to_owned())];
1957        let args = line(linux(), &LinkOptions::default(), &items, "a.out").expect("a line");
1958        let obj = args.iter().position(|a| a == "a.o").expect("the object");
1959        let m = args.iter().position(|a| a == "-lm").expect("the library");
1960        let c = args.iter().position(|a| a == "-lc").expect("the library");
1961        assert!(obj < m && m < c, "{args:?}");
1962    }
1963
1964    #[test]
1965    fn what_the_user_told_the_linker_stays_where_the_user_wrote_it() {
1966        // The pair libtool writes around a set of convenience archives, which is what found this.
1967        // Both words are about the files between them, so a line that collects them and puts them
1968        // at the end has two options that do nothing and an archive whose members were all dropped.
1969        let items = vec![
1970            Item::File("a.o".to_owned()),
1971            Item::Linker("--whole-archive".to_owned()),
1972            Item::File("libaesni.a".to_owned()),
1973            Item::Linker("--no-whole-archive".to_owned()),
1974            Item::Library("m".to_owned()),
1975        ];
1976        let args = line(linux(), &LinkOptions::default(), &items, "a.out").expect("a line");
1977        let at = |what: &str| args.iter().position(|a| a == what).expect(what);
1978        assert!(at("a.o") < at("--whole-archive"), "{args:?}");
1979        assert!(at("--whole-archive") < at("libaesni.a"), "{args:?}");
1980        assert!(at("libaesni.a") < at("--no-whole-archive"), "{args:?}");
1981        assert!(at("--no-whole-archive") < at("-lm"), "{args:?}");
1982        assert!(at("-lm") < at("-lc"), "{args:?}");
1983    }
1984
1985    #[test]
1986    fn a_sysroot_moves_every_path_this_decided_and_none_the_user_wrote() {
1987        let opts = LinkOptions {
1988            sysroot: Some(PathBuf::from("/nowhere-at-all")),
1989            search: vec![PathBuf::from("/opt/mine")],
1990            ..LinkOptions::default()
1991        };
1992        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1993        let at = args.iter().position(|a| a == "-dynamic-linker").expect("the flag");
1994        assert_eq!(args[at + 1], "/nowhere-at-all/lib64/ld-linux-x86-64.so.2");
1995        assert!(args.contains(&"-L/opt/mine".to_owned()), "{args:?}");
1996    }
1997
1998    #[test]
1999    fn a_platform_with_no_link_line_is_said_so_rather_than_linked_wrongly() {
2000        let triple = Triple::new(Arch::X86_64, Os::Windows, Env::Msvc);
2001        let error = line(triple, &LinkOptions::default(), &one("a.o"), "a.out")
2002            .expect_err("no line for it");
2003        assert!(matches!(error, Error::Target { .. }), "{error:?}");
2004    }
2005
2006    /// A made up SDK with the one file the line reads out of it, so that nothing about this machine
2007    /// decides what the line says.
2008    fn an_sdk(name: &str, settings: Option<&str>) -> PathBuf {
2009        let dir = std::env::temp_dir()
2010            .join(format!("rucc-sdk-{}-{}", std::process::id(), name.replace('.', "-")))
2011            .join(name);
2012        fs::create_dir_all(&dir).expect("a temporary directory");
2013        if let Some(settings) = settings {
2014            fs::write(dir.join("SDKSettings.json"), settings).expect("a settings file");
2015        }
2016        dir
2017    }
2018
2019    fn mac() -> Triple {
2020        Triple::new(Arch::Aarch64, Os::Darwin, Env::None)
2021    }
2022
2023    #[test]
2024    fn a_mac_link_names_the_platform_the_sdk_and_libsystem() {
2025        let sdk = an_sdk(
2026            "MacOSX.sdk",
2027            Some(
2028                "{\"CanonicalName\": \"macosx15.2\", \"Version\" : \"15.2\", \"MaximumDeploymentTarget\": \"15.2.99\"}",
2029            ),
2030        );
2031        let opts = LinkOptions {
2032            sysroot: Some(sdk.clone()),
2033            search: vec![PathBuf::from("/opt/mine")],
2034            no_builtins_lib: true,
2035            ..LinkOptions::default()
2036        };
2037        let items = vec![Item::File("a.o".to_owned()), Item::Library("m".to_owned())];
2038        let args = line(mac(), &opts, &items, "a.out").expect("a line");
2039        let sdk = sdk.display().to_string();
2040        let want: Vec<&str> = vec![
2041            "-arch",
2042            "arm64",
2043            "-platform_version",
2044            "macos",
2045            "11.0",
2046            "15.2",
2047            "-syslibroot",
2048            &sdk,
2049            "-o",
2050            "a.out",
2051            "-L/opt/mine",
2052            "a.o",
2053            "-lm",
2054            "-lSystem",
2055        ];
2056        assert_eq!(args, want);
2057
2058        // The deployment target from the command line, and a shared library.
2059        let opts =
2060            LinkOptions { os_version: Some(rucc_tuple::Version::new(13, 4)), shared: true, ..opts };
2061        let args = line(mac(), &opts, &one("a.o"), "liba.dylib").expect("a line");
2062        assert_eq!(args[3..6], ["macos", "13.4", "15.2"]);
2063        assert!(args.contains(&"-dylib".to_owned()), "{args:?}");
2064        assert!(!args.iter().any(|arg| arg.contains("pie")), "{args:?}");
2065    }
2066
2067    #[test]
2068    fn the_sdk_version_comes_from_its_name_when_it_has_no_settings() {
2069        let sdk = an_sdk("MacOSX14.5.sdk", None);
2070        let opts =
2071            LinkOptions { sysroot: Some(sdk), no_builtins_lib: true, ..LinkOptions::default() };
2072        let args = line(mac(), &opts, &one("a.o"), "a.out").expect("a line");
2073        assert_eq!(args[3..6], ["macos", "11.0", "14.5"]);
2074        // And the deployment target when neither says anything.
2075        let sdk = an_sdk("Somewhere", None);
2076        let opts = LinkOptions {
2077            sysroot: Some(sdk),
2078            os_version: Some(rucc_tuple::Version::major(12)),
2079            ..opts
2080        };
2081        let args = line(mac(), &opts, &one("a.o"), "a.out").expect("a line");
2082        assert_eq!(args[3..6], ["macos", "12", "12"]);
2083    }
2084
2085    #[test]
2086    fn a_mac_link_looks_for_a_mach_o_linker_and_nothing_else() {
2087        assert_eq!(order(mac(), &LinkOptions::default()), ["ld", "ld64.lld"]);
2088    }
2089
2090    #[test]
2091    fn a_static_mac_program_is_refused_and_a_relocatable_one_is_not() {
2092        let sdk = an_sdk("MacOSX.sdk", None);
2093        let opts = LinkOptions { sysroot: Some(sdk), is_static: true, ..LinkOptions::default() };
2094        let error = line(mac(), &opts, &one("a.o"), "a.out").expect_err("no static line");
2095        let Error::Cross { why } = &error else { panic!("{error:?}") };
2096        assert!(why.contains("-static"), "{why}");
2097        let args = relocatable_line(mac(), &LinkOptions::default(), &one("a.o"), "b.o")
2098            .expect("ld64 takes -r");
2099        assert_eq!(args, ["-r", "-arch", "arm64", "-o", "b.o", "a.o"]);
2100    }
2101
2102    #[test]
2103    fn the_line_is_printed_the_way_it_would_be_typed() {
2104        let linker = Linker { name: "ld".to_owned(), path: PathBuf::from("/usr/bin/ld") };
2105        let args = ["-o".to_owned(), "a b".to_owned()];
2106        assert_eq!(render(&linker, &args), "/usr/bin/ld -o \"a b\"");
2107    }
2108
2109    #[test]
2110    fn a_linker_that_is_not_there_is_said_by_name() {
2111        let opts = LinkOptions {
2112            use_ld: Some("a-linker-nobody-has".to_owned()),
2113            ..LinkOptions::default()
2114        };
2115        let error = find(linux(), &opts).expect_err("not on this machine");
2116        assert_eq!(error, Error::Named { name: "a-linker-nobody-has".to_owned() });
2117    }
2118    /// A directory with a `libgcc.a` in it, so a test can say what a machine with a gcc on it
2119    /// looks like without needing one.
2120    fn a_gcc_dir(name: &str) -> PathBuf {
2121        let dir = std::env::temp_dir().join(format!("rucc-link-{name}-{}", std::process::id()));
2122        fs::create_dir_all(&dir).expect("a temporary directory");
2123        fs::write(dir.join("libgcc.a"), b"not really an archive").expect("a file in it");
2124        dir
2125    }
2126
2127    #[test]
2128    fn the_c_library_supplies_the_block_routines_and_our_runtime_does_not_displace_them() {
2129        let gcc = a_gcc_dir("order");
2130        let ours = PathBuf::from("/somewhere/librucc_builtins.a");
2131        let args = runtime_items(&LinkOptions::default(), &[gcc], Some(&ours));
2132        let at_libc = args.iter().position(|a| a == "-lc").expect("libc");
2133        let at_ours = args.iter().position(|a| a.ends_with("librucc_builtins.a")).expect("ours");
2134        // glibc's `memcpy` is assembly per microarchitecture and ours is a word at a time loop,
2135        // so on a target that has one, its is the one that should answer.
2136        assert!(at_libc < at_ours, "{args:?}");
2137    }
2138
2139    #[test]
2140    fn a_static_link_puts_them_in_a_group_because_two_of_them_refer_to_each_other() {
2141        let gcc = a_gcc_dir("group");
2142        let opts = LinkOptions { is_static: true, ..LinkOptions::default() };
2143        let args = runtime_items(&opts, &[gcc], None);
2144        assert_eq!(args.first().map(String::as_str), Some("--start-group"), "{args:?}");
2145        assert_eq!(args.last().map(String::as_str), Some("--end-group"), "{args:?}");
2146        // The unwinder, which is what `libc.a` refers to and what a static link fails on without
2147        // it. Issue #277.
2148        assert!(args.contains(&"-lgcc_eh".to_owned()), "{args:?}");
2149    }
2150
2151    #[test]
2152    fn a_dynamic_link_needs_no_group_and_asks_for_the_shared_half_only_if_something_wants_it() {
2153        let gcc = a_gcc_dir("dynamic");
2154        let args = runtime_items(&LinkOptions::default(), &[gcc], None);
2155        assert!(!args.contains(&"--start-group".to_owned()), "{args:?}");
2156        assert!(!args.contains(&"-lgcc_eh".to_owned()), "{args:?}");
2157        let at = args.iter().position(|a| a == "-lgcc_s").expect("the shared half");
2158        assert_eq!(args[at - 1], "--as-needed", "{args:?}");
2159        assert_eq!(args[at + 1], "--no-as-needed", "{args:?}");
2160    }
2161
2162    #[test]
2163    fn our_own_runtime_comes_before_the_machines_because_the_two_are_interchangeable() {
2164        let gcc = a_gcc_dir("ours");
2165        let ours = PathBuf::from("/somewhere/librucc_builtins.a");
2166        let args = runtime_items(&LinkOptions::default(), &[gcc], Some(&ours));
2167        let at_ours = args.iter().position(|a| a.ends_with("librucc_builtins.a")).expect("ours");
2168        let at_gcc = args.iter().position(|a| a == "-lgcc").expect("libgcc");
2169        assert!(at_ours < at_gcc, "{args:?}");
2170    }
2171
2172    #[test]
2173    fn no_builtins_lib_leaves_ours_off_and_keeps_the_machines() {
2174        let gcc = a_gcc_dir("theirs");
2175        let opts = LinkOptions { no_builtins_lib: true, ..LinkOptions::default() };
2176        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
2177        assert!(!args.iter().any(|a| a.ends_with("librucc_builtins.a")), "{args:?}");
2178        // And the machine's half is still decided the same way it was, from the directories
2179        // that are there, which on the machine running this test may be none.
2180        assert!(runtime_items(&opts, &[gcc], None).contains(&"-lgcc".to_owned()));
2181    }
2182
2183    #[test]
2184    fn nodefaultlibs_leaves_the_whole_runtime_off_and_not_only_the_c_library() {
2185        let gcc = a_gcc_dir("none");
2186        let opts = LinkOptions { no_defaultlibs: true, ..LinkOptions::default() };
2187        assert!(runtime_items(&opts, &[gcc], None).is_empty());
2188    }
2189
2190    #[test]
2191    fn a_machine_with_no_gcc_on_it_gets_no_names_for_libraries_that_are_not_there() {
2192        let empty = std::env::temp_dir().join("rucc-link-empty-not-a-gcc");
2193        let args = runtime_items(&LinkOptions::default(), &[empty], None);
2194        assert_eq!(args, ["-lc"], "{args:?}");
2195    }
2196
2197    #[test]
2198    fn a_gcc_version_directory_is_read_as_a_version_and_not_as_a_word() {
2199        assert!(version_key(Path::new("/usr/lib/gcc/x/13")) > version_key(Path::new("/x/9")));
2200        assert!(version_key(Path::new("/x/10.2")) > version_key(Path::new("/x/10")));
2201        // Something that is not a version at all still sorts, and sorts below one that is.
2202        assert!(version_key(Path::new("/x/snapshot")) < version_key(Path::new("/x/1")));
2203    }
2204
2205    /// A command line that has a cache to find generated sysroots in, which a real one always has.
2206    ///
2207    /// And a `-B` prefix with our runtime in it, because a cross link refuses without one and
2208    /// every machine that does this for real has the archive `cargo xtask builtins` wrote. What
2209    /// happens when it is missing is its own test below.
2210    /// The fast math startup file follows gcc's end file spec: the family puts it in anything
2211    /// that is not a shared object, and `-mdaz-ftz` decides it outright either way.
2212    #[test]
2213    fn the_fast_math_startup_file_is_wanted_where_gccs_spec_wants_it() {
2214        let fast = LinkOptions { fast_math: true, ..LinkOptions::default() };
2215        assert!(!LinkOptions::default().wants_fastmath());
2216        assert!(fast.wants_fastmath());
2217        assert!(!LinkOptions { shared: true, ..fast.clone() }.wants_fastmath());
2218        assert!(!LinkOptions { daz_ftz: Some(false), ..fast.clone() }.wants_fastmath());
2219        let forced = LinkOptions { shared: true, daz_ftz: Some(true), ..LinkOptions::default() };
2220        assert!(forced.wants_fastmath());
2221    }
2222
2223    fn cached() -> LinkOptions {
2224        LinkOptions {
2225            cache: Some(PathBuf::from("/cache")),
2226            prefixes: vec![a_builtins_dir()],
2227            ..LinkOptions::default()
2228        }
2229    }
2230
2231    /// A directory with our runtime archive in it, so that a test can say what a machine where the
2232    /// runtime was built looks like without building one.
2233    ///
2234    /// One directory for every test rather than one each, since none of them writes to it and the
2235    /// name of the file is the whole of what they read.
2236    fn a_builtins_dir() -> PathBuf {
2237        let dir = std::env::temp_dir().join(format!("rucc-link-ours-{}", std::process::id()));
2238        fs::create_dir_all(&dir).expect("a temporary directory");
2239        fs::write(dir.join("librucc_builtins.a"), b"not really an archive").expect("a file in it");
2240        dir
2241    }
2242
2243    /// An archive in a directory named by the short tuple is found, which is where `cargo xtask
2244    /// builtins --target=aarch64-linux-musl` puts it.
2245    #[test]
2246    fn the_runtime_is_found_under_the_tuple_as_well_as_the_triple() {
2247        let dir = std::env::temp_dir().join(format!("rucc-link-tuple-{}", std::process::id()));
2248        let target: Triple = "aarch64-linux-musl".parse().expect("a triple");
2249        let under = dir.join(target.tuple().to_string());
2250        fs::create_dir_all(&under).expect("a temporary directory");
2251        fs::write(under.join("librucc_builtins.a"), b"not really an archive")
2252            .expect("a file in it");
2253        let found = builtins_archive(target, std::slice::from_ref(&dir));
2254        assert_eq!(found, Some(under.join("librucc_builtins.a")));
2255        let _ = fs::remove_dir_all(&dir);
2256    }
2257
2258    /// Where that cache would keep this target's sysroot.
2259    fn a_sysroot(target: Triple) -> Sysroot {
2260        Sysroot::in_cache(Path::new("/cache"), target.tuple())
2261    }
2262
2263    /// A target that is not the machine running this test, whatever machine that is.
2264    ///
2265    /// A freestanding one, because [`Triple::host`] answers Linux, Darwin or Windows and never
2266    /// `Os::None`. Every other triple is somebody's host, so a test that wants the cross path out of
2267    /// [`line`] itself has to use this one and the rest go through [`cross_line`].
2268    fn foreign() -> Triple {
2269        Triple::new(Arch::X86_64, Os::None, Env::None)
2270    }
2271
2272    #[test]
2273    fn a_cross_link_reads_the_targets_own_sysroot_and_nothing_of_this_machine() {
2274        let target = Triple::new(Arch::Aarch64, Os::Linux, Env::Musl);
2275        let sysroot = a_sysroot(target);
2276        // The paths as this host spells them, because what is being checked is which directory the
2277        // files are in and a Windows separator is a backslash.
2278        let root = sysroot.root().display().to_string();
2279        let lib = sysroot.lib();
2280        let args = cross_line(target, &cached(), &one("a.o"), "a.out", &sysroot).expect("a line");
2281        assert!(args.contains(&format!("--sysroot={root}")), "{args:?}");
2282        assert!(args.contains(&format!("-L{}", lib.display())), "{args:?}");
2283        assert!(args.contains(&lib.join("libc.a").display().to_string()), "{args:?}");
2284        let at = args.iter().position(|a| a == "-dynamic-linker").expect("the loader");
2285        assert_eq!(args[at + 1], "/lib/ld-musl-aarch64.so.1", "{args:?}");
2286        // The whole point of the other path not being taken: not one directory of this machine is
2287        // on the line, so the line is the same on every host and the recorded ones describe it.
2288        for arg in &args {
2289            assert!(!arg.contains("/usr/lib"), "{arg} in {args:?}");
2290            assert!(!arg.contains("/lib64"), "{arg} in {args:?}");
2291        }
2292    }
2293
2294    #[test]
2295    fn a_freestanding_target_links_against_our_runtime_instead_of_being_refused() {
2296        let args = line(foreign(), &cached(), &one("a.o"), "a.out").expect("a line");
2297        assert!(args.iter().any(|a| a.ends_with("librucc_builtins.a")), "{args:?}");
2298        // No libc, because there is not one, and no start file either: what runs before `main` on a
2299        // freestanding target comes from whatever is being built.
2300        assert!(!args.iter().any(|a| a.ends_with("libc.a")), "{args:?}");
2301        assert!(!args.contains(&"-lc".to_owned()), "{args:?}");
2302        assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
2303    }
2304
2305    /// And with nothing to find sysroots in it is refused, which is what it was before this.
2306    #[test]
2307    fn a_driver_with_no_cache_to_look_in_says_so_rather_than_guessing() {
2308        let error = line(foreign(), &LinkOptions::default(), &one("a.o"), "a.out")
2309            .expect_err("no line for it");
2310        assert!(matches!(error, Error::Target { .. }), "{error:?}");
2311    }
2312
2313    #[test]
2314    fn a_static_link_against_a_libc_that_is_a_stub_is_refused_rather_than_attempted() {
2315        let target = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
2316        let opts = LinkOptions { is_static: true, ..cached() };
2317        let error = cross_line(target, &opts, &one("a.o"), "a.out", &a_sysroot(target))
2318            .expect_err("there is no libc.a in a stub sysroot");
2319        let Error::Cross { why } = &error else { panic!("{error:?}") };
2320        // Because a stub carries the names a library exports and none of the bodies, which is
2321        // everything a dynamic link reads and nothing a static one does.
2322        assert!(why.contains("stub"), "{why}");
2323    }
2324
2325    #[test]
2326    fn a_target_whose_linker_wants_a_different_line_is_refused_by_name() {
2327        for target in [
2328            Triple::new(Arch::Aarch64, Os::Darwin, Env::None),
2329            Triple::new(Arch::X86_64, Os::Windows, Env::Msvc),
2330        ] {
2331            let error = cross_line(target, &cached(), &one("a.o"), "a.out", &a_sysroot(target))
2332                .expect_err("no line for that format");
2333            let Error::Cross { why } = &error else { panic!("{error:?}") };
2334            assert!(why.contains(&target.tuple().to_canonical_string()), "{why}");
2335        }
2336    }
2337
2338    #[test]
2339    fn a_mingw_target_links_and_looks_for_a_linker_that_can_write_a_pe_image() {
2340        let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2341        let args = cross_line(target, &cached(), &one("a.o"), "a.exe", &a_sysroot(target))
2342            .expect("a line for mingw-w64");
2343        let at = |flag: &str| args.iter().position(|arg| arg == flag).expect(flag);
2344        assert_eq!(args[at("-m") + 1], "i386pep");
2345        assert_eq!(args[at("--subsystem") + 1], "console");
2346        assert!(args.iter().any(|arg| arg.ends_with("libmsvcrt.a")), "{args:?}");
2347        // And the prefixed name a distribution files its mingw binutils under, which is not the
2348        // multiarch one.
2349        let names = cross_order(target);
2350        assert_eq!(names.first().map(String::as_str), Some("ld.lld"));
2351        assert!(names.contains(&"x86_64-w64-mingw32-ld".to_owned()), "{names:?}");
2352    }
2353
2354    #[test]
2355    fn our_runtime_comes_from_beside_the_compiler_rather_than_from_inside_the_sysroot() {
2356        // The two halves of tamnd/rucc#1514. The line used to name it under the sysroot's `lib`,
2357        // where nothing ever put it: it is this compiler's output for the target and a sysroot
2358        // fetched from a release holds the platform's files and not ours. So the path on the line
2359        // is the one the driver found, and the only `librucc_builtins.a` on the line is that one.
2360        let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2361        let sysroot = a_sysroot(target);
2362        let opts = cached();
2363        let args = cross_line(target, &opts, &one("a.o"), "a.exe", &sysroot).expect("a line");
2364        let ours: Vec<&String> =
2365            args.iter().filter(|arg| arg.ends_with("librucc_builtins.a")).collect();
2366        assert_eq!(ours.len(), 1, "{args:?}");
2367        assert_eq!(ours[0], &opts.prefixes[0].join("librucc_builtins.a").display().to_string());
2368        assert!(!ours[0].starts_with(&sysroot.lib().display().to_string()), "{args:?}");
2369        // And it is still last, after everything that calls into it.
2370        assert_eq!(args.last(), Some(ours[0]), "{args:?}");
2371    }
2372
2373    #[test]
2374    fn a_cross_link_with_no_runtime_to_find_says_which_command_writes_one() {
2375        // What the linker would say instead is that `___chkstk_ms` is undefined, referenced from
2376        // mingw-w64's own objects, which is tamnd/rucc#1513 and is nobody's first guess at a
2377        // missing archive.
2378        let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2379        let opts = LinkOptions { prefixes: Vec::new(), ..cached() };
2380        let error = cross_line(target, &opts, &one("a.o"), "a.exe", &a_sysroot(target))
2381            .expect_err("there is no runtime for it to find");
2382        let Error::Cross { why } = &error else { panic!("{error:?}") };
2383        assert!(why.contains("cargo xtask builtins"), "{why}");
2384        assert!(why.contains("-fno-builtins-lib"), "{why}");
2385
2386        // And that flag is the way through it, for somebody who meant to link without ours.
2387        let without = LinkOptions { no_builtins_lib: true, ..opts };
2388        let args = cross_line(target, &without, &one("a.o"), "a.exe", &a_sysroot(target))
2389            .expect("a line without ours on it");
2390        assert!(!args.iter().any(|arg| arg.ends_with("librucc_builtins.a")), "{args:?}");
2391    }
2392
2393    #[test]
2394    fn the_version_an_lld_prints_is_read_and_nothing_elses_is() {
2395        // What each of these programs actually prints, because the word being in the line is the
2396        // whole of how one is told from another.
2397        assert_eq!(lld_major("LLD 18.1.8 (compatible with GNU linkers)\n"), Some(18));
2398        assert_eq!(lld_major("Ubuntu LLD 18.1.3 (compatible with GNU linkers)\n"), Some(18));
2399        assert_eq!(lld_major("LLD 20.1.2 (compatible with GNU linkers)\n"), Some(20));
2400
2401        // Binutils and mold do not have it, and neither of them has this problem, so the answer
2402        // for both is that this check has nothing to say about them.
2403        assert_eq!(lld_major("GNU ld (GNU Binutils for Ubuntu) 2.42\n"), None);
2404        assert_eq!(lld_major("mold 2.4.1 (compatible with GNU ld)\n"), None);
2405        assert_eq!(lld_major(""), None);
2406    }
2407
2408    /// A program that prints `text` and exits, which is as much of a linker as this check reads.
2409    ///
2410    /// Named after what it says, so that two of them in one test are two files.
2411    #[cfg(unix)]
2412    fn a_linker_that_says(tag: &str, text: &str) -> Linker {
2413        use std::os::unix::fs::PermissionsExt as _;
2414        let dir = std::env::temp_dir().join(format!("rucc-link-ld-{}", std::process::id()));
2415        fs::create_dir_all(&dir).expect("a temporary directory");
2416        let path = dir.join(format!("ld.lld-{tag}"));
2417        fs::write(&path, format!("#!/bin/sh\necho '{text}'\n")).expect("a script");
2418        fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).expect("an executable one");
2419        Linker { name: "ld.lld".to_owned(), path }
2420    }
2421
2422    #[test]
2423    #[cfg(unix)]
2424    fn an_lld_too_old_to_read_exportas_is_refused_for_windows_gnu_and_nowhere_else() {
2425        // The failure this replaces has no diagnostic at all: 18 writes an import by ordinal zero,
2426        // exits successfully, and the program dies at startup under wine. tamnd/rucc#1515.
2427        let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2428        let old = a_linker_that_says("18", "LLD 18.1.8 (compatible with GNU linkers)");
2429        let error = suitable(windows, &old).expect_err("18 cannot link this");
2430        let Error::TooOld { name, found, target } = &error else { panic!("{error:?}") };
2431        assert_eq!((name.as_str(), *found, target.as_str()), ("ld.lld", 18, "x86_64-windows-gnu"));
2432        assert!(error.to_string().contains("IMPORT_NAME_EXPORTAS"), "{error}");
2433
2434        // The same linker for a target whose import libraries have no such records in them, which
2435        // is every other target, since this is one encoding in one format.
2436        let linux = Triple::new(Arch::X86_64, Os::Linux, Env::Musl);
2437        assert_eq!(suitable(linux, &old), Ok(()));
2438
2439        // And the first one that reads them.
2440        let new = a_linker_that_says("19", "LLD 19.1.0 (compatible with GNU linkers)");
2441        assert_eq!(suitable(windows, &new), Ok(()));
2442    }
2443
2444    #[test]
2445    #[cfg(unix)]
2446    fn an_old_lld_first_in_line_is_passed_over_for_a_newer_one_behind_it() {
2447        // Ubuntu 24.04 after `apt install lld-19`: 18 is the one on PATH and 19 is somewhere else.
2448        // Two -B prefixes stand in for the two places, since the search asks them in order too. The
2449        // name is one nothing on this machine is called, so a real lld on PATH does not answer.
2450        use std::os::unix::fs::PermissionsExt as _;
2451        let root = std::env::temp_dir().join(format!("rucc-link-two-lld-{}", std::process::id()));
2452        let mut prefixes = Vec::new();
2453        for (dir, version) in [("old", "18.1.3"), ("new", "19.1.7")] {
2454            let dir = root.join(dir);
2455            fs::create_dir_all(&dir).expect("a temporary directory");
2456            let path = dir.join("ld.rucc-test-lld");
2457            fs::write(&path, format!("#!/bin/sh\necho 'Ubuntu LLD {version}'\n"))
2458                .expect("a script");
2459            fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).expect("executable");
2460            prefixes.push(dir);
2461        }
2462        let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2463        let opts = LinkOptions {
2464            use_ld: Some("rucc-test-lld".to_owned()),
2465            prefixes,
2466            ..LinkOptions::default()
2467        };
2468        let found = find(windows, &opts).expect("the newer one");
2469        assert_eq!(found.path, root.join("new").join("ld.rucc-test-lld"));
2470
2471        // With only the old one there, the answer is the refusal that names it.
2472        let opts = LinkOptions { prefixes: vec![root.join("old")], ..opts };
2473        let error = find(windows, &opts).expect_err("only 18 is here");
2474        assert!(matches!(error, Error::TooOld { found: 18, .. }), "{error:?}");
2475        let _ = fs::remove_dir_all(&root);
2476    }
2477
2478    #[test]
2479    fn a_long_line_goes_in_a_response_file_that_reads_back_as_it_was() {
2480        let args: Vec<String> =
2481            ["-o", r"C:\Users\a b\out.exe", "-m", "i386pep", r#"say "hi""#, "", "plain.o"]
2482                .map(str::to_owned)
2483                .to_vec();
2484        let (front, rest) = split_for_file(&args);
2485        assert_eq!(front, ["-m", "i386pep"]);
2486        assert_eq!(
2487            crate::response_words(&response_text(&rest, false)),
2488            [&args[..2], &args[4..]].concat()
2489        );
2490        // And the Windows way, which leaves the backslashes in a path alone.
2491        assert_eq!(
2492            response_text(&rest, true),
2493            "\"-o\"\n\"C:\\Users\\a b\\out.exe\"\n\"say \\\"hi\\\"\"\n\"\"\n\"plain.o\"\n"
2494        );
2495        assert_eq!(response_text(&[r"C:\dir\".to_owned()], true), "\"C:\\dir\\\\\"\n");
2496        assert!(!too_long(&args, 1000));
2497        assert!(too_long(&vec!["x".repeat(100); 400], WINDOWS_LINE));
2498    }
2499
2500    #[test]
2501    fn a_program_name_is_tried_with_each_pathext_extension_it_does_not_already_have() {
2502        let exts = vec![".com".to_owned(), ".exe".to_owned()];
2503        let dir = Path::new("bin");
2504        assert_eq!(
2505            spellings(&dir.join("ld.lld"), &exts),
2506            [dir.join("ld.lld.com"), dir.join("ld.lld.exe")]
2507        );
2508        assert_eq!(spellings(&dir.join("lld-link.EXE"), &exts), [dir.join("lld-link.EXE")]);
2509        assert_eq!(spellings(&dir.join("ld.lld"), &[]), [dir.join("ld.lld")]);
2510    }
2511
2512    #[test]
2513    #[cfg(unix)]
2514    fn an_lld_off_path_is_found_under_its_own_name() {
2515        use std::os::unix::fs::PermissionsExt as _;
2516        let dir = std::env::temp_dir().join(format!("rucc-link-off-path-{}", std::process::id()));
2517        fs::create_dir_all(&dir).expect("a temporary directory");
2518        let lld = dir.join("ld.lld");
2519        fs::write(&lld, "#!/bin/sh\n").expect("a file");
2520        fs::set_permissions(&lld, fs::Permissions::from_mode(0o755)).expect("permissions");
2521        let found =
2522            linker_candidates("ld.lld", &LinkOptions::default(), std::slice::from_ref(&dir));
2523        let _ = fs::remove_dir_all(&dir);
2524        assert!(found.contains(&lld), "{found:?}");
2525    }
2526
2527    #[test]
2528    fn lld_is_looked_for_where_package_managers_put_it_newest_version_first() {
2529        let root = std::env::temp_dir().join(format!("rucc-link-lld-dirs-{}", std::process::id()));
2530        for dir in ["usr/lib/llvm-18/bin", "usr/lib/llvm-19/bin", "usr/lib/llvm-9/bin", "usr/lib/x"]
2531        {
2532            fs::create_dir_all(root.join(dir)).expect("a temporary directory");
2533        }
2534        let dirs = lld_dirs(&root, Some(PathBuf::from("C:/Program Files")));
2535        let under_lib: Vec<_> =
2536            dirs.iter().filter(|dir| dir.starts_with(root.join("usr/lib"))).collect();
2537        assert_eq!(
2538            under_lib,
2539            [
2540                &root.join("usr/lib/llvm-19/bin"),
2541                &root.join("usr/lib/llvm-18/bin"),
2542                &root.join("usr/lib/llvm-9/bin")
2543            ]
2544        );
2545        assert!(dirs.contains(&root.join("opt/homebrew/opt/lld/bin")), "{dirs:?}");
2546        assert_eq!(dirs.last(), Some(&PathBuf::from("C:/Program Files").join("LLVM").join("bin")));
2547        let _ = fs::remove_dir_all(&root);
2548    }
2549
2550    #[test]
2551    fn no_linker_says_where_to_get_lld_only_when_lld_was_looked_for() {
2552        let cross = Error::NoLinker { tried: vec!["ld.lld".to_owned(), "lld".to_owned()] };
2553        assert!(cross.to_string().contains("lld 19 or newer"), "{cross}");
2554        let native = Error::NoLinker { tried: vec!["ld".to_owned()] };
2555        assert_eq!(native.to_string(), "no linker was found; tried ld");
2556    }
2557
2558    #[test]
2559    #[cfg(unix)]
2560    fn a_linker_that_will_not_say_what_it_is_is_left_alone() {
2561        // Every linker that is not an lld reaches this check too, and what it can establish is
2562        // that a specific old lld is here rather than that anything else is fit. Turning "I did
2563        // not recognise this" into a refusal would break machines this problem never touched.
2564        let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2565        let quiet = a_linker_that_says("gnu", "GNU ld (GNU Binutils for Ubuntu) 2.42");
2566        assert_eq!(suitable(windows, &quiet), Ok(()));
2567
2568        let missing = Linker { name: "ld.lld".to_owned(), path: PathBuf::from("/no/such/linker") };
2569        assert_eq!(suitable(windows, &missing), Ok(()));
2570    }
2571
2572    #[test]
2573    fn profiling_a_cross_link_is_refused_because_the_startup_file_is_compiled_code() {
2574        let target = Triple::new(Arch::X86_64, Os::Linux, Env::Musl);
2575        let opts = LinkOptions { profile: true, ..cached() };
2576        let error = cross_line(target, &opts, &one("a.o"), "a.out", &a_sysroot(target))
2577            .expect_err("there is no gcrt1.o in a generated sysroot");
2578        let Error::Cross { why } = &error else { panic!("{error:?}") };
2579        assert!(why.contains("gcrt1.o"), "{why}");
2580    }
2581
2582    #[test]
2583    fn a_distributions_cross_tree_is_used_when_there_is_no_sysroot_of_ours() {
2584        let usr = std::env::temp_dir().join(format!("rucc-link-usr-{}", std::process::id()));
2585        let root = usr.join("aarch64-linux-gnu");
2586        for dir in ["include", "lib"] {
2587            fs::create_dir_all(root.join(dir)).expect("a scratch tree");
2588        }
2589        for version in ["9", "13"] {
2590            fs::create_dir_all(usr.join("lib/gcc-cross/aarch64-linux-gnu").join(version))
2591                .expect("a scratch gcc");
2592        }
2593        let host = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
2594        let arm = Triple::new(Arch::Aarch64, Os::Linux, Env::Gnu);
2595        let opts = LinkOptions { usr: Some(usr.clone()), ..cached() };
2596        let distro = distro_for(arm, &opts, Some(host)).expect("the packages are there");
2597        assert_eq!(distro.include(), root.join("include"));
2598        assert_eq!(distro.lib(), root.join("lib"));
2599        assert_eq!(
2600            distro.gcc,
2601            [13, 9].map(|v| usr.join("lib/gcc-cross/aarch64-linux-gnu").join(v.to_string()))
2602        );
2603        // And then it is not a link against a sysroot of ours, which is what decides the line.
2604        assert!(cross_for(arm, &opts, Some(host)).is_none());
2605        // The host itself, a target with no tree, a named tree and a pinned release all leave it.
2606        assert!(distro_for(host, &opts, Some(host)).is_none());
2607        let riscv = Triple::new(Arch::Riscv64, Os::Linux, Env::Gnu);
2608        assert!(distro_for(riscv, &opts, Some(host)).is_none());
2609        let named = LinkOptions { sysroot: Some(PathBuf::from("/opt/root")), ..opts.clone() };
2610        assert!(distro_for(arm, &named, Some(host)).is_none());
2611        let pinned = LinkOptions {
2612            pinned: Some("aarch64-linux-gnu.2.28".parse::<TargetTuple>().expect("a release")),
2613            ..opts.clone()
2614        };
2615        assert!(distro_for(arm, &pinned, Some(host)).is_none());
2616        // A sysroot of ours in the cache wins over the packages, because it is the one pinned.
2617        let cache = usr.join("cache");
2618        fs::create_dir_all(Sysroot::in_cache(&cache, arm.tuple()).lib()).expect("a sysroot");
2619        let fetched = LinkOptions { cache: Some(cache), ..opts };
2620        assert!(distro_for(arm, &fetched, Some(host)).is_none());
2621        let _ = fs::remove_dir_all(&usr);
2622    }
2623
2624    #[test]
2625    fn the_host_takes_the_host_line_and_a_tree_the_user_named_takes_it_too() {
2626        let host = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
2627        let other = Triple::new(Arch::Riscv64, Os::Linux, Env::Musl);
2628        assert!(cross_for(host, &cached(), Some(host)).is_none());
2629        assert!(cross_for(other, &cached(), Some(host)).is_some());
2630        // A tree somebody assembled and named is what `--sysroot` has always meant here, and the
2631        // native line prefixes every path it decides with it.
2632        let named = LinkOptions { sysroot: Some(PathBuf::from("/opt/root")), ..cached() };
2633        assert!(cross_for(other, &named, Some(host)).is_none());
2634        // A host this compiler cannot name is a host whose directories it should not be guessing at.
2635        assert!(cross_for(other, &cached(), None).is_some());
2636    }
2637
2638    #[test]
2639    fn a_windows_host_compiles_for_itself_against_the_fetched_tree() {
2640        // There is no `/usr/include` on Windows, so the host line would find no `stdio.h` at all.
2641        let host = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2642        let at =
2643            cross_for(host, &cached(), Some(host)).expect("the cache is the only tree there is");
2644        assert!(at.root().ends_with("x86_64-windows-gnu"), "{:?}", at.root());
2645    }
2646
2647    #[test]
2648    fn a_pinned_release_on_this_machines_own_target_is_a_cross_compile() {
2649        // The case that used to be dropped on the floor. `--target=x86_64-linux-gnu.2.28` on an
2650        // x86-64 glibc machine read that machine's headers and linked that machine's libc, and the
2651        // release reached nothing, so what came out was a binary for whatever release the build
2652        // machine happened to have. A pin is the one thing a person writes to say otherwise.
2653        let host = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
2654        let pinned = LinkOptions {
2655            pinned: Some(
2656                "x86_64-linux-gnu.2.28".parse::<TargetTuple>().expect("a spelling with a release"),
2657            ),
2658            ..cached()
2659        };
2660        let at = cross_for(host, &pinned, Some(host)).expect("a pin is a cross compile");
2661        // And against the release's own directory, because the release is in the cache key: a tree
2662        // produced for 2.28 and a tree produced for 2.44 are two trees and the path has to say which.
2663        assert!(at.root().ends_with("x86_64-linux-gnu.2.28"), "{:?}", at.root());
2664        // The release is the whole of the difference. The same command line without it is this
2665        // machine, which is what every native compile has always been.
2666        let bare = LinkOptions { pinned: None, ..cached() };
2667        assert!(cross_for(host, &bare, Some(host)).is_none());
2668    }
2669
2670    #[test]
2671    fn a_glibc_cross_link_writes_its_stubs_beside_the_sysroot_once() {
2672        let cache = std::env::temp_dir().join(format!("rucc-link-stubs-{}", std::process::id()));
2673        let _ = fs::remove_dir_all(&cache);
2674        let target = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
2675        let pinned = LinkOptions {
2676            cache: Some(cache.clone()),
2677            pinned: Some("x86_64-linux-gnu.2.28".parse().expect("a spelling with a release")),
2678            ..LinkOptions::default()
2679        };
2680        write_stubs(target, &pinned).expect("x86_64 glibc has a description");
2681        let dir = cache.join("stubs").join("x86_64-linux-gnu.2.28");
2682        let libc = dir.join("libc.so");
2683        let bytes = fs::read(&libc).expect("libc.so was written");
2684        assert!(bytes.starts_with(b"\x7fELF"));
2685        assert!(dir.join("libm.so").is_file());
2686        // 2.28 is before the release that emptied libpthread, so there is no empty one to write.
2687        assert!(!dir.join("libpthread.so").exists());
2688        // The second time finds the same bytes and leaves the file alone, which is what keeps a
2689        // linker in another build from ever reading one that is being replaced.
2690        let before = fs::metadata(&libc).and_then(|m| m.modified()).expect("a time");
2691        write_stubs(target, &pinned).expect("again");
2692        let after = fs::metadata(&libc).and_then(|m| m.modified()).expect("a time");
2693        assert_eq!(before, after);
2694        // And nothing for a libc that is not glibc, whose sysroot has a real one in it.
2695        let musl = LinkOptions {
2696            cache: Some(cache.clone()),
2697            pinned: Some("x86_64-linux-musl".parse().expect("musl")),
2698            ..LinkOptions::default()
2699        };
2700        write_stubs(Triple::new(Arch::X86_64, Os::Linux, Env::Musl), &musl).expect("nothing");
2701        assert!(!cache.join("stubs").join("x86_64-linux-musl").exists());
2702        let _ = fs::remove_dir_all(&cache);
2703    }
2704
2705    #[test]
2706    fn what_a_cross_link_searches_is_the_sysroot_and_not_this_machine() {
2707        let dirs = search_dirs(&cached(), foreign());
2708        // One directory, because that is what the line has, and the same one the line has, because
2709        // `-print-search-dirs` is what a build system reads to write a link line of its own.
2710        assert_eq!(dirs.len(), 1, "{dirs:?}");
2711        assert!(dirs[0].starts_with("/cache"), "{dirs:?}");
2712        assert!(dirs[0].ends_with("lib"), "{dirs:?}");
2713        // And what the user wrote still comes first, the way it does on the line itself.
2714        let mine = LinkOptions { search: vec![PathBuf::from("/opt/mine")], ..cached() };
2715        assert_eq!(search_dirs(&mine, foreign())[0], PathBuf::from("/opt/mine"));
2716    }
2717
2718    #[test]
2719    fn gnu_ld_is_not_looked_for_against_the_fetched_mingw_sysroot() {
2720        let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
2721        assert_eq!(order(windows, &cached()), ["ld.lld", "lld"]);
2722    }
2723
2724    #[test]
2725    fn the_linker_looked_for_on_a_cross_link_is_one_that_can_cross() {
2726        let names = cross_order(Triple::new(Arch::Aarch64, Os::Linux, Env::Gnu));
2727        assert_eq!(names.first().map(String::as_str), Some("ld.lld"));
2728        assert!(names.contains(&"aarch64-linux-gnu-ld".to_owned()), "{names:?}");
2729        // mold links for the machine it is running on, and so does a distribution's own `ld`, so
2730        // neither is a default here. `-fuse-ld=` is still there for somebody whose is different.
2731        assert!(!names.iter().any(|name| name.contains("mold")), "{names:?}");
2732        assert!(!names.contains(&"ld".to_owned()), "{names:?}");
2733        // And the lookup the driver really does for a target that is not this machine.
2734        assert_eq!(order(foreign(), &cached()), ["ld.lld", "lld"]);
2735    }
2736
2737    #[test]
2738    fn the_four_flags_become_the_five_modes_they_describe() {
2739        let plain = LinkOptions::default();
2740        assert_eq!(mode(&plain), LinkMode::Dynamic);
2741        assert_eq!(
2742            mode(&LinkOptions { pie: Some(false), ..plain.clone() }),
2743            LinkMode::DynamicNoPie
2744        );
2745        assert_eq!(mode(&LinkOptions { is_static: true, ..plain.clone() }), LinkMode::Static);
2746        let both = LinkOptions { is_static: true, pie: Some(true), ..plain.clone() };
2747        assert_eq!(mode(&both), LinkMode::StaticPie);
2748        assert_eq!(mode(&LinkOptions { shared: true, ..plain }), LinkMode::Shared);
2749    }
2750
2751    #[test]
2752    fn a_sysroot_that_has_not_been_built_is_named_before_anything_is_compiled() {
2753        let opts = LinkOptions {
2754            cache: Some(std::env::temp_dir().join("rucc-a-cache-nobody-filled")),
2755            ..LinkOptions::default()
2756        };
2757        let error = preflight(foreign(), &opts).expect_err("nothing has built one");
2758        let Error::Sysroot { dir, pinned, .. } = &error else { panic!("{error:?}") };
2759        assert!(dir.ends_with("x86_64-none"), "{dir}");
2760        // Nothing is pinned for that target, or for any target yet, so the message says that rather
2761        // than naming a command that would not work.
2762        assert!(!pinned, "nothing should be pinned for a bare metal target");
2763        let said = error.to_string();
2764        assert!(said.contains("pins none for it to fetch"), "{said}");
2765    }
2766
2767    /// The other half of the same message, which is what a target this release does pin an artifact
2768    /// for is told. Built by hand rather than through `preflight`, because what is being checked is
2769    /// the message and not which targets `rucc_sysroot::artifact` happens to pin this release.
2770    #[test]
2771    fn a_sysroot_that_could_be_fetched_is_told_what_to_run() {
2772        let said = Error::Sysroot {
2773            target: "x86_64-linux-musl".to_owned(),
2774            dir: "/somewhere/sysroots/x86_64-linux-musl".to_owned(),
2775            pinned: true,
2776        }
2777        .to_string();
2778        assert!(said.contains("`rucc --fetch x86_64-linux-musl`"), "{said}");
2779        // And the other way out of it, because a person who has a tree already does not want a
2780        // download.
2781        assert!(said.contains("--sysroot=<dir>"), "{said}");
2782    }
2783
2784    #[test]
2785    fn a_link_against_this_machine_has_nothing_to_check_before_it_starts() {
2786        // Its directories are looked for as the line is built, and one that is not there is simply
2787        // one that is not offered, so there is no question to answer early.
2788        assert!(preflight(linux(), &LinkOptions::default()).is_ok());
2789    }
2790
2791    #[test]
2792    fn a_runtime_directory_that_is_not_on_this_machine_is_not_offered() {
2793        let dirs = runtime_dirs(linux(), Some(Path::new("/definitely/not/a/sysroot")));
2794        assert!(dirs.is_empty(), "{dirs:?}");
2795    }
2796}