use std::process::Command;
use tracing::info;
use crate::project::Project;
use crate::target::Target;
use super::{BuildDriver, CompileError};
#[derive(Debug)]
pub struct CargoDriver<'a> {
command: &'a str,
}
impl<'a> CargoDriver<'a> {
pub fn new(command: &'a str) -> Self {
Self { command }
}
}
impl BuildDriver for CargoDriver<'_> {
fn build(&self, project: &Project, target: &Target) -> Result<(), CompileError> {
info!(triple = %target.triple, bin = %project.bin, "building");
let mut command = Command::new(self.command);
command
.args(["build", "--release", "--target"])
.arg(&target.triple);
if let Some(package) = &project.package {
command.arg("--package").arg(package);
}
command
.arg("--bin")
.arg(&project.bin)
.current_dir(&project.workspace_root);
let status = command.status().map_err(|source| CompileError::Spawn {
driver: self.command.to_owned(),
source,
})?;
if !status.success() {
return Err(CompileError::BuildFailed {
triple: target.triple.clone(),
status,
});
}
Ok(())
}
}