0

我正在使用类似于此处介绍的非递归 make 的实现:http: //evbergen.home.xs4all.nl/nonrecursive-make.html

这是问题的一个例子。

主要Makefile包括foo/Rules.mk. foo/Rules.mk包含片段:

# Here, d is bound to foo, the path to the current directory
$(d)/foo.zip: $(d)/bar
    zip -r $@ $^
    # This expands to the recipe: zip -r foo/foo.zip foo/bar

不幸的是,这会创建一个包含 的 zip 存档foo/bar,但我需要它包含bar,也就是说,使存档相对于给定目录。cd不起作用。

# DOES NOT WORK
$(d)/foo.zip: d := $(d)  # this makes the variable d work in the recipe
$(d)/foo.zip: $(d)/bar
    cd $(d); zip -r $@ $^
    # This expands to the recipe: cd foo; zip -r foo/foo.zip foo/bar

在一般情况下如何使这项工作(d 可以是任何路径,zip 包含任意选择的文件和子目录)?

4

2 回答 2

0

我想出了以下hack,请编程大神原谅我。

x := $(d)/foo.zip  # targets
y := $(d)/bar  # prerequisites
$(x): x := $(x)
$(x): y := $(y)
$(x): d := $(d)
$(x): $(y)
    cd $(d); zip -r $(x:$(d)/%=%) $(y:$(d)/%=%)
    # Expands to cd foo; zip -r foo.zip bar
于 2015-06-22T07:19:12.613 回答
0

就这个?

$(d)/foo.zip: $(d)/bar
    zip -r $(@:$(d)/%=%) $(<:$(d)/%=%) # Expands to zip -r foo.zip bar
于 2015-06-22T12:36:56.607 回答