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//! Seven rows, which are the three windows-gnu targets and four musl 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 and the import libraries into the `lib` directory beside them, `bin/artifact`
38//! packs the tree, and the release `sysroots-2026-09-21` is where the files are. The archives are
39//! 15.2 MiB for x86_64, 14.8 MiB for i686 and 12.5 MiB for aarch64, installing to 134 MB, 128 MB and
40//! 113 MB. The header half is the same 1702 files in all three, because mingw-w64 has no per
41//! architecture split and [`crate::Sysroot::splits_by_arch`] says so, and the `lib` half is what
42//! differs.
43//!
44//! For musl, `bin/sysroot` builds musl 1.2.5 for x86_64, aarch64, riscv64 and armv7 hard float, and
45//! the release `sysroots-2026-09-23` has the four archives, each about 2 MiB and installing to between
46//! 7 and 13 MB. They hold musl's headers and its static libraries and start files, and not the Linux
47//! uapi headers, which are [`KERNEL_HEADERS`] and fetched once for every Linux target. Both runs that
48//! produced them, on two machines, packed the same bytes.
49//!
50//! Every other target is still unpublished, which is a statement about producers rather than about
51//! this table: `--fetch` of one says so by name, and the day a tree for it is published is the day a
52//! row for it is added here. The rows that are here are the first thing `--fetch` has ever had
53//! anything to move, so they are also what the fetch and the install are tested against.
54
55use std::path::{Path, PathBuf};
56
57/// One artifact: the sysroot for one target, as this release pins it.
58///
59/// Or the kernel header tree, which is [`KERNEL_HEADERS`] and the one artifact that is not any
60/// target's.
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub struct Pinned {
63 /// The target it is the sysroot for, in the spelling that names its directory under the cache,
64 /// or `kernel-headers` for the tree every Linux target shares, which is that tree's directory.
65 pub tuple: &'static str,
66 /// Where to get it. Handed to a downloader as it stands, and nothing here builds it out of
67 /// parts.
68 pub url: &'static str,
69 /// The sha256 of the archive, lowercase hex, which is what the bytes that arrive are held to.
70 pub sha256: &'static str,
71}
72
73impl Pinned {
74 /// The name to write the archive under, which is the last component of the URL.
75 ///
76 /// The URL's own name rather than one built out of the tuple, so that the file on disk is the
77 /// file the server served and a person comparing the two is comparing names as well as bytes.
78 #[must_use]
79 pub fn file_name(&self) -> &'static str {
80 self.url.rsplit('/').next().unwrap_or(self.url)
81 }
82
83 /// Where in the cache the archive is kept.
84 ///
85 /// Under the cache rather than in a temporary directory, because a machine with no downloader is
86 /// told this exact path and a second `--fetch` carries on from the check, which is section 13.8's
87 /// answer for a host that cannot reach the network at all. It is kept after the install for the
88 /// same reason and for one more: a fetch of a target that is already installed then moves
89 /// nothing and says so.
90 ///
91 /// The directory in front of the name is the first twelve characters of the hash, and it is
92 /// there because the name alone does not say which artifact this is. Two releases of a sysroot
93 /// for one target have the same file name, so a cache that kept the name alone would hold last
94 /// release's archive under the name this release wants, and a fetch refuses a file that does not
95 /// match rather than downloading over the top of it. That refusal is right for a file somebody
96 /// placed by hand and wrong for one we put there ourselves, so the fix is to stop the collision
97 /// rather than to soften the check. The hash is what has to change when the bytes change, so it
98 /// is the thing that separates them.
99 #[must_use]
100 pub fn archive_in(&self, cache: &Path) -> PathBuf {
101 cache.join("downloads").join(&self.sha256[..12]).join(self.file_name())
102 }
103}
104
105/// Every artifact this release pins, in tuple order.
106///
107/// A row is three strings and the test below says what they have to be. The order is the tuple's
108/// rather than the order they were published in, so that a row is found by reading down the column
109/// and two releases of this file diff as what changed between them.
110pub const PINNED: &[Pinned] = &[
111 Pinned {
112 tuple: "aarch64-linux-musl",
113 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-aarch64-linux-musl.tar.gz",
114 sha256: "098c24c0c27d264dceaee76141f0933845dcc8a7c3a4b473954987f731fecde5",
115 },
116 Pinned {
117 tuple: "aarch64-windows-gnu",
118 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-21/rucc-sysroot-aarch64-windows-gnu.tar.gz",
119 sha256: "cc6be4263b09a475895d9054bb4b096d837010b7e5ed25ebc8934accccd5258e",
120 },
121 Pinned {
122 tuple: "armv7-linux-musleabihf",
123 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-armv7-linux-musleabihf.tar.gz",
124 sha256: "645ceef60e0302b260ad804569243c3470f7545f1b135a143f9b7ec8408b6f78",
125 },
126 Pinned {
127 tuple: "i686-windows-gnu",
128 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-21/rucc-sysroot-i686-windows-gnu.tar.gz",
129 sha256: "296de7554f57d308c00b405c145eb314886e1e28ff8507aa6e7b34e7e4def7f1",
130 },
131 Pinned {
132 tuple: "riscv64-linux-musl",
133 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-riscv64-linux-musl.tar.gz",
134 sha256: "00fc00f996d0a9a3de1cabd95840d423de1562ed5345dd5c47f1bf89b30f0b99",
135 },
136 Pinned {
137 tuple: "x86_64-linux-musl",
138 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-sysroot-x86_64-linux-musl.tar.gz",
139 sha256: "93b42df66c0a7547c3e96cd5512267790df1ca0227a0000ba4f1808eb214c376",
140 },
141 Pinned {
142 tuple: "x86_64-windows-gnu",
143 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-21/rucc-sysroot-x86_64-windows-gnu.tar.gz",
144 sha256: "2f1e34ea0ad3e1ae8be08c054c61030e0a916053e194916f4045e08dcfd69545",
145 },
146];
147
148/// The Linux uapi header tree, which every Linux target reads beside its own sysroot.
149///
150/// One archive rather than a part of each Linux sysroot, for the reason [`crate::Kernel`] gives
151/// about the directory: the tree is the same for every Linux row except a small `asm/` per
152/// architecture, so a copy in every sysroot would be the same eleven megabytes once per target.
153/// `bin/kernel-headers` in `tamnd/rucc-cross` produces it and `bin/artifact kernel-headers` packs it,
154/// and `--fetch` of a Linux target installs it after the sysroot when it is not there already.
155pub const KERNEL_HEADERS: Pinned = Pinned {
156 tuple: "kernel-headers",
157 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-23/rucc-kernel-headers.tar.gz",
158 sha256: "e96934f553df19d6bdbb6804f1b52d079adc4bea10a819d51eda5bc4ec584f2d",
159};
160
161/// The artifact this release pins for `tuple`, if it pins one.
162///
163/// The canonical spelling is what a row is named by, so the caller parses what the user wrote and
164/// asks with the tuple's own text rather than with theirs.
165#[must_use]
166pub fn pinned_for(tuple: &str) -> Option<&'static Pinned> {
167 look(PINNED, tuple)
168}
169
170/// Every target this release pins an artifact for, for a message that has to say what there is.
171#[must_use]
172pub fn pinned_targets() -> Vec<&'static str> {
173 PINNED.iter().map(|what| what.tuple).collect()
174}
175
176/// The same lookup over a table that is passed in, so what the lookup does is tested against rows
177/// that are written for it rather than against whatever [`PINNED`] happens to hold this release.
178fn look<'a>(table: &'a [Pinned], tuple: &str) -> Option<&'a Pinned> {
179 table.iter().find(|what| what.tuple == tuple)
180}
181
182#[cfg(test)]
183mod tests {
184 use std::path::PathBuf;
185
186 use rucc_tuple::TargetTuple;
187
188 use super::*;
189
190 /// A table with rows in it, which is what [`PINNED`] will look like.
191 const TABLE: &[Pinned] = &[
192 Pinned {
193 tuple: "aarch64-linux-musl",
194 url: "https://example.invalid/rucc-sysroot-aarch64-linux-musl.tar.gz",
195 sha256: "1111111111111111111111111111111111111111111111111111111111111111",
196 },
197 Pinned {
198 tuple: "x86_64-linux-musl",
199 url: "https://example.invalid/rucc-sysroot-x86_64-linux-musl.tar.gz",
200 sha256: "2222222222222222222222222222222222222222222222222222222222222222",
201 },
202 ];
203
204 #[test]
205 fn a_target_the_table_names_is_found_and_one_it_does_not_is_not() {
206 let found = look(TABLE, "x86_64-linux-musl").expect("the table has that one");
207 assert_eq!(found.sha256, TABLE[1].sha256);
208 assert_eq!(look(TABLE, "riscv64-linux-gnu"), None);
209 }
210
211 /// A tuple that starts with one the table has is a different target and not a match.
212 #[test]
213 fn a_longer_tuple_is_not_the_row_it_begins_with() {
214 assert_eq!(look(TABLE, "x86_64-linux-musl.1.2.5"), None);
215 assert_eq!(look(TABLE, "x86_64-linux"), None);
216 }
217
218 #[test]
219 fn the_archive_is_named_by_the_url_and_kept_under_the_cache() {
220 let what = TABLE[0];
221 assert_eq!(what.file_name(), "rucc-sysroot-aarch64-linux-musl.tar.gz");
222 assert_eq!(
223 what.archive_in(&PathBuf::from("/tmp/cache")),
224 PathBuf::from(
225 "/tmp/cache/downloads/111111111111/rucc-sysroot-aarch64-linux-musl.tar.gz"
226 )
227 );
228 }
229
230 /// Two releases of the sysroot for one target have the same file name, and the cache has to keep
231 /// them apart, because a fetch refuses a file under the artifact's name that is not the artifact.
232 #[test]
233 fn two_releases_of_one_target_are_not_the_same_path() {
234 let cache = PathBuf::from("/tmp/cache");
235 let old = TABLE[0];
236 let new = Pinned { sha256: TABLE[1].sha256, ..old };
237 assert_eq!(old.file_name(), new.file_name());
238 assert_ne!(old.archive_in(&cache), new.archive_in(&cache));
239 }
240
241 /// What every row of [`PINNED`] has to be.
242 ///
243 /// Left as a test rather than as a comment above the table, because the day somebody adds a row
244 /// is the day the rules stop being obvious, and a pasted hash with a capital letter in it or a
245 /// tuple spelled the way the URL spells it would otherwise be found by a user.
246 #[test]
247 fn every_row_is_a_target_a_url_and_a_hash() {
248 for what in PINNED {
249 let tuple: TargetTuple =
250 what.tuple.parse().unwrap_or_else(|why| panic!("{}: {why}", what.tuple));
251 assert_eq!(
252 tuple.to_canonical_string(),
253 what.tuple,
254 "a row is named by the canonical spelling, because that is what names the \
255 directory the tree is installed at"
256 );
257 assert!(what.url.starts_with("https://"), "{}: {}", what.tuple, what.url);
258 // A query string or a fragment would make the file name something other than the last
259 // component of the URL, which is the one thing the name is read out of.
260 assert!(!what.url.contains('?') && !what.url.contains('#'), "{}", what.url);
261 assert!(!what.file_name().is_empty(), "{} ends with a separator", what.url);
262 assert_eq!(what.sha256.len(), 64, "{}: {}", what.tuple, what.sha256);
263 assert!(
264 what.sha256.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
265 "{}: {} is not lowercase hex, and the check compares text",
266 what.tuple,
267 what.sha256
268 );
269 }
270 let mut sorted: Vec<&str> = pinned_targets();
271 sorted.sort_unstable();
272 sorted.dedup();
273 assert_eq!(sorted, pinned_targets(), "the rows are in tuple order and each target once");
274 }
275
276 /// The kernel tree's row is held to the same rules as a target's, except that its name is the
277 /// directory it is installed at rather than a tuple.
278 #[test]
279 fn the_kernel_tree_is_a_url_and_a_hash_and_no_target_is_called_that() {
280 let what = KERNEL_HEADERS;
281 assert!(what.url.starts_with("https://"), "{}", what.url);
282 assert!(!what.url.contains('?') && !what.url.contains('#'), "{}", what.url);
283 assert_eq!(what.file_name(), "rucc-kernel-headers.tar.gz");
284 assert_eq!(what.sha256.len(), 64, "{}", what.sha256);
285 assert!(
286 what.sha256.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
287 "{} is not lowercase hex",
288 what.sha256
289 );
290 assert_eq!(pinned_for(what.tuple), None);
291 }
292}