diff --git a/build.rs b/build.rs index f9de6e6..951c543 100644 --- a/build.rs +++ b/build.rs @@ -7,39 +7,50 @@ fn main() -> Result<()> { build_phinit()?; run_simple_make("sommelier")?; run_simple_make("kernel")?; + // Rerun build.rs upon making or pulling in new commits + println!("cargo:rerun-if-changed=.git/refs/heads/master"); Ok(()) } fn build_phinit() -> Result<()> { - run_command_in_directory( - "ph-init", - "cargo", - &["build", "--release"] - )?; - run_command_in_directory( - "ph-init", - "strip", - &["target/release/ph-init"] - )?; + let _dir = ChdirTo::path("ph-init"); + + Command::new("cargo") + .arg("build") + .arg("--release") + .status()?; + + Command::new("strip") + .arg("target/release/ph-init") + .status()?; + Ok(()) } fn run_simple_make>(directory: P) -> Result<()> { - run_command_in_directory(directory, "make", &[]) + let _dir = ChdirTo::path(directory); + Command::new("make").status()?; + Ok(()) } -fn run_command_in_directory>(directory: P, cmd: &str, args: &[&str]) -> Result<()> { - let saved = push_directory(directory)?; - Command::new(cmd).args(args).status()?; - env::set_current_dir(saved) +struct ChdirTo { + saved: PathBuf, } -fn push_directory>(directory: P) -> Result { - let current = env::current_dir()?; - env::set_current_dir(directory.as_ref())?; - Ok(current) +impl ChdirTo { + fn path>(directory: P) -> ChdirTo { + let saved = env::current_dir() + .expect("current_dir()"); + env::set_current_dir(directory.as_ref()) + .expect("set_current_dir()"); + ChdirTo { saved } + } } - - +impl Drop for ChdirTo { + fn drop(&mut self) { + env::set_current_dir(&self.saved) + .expect("restore current dir"); + } +}