# instead of trying to anticipate which prerequisites it will need in advance.
.SECONDEXPANSION:
+# Deletion of intermediate files messes everything up.
+# TODO: See if we can relax this requirement.
+.SECONDARY:
+
# TEXT UTILITIES
empty:=
bs:=\$(empty)
hash:=\#
+comma:=,
pct:=%
define nl
# Make-quote: a$\nb => a$$$(nl)b
# This is enough to assign the value, *but not to use it as an argument!*
mqas=$(subst $(hash),$$(hash),$(subst $(nl),$$(nl),$(subst $$,$$$$,$1)))
+# Make the value safe to use as an argument without doubling $.
+# *The implementation is incomplete*; I will improve it as needed.
+msarg=$(subst $(comma),$$(comma),$1)
# Return nonempty if the strings are equal, empty otherwise.
# If the strings have only nice characters, you can do $(filter x$1,x$2).
streq=$(findstring x$1,$(findstring x$2,x$1))
$(mg-scout-oid)@ocmd:=
# Mgear-ized automatic variables.
-# $@: needs no translation
-# NOTE: $(mg@) is *eventual* target. Commands must write to temp file, $t .
+# For now, if you really want the eventual target, write $$(@).
+mg@ = $@.tmp
# $%: haven't thought about it much, but probably needs no translation
mg< = $(firstword $(mg^))
mg? = $(filter-out $(opfx)%,$($@@gq))
# $|: needs no translation
# $*: needs no translation
-# $(call mg-translate-cmd,cat $$< >$$t)
+# $(call mg-translate-cmd,cat $$< >$$@)
# Replaces references to automatic variables with references to their mgear-ized
# counterparts. There might be false matches, e.g., $$@ => $$(mg@) ;
# to prevent that, write $$$(empty)@ instead. (c.f. autoconf empty quadrigraph)
-mg-translate-cmd=$(subst $$t,$$@.tmp,$(subst $$?,$$(mg?),$(subst $$<,$$(mg<),$(subst $$^,$$(mg^),$(subst $$+,$$(mg+),$1)))))
+mg-translate-cmd=$(subst $$@,$$(mg@),$(subst $$?,$$(mg?),$(subst $$<,$$(mg<),$(subst $$^,$$(mg^),$(subst $$+,$$(mg+),$1)))))
# $(call mg-prereq-predict,target,prerequisites)
# Expands to code that does the following at second-expansion time:
MG-FORCE:
.PHONY: MG-FORCE
-# $(call mg-define-rule,target,prerequisites,cmd)
-# Defines a rule. cmd is expanded again when it is run, at which time
-# Mgear-ized automatic variables are available.
+# $(call mg-define-rule,target,prerequisites,cmdvar)
+# Defines a rule. cmdvar is *the name of a variable* containing the command.
+# The variable is read immediately with $(value) and then expanded in the scope
+# of mgear-ized automatic variables each time the rule is run.
#
# I eradicated the target-specific variables because they fail when there are
# multiple implicit rules with the same target pattern.
# $(bar@gq).
define mg-define-rule
$(eval $1: $(call mg-prereq-predict,$1,$2) $2 MG-FORCE $(mg-genfile-oid)$(aname)$$$$@.g
- $$(call mg-rule-cmd,$(call mg-translate-cmd,$3)))
+ $$(call mg-rule-cmd,$(call msarg,$(call mg-translate-cmd,$(value $3)))))
endef
# TODO Provide a way to define static pattern rules.