Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make recipe execute twice

Tags:

makefile

Is there a way to execute a recipe more than once? In the SSCCE below it appears that the recipe is executed only once:

$ cat Makefile 
.PHONY: a b
a: b b 
b:
    echo "beta"
$ make 
echo "beta"
beta
like image 922
Marcus Junius Brutus Avatar asked Sep 03 '14 22:09

Marcus Junius Brutus


1 Answers

Once you've read and understood the comments ;-), there are two ways I can think of to run a recipe twice:

@OliCharlesworth mentions the first - use a loop inside your recipe:

.PHONY: a b
a: b b
b:
    for i in 1 2; do \
        echo "beta" ;\
    done

Note you need to be quite careful when embedding multi-line shell expressions in recipes. Unless you terminate lines with backslashes, make will treat each line as a separate shell invocation, which wouldn't work for a loop.

The other way is to duplicate your b target, so that both copies have the same recipe:

.PHONY: a b1 b2
a: b1 b2
b1 b2:
    echo "beta"

This defines the b1 and b2 targets with the same recipe. Then a depends on both b1 and b2, so the recipe gets called twice. Note that there is no guarantee of the order in which b1 and b2 will be invoked - and they'll likely be invoked concurrently if you have a -j factor greater than 1.

like image 118
Digital Trauma Avatar answered Nov 03 '22 19:11

Digital Trauma