如何在GNU Make模式规则中包含路径前缀 [英] How to include path prefix in GNU Make pattern rule

查看:92
本文介绍了如何在GNU Make模式规则中包含路径前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

%.foo: %.bar
    echo $< > $@ 

假设我们只有一个文件1.bar,执行的命令就是echo 1.bar > 1.foo.但是,当%包含路径而不是仅文件名时,它开始变得挑剔.我的问题是我想在%.bar之前添加另一条路径,该模式会完全混乱.即,当%.barnice/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?

推荐答案

您可能想阅读

当目标模式不包含斜杠(通常不包含斜杠)时,在将文件名中的目录名与目标前缀和后缀进行比较之前,将从文件名中删除该目录名.在将文件名与目标模式进行比较之后,目录名称以及以它们结尾的斜杠将添加到根据模式规则的先决条件模式和文件名生成的先决条件文件名中.仅出于查找要使用的隐式规则的目的而忽略目录,而不是在该规则的应用程序中忽略目录.因此,"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. After the comparison of the file name to the target pattern, the directory names, along with the slash that ends them, are added on to the prerequisite file names generated from the pattern rule’s prerequisite patterns and the file name. 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.

以上内容解释了为什么nice/path/被置于前提条件/project/something/1.bar之前.

The above explains why nice/path/ is prepended to prerequisite /project/something/1.bar.

一种解决方法是在规则中使用完整文件名,例如:

One fix would be to use full file names in rules, e.g.:

${CURDIR}/nice/path/%.foo: /project/something/%.bar    

这篇关于如何在GNU Make模式规则中包含路径前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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