【发布时间】:2018-03-14 19:39:07
【问题描述】:
我希望能够在 GNU make 中另一个规则的先决条件中使用在规则中创建的目标的结果。比如:
PREREQ = $(shell echo "reading target1" >&2; cat target1)
target1:
echo "prereq" > $@
target2: target1 $(PREREQ)
echo foo > $@
target2 应该依赖于从 target1 文件中读取的 prereq,但在执行 target1 配方之前,它不在文件中。
当然,这是一个非常人为的例子,我确信有很多关于如何重构这个特定例子的建议,但我不打算重构这个例子。这只是我的更复杂问题的一个简化示例,我需要从一个文件的内容中获取先决条件,该文件在执行 Makefile 中的配方之前没有创建。
问题是,[如何] 我可以扩展$(PREREQ)(因此$(shell cat target1) 的执行推迟到target1 规则实际执行之后?
更新:我尝试了.SECONDARYEXPANSION:,但这似乎并没有起到作用:
$ make -d target2
...
reading target1
cat: target1: No such file or directory
...
Updating goal targets....
Considering target file 'target2'.
File 'target2' does not exist.
Considering target file 'target1'.
File 'target1' does not exist.
Finished prerequisites of target file 'target1'.
Must remake target 'target1'.
echo "prereq" > target1
[ child management ]
Successfully remade target file 'target1'.
Finished prerequisites of target file 'target2'.
Must remake target 'target2'.
echo foo > target2
[ child management ]
Successfully remade target file 'target2'.
如您所见,“阅读目标”在一开始只打印了一次,表明 PREREQ 没有再次扩展,因为 .SECONDEXPANSION: 和目标列表考虑 target2 不包括 prereq。
【问题讨论】:
-
对不起,我现在意识到它不能按预期工作。但是要明确一点:“二次扩展”并不是要扩展两次,而是在匹配规则的那一刻扩展(即:仍然只有一次)。问题似乎是在创建
target1之前仍然执行了二次扩展。 -
我删除了之前的答案并添加了一个全新的潜在解决方案。我希望它有所帮助。