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//! [`Manifest::digest`] is the same comparison in one line, which is what the cache layout of
51//! `spec/cross-compile/13-distribution.md` section 13.2 wanted a hash in a directory's name for.
52//!
53//! # What is not in here
54//!
55//! Nothing fetches. Downloading musl, verifying it and unpacking it is
56//! `spec/cross-compile/13-distribution.md`, and the network policy it needs is that document's
57//! section 13.8: the bytes are moved by a downloader the machine already has, and the hash check,
58//! the manifest and the rename into place are ours. None of those three is in this crate either.
59//! What this crate settles is where the result goes and how it is searched, which is the part that
60//! has to be decided before anything is worth downloading.
61//!
62//! ```
63//! use rucc_sysroot::{Sysroot, LinkLine, LinkMode, argv};
64//! use rucc_tuple::TargetTuple;
65//! use std::path::Path;
66//!
67//! let target: TargetTuple = "aarch64-linux-musl".parse().unwrap();
68//! let sysroot = Sysroot::in_cache(Path::new("/cache"), target);
69//!
70//! // The cache key is the canonical spelling, so two hosts asking for the same target ask for
71//! // the same directory.
72//! assert_eq!(sysroot.cache_key(), "aarch64-linux-musl");
73//! assert_eq!(sysroot.root(), Path::new("/cache/sysroots/aarch64-linux-musl"));
74//!
75//! // A static link needs three start files, and `crtn.o` goes after the libraries rather than
76//! // with the other two.
77//! let line = LinkLine::musl(&sysroot, LinkMode::Static);
78//! assert_eq!(line.start.last().unwrap().file_name().unwrap(), "crti.o");
79//! assert_eq!(line.end.first().unwrap().file_name().unwrap(), "crtn.o");
80//!
81//! // And the whole linker command line, which names nothing on this machine.
82//! let options = argv::Invocation { mode: LinkMode::Static, ..Default::default() };
83//! let line = argv::argv(target, &sysroot, &options).unwrap();
84//! assert!(line.contains(&"-static".to_owned()));
85//! assert!(line.contains(&"-m".to_owned()) && line.contains(&"aarch64linux".to_owned()));
86//! ```
87
88#![doc(html_root_url = "https://docs.rs/rucc-sysroot/0.10.23")]
89// Every public item here is read by somebody bringing up a target, and an undocumented one is a
90// question they have to answer by reading the body.
91#![deny(missing_docs)]
92
93pub mod argv;
94pub mod layout;
95pub mod link;
96pub mod manifest;
97pub mod search;
98mod sha256;
99
100pub use argv::{Invocation, Item, Unsupported};
101pub use layout::{BUNDLED_GLIBC, GlibcSkew, Kernel, Sysroot, bundled_glibc_minor};
102pub use link::{Libc, LinkLine, LinkMode, libc};
103pub use manifest::{Input, Licence, Manifest, ManifestError, Provenance};
104pub use search::{Entry, Options, Origin, include_paths};
105
106#[doc(inline)]
107pub use rucc_tuple::TargetTuple;