我想从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 $^ >> $@
我想删除冗余代码。通过使所有目标/依赖组合共享一个规则,或通过其他方式。
答案 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 $^ >> $@