summaryrefslogtreecommitdiffstats
path: root/src/tools/bump-stage0/src/main.rs
blob: f530a4d73d36095dae366cf602e195d81b412723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use anyhow::{Context, Error};
use curl::easy::Easy;
use indexmap::IndexMap;
use std::collections::HashMap;
use std::convert::TryInto;

const PATH: &str = "src/stage0.json";
const COMPILER_COMPONENTS: &[&str] = &["rustc", "rust-std", "cargo"];
const RUSTFMT_COMPONENTS: &[&str] = &["rustfmt-preview", "rustc"];

struct Tool {
    config: Config,
    comments: Vec<String>,

    channel: Channel,
    date: Option<String>,
    version: [u16; 3],
    checksums: IndexMap<String, String>,
}

impl Tool {
    fn new(date: Option<String>) -> Result<Self, Error> {
        let channel = match std::fs::read_to_string("src/ci/channel")?.trim() {
            "stable" => Channel::Stable,
            "beta" => Channel::Beta,
            "nightly" => Channel::Nightly,
            other => anyhow::bail!("unsupported channel: {}", other),
        };

        // Split "1.42.0" into [1, 42, 0]
        let version = std::fs::read_to_string("src/version")?
            .trim()
            .split('.')
            .map(|val| val.parse())
            .collect::<Result<Vec<_>, _>>()?
            .try_into()
            .map_err(|_| anyhow::anyhow!("failed to parse version"))?;

        let existing: Stage0 = serde_json::from_slice(&std::fs::read(PATH)?)?;

        Ok(Self {
            channel,
            version,
            date,
            config: existing.config,
            comments: existing.comments,
            checksums: IndexMap::new(),
        })
    }

    fn update_json(mut self) -> Result<(), Error> {
        std::fs::write(
            PATH,
            format!(
                "{}\n",
                serde_json::to_string_pretty(&Stage0 {
                    compiler: self.detect_compiler()?,
                    rustfmt: self.detect_rustfmt()?,
                    checksums_sha256: {
                        // Keys are sorted here instead of beforehand because values in this map
                        // are added while filling the other struct fields just above this block.
                        self.checksums.sort_keys();
                        self.checksums
                    },
                    config: self.config,
                    comments: self.comments,
                })?
            ),
        )?;
        Ok(())
    }

    // Currently Rust always bootstraps from the previous stable release, and in our train model
    // this means that the master branch bootstraps from beta, beta bootstraps from current stable,
    // and stable bootstraps from the previous stable release.
    //
    // On the master branch the compiler version is configured to `beta` whereas if you're looking
    // at the beta or stable channel you'll likely see `1.x.0` as the version, with the previous
    // release's version number.
    fn detect_compiler(&mut self) -> Result<Stage0Toolchain, Error> {
        let channel = match self.channel {
            Channel::Stable | Channel::Beta => {
                // The 1.XX manifest points to the latest point release of that minor release.
                format!("{}.{}", self.version[0], self.version[1] - 1)
            }
            Channel::Nightly => "beta".to_string(),
        };

        let manifest = fetch_manifest(&self.config, &channel, self.date.as_deref())?;
        self.collect_checksums(&manifest, COMPILER_COMPONENTS)?;
        Ok(Stage0Toolchain {
            date: manifest.date,
            version: if self.channel == Channel::Nightly {
                "beta".to_string()
            } else {
                // The version field is like "1.42.0 (abcdef1234 1970-01-01)"
                manifest.pkg["rust"]
                    .version
                    .split_once(' ')
                    .expect("invalid version field")
                    .0
                    .to_string()
            },
        })
    }

    /// We use a nightly rustfmt to format the source because it solves some bootstrapping issues
    /// with use of new syntax in this repo. For the beta/stable channels rustfmt is not provided,
    /// as we don't want to depend on rustfmt from nightly there.
    fn detect_rustfmt(&mut self) -> Result<Option<Stage0Toolchain>, Error> {
        if self.channel != Channel::Nightly {
            return Ok(None);
        }

        let manifest = fetch_manifest(&self.config, "nightly", self.date.as_deref())?;
        self.collect_checksums(&manifest, RUSTFMT_COMPONENTS)?;
        Ok(Some(Stage0Toolchain { date: manifest.date, version: "nightly".into() }))
    }

    fn collect_checksums(&mut self, manifest: &Manifest, components: &[&str]) -> Result<(), Error> {
        let prefix = format!("{}/", self.config.dist_server);
        for component in components {
            let pkg = manifest
                .pkg
                .get(*component)
                .ok_or_else(|| anyhow::anyhow!("missing component from manifest: {}", component))?;
            for target in pkg.target.values() {
                for pair in &[(&target.url, &target.hash), (&target.xz_url, &target.xz_hash)] {
                    if let (Some(url), Some(sha256)) = pair {
                        let url = url
                            .strip_prefix(&prefix)
                            .ok_or_else(|| {
                                anyhow::anyhow!("url doesn't start with dist server base: {}", url)
                            })?
                            .to_string();
                        self.checksums.insert(url, sha256.clone());
                    }
                }
            }
        }
        Ok(())
    }
}

fn main() -> Result<(), Error> {
    let tool = Tool::new(std::env::args().nth(1))?;
    tool.update_json()?;
    Ok(())
}

fn fetch_manifest(config: &Config, channel: &str, date: Option<&str>) -> Result<Manifest, Error> {
    let url = if let Some(date) = date {
        format!("{}/dist/{}/channel-rust-{}.toml", config.dist_server, date, channel)
    } else {
        format!("{}/dist/channel-rust-{}.toml", config.dist_server, channel)
    };

    Ok(toml::from_slice(&http_get(&url)?)?)
}

fn http_get(url: &str) -> Result<Vec<u8>, Error> {
    let mut data = Vec::new();
    let mut handle = Easy::new();
    handle.fail_on_error(true)?;
    handle.url(url)?;
    {
        let mut transfer = handle.transfer();
        transfer.write_function(|new_data| {
            data.extend_from_slice(new_data);
            Ok(new_data.len())
        })?;
        transfer.perform().context(format!("failed to fetch {url}"))?;
    }
    Ok(data)
}

#[derive(Debug, PartialEq, Eq)]
enum Channel {
    Stable,
    Beta,
    Nightly,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Stage0 {
    config: Config,
    // Comments are explicitly below the config, do not move them above.
    //
    // Downstream forks of the compiler codebase can change the configuration values defined above,
    // but doing so would risk merge conflicts whenever they import new changes that include a
    // bootstrap compiler bump.
    //
    // To lessen the pain, a big block of comments is placed between the configuration and the
    // auto-generated parts of the file, preventing git diffs of the config to include parts of the
    // auto-generated content and vice versa. This should prevent merge conflicts.
    #[serde(rename = "__comments")]
    comments: Vec<String>,
    compiler: Stage0Toolchain,
    rustfmt: Option<Stage0Toolchain>,
    checksums_sha256: IndexMap<String, String>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Config {
    dist_server: String,
    // There are other fields in the configuration, which will be read by src/bootstrap or other
    // tools consuming stage0.json. To avoid the need to update bump-stage0 every time a new field
    // is added, we collect all the fields in an untyped Value and serialize them back with the
    // same order and structure they were deserialized in.
    #[serde(flatten)]
    other: serde_json::Value,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Stage0Toolchain {
    date: String,
    version: String,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Manifest {
    date: String,
    pkg: HashMap<String, ManifestPackage>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct ManifestPackage {
    version: String,
    target: HashMap<String, ManifestTargetPackage>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct ManifestTargetPackage {
    url: Option<String>,
    hash: Option<String>,
    xz_url: Option<String>,
    xz_hash: Option<String>,
}