From faec12461f34db4145000a891c4ee5df2e8d4132 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 10 Oct 2020 16:41:31 +0200 Subject: [PATCH] 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 --- build_sysroot/build_sysroot.sh | 4 +- src/bin/cg_clif.rs | 6 +- src/bin/cg_clif_build_sysroot.rs | 112 +++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 src/bin/cg_clif_build_sysroot.rs diff --git a/build_sysroot/build_sysroot.sh b/build_sysroot/build_sysroot.sh index 930f3054b03..04c82ca2a51 100755 --- a/build_sysroot/build_sysroot.sh +++ b/build_sysroot/build_sysroot.sh @@ -16,8 +16,8 @@ rm -r sysroot/ 2>/dev/null || true # Use rustc with cg_clif as hotpluggable backend instead of the custom cg_clif driver so that # build scripts are still compiled using cg_llvm. -export RUSTC=rustc -export RUSTFLAGS=$RUSTFLAGS" -Ztrim-diagnostic-paths=no -Zcodegen-backend=$(pwd)/../target/"$CHANNEL"/librustc_codegen_cranelift."$dylib_ext" --sysroot $(pwd)/sysroot" +export RUSTC=$(pwd)/../"target/"$CHANNEL"/cg_clif_build_sysroot" +export RUSTFLAGS=$RUSTFLAGS" --clif" # Build libs export RUSTFLAGS="$RUSTFLAGS -Zforce-unstable-if-unmarked -Cpanic=abort" diff --git a/src/bin/cg_clif.rs b/src/bin/cg_clif.rs index 8ff1bf271ef..b7524993ad7 100644 --- a/src/bin/cg_clif.rs +++ b/src/bin/cg_clif.rs @@ -13,11 +13,11 @@ use rustc_target::spec::PanicStrategy; #[derive(Default)] -pub struct TimePassesCallbacks { +pub struct CraneliftPassesCallbacks { time_passes: bool, } -impl rustc_driver::Callbacks for TimePassesCallbacks { +impl rustc_driver::Callbacks for CraneliftPassesCallbacks { fn config(&mut self, config: &mut interface::Config) { // If a --prints=... option has been given, we don't print the "total" // time because it will mess up the --prints output. See #64339. @@ -47,7 +47,7 @@ fn config(&mut self, config: &mut interface::Config) { fn main() { let start = std::time::Instant::now(); rustc_driver::init_rustc_env_logger(); - let mut callbacks = TimePassesCallbacks::default(); + let mut callbacks = CraneliftPassesCallbacks::default(); rustc_driver::install_ice_hook(); let exit_code = rustc_driver::catch_with_exit_code(|| { let mut use_jit = false; diff --git a/src/bin/cg_clif_build_sysroot.rs b/src/bin/cg_clif_build_sysroot.rs new file mode 100644 index 00000000000..fe8bcf8b791 --- /dev/null +++ b/src/bin/cg_clif_build_sysroot.rs @@ -0,0 +1,112 @@ +//! The only difference between this and cg_clif.rs is that this binary defaults to using cg_llvm +//! instead of cg_clif and requires `--clif` to use cg_clif and that this binary doesn't have JIT +//! support. +//! This is necessary as with Cargo `RUSTC` applies to both target crates and host crates. The host +//! crates must be built with cg_llvm as we are currently building a sysroot for cg_clif. +//! `RUSTFLAGS` however is only applied to target crates, so `--clif` would only be passed to the +//! target crates. + +#![feature(rustc_private)] + +extern crate rustc_data_structures; +extern crate rustc_driver; +extern crate rustc_interface; +extern crate rustc_session; +extern crate rustc_target; + +use std::path::PathBuf; + +use rustc_interface::interface; +use rustc_session::config::ErrorOutputType; +use rustc_session::early_error; +use rustc_target::spec::PanicStrategy; + +fn find_sysroot() -> String { + // Taken from https://github.com/Manishearth/rust-clippy/pull/911. + let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); + let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); + match (home, toolchain) { + (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain), + _ => option_env!("RUST_SYSROOT") + .expect("need to specify RUST_SYSROOT env var or use rustup or multirust") + .to_owned(), + } +} + +pub struct CraneliftPassesCallbacks { + use_clif: bool, +} + +impl rustc_driver::Callbacks for CraneliftPassesCallbacks { + fn config(&mut self, config: &mut interface::Config) { + if !self.use_clif { + config.opts.maybe_sysroot = Some(PathBuf::from(find_sysroot())); + return; + } + + // FIXME workaround for an ICE + config.opts.debugging_opts.trim_diagnostic_paths = false; + + config.opts.cg.panic = Some(PanicStrategy::Abort); + config.opts.debugging_opts.panic_abort_tests = true; + config.opts.maybe_sysroot = Some( + std::env::current_exe() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .join("build_sysroot") + .join("sysroot"), + ); + } +} + +fn main() { + rustc_driver::init_rustc_env_logger(); + rustc_driver::install_ice_hook(); + let exit_code = rustc_driver::catch_with_exit_code(|| { + let mut use_clif = false; + + let args = std::env::args_os() + .enumerate() + .map(|(i, arg)| { + arg.into_string().unwrap_or_else(|arg| { + early_error( + ErrorOutputType::default(), + &format!("Argument {} is not valid Unicode: {:?}", i, arg), + ) + }) + }) + .filter(|arg| { + if arg == "--clif" { + use_clif = true; + false + } else { + true + } + }) + .collect::>(); + + let mut callbacks = CraneliftPassesCallbacks { use_clif }; + + rustc_driver::run_compiler( + &args, + &mut callbacks, + None, + None, + if use_clif { + Some(Box::new(move |_| { + Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { + config: rustc_codegen_cranelift::BackendConfig { use_jit: false }, + }) + })) + } else { + None + }, + ) + }); + std::process::exit(exit_code) +} -- 2.44.0