Makefile:生成所有子文件夹的zip文件 [英] Makefile: Generating zip files of all sub folders

查看:65
本文介绍了Makefile:生成所有子文件夹的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从临时构建脚本过渡过来,该脚本遍历项目家庭的所有子级,并为每个子级创建相应的zip文件:

I'm trying to transition away from my ad-hoc build script, which iterates over all children of the project's home, and creates a corresponding zip file for each:

for i in */; do
    if [[ -d $i ]]; then
        echo "Processing folder $i";
        zip "${i%/}.zip" "$i"*.txt
        echo "Created ${i%/}.zip"
    fi
done;

第一行很笨拙,但是它只是在所有子目录(不包括隐藏目录)中进行迭代.如您所见,逻辑很简单,但是在Makefile中执行此操作而不显式指定每个子目录似乎很困难.

The first line is clunky, but it's just iterating through all subdirs excluding hidden ones. As you can see the logic is straightforward, but doing this in a Makefile without explicitly specifying each subdirectory seems difficult.

理想情况下,我们应该从以下内容开始:

Ideally we should start with:

project
  subdir1
    file1.txt
    file2.txt
  subdir2
    file3.txt

...并最终得到:

project
  subdir1
    file1.txt
    file2.txt
  subdir1.zip
  subdir2
    file3.txt
  subdir2.zip

是否有一种方法可以安全地使这种递归失效?

Is there a way to safely get this sort of recursion out of make?

edit:感谢Etan

推荐答案

类似的事情可能会满足您的要求.

Something like this might do what you want.

SUBDIRS := $(wildcard */)
ZIPS := $(addsuffix .zip,$(subst /,,$(SUBDIRS)))

$(ZIPS) : %.zip : | %
    zip $@ $*/*.txt

dist: $(ZIPS)

您可能需要调整该静态模式规则等的先决条件,才能获得正确的重建行为(应该检测何时需要重建,是否应该始终重建,是否应该在.zip文件存在时从不重建)等).

You might need to adjust the prerequisites on that static pattern rule, etc. to get the correct rebuilding behavior (should make detect when it needs to rebuild, should it always rebuild, should it never rebuild when the .zip file exists, etc).

这篇关于Makefile:生成所有子文件夹的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆