From: Matt McCutchen Date: Thu, 14 Jun 2007 02:11:41 +0000 (-0400) Subject: Changes necessary and proper to get mgear to build rsync: X-Git-Url: https://mattmccutchen.net/mgear/mgear.git/commitdiff_plain/0382f3351db8ba1e76ce006859783b7d49622892 Changes necessary and proper to get mgear to build rsync: - $(mg-define-rule) now takes the name of the variable holding the command instead of the command itself. This generally saves the user a $(value). - I changed my mind again: $@ in a command is translated to the temp file rather than the eventual target. - Handle commas in commands better. - Enforce that all files are secondary. --- diff --git a/demo/Makefile b/demo/Makefile index 7eb0502..ff220b6 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -6,11 +6,11 @@ include mgear.mk all: -cmd-xgrep = grep 'X' '$<' >'$t' && echo "Hello world from xgrep" -$(call mg-define-rule,%.x,%,$(value cmd-xgrep)) +cmd-xgrep = grep 'X' '$<' >'$@' && echo "Hello world from xgrep" +$(call mg-define-rule,%.x,%,cmd-xgrep) -cmd-addheader = echo 'Header:' | cat - '$<' >'$t' -$(call mg-define-rule,%.h,%,$(value cmd-addheader)) +cmd-addheader = echo 'Header:' | cat - '$<' >'$@' +$(call mg-define-rule,%.h,%,cmd-addheader) # Testing obfuscation mainoid := $(newoid) diff --git a/mgear.mk b/mgear.mk index 7eb2877..cccdbc2 100644 --- a/mgear.mk +++ b/mgear.mk @@ -10,11 +10,16 @@ mg-orig-default-goal:=$(.DEFAULT_GOAL) # 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 @@ -25,6 +30,9 @@ sq='$(subst ','\'',$1)' # 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)) @@ -171,8 +179,8 @@ $(mg-scout-oid)@opr:= $(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)) @@ -181,11 +189,11 @@ mg+ = $(filter-out MG-% $(opfx)%,$+) # $|: 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: @@ -204,9 +212,10 @@ mg-prereq-predict=$$$$(call mg-set-gdeps,$$$$+ $(if $(findstring /,$1),$$$$(subs 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. @@ -216,7 +225,7 @@ MG-FORCE: # $(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. diff --git a/testsuite b/testsuite index 8cb46a0..f7ec807 100755 --- a/testsuite +++ b/testsuite @@ -56,7 +56,7 @@ function assert_saw { } function assert_not_saw { if ! grep -q "$@" mgear.log; then - echo "Good, saw '${@:$#}' in build log." + echo "Good, didn't see '${@:$#}' in build log." else echo "Did not expect '${@:$#}' in build log but saw it!" fail @@ -118,8 +118,10 @@ include mgear.mk .SECONDARY: include hc-rule.mk -$(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t) -$(call mg-define-rule,index,index.in,sort $$< >$$t) +cmd-ssc=sleep 1 && grep 'warn' $< && sed -e 's_//.*$$__' $< >$@ +$(call mg-define-rule,%,%.ssc,cmd-ssc) +cmd-index=sort $< >$@ +$(call mg-define-rule,index,index.in,cmd-index) clean: $(call mg-clean-cmd,.) @@ -127,7 +129,8 @@ clean: EOF cat >hc-rule.mk <<'EOF' -$(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t) +cmd-hc=sed -e 's_$(hash).*$$__' $< >$@ +$(call mg-define-rule,%,%.hc,cmd-hc) EOF # Input files. @@ -218,10 +221,12 @@ EOF # Change the rule for # comments to strip spaces before a #. # Make sure foo is updated properly. +# Also, make sure commas in commands work properly. start_section "Command change for % <- %.hc" cat >hc-rule.mk <<'EOF' -$(call mg-define-rule,%,%.hc,sed -e 's_ *#.*$$$$__' $$< >$$t) +cmd-hc=sed -e 's, *$(hash).*$$,,' $< >$@ +$(call mg-define-rule,%,%.hc,cmd-hc) EOF do_mgear foo