Gui13
Gui13

Reputation: 13551

Makefile: rule that match multiple patterns

I have this rule in my Makefile, that responds to flags I pass:

$(BUILD_DIR)/disable_%:
    mkdir -p $(BUILD_DIR)
    touch $(BUILD_DIR)/disable_$*
    rm -f $(BUILD_DIR)/enable_$*
    cd $(BUILD_DIR) && rm -f Makefile   

$(BUILD_DIR)/enable_%:
    mkdir -p $(BUILD_DIR)
    touch $(BUILD_DIR)/enable_$*
    rm -f $(BUILD_DIR)/disable_$*
    cd $(BUILD_DIR) && rm -f Makefile

What this means is that when changing the flags by which I invoke the makefile, I can trigger some recompilations that could depend on these flags.
The code presented above is a bit redundant: you see that I remove a file, touch another and remove a Makefile in both cases. The only thing that changes is the name of the files that I touch/remove, and they are related.

For instance,

make clean
make enable_debug=yes enable_video=no # will compile from zero
make enable_debug=no enable_video=no  # flag change detected -> recompile some submodules that depend on this flag

Provided that the only thing that changes between the two rules ( [en|dis]able ), what I would like is to only have 1 generic rule, something like that:

# match 2 parts in the rule
$(BUILD_DIR)/%ble_%: 
    mkdir -p $(BUILD_DIR)
    touch $(BUILD_DIR)/(???)ble_$*  # should be $@
    rm -f $(BUILD_DIR)/(???)able_$* # should be disable if $@ is enable and inverse
    cd $(BUILD_DIR) && rm -f Makefile

Is this possible ?

PS: Sorry if I didn't get the title correctly, I couldn't figure how to explain it better.

Upvotes: 1

Views: 1398

Answers (1)

keltar
keltar

Reputation: 18399

$(BUILD_DIR)/enable_% $(BUILD_DIR)/disable_%:
    mkdir -p $(BUILD_DIR)
    rm -f $(BUILD_DIR)/*able_$*
    touch $@
    cd $(BUILD_DIR) && rm -f Makefile

Not literally what you wanted (multi-wildcards are forbidden in make), but does quite the same.

Upvotes: 1

Related Questions