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//! Three rows, which are the three windows-gnu targets. `bin/mingw-headers` in `tamnd/rucc-cross`
36//! installs mingw-w64 14.0.0's headers, `bin/artifact` packs the tree, and the release
37//! `sysroots-2026-09-18` is where the files are. Each archive is 8.4 MiB of the same 1702 headers,
38//! 84 MiB installed, and the three differ only in the `target` line of the manifest inside them,
39//! because mingw-w64 has no per architecture split and [`crate::Sysroot::splits_by_arch`] says so.
40//!
41//! Every other target is still unpublished, which is a statement about producers rather than about
42//! this table: `--fetch` of one says so by name, and the day a tree for it is published is the day a
43//! row for it is added here. The rows that are here are the first thing `--fetch` has ever had
44//! anything to move, so they are also what the fetch and the install are tested against.
45
46use std::path::{Path, PathBuf};
47
48/// One artifact: the sysroot for one target, as this release pins it.
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub struct Pinned {
51 /// The target it is the sysroot for, in the spelling that names its directory under the cache.
52 pub tuple: &'static str,
53 /// Where to get it. Handed to a downloader as it stands, and nothing here builds it out of
54 /// parts.
55 pub url: &'static str,
56 /// The sha256 of the archive, lowercase hex, which is what the bytes that arrive are held to.
57 pub sha256: &'static str,
58}
59
60impl Pinned {
61 /// The name to write the archive under, which is the last component of the URL.
62 ///
63 /// The URL's own name rather than one built out of the tuple, so that the file on disk is the
64 /// file the server served and a person comparing the two is comparing names as well as bytes.
65 #[must_use]
66 pub fn file_name(&self) -> &'static str {
67 self.url.rsplit('/').next().unwrap_or(self.url)
68 }
69
70 /// Where in the cache the archive is kept.
71 ///
72 /// Under the cache rather than in a temporary directory, because a machine with no downloader is
73 /// told this exact path and a second `--fetch` carries on from the check, which is section 13.8's
74 /// answer for a host that cannot reach the network at all. It is kept after the install for the
75 /// same reason and for one more: a fetch of a target that is already installed then moves
76 /// nothing and says so.
77 #[must_use]
78 pub fn archive_in(&self, cache: &Path) -> PathBuf {
79 cache.join("downloads").join(self.file_name())
80 }
81}
82
83/// Every artifact this release pins, in tuple order.
84///
85/// A row is three strings and the test below says what they have to be. The order is the tuple's
86/// rather than the order they were published in, so that a row is found by reading down the column
87/// and two releases of this file diff as what changed between them.
88pub const PINNED: &[Pinned] = &[
89 Pinned {
90 tuple: "aarch64-windows-gnu",
91 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-18/rucc-sysroot-aarch64-windows-gnu.tar.gz",
92 sha256: "037b839abe565493360d320d833b5bfa048884eed445b51d41aafb59acfbac84",
93 },
94 Pinned {
95 tuple: "i686-windows-gnu",
96 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-18/rucc-sysroot-i686-windows-gnu.tar.gz",
97 sha256: "e66deb5e67e35e12d2299d136a0d150168568e2b8aa7aab0c2b6cc8254b0cb53",
98 },
99 Pinned {
100 tuple: "x86_64-windows-gnu",
101 url: "https://github.com/tamnd/rucc-cross/releases/download/sysroots-2026-09-18/rucc-sysroot-x86_64-windows-gnu.tar.gz",
102 sha256: "8774d78560c196f182b8cf865e9fe57e2937779fdcf14b7f7b3f321de2cf4488",
103 },
104];
105
106/// The artifact this release pins for `tuple`, if it pins one.
107///
108/// The canonical spelling is what a row is named by, so the caller parses what the user wrote and
109/// asks with the tuple's own text rather than with theirs.
110#[must_use]
111pub fn pinned_for(tuple: &str) -> Option<&'static Pinned> {
112 look(PINNED, tuple)
113}
114
115/// Every target this release pins an artifact for, for a message that has to say what there is.
116#[must_use]
117pub fn pinned_targets() -> Vec<&'static str> {
118 PINNED.iter().map(|what| what.tuple).collect()
119}
120
121/// The same lookup over a table that is passed in, so what the lookup does is tested against rows
122/// that are written for it rather than against whatever [`PINNED`] happens to hold this release.
123fn look<'a>(table: &'a [Pinned], tuple: &str) -> Option<&'a Pinned> {
124 table.iter().find(|what| what.tuple == tuple)
125}
126
127#[cfg(test)]
128mod tests {
129 use std::path::PathBuf;
130
131 use rucc_tuple::TargetTuple;
132
133 use super::*;
134
135 /// A table with rows in it, which is what [`PINNED`] will look like.
136 const TABLE: &[Pinned] = &[
137 Pinned {
138 tuple: "aarch64-linux-musl",
139 url: "https://example.invalid/rucc-sysroot-aarch64-linux-musl.tar.gz",
140 sha256: "1111111111111111111111111111111111111111111111111111111111111111",
141 },
142 Pinned {
143 tuple: "x86_64-linux-musl",
144 url: "https://example.invalid/rucc-sysroot-x86_64-linux-musl.tar.gz",
145 sha256: "2222222222222222222222222222222222222222222222222222222222222222",
146 },
147 ];
148
149 #[test]
150 fn a_target_the_table_names_is_found_and_one_it_does_not_is_not() {
151 let found = look(TABLE, "x86_64-linux-musl").expect("the table has that one");
152 assert_eq!(found.sha256, TABLE[1].sha256);
153 assert_eq!(look(TABLE, "riscv64-linux-gnu"), None);
154 }
155
156 /// A tuple that starts with one the table has is a different target and not a match.
157 #[test]
158 fn a_longer_tuple_is_not_the_row_it_begins_with() {
159 assert_eq!(look(TABLE, "x86_64-linux-musl.1.2.5"), None);
160 assert_eq!(look(TABLE, "x86_64-linux"), None);
161 }
162
163 #[test]
164 fn the_archive_is_named_by_the_url_and_kept_under_the_cache() {
165 let what = TABLE[0];
166 assert_eq!(what.file_name(), "rucc-sysroot-aarch64-linux-musl.tar.gz");
167 assert_eq!(
168 what.archive_in(&PathBuf::from("/tmp/cache")),
169 PathBuf::from("/tmp/cache/downloads/rucc-sysroot-aarch64-linux-musl.tar.gz")
170 );
171 }
172
173 /// What every row of [`PINNED`] has to be.
174 ///
175 /// Left as a test rather than as a comment above the table, because the day somebody adds a row
176 /// is the day the rules stop being obvious, and a pasted hash with a capital letter in it or a
177 /// tuple spelled the way the URL spells it would otherwise be found by a user.
178 #[test]
179 fn every_row_is_a_target_a_url_and_a_hash() {
180 for what in PINNED {
181 let tuple: TargetTuple =
182 what.tuple.parse().unwrap_or_else(|why| panic!("{}: {why}", what.tuple));
183 assert_eq!(
184 tuple.to_canonical_string(),
185 what.tuple,
186 "a row is named by the canonical spelling, because that is what names the \
187 directory the tree is installed at"
188 );
189 assert!(what.url.starts_with("https://"), "{}: {}", what.tuple, what.url);
190 // A query string or a fragment would make the file name something other than the last
191 // component of the URL, which is the one thing the name is read out of.
192 assert!(!what.url.contains('?') && !what.url.contains('#'), "{}", what.url);
193 assert!(!what.file_name().is_empty(), "{} ends with a separator", what.url);
194 assert_eq!(what.sha256.len(), 64, "{}: {}", what.tuple, what.sha256);
195 assert!(
196 what.sha256.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
197 "{}: {} is not lowercase hex, and the check compares text",
198 what.tuple,
199 what.sha256
200 );
201 }
202 let mut sorted: Vec<&str> = pinned_targets();
203 sorted.sort_unstable();
204 sorted.dedup();
205 assert_eq!(sorted, pinned_targets(), "the rows are in tuple order and each target once");
206 }
207}