现在抓我的头一会儿,但我想复制一个任意文件列表,其中包含指向系统中指定路径下的路径。
文件布局:
data/a/file1.ext1
data/b/randomfile.ext2
data/c/file3.ext3
data/c/subdir/randomfile.2
运行make -f Makefile deploy DESTDIR=/path/to/somewhere
将这些文件复制到:
$(DESTDIR)/a/file1.ext1
$(DESTDIR)/b/randomfile.ext2
$(DESTDIR)/c/file3.ext3
$(DESTDIR)/c/subdir/randomfile.2
Makefile
:
$FILES = \
a/file1.ext1 \
b/randomfile.ext2 \
c/file3.ext3 \
c/subdir/randomfile.2
ifneq ($(filter env_check,$(MAKECMDGOALS)),$())
ifndef DESTDIR
$(error DESTDIR not defined)
endif
endif
# lots of currently broken rules :(
# check whether target directory has certain structure
# check whether all the files listed in $(FILES) are in repository
答案 0 :(得分:1)
你在找这样的东西吗?
FILES := ...
DST_FILES := $(addprefix $(DESTDIR)/,$(FILES))
ifneq ($(filter env_check,$(MAKECMDGOALS)),$())
ifndef DESTDIR
$(error DESTDIR not defined)
endif
endif
all: $(DST_FILES)
$(DST_FILES) : ${DESTDIR}/% : %
@echo "$< ==> $@"
@[[ -e $< ]] || (echo "some error for $<" && false)
@mkdir -p $(dir $@)
@cp $< $@
<强> [编辑] 强>: 虽然版本有些工作,但我仍然需要做以下调整:
data
目录下 - 使用$addprefix
调用$(DESTDIR)
中的文件已经存在时,它从未被复制 - 使用.FORCE
目标。 (另一种选择是--always-make
命令行选项)。最终,正在运行的Makefile看起来像这样:
# File are stored under data/
FILES= \
foo/file1.ext \
bar/file2.txe \
bar/dir/file3.txt
ifneq ($(filter env_check,$(MAKECMDGOALS)),$())
ifndef DESTDIR
$(error DESTDIR not defined)
endif
endif
.PHONY: deploy help
help:
@echo "Deploy stuff"
# Check whether certain directories in the output are present
env_check:
@test -d $(DESTDIR)/WEB-INF -a -d $(DESTDIR)/META-INF || \
( echo "DESTDIR: \"$(DESTDIR)\" is not proper deployment path" && exit 1 )
DST_FILES := $(addprefix $(DESTDIR)/, $(FILES))
# We need to add our path prefix to local files and FORCE to always do the copying
$(DST_FILES) : $(addprefix $(DESTDIR), %) : $(addprefix data,%) .FORCE
@cp -pv $< $@
.FORCE:
deploy: env_check $(DST_FILES)
@echo "Deployment done..."