Remove cg_clif_build_sysroot

This commit is contained in:
bjorn3 2022-02-13 18:33:30 +01:00
parent 377f44d38c
commit 617171e930
3 changed files with 31 additions and 118 deletions

View file

@ -1,4 +1,3 @@
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
@ -22,25 +21,21 @@ pub(crate) fn build_sysroot(
fs::create_dir_all(target_dir.join("lib")).unwrap();
// Copy the backend
for file in ["cg_clif", "cg_clif_build_sysroot"] {
try_hard_link(
cg_clif_build_dir.join(get_file_name(file, "bin")),
target_dir.join("bin").join(get_file_name(file, "bin")),
);
}
let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
let cg_clif_dylib_path = target_dir
.join(if cfg!(windows) {
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
// binaries.
"bin"
} else {
"lib"
})
.join(&cg_clif_dylib);
try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
try_hard_link(
cg_clif_build_dir.join(&cg_clif_dylib),
target_dir
.join(if cfg!(windows) {
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
// binaries.
"bin"
} else {
"lib"
})
.join(cg_clif_dylib),
cg_clif_build_dir.join(get_file_name("cg_clif", "bin")),
target_dir.join("bin").join(get_file_name("cg_clif", "bin")),
);
// Build and copy cargo wrapper
@ -117,7 +112,13 @@ pub(crate) fn build_sysroot(
}
}
SysrootKind::Clif => {
build_clif_sysroot_for_triple(channel, target_dir, host_triple, None);
build_clif_sysroot_for_triple(
channel,
target_dir,
host_triple,
&cg_clif_dylib_path,
None,
);
if host_triple != target_triple {
// When cross-compiling it is often necessary to manually pick the right linker
@ -126,7 +127,13 @@ pub(crate) fn build_sysroot(
} else {
None
};
build_clif_sysroot_for_triple(channel, target_dir, target_triple, linker);
build_clif_sysroot_for_triple(
channel,
target_dir,
target_triple,
&cg_clif_dylib_path,
linker,
);
}
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
@ -145,6 +152,7 @@ fn build_clif_sysroot_for_triple(
channel: &str,
target_dir: &Path,
triple: &str,
cg_clif_dylib_path: &Path,
linker: Option<&str>,
) {
match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
@ -178,7 +186,8 @@ fn build_clif_sysroot_for_triple(
// Build sysroot
let mut build_cmd = Command::new("cargo");
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
if channel == "release" {
build_cmd.arg("--release");
rustflags.push_str(" -Zmir-opt-level=3");
@ -188,10 +197,6 @@ fn build_clif_sysroot_for_triple(
write!(rustflags, " -Clinker={}", linker).unwrap();
}
build_cmd.env("RUSTFLAGS", rustflags);
build_cmd.env(
"RUSTC",
env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"),
);
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
spawn_and_wait(build_cmd);

View file

@ -86,6 +86,7 @@ pub fn main() {
arg => arg_error!("Unexpected argument {}", arg),
}
}
target_dir = std::env::current_dir().unwrap().join(target_dir);
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
host_triple

View file

@ -1,93 +0,0 @@
//! 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)]
#![warn(rust_2018_idioms)]
#![warn(unused_lifetimes)]
#![warn(unreachable_pub)]
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;
}
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().to_owned());
}
}
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::<Vec<_>>();
let mut callbacks = CraneliftPassesCallbacks { use_clif };
let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
if use_clif {
run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
})));
}
run_compiler.run()
});
std::process::exit(exit_code)
}