From 724f701f79a57ad83797391b28e04dea6b8318cd Mon Sep 17 00:00:00 2001 From: Samson <16504129+sagudev@users.noreply.github.com> Date: Fri, 31 Jan 2025 19:08:04 +0100 Subject: [PATCH] crown: Pass `--cfg crown` to rustc from crown (#35073) * crown: Pass `--cfg crown` to rustc from crown also includes minor fix in crown for wrapper running based on clippy code Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * fix doc Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * Update python/servo/command_base.py Co-authored-by: Martin Robinson Signed-off-by: Samson <16504129+sagudev@users.noreply.github.com> --------- Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> Signed-off-by: Samson <16504129+sagudev@users.noreply.github.com> Co-authored-by: Martin Robinson --- python/servo/command_base.py | 12 +++++------- support/crown/src/main.rs | 12 +++++++++++- support/crown/tests/run-pass/cfg_crown.rs | 10 ++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 support/crown/tests/run-pass/cfg_crown.rs diff --git a/python/servo/command_base.py b/python/servo/command_base.py index ca026f07758..799fbdca8ee 100644 --- a/python/servo/command_base.py +++ b/python/servo/command_base.py @@ -807,14 +807,12 @@ class CommandBase(object): f'already set to `{current_rustc}` in the parent environment.\n' 'These options conflict, please specify only one of them.') sys.exit(1) - features += ["crown"] env['CARGO_BUILD_RUSTC'] = 'crown' - # Changing `RUSTC` or `CARGO_BUILD_RUSTC` does not cause `cargo check` to - # recheck files with the new compiler. `cargo build` is not affected and - # triggers a rebuild as expected. To also make `check` work as expected, - # we add a dummy `cfg` to RUSTFLAGS when using crown, so as to have different - # RUSTFLAGS when using `crown`, to reliably trigger re-checking. - env['RUSTFLAGS'] = env.get('RUSTFLAGS', "") + " --cfg=crown" + # Modyfing `RUSTC` or `CARGO_BUILD_RUSTC` to use a linter does not cause + # `cargo check` to rebuild. To work around this bug use a `crown` feature + # to invalidate caches and force a rebuild / relint. + # See https://github.com/servo/servo/issues/35072#issuecomment-2600749483 + features += ["crown"] if "-p" not in cargo_args: # We're building specific package, that may not have features features += list(self.features) diff --git a/support/crown/src/main.rs b/support/crown/src/main.rs index 9a3daa092a4..cd64ca7331f 100644 --- a/support/crown/src/main.rs +++ b/support/crown/src/main.rs @@ -22,6 +22,7 @@ extern crate rustc_span; extern crate rustc_trait_selection; extern crate rustc_type_ir; +use std::path::Path; use std::process::ExitCode; use rustc_driver::Callbacks; @@ -64,7 +65,16 @@ fn main() -> ExitCode { let handler = rustc_session::EarlyDiagCtxt::new(rustc_session::config::ErrorOutputType::default()); rustc_driver::init_logger(&handler, rustc_log::LoggerConfig::from_env("CROWN_LOG")); - let args: Vec<_> = std::env::args().collect(); + let mut args: Vec<_> = std::env::args().collect(); + + // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument. + // We're invoking the compiler programmatically, so we ignore this + if args.get(1).map(Path::new).and_then(Path::file_stem) == Some("rustc".as_ref()) { + args.remove(1); + } + + // Pass cfg(crown) to rustc + args.extend(["--cfg".to_owned(), "crown".to_owned()]); match rustc_driver::RunCompiler::new(&args, &mut MyCallbacks).run() { Ok(_) => ExitCode::SUCCESS, diff --git a/support/crown/tests/run-pass/cfg_crown.rs b/support/crown/tests/run-pass/cfg_crown.rs new file mode 100644 index 00000000000..80a53aeb4d4 --- /dev/null +++ b/support/crown/tests/run-pass/cfg_crown.rs @@ -0,0 +1,10 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +//@rustc-env:RUSTC_BOOTSTRAP=1 + +#![allow(dead_code)] + +const _: () = assert!(cfg!(crown), "cfg(crown) not set!"); + +fn main() {}