From c50170f000d382fa02090fbf79d0e5d12036f53b Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Mon, 5 Apr 2021 11:41:17 -0600 Subject: implement function `realpath` --- src/makefile/functions.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 6dacd10..6815ac4 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -53,6 +53,10 @@ pub fn expand_call( assert_eq!(args.len(), 1); file_name::wildcard(macros, &args[0]) } + "realpath" => { + assert_eq!(args.len(), 1); + file_name::realpath(macros, &args[0]) + } "if" => { assert!(args.len() == 2 || args.len() == 3); @@ -179,6 +183,7 @@ mod text { mod file_name { use std::env; use std::ffi::OsStr; + use std::fs; use std::path::Path; use eyre::WrapErr; @@ -246,6 +251,19 @@ mod file_name { .collect::>(); Ok(results.join(" ")) } + + pub fn realpath(macros: &MacroSet, targets: &TokenString) -> Result { + let targets = macros.expand(targets)?; + let results = targets + .split_whitespace() + .map(|x| { + fs::canonicalize(x) + .map(|p| p.to_string_lossy().into_owned()) + .unwrap_or_else(|_| x.to_owned()) + }) + .collect::>(); + Ok(results.join(" ")) + } } // Functions for Conditionals -- cgit v1.2.3