make targets depend on variables
Make wasn't designed to refer to variable content but Reinier's approach shows us the workaround. Unfortunately, using variable value as a file name is both insecure and error-prone. Hopefully, Unix tools can help us to properly encode the value. So
IMPORTANTVARIABLE = a trouble
# GUARD is a function which calculates md5 sum for its
# argument variable name. Note, that both cut and md5sum are
# members of coreutils package so they should be available on
# nearly all systems.
GUARD = $(1)_GUARD_$(shell echo $($(1)) | md5sum | cut -d ' ' -f 1)
foo: bar $(call GUARD,IMPORTANTVARIABLE)
@echo "Rebuilding foo with $(IMPORTANTVARIABLE)"
@touch $@
$(call GUARD,IMPORTANTVARIABLE):
rm -rf IMPORTANTVARIABLE*
touch $@
Here you virtually depend your target on a special file named $(NAME)_GUARD_$(VALUEMD5)
which is safe to refer to and has (almost) 1-to-1 correspondence with variable's value. Note that call
and shell
are GNU Make extensions.
You could use empty files to record the last value of your variable by using something like this:
someTarget: IMPORTANTVARIABLE.$(IMPORTANTVARIABLE)
@echo Remaking $@ because IMPORTANTVARIABLE has changed
touch $@
IMPORTANTVARIABLE.$(IMPORTANTVARIABLE):
@rm -f IMPORTANTVARIABLE.*
touch $@
After your make
run, there will be an empty file in your directory whose name starts with IMPORTANTVARIABLE.
and has the value of your variable appended. This basically contains the information about what the last value of the variable IMPORTANTVARIABLE
was.
You can add more variables with this approach and make it more sophisticated using pattern rules -- but this example gives you the gist of it.