Skip to content

Commit faec124

Browse files
committed
Use custom driver for sysroot building too
This required another custom driver to ensure that build scripts are built using cg_llvm instead of cg_clif. After this change only rustdoc still uses -Zcodegen-backend
1 parent c352f91 commit faec124

File tree

3 files changed

+117
-5
lines changed

3 files changed

+117
-5
lines changed

build_sysroot/build_sysroot.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ rm -r sysroot/ 2>/dev/null || true
1616

1717
# Use rustc with cg_clif as hotpluggable backend instead of the custom cg_clif driver so that
1818
# build scripts are still compiled using cg_llvm.
19-
export RUSTC=rustc
20-
export RUSTFLAGS=$RUSTFLAGS" -Ztrim-diagnostic-paths=no -Zcodegen-backend=$(pwd)/../target/"$CHANNEL"/librustc_codegen_cranelift."$dylib_ext" --sysroot $(pwd)/sysroot"
19+
export RUSTC=$(pwd)/../"target/"$CHANNEL"/cg_clif_build_sysroot"
20+
export RUSTFLAGS=$RUSTFLAGS" --clif"
2121

2222
# Build libs
2323
export RUSTFLAGS="$RUSTFLAGS -Zforce-unstable-if-unmarked -Cpanic=abort"

src/bin/cg_clif.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use rustc_session::early_error;
1313
use rustc_target::spec::PanicStrategy;
1414

1515
#[derive(Default)]
16-
pub struct TimePassesCallbacks {
16+
pub struct CraneliftPassesCallbacks {
1717
time_passes: bool,
1818
}
1919

20-
impl rustc_driver::Callbacks for TimePassesCallbacks {
20+
impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
2121
fn config(&mut self, config: &mut interface::Config) {
2222
// If a --prints=... option has been given, we don't print the "total"
2323
// time because it will mess up the --prints output. See #64339.
@@ -47,7 +47,7 @@ impl rustc_driver::Callbacks for TimePassesCallbacks {
4747
fn main() {
4848
let start = std::time::Instant::now();
4949
rustc_driver::init_rustc_env_logger();
50-
let mut callbacks = TimePassesCallbacks::default();
50+
let mut callbacks = CraneliftPassesCallbacks::default();
5151
rustc_driver::install_ice_hook();
5252
let exit_code = rustc_driver::catch_with_exit_code(|| {
5353
let mut use_jit = false;

src/bin/cg_clif_build_sysroot.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//! The only difference between this and cg_clif.rs is that this binary defaults to using cg_llvm
2+
//! instead of cg_clif and requires `--clif` to use cg_clif and that this binary doesn't have JIT
3+
//! support.
4+
//! This is necessary as with Cargo `RUSTC` applies to both target crates and host crates. The host
5+
//! crates must be built with cg_llvm as we are currently building a sysroot for cg_clif.
6+
//! `RUSTFLAGS` however is only applied to target crates, so `--clif` would only be passed to the
7+
//! target crates.
8+
9+
#![feature(rustc_private)]
10+
11+
extern crate rustc_data_structures;
12+
extern crate rustc_driver;
13+
extern crate rustc_interface;
14+
extern crate rustc_session;
15+
extern crate rustc_target;
16+
17+
use std::path::PathBuf;
18+
19+
use rustc_interface::interface;
20+
use rustc_session::config::ErrorOutputType;
21+
use rustc_session::early_error;
22+
use rustc_target::spec::PanicStrategy;
23+
24+
fn find_sysroot() -> String {
25+
// Taken from https://github.com/Manishearth/rust-clippy/pull/911.
26+
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
27+
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
28+
match (home, toolchain) {
29+
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
30+
_ => option_env!("RUST_SYSROOT")
31+
.expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
32+
.to_owned(),
33+
}
34+
}
35+
36+
pub struct CraneliftPassesCallbacks {
37+
use_clif: bool,
38+
}
39+
40+
impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
41+
fn config(&mut self, config: &mut interface::Config) {
42+
if !self.use_clif {
43+
config.opts.maybe_sysroot = Some(PathBuf::from(find_sysroot()));
44+
return;
45+
}
46+
47+
// FIXME workaround for an ICE
48+
config.opts.debugging_opts.trim_diagnostic_paths = false;
49+
50+
config.opts.cg.panic = Some(PanicStrategy::Abort);
51+
config.opts.debugging_opts.panic_abort_tests = true;
52+
config.opts.maybe_sysroot = Some(
53+
std::env::current_exe()
54+
.unwrap()
55+
.parent()
56+
.unwrap()
57+
.parent()
58+
.unwrap()
59+
.parent()
60+
.unwrap()
61+
.join("build_sysroot")
62+
.join("sysroot"),
63+
);
64+
}
65+
}
66+
67+
fn main() {
68+
rustc_driver::init_rustc_env_logger();
69+
rustc_driver::install_ice_hook();
70+
let exit_code = rustc_driver::catch_with_exit_code(|| {
71+
let mut use_clif = false;
72+
73+
let args = std::env::args_os()
74+
.enumerate()
75+
.map(|(i, arg)| {
76+
arg.into_string().unwrap_or_else(|arg| {
77+
early_error(
78+
ErrorOutputType::default(),
79+
&format!("Argument {} is not valid Unicode: {:?}", i, arg),
80+
)
81+
})
82+
})
83+
.filter(|arg| {
84+
if arg == "--clif" {
85+
use_clif = true;
86+
false
87+
} else {
88+
true
89+
}
90+
})
91+
.collect::<Vec<_>>();
92+
93+
let mut callbacks = CraneliftPassesCallbacks { use_clif };
94+
95+
rustc_driver::run_compiler(
96+
&args,
97+
&mut callbacks,
98+
None,
99+
None,
100+
if use_clif {
101+
Some(Box::new(move |_| {
102+
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend {
103+
config: rustc_codegen_cranelift::BackendConfig { use_jit: false },
104+
})
105+
}))
106+
} else {
107+
None
108+
},
109+
)
110+
});
111+
std::process::exit(exit_code)
112+
}

0 commit comments

Comments
 (0)