用于切换 Makefile 中目标的先决条件的变量

variable to switch the pre-requisites for a target in a Makefile

我希望使用 'MY_TYPE' 切换目标 'top' 的先决条件。

基于MY_TYPE我可以select满足先决条件

例如,对于

MY_TYPE=FOO ,我希望有 $(BUILD) $(TARGETS) $(X_LOCALS) 作为 top

的先决条件

MY_TYPE=BAR ,我希望将 $(BUILD) $(TARGETS) $(Y_LOCALS) 作为 top

的先决条件

如何实现?

下面是一个简单的代码片段。

BUILD = build
TARGETS = targets
XLOCALS = xlocals
YLOCALS = ylocals

# can be FOO or BAR
MY_TYPE ?= FOO

$(BUILD):
    printf "\t Doing Build\n"

$(TARGETS): 
    printf "\t Doing target\n"

$(XLOCALS):
    printf "\t my local build for X \n"

$(YLOCALS):
    printf "\t my local build for Y \n"


# based on MY_TYPE Can I select the pre-requisites required
# For example, for 
# MY_TYPE=FOO , I wish to have  $(BUILD) $(TARGETS) $(X_LOCALS) as pre-requisites for 'top' target
# MY_TYPE=BAR , I wish to have  $(BUILD) $(TARGETS) $(Y_LOCALS) as pre-requisites for 'top' target
# How can I achieve it .

top: $(BUILD) $(TARGETS) $(XLOCALS)
    printf "\t doing top\n"

很高兴接受你的想法。

再简单不过了,只需使用 conditionals:

ifeq ($(MY_TYPE),FOO)
top: $(X_LOCALS)
endif

ifeq ($(MY_TYPE),BAR)
top: $(Y_LOCALS)
endif

top: $(BUILD) $(TARGETS)
        @echo prereqs are $^