aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/makefile/functions.rs285
-rw-r--r--src/makefile/macro.rs189
-rw-r--r--src/makefile/mod.rs306
-rw-r--r--src/makefile/pattern.rs49
-rw-r--r--src/makefile/token.rs7
5 files changed, 647 insertions, 189 deletions
diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs
index bc39886..b9a6a6d 100644
--- a/src/makefile/functions.rs
+++ b/src/makefile/functions.rs
@@ -1,26 +1,160 @@
+use std::env;
+use std::path::Path;
+
+use super::pattern::r#match;
+use super::r#macro::{MacroSet, MacroSource};
use super::token::TokenString;
-pub(crate) fn call(name: &str, args: &[TokenString]) -> TokenString {
+pub(crate) struct FunctionCall {}
+
+fn filter(macros: &MacroSet, patterns: &TokenString, text: &TokenString) -> String {
+ let patterns = macros.expand(patterns);
+ let patterns = patterns.split_whitespace().collect::<Vec<_>>();
+ let text = macros.expand(&text);
+ let text = text.split_whitespace();
+ let mut result_pieces = vec![];
+ for word in text {
+ if patterns
+ .iter()
+ .any(|pattern| r#match(pattern, word).is_some())
+ {
+ result_pieces.push(word);
+ }
+ }
+ result_pieces.join(" ")
+}
+
+pub(crate) fn call(name: &str, args: &[TokenString], macros: &MacroSet) -> String {
match name {
// Text Functions
- "filter" => todo!(),
- "filter-out" => todo!(),
- "sort" => todo!(),
+ "filter" => {
+ assert_eq!(args.len(), 2);
+ filter(macros, &args[0], &args[1])
+ }
+ "filter-out" => {
+ assert_eq!(args.len(), 2);
+ let patterns = macros.expand(&args[0]);
+ let patterns = patterns.split_whitespace().collect::<Vec<_>>();
+ let text = macros.expand(&args[1]);
+ let text = text.split_whitespace();
+ let mut result_pieces = vec![];
+ for word in text {
+ if patterns
+ .iter()
+ .all(|pattern| r#match(pattern, word).is_none())
+ {
+ result_pieces.push(word);
+ }
+ }
+ result_pieces.join(" ")
+ }
+ "sort" => {
+ assert_eq!(args.len(), 1);
+ let words = macros.expand(&args[0]);
+ let mut words = words.split_whitespace().collect::<Vec<_>>();
+ words.sort();
+ words.dedup();
+ words.join(" ")
+ }
// File Name Functions
- "notdir" => todo!(),
- "basename" => todo!(),
- "addprefix" => todo!(),
- "wildcard" => todo!(),
+ "notdir" => {
+ assert_eq!(args.len(), 1);
+ let words = macros.expand(&args[0]);
+ let words = words
+ .split_whitespace()
+ .map(|word| {
+ Path::new(word)
+ .file_name()
+ .and_then(|filename| filename.to_str())
+ .unwrap_or("")
+ })
+ .collect::<Vec<_>>();
+ words.join(" ")
+ }
+ "basename" => {
+ assert_eq!(args.len(), 1);
+ let words = macros.expand(&args[0]);
+ let words = words
+ .split_whitespace()
+ .map(|word| {
+ Path::new(word)
+ .with_extension("")
+ .to_str()
+ .map_or_else(String::new, ToString::to_string)
+ })
+ .collect::<Vec<_>>();
+ words.join(" ")
+ }
+ "addprefix" => {
+ assert_eq!(args.len(), 2);
+ let prefix = macros.expand(&args[0]);
+ let targets = macros.expand(&args[1]);
+ let results = targets
+ .split_whitespace()
+ .map(|t| format!("{}{}", prefix, t))
+ .collect::<Vec<_>>();
+ results.join(" ")
+ }
+ "wildcard" => {
+ assert_eq!(args.len(), 1);
+ let pattern = macros.expand(&args[0]);
+ let home_dir = env::var("HOME")
+ .ok()
+ .or(dirs::home_dir().and_then(|p| p.to_str().map(String::from)));
+ let pattern = if let Some(home_dir) = home_dir {
+ pattern.replace('~', &home_dir)
+ } else {
+ pattern
+ };
+ let results = glob::glob(&pattern)
+ .expect("invalid glob pattern!")
+ .filter_map(|path| path.ok())
+ .map(|path| path.to_str().map(ToString::to_string).unwrap_or_default())
+ .collect::<Vec<_>>();
+ results.join(" ")
+ }
// foreach
- "foreach" => todo!(),
+ "foreach" => {
+ assert_eq!(args.len(), 3);
+ let var = macros.expand(&args[0]);
+ let list = macros.expand(&args[1]);
+ let text = &args[2];
+ let words = list.split_whitespace();
+
+ let mut macros = macros.clone();
+ let results = words
+ .map(|word| {
+ macros.set(var.clone(), MacroSource::File, TokenString::text(word));
+ macros.expand(text)
+ })
+ .collect::<Vec<_>>();
+ results.join(" ")
+ }
// call
- "call" => todo!(),
+ "call" => {
+ assert!(args.len() > 0);
+ let args = args
+ .iter()
+ .map(|arg| macros.expand(arg))
+ .collect::<Vec<_>>();
+ let function = args[0].clone();
+
+ let mut macros = macros.clone();
+ for (i, x) in args.into_iter().enumerate() {
+ macros.set(i.to_string(), MacroSource::File, TokenString::text(x));
+ }
+ macros.expand(&TokenString::r#macro(function))
+ }
// eval
- "eval" => todo!(),
+ "eval" => {
+ assert_eq!(args.len(), 1);
+ let arg = macros.expand(&args[0]);
+ todo!()
+ }
// shell
"shell" => todo!(),
@@ -29,3 +163,132 @@ pub(crate) fn call(name: &str, args: &[TokenString]) -> TokenString {
_ => panic!("function not implemented: {}", name),
}
}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ fn call(name: &str, args: &[TokenString], macros: &MacroSet) -> String {
+ super::call(name, args, macros, lookup_fail)
+ }
+
+ macro_rules! call {
+ ($func:literal $($arg:literal),+) => {
+ call($func, &[$(TokenString::text($arg)),+], &MacroSet::new())
+ };
+ ($func:ident $($arg:literal),+) => {
+ call(stringify!($func), &[$(TokenString::text($arg)),+], &MacroSet::new())
+ };
+ }
+
+ #[test]
+ fn filter() {
+ let result = call!(filter "word", "this contains a word inside it");
+ assert_eq!(result, "word");
+
+ let result = call!(filter "%.c %.s", "foo.c bar.c baz.s ugh.h");
+ assert_eq!(result, "foo.c bar.c baz.s");
+ }
+
+ #[test]
+ fn filter_out() {
+ let result = call!("filter-out" "main1.o main2.o", "main1.o foo.o main2.o bar.o");
+ assert_eq!(result, "foo.o bar.o");
+ }
+
+ #[test]
+ fn sort() {
+ let result = call!(sort "foo bar lose foo");
+ assert_eq!(result, "bar foo lose");
+ }
+
+ #[test]
+ fn notdir() {
+ let result = call!(notdir "src/foo.c hacks");
+ assert_eq!(result, "foo.c hacks");
+ }
+
+ #[test]
+ fn basename() {
+ let result = call!(basename "src/foo.c src-1.0/bar hacks");
+ assert_eq!(result, "src/foo src-1.0/bar hacks");
+ }
+
+ #[test]
+ fn addprefix() {
+ let result = call!(addprefix "src/", "foo bar");
+ assert_eq!(result, "src/foo src/bar");
+ }
+
+ #[test]
+ fn wildcard() {
+ use std::env::{set_current_dir, set_var};
+ use std::fs::write;
+ use std::path::MAIN_SEPARATOR;
+
+ let tempdir = tempfile::tempdir().unwrap();
+
+ write(tempdir.path().join("foo.c"), "").unwrap();
+ write(tempdir.path().join("bar.h"), "").unwrap();
+ write(tempdir.path().join("baz.txt"), "").unwrap();
+ write(tempdir.path().join("acab.c"), "ACAB").unwrap();
+ write(tempdir.path().join("based.txt"), "☭").unwrap();
+
+ set_current_dir(tempdir.path()).unwrap();
+ set_var("HOME", tempdir.path().to_str().unwrap());
+ let sort = |x: String| call("sort", &[TokenString::text(&x)], &MacroSet::new());
+ assert_eq!(sort(call!(wildcard "*.c")), "acab.c foo.c");
+ assert_eq!(
+ sort(call!(wildcard "~/ba?.*")),
+ format!(
+ "{0}{1}bar.h {0}{1}baz.txt",
+ tempdir.path().to_str().unwrap(),
+ MAIN_SEPARATOR
+ )
+ );
+ }
+
+ #[test]
+ fn foreach() {
+ let mut macros = MacroSet::new();
+ macros.set(
+ "test".to_string(),
+ MacroSource::File,
+ "worked for $(item).".parse().unwrap(),
+ );
+ assert_eq!(
+ call(
+ "foreach",
+ &[
+ TokenString::text("item"),
+ TokenString::text("a b c d"),
+ TokenString::r#macro("test")
+ ],
+ &macros,
+ ),
+ "worked for a. worked for b. worked for c. worked for d."
+ );
+ }
+
+ #[test]
+ fn call_test() {
+ let mut macros = MacroSet::new();
+ macros.set(
+ "reverse".to_string(),
+ MacroSource::File,
+ "$(2) $(1)".parse().unwrap(),
+ );
+ assert_eq!(
+ call(
+ "call",
+ &[
+ TokenString::text("reverse"),
+ TokenString::text("a"),
+ TokenString::text("b")
+ ],
+ &macros,
+ ),
+ "b a"
+ );
+ }
+}
diff --git a/src/makefile/macro.rs b/src/makefile/macro.rs
new file mode 100644
index 0000000..139903f
--- /dev/null
+++ b/src/makefile/macro.rs
@@ -0,0 +1,189 @@
+use std::cell::RefCell;
+use std::collections::HashMap;
+use std::env;
+use std::fmt;
+use std::rc::{Rc, Weak};
+
+use regex::Regex;
+
+use super::functions;
+use super::token::{Token, TokenString};
+
+#[derive(Debug, Clone)]
+pub(crate) enum MacroSource {
+ File,
+ CommandLineOrMakeflags,
+ Environment,
+ Builtin,
+}
+
+pub(crate) trait LookupInternal: for<'a> Fn(&'a str) -> String {}
+
+impl<F: for<'a> Fn(&'a str) -> String> LookupInternal for F {}
+
+fn lookup_fail(_: &str) -> String {
+ panic!("internal variables not available!");
+}
+
+#[derive(Clone)]
+pub(crate) struct MacroSet {
+ data: HashMap<String, (MacroSource, TokenString)>,
+ lookup_internal: RefCell<Weak<dyn LookupInternal>>,
+}
+
+impl MacroSet {
+ pub(crate) fn new() -> Self {
+ let lookup_fail: Rc<dyn LookupInternal> = Rc::new(lookup_fail);
+ Self {
+ data: HashMap::new(),
+ lookup_internal: RefCell::new(Rc::downgrade(&lookup_fail)),
+ }
+ }
+
+ pub(crate) fn lookup(&self, lookup: Weak<dyn LookupInternal>) {
+ self.lookup_internal.replace(lookup);
+ }
+
+ pub(crate) fn add_builtins(&mut self) {
+ for (k, v) in builtins() {
+ self.data.insert(k.into(), (MacroSource::Builtin, v));
+ }
+ }
+
+ pub(crate) fn add_env(&mut self) {
+ for (k, v) in env::vars() {
+ if k != "MAKEFLAGS" && k != "SHELL" {
+ self.data
+ .insert(k, (MacroSource::Environment, TokenString::text(v)));
+ }
+ }
+ }
+
+ pub(crate) fn set(&mut self, name: String, source: MacroSource, text: TokenString) {
+ self.data.insert(name, (source, text));
+ }
+
+ pub(crate) fn is_defined(&self, name: &str) -> bool {
+ self.data.contains_key(name)
+ }
+
+ pub(crate) fn get(&self, name: &str) -> Option<&(MacroSource, TokenString)> {
+ self.data.get(name)
+ }
+
+ // `remove` is fine, but I think for "remove-and-return" `pop` is better
+ pub(crate) fn pop(&mut self, name: &str) -> Option<(MacroSource, TokenString)> {
+ self.data.remove(name)
+ }
+
+ pub(crate) fn expand(&self, text: &TokenString) -> String {
+ let mut result = String::new();
+ for token in text.tokens() {
+ match token {
+ Token::Text(t) => result.push_str(t),
+ Token::MacroExpansion { name, replacement } => {
+ let internal_macro_names = &['@', '?', '<', '*'][..];
+ let internal_macro_suffices = &['D', 'F'][..];
+ let just_internal = name.len() == 1 && name.starts_with(internal_macro_names);
+ let suffixed_internal = name.len() == 2
+ && name.starts_with(internal_macro_names)
+ && name.ends_with(internal_macro_suffices);
+ let macro_value = if just_internal || suffixed_internal {
+ let lookup_internal = self.lookup_internal.borrow();
+ let lookup_internal = match lookup_internal.upgrade() {
+ Some(f) => f,
+ None => Rc::new(lookup_fail),
+ };
+ lookup_internal(name)
+ } else {
+ self.data
+ .get(name)
+ .map_or_else(String::new, |(_, macro_value)| self.expand(macro_value))
+ };
+ let macro_value = match replacement {
+ Some((subst1, subst2)) => {
+ let subst1 = self.expand(subst1);
+ let subst1_suffix = regex::escape(&subst1);
+ let subst1_suffix =
+ Regex::new(&format!(r"{}\b", subst1_suffix)).unwrap();
+ let subst2 = self.expand(subst2);
+ subst1_suffix.replace_all(&macro_value, subst2).to_string()
+ }
+ None => macro_value,
+ };
+ result.push_str(&macro_value);
+ }
+ Token::FunctionCall { name, args } => {
+ result.push_str(&functions::call(name, args, &self));
+ }
+ }
+ }
+ result
+ }
+}
+
+impl fmt::Debug for MacroSet {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{:?}", &self.data)
+ }
+}
+
+impl fmt::Display for MacroSet {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let pieces = self
+ .data
+ .iter()
+ .map(|(k, (_, v))| format!("{}={}", k, v))
+ .collect::<Vec<_>>();
+ write!(f, "{}", pieces.join("\n"))
+ }
+}
+
+fn builtins() -> Vec<(&'static str, TokenString)> {
+ // Fuck it, might as well.
+ macro_rules! handle {
+ ($value:ident) => {
+ stringify!($value)
+ };
+ ($value:literal) => {
+ $value
+ };
+ }
+ macro_rules! make {
+ ($($name:ident=$value:tt)+) => {vec![$(
+ (stringify!($name), handle!($value).parse().unwrap())
+ ),+]};
+ }
+
+ make![
+ MAKE=makers
+ AR=ar
+ YACC=yacc
+ YFLAGS=""
+ LEX=lex
+ LFLAGS=""
+ LDFLAGS=""
+
+ AS=as
+ CC=cc
+ CXX="g++"
+ CPP="$(CC) -E"
+ FC=f77
+ PC=pc
+ CO=co
+ GET=get
+ LINT=lint
+ MAKEINFO=makeinfo
+ TEX=tex
+ TEXI2DVI=texi2dvi
+ WEAVE=weave
+ CWEAVE=cweave
+ TANGLE=tangle
+ CTANGLE=ctangle
+ RM="rm -f"
+
+ ARFLAGS="rv"
+ CFLAGS=""
+ FFLAGS=""
+ ]
+}
diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs
index 1fc8303..7610197 100644
--- a/src/makefile/mod.rs
+++ b/src/makefile/mod.rs
@@ -7,21 +7,23 @@ use std::io::{BufRead, BufReader, Error as IoError};
use std::path::Path;
use std::rc::Rc;
+use crate::args::Args;
use lazy_static::lazy_static;
use regex::Regex;
-use crate::args::Args;
-
mod command_line;
mod conditional;
mod functions;
mod inference_rules;
+mod r#macro;
+mod pattern;
mod target;
mod token;
use command_line::CommandLine;
use conditional::{ConditionalLine, ConditionalState};
use inference_rules::InferenceRule;
+use r#macro::{LookupInternal, MacroSet, MacroSource};
use target::Target;
use token::{tokenize, Token, TokenString};
@@ -58,13 +60,6 @@ impl LineType {
}
}
-enum MacroSource {
- File,
- CommandLineOrMakeflags,
- Environment,
- Builtin,
-}
-
fn inference_match<'a>(
targets: &[&'a str],
prerequisites: &[String],
@@ -91,7 +86,7 @@ fn inference_match<'a>(
pub(crate) struct Makefile<'a> {
inference_rules: Vec<InferenceRule>,
- macros: HashMap<String, (MacroSource, TokenString)>,
+ macros: MacroSet,
targets: RefCell<HashMap<String, Rc<RefCell<Target>>>>,
pub(crate) first_non_special_target: Option<String>,
args: &'a Args,
@@ -101,15 +96,13 @@ pub(crate) struct Makefile<'a> {
impl<'a> Makefile<'a> {
pub(crate) fn new(args: &'a Args) -> Self {
let mut inference_rules = vec![];
- let mut macros = HashMap::new();
+ let mut macros = MacroSet::new();
let mut targets = HashMap::new();
let first_non_special_target = None;
if !args.no_builtin_rules {
inference_rules.extend(builtin_inference_rules());
- for (k, v) in builtin_macros() {
- macros.insert(k.into(), (MacroSource::Builtin, v));
- }
+ macros.add_builtins();
targets.extend(
builtin_targets()
.into_iter()
@@ -117,20 +110,14 @@ impl<'a> Makefile<'a> {
);
}
- for (k, v) in env::vars() {
- if k != "MAKEFLAGS" && k != "SHELL" {
- macros.insert(k, (MacroSource::Environment, TokenString::text(v)));
- }
- }
+ macros.add_env();
for r#macro in args.macros() {
if let [name, value] = *r#macro.splitn(2, '=').collect::<Vec<_>>() {
- macros.insert(
+ macros.set(
name.into(),
- (
- MacroSource::CommandLineOrMakeflags,
- TokenString::text(value),
- ),
+ MacroSource::CommandLineOrMakeflags,
+ TokenString::text(value),
);
}
}
@@ -197,7 +184,7 @@ impl<'a> Makefile<'a> {
{
line.action(
conditional_stack.last(),
- |name| self.macros.contains_key(name),
+ |name| self.macros.is_defined(name),
|t| self.expand_macros(t, None),
)
.apply_to(&mut conditional_stack);
@@ -375,7 +362,7 @@ impl<'a> Makefile<'a> {
_ => {}
}
- let value = match self.macros.remove(name) {
+ let value = match self.macros.pop(name) {
Some((_, mut old_value)) if append => {
// TODO eagerly expand if appending to eagerly-expanded macro
old_value.extend(TokenString::text(" "));
@@ -384,7 +371,7 @@ impl<'a> Makefile<'a> {
}
_ => value,
};
- self.macros.insert(name.into(), (MacroSource::File, value));
+ self.macros.set(name.into(), MacroSource::File, value);
}
fn special_target_has_prereq(&self, target: &str, name: &str) -> bool {
@@ -501,104 +488,75 @@ impl<'a> Makefile<'a> {
}
fn expand_macros(&self, text: &TokenString, target: Option<&Target>) -> String {
- let mut result = String::new();
- for token in text.tokens() {
- match token {
- Token::Text(t) => result.push_str(t),
- Token::MacroExpansion { name, replacement } => {
- let internal_macro_names = &['@', '?', '<', '*'][..];
- let internal_macro_suffices = &['D', 'F'][..];
- let just_internal = name.len() == 1 && name.starts_with(internal_macro_names);
- let suffixed_internal = name.len() == 2
- && name.starts_with(internal_macro_names)
- && name.ends_with(internal_macro_suffices);
- let macro_value = if just_internal || suffixed_internal {
- let target = target.expect("internal macro but no current target!");
- let macro_pieces = if name.starts_with('@') {
- // The $@ shall evaluate to the full target name of the
- // current target.
- vec![target.name.clone()]
- } else if name.starts_with('?') {
- // The $? macro shall evaluate to the list of prerequisites
- // that are newer than the current target.
- target
- .prerequisites
- .iter()
- .filter(|prereq| {
- self.get_target(prereq)
- .borrow()
- .newer_than(target)
- .unwrap_or(false)
- })
- .cloned()
- .collect()
- } else if name.starts_with('<') {
- // In an inference rule, the $< macro shall evaluate to the
- // filename whose existence allowed the inference rule to be
- // chosen for the target. In the .DEFAULT rule, the $< macro
- // shall evaluate to the current target name.
- target.prerequisites.clone()
- } else if name.starts_with('*') {
- // The $* macro shall evaluate to the current target name with
- // its suffix deleted.
- vec![Path::new(name).with_extension("").to_string_lossy().into()]
- } else {
- unreachable!()
- };
+ let target = target.cloned();
+ let lookup_internal = move |name: &str| {
+ let target = target
+ .as_ref()
+ .expect("internal macro but no current target!");
+ let macro_pieces = if name.starts_with('@') {
+ // The $@ shall evaluate to the full target name of the
+ // current target.
+ vec![target.name.clone()]
+ } else if name.starts_with('?') {
+ // The $? macro shall evaluate to the list of prerequisites
+ // that are newer than the current target.
+ target
+ .prerequisites
+ .iter()
+ .filter(|prereq| {
+ self.get_target(prereq)
+ .borrow()
+ .newer_than(&target)
+ .unwrap_or(false)
+ })
+ .cloned()
+ .collect()
+ } else if name.starts_with('<') {
+ // In an inference rule, the $< macro shall evaluate to the
+ // filename whose existence allowed the inference rule to be
+ // chosen for the target. In the .DEFAULT rule, the $< macro
+ // shall evaluate to the current target name.
+ target.prerequisites.clone()
+ } else if name.starts_with('*') {
+ // The $* macro shall evaluate to the current target name with
+ // its suffix deleted.
+ vec![Path::new(name).with_extension("").to_string_lossy().into()]
+ } else {
+ unreachable!()
+ };
- let macro_pieces = if name.ends_with('D') {
- macro_pieces
- .into_iter()
- .map(|x| {
- Path::new(&x)
- .parent()
- .expect("no parent")
- .to_string_lossy()
- .into()
- })
- .collect()
- } else if name.ends_with('F') {
- macro_pieces
- .into_iter()
- .map(|x| {
- Path::new(&x)
- .file_name()
- .expect("no filename")
- .to_string_lossy()
- .into()
- })
- .collect()
- } else {
- macro_pieces
- };
+ let macro_pieces = if name.ends_with('D') {
+ macro_pieces
+ .into_iter()
+ .map(|x| {
+ Path::new(&x)
+ .parent()
+ .expect("no parent")
+ .to_string_lossy()
+ .into()
+ })
+ .collect()
+ } else if name.ends_with('F') {
+ macro_pieces
+ .into_iter()
+ .map(|x| {
+ Path::new(&x)
+ .file_name()
+ .expect("no filename")
+ .to_string_lossy()
+ .into()
+ })
+ .collect()
+ } else {
+ macro_pieces
+ };
- macro_pieces.join(" ")
- } else {
- self.macros
- .get(name)
- .map_or_else(String::new, |(_, macro_value)| {
- self.expand_macros(macro_value, target)
- })
- };
- let macro_value = match replacement {
- Some((subst1, subst2)) => {
- let subst1 = self.expand_macros(subst1, target);
- let subst1_suffix = regex::escape(&subst1);
- let subst1_suffix =
- Regex::new(&format!(r"{}\b", subst1_suffix)).unwrap();
- let subst2 = self.expand_macros(subst2, target);
- subst1_suffix.replace_all(&macro_value, subst2).to_string()
- }
- None => macro_value,
- };
- result.push_str(&macro_value);
- }
- Token::FunctionCall { name, args } => {
- result.push_str(&self.expand_macros(&functions::call(name, args), None));
- }
- }
- }
- result
+ macro_pieces.join(" ")
+ };
+
+ let lookup_internal: Rc<dyn LookupInternal> = Rc::new(lookup_internal);
+ self.macros.lookup(Rc::downgrade(&lookup_internal));
+ self.macros.expand(text)
}
}
@@ -614,9 +572,7 @@ impl fmt::Display for Makefile<'_> {
writeln!(f)?;
header(f, "Macros")?;
- for (k, (_, v)) in &self.macros {
- writeln!(f, "{}={}", k, v)?;
- }
+ writeln!(f, "{}", &self.macros)?;
writeln!(f)?;
header(f, "Targets")?;
@@ -691,54 +647,6 @@ fn builtin_inference_rules() -> Vec<InferenceRule> {
"rm -f $*.o"
}
}
-fn builtin_macros() -> Vec<(&'static str, TokenString)> {
- // Fuck it, might as well.
- macro_rules! handle {
- ($value:ident) => {
- stringify!($value)
- };
- ($value:literal) => {
- $value
- };
- }
- macro_rules! make {
- ($($name:ident=$value:tt)+) => {vec![$(
- (stringify!($name), handle!($value).parse().unwrap())
- ),+]};
- }
-
- make![
- MAKE=makers
- AR=ar
- YACC=yacc
- YFLAGS=""
- LEX=lex
- LFLAGS=""
- LDFLAGS=""
-
- AS=as
- CC=cc
- CXX="g++"
- CPP="$(CC) -E"
- FC=f77
- PC=pc
- CO=co
- GET=get
- LINT=lint
- MAKEINFO=makeinfo
- TEX=tex
- TEXI2DVI=texi2dvi
- WEAVE=weave
- CWEAVE=cweave
- TANGLE=tangle
- CTANGLE=ctangle
- RM="rm -f"
-
- ARFLAGS="rv"
- CFLAGS=""
- FFLAGS=""
- ]
-}
fn builtin_targets() -> Vec<Target> {
// even i'm not going to do that just for this
vec![Target {
@@ -754,14 +662,14 @@ fn builtin_targets() -> Vec<Target> {
#[cfg(test)]
mod test {
- use super::*;
-
use std::io::Cursor;
+ use super::*;
+
fn empty_makefile(args: &Args) -> Makefile {
Makefile {
inference_rules: vec![],
- macros: HashMap::new(),
+ macros: MacroSet::new(),
targets: RefCell::new(HashMap::new()),
first_non_special_target: None,
args,
@@ -781,8 +689,50 @@ endif
let mut makefile = empty_makefile(&args);
makefile.and_read(Cursor::new(file));
assert_eq!(
- makefile.expand_macros(&"$(worked)".parse().unwrap(), None),
+ makefile.expand_macros(&TokenString::r#macro("worked"), None),
"yes"
);
}
+
+ #[test]
+ #[ignore = "I still haven't implemented `eval` or `define` or %-based macro substitution."]
+ fn eval() {
+ // This, for the record, is a terrible misfeature.
+ // If you need this, you probably shouldn't be using Make.
+ // But a lot of people are using this and still use Make anyway, so here we go,
+ // I guess.
+
+ let file = "
+PROGRAMS = server client
+
+server_OBJS = server.o server_priv.o server_access.o
+server_LIBS = priv protocol
+
+client_OBJS = client.o client_api.o client_mem.o
+client_LIBS = protocol
+
+# Everything after this is generic
+
+.PHONY: all
+all: $(PROGRAMS)
+
+define PROGRAM_template =
+ $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
+ ALL_OBJS += $$($(1)_OBJS)
+endef
+
+$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
+
+$(PROGRAMS):
+ $(LINK.o) $^ $(LDLIBS) -o $@
+
+clean:
+ rm -f $(ALL_OBJS) $(PROGRAMS)
+ ";
+
+ let args = Args::empty();
+ let mut makefile = empty_makefile(&args);
+ makefile.and_read(Cursor::new(file));
+ assert!(makefile.targets.borrow().contains_key("server"))
+ }
}
diff --git a/src/makefile/pattern.rs b/src/makefile/pattern.rs
new file mode 100644
index 0000000..733721e
--- /dev/null
+++ b/src/makefile/pattern.rs
@@ -0,0 +1,49 @@
+use regex::{Captures, Regex};
+
+fn compile_pattern(pattern: &str) -> Regex {
+ let mut result = String::new();
+ for c in pattern.chars() {
+ match c {
+ // This is a nightmare, because we're escaping for regex syntax before we put
+ // things into result.
+
+ // We don't end with a backslash, so this is an unescaped wildcard.
+ '%' if !result.ends_with(r"\\") => {
+ result.push_str(r"(\w*)");
+ }
+
+ // We end with two backslashes, so this is an escaped backslash and then an
+ // unescaped wildcard.
+ '%' if result.ends_with(r"\\\\") => {
+ result = result.strip_suffix(r"\\\\").unwrap().to_string();
+ result.push_str(r"\\(\w*)");
+ }
+
+ // We end with one backslash, so this is an escaped wildcard.
+ '%' if result.ends_with(r"\\") => {
+ result = result.strip_suffix(r"\\").unwrap().to_string();
+ result.push('%');
+ }
+ _ => result.push_str(&regex::escape(&c.to_string())),
+ }
+ }
+ Regex::new(&result).expect("built invalid regex!")
+}
+
+pub(crate) fn r#match<'a>(pattern: &str, text: &'a str) -> Option<Captures<'a>> {
+ compile_pattern(pattern).captures(text)
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn pattern_backslashes() {
+ let test_case = compile_pattern(r"the\%weird\\%pattern\\");
+ assert_eq!(test_case.to_string(), r"the%weird\\(\w*)pattern\\\\");
+
+ let hell = compile_pattern(r"\\\\%");
+ assert_eq!(hell.to_string(), r"\\\\\\(\w*)");
+ }
+}
diff --git a/src/makefile/token.rs b/src/makefile/token.rs
index eca40ed..8b21f06 100644
--- a/src/makefile/token.rs
+++ b/src/makefile/token.rs
@@ -19,6 +19,13 @@ impl TokenString {
Self(vec![Token::Text(text.into())])
}
+ pub(crate) fn r#macro(name: impl Into<String>) -> Self {
+ Self(vec![Token::MacroExpansion {
+ name: name.into(),
+ replacement: None,
+ }])
+ }
+
pub(crate) fn tokens(&self) -> impl Iterator<Item = &Token> {
self.0.iter()
}