基于活动 makefile 目标的变量(外部规则)

Variable based on active makefile target (outside rule)

我有一个看起来像这样的构建配置:

生成文件:

build-for-x: X := yes
build-for-x: all

build-for-y: Y := yes
build-for-y: all

SRC :=
include foo.mk
include bar.mk

all: # ... some targets which use SRC eventually

foo.mk:

ifeq ($(X),yes)
    SRC += foo_x.c
endif
ifeq ($(Y),yes)
    SRC += foo_y.c
endif

bar.mk:

ifeq ($(X),yes)
    SRC += bar_x.c
endif
ifeq ($(Y),yes)
    SRC += bar_y.c
endif

这显然是高度简化的。我实际上在做的是为两个不同的嵌入式平台构建相同的代码库,其中有许多不同的模块化部分(这里是 foo 和 bar)和几个用于源、包含、测试等的变量。

以上不起作用,因为 X 或 Y 仅在 build-for-x/build-for-y 具有先决条件的目标中设置。 foo.mk/bar.mk 中的代码在两个变量都未设置时预先计算。

这种情况有什么好的处理方法吗?我可以将我的构建配置重组为:

我想避免让 foo.mk 看起来像:

build-for-x: SRC += foo_x.c
build-for-y: SRC += foo_y.c

除非我绝对必须这样做,因为这迫使我依赖于特定目标。我宁愿将所有与目标相关的分配都放在顶层。

这个怎么样:

生成文件:

build-for-x: SRC := SRC_X
build-for-x: all

build-for-y: SRC := SRC_Y
build-for-y: all

foo.mk:

SRC_X += foo.x_c
SRC_Y += foo.y_c

bar.mk:

SRC_X += bar.x_c
SRC_Y += bar.y_c