如何复制Makefile中的目录? [英] How to copy a directory in a Makefile?

查看:1365
本文介绍了如何复制Makefile中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录 images / ,我想从一个Makefile中复制到 build / images / 。该目录可能包含多个级别的子目录。这样做最优雅的方式是什么?我想要:




  • 避免每个 make 运行完整的目录副本 cp -r )

  • 保证一致性(即如果文件更改为 images / 应该自动更新 build / images /

  • 避免为每个图像和每个子目录中的每个子目录指定规则Makefile

  • 解决 make 中的问题,所以没有 rsync cp -u 如果可能



我使用GNU make,所以GNU具体的东西是

解决方案

嗯,我只是使用 rsync 。您将使用这些约束创建的任何 make 脚本将只会复制其功能,并且可能会更慢,并可能包含错误。一个示例规则可能是:

  build / images:
rsync -rupE images build /

.PHONY:build / images

(。PHONY每次触发规则) p>

可以使用符号链接或硬链接吗?

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

如果你真的想避免 rsync 和链接,这一块以某种方式重新实现它们(未测试,需要查找 mkdir and plain cp ):

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

build:$(patsubst%,build /%,image_files)


I have a directory images/ that I want to copy to build/images/ from within a Makefile. The directory might contain multiple levels of subdirectories. What would be the most elegant way to do that? I want:

  • avoid a full directory copy on each make run (i.e. no cp -r)
  • guaranteed consistency (i.e. if a file changed in images/ it should be automatically updated in build/images/)
  • avoid to specify a rule for each image and each subdirectory in the Makefile
  • solve the issue within make, so no rsync or cp -u if possible

I am using GNU make, so GNU specific stuff is allowed.

解决方案

Well, I'd just use rsync. Any make script you will create with these constraints will just replicate its functionality, and most probably will be slower and may contain bugs. An example rule might look:

build/images:
    rsync -rupE images build/

.PHONY: build/images

(.PHONY to trigger the rule every time).

Maybe symlinks or hardlinks can be used instead?

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

If you really want to avoid rsync and links, this piece reimplements them somehow (not tested, needs find, mkdir and plain cp):

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

build: $(patsubst %,build/%,image_files)

这篇关于如何复制Makefile中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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