use super::{collect_from_prefix, run, BuildOutput, DepBuilder};
use crate::config::DependencySpec;
use crate::error::{BuildError, Result};
use std::path::Path;
use std::process::Command;
pub struct CustomBuilder {
commands: Vec<String>,
libs: Vec<String>,
extra_includes: Vec<String>,
}
impl CustomBuilder {
pub fn new(spec: &DependencySpec) -> Self {
Self {
commands: spec.build_commands.clone(),
libs: spec.libs.clone(),
extra_includes: spec.extra_includes.clone(),
}
}
}
impl DepBuilder for CustomBuilder {
fn detect(_src_dir: &Path) -> bool {
false
}
fn name(&self) -> &'static str {
"custom"
}
fn build(&self, src_dir: &Path, prefix: &Path) -> Result<BuildOutput> {
if self.commands.is_empty() {
return Err(BuildError::Dependency {
name: src_dir.display().to_string(),
reason: "build_system = \"custom\", but build_commands is empty \
in smidr.toml"
.to_string(),
});
}
for raw_cmd in &self.commands {
let expanded = raw_cmd.replace("$SMIDR_PREFIX", &prefix.display().to_string());
run(Command::new("sh")
.arg("-c")
.arg(&expanded)
.current_dir(src_dir))?;
}
let mut output = collect_from_prefix(prefix);
output.libs = self.libs.clone();
for extra in &self.extra_includes {
output.include_dirs.push(prefix.join(extra));
}
Ok(output)
}
}