如何通过通配子目录自动生成make目标列表?

发布于 2024-12-28 00:39:57 字数 715 浏览 0 评论 0原文

我想使用一个 Makefile 在数百个子目录中生成目标。每个子目录都是一个日期/时间戳,如下所示:20120119_153957,它与以下模式?????????_?????? 匹配。没有其他子目录与此模式匹配。

我想生成的一个目标名为 ??????_??????/graph.pdf。我有一个名为 make_graph 的脚本,它将根据给定的子目录名称生成图表。但我不确定如何编写一个 Makefile 来自动全局所有子目录并以编程方式生成这些目标。

例如,代码 SUBDIRS:=?????????_?????? 似乎正确地全局显示了所有子目录。我可以检查这个规则:

.PHONY: print
print:
        echo $(SUBDIRS)

但是这个变量分配

TARGETS:=$(SUBDIRS:%=%/graph.pdf)

似乎没有达到我的预期并分配了很多很多的目标。相反,以下规则仅打印一个目标。

.PHONY: print
print:
        echo $(TARGETS)

令人困惑的是,SUBDIRS 应该具有正确的子目录,但 TARGET 只有一个文件。

I would like to use a single Makefile to generate targets in hundreds of subdirectories. Each subdirectory is a date/time stamp like this: 20120119_153957, which matches the following pattern ????????_??????. There are no other subdirectories that match this pattern.

One target I would like to generate is called ????????_??????/graph.pdf. I have a script called make_graph that will make the graph given the subdirectory name. But I'm not sure how to write a Makefile that will automatically glob all of the subdirectores and generate these targets programmatically.

For example, the code SUBDIRS:=????????_?????? seems to correctly glob all of the subdirectories. I can check with this rule:

.PHONY: print
print:
        echo $(SUBDIRS)

However this variable assignment

TARGETS:=$(SUBDIRS:%=%/graph.pdf)

does not seem to do what I expect and assign lots and lots of targets. Instead the following rule just prints one target.

.PHONY: print
print:
        echo $(TARGETS)

It is very confusing that SUBDIRS should have the correct subdirectories but TARGET only has one file.

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

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

发布评论

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

评论(1

你好,陌生人 2025-01-04 00:39:57

在您的示例中,全局匹配由 shell 执行。

GNU Make 具有内置的 通配符 函数 ,您可以按如下方式使用它:

SUBDIRS := $(wildcard ????????_??????)

现在您可以使用此变量来构造目标列表:

.PHONY : all
all : $(SUBDIRS:%=%/graph.pdf)

%/graph.pdf : # list prerequisites here.
    # recipe to make '$@' in directory '$(@D)' from '$^'.

另请参阅: 模式规则, 自动变量

In your example glob matching is performed by the shell.

GNU Make has the built-in wildcard function, which you can use as follows:

SUBDIRS := $(wildcard ????????_??????)

Now you can use this variable to construct a list of targets:

.PHONY : all
all : $(SUBDIRS:%=%/graph.pdf)

%/graph.pdf : # list prerequisites here.
    # recipe to make '$@' in directory '$(@D)' from '$^'.

See also: pattern rules, automatic variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文