Makefile:“优化”的 makefile 中“$

Makefile:“优化”的 makefile 中“$

我有一个这样的目录结构:

./
|
|- values-1/
|  |- thing0.yaml
|
|- values-2/
|  |- thing1.yaml
|
|- dachart-1/
|  |- thing0.yaml   (this should be generated by make
|                    from values-1/thing0.yaml)
|- dachart-2/
|  |- thing1.yaml   (this should be generated by make
                     from values-2/thing1.yaml)

“构建规则”基本相同,但长约 30 行。

这个非常简单的 Makefile 说明了它的工作原理:

# WORKING makefile, but with two redundant build rules

.SOURCES:

SHELL=bash

src1_files := $(wildcard values-1/*.yaml)
src2_files := $(wildcard values-2/*.yaml)
dst1_files := $(patsubst values-%,dachart-%,$(src1_files))
dst2_files := $(patsubst values-%,dachart-%,$(src2_files))

all: $(dst2_files) $(dst1_files)

dachart-1/%.yaml: values-1/%.yaml
    # SIMPLIFIED EXAMPLES
    @echo "source $<"
    @echo "target $@"

dachart-2/%.yaml: values-2/%.yaml
    @echo "source $<"
    @echo "target $@"

但由于构建规则非常长且完全相同,因此我希望为所有文件建立规则定义。

经过一番折腾,我写出了一个 Makefile,似乎做我想做的事(并且没有出错),可惜扩展$<不再起作用(见下面的输出)。

# NON-working makefile, but kinda what I want (only one build rule)

dachart-1/%.yaml: $(src1_files)
dachart-2/%.yaml: $(src2_files)

%.yaml:
    @echo "source $<"
    @echo "target $@"

输出为:

# working makefile
source values-1/thing0.yaml
target dachart-1/thing0.yaml

# alternate makefile
source
target dachart-1/thing0.yaml

问题:有没有办法按照我的无法正常工作的 Makefile 的意义来构建规则?

答案1

好吧,令人惊讶的是,这是有效的:

.SOURCES:

SHELL=bash

src1_files := $(wildcard values-0.1/*.yaml)
src2_files := $(wildcard values-2.0/*.yaml)
dst1_files := $(patsubst values-%,dachart-%,$(src1_files))
dst2_files := $(patsubst values-%,dachart-%,$(src2_files))

all: $(dst2_files) $(dst1_files)

fresh: clean all
.PHONY: fresh

dachart-0.1/%.yaml: THING=one
dachart-0.1/%.yaml: $(src1_files)

dachart-2.0/%.yaml: THING=two
dachart-2.0/%.yaml: $(src2_files)


dachart-*/%.yaml: values-*/%.yaml
    @echo THING=${THING}
    @echo "source $<"
    @echo "target $@"

相关内容