Gnu在模式规则中使用多个%? [英] Gnu make with multiple % in pattern rule?

查看:41
本文介绍了Gnu在模式规则中使用多个%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正确理解了make文档,那么我只能在目标模式中使用一次字符%!

If I understand the make documentation correctly I can only use character % once in my target-pattern!

我正面临着我的目标看起来像的情况:

I am facing a condition which my target is look like:

$(Home)/ecc _%_ 1st _%.gff:%.fpp

每当我尝试匹配/home/ecc_map_1st_map.gff:map.fpp 抱怨找不到模式.

And whenever I am trying to match /home/ecc_map_1st_map.gff: map.fpp Make complains that can't find a pattern.

我知道这是因为两次使用%( $(Home)/ecc_map_1st _%.gff:%.fpp 完美运行)

I know it is because of using % twice ($(Home)/ecc_map_1st_%.gff: %.fpp runes perfectly)

反正我可以在一个目标中使用2%!?

Is there anyway which I can use two % in one target!?

谢谢

推荐答案

您不能有两种模式.最好的方法是不要在目标名称中两次使用 map .

You can't have two patterns. The best way would be not having map twice in the target name.

如果必须,并且您正在使用GNU make,请二次扩展可能有用:

If you must, and you're using GNU make, secondary expansion might be useful:

.SECONDEXPANSION:
$(Home)/ecc_%.gff: $$(word 1,$$(subst _, ,$$*)).fpp

通常您不能使用自动变量,例如先决条件中的 $ * ,但是二级扩展使您可以做到这一点.请注意,我用 $$ 代替了 $ ,以便真正的扩展发生在第二遍而不是第一遍.

Normally you can't use automatic variables such as $* in prerequisites, but secondary expansion allows you to do just that. Note that I used $$ in place of $ so that the real expansion happens in the second pass and not in the first one.

假设您要制作 $(Home)/ecc_map_1st_map.gff . $ * 变量将成为词干,即 map_1st_map .然后 $(sub _,,...)用空格替换下划线,以获得 map 1st map .现在 $(word N,...)提取其中的第N个空格分隔的单词.第一个单词是第一个 map ,第二个单词是 1st ,第三个单词是第二个 map .我正在选择第一个.

Say you are making $(Home)/ecc_map_1st_map.gff. The $* variable is going to be the stem, i.e. map_1st_map. Then $(subst _, ,...) replaces underscores with spaces, to obtain map 1st map. Now $(word N,...) extracts the N-th space-separated word in there. First word would be the first map, second would be 1st, third would be the second map. I'm picking the first one.

当然,这很松懈,并不能保证您会完全匹配所需的目标( $(Home)/ecc_map_2nd_notmap__foo.gff 会匹配并给出 map.fpp ).您可以做进一步的检查(中间是 1st ,其他两个必须匹配)并纾困:

Of course this is pretty lax and doesn't guarantee you'll match exactly the targets you wanted ($(Home)/ecc_map_2nd_notmap__foo.gff would match and give map.fpp). You could do further checks (1st in the middle, other two must match) and bail out:

extract-name = $(call extract-name-helper,$1,$(word 1,$(subst _, ,$1)))
extract-name-helper = $(if $(filter $2_1st_$2,$1),$2,$(error Invalid .gff target))

.SECONDEXPANSION:
ecc_%.gff: $$(call extract-name,$$*).fpp

但这看起来并不明智.

最后,如果没有类似名称的文件,则第一个解决方案会很好地工作.另外,如果您事先知道目标名称,则可以使用明确的模式规则使其更安全(甚至可以动态生成目标名称).

Wrapping up, if don't have similarly named files, then the first solution would work well. Also, if you know the target names beforehand, you could make it safer with an explicit pattern rule (or even generate them on the fly).

这篇关于Gnu在模式规则中使用多个%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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