From d32ff14b86c72f55a113a3f477c42b2995e8c620 Mon Sep 17 00:00:00 2001 From: est31 Date: Tue, 23 Aug 2022 22:14:12 +0200 Subject: [PATCH] Add replace-version-placeholder tool This tool is to be ran at specific points in the release process to replace the version place holder made by stabilizations with the version number. --- Cargo.lock | 8 ++++ Cargo.toml | 1 + src/bootstrap/builder.rs | 8 +++- src/bootstrap/run.rs | 22 +++++++++++ src/bootstrap/test.rs | 37 +++++++++++++++++++ src/bootstrap/tool.rs | 1 + .../replace-version-placeholder/Cargo.toml | 10 +++++ .../replace-version-placeholder/src/main.rs | 30 +++++++++++++++ 8 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/tools/replace-version-placeholder/Cargo.toml create mode 100644 src/tools/replace-version-placeholder/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 2ea32a80120..dda8c03e256 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3293,6 +3293,14 @@ dependencies = [ "winapi", ] +[[package]] +name = "replace-version-placeholder" +version = "0.1.0" +dependencies = [ + "tidy", + "walkdir", +] + [[package]] name = "rls" version = "1.41.0" diff --git a/Cargo.toml b/Cargo.toml index ffc886d47f3..1e83f05e0ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ members = [ "src/tools/jsondocck", "src/tools/html-checker", "src/tools/bump-stage0", + "src/tools/replace-version-placeholder", "src/tools/lld-wrapper", ] diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index ba732cd7742..c4710e8faec 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -647,6 +647,7 @@ macro_rules! describe { test::CrateRustdocJsonTypes, test::Linkcheck, test::TierCheck, + test::ReplacePlaceholderTest, test::Cargotest, test::Cargo, test::Rls, @@ -746,7 +747,12 @@ macro_rules! describe { install::Src, install::Rustc ), - Kind::Run => describe!(run::ExpandYamlAnchors, run::BuildManifest, run::BumpStage0), + Kind::Run => describe!( + run::ExpandYamlAnchors, + run::BuildManifest, + run::BumpStage0, + run::ReplaceVersionPlaceholder, + ), // These commands either don't use paths, or they're special-cased in Build::build() Kind::Clean | Kind::Format | Kind::Setup => vec![], } diff --git a/src/bootstrap/run.rs b/src/bootstrap/run.rs index 25abe7a72fd..511872903d1 100644 --- a/src/bootstrap/run.rs +++ b/src/bootstrap/run.rs @@ -103,3 +103,25 @@ fn run(self, builder: &Builder<'_>) -> Self::Output { builder.run(&mut cmd); } } + +#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)] +pub struct ReplaceVersionPlaceholder; + +impl Step for ReplaceVersionPlaceholder { + type Output = (); + const ONLY_HOSTS: bool = true; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.path("src/tools/replace-version-placeholder") + } + + fn make_run(run: RunConfig<'_>) { + run.builder.ensure(ReplaceVersionPlaceholder); + } + + fn run(self, builder: &Builder<'_>) -> Self::Output { + let mut cmd = builder.tool_cmd(Tool::ReplaceVersionPlaceholder); + cmd.arg(&builder.src); + builder.run(&mut cmd); + } +} diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index c759d9b88e2..9cbdb3aca32 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -2527,6 +2527,43 @@ fn run(self, builder: &Builder<'_>) { } } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct ReplacePlaceholderTest; + +impl Step for ReplacePlaceholderTest { + type Output = (); + const ONLY_HOSTS: bool = true; + const DEFAULT: bool = true; + + /// Ensure the version placeholder replacement tool builds + fn run(self, builder: &Builder<'_>) { + builder.info("build check for version replacement placeholder"); + + // Test the version placeholder replacement tool itself. + let bootstrap_host = builder.config.build; + let compiler = builder.compiler(0, bootstrap_host); + let cargo = tool::prepare_tool_cargo( + builder, + compiler, + Mode::ToolBootstrap, + bootstrap_host, + "test", + "src/tools/replace-version-placeholder", + SourceType::InTree, + &[], + ); + try_run(builder, &mut cargo.into()); + } + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.path("src/tools/replace-version-placeholder") + } + + fn make_run(run: RunConfig<'_>) { + run.builder.ensure(Self); + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct LintDocs { pub compiler: Compiler, diff --git a/src/bootstrap/tool.rs b/src/bootstrap/tool.rs index 570da20e7d6..5bb40014eb9 100644 --- a/src/bootstrap/tool.rs +++ b/src/bootstrap/tool.rs @@ -378,6 +378,7 @@ fn run(self, builder: &Builder<'_>) -> PathBuf { JsonDocCk, "src/tools/jsondocck", "jsondocck"; HtmlChecker, "src/tools/html-checker", "html-checker"; BumpStage0, "src/tools/bump-stage0", "bump-stage0"; + ReplaceVersionPlaceholder, "src/tools/replace-version-placeholder", "replace-version-placeholder"; ); #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)] diff --git a/src/tools/replace-version-placeholder/Cargo.toml b/src/tools/replace-version-placeholder/Cargo.toml new file mode 100644 index 00000000000..346ce6bd1db --- /dev/null +++ b/src/tools/replace-version-placeholder/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "replace-version-placeholder" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tidy = { path = "../tidy" } +walkdir = "2" diff --git a/src/tools/replace-version-placeholder/src/main.rs b/src/tools/replace-version-placeholder/src/main.rs new file mode 100644 index 00000000000..146e53f2e9a --- /dev/null +++ b/src/tools/replace-version-placeholder/src/main.rs @@ -0,0 +1,30 @@ +use std::path::PathBuf; +use tidy::{t, walk}; + +pub const VERSION_PLACEHOLDER: &str = "CURRENT_RUSTC_VERSION"; + +fn main() { + let root_path: PathBuf = std::env::args_os().nth(1).expect("need path to root of repo").into(); + let version_path = root_path.join("src").join("version"); + let version_str = t!(std::fs::read_to_string(&version_path), version_path); + let version_str = version_str.trim(); + walk::walk( + &root_path, + &mut |path| { + walk::filter_dirs(path) + // We exempt these as they require the placeholder + // for their operation + || path.ends_with("compiler/rustc_passes/src/lib_features.rs") + || path.ends_with("src/tools/tidy/src/features/version.rs") + || path.ends_with("src/tools/replace-version-placeholder") + }, + &mut |entry, contents| { + if !contents.contains(VERSION_PLACEHOLDER) { + return; + } + let new_contents = contents.replace(VERSION_PLACEHOLDER, version_str); + let path = entry.path(); + t!(std::fs::write(&path, new_contents), path); + }, + ); +}