Skip to content

Commit

Permalink
use shared stage0 parser from build_helper
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed May 11, 2024
1 parent f2d50b6 commit b46c3f2
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ name = "bump-stage0"
version = "0.1.0"
dependencies = [
"anyhow",
"build_helper",
"curl",
"indexmap",
"serde",
"serde_json",
"toml 0.5.11",
]

Expand Down
38 changes: 4 additions & 34 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub struct Config {
pub test_compare_mode: bool,
pub color: Color,
pub patch_binaries_for_nix: Option<bool>,
pub stage0_metadata: Stage0Metadata,
pub stage0_metadata: build_helper::stage0_parser::Stage0,
pub android_ndk: Option<PathBuf>,
/// Whether to use the `c` feature of the `compiler_builtins` crate.
pub optimized_compiler_builtins: bool,
Expand Down Expand Up @@ -350,34 +350,6 @@ pub struct Config {
pub paths: Vec<PathBuf>,
}

#[derive(Default, Deserialize, Clone)]
pub struct Stage0Metadata {
pub compiler: CompilerMetadata,
pub config: Stage0Config,
pub checksums_sha256: HashMap<String, String>,
pub rustfmt: Option<RustfmtMetadata>,
}
#[derive(Default, Deserialize, Clone)]
pub struct CompilerMetadata {
pub date: String,
pub version: String,
}

#[derive(Default, Deserialize, Clone)]
pub struct Stage0Config {
pub dist_server: String,
pub artifacts_server: String,
pub artifacts_with_llvm_assertions_server: String,
pub git_merge_commit_email: String,
pub git_repository: String,
pub nightly_branch: String,
}
#[derive(Default, Deserialize, Clone)]
pub struct RustfmtMetadata {
pub date: String,
pub version: String,
}

#[derive(Clone, Debug, Default)]
pub enum RustfmtState {
SystemToolchain(PathBuf),
Expand Down Expand Up @@ -1296,13 +1268,13 @@ impl Config {
Some(p) => PathBuf::from(p),
None => git_root,
};
// If this doesn't have at least `stage0.json`, we guessed wrong. This can happen when,
// If this doesn't have at least `stage0`, we guessed wrong. This can happen when,
// for example, the build directory is inside of another unrelated git directory.
// In that case keep the original `CARGO_MANIFEST_DIR` handling.
//
// NOTE: this implies that downloadable bootstrap isn't supported when the build directory is outside
// the source directory. We could fix that by setting a variable from all three of python, ./x, and x.ps1.
if git_root.join("src").join("stage0.json").exists() {
if git_root.join("src").join("stage0").exists() {
config.src = git_root;
}
} else {
Expand All @@ -1320,9 +1292,7 @@ impl Config {
.to_path_buf();
}

let stage0_json = t!(std::fs::read(config.src.join("src").join("stage0.json")));

config.stage0_metadata = t!(serde_json::from_slice::<Stage0Metadata>(&stage0_json));
config.stage0_metadata = build_helper::stage0_parser::parse_stage0_file();

// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
let toml_path = flags
Expand Down
8 changes: 4 additions & 4 deletions src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use std::{
};

use build_helper::ci::CiEnv;
use build_helper::stage0_parser::VersionMetadata;
use xz2::bufread::XzDecoder;

use crate::core::config::RustfmtMetadata;
use crate::utils::helpers::{check_run, exe, program_out_of_date};
use crate::{core::build_steps::llvm::detect_llvm_sha, utils::helpers::hex_encode};
use crate::{t, Config};
Expand Down Expand Up @@ -408,7 +408,7 @@ impl Config {
/// NOTE: rustfmt is a completely different toolchain than the bootstrap compiler, so it can't
/// reuse target directories or artifacts
pub(crate) fn maybe_download_rustfmt(&self) -> Option<PathBuf> {
let RustfmtMetadata { date, version } = self.stage0_metadata.rustfmt.as_ref()?;
let VersionMetadata { date, version } = self.stage0_metadata.rustfmt.as_ref()?;
let channel = format!("{version}-{date}");

let host = self.build;
Expand Down Expand Up @@ -606,7 +606,7 @@ impl Config {
DownloadSource::Dist => {
let dist_server = env::var("RUSTUP_DIST_SERVER")
.unwrap_or(self.stage0_metadata.config.dist_server.to_string());
// NOTE: make `dist` part of the URL because that's how it's stored in src/stage0.json
// NOTE: make `dist` part of the URL because that's how it's stored in src/stage0
(dist_server, format!("dist/{key}/{filename}"), true)
}
};
Expand All @@ -616,7 +616,7 @@ impl Config {
// this on each and every nightly ...
let checksum = if should_verify {
let error = format!(
"src/stage0.json doesn't contain a checksum for {url}. \
"src/stage0 doesn't contain a checksum for {url}. \
Pre-built artifacts might not be available for this \
target at this time, see https://doc.rust-lang.org/nightly\
/rustc/platform-support.html for more information."
Expand Down
1 change: 1 addition & 0 deletions src/tools/build_helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod ci;
pub mod git;
pub mod metrics;
pub mod util;
pub mod stage0_parser;
76 changes: 76 additions & 0 deletions src/tools/build_helper/src/stage0_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::collections::BTreeMap;

#[derive(Default, Clone)]
pub struct Stage0 {
pub compiler: VersionMetadata,
pub rustfmt: Option<VersionMetadata>,
pub config: Stage0Config,
pub checksums_sha256: BTreeMap<String, String>,
}

#[derive(Default, Clone)]
pub struct VersionMetadata {
pub date: String,
pub version: String,
}

#[derive(Default, Clone)]
pub struct Stage0Config {
pub dist_server: String,
pub artifacts_server: String,
pub artifacts_with_llvm_assertions_server: String,
pub git_merge_commit_email: String,
pub git_repository: String,
pub nightly_branch: String,
}

pub fn parse_stage0_file() -> Stage0 {
let stage0_content = include_str!("../../../stage0");

let mut stage0 = Stage0::default();
for line in stage0_content.lines() {
let line = line.trim();

if line.is_empty() {
continue;
}

// Ignore comments
if line.starts_with('#') {
continue;
}

let (key, value) = line.split_once('=').unwrap();

match key {
"dist_server" => stage0.config.dist_server = value.to_owned(),
"artifacts_server" => stage0.config.artifacts_server = value.to_owned(),
"artifacts_with_llvm_assertions_server" => {
stage0.config.artifacts_with_llvm_assertions_server = value.to_owned()
}
"git_merge_commit_email" => stage0.config.git_merge_commit_email = value.to_owned(),
"git_repository" => stage0.config.git_repository = value.to_owned(),
"nightly_branch" => stage0.config.nightly_branch = value.to_owned(),

"compiler_date" => stage0.compiler.date = value.to_owned(),
"compiler_version" => stage0.compiler.version = value.to_owned(),

"rustfmt_date" => {
stage0.rustfmt.get_or_insert(VersionMetadata::default()).date = value.to_owned();
}
"rustfmt_version" => {
stage0.rustfmt.get_or_insert(VersionMetadata::default()).version = value.to_owned();
}

dist if dist.starts_with("dist") => {
stage0.checksums_sha256.insert(key.to_owned(), value.to_owned());
}

unsupported => {
println!("'{unsupported}' field is not supported.");
}
}
}

stage0
}
2 changes: 1 addition & 1 deletion src/tools/bump-stage0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2021"

[dependencies]
anyhow = "1.0.34"
build_helper = { path = "../build_helper" }
curl = "0.4.38"
indexmap = { version = "2.0.0", features = ["serde"] }
serde = { version = "1.0.125", features = ["derive"] }
serde_json = { version = "1.0.59", features = ["preserve_order"] }
toml = "0.5.7"
Loading

0 comments on commit b46c3f2

Please sign in to comment.