3 use indexmap::IndexMap;
4 use std::collections::HashMap;
5 use std::convert::TryInto;
7 const PATH: &str = "src/stage0.json";
8 const COMPILER_COMPONENTS: &[&str] = &["rustc", "rust-std", "cargo"];
9 const RUSTFMT_COMPONENTS: &[&str] = &["rustfmt-preview"];
13 comments: Vec<String>,
17 checksums: IndexMap<String, String>,
21 fn new() -> Result<Self, Error> {
22 let channel = match std::fs::read_to_string("src/ci/channel")?.trim() {
23 "stable" => Channel::Stable,
24 "beta" => Channel::Beta,
25 "nightly" => Channel::Nightly,
26 other => anyhow::bail!("unsupported channel: {}", other),
29 // Split "1.42.0" into [1, 42, 0]
30 let version = std::fs::read_to_string("src/version")?
33 .map(|val| val.parse())
34 .collect::<Result<Vec<_>, _>>()?
36 .map_err(|_| anyhow::anyhow!("failed to parse version"))?;
38 let existing: Stage0 = serde_json::from_slice(&std::fs::read(PATH)?)?;
43 config: existing.config,
44 comments: existing.comments,
45 checksums: IndexMap::new(),
49 fn update_json(mut self) -> Result<(), Error> {
54 serde_json::to_string_pretty(&Stage0 {
55 compiler: self.detect_compiler()?,
56 rustfmt: self.detect_rustfmt()?,
58 // Keys are sorted here instead of beforehand because values in this map
59 // are added while filling the other struct fields just above this block.
60 self.checksums.sort_keys();
64 comments: self.comments,
71 // Currently Rust always bootstraps from the previous stable release, and in our train model
72 // this means that the master branch bootstraps from beta, beta bootstraps from current stable,
73 // and stable bootstraps from the previous stable release.
75 // On the master branch the compiler version is configured to `beta` whereas if you're looking
76 // at the beta or stable channel you'll likely see `1.x.0` as the version, with the previous
77 // release's version number.
78 fn detect_compiler(&mut self) -> Result<Stage0Toolchain, Error> {
79 let channel = match self.channel {
80 Channel::Stable | Channel::Beta => {
81 // The 1.XX manifest points to the latest point release of that minor release.
82 format!("{}.{}", self.version[0], self.version[1] - 1)
84 Channel::Nightly => "beta".to_string(),
87 let manifest = fetch_manifest(&self.config, &channel)?;
88 self.collect_checksums(&manifest, COMPILER_COMPONENTS)?;
91 version: if self.channel == Channel::Nightly {
94 // The version field is like "1.42.0 (abcdef1234 1970-01-01)"
98 .expect("invalid version field")
105 /// We use a nightly rustfmt to format the source because it solves some bootstrapping issues
106 /// with use of new syntax in this repo. For the beta/stable channels rustfmt is not provided,
107 /// as we don't want to depend on rustfmt from nightly there.
108 fn detect_rustfmt(&mut self) -> Result<Option<Stage0Toolchain>, Error> {
109 if self.channel != Channel::Nightly {
113 let manifest = fetch_manifest(&self.config, "nightly")?;
114 self.collect_checksums(&manifest, RUSTFMT_COMPONENTS)?;
115 Ok(Some(Stage0Toolchain { date: manifest.date, version: "nightly".into() }))
118 fn collect_checksums(&mut self, manifest: &Manifest, components: &[&str]) -> Result<(), Error> {
119 let prefix = format!("{}/", self.config.dist_server);
120 for component in components {
124 .ok_or_else(|| anyhow::anyhow!("missing component from manifest: {}", component))?;
125 for target in pkg.target.values() {
126 for pair in &[(&target.url, &target.hash), (&target.xz_url, &target.xz_hash)] {
127 if let (Some(url), Some(sha256)) = pair {
129 .strip_prefix(&prefix)
131 anyhow::anyhow!("url doesn't start with dist server base: {}", url)
134 self.checksums.insert(url, sha256.clone());
143 fn main() -> Result<(), Error> {
144 let tool = Tool::new()?;
149 fn fetch_manifest(config: &Config, channel: &str) -> Result<Manifest, Error> {
150 Ok(toml::from_slice(&http_get(&format!(
151 "{}/dist/channel-rust-{}.toml",
152 config.dist_server, channel
156 fn http_get(url: &str) -> Result<Vec<u8>, Error> {
157 let mut data = Vec::new();
158 let mut handle = Easy::new();
159 handle.fail_on_error(true)?;
162 let mut transfer = handle.transfer();
163 transfer.write_function(|new_data| {
164 data.extend_from_slice(new_data);
172 #[derive(Debug, PartialEq, Eq)]
179 #[derive(Debug, serde::Serialize, serde::Deserialize)]
182 // Comments are explicitly below the config, do not move them above.
184 // Downstream forks of the compiler codebase can change the configuration values defined above,
185 // but doing so would risk merge conflicts whenever they import new changes that include a
186 // bootstrap compiler bump.
188 // To lessen the pain, a big block of comments is placed between the configuration and the
189 // auto-generated parts of the file, preventing git diffs of the config to include parts of the
190 // auto-generated content and vice versa. This should prevent merge conflicts.
191 #[serde(rename = "__comments")]
192 comments: Vec<String>,
193 compiler: Stage0Toolchain,
194 rustfmt: Option<Stage0Toolchain>,
195 checksums_sha256: IndexMap<String, String>,
198 #[derive(Debug, serde::Serialize, serde::Deserialize)]
201 // There are other fields in the configuration, which will be read by src/bootstrap or other
202 // tools consuming stage0.json. To avoid the need to update bump-stage0 every time a new field
203 // is added, we collect all the fields in an untyped Value and serialize them back with the
204 // same order and structure they were deserialized in.
206 other: serde_json::Value,
209 #[derive(Debug, serde::Serialize, serde::Deserialize)]
210 struct Stage0Toolchain {
215 #[derive(Debug, serde::Serialize, serde::Deserialize)]
218 pkg: HashMap<String, ManifestPackage>,
221 #[derive(Debug, serde::Serialize, serde::Deserialize)]
222 struct ManifestPackage {
224 target: HashMap<String, ManifestTargetPackage>,
227 #[derive(Debug, serde::Serialize, serde::Deserialize)]
228 struct ManifestTargetPackage {
230 hash: Option<String>,
231 xz_url: Option<String>,
232 xz_hash: Option<String>,