如何在GNU Make模式规则中包含路径前缀 [英] How to include path prefix in GNU Make pattern rule
问题描述
请考虑以下内容:
%.foo: %.bar
echo $< > $@
假设我们只有一个文件1.bar
,执行的命令就是echo 1.bar > 1.foo
.但是,当%
包含路径而不是仅文件名时,它开始变得挑剔.我的问题是我想在%.bar
之前添加另一条路径,该模式会完全混乱.即,当%.bar
为nice/path/1.bar
时,这变得不可能:
Assuming we have one file 1.bar
, the command executed is simply echo 1.bar > 1.foo
. However, when %
contains a path, rather than just a file name, it start becoming finicky. My problem is that I want to prepend another path to %.bar
, the pattern becomes completely mangled. I.e., when %.bar
is nice/path/1.bar
, this becomes impossible:
%.foo: /project/something/%.bar
echo $< > $@
这将运行,但它将代替echo /project/something/nice/path1.bar > 1.foo
执行echo nice/path//project/something/1.bar > 1.foo
This will run, but it executes echo nice/path//project/something/1.bar > 1.foo
in stead of echo /project/something/nice/path1.bar > 1.foo
这样做的原因在于其模式规则的制作方式.从文档中:
The reason for this is in how make does its pattern rules. From the docs:
当目标模式不包含斜杠(通常不包含斜杠)时,在将文件名中的目录名与目标前缀和后缀进行比较之前,将从文件名中删除该目录名. [...]仅出于查找要使用的隐式规则的目的而忽略目录,而不是在该规则的应用程序中忽略目录.因此,"e%t"与文件名src/eat匹配,以"src/a"作为主干.将先决条件转换为文件名后,词干中的目录将添加到最前面,而词干中的其余部分将替换为%".带有必要模式"c%r"的词干"src/a"的文件名为src/car
When the target pattern does not contain a slash (and it usually does not), directory names in the file names are removed from the file name before it is compared with the target prefix and suffix. [...] The directories are ignored only for the purpose of finding an implicit rule to use, not in the application of that rule. Thus, ‘e%t’ matches the file name src/eat, with ‘src/a’ as the stem. When prerequisites are turned into file names, the directories from the stem are added at the front, while the rest of the stem is substituted for the ‘%’. The stem ‘src/a’ with a prerequisite pattern ‘c%r’ gives the file name src/car
有什么方法可以针对特定规则将其关闭吗?
Is there any way I can turn this off for a specific rule?