相同的规则,不同的目标和依赖关系

时间:2018-01-19 23:21:57

标签: makefile gnu-make

我想从Makefile中删除一些重复内容。它接收带有主机列表的文件,并将它们连接成更大的组。例如,这会从hosts/west.txt的内容生成hosts/oregon.txt hosts/washington.txt hosts/idaho.txt,依此类推。

ALLFILE = hosts/all.txt
WESTFILE = hosts/west.txt
EASTFILE = hosts/east.txt
GENERATEDFILES = $(ALLFILE) $(WESTFILE) $(EASTFILE)

ALLFILES = $(filter-out $(GENERATEDFILES), $(wildcard hosts/*.txt))
WESTFILES = hosts/oregon.txt hosts/washington.txt hosts/idaho.txt
EASTFILES = hosts/new_york.txt hosts/virginia.txt

$(ALLFILE) : $(ALLFILES)
    @echo '# DO NOT EDIT' > $@
    @echo '# Edit individual host/ files instead.' >> $@
    @echo '# Regenerate with `make`.' >> $@
    @echo '' >> $@
    cat $^ >> $@

$(WESTFILE) : $(WESTFILES)
    @echo '# DO NOT EDIT' > $@
    @echo '# Edit individual host/ files instead.' >> $@
    @echo '# Regenerate with `make`.' >> $@
    @echo '' >> $@
    cat $^ >> $@

$(EASTFILE) : $(EASTFILES)
    @echo '# DO NOT EDIT' > $@
    @echo '# Edit individual host/ files instead.' >> $@
    @echo '# Regenerate with `make`.' >> $@
    @echo '' >> $@
    cat $^ >> $@

我想删除冗余代码。通过使所有目标/依赖组合共享一个规则,或通过其他方式。

1 个答案:

答案 0 :(得分:2)

$(ALLFILE) : $(ALLFILES)
$(WESTFILE) : $(WESTFILES)
$(EASTFILE) : $(EASTFILES)

$(ALLFILE) $(WESTFILE) $(EASTFILE):
    @echo '# DO NOT EDIT' > $@
    @echo '# Edit individual host/ files instead.' >> $@
    @echo '# Regenerate with `make`.' >> $@
    @echo '' >> $@
    cat $^ >> $@