2021-07-07 04:14:20 -05:00
|
|
|
use std::env;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::process::CommandExt;
|
|
|
|
use std::process::Command;
|
|
|
|
|
2023-08-02 04:06:29 -05:00
|
|
|
include!("../build_system/shared_utils.rs");
|
|
|
|
|
2021-07-07 04:14:20 -05:00
|
|
|
fn main() {
|
2023-02-19 09:28:01 -06:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
|
|
|
let mut sysroot = current_exe.parent().unwrap();
|
|
|
|
if sysroot.file_name().unwrap().to_str().unwrap() == "bin" {
|
|
|
|
sysroot = sysroot.parent().unwrap();
|
|
|
|
}
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-08-02 04:06:29 -05:00
|
|
|
let mut rustflags = vec!["-Cpanic=abort".to_owned(), "-Zpanic-abort-tests".to_owned()];
|
2023-02-15 13:32:58 -06:00
|
|
|
if let Some(name) = option_env!("BUILTIN_BACKEND") {
|
2023-08-02 04:06:29 -05:00
|
|
|
rustflags.push(format!("-Zcodegen-backend={name}"));
|
2023-02-15 13:32:58 -06:00
|
|
|
} else {
|
2023-08-02 04:06:29 -05:00
|
|
|
let dylib = sysroot.join(if cfg!(windows) { "bin" } else { "lib" }).join(
|
|
|
|
env::consts::DLL_PREFIX.to_string()
|
|
|
|
+ "rustc_codegen_cranelift"
|
|
|
|
+ env::consts::DLL_SUFFIX,
|
2023-02-15 13:32:58 -06:00
|
|
|
);
|
2023-08-02 04:06:29 -05:00
|
|
|
rustflags.push(format!("-Zcodegen-backend={}", dylib.to_str().unwrap()));
|
2023-02-15 13:32:58 -06:00
|
|
|
}
|
2023-08-02 04:06:29 -05:00
|
|
|
rustflags.push("--sysroot".to_owned());
|
|
|
|
rustflags.push(sysroot.to_str().unwrap().to_owned());
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-02-13 04:11:45 -06:00
|
|
|
let cargo = if let Some(cargo) = option_env!("CARGO") {
|
|
|
|
cargo
|
|
|
|
} else {
|
|
|
|
// Ensure that the right toolchain is used
|
|
|
|
env::set_var("RUSTUP_TOOLCHAIN", option_env!("TOOLCHAIN_NAME").expect("TOOLCHAIN_NAME"));
|
|
|
|
"cargo"
|
|
|
|
};
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-07-12 12:24:46 -05:00
|
|
|
let mut args = env::args().skip(1).collect::<Vec<_>>();
|
|
|
|
if args.get(0).map(|arg| &**arg) == Some("clif") {
|
|
|
|
// Avoid infinite recursion when invoking `cargo-clif` as cargo subcommand using
|
|
|
|
// `cargo clif`.
|
|
|
|
args.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let args: Vec<_> = match args.get(0).map(|arg| &**arg) {
|
2021-07-07 04:14:20 -05:00
|
|
|
Some("jit") => {
|
2023-08-02 04:06:29 -05:00
|
|
|
rustflags.push("-Cprefer-dynamic".to_owned());
|
2023-07-12 12:24:46 -05:00
|
|
|
args.remove(0);
|
2021-09-03 05:36:33 -05:00
|
|
|
IntoIterator::into_iter(["rustc".to_string()])
|
2023-07-12 12:24:46 -05:00
|
|
|
.chain(args)
|
2021-08-06 09:26:56 -05:00
|
|
|
.chain([
|
|
|
|
"--".to_string(),
|
2022-05-01 08:54:12 -05:00
|
|
|
"-Zunstable-options".to_string(),
|
2021-08-06 09:26:56 -05:00
|
|
|
"-Cllvm-args=mode=jit".to_string(),
|
|
|
|
])
|
2021-07-07 04:14:20 -05:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
Some("lazy-jit") => {
|
2023-08-02 04:06:29 -05:00
|
|
|
rustflags.push("-Cprefer-dynamic".to_owned());
|
2023-07-12 12:24:46 -05:00
|
|
|
args.remove(0);
|
2021-09-03 05:36:33 -05:00
|
|
|
IntoIterator::into_iter(["rustc".to_string()])
|
2023-07-12 12:24:46 -05:00
|
|
|
.chain(args)
|
2021-08-06 09:26:56 -05:00
|
|
|
.chain([
|
|
|
|
"--".to_string(),
|
2022-05-01 08:54:12 -05:00
|
|
|
"-Zunstable-options".to_string(),
|
2021-08-06 09:26:56 -05:00
|
|
|
"-Cllvm-args=mode=jit-lazy".to_string(),
|
|
|
|
])
|
2021-07-07 04:14:20 -05:00
|
|
|
.collect()
|
|
|
|
}
|
2023-07-12 12:24:46 -05:00
|
|
|
_ => args,
|
2021-07-07 04:14:20 -05:00
|
|
|
};
|
|
|
|
|
2023-08-02 04:06:29 -05:00
|
|
|
let mut cmd = Command::new(cargo);
|
|
|
|
cmd.args(args);
|
|
|
|
rustflags_to_cmd_env(
|
|
|
|
&mut cmd,
|
|
|
|
"RUSTFLAGS",
|
|
|
|
&rustflags_from_env("RUSTFLAGS")
|
|
|
|
.into_iter()
|
|
|
|
.chain(rustflags.iter().map(|flag| flag.clone()))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
);
|
|
|
|
rustflags_to_cmd_env(
|
|
|
|
&mut cmd,
|
|
|
|
"RUSTDOCFLAGS",
|
|
|
|
&rustflags_from_env("RUSTDOCFLAGS")
|
|
|
|
.into_iter()
|
|
|
|
.chain(rustflags.iter().map(|flag| flag.clone()))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
);
|
|
|
|
|
2021-07-07 04:14:20 -05:00
|
|
|
#[cfg(unix)]
|
2023-08-02 04:06:29 -05:00
|
|
|
panic!("Failed to spawn cargo: {}", cmd.exec());
|
2021-07-07 04:14:20 -05:00
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2023-08-02 04:06:29 -05:00
|
|
|
std::process::exit(cmd.spawn().unwrap().wait().unwrap().code().unwrap_or(1));
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|