代码之家  ›  专栏  ›  技术社区  ›  Grumbel

如何在Makefile中复制目录?

  •  20
  • Grumbel  · 技术社区  · 15 年前

    images/ 我想抄送的 build/images/ 从Makefile中。该目录可能包含多个级别的子目录。最优雅的方法是什么?我想要:

    • 避免在每个 make 运行(即:否 cp -r
    • 保证一致性(即如果文件在 图像/ 构建/图像/
    • 避免为Makefile中的每个图像和每个子目录指定规则
    • 制作 ,所以没有 rsync cp -u 如果可能的话

    1 回复  |  直到 15 年前
        1
  •  33
  •   Community CDub    6 年前

    好吧,我只是用 rsync make 使用这些约束创建的脚本只会复制它的功能,而且很可能速度较慢,并且可能包含bug。示例规则如下所示:

    build/images:
        rsync -rupE images build/
    
    .PHONY: build/images
    

    ( .PHONY 每次触发规则)。

    也许可以使用符号链接或硬链接代替?

    build/images:
        ln -s ../images build/images
    

    如果你真的想避免 远程同步 和链接,这篇文章以某种方式重新实现了它们(未经测试,需要 find , mkdir ,和平原 cp ):

    image_files:=$(shell find images -type f)
    build/images/%: images/%
        mkdir -p $(@D)
        cp $< $@
    
    build: $(patsubst %,build/%,$(image_files))