Skip to content

Commit 2979a2c

Browse files
committed
use RUSTC_WRAPPER instead of RUSTC
1 parent 336ed0e commit 2979a2c

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

src/bootstrap/src/bin/rustc.rs

+38-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
//! never get replaced.
1717
1818
use std::env;
19-
use std::path::PathBuf;
19+
use std::path::{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 stage = bin_helpers::parse_rustc_stage();
3638
let verbose = bin_helpers::parse_rustc_verbose();
@@ -54,12 +56,42 @@ 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+
// Cargo doesn't respect RUSTC_WRAPPER for version information >:(
76+
// don't remove the first arg if we're being run as RUSTC instead of RUSTC_WRAPPER.
77+
// Cargo also sometimes doesn't pass the `.exe` suffix on Windows - add it manually.
78+
let current_exe = env::current_exe().expect("couldn't get path to rustc shim");
79+
// NOTE: we intentionally pass the name of the host, not the target.
80+
let host = env::var("CFG_COMPILER_BUILD_TRIPLE").unwrap();
81+
let arg0 = exe(args[0].to_str().expect("only utf8 paths are supported"), &host);
82+
if Path::new(&arg0) == current_exe {
83+
args.remove(0);
84+
}
85+
rustc_real
86+
};
87+
88+
let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER_REAL") {
89+
let mut cmd = Command::new(wrapper);
90+
cmd.arg(rustc_driver);
91+
cmd
92+
} else {
93+
Command::new(rustc_driver)
94+
};
6395
cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
6496

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

src/bootstrap/src/core/builder.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,19 @@ 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+
// 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+
// NOTE: we also need to set RUSTC so cargo can run `rustc -vV`; apparently that ignores RUSTC_WRAPPER >:(
16171623
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
1624+
1625+
// Someone might have set some previous rustc wrapper (e.g.
1626+
// sccache) before bootstrap overrode it. Respect that variable.
1627+
if let Some(existing_wrapper) = env::var_os("RUSTC_WRAPPER") {
1628+
cargo.env("RUSTC_WRAPPER_REAL", existing_wrapper);
1629+
}
16181630
}
16191631

16201632
// Dealing with rpath here is a little special, so let's go into some
@@ -1991,7 +2003,11 @@ impl<'a> Builder<'a> {
19912003
// Environment variables *required* throughout the build
19922004
//
19932005
// FIXME: should update code to not require this env var
2006+
2007+
// The host this new compiler will *run* on.
19942008
cargo.env("CFG_COMPILER_HOST_TRIPLE", target.triple);
2009+
// The host this new compiler is being *built* on.
2010+
cargo.env("CFG_COMPILER_BUILD_TRIPLE", compiler.host.triple);
19952011

19962012
// Set this for all builds to make sure doc builds also get it.
19972013
cargo.env("CFG_RELEASE_CHANNEL", &self.config.channel);

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
@@ -46,16 +46,8 @@ macro_rules! t {
4646
}
4747
pub use t;
4848

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

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

0 commit comments

Comments
 (0)