如何在仅订购先决条件内调用函数? [英] How to call functions within order-only prerequisites?
问题描述
给出Makefile的这一点:
Given this bit of Makefile:
# for pattern matching
$(OBJDIR) := build
# just to see if a level of indirection will work
my_dir = $(dir $(1))
$(OBJECTS) : $(OBJDIR)/% : $(HEADERS) $(SRCDIR)/% | % $(dir %) $(call my_dir,%)
@echo output-only = $|
考虑目标"build/utility/debug.js".上面规则的输出将是这样:
Consider the target "build/utility/debug.js". The output of the above rule will be this:
output-only = utility/debug.js ./
- 第一个组件"utility/debug.js"已从词干(%)中正确复制.
- 第二个组件"./"是先决条件列表中调用
dir
函数的输出. - 第三个组件,一个空字符串,是在先决条件列表中调用我的
my_dir
函数的输出. - The first component, "utility/debug.js", is properly copied from the stem (%).
- The second component, "./", is the output of calling the
dir
function in the prerequisites list. - The third component, an empty string, is the output of calling my
my_dir
function in the prerequisites list.
如果我将my_dir
更改为此:
my_dir = $(1)
输出保持不变.如果我将其更改为此:
The output remains the same. If I change it to this:
my_dir = "foo"
然后,make抱怨没有规则可以使"foo"(这是预期的).这样看来,$(1)
并没有绑定到对my_dir
的调用中.
Then make complains there is no rule to make "foo" (which is expected). It appears, then, that $(1)
is not getting bound in the call to my_dir
.
这是怎么回事?为什么我不能将词干传递给函数?我有一个使用二级扩展的解决方法,但是我想知道为什么我不能用这种方式编写规则.
What's going on? Why can't I pass the stem to a function? I have a workaround that uses secondary expansion, but I want to know why I can't write my rule this way.
编辑:我是stackoverflow的新手,如果这不是这里做事的方式,请原谅我.
I'm new to stackoverflow, forgive me if this is not the way things are done here.
我想要$(1),因为正如Alex指出的那样,我将词干作为参数传递给my_dir
.
I want $(1) because I am passing the stem as an argument to my_dir
, as Alex pointed out.
我不知道为什么有人建议我要"$".我不认为$本身可以在任何情况下扩展到任何东西.
I don't know why it was suggested I want "$". I don't believe $ by itself expands to anything in any context.
我知道自动变量仅在配方中可用.我没有在先决条件中使用自动变量-我正在使用词干:
I know that automatic variables are only available in the recipe. I am not using an automatic variable in the prerequisites - I am using the stem:
将每个目标与目标模式进行匹配,以提取目标名称的一部分,即茎.该词干被替换为每个prereq模式中的一个,以制成先决条件名称(每个prereq模式中的一个). -手册
以下示例说明了词干可用的事实:词干在单独使用时会扩展到正确的值,但在传递给函数时不会扩展.
The fact that the stem is available is demonstrated by the example: the stem expands to the correct value when used alone, but not when passed to a function.
推荐答案
As can be seen in this section of the GNU make manual, variable and function references in the list of prerequisites are immediately expanded, during the read in phase. This means, before any pattern matching is done, so the %
has no special meaning yet; it is interpreted as a literal character in the two function references, $(dir %)
and $(call my_dir,%)
, both having ./
as a result, which get merged in the reference to $|
in the recipe.
除了您已经找到的其他解决方法,我不知道其他解决方法,即二次扩展.
I don't know of any other workaround than the one you already found, i.e. secondary expansion.
这篇关于如何在仅订购先决条件内调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!