问题
I'm writing a Makefile using static pattern rules and I want for each element of TARGETS a variable assigned to the current target name (here the stem '%').
TARGETS = a b c d
all : $(TARGETS)
$(TARGETS) : % : DIR = %
$(TARGETS) : % : %_setup build
a_setup :
code for a
b_setup :
code for b
...
build
code using "DIR = XX" previously configured
but gnumake complains about the target-specific variable DIR:
make: *** No rule to make target 'DIR', needed by 'a'
Is it possible to mix static pattern rules and variable assignation? Thanks!
回答1:
According to the GNU make manual you can't do it like that. However, you can use $@. In you example you can directly assign DIR=$@ but more generally you can use $@ in combination with patsubst:
TARGETS = a b c d
all : $(TARGETS)
$(TARGETS) : DIR = $(patsubst %,%,$@)
$(TARGETS) : % : %_setup build
echo $@: DIR:$(DIR)
%_setup :
echo $@
build:
echo $@
来源:https://stackoverflow.com/questions/42013532/make-setting-target-specific-variables-in-static-pattern-rules