可变变量以更改Makefile中目标的先决条件

问题描述 投票:0回答:1

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

基于MY_TYPE,我可以选择所需的先决条件

例如,对于

MY_TYPE = FOO,我希望以$(BUILD)$(TARGETS)$(X_LOCALS)作为页首的先决条件

MY_TYPE = BAR,我希望以$(BUILD)$(TARGETS)$(Y_LOCALS)作为页首的先决条件

如何实现?

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

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"

很高兴考虑一下。

linux makefile gnu-make
1个回答
0
投票

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

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

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

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

EDIT:完整示例:

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

# can be FOO or BAR                                                                                                       
MY_TYPE ?= BAR

$(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"

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

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

top: $(BUILD) $(TARGETS)
    printf "\t doing top\n"
© www.soinside.com 2019 - 2024. All rights reserved.