Skip to content
Advertisement

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 Buildn"

$(TARGETS): 
    printf "t Doing targetn"

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

Happy to take your thoughts.

Advertisement

Answer

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 $^
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement