Alb*_*tti 17 directory recursion makefile subdirectory
如何make
在Makefile中make
命令命令在所有子目录命令(在子目录的Makefile中定义)中递归执行?
@ 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!"
Run Code Online (Sandbox Code Playgroud)
小智 5
我将在这里提交我的特定解决方案。假设我们有一个包含许多子目录的目录,所有子目录都有自己的 makefile:
root-dir\
+----subdir1
+----subdir2
...
+----subdirn
Run Code Online (Sandbox Code Playgroud)
然后,只需将这个 Makefile 复制到根目录中:
SUBDIRS = $(shell ls -d */)
all:
for dir in $(SUBDIRS) ; do \
make -C $$dir ; \
done
Run Code Online (Sandbox Code Playgroud)
当所有子目录都有 Makefile 时,上述答案很有效。只在包含 Makefile 的目录上运行 make 并不困难,也没有限制递归的级别数。在我的示例代码中,我将生成文件的搜索限制在父目录正下方的子目录中。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
Run Code Online (Sandbox Code Playgroud)