按文件名划分 make 中的输出

dividing outputs in make by filename

我正在处理一些文件,并希望在某一时刻根据文件名创建两个类别,以便我可以比较两者。这在 makefile 中可能吗?

%.output1: %.input
    ifneq (,$(findstring filename1,$(echo $<)))
        mv $<.output1 $@
    endif

%.output2: %.input
    ifneq (,$(findstring filename2,$(echo $<)))
        mv $<.output2 $@
    endif

%.output_final: %.output1 %.output2
    do_something

我认为这段代码有两处错误:

  1. ifneq 行有错误。
  2. %.output1 %.output2 将始终使用相同的文件名 - 在 'make' 中可能无法做到这一点,这可能需要 ruffus。

您在 ifneq 行进行了制表符缩进,因此 make 不认为它是 make 指令,而是将其视为 shell 命令并试图将其传递给 shell执行(因此您在最近的编辑中删除了 shell 错误)。

在该行上使用空格(或不缩进)使 make 能够正确处理它。话虽这么说,但您不能在比较中使用 $<,因为此时不会设置它。

$(echo) 也不是 make 函数。您有 mixed/confused 处理时间。您不能以这种方式组合 make 和 shell 操作。 (并不是说你需要 echo 开始。)

如果您希望在 shell 时进行比较,请不要使用 make 构造,而是使用 shell 构造:

%.output1: %.input
    if [ filename1 = '$<' ]; then
        mv $<.output1 $@
    fi

虽然这也是不正确的,因为 $<%.input 并且 $@%.output1 对于匹配 % 的任何词干。该规则可能看起来更像这样(尽管我无法理解您甚至试图让该规则执行的操作,所以我可能弄错了)。

%.output1: %.input
    # If the stem that matched the '%' is equal to 'filename1'
    if [ filename1 = '$*' ]; then
        # Then copy the prerequisite/input file to the output file name.
        cp $< $@
    fi

我不确定我是否理解你的第二个问题点。单个规则中的 % 将始终匹配相同的内容,但规则之间可能会有所不同。

%.output_final: %.output1 %.output2 目标会将目标文件 foo.output_final 映射到先决条件文件 foo.output1foo.output2。但也会将任何其他 *.output_final 文件映射到适当匹配的先决条件文件。