Search code examples
makefilegnu-make

Makefile order of dependencies


I was just handed a Makefile that has the following:

rpm: clean $(JARFILE)
    # commands here...

As you might have guessed, the clean target deletes the jar if it exists. Is this Makefile correct? Is there a guarantee that the clean target will be run before the JARFILE target? I'm not asking about style, but correctness. Is there a chance the JARFILE will be built but then just blown away by the clean target?

I'm using gnu make so that's what I'm most interested in; I'm not sure if this is intended to be portable to other flavors of make.


Solution

  • If you run make without any parallelism enabled (without -j) then your makefile will work properly. Make does guarantee that prerequisites are built in the order that they're listed in the makefile, if they are run serially.

    However if you enable -j then both targets will probably be run in parallel and then you could run into trouble.