GNU可以使用模式匹配来查找变量吗?

发布于 2025-01-30 20:06:50 字数 895 浏览 3 评论 0 原文

我正在尝试建立一些数据分析,其中有一个由一个总参数控制的文件列表。

明确编写它是类似的:

A_EXTS = a b c d e
B_EXTS = f g h i j
C_EXTS = k l m n o

A.dat : $(foreach EXT, ${A_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

B.dat : $(foreach EXT, ${B_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

C.dat : $(foreach EXT, ${C_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

显然,这三个规则之间的唯一区别是 a vs b vs vs c

我想尝试类似的东西

%.dat : $(foreach EXT, ${%_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

……但是那行不通。例如使B.Dat 运行 b.dat 的规则,但忽略了依赖项; $^设置为空字符串。

启动 prefix2 _ 是由另一个食谱生成的,因此我不能只在配方中指定它们,需要在此处标记为依赖项。

这可以在不重复相同规则的情况下表达这些依赖性吗?

I'm trying to get Make to build some data analysis, where there are file lists controlled by one overall parameter.

To write it explicitly would be something like:

A_EXTS = a b c d e
B_EXTS = f g h i j
C_EXTS = k l m n o

A.dat : $(foreach EXT, ${A_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

B.dat : $(foreach EXT, ${B_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

C.dat : $(foreach EXT, ${C_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

Obviously the only difference between the three rules is the A vs B vs C.

I thought to try something like

%.dat : $(foreach EXT, ${%_EXTS}, prefix1_${EXT}.dat prefix2_${EXT}.dat)
    python analyse.py $^ > $@

…but that doesn't work; e.g. make B.dat runs the rule for B.dat but ignores the dependencies; $^ is set to the empty string.

The files starting prefix2_ are generated by another recipe, so I can't just specify them within the recipe, they need to be marked as dependencies here.

Is this possible to express these dependencies without repeating the same rule?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2025-02-06 20:06:50

好吧,您不能像在这里那样做到这一点,但它与查找变量名称无关:这是因为扩展顺序。

当解析MakeFile时,目标和先决条件中的变量会扩展,但是Make尚未扩展模式规则中的模式,直到很久以后。这意味着,当制作扩展 $ {%_ exts} 变量时,它可以解析MakeFile时,它不知道>%的价值将在稍后实际上试图构建时会发生什么事物。

您可以使用 secondary Expansion 延迟变量扩张的扩展直到将其实际找到目标名称进行第二次通过。我将逻辑拉到一个单独的变量中,并使用 使其更具可读性:

.SECONDEXPANSION:

EXPANDDEPS = $(foreach EXT,${$1_EXTS},prefix1_${EXT}.dat prefix2_${EXT}.dat)

%.dat : $(call EXPANDDEPS,$*)
        python analyse.py $^ > $@

Well, you can't do it quite like you want to here, but it's not related to looking up variable names: it's because of expansion order.

Variables in targets and prerequisites are expanded when the makefile is parsed, but make doesn't expand the patterns in pattern rules until much later. That means when make expands the ${%_EXTS} variable as it parses the makefile, it has no idea what the value of % will be later when it's actually trying to build things.

You can use secondary expansion to delay expansion of variables until make's second pass where it is actually finding target names. I pulled the logic out into a separate variable and used call to make it a bit more readable:

.SECONDEXPANSION:

EXPANDDEPS = $(foreach EXT,${$1_EXTS},prefix1_${EXT}.dat prefix2_${EXT}.dat)

%.dat : $(call EXPANDDEPS,$*)
        python analyse.py $^ > $@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文