- Add a full target obfuscation factory and rule and a comment explaining them.
[mgear/mgear.git] / src / mage.mk
CommitLineData
ac64c802
MM
1# Mage Build Tool by Matt McCutchen
2# http://www.kepreon.com/~matt/mage/
00804b7c 3
ac64c802
MM
4# Remember the original default goal so we can restore it at the end of mage.mk.
5mg-orig-default-goal := $(.DEFAULT_GOAL)
6
7# TEXT UTILITIES
00804b7c
MM
8empty :=
9bs := \$(empty)
10define nl
11
12
13endef
14# Shell-quote: a'b => 'a'\''b'
15sq = '$(subst ','\'',$1)'
16# Make-quote: a$\nb => a$$$(nl)b
17# This is enough to assign the value, *but not to use it as an argument!*
18mqas = $(subst $(nl),$$(nl),$(subst $$,$$$$,$1))
19# Return nonempty if the strings are equal, empty otherwise.
20# If the strings have only nice characters, you can do $(filter x$1,x$2).
21streq = $(findstring x$1,$(findstring x$2,x$1))
22
23# $(call fmt-make-assignment,foo,bar)
24# Output a make assignment that stores bar in variable foo.
25# The result is like `foo:=bar' but handles leading spaces, $, and
26# newlines appearing in bar safely.
27fmt-make-assignment = $1:=$$(empty)$(call mqas,$2)
28
8d481cf5
MM
29# We use second-expansion heavily to dynamically compute prerequisites when they
30# are needed. Second-expansion lets us follow make's implicit rule search
31# instead of trying to anticipate which prerequisites it will need in advance.
32.SECONDEXPANSION:
ac64c802 33
8d481cf5
MM
34# TARGET OBFUSCATION
35
36# Mage uses two kinds of "obfuscated targets" (those that are not the simple
37# names of real files):
38# - An always-exists target like /.//. is used as a prerequisite of an implicit
39# rule. Since it exists, make doesn't second-expand its own prerequisites
40# until it is actually run. This way, a target's prerequisites can depend on
41# the results of previous command scripts.
42# - An alternative-name target like /.//./proc/self/cwd/bar is used to check the
43# mtime of a file without actually building it or introducing a circular
44# dependency.
45
46# $(newoid) allocates and returns a new obfuscation ID (oid). Example:
47# x:=$(newoid)
48# You can then refer to target $x or $x$(aname)bar (for any file bar).
49# Prerequisites and commands come from $($x@opr) and $($x@ocmd).
50# Target-specific variables $(oid) and $(otgt) (if alternate-name) are
51# available.
52
53opfx:=/./
54# TODO If the system doesn't support /proc/self/cwd, use something else.
55aname:=/proc/self/cwd/
56nextoid:=/./.
57newoid=$(nextoid)$(eval nextoid:=$(subst /.//////,//./,$(patsubst /.//////%,/././%,$(nextoid:.=/.))))
58
59# High-priority implicit rule for obfuscated targets. Works for both always-
60# exists and alternate-name targets.
61$(opfx)%: oid=$(word 1,$(subst $(aname), ,$@))
62$(opfx)%: otgt=$(word 2,$(subst $(aname), ,$@))
63$(opfx)%: $$($$(oid)@opr)
64 $($(oid)@ocmd)
00804b7c 65
ac64c802
MM
66# MAIN BUILD LOGIC
67
68# bar.g file format:
69# bar@cmd:=cat foo >bar.tmp
70# bar@warnings:=$(empty)yikes!
71# And if dependency-logging:
72# List of filename@revision, revision is x for exists and empty for doesn't
73# exist. Later perhaps x will be the mtime.
74# bar@deps:=included@x oops@
75# If bar is overridden, we clear all three variables.
76
77# $(call gload,foo.o)
00804b7c 78# Make sure foo.o's genfile, if any, has been loaded.
ac64c802
MM
79define gload
80$(if $($1@gloaded),,$(eval
81$1@cmd:=
82$1@warnings:=
83$1@deps:=
00804b7c 84-include $1.g
ac64c802 85$1@gloaded:=1
00804b7c
MM
86))
87endef
88
89# $(call mg-translate-cmd,touch $$@)
ac64c802
MM
90# Translates $@, $<, $^, $+, $* to Mage-ized variables if necessary.
91# We won't support $%, $?, $|.
92# There might be false matches, e.g., $$@ => $$(mg@) ;
00804b7c
MM
93# to prevent that, do $$$(empty)@ .
94define mg-translate-cmd
95$(subst $$@,$$(mg@),$(subst $$<,$$(mg<),$(subst $$^,$$(mg^),$(subst $$+,$$(mg+),$1))))
96endef
97
00804b7c
MM
98# $(call mg-rule,target,prerequisite,cmd)
99# Defines a rule.
100# If cmd uses $@, quote if necessary so this function sees $@, etc.
101#
ac64c802 102# When we generate bar from foo using a Mage rule:
00804b7c 103# Division of work:
ac64c802
MM
104# - "bar.g: foo" generates bar if necessary.
105# - "bar: bar.g" loads new genfile and replays warnings if bar.g was
106# already up to date.
00804b7c
MM
107# Cases:
108# - bar is managed and up to date => replay
ac64c802 109# - bar doesn't exist or is managed and out of date => generate
00804b7c 110# - bar is unmanaged => warn about override
ac64c802 111MG-FORCE:
ce8fee85 112.PHONY: MG-FORCE
8d481cf5 113scout-oid:=$(newoid)
ac64c802 114define mg-define-rule
00804b7c
MM
115$(eval
116
117# Define some target-specific variables.
118# It might look like we could use $*, but $1 most likely isn't %.
119$1.g: target = $$(@:.g=)
120$1.g: cmd = $(call mg-translate-cmd,$3)
121$1.g: mg@ = $$(target).tmp
ac64c802
MM
122$1.g: mg^ = $$(filter-out MG-% /./%,$$^)
123$1.g: mg+ = $$(filter-out MG-% /./%,$$+)
00804b7c
MM
124$1.g: mg< = $$(firstword $$(mg^))
125
ac64c802
MM
126# Rule for the genfile. Evidently all the prerequisites we want second-expanded
127# have to go on the same rule.
128$1.g: $2 $$$$(mg-scout-target) MG-FORCE
129 $$(mg-generate)
130
131# If the file was regenerated, load the new genfile.
132# If not, replay any warnings.
133## We don't have to worry about .DELETE_ON_ERROR deleting the target because
134## it is only touched if we *successfully* regenerate it.
135# - No longer applies because we don't remember errors.
136# HMMM Maybe errors should go to stderr???
137$1: MG-FORCE | $1.g
138 $$(call gload,$$@)
139 $$(if $$($$@@warnings),$$(if $$($$@@built),,$$(info $$($$@@cmd) # warning replay)$$(info $$($$@@warnings))),)
140)
141endef
00804b7c 142
00804b7c
MM
143# If the target is unmanaged, we must run the rule; we'll see that the
144# obfuscated target is in $? and complain.
8d481cf5 145mg-scout-target=$(if $(wildcard $(target)),$(scout-oid)$(aname)$(target),)
00804b7c
MM
146
147# If the command changed, we must regenerate.
ac64c802 148mg-check-cmd=$(if $(call streq,$(cmd),$($(target)@cmd)),,x)
00804b7c
MM
149
150# Just add additional prerequisites.
ac64c802
MM
151# I don't think this works for implicit rules.
152define mg-define-prereq
00804b7c
MM
153$(eval
154$1.g: $2
155)
156endef
157
ac64c802 158# Procedure to generate bar. Remember, $@ is bar.g.
00804b7c 159# If an override:
ac64c802
MM
160# 1. Complain. (Should we stop "`bar' is up to date" using @:; ?)
161# 2. Clear out the warnings so we don't replay them.
162# Otherwise, check prereqs, command, and nonexistence to decide whether bar
163# needs to be regenerated. If so:
164# 1. Set a flag so Mage knows to reread the genfile when make runs target
165# "bar".
166# 2. Echo the command being run.
167# On error, skip to 8:
168# 3. Open the new genfile bar.g.tmp for writing.
169# 4. Store the command in bar.g.tmp.
170# 5. Run the command to bar.tmp, storing warnings in bar.g.tmp.
171# 6. If bar.tmp differs from bar:
172# a. Clear and touch bar.g (so bar doesn't become an override if we're
173# killed between steps 6b and 7).
174# b. Move in the new bar.
175# 7. Now that bar is known to be up to date, move in the new bar.g.
176# 8. Defensively remove both temporary files and exit with the exit code
177# from before the removal. (trap EXIT)
178define mg-generate
179 $(call gload,$(target))\
8d481cf5 180 $(if $(filter $(scout-oid)$(aname)$(target),$?),\
00804b7c 181 $(info mage: warning: Manually created/modified file at $(target) overrides rule.)\
ac64c802 182 $(eval $(target)@cmd:=)$(eval $(target)@warnings:=)$(eval $(target)@deps:=)\
00804b7c 183 ,\
ac64c802
MM
184 $(if $?$(if $(wildcard $(target)),,x)$(mg-check-cmd),\
185 $(eval $(target)@gloaded:=)$(eval $(target)@built:=1)$(info $(cmd))\
186 @trap 'rm -f $(target).tmp $@.tmp' EXIT &&\
187 exec 3>$@.tmp && $(mg-assign-cmd) >&3 &&\
188 set -o pipefail && { $(mg-run-cmd) | tee /dev/fd/4 | $(mg-wrap-warnings) >&3; } 4>&1 &&\
189 $(mg-maybe-move-target) && mv -f $@.tmp $@\
190 ))
00804b7c
MM
191endef
192
ac64c802
MM
193# Pieces of mg-generate that I factored out to make mg-generate more readable.
194mg-assign-cmd=echo $(call sq,$(call fmt-make-assignment,$(target)@cmd,$(cmd)))
195mg-run-cmd={ ($(cmd)) 2>&1 && { [ -r $(target).tmp ] || { echo 'mage: error: Command for $(target) succeeded without creating it!'; false; }; }; }
196mg-wrap-warnings=sed -re '1s/^/$(target)@warnings:=$$(empty)/; 1!s/^/$(target)@warnings+=$$(nl)/'
197mg-maybe-move-target={ cmp -s $(target) $(target).tmp || echo >$@ && mv -f $(target).tmp $(target); }
198
199# END
200
00804b7c 201# We don't want anything we defined to become the default goal.
ac64c802 202.DEFAULT_GOAL := $(mg-orig-default-goal)