diff --git a/cranelift/codegen/meta/src/srcgen.rs b/cranelift/codegen/meta/src/srcgen.rs index d3c321e5bc51..5b94ddd1979d 100644 --- a/cranelift/codegen/meta/src/srcgen.rs +++ b/cranelift/codegen/meta/src/srcgen.rs @@ -94,7 +94,6 @@ impl Formatter { directory: &std::path::Path, ) -> Result<(), error::Error> { let path = directory.join(&filename); - eprintln!("Writing generated file: {}", path.display()); let mut f = fs::File::create(path)?; for l in self.lines.iter().map(|l| l.as_bytes()) { diff --git a/cranelift/isle/isle/src/codegen.rs b/cranelift/isle/isle/src/codegen.rs index 158285832a8e..a93b4b226a5d 100644 --- a/cranelift/isle/isle/src/codegen.rs +++ b/cranelift/isle/isle/src/codegen.rs @@ -127,7 +127,7 @@ impl<'a> Codegen<'a> { "// Generated automatically from the instruction-selection DSL code in:", ) .unwrap(); - for file in &self.files.file_names { + for file in &self.files.file_names_relative() { writeln!(code, "// - {file}").unwrap(); } diff --git a/cranelift/isle/isle/src/files.rs b/cranelift/isle/isle/src/files.rs index 703e19f04c87..c8e59b4bf178 100644 --- a/cranelift/isle/isle/src/files.rs +++ b/cranelift/isle/isle/src/files.rs @@ -100,6 +100,18 @@ impl Files { self.file_names.get(file).map(|x| x.as_str()) } + /// Same as `file_name` but try to make the file relative to the project root. Otherwise, + /// return the original file name (if found). + pub fn file_name_relative(&self, file: usize) -> Option<&str> { + self.file_name(file).map(|f| relative(f)) + } + + /// Try to make file names relative to the project root. Otherwise, return the original file + /// names. + pub fn file_names_relative(&self) -> Vec<&str> { + self.file_names.iter().map(|f| relative(f)).collect() + } + pub fn file_text(&self, file: usize) -> Option<&str> { self.file_texts.get(file).map(|x| x.as_str()) } @@ -109,6 +121,15 @@ impl Files { } } +/// If OUT_DIR is set, strips it from the file name. +/// Otherwise returns the original file name. +pub fn relative(file_name: &str) -> &str { + match std::env::var("OUT_DIR") { + Err(_) => file_name, + Ok(root) => file_name.strip_prefix(&root).unwrap_or(file_name).into(), + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/cranelift/isle/isle/src/lexer.rs b/cranelift/isle/isle/src/lexer.rs index 2b792ab962c8..26cefb55347c 100644 --- a/cranelift/isle/isle/src/lexer.rs +++ b/cranelift/isle/isle/src/lexer.rs @@ -39,7 +39,7 @@ impl Pos { pub fn pretty_print_line(&self, files: &Files) -> String { format!( "{} line {}", - files.file_name(self.file).unwrap(), + files.file_name_relative(self.file).unwrap(), files.file_line_map(self.file).unwrap().line(self.offset) ) }