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
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
9 #![feature(rustc_private)]
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
;
17 use std
::path
::PathBuf
;
19 use rustc_interface
::interface
;
20 use rustc_session
::config
::ErrorOutputType
;
21 use rustc_session
::early_error
;
22 use rustc_target
::spec
::PanicStrategy
;
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")
36 pub struct CraneliftPassesCallbacks
{
40 impl rustc_driver
::Callbacks
for CraneliftPassesCallbacks
{
41 fn config(&mut self, config
: &mut interface
::Config
) {
43 config
.opts
.maybe_sysroot
= Some(PathBuf
::from(find_sysroot()));
47 config
.opts
.cg
.panic
= Some(PanicStrategy
::Abort
);
48 config
.opts
.debugging_opts
.panic_abort_tests
= true;
49 config
.opts
.maybe_sysroot
= Some(
50 std
::env
::current_exe()
58 .join("build_sysroot")
65 rustc_driver
::init_rustc_env_logger();
66 rustc_driver
::install_ice_hook();
67 let exit_code
= rustc_driver
::catch_with_exit_code(|| {
68 let mut use_clif
= false;
70 let args
= std
::env
::args_os()
73 arg
.into_string().unwrap_or_else(|arg
| {
75 ErrorOutputType
::default(),
76 &format
!("Argument {} is not valid Unicode: {:?}", i
, arg
),
90 let mut callbacks
= CraneliftPassesCallbacks { use_clif }
;
92 let mut run_compiler
= rustc_driver
::RunCompiler
::new(&args
, &mut callbacks
);
94 run_compiler
.set_make_codegen_backend(Some(Box
::new(move |_
| {
95 Box
::new(rustc_codegen_cranelift
::CraneliftCodegenBackend
{
96 config
: rustc_codegen_cranelift
::BackendConfig { use_jit: false }
,
102 std
::process
::exit(exit_code
)