Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutex in GNU Make?

Two targets are satisfied by one rule like this:

foo.o foo.h :
    build_foo # this makes both foo.o and foo.h

Later, some targets need one, others need both:

bar : foo.o
    build_bar

baz : foo.h
    build_baz

qux : bar baz
    build qux

How do I prevent the recipe from running twice when make is invoked with -jN?

like image 397
Jacob Marble Avatar asked Feb 12 '26 22:02

Jacob Marble


1 Answers

I avoid pattern rules when I can. In your case you will find that after the recipe is run, one of foo.o and foo.h is newer than the other (just use $ ls --full-time on them).

Let's assume your recipe creates foo.h and then goes on to create foo.o. That suggests:

foo.o: foo.h ; # Empty recipe

foo.h: prereq1 prereq2 ...
    build_foo

So now if foo.o is a prequisite in another rule, make will build foo.h first, creating foo.o as a side-effect. Then it will run foo.o's recipe. Job done.

If you feel uneasy, you could add a simple assert that foo.o is indeed newer than foo.h

foo.o: foo.h
    test $@ -nt $<

For more about this topic, check out the automake (not make) chapter titled Handling Tools that Produce Many Outputs

like image 81
bobbogo Avatar answered Feb 15 '26 18:02

bobbogo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!