rucc_sysroot/artifact.rs
1//! What this release pins: one sysroot artifact per target, by URL and by hash.
2//!
3//! Design: `spec/cross-compile/13-distribution.md` section 13.2, which says every downloaded
4//! artifact has a hash pinned in the rucc release, checked before use, with a mismatch being a hard
5//! failure and no flag to get past it. Section 13.8 divides the work in three and the other two are
6//! written: `rucc_driver::fetch` moves the bytes with a program the machine already has, and
7//! `rucc_driver::install` decides whether what arrived is the right tree. This is the third, which
8//! is the statement of what the right one is, and it is the half that makes the other two mean
9//! anything.
10//!
11//! # Why it is here rather than with the two halves that use it
12//!
13//! Because it is read by something that cannot depend on the driver. The distribution manifest of
14//! section 13.5 is generated by a build tool, the same way `docs/TARGETS.md` and `tests/link-lines`
15//! are, and what it says about a target is what this table says plus what [`crate::Wall`] says. A
16//! build tool that pulled in the whole driver to read three strings would be a layer violation
17//! dressed up as convenience. It sits well here for a second reason as well: a pin is a fact about
18//! a sysroot, which is what this crate is for, and the fetch and the install are what a driver does
19//! with one.
20//!
21//! # Why the table is in the binary
22//!
23//! Because a hash that travels with the artifact is not a pin, and a hash in a file beside the
24//! compiler is a hash whoever replaces the artifact can replace too. The release is the authority
25//! for what an artifact of that release is, so the table is compiled into the release, which also
26//! means an upgrade can change a URL without anything on the machine having to be told.
27//!
28//! It is a table rather than a computed URL for the same reason. A name built out of a version and
29//! a tuple looks tidier and quietly says that every target's artifact is at a predictable address
30//! forever, which is a promise about somebody else's file server. A row per target costs three
31//! strings and says only what is true.
32//!
33//! # What is in it
34//!
35//! Eighteen rows, which are the three windows-gnu targets, four musl ones, eight glibc ones and three WASI ones. For windows-gnu,
36//! `bin/mingw-headers` in `tamnd/rucc-cross` installs mingw-w64 14.0.0's headers, `bin/mingw-runtime`
37//! builds the runtime, the import libraries and a static winpthreads into the `lib` directory beside
38//! them, `bin/artifact` packs the tree, and the release `sysroots-2026-09-29` is where the files are.
39//! They are built for the UCRT, and the same release has a `-msvcrt` archive of each that nothing
40//! here pins. The archives are 15.2 MiB for x86_64, 14.9 MiB for i686 and 12.6 MiB for aarch64,
41//! installing to 141 MB, 135 MB and 119 MB. The header half is the same in all three, because
42//! mingw-w64 has no per architecture split and [`crate::Sysroot::splits_by_arch`] says so, and the
43//! `lib` half is what differs.
44//!
45//! For musl, `bin/sysroot` builds musl 1.2.5 for x86_64, aarch64, riscv64 and armv7 hard float, and
46//! the release `sysroots-2026-09-23` has the four archives, each about 2 MiB and installing to between
47//! 7 and 13 MB. They hold musl's headers and its static libraries and start files, and not the Linux
48//! uapi headers, which are [`KERNEL_HEADERS`] and fetched once for every Linux target. Both runs that
49//! produced them, on two machines, packed the same bytes.
50//!
51//! For glibc, `bin/glibc-sysroot` puts together the merged header tree of every pinned release from
52//! 2.28 to 2.44 with glibc 2.44's start files and `libc_nonshared.a`, for x86_64, i686, aarch64,
53//! armv7 hard float, riscv64, powerpc64le, s390x and loongarch64, and the release
54//! `sysroots-2026-09-24b` has the eight archives, each under 600 KB. The seven whose ABI has a glibc
55//! older than 2.33 also carry `libc_nonshared_stat.a`, the `stat` family those releases kept in
56//! `libc_nonshared.a`, which [`crate::link::LinkLine::glibc`] adds for a pin before 2.33. There is no `libc.so` in them,
57//! because the driver writes the stubs itself, and one archive serves every release of its target,
58//! which is [`pinned_for_target`]. server2 and server3 built them from separate inputs and packed
59//! the same bytes.
60//!
61//! For WASI, `bin/wasi-sysroot` takes the C part of one preview out of the sysroot of wasi-sdk 34, `wasi-sysroot-34.0.tar.gz`, which it fetches by its sha256 `9d813544`. rucc does not build wasi-libc, because the bytes clang users link against are the bytes a rucc object has to link against too. The upstream archive is 119 MB and holds libc++ for four targets and two exception models, so the C headers, the static archives and the three start files of each preview are packed on their own, and the release `sysroots-2026-10-05` has the three archives, each about 1.5 MB. Every file in them is upstream's byte for byte. server2 and server3 produced the same bytes.
62//!
63//! Every other target is still unpublished, which is a statement about producers rather than about
64//! this table: `--fetch` of one says so by name, and the day a tree for it is published is the day a
65//! row for it is added here. The rows that are here are the first thing `--fetch` has ever had
66//! anything to move, so they are also what the fetch and the install are tested against.
67
68use std::path::{Path, PathBuf};
69
70use rucc_tuple::{Env, Os, TargetTuple};
71
72/// One artifact: the sysroot for one target, as this release pins it.
73///
74/// Or the kernel header tree, which is [`KERNEL_HEADERS`] and the one artifact that is not any
75/// target's.
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub struct Pinned {
78 /// The target it is the sysroot for, in the spelling that names its directory under the cache,
79 /// or `kernel-headers` for the tree every Linux target shares, which is that tree's directory.
80 pub tuple: &'static str,
81 /// Where to get it. Handed to a downloader as it stands, and nothing here builds it out of
82 /// parts.
83 pub url: &'static str,
84 /// The sha256 of the archive, lowercase hex, which is what the bytes that arrive are held to.
85 pub sha256: &'static str,
86}
87
88impl Pinned {
89 /// The name to write the archive under, which is the last component of the URL.
90 ///
91 /// The URL's own name rather than one built out of the tuple, so that the file on disk is the
92 /// file the server served and a person comparing the two is comparing names as well as bytes.
93 #[must_use]
94 pub fn file_name(&self) -> &'static str {
95 self.url.rsplit('/').next().unwrap_or(self.url)
96 }
97
98 /// Where in the cache the archive is kept.
99 ///
100 /// Under the cache rather than in a temporary directory, because a machine with no downloader is
101 /// told this exact path and a second `--fetch` carries on from the check, which is section 13.8's
102 /// answer for a host that cannot reach the network at all. It is kept after the install for the
103 /// same reason and for one more: a fetch of a target that is already installed then moves
104 /// nothing and says so.
105 ///
106 /// The directory in front of the name is the first twelve characters of the hash, and it is
107 /// there because the name alone does not say which artifact this is. Two releases of a sysroot
108 /// for one target have the same file name, so a cache that kept the name alone would hold last
109 /// release's archive under the name this release wants, and a fetch refuses a file that does not
110 /// match rather than downloading over the top of it. That refusal is right for a file somebody
111 /// placed by hand and wrong for one we put there ourselves, so the fix is to stop the collision
112 /// rather than to soften the check. The hash is what has to change when the bytes change, so it
113 /// is the thing that separates them.
114 #[must_use]
115 pub fn archive_in(&self, cache: &Path) -> PathBuf {
116 cache.join("downloads").join(&self.sha256[..12]).join(self.file_name())
117 }
118}
119
120/// Every artifact this release pins, in tuple order.
121///
122/// A row is three strings and the test below says what they have to be. The order is the tuple's
123/// rather than the order they were published in, so that a row is found by reading down the column
124/// and two releases of this file diff as what changed between them.
125pub const PINNED: &[Pinned] = &[
126 Pinned {
127 tuple: "aarch64-linux-gnu",
128 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-aarch64-linux-gnu.tar.gz",
129 sha256: "7a3e54260de1a07d00c7b6a1d8c4a3868367885d4aa96351b804a3a4c14e1a3c",
130 },
131 Pinned {
132 tuple: "aarch64-linux-musl",
133 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-aarch64-linux-musl.tar.gz",
134 sha256: "098c24c0c27d264dceaee76141f0933845dcc8a7c3a4b473954987f731fecde5",
135 },
136 Pinned {
137 tuple: "aarch64-windows-gnu",
138 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-29/rucc-sysroot-aarch64-windows-gnu.tar.gz",
139 sha256: "b304bbbe1a11eaf9b41f9be810c27a3cf4965699872e71e2b703a59941db7aaf",
140 },
141 Pinned {
142 tuple: "armv7-linux-gnueabihf",
143 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-armv7-linux-gnueabihf.tar.gz",
144 sha256: "f96e369bdbe1013a2d8d162fce9fb83444835098c606f5e200f181db63ab011f",
145 },
146 Pinned {
147 tuple: "armv7-linux-musleabihf",
148 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-armv7-linux-musleabihf.tar.gz",
149 sha256: "645ceef60e0302b260ad804569243c3470f7545f1b135a143f9b7ec8408b6f78",
150 },
151 Pinned {
152 tuple: "i686-linux-gnu",
153 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-i686-linux-gnu.tar.gz",
154 sha256: "6860bcb60e2079d7ba40f29d8ce6344da202affe0a0f9462d12652bff773d513",
155 },
156 Pinned {
157 tuple: "i686-windows-gnu",
158 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-29/rucc-sysroot-i686-windows-gnu.tar.gz",
159 sha256: "cea70fbedd27aa9cdc08c335105714747919f09f72b8f33ec5d49c659b3526b1",
160 },
161 Pinned {
162 tuple: "loongarch64-linux-gnu",
163 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-loongarch64-linux-gnu.tar.gz",
164 sha256: "67e0565d9acc6a768dd2ad6318fab716c34bcbfaeb005398e38d4ca8d7f5c3c2",
165 },
166 Pinned {
167 tuple: "powerpc64le-linux-gnu",
168 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-powerpc64le-linux-gnu.tar.gz",
169 sha256: "c5262070d3de6f6a91d6df9ee0acf32088d7d03720ce2bb650cae1f865d2ba05",
170 },
171 Pinned {
172 tuple: "riscv64-linux-gnu",
173 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-riscv64-linux-gnu.tar.gz",
174 sha256: "6157fb51fb66fac4fedfe5233d0a2857bcce346cd8f376c595db558e0470c42f",
175 },
176 Pinned {
177 tuple: "riscv64-linux-musl",
178 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-riscv64-linux-musl.tar.gz",
179 sha256: "00fc00f996d0a9a3de1cabd95840d423de1562ed5345dd5c47f1bf89b30f0b99",
180 },
181 Pinned {
182 tuple: "s390x-linux-gnu",
183 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-s390x-linux-gnu.tar.gz",
184 sha256: "ac73fa903ef8816f2eebb35d106bcf6aabdc3e5c22e94646af196df8d5615b56",
185 },
186 Pinned {
187 tuple: "wasm32-wasip1",
188 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-10-05/rucc-sysroot-wasm32-wasip1.tar.gz",
189 sha256: "4a477d560f270d965d8fa416cf199e564c16331cf768f5fefc9f45378e51ada9",
190 },
191 Pinned {
192 tuple: "wasm32-wasip2",
193 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-10-05/rucc-sysroot-wasm32-wasip2.tar.gz",
194 sha256: "09d580524d79ec6ed5cc1179a5baba40fbfd724798f17e1cb3ad7a61d9f6f159",
195 },
196 Pinned {
197 tuple: "wasm32-wasip3",
198 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-10-05/rucc-sysroot-wasm32-wasip3.tar.gz",
199 sha256: "48fc4a3bea00c2c7d625dfa9bc53e153db0f2183924783fa137ffc9f31383378",
200 },
201 Pinned {
202 tuple: "x86_64-linux-gnu",
203 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-24b/rucc-sysroot-x86_64-linux-gnu.tar.gz",
204 sha256: "a951b219c641ee0b17e228adc8e43f36eaa70ee255d3c6f7717ec39c41e8b5b8",
205 },
206 Pinned {
207 tuple: "x86_64-linux-musl",
208 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-x86_64-linux-musl.tar.gz",
209 sha256: "93b42df66c0a7547c3e96cd5512267790df1ca0227a0000ba4f1808eb214c376",
210 },
211 Pinned {
212 tuple: "x86_64-windows-gnu",
213 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-29/rucc-sysroot-x86_64-windows-gnu.tar.gz",
214 sha256: "9f3919c9bee5447380a9746759439e051087b2364084447d6481fb9425c93758",
215 },
216];
217
218/// The Linux uapi header tree, which every Linux target reads beside its own sysroot.
219///
220/// One archive rather than a part of each Linux sysroot, for the reason [`crate::Kernel`] gives
221/// about the directory: the tree is the same for every Linux row except a small `asm/` per
222/// architecture, so a copy in every sysroot would be the same eleven megabytes once per target.
223/// `bin/kernel-headers` in `tamnd/rucc-cross` produces it and `bin/artifact kernel-headers` packs it,
224/// and `--fetch` of a Linux target installs it after the sysroot when it is not there already.
225pub const KERNEL_HEADERS: Pinned = Pinned {
226 tuple: "kernel-headers",
227 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-kernel-headers.tar.gz",
228 sha256: "e96934f553df19d6bdbb6804f1b52d079adc4bea10a819d51eda5bc4ec584f2d",
229};
230
231/// The artifact this release pins for `tuple`, if it pins one.
232///
233/// The canonical spelling is what a row is named by, so the caller parses what the user wrote and
234/// asks with the tuple's own text rather than with theirs.
235#[must_use]
236pub fn pinned_for(tuple: &str) -> Option<&'static Pinned> {
237 look(PINNED, tuple)
238}
239
240/// The artifact this release pins for a target, which for a glibc tuple that names a release is the
241/// one for the same tuple without it.
242///
243/// One glibc sysroot serves every release, because the headers in it pick the release with
244/// `__GLIBC_MINOR__` and the libraries a link reads are the stubs the driver writes for the release
245/// asked for. So `x86_64-linux-gnu.2.28` and `x86_64-linux-gnu` fetch the same archive, and it is
246/// installed under the pinned spelling because that is the directory the compiler reads.
247#[must_use]
248pub fn pinned_for_target(target: TargetTuple) -> Option<&'static Pinned> {
249 pinned_for(&target.to_canonical_string())
250 .or_else(|| pinned_for(&glibc_base(target)?.to_canonical_string()))
251}
252
253/// The same glibc target with its release left off, or [`None`] for a target that is not glibc or
254/// names no release.
255///
256/// What [`pinned_for_target`] looks a pinned release up under, and what the install accepts as the
257/// target of an archive fetched for one.
258#[must_use]
259pub fn glibc_base(target: TargetTuple) -> Option<TargetTuple> {
260 if target.os() != Os::Linux || target.env() != Env::Gnu {
261 return None;
262 }
263 target.env_version()?;
264 let mut base = TargetTuple::builder(target.arch(), target.os())
265 .sub_arch(target.sub_arch())
266 .endian(target.endian())
267 .data_model(target.data_model())
268 .env(target.env())
269 .abi(target.abi());
270 if let Some(version) = target.os_version() {
271 base = base.os_version(version);
272 }
273 base.build().ok()
274}
275
276/// Every target this release pins an artifact for, for a message that has to say what there is.
277#[must_use]
278pub fn pinned_targets() -> Vec<&'static str> {
279 PINNED.iter().map(|what| what.tuple).collect()
280}
281
282/// The same lookup over a table that is passed in, so what the lookup does is tested against rows
283/// that are written for it rather than against whatever [`PINNED`] happens to hold this release.
284fn look<'a>(table: &'a [Pinned], tuple: &str) -> Option<&'a Pinned> {
285 table.iter().find(|what| what.tuple == tuple)
286}
287
288#[cfg(test)]
289mod tests {
290 use std::path::PathBuf;
291
292 use super::*;
293
294 #[test]
295 fn a_pinned_glibc_release_shares_the_artifact_of_its_tuple() {
296 let tuple = |spelling: &str| spelling.parse::<TargetTuple>().expect("a tuple");
297 let base = glibc_base(tuple("x86_64-linux-gnu.2.28")).expect("glibc with a release");
298 assert_eq!(base.to_canonical_string(), "x86_64-linux-gnu");
299 let arm = glibc_base(tuple("armv7-linux-gnu.2.31eabihf")).expect("glibc with a release");
300 assert_eq!(arm.to_canonical_string(), "armv7-linux-gnueabihf");
301 // No release, or not glibc, is nothing to fall back to.
302 assert_eq!(glibc_base(tuple("x86_64-linux-gnu")), None);
303 assert_eq!(glibc_base(tuple("x86_64-linux-musl")), None);
304 // And a musl row is still found under its own name.
305 assert_eq!(
306 pinned_for_target(tuple("x86_64-linux-musl")).map(|what| what.tuple),
307 Some("x86_64-linux-musl")
308 );
309 }
310
311 /// A table with rows in it, which is what [`PINNED`] will look like.
312 const TABLE: &[Pinned] = &[
313 Pinned {
314 tuple: "aarch64-linux-musl",
315 url: "https://example.invalid/rucc-sysroot-aarch64-linux-musl.tar.gz",
316 sha256: "1111111111111111111111111111111111111111111111111111111111111111",
317 },
318 Pinned {
319 tuple: "x86_64-linux-musl",
320 url: "https://example.invalid/rucc-sysroot-x86_64-linux-musl.tar.gz",
321 sha256: "2222222222222222222222222222222222222222222222222222222222222222",
322 },
323 ];
324
325 #[test]
326 fn a_target_the_table_names_is_found_and_one_it_does_not_is_not() {
327 let found = look(TABLE, "x86_64-linux-musl").expect("the table has that one");
328 assert_eq!(found.sha256, TABLE[1].sha256);
329 assert_eq!(look(TABLE, "riscv64-linux-gnu"), None);
330 }
331
332 /// A tuple that starts with one the table has is a different target and not a match.
333 #[test]
334 fn a_longer_tuple_is_not_the_row_it_begins_with() {
335 assert_eq!(look(TABLE, "x86_64-linux-musl.1.2.5"), None);
336 assert_eq!(look(TABLE, "x86_64-linux"), None);
337 }
338
339 #[test]
340 fn the_archive_is_named_by_the_url_and_kept_under_the_cache() {
341 let what = TABLE[0];
342 assert_eq!(what.file_name(), "rucc-sysroot-aarch64-linux-musl.tar.gz");
343 assert_eq!(
344 what.archive_in(&PathBuf::from("/tmp/cache")),
345 PathBuf::from(
346 "/tmp/cache/downloads/111111111111/rucc-sysroot-aarch64-linux-musl.tar.gz"
347 )
348 );
349 }
350
351 /// Two releases of the sysroot for one target have the same file name, and the cache has to keep
352 /// them apart, because a fetch refuses a file under the artifact's name that is not the artifact.
353 #[test]
354 fn two_releases_of_one_target_are_not_the_same_path() {
355 let cache = PathBuf::from("/tmp/cache");
356 let old = TABLE[0];
357 let new = Pinned { sha256: TABLE[1].sha256, ..old };
358 assert_eq!(old.file_name(), new.file_name());
359 assert_ne!(old.archive_in(&cache), new.archive_in(&cache));
360 }
361
362 /// What every row of [`PINNED`] has to be.
363 ///
364 /// Left as a test rather than as a comment above the table, because the day somebody adds a row
365 /// is the day the rules stop being obvious, and a pasted hash with a capital letter in it or a
366 /// tuple spelled the way the URL spells it would otherwise be found by a user.
367 #[test]
368 fn every_row_is_a_target_a_url_and_a_hash() {
369 for what in PINNED {
370 let tuple: TargetTuple =
371 what.tuple.parse().unwrap_or_else(|why| panic!("{}: {why}", what.tuple));
372 assert_eq!(
373 tuple.to_canonical_string(),
374 what.tuple,
375 "a row is named by the canonical spelling, because that is what names the \
376 directory the tree is installed at"
377 );
378 assert!(what.url.starts_with("https://"), "{}: {}", what.tuple, what.url);
379 // A query string or a fragment would make the file name something other than the last
380 // component of the URL, which is the one thing the name is read out of.
381 assert!(!what.url.contains('?') && !what.url.contains('#'), "{}", what.url);
382 assert!(!what.file_name().is_empty(), "{} ends with a separator", what.url);
383 assert_eq!(what.sha256.len(), 64, "{}: {}", what.tuple, what.sha256);
384 assert!(
385 what.sha256.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
386 "{}: {} is not lowercase hex, and the check compares text",
387 what.tuple,
388 what.sha256
389 );
390 }
391 let mut sorted: Vec<&str> = pinned_targets();
392 sorted.sort_unstable();
393 sorted.dedup();
394 assert_eq!(sorted, pinned_targets(), "the rows are in tuple order and each target once");
395 }
396
397 /// The kernel tree's row is held to the same rules as a target's, except that its name is the
398 /// directory it is installed at rather than a tuple.
399 #[test]
400 fn the_kernel_tree_is_a_url_and_a_hash_and_no_target_is_called_that() {
401 let what = KERNEL_HEADERS;
402 assert!(what.url.starts_with("https://"), "{}", what.url);
403 assert!(!what.url.contains('?') && !what.url.contains('#'), "{}", what.url);
404 assert_eq!(what.file_name(), "rucc-kernel-headers.tar.gz");
405 assert_eq!(what.sha256.len(), 64, "{}", what.sha256);
406 assert!(
407 what.sha256.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
408 "{} is not lowercase hex",
409 what.sha256
410 );
411 assert_eq!(pinned_for(what.tuple), None);
412 }
413}