【发布时间】:2012-03-04 05:16:09
【问题描述】:
如何在 Makefile 中命令 make 命令在所有子目录中递归执行 make 命令(在子目录中的 Makefile 中定义)?
【问题讨论】:
标签: recursion directory makefile subdirectory
如何在 Makefile 中命令 make 命令在所有子目录中递归执行 make 命令(在子目录中的 Makefile 中定义)?
【问题讨论】:
标签: recursion directory makefile subdirectory
附:从my answer 到一个不同但相关的问题的代码 sn-p 可以用作粗略的近似值。
【讨论】:
@eldar-abusalimov,您发布的第一个链接假定 makefile 知道子文件夹是什么。这并不总是正确的,我想这就是@tyranitar 想知道的。在这种情况下,这样的解决方案可以完成这项工作: (花了我一些时间,但我也需要它)
SHELL=/bin/bash
all:
@for a in $$(ls); do \
if [ -d $$a ]; then \
echo "processing folder $$a"; \
$(MAKE) -C $$a; \
fi; \
done;
@echo "Done!"
【讨论】:
SUBDIRS := $(wildcard */.)。然后遍历SUBDIRS 并调用子制作。
我将在这里提交我的特定解决方案。 假设我们有一个包含许多子目录的目录,它们都有自己的 makefile:
root-dir\
+----subdir1
+----subdir2
...
+----subdirn
然后,只需将这个 Makefile 复制到根目录中:
SUBDIRS = $(shell ls -d */)
all:
for dir in $(SUBDIRS) ; do \
make -C $$dir ; \
done
【讨论】:
当所有子目录都有 Makefile 时,上述答案效果很好。只在包含 Makefile 的目录上运行 make 并不难,限制递归的级别数也不难。在我的示例代码中,我将 makefile 的搜索限制在父目录正下方的子目录中。 filter-out 语句(第 2 行)阻止此 Makefile 包含在递归 make 中。
MAKEFILES = $(shell find . -maxdepth 2 -type f -name Makefile)
SUBDIRS = $(filter-out ./,$(dir $(MAKEFILES)))
all:
for dir in $(SUBDIRS); do \
make -C $$dir all; \
done
【讨论】:
-mindepth 选项过滤掉根 Makefile。
MAKEFILES 作为变量,make 将解析所有这些文件并抱怨它们是否有重复的目标。如果这不是您想要的,您应该使用不同的变量名。
Makefile:24: warning: overriding recipe for target