Skip to content

Commit 69b292d

Browse files
committed
bootstrap: Add RUSTC_EMIT option to pass on --emit to crates during bootstrap
1 parent a167cbd commit 69b292d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/bootstrap/bin/rustc.rs

+41
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
include!("../dylib_util.rs");
1919

2020
use std::env;
21+
use std::ffi::OsStr;
2122
use std::path::PathBuf;
2223
use std::process::{Child, Command};
2324
use std::str::FromStr;
@@ -70,6 +71,46 @@ fn main() {
7071
cmd.arg("-Ztime-passes");
7172
}
7273
}
74+
75+
if let Some(crate_targets) = env::var_os("RUSTC_EMIT") {
76+
let formats: Vec<&str> = crate_targets
77+
.to_str()
78+
.unwrap()
79+
.split(';')
80+
.filter_map(|target| {
81+
let mut iter = target.split('=');
82+
let krate = iter.next().unwrap();
83+
let formats = iter.next().unwrap();
84+
assert!(iter.next().is_none(), "Invalid format for RUSTC_EMIT");
85+
(krate.trim() == crate_name || krate.trim() == "*").then_some(formats)
86+
})
87+
.flat_map(|formats| formats.split(',').map(|format| format.trim()))
88+
.collect();
89+
90+
if !formats.is_empty() {
91+
let dir = PathBuf::from(
92+
env::var_os("RUSTC_EMIT_DIR").expect("RUSTC_EMIT_DIR was not set"),
93+
);
94+
std::fs::create_dir_all(&dir).expect("unable to create dump directory");
95+
96+
for format in formats {
97+
let ext = match format {
98+
"llvm-ir" => "ll",
99+
"llvm-bc" => "bc",
100+
"asm" => {
101+
if target.map_or(false, |target| target.starts_with("x86")) {
102+
cmd.arg("-Cllvm-args=-x86-asm-syntax=intel");
103+
}
104+
"s"
105+
}
106+
_ => format,
107+
};
108+
let mut arg = OsStr::new(&format!("--emit={format}=")).to_owned();
109+
arg.push(dir.join(format!("{crate_name}.{ext}")).as_os_str());
110+
cmd.arg(arg);
111+
}
112+
}
113+
}
73114
}
74115

75116
// Print backtrace in case of ICE

src/bootstrap/builder.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,11 @@ impl<'a> Builder<'a> {
11831183
let mut cargo = self.bare_cargo(compiler, mode, target, cmd);
11841184
let out_dir = self.stage_out(compiler, mode);
11851185

1186+
cargo.env(
1187+
"RUSTC_EMIT_DIR",
1188+
self.out.join(target.triple).join("emit").join(out_dir.file_name().unwrap()),
1189+
);
1190+
11861191
// Codegen backends are not yet tracked by -Zbinary-dep-depinfo,
11871192
// so we need to explicitly clear out if they've been updated.
11881193
for backend in self.codegen_backends(compiler) {

0 commit comments

Comments
 (0)