'Makefile: always run prerequisite for a rule?

I'm wondering if there's a way for to have make always run some rule before another if it's a prerequisite.

For example:


setup:
    @echo "Setup"
    
rule1: setup
    @echo "Rule 1"

rule2: setup
    @echo "Rule 2"

Running make with the above Makefile, outputs:

Setup
Rule 1
Rule 2

But I want setup to be run before Rule 2 as well. With would produce an output like this:

Setup
Rule 1
Setup
Rule 2

Is calling make setup inside the command list my only option?



Solution 1:[1]

It's not possible to have a target built more than one time per invocation of make.

If you have a "preamble" you want to run before each rule then you can put it into a variable and add the variable into every recipe:

SETUP = @echo "Setup"

rule1:
        $(SETUP)
        @echo "Rule 1"

rule2:
        $(SETUP)
        @echo "Rule 2"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MadScientist