82 lines
2.4 KiB
Rust
Raw Normal View History

2024-03-12 18:17:47 +02:00
use std::{ffi::OsStr, path::Path, process::Command};
use crate::{
env::{BuildEnv, Profile},
error::Error,
};
pub enum CargoBuilder<'e> {
Host,
Userspace(&'e BuildEnv),
Kernel(&'e BuildEnv),
}
impl<'e> CargoBuilder<'e> {
pub fn run<P: AsRef<Path>, S: AsRef<OsStr>>(self, dir: P, arg: S) -> Result<(), Error> {
let mut command = Command::new("cargo");
command.current_dir(dir);
match self {
Self::Userspace(env) => {
// Add clippy dependency libraries to LD_LIBRARY_PATH
let toolchain_libs_path = env
.workspace_root
.join("toolchain/build")
.join(&env.host_triple)
.join("stage1/lib/rustlib")
.join(&env.host_triple)
.join("lib");
let ld_library_path = std::env::var("LD_LIBRARY_PATH");
let ld_library_path = match ld_library_path {
Ok(path) => format!("{}:{}", toolchain_libs_path.display(), path),
Err(_) => toolchain_libs_path.display().to_string(),
};
command.env("LD_LIBRARY_PATH", ld_library_path);
command
.arg("+ygg-stage1")
.arg(arg)
.arg(&format!("--target={}", env.arch.user_triple()));
if env.profile == Profile::Release {
command.arg("--release");
}
}
Self::Host => {
command.arg("+nightly").arg(arg);
}
Self::Kernel(env) => {
command.arg("+nightly").arg(arg);
let target_spec_arg = format!(
"--target={}/etc/{}.json",
env.workspace_root.display(),
env.arch.kernel_triple()
);
command.arg(target_spec_arg);
command.args(["-Z", "build-std=core,alloc,compiler_builtins"]);
if env.profile == Profile::Release {
command.arg("--release");
}
}
}
log::trace!("Run cargo: {:?}", command);
let status = command.status()?;
if status.success() {
Ok(())
} else {
Err(Error::CargoFailed)
}
}
pub fn build<P: AsRef<Path>>(self, dir: P) -> Result<(), Error> {
self.run(dir, "build")
}
}