【问题标题】:Simple makefile for rendering templates用于渲染模板的简单 makefile
【发布时间】:2021-06-07 15:02:20
【问题描述】:
我想要尽可能简单的 Makefile,因此使用 make(无参数)将呈现当前目录中的所有 *.erb 模板,删除模板扩展名。到目前为止,这是我想出的:
INPUTS = $(wildcard *.erb)
all: $(INPUTS:.erb=)
%: %.erb
cp $@ $@.old 2>/dev/null || true
erb -T - $< > $@
为什么不只是目标符号独立工作或例如all: $($(wildcard *.erb):.erb=)?
有没有办法实现这个更短/更优雅?
【问题讨论】:
标签:
templates
makefile
gnu-make
erb
【解决方案1】:
$($(wildcard *.erb):.erb=) 之类的东西不起作用,因为 $(...:...) 格式是 variable 替换,而不是 string 替换。也就是说,:左边的东西是要扩展的变量的name,而不是字符串。
所以$($(wildcard *.erb):...) 将首先运行通配符并替换结果,然后将其视为要替换的变量的名称。显然没有那个名字的变量,所以它扩展为空字符串。
如果您不想要INPUTS 变量,您可以使用basename 函数:
all: $(basename $(wildcard *.erb))
至于它是否“更优雅”,这是旁观者的看法,所以并不是 SO 可以提供帮助的。