Skip to content

Commit 6bbadf2

Browse files
committed
use RUSTC_WRAPPER instead of RUSTC
fix RUSTC_WRAPPER hacks
1 parent a1d1805 commit 6bbadf2

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

src/bootstrap/src/bin/rustc.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::path::PathBuf;
2020
use std::process::{Child, Command};
2121
use std::time::Instant;
2222

23-
use dylib_util::{dylib_path, dylib_path_var};
23+
use dylib_util::{dylib_path, dylib_path_var, exe};
2424

2525
#[path = "../utils/bin_helpers.rs"]
2626
mod bin_helpers;
@@ -29,8 +29,10 @@ mod bin_helpers;
2929
mod dylib_util;
3030

3131
fn main() {
32-
let args = env::args_os().skip(1).collect::<Vec<_>>();
33-
let arg = |name| args.windows(2).find(|args| args[0] == name).and_then(|args| args[1].to_str());
32+
let orig_args = env::args_os().skip(1).collect::<Vec<_>>();
33+
let mut args = orig_args.clone();
34+
let arg =
35+
|name| orig_args.windows(2).find(|args| args[0] == name).and_then(|args| args[1].to_str());
3436

3537
// We don't use the stage in this shim, but let's parse it to make sure that we're invoked
3638
// by bootstrap, or that we provide a helpful error message if not.
@@ -56,12 +58,33 @@ fn main() {
5658
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
5759
let on_fail = env::var_os("RUSTC_ON_FAIL").map(Command::new);
5860

59-
let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
61+
let rustc_real = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
6062
let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
6163
let mut dylib_path = dylib_path();
6264
dylib_path.insert(0, PathBuf::from(&libdir));
6365

64-
let mut cmd = Command::new(rustc);
66+
// if we're running clippy, trust cargo-clippy to set clippy-driver appropriately (and don't override it with rustc).
67+
// otherwise, substitute whatever cargo thinks rustc should be with RUSTC_REAL.
68+
// NOTE: this means we ignore RUSTC in the environment.
69+
// FIXME: We might want to consider removing RUSTC_REAL and setting RUSTC directly?
70+
let target_name = target
71+
.map(|s| s.to_owned())
72+
.unwrap_or_else(|| env::var("CFG_COMPILER_HOST_TRIPLE").unwrap());
73+
let is_clippy = args[0].to_string_lossy().ends_with(&exe("clippy-driver", &target_name));
74+
let rustc_driver = if is_clippy {
75+
args.remove(0)
76+
} else {
77+
args.remove(0);
78+
rustc_real
79+
};
80+
81+
let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER_REAL") {
82+
let mut cmd = Command::new(wrapper);
83+
cmd.arg(rustc_driver);
84+
cmd
85+
} else {
86+
Command::new(rustc_driver)
87+
};
6588
cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
6689

6790
// Get the name of the crate we're compiling, if any.

src/bootstrap/src/core/builder.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,17 @@ impl<'a> Builder<'a> {
16141614
// Clippy support is a hack and uses the default `cargo-clippy` in path.
16151615
// Don't override RUSTC so that the `cargo-clippy` in path will be run.
16161616
if cmd != "clippy" {
1617-
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
1617+
// Set RUSTC_WRAPPER to the bootstrap shim, which switches between beta and in-tree
1618+
// sysroot depending on whether we're building build scripts.
1619+
// NOTE: we intentionally use RUSTC_WRAPPER so that we can support clippy - RUSTC is not
1620+
// respected by clippy-driver; RUSTC_WRAPPER happens earlier, before clippy runs.
1621+
cargo.env("RUSTC_WRAPPER", self.bootstrap_out.join("rustc"));
1622+
1623+
// Someone might have set some previous rustc wrapper (e.g.
1624+
// sccache) before bootstrap overrode it. Respect that variable.
1625+
if let Some(existing_wrapper) = env::var_os("RUSTC_WRAPPER") {
1626+
cargo.env("RUSTC_WRAPPER_REAL", existing_wrapper);
1627+
}
16181628
}
16191629

16201630
// Dealing with rpath here is a little special, so let's go into some

src/bootstrap/src/utils/dylib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,16 @@ pub fn dylib_path() -> Vec<std::path::PathBuf> {
2525
};
2626
std::env::split_paths(&var).collect()
2727
}
28+
29+
/// Given an executable called `name`, return the filename for the
30+
/// executable for a particular target.
31+
#[allow(dead_code)]
32+
pub fn exe(name: &str, target: &str) -> String {
33+
if target.contains("windows") {
34+
format!("{name}.exe")
35+
} else if target.contains("uefi") {
36+
format!("{name}.efi")
37+
} else {
38+
name.to_string()
39+
}
40+
}

src/bootstrap/src/utils/helpers.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,8 @@ macro_rules! t {
4545
}
4646
pub use t;
4747

48-
/// Given an executable called `name`, return the filename for the
49-
/// executable for a particular target.
5048
pub fn exe(name: &str, target: TargetSelection) -> String {
51-
if target.contains("windows") {
52-
format!("{name}.exe")
53-
} else if target.contains("uefi") {
54-
format!("{name}.efi")
55-
} else {
56-
name.to_string()
57-
}
49+
crate::utils::dylib::exe(name, &target.triple)
5850
}
5951

6052
/// Returns `true` if the file name given looks like a dynamic library.

0 commit comments

Comments
 (0)