一个Makefile,在多个子目录中使用

发布于 2024-10-17 23:17:26 字数 449 浏览 9 评论 0原文

我正在寻找编写一个单独的 makefile,它在当前目录的所有子目录中调用它本身的目标。我以此为出发点:

SUBDIRS := $(shell find . -maxdepth 1 -type d | sed -e 's/^/\"/;s/$$/\"/')

all:
        @for d in $(SUBDIRS); do \
                ($(MAKE) -f "../$(lastword $(MAKEFILE_LIST))" -C "$$d" gallery.html); \
        done

gallery.html:
        echo "Making gallery\n";
  1. 这是解决问题的正确方法吗?
  2. 这真的是发现目录并正确引用它们的“最干净”的方法吗?

我很高兴使用 GNU make 扩展。

I'm looking to write a single makefile that calls a target in it itself in all the subdirectories of the current directory. I've come up with this as a starting point:

SUBDIRS := $(shell find . -maxdepth 1 -type d | sed -e 's/^/\"/;s/$/\"/')

all:
        @for d in $(SUBDIRS); do \
                ($(MAKE) -f "../$(lastword $(MAKEFILE_LIST))" -C "$d" gallery.html); \
        done

gallery.html:
        echo "Making gallery\n";
  1. Is this the right approach to the problem?
  2. Is that really the "cleanest" way to discover diretories and quote them correctly?

I'm happy enough using GNU make extensions.

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

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

发布评论

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

评论(2

尘曦 2024-10-24 23:17:26

你所拥有的本质上是一个混淆的 shell 脚本——你没有使用 make 本身的任何功能。

您的子目录名称是否包含空格字符?如果是这样,那么 make 可能根本不是适合这项工作的工具:它确实想要处理以空格分隔的单词列表,并且没有引用的概念可以帮助它处理空格路径。

如果子目录名称包含空格字符,我会将其拆分为单独的 shell 脚本和 makefile,如下所示:

#/bin/sh
find . -maxdepth 1 -type d | while read d; do
    make -f `dirname $0`/Makefile -C "$d" gallery.html
done

并且

gallery.html:
        echo "Making gallery\n";

(请记住,如果子目录中有更多带有空格的路径,make 仍然可能是一个糟糕的选择)

如果子目录名称不包含空格字符,那么事情可以变得更简单,并且您还可以避免递归 make 调用(谷歌“递归 make 被认为是有害的”原因):

SUBDIRS := $(wildcard */)

all: $(SUBDIRS:%=%/gallery.html)

%/gallery.html:
    cd $* && echo "Making gallery in `pwd`\n"

What you have there is essentially an obfuscated shell script -- you're not using any features of make itself.

Do your subdirectory names contain space characters? If so, then make is perhaps simply not the right tool for the job: It really wants to work with space-separated lists of words, and has no concept of quoting that could help it deal with spaces in paths.

If the subdirectory names contain space characters, I would split things up into a separate shell script and makefile, like so:

#/bin/sh
find . -maxdepth 1 -type d | while read d; do
    make -f `dirname $0`/Makefile -C "$d" gallery.html
done

and

gallery.html:
        echo "Making gallery\n";

(keeping in mind that, if there are more paths with spaces inside your subdirectories, make could still be a poor choice)

If the subdirectory names do not contain space characters, then things can be made much simpler, and you can also avoid the recursive make invocation (google "recursive make considered harmful" for reasons why):

SUBDIRS := $(wildcard */)

all: $(SUBDIRS:%=%/gallery.html)

%/gallery.html:
    cd $* && echo "Making gallery in `pwd`\n"
我不吻晚风 2024-10-24 23:17:26

仅当 gallery.html 具有一些复杂的本地先决条件时,这种递归才有意义,即使如此,它也可能不是最好的方法。这种对 makefile 名称的敏感性是一个非常糟糕的迹象。但如果这就是您想要的,我建议进行以下更改:

.PHONY: $(SUBDIRS)
all: $(SUBDIRS)

$(SUBDIRS):
    $(MAKE) -f "../$(lastword $(MAKEFILE_LIST))" -C $@ gallery.html

This kind of recursion makes sense only if gallery.html has some complicated local prerequisites, and even then it probably isn't the best way to go. And this sensitivity to the name of the makefile is a very bad sign. But if that's what you want, I suggest the following change:

.PHONY: $(SUBDIRS)
all: $(SUBDIRS)

$(SUBDIRS):
    $(MAKE) -f "../$(lastword $(MAKEFILE_LIST))" -C $@ gallery.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文