user2532296
user2532296

Reputation: 848

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

I wish to use 'MY_TYPE' to switch the prerequisites for the target 'top'.

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

MY_TYPE=BAR , I wish to have $(BUILD) $(TARGETS) $(Y_LOCALS) as pre-requisites for top

How can I achieve it?

Below is a simple code snippet.

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"

Happy to take your thoughts.

Upvotes: 0

Views: 137

Answers (1)

Beta
Beta

Reputation: 99172

Nothing easier, just use conditionals:

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

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

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

Upvotes: 1

Related Questions