在GNU makefile中使用$(filter)静态模式规则

时间:2016-01-23 08:56:26

标签: build makefile gnu-make

假设我有这个GNU makefile:

SOURCE = source1/a source2/b source3/c
BUILD  = build/a build/b build/c
BASE   = build/

$(BUILD): $(BASE)%: %
    install $< $@

所以基本上我在目录 source1 source2 source3 中都有文件,我想把它放在 build中目录。

我想用一个静态模式规则来完成这个任务。我天真的做法是:

$(BUILD): $(BASE)%: $(filter *%, $(SOURCE))
    install $< $@

这不起作用。我知道过滤器也用百分号表示。

$(BUILD): $(BASE)%: $(wildcard */$(notdir %))
    install $< $@

这也不起作用。但即便如此,这仍然不会令人满意,因为我可能想touch一个新文件source1 / b,这会弄乱一切。

如何在GNU makefile静态模式规则中使用$(filter)函数?或者还有另一种方法可以做到这一点吗?

层次结构现在看起来像这样:

source1/
        a
source2/
        b
source3/
        c
build/

我希望它看起来像这样:

source1/
        a
source2/
        b
source3/
        c
build/
        a
        b
        c

2 个答案:

答案 0 :(得分:2)

在阅读了一千个Stackoverflow问题和GNU make教程几个小时后,我终于开始工作了:

SOURCE = source1/a source2/b source3/c
TEST   = a b c
BUILD  = build/a build/b build/c
BASE   = build/
PERCENT = %

.SECONDEXPANSION:
$(BUILD): $(BASE)%: $$(filter $$(PERCENT)/$$*,$$(SOURCE))
    install $< $@

这有点像黑客,但我很高兴。如果有人有任何好主意,请告诉我。

答案 1 :(得分:2)

您可以使用the VPATH variable执行此操作:

SOURCE = source1/a source2/b source3/c
BUILD  = build/a build/b build/c
BASE   = build/

VPATH = source1 source2 source3

$(BUILD): $(BASE)%: %
    install $< $@

或者:

SOURCE = source1/a source2/b source3/c
BASE   = build/
BUILD  = $(addprefix $(BASE), $(notdir $(SOURCE)))

VPATH = $(dir $(SOURCE))

$(BUILD): $(BASE)%: %
    install $< $@