【问题标题】:Using $(filter) in GNU makefiles static pattern rules在 GNU makefile 静态模式规则中使用 $(filter)
【发布时间】:2016-01-23 08:56:25
【问题描述】:

假设我有这个 GNU makefile:

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

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

所以基本上我在 source1source2source3 目录中都有文件,我想将它们放入 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

【问题讨论】:

  • 如果你有source1/bsource2/b,那么你想在build/里面放什么?
  • 我想把abc放到build中。
  • 哪个 b你想放入build/?你说你可能想在source2/b之外介绍source/b,但你没有说你想让Make在这种情况下做什么。
  • 哦,对不起,我看错了你的答案!我想将source2/b 放入构建中,因为它位于$(SOURCE) 变量中。我认为这个问题不能在make中得到适当的解决,所以我会在make之外这样做。

标签: build makefile gnu-make


【解决方案1】:

您可以使用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 $< $@

【讨论】:

    【解决方案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 $< $@
    

    这有点老套,但我很满意。如果有人有更好的想法,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      相关资源
      最近更新 更多