Skip to main content

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