Make: Setting target-specific variables in static pattern rules

风格不统一 提交于 2019-12-11 06:58:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!