rust/build_system/mod.rs

207 lines
6.2 KiB
Rust
Raw Normal View History

use std::env;
use std::path::PathBuf;
use std::process;
use self::utils::{is_ci, Compiler};
2022-08-05 07:57:19 -05:00
2022-09-26 06:56:18 -05:00
mod abi_cafe;
mod bench;
mod build_backend;
mod build_sysroot;
mod config;
2022-12-01 07:30:03 -06:00
mod path;
mod prepare;
mod rustc_info;
2022-07-30 04:32:54 -05:00
mod tests;
2022-07-31 05:15:56 -05:00
mod utils;
const USAGE: &str = r#"The build system of cg_clif.
USAGE:
./y.rs prepare [--out-dir DIR]
./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features]
./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features]
./y.rs bench [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features]
OPTIONS:
--sysroot none|clif|llvm
Which sysroot libraries to use:
`none` will not include any standard library in the sysroot.
`clif` will build the standard library using Cranelift.
`llvm` will use the pre-compiled standard library of rustc which is compiled with LLVM.
--out-dir DIR
Specify the directory in which the download, build and dist directories are stored.
By default this is the working directory.
--no-unstable-features
fSome features are not yet ready for production usage. This option will disable these
features. This includes the JIT mode and inline assembly support.
"#;
fn usage() {
eprintln!("{USAGE}");
}
macro_rules! arg_error {
($($err:tt)*) => {{
eprintln!($($err)*);
usage();
std::process::exit(1);
}};
}
2022-07-30 04:32:54 -05:00
#[derive(PartialEq, Debug)]
enum Command {
Prepare,
Build,
2022-07-30 04:32:54 -05:00
Test,
Bench,
}
2022-07-30 04:32:54 -05:00
#[derive(Copy, Clone, Debug)]
pub(crate) enum SysrootKind {
None,
Clif,
Llvm,
}
pub fn main() {
if env::var("RUST_BACKTRACE").is_err() {
env::set_var("RUST_BACKTRACE", "1");
}
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
2022-08-05 07:57:19 -05:00
if is_ci() {
// Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway
env::set_var("CARGO_BUILD_INCREMENTAL", "false");
// Enable the Cranelift verifier
env::set_var("CG_CLIF_ENABLE_VERIFIER", "1");
2022-08-05 07:57:19 -05:00
}
let mut args = env::args().skip(1);
let command = match args.next().as_deref() {
Some("prepare") => Command::Prepare,
Some("build") => Command::Build,
2022-07-30 04:32:54 -05:00
Some("test") => Command::Test,
Some("bench") => Command::Bench,
Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
Some(command) => arg_error!("Unknown command {}", command),
None => {
usage();
process::exit(0);
}
};
let mut out_dir = PathBuf::from(".");
let mut channel = "release";
let mut sysroot_kind = SysrootKind::Clif;
let mut use_unstable_features = true;
while let Some(arg) = args.next().as_deref() {
match arg {
"--out-dir" => {
out_dir = PathBuf::from(args.next().unwrap_or_else(|| {
arg_error!("--out-dir requires argument");
}))
}
"--debug" => channel = "debug",
"--sysroot" => {
sysroot_kind = match args.next().as_deref() {
Some("none") => SysrootKind::None,
Some("clif") => SysrootKind::Clif,
Some("llvm") => SysrootKind::Llvm,
Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
None => arg_error!("--sysroot requires argument"),
}
}
"--no-unstable-features" => use_unstable_features = false,
flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
arg => arg_error!("Unexpected argument {}", arg),
}
}
let host_compiler = Compiler::llvm_with_triple(
std::env::var("HOST_TRIPLE")
.ok()
.or_else(|| config::get_value("host"))
.unwrap_or_else(|| rustc_info::get_host_triple()),
);
let target_triple = std::env::var("TARGET_TRIPLE")
.ok()
.or_else(|| config::get_value("target"))
.unwrap_or_else(|| host_compiler.triple.clone());
2022-07-30 17:04:59 -05:00
// FIXME allow changing the location of these dirs using cli arguments
let current_dir = std::env::current_dir().unwrap();
out_dir = current_dir.join(out_dir);
let dirs = path::Dirs {
source_dir: current_dir.clone(),
download_dir: out_dir.join("download"),
build_dir: out_dir.join("build"),
dist_dir: out_dir.join("dist"),
};
path::RelPath::BUILD.ensure_exists(&dirs);
{
// Make sure we always explicitly specify the target dir
let target =
path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs);
env::set_var("CARGO_TARGET_DIR", &target);
let _ = std::fs::remove_file(&target);
std::fs::File::create(target).unwrap();
}
if command == Command::Prepare {
prepare::prepare(&dirs);
process::exit(0);
}
let cg_clif_dylib =
build_backend::build_backend(&dirs, channel, &host_compiler, use_unstable_features);
2022-07-30 06:50:05 -05:00
match command {
Command::Prepare => {
// Handled above
}
2022-07-30 06:50:05 -05:00
Command::Test => {
tests::run_tests(
&dirs,
channel,
sysroot_kind,
&cg_clif_dylib,
&host_compiler,
&target_triple,
);
2022-08-06 15:24:38 -05:00
if host_compiler.triple == target_triple {
abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &host_compiler);
} else {
eprintln!("[SKIP] abi-cafe (cross-compilation not supported)");
return;
}
2022-07-30 06:50:05 -05:00
}
2022-08-06 15:24:38 -05:00
Command::Build => {
build_sysroot::build_sysroot(
&dirs,
2022-08-06 07:34:55 -05:00
channel,
sysroot_kind,
&cg_clif_dylib,
&host_compiler,
2022-08-06 07:34:55 -05:00
&target_triple,
);
}
Command::Bench => {
build_sysroot::build_sysroot(
&dirs,
channel,
sysroot_kind,
&cg_clif_dylib,
&host_compiler,
&target_triple,
);
bench::benchmark(&dirs, &host_compiler);
}
2022-07-30 04:32:54 -05:00
}
}