Skip to content

Commit a6ea472

Browse files
committed
use RUSTC_WRAPPER instead of RUSTC
fix RUSTC_WRAPPER hacks
1 parent 4afb1d5 commit a6ea472

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

src/bootstrap/src/bin/rustc.rs

+28-5
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
let verbose = bin_helpers::parse_rustc_verbose();
3638

@@ -54,12 +56,33 @@ fn main() {
5456
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
5557
let on_fail = env::var_os("RUSTC_ON_FAIL").map(Command::new);
5658

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

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

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

src/bootstrap/src/core/builder.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,17 @@ impl<'a> Builder<'a> {
16331633
// Clippy support is a hack and uses the default `cargo-clippy` in path.
16341634
// Don't override RUSTC so that the `cargo-clippy` in path will be run.
16351635
if cmd != "clippy" {
1636-
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
1636+
// Set RUSTC_WRAPPER to the bootstrap shim, which switches between beta and in-tree
1637+
// sysroot depending on whether we're building build scripts.
1638+
// NOTE: we intentionally use RUSTC_WRAPPER so that we can support clippy - RUSTC is not
1639+
// respected by clippy-driver; RUSTC_WRAPPER happens earlier, before clippy runs.
1640+
cargo.env("RUSTC_WRAPPER", self.bootstrap_out.join("rustc"));
1641+
1642+
// Someone might have set some previous rustc wrapper (e.g.
1643+
// sccache) before bootstrap overrode it. Respect that variable.
1644+
if let Some(existing_wrapper) = env::var_os("RUSTC_WRAPPER") {
1645+
cargo.env("RUSTC_WRAPPER_REAL", existing_wrapper);
1646+
}
16371647
}
16381648

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

src/bootstrap/src/utils/dylib.rs

+13
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

+1-9
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,8 @@ macro_rules! t {
4444
}
4545
pub use t;
4646

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

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

0 commit comments

Comments
 (0)