makefile - 在不使用目标的情况下,如何防止目标的先决条件扩展?
问题描述
在具有多个目标的 Makefile 中,如何防止扩展未使用的目标的先决条件?请参见以下示例:
thisexpands = $(warning Expanded!)
.PHONY: target1
target1: $(thisexpands)
@echo Target 1
.PHONY: target2
target2:
@echo Target 2
呼吁target2
力量thisexpands
扩大,即使它被懒惰地评估并且它target1
从未被使用过。
在我的现实世界中thisexpands
,调用时扩展target1
是一个问题,因为它是一个 shell 命令,当在 target1 的上下文之外调用时会打印错误,并且它是父目标(此处未显示)。
解决方案
在运行第一条规则之前,Makefile 已被完全解析。作为解析的一部分,必须扩展所有目标和先决条件。您可以在 GNU make 手册中的How make Reads a Makefile中找到有关 makefile 不同部分何时发生扩展的详细信息。
一种方法是使用递归:
thisexpands = $(warning Expanded!)
target1: ; $(MAKE) target1-recurse T1_DEPS='$(value thisexpands)'
T1_DEPS =
target1-recurse: $(T1_DEPS)
@echo Target 1
这不起作用:
您可以通过使用辅助扩展来推迟扩展,如下所示:
.SECONDEXPANSION:
target1: $$(thisexpands)
要非常小心,适当地转义先决条件列表。
推荐阅读
- android - 在 Android 中点击一定次数后,使用 SharedPreference 使按钮无法点击
- c# - 在 C# 中获取 OpenPGP 密钥的指纹?
- python - 在python中将字符串转换为二维数组
- makefile - gnu makefile:如果条件在多行赋值中
- reactjs - 我怎么知道在组件卸载后计时器是否消失了?
- c# - 在 Selenium C# 中打开多个浏览器
- spring - 如何从 JPA 获取不同的记录
- python - 在一行代码中从表中获取第一个元素的列表[python]
- c# - 找不到面板添加到面板服务器端
- java - 如何包装发送消息到断路器内的队列(springboot +弹性4j)