rust/build_system/src/build.rs

232 lines
7.2 KiB
Rust
Raw Normal View History

2023-09-26 16:09:51 +02:00
use crate::config::set_config;
2023-10-04 16:01:02 +02:00
use crate::utils::{
get_gcc_path, run_command, run_command_with_output_and_env, walk_dir,
2023-10-04 16:01:02 +02:00
};
2023-09-26 16:09:51 +02:00
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
#[derive(Default)]
struct BuildArg {
codegen_release_channel: bool,
sysroot_release_channel: bool,
2023-09-06 19:01:04 -04:00
flags: Vec<String>,
2023-09-26 16:09:51 +02:00
gcc_path: String,
}
impl BuildArg {
fn new() -> Result<Option<Self>, String> {
let gcc_path = get_gcc_path()?;
let mut build_arg = Self {
gcc_path,
..Default::default()
};
2023-10-04 16:01:02 +02:00
// We skip binary name and the `build` command.
2023-09-26 16:09:51 +02:00
let mut args = std::env::args().skip(2);
while let Some(arg) = args.next() {
match arg.as_str() {
"--release" => build_arg.codegen_release_channel = true,
"--release-sysroot" => build_arg.sysroot_release_channel = true,
2023-10-04 16:01:02 +02:00
"--no-default-features" => {
2023-09-06 19:01:04 -04:00
build_arg.flags.push("--no-default-features".to_string());
2023-10-04 16:01:02 +02:00
}
2023-09-26 16:09:51 +02:00
"--features" => {
if let Some(arg) = args.next() {
2023-09-06 19:01:04 -04:00
build_arg.flags.push("--features".to_string());
build_arg.flags.push(arg.as_str().into());
2023-09-26 16:09:51 +02:00
} else {
2023-10-04 16:01:02 +02:00
return Err(
"Expected a value after `--features`, found nothing".to_string()
);
2023-09-26 16:09:51 +02:00
}
}
"--help" => {
Self::usage();
return Ok(None);
}
2023-09-06 19:01:04 -04:00
"--target-triple" => {
if args.next().is_some() {
// Handled in config.rs.
} else {
return Err(
"Expected a value after `--target-triple`, found nothing".to_string()
);
}
}
2023-10-04 16:01:02 +02:00
arg => return Err(format!("Unknown argument `{}`", arg)),
2023-09-26 16:09:51 +02:00
}
}
Ok(Some(build_arg))
}
fn usage() {
println!(
r#"
`build` command help:
--release : Build codegen in release mode
--release-sysroot : Build sysroot in release mode
--no-default-features : Add `--no-default-features` flag
--features [arg] : Add a new feature [arg]
2023-09-06 19:01:04 -04:00
--target-triple [arg] : Set the target triple to [arg]
2023-09-26 16:09:51 +02:00
--help : Show this help
"#
)
}
}
fn build_sysroot(
env: &mut HashMap<String, String>,
release_mode: bool,
target_triple: &str,
) -> Result<(), String> {
std::env::set_current_dir("build_sysroot")
2023-10-04 16:01:02 +02:00
.map_err(|error| format!("Failed to go to `build_sysroot` directory: {:?}", error))?;
2023-09-26 16:09:51 +02:00
// Cleanup for previous run
2023-10-04 16:01:02 +02:00
// Clean target dir except for build scripts and incremental cache
let _ = walk_dir(
2023-09-26 16:09:51 +02:00
"target",
|dir: &Path| {
for top in &["debug", "release"] {
2023-10-04 16:01:02 +02:00
let _ = fs::remove_dir_all(dir.join(top).join("build"));
let _ = fs::remove_dir_all(dir.join(top).join("deps"));
let _ = fs::remove_dir_all(dir.join(top).join("examples"));
let _ = fs::remove_dir_all(dir.join(top).join("native"));
2023-09-26 16:09:51 +02:00
2023-10-04 16:01:02 +02:00
let _ = walk_dir(
2023-09-26 16:09:51 +02:00
dir.join(top),
|sub_dir: &Path| {
if sub_dir
.file_name()
2023-10-04 16:01:02 +02:00
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
2023-09-26 16:09:51 +02:00
.unwrap_or(false)
{
2023-10-04 16:01:02 +02:00
let _ = fs::remove_dir_all(sub_dir);
2023-09-26 16:09:51 +02:00
}
Ok(())
},
|file: &Path| {
if file
.file_name()
2023-10-04 16:01:02 +02:00
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
2023-09-26 16:09:51 +02:00
.unwrap_or(false)
{
2023-10-04 16:01:02 +02:00
let _ = fs::remove_file(file);
2023-09-26 16:09:51 +02:00
}
Ok(())
},
);
}
Ok(())
},
|_| Ok(()),
);
2023-10-04 16:01:02 +02:00
let _ = fs::remove_file("Cargo.lock");
let _ = fs::remove_file("test_target/Cargo.lock");
let _ = fs::remove_dir_all("sysroot");
2023-09-26 16:09:51 +02:00
// Builds libs
let channel = if release_mode {
let rustflags = env
2023-10-04 16:01:02 +02:00
.get("RUSTFLAGS")
2023-09-26 16:09:51 +02:00
.cloned()
.unwrap_or_default();
env.insert(
2023-10-04 16:01:02 +02:00
"RUSTFLAGS".to_string(),
format!("{} -Zmir-opt-level=3", rustflags),
2023-09-26 16:09:51 +02:00
);
2023-10-04 16:01:02 +02:00
run_command_with_output_and_env(
2023-09-26 16:09:51 +02:00
&[
&"cargo",
&"build",
&"--target",
&target_triple,
&"--release",
],
None,
Some(&env),
)?;
"release"
} else {
2023-10-04 16:01:02 +02:00
run_command_with_output_and_env(
2023-09-26 16:09:51 +02:00
&[
&"cargo",
&"build",
&"--target",
&target_triple,
],
None,
Some(env),
)?;
"debug"
};
// Copy files to sysroot
2023-10-04 16:01:02 +02:00
let sysroot_path = format!("sysroot/lib/rustlib/{}/lib/", target_triple);
2023-09-26 16:09:51 +02:00
fs::create_dir_all(&sysroot_path)
2023-10-04 16:01:02 +02:00
.map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_path, error))?;
let copier = |dir_to_copy: &Path| {
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
};
2023-09-26 16:09:51 +02:00
walk_dir(
2023-10-04 16:01:02 +02:00
&format!("target/{}/{}/deps", target_triple, channel),
2023-09-26 16:09:51 +02:00
copier,
copier,
)?;
Ok(())
}
fn build_codegen(args: &BuildArg) -> Result<(), String> {
let mut env = HashMap::new();
2023-10-04 16:01:02 +02:00
env.insert("LD_LIBRARY_PATH".to_string(), args.gcc_path.clone());
env.insert("LIBRARY_PATH".to_string(), args.gcc_path.clone());
2023-09-26 16:09:51 +02:00
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &"rustc"];
if args.codegen_release_channel {
command.push(&"--release");
2023-10-04 16:01:02 +02:00
env.insert("CHANNEL".to_string(), "release".to_string());
env.insert("CARGO_INCREMENTAL".to_string(), "1".to_string());
2023-09-26 16:09:51 +02:00
} else {
2023-10-04 16:01:02 +02:00
env.insert("CHANNEL".to_string(), "debug".to_string());
2023-09-26 16:09:51 +02:00
}
2023-09-06 19:01:04 -04:00
let flags = args.flags.iter().map(|s| s.as_str()).collect::<Vec<_>>();
for flag in &flags {
command.push(flag);
2023-09-26 16:09:51 +02:00
}
run_command_with_output_and_env(&command, None, Some(&env))?;
2023-09-26 16:09:51 +02:00
let config = set_config(&mut env, &[], Some(&args.gcc_path))?;
// We voluntarily ignore the error.
2023-10-04 16:01:02 +02:00
let _ = fs::remove_dir_all("target/out");
2023-09-26 16:09:51 +02:00
let gccjit_target = "target/out/gccjit";
2023-10-04 16:01:02 +02:00
fs::create_dir_all(gccjit_target).map_err(|error| {
format!(
"Failed to create directory `{}`: {:?}",
gccjit_target, error
)
})?;
2023-09-26 16:09:51 +02:00
println!("[BUILD] sysroot");
build_sysroot(
&mut env,
args.sysroot_release_channel,
&config.target_triple,
)?;
Ok(())
}
2023-08-18 16:06:20 +02:00
pub fn run() -> Result<(), String> {
2023-09-26 16:09:51 +02:00
let args = match BuildArg::new()? {
2023-10-04 16:01:02 +02:00
Some(args) => args,
2023-09-26 16:09:51 +02:00
None => return Ok(()),
};
build_codegen(&args)?;
2023-08-18 16:06:20 +02:00
Ok(())
}