生成文件&中的特定于目标的变量先决条件 [英] Target-specific variables in a makefile & prerequisites

查看:73
本文介绍了生成文件&中的特定于目标的变量先决条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GNU make中看到针对特定目标变量的意外结果.

I'm seeing unexpected results for target-specfic variables in GNU make.

我想要设置一个影响依赖项的特定于目标的变量.我可以使用.SECONDEXPANSION来实现.

What I want is to set a target-specific variable that affects dependencies. I can use .SECONDEXPANSION to achieve that.

some-target: DEP := debug-dep

debug: some-target

.SECONDEXPANSION:
some-target: $$(DEP)
    @echo $^

debug-dep:

make debug打印debug-dep.

现在,我读到 make为后代规则定义了特定于目标的变量:

定义特定于目标的变量时,该变量值也对该目标的所有先决条件及其所有先决条件等有效.

When you define a target-specific variable that variable value is also in effect for all prerequisites of this target, and all their prerequisites, etc.

但是当我更改makefile以在父"目标上设置变量时:

But when I change my makefile to have the variable set on "parent" target:

debug: DEP := debug-dep

debug: some-target

.SECONDEXPANSION:
some-target: $$(DEP)
    @echo $^

debug-dep:

然后做make debug我得到一个空白行.

and do make debug I get a blank line.

这似乎与记录的目标特定变量的行为相矛盾.有什么我想念的吗?

This seems to contradict the documented behavior of target-specific variables. Is there something that I am missing?

这有点类似于 make:在先决条件中使用目标特定的变量,但是我的工作不起作用.

This is sort of similar to make: Using target specific variables in prerequisites, but what I am doing isn't working.

推荐答案

我相信这里的问题是直到运行目标之前,才设置特定于目标的变量(如果将@echo '$(DEP)'添加到some-target正文中在第二种情况下,您将看到已设置),但是第二种扩展发生在初始读入阶段之后.

I believe the issue here is that target-specific variables are not set until the targets are being run (if you add @echo '$(DEP)' to your some-target body in that second case you will see that it is set) but that second expansion happens immediately after the initial read-in phase.

我要说的是,我真的很惊讶这在第一种情况下完全有效(并推测原因),但随后我拉了一下手册,一边阅读有关.SECONDEXPANSION的内容,却发现关注:

I was going to say that I'm actually surprised that this works in the first case at all (and speculate as to why) but then I pulled up the manual for a minute and while reading about .SECONDEXPANSION I found the following:

仅当您发现该目标的自动变量范围内总是发生二次扩展时,此功能的真正力量才变得显而易见.这意味着您可以在第二次扩展中使用$ @,$ *等变量,它们将具有其期望值,就像在配方中一样.您所要做的就是通过转义$来延迟扩展.同样,显式和隐式(模式)规则都会发生二次扩展.

[T]he true power of this feature only becomes apparent when you discover that secondary expansions always take place within the scope of the automatic variables for that target. This means that you can use variables such as $@, $*, etc. during the second expansion and they will have their expected values, just as in the recipe. All you have to do is defer the expansion by escaping the $. Also, secondary expansion occurs for both explicit and implicit (pattern) rules.

确切解释了您的行为.扩展只会看到在目标范围内设置的变量,前提条件继承仅在目标评估时发生(因为它取决于目标前提条件).

Which explains your behaviour exactly. The expansion only sees variables set in the scope of the target and the prerequisite inheriting only happens at target evaluation time (since it depends on target prerequisites).

这篇关于生成文件&中的特定于目标的变量先决条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆