Makefile:修改模式规则中的词干 [英] Makefile: modifying stem in pattern rule

查看:61
本文介绍了Makefile:修改模式规则中的词干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为 data helpers 的目录中有文件,我想同时使用它们在 result 中创建目标文件.

I have files in directories called data and helpers, and I would like to use both of them to create target files in result.

目录结构如下:

data
 + A
 | + file1
 | + file2
 | + ...
 + B
 | + file1
 ...
helpers
 + file1
 + file2
 ...

结果结果中的目录结构与 data 中的目录结构相同.因此,我想编写以下规则:

The directory structure in result is the same as in data. Therefore, I would like to write the following rule:

result/%: data/% helpers/% script
    script $@ $(word 1,$^) $(word 2,$^)

此问题是 helpers 目录中的 file1 file2 等不在子目录中.

The problem with this is that file1, file2, etc. in the helpers directory are not in the subdirectory.

我现在有以下选择:

  • 为每个子目录写一个单独的规则(不切实际,有20条)
  • 使用 foreach 包含每个子目录来创建规则(然后如何访问先决条件列表?)
  • write a separate rule for every subdirectory (impractical, there are 20)
  • do a foreach including every subdirectory to create the rule (how can I access the prerequisite list then?)

但是,我宁愿编写以下内容之一:

However, I would much rather write one of the following:

result/%: data/% helpers/$(basename %) # this doesn't work
result/%: data/% helpers/$(basename $*) # this doesn't work either

是否可以通过修改匹配的词干来修改规则声明中的模式?

Is there any way to modify a pattern in the rule declaration by modifying the stem it matches?

推荐答案

执行所需操作的唯一方法是启用

The only way to do what you want is to enable Secondary Expansion in your makefile.

示例:

请注意,如隐式规则搜索算法中所述,目录前缀 (D) 已附加(扩展后)到先决条件列表中的所有模式.举个例子:

Note that the directory prefix (D), as described in Implicit Rule Search Algorithm, is appended (after expansion) to all the patterns in the prerequisites list. As an example:

.SECONDEXPANSION:

.SECONDEXPANSION:

/tmp/foo.o:

/tmp/foo.o:

%.o: $$(addsuffix /%.c,foo bar) foo.h
        @echo $^

在二次扩展和目录前缀重建之后,打印的先决条件列表将是/tmp/foo/foo.c/tmp/bar/foo.cfoo.h.如果您对此重构不感兴趣,则可以使用前提条件列表中的$$ *代替%.

The prerequisite list printed, after the secondary expansion and directory prefix reconstruction, will be /tmp/foo/foo.c /tmp/bar/foo.c foo.h. If you are not interested in this reconstruction, you can use $$* instead of % in the prerequisites list.

所以这样的事情应该起作用(未经测试):

So something like this should work (untested):

.SECONDEXPANSION:
result/%: data/% helpers/$$(notdir %)

make的 basename 与shell的 basename 不同(不断会绊倒我).

make's basename is not the same as the shell's basename (that constantly trips me up).

如果您想使用 $(foreach),但您希望使用类似(unested)的内容:

If you wanted to use $(foreach) though you would want something like (untested):

define dirrule
result/$1/%: data/$1/% helpers/%
        @echo cmd 1
        @echo cmd 2
endef

$(foreach dir,subdir1 subdir2 ... subdir20,$(eval $(call dirrule,$(dir))))

这篇关于Makefile:修改模式规则中的词干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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