From 455b77bdbc78729c4175e27a949327c9836b8794 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Tue, 6 Apr 2021 14:22:19 -0600 Subject: if an included makefile didn't exist, try building it --- src/main.rs | 4 ++-- src/makefile/input.rs | 17 ++++++++++++++--- src/makefile/mod.rs | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 3d4b19d..c3cedee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,11 +62,11 @@ fn main() -> Result<()> { if filename == &PathBuf::from("-") { let macros = makefile.macros.with_overlay(); let file = MakefileReader::read(&args, macros, stdin().lock())?.finish(); - makefile.extend(file); + makefile.extend(file)?; } else { let macros = makefile.macros.with_overlay(); let file = MakefileReader::read_file(&args, macros, filename)?.finish(); - makefile.extend(file); + makefile.extend(file)?; }; } diff --git a/src/makefile/input.rs b/src/makefile/input.rs index 5883590..700bf4b 100644 --- a/src/makefile/input.rs +++ b/src/makefile/input.rs @@ -147,6 +147,7 @@ pub struct MakefileReader<'a, 'parent, R: BufRead> { pub macros: MacroSet<'parent, 'static>, pub targets: HashMap, pub first_non_special_target: Option, + pub failed_includes: Vec, args: &'a Args, lines_iter: Peekable>>, // join with escaped_newline_replacement to get the actual line @@ -194,6 +195,7 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { macros, targets: HashMap::new(), first_non_special_target: None, + failed_includes: Vec::new(), args, lines_iter: source.lines().line_numbered().peekable(), pending_line: None, @@ -235,9 +237,15 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { for field in fields { let child_macros = self.macros.with_overlay(); let child = MakefileReader::read_file(self.args, child_macros, field) - .with_context(|| format!("while including {}", field))? - .finish(); - self.extend(child); + .with_context(|| format!("while including {}", field)); + if let Ok(child) = child { + let child = child.finish(); + self.extend(child); + } else { + // TODO handle non-file-not-found errors + log::error!("included makefile {} not found", field); + self.failed_includes.push(field.to_owned()); + } } continue; } @@ -655,6 +663,7 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { macros: self.macros.data, targets: self.targets, first_non_special_target: self.first_non_special_target, + failed_includes: self.failed_includes, } } @@ -665,6 +674,7 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { if self.first_non_special_target.is_none() { self.first_non_special_target = new.first_non_special_target; } + self.failed_includes.extend(new.failed_includes); } } @@ -673,6 +683,7 @@ pub struct FinishedMakefileReader { pub macros: HashMap, pub targets: HashMap, pub first_non_special_target: Option, + pub failed_includes: Vec, } #[cfg(test)] diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index aab5054..f17ba64 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -5,7 +5,7 @@ use std::fmt; use std::path::{Path, PathBuf}; use std::rc::Rc; -use eyre::{bail, eyre, Result}; +use eyre::{bail, eyre, Result, WrapErr}; use crate::args::Args; @@ -108,7 +108,7 @@ impl<'a> Makefile<'a> { } } - pub fn extend(&mut self, new: FinishedMakefileReader) { + pub fn extend(&mut self, new: FinishedMakefileReader) -> Result<()> { self.inference_rules.extend(new.inference_rules); self.macros.extend(new.macros); self.targets.borrow_mut().extend( @@ -119,6 +119,16 @@ impl<'a> Makefile<'a> { if self.first_non_special_target.is_none() { self.first_non_special_target = new.first_non_special_target; } + for failed_include in new.failed_includes { + // try rebuilding + self.update_target(&failed_include).wrap_err_with(|| { + format!("while building missing included file {}", &failed_include) + })?; + let macros = self.macros.with_overlay(); + let file = MakefileReader::read_file(self.args, macros, failed_include)?.finish(); + self.extend(file)?; + } + Ok(()) } fn special_target_has_prereq(&self, target: &str, name: &str) -> bool { -- cgit v1.2.3