一个Makefile,在多个子目录中使用
我正在寻找编写一个单独的 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";
- 这是解决问题的正确方法吗?
- 这真的是发现目录并正确引用它们的“最干净”的方法吗?
我很高兴使用 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";
- Is this the right approach to the problem?
- Is that really the "cleanest" way to discover diretories and quote them correctly?
I'm happy enough using GNU make extensions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你所拥有的本质上是一个混淆的 shell 脚本——你没有使用 make 本身的任何功能。
您的子目录名称是否包含空格字符?如果是这样,那么 make 可能根本不是适合这项工作的工具:它确实想要处理以空格分隔的单词列表,并且没有引用的概念可以帮助它处理空格路径。
如果子目录名称包含空格字符,我会将其拆分为单独的 shell 脚本和 makefile,如下所示:
并且
(请记住,如果子目录中有更多带有空格的路径,make 仍然可能是一个糟糕的选择)
如果子目录名称不包含空格字符,那么事情可以变得更简单,并且您还可以避免递归 make 调用(谷歌“递归 make 被认为是有害的”原因):
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:
and
(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):
仅当
gallery.html
具有一些复杂的本地先决条件时,这种递归才有意义,即使如此,它也可能不是最好的方法。这种对 makefile 名称的敏感性是一个非常糟糕的迹象。但如果这就是您想要的,我建议进行以下更改: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: