【问题标题】:function call as target in implicit rules函数调用作为隐式规则中的目标
【发布时间】:2016-10-03 08:20:26
【问题描述】:

在我的 Makefile 中,我有一个生成多个文件的配方。像这样的:

foo-% bar-%: foobar-%
    grep "foo" $^ > foo-$*
    grep "bar" $^ > bar-$*

按预期工作:

$ make foo-lol
grep "foo" foobar-lol > foo-lol
grep "bar" foobar-lol > bar-lol

在我的真实案例中,目标和先决条件文件路径要复杂得多(见最后)并在其他地方使用,所以我将逻辑放在这样的函数中:

target_names = foo-$(1) bar-$(1)

并尝试在我的食谱中使用它

$(call target_names,%): foobar-%
    grep "foo" $^ > foo-$*
    grep "bar" $^ > bar-$*

令我惊讶的是,这也有效:

$ make foo-lol
grep "foo" foobar-lol > foo-lol
grep "bar" foobar-lol > bar-lol

我不明白它为什么起作用。 Make如何找到这个食谱?我一直认为 Make 会遍历所有食谱中的目标列表。但如果是这样,那么当函数调用而不是目标文件名时,它背后的逻辑是什么? Make 如何为返回所需目标的函数调用找到正确的输入?


正如我所提到的,我的真实示例相当复杂,有 8 个目标和 2 个先决条件:

target_names = \
  $(foreach tissue, $(TISSUE), \
    $(foreach chain, $(CHAIN), \
      $(foreach direction, R1 R2, \
        $(foreach read, $(filter %_$(direction)_001.fastq, $(1)), \
          $(patsubst data/%_$(direction)_001.fastq, \
                     results/%-$(tissue)-$(chain)-$(direction).fastq, \
                     $(read))))))

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    函数调用被处理之前规则被添加到规则列表中,你可以通过运行make -Rpn来验证这一点。

    $(call target_names,%)

    -> 替换foo-$(1) bar-$(1),其中$1 == %

    -> 返回foo-% bar-%

    -> foo-% bar-%: foobar-% 被添加到规则列表中

    【讨论】:

      猜你喜欢
      • 2012-01-03
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2015-05-09
      相关资源
      最近更新 更多