rucc_sysroot/lib.rs
1//! Where a target's headers and link inputs live, which directories are searched for them, and in
2//! what order they reach the linker.
3//!
4//! Design: `spec/cross-compile/08-sysroots.md`, and section 8.5 for the rule this crate exists to
5//! enforce.
6//!
7//! # The claim this crate is responsible for
8//!
9//! `spec/cross-compile/02-the-goal.md` claim 5 is byte identical output from different hosts for
10//! the same target. It holds only because the search path rules make the libc header directory a
11//! function of the target rather than of the machine, and that function is here.
12//!
13//! So every answer in this crate is derived from a [`TargetTuple`] and from paths the caller
14//! supplies. Nothing reads an environment variable, nothing looks at `std::env::consts`, and
15//! nothing touches the filesystem. A host directory can only enter through
16//! [`Options::host_include`], which [`include_paths`] uses exactly once and only when the target
17//! is the host, and that is checkable by reading one function.
18//!
19//! # What is in here
20//!
21//! [`Sysroot`] is the directory layout for one target: where its headers go, where its link inputs
22//! go, and where the record of what they are goes. Its root is a function of a cache directory and
23//! the tuple, which is what makes the tuple a cache key.
24//!
25//! [`Kernel`] is the other half of a Linux target's headers. `linux/` and `asm/` are the system
26//! call interface rather than the C library, 31 of glibc's installed headers and 3 of musl's
27//! include one of them, and they are the same files for every target that shares an architecture.
28//! So they sit in the cache rather than in a sysroot and a Linux target searches four directories.
29//!
30//! [`bundled_glibc_minor`] is the version half of the same tree. One tree serves every glibc
31//! release, with the differences inside the files as `#if __GLIBC_MINOR__ >= n`, so the release is
32//! what the target supplies and the compiler defines the macro. It answers [`None`] for every libc
33//! that has no such macro and an error for a release newer than the tree, which is the one direction
34//! that cannot be approximated.
35//!
36//! [`include_paths`] is section 8.5 as an ordered list, with each entry saying which of the four
37//! steps put it there. [`LinkLine`] is the start files, the libraries and the end files, in the
38//! order a linker needs them, for either libc.
39//!
40//! [`argv`] is `spec/cross-compile/11-linking.md` section 11.3: the whole linker command line as a
41//! function of the target, the sysroot and what the user asked for. It is the same division as the
42//! one above, one level up. [`LinkLine`] is what has to be linked, which is a fact about the target,
43//! and [`argv`] is how that is spelled for a linker, which is a fact about the linker. Section 11.3
44//! asks for a golden file per target and `tests/link-lines` is it, one file per target, regenerated
45//! by `cargo xtask link-lines` and checked in CI.
46//!
47//! [`Manifest`] is what a produced sysroot carries: every input with where it came from, its hash
48//! and its licence. Two sysroots for the same target built on two hosts have the same manifest, and
49//! comparing manifests is how that gets checked without comparing several thousand files.
50//!
51//! # What is not in here
52//!
53//! Nothing fetches. Downloading musl, verifying it and unpacking it is
54//! `spec/cross-compile/13-distribution.md`, and it needs a cache, a provenance record and a network
55//! policy that this crate deliberately has no opinion about. What this crate settles is where the
56//! result goes and how it is searched, which is the part that has to be decided before anything is
57//! worth downloading.
58//!
59//! ```
60//! use rucc_sysroot::{Sysroot, LinkLine, LinkMode, argv};
61//! use rucc_tuple::TargetTuple;
62//! use std::path::Path;
63//!
64//! let target: TargetTuple = "aarch64-linux-musl".parse().unwrap();
65//! let sysroot = Sysroot::in_cache(Path::new("/cache"), target);
66//!
67//! // The cache key is the canonical spelling, so two hosts asking for the same target ask for
68//! // the same directory.
69//! assert_eq!(sysroot.cache_key(), "aarch64-linux-musl");
70//! assert_eq!(sysroot.root(), Path::new("/cache/sysroots/aarch64-linux-musl"));
71//!
72//! // A static link needs three start files, and `crtn.o` goes after the libraries rather than
73//! // with the other two.
74//! let line = LinkLine::musl(&sysroot, LinkMode::Static);
75//! assert_eq!(line.start.last().unwrap().file_name().unwrap(), "crti.o");
76//! assert_eq!(line.end.first().unwrap().file_name().unwrap(), "crtn.o");
77//!
78//! // And the whole linker command line, which names nothing on this machine.
79//! let options = argv::Invocation { mode: LinkMode::Static, ..Default::default() };
80//! let line = argv::argv(target, &sysroot, &options).unwrap();
81//! assert!(line.contains(&"-static".to_owned()));
82//! assert!(line.contains(&"-m".to_owned()) && line.contains(&"aarch64linux".to_owned()));
83//! ```
84
85#![doc(html_root_url = "https://docs.rs/rucc-sysroot/0.10.21")]
86// Every public item here is read by somebody bringing up a target, and an undocumented one is a
87// question they have to answer by reading the body.
88#![deny(missing_docs)]
89
90pub mod argv;
91pub mod layout;
92pub mod link;
93pub mod manifest;
94pub mod search;
95
96pub use argv::{Invocation, Item, Unsupported};
97pub use layout::{BUNDLED_GLIBC, GlibcSkew, Kernel, Sysroot, bundled_glibc_minor};
98pub use link::{Libc, LinkLine, LinkMode, libc};
99pub use manifest::{Input, Licence, Manifest, ManifestError, Provenance};
100pub use search::{Entry, Options, Origin, include_paths};
101
102#[doc(inline)]
103pub use rucc_tuple::TargetTuple;