]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/bin/cg_clif_build_sysroot.rs
Merge commit '9a0c32934ebe376128230aa8da3275697b2053e7' into sync_cg_clif-2021-03-05
[rust.git] / compiler / rustc_codegen_cranelift / src / bin / cg_clif_build_sysroot.rs
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         config.opts.cg.panic = Some(PanicStrategy::Abort);
48         config.opts.debugging_opts.panic_abort_tests = true;
49         config.opts.maybe_sysroot =
50             Some(std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned());
51     }
52 }
53
54 fn main() {
55     rustc_driver::init_rustc_env_logger();
56     rustc_driver::install_ice_hook();
57     let exit_code = rustc_driver::catch_with_exit_code(|| {
58         let mut use_clif = false;
59
60         let args = std::env::args_os()
61             .enumerate()
62             .map(|(i, arg)| {
63                 arg.into_string().unwrap_or_else(|arg| {
64                     early_error(
65                         ErrorOutputType::default(),
66                         &format!("Argument {} is not valid Unicode: {:?}", i, arg),
67                     )
68                 })
69             })
70             .filter(|arg| {
71                 if arg == "--clif" {
72                     use_clif = true;
73                     false
74                 } else {
75                     true
76                 }
77             })
78             .collect::<Vec<_>>();
79
80         let mut callbacks = CraneliftPassesCallbacks { use_clif };
81
82         let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
83         if use_clif {
84             run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
85                 Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
86             })));
87         }
88         run_compiler.run()
89     });
90     std::process::exit(exit_code)
91 }