'Makefiles: What is an order-only prerequisite?

The GNU make manual introduces order-only prerequisites:

target: prerequisite1 prerequisite2 | orderonlyprerequisite1 orderonlyprerequisite2

However, while that manual page gives an example and describes the purpose of order-only prerequisites, at no point does the manual page answer the question in the title: What is an order-only prerequisite?

Can you give a precise definition how order-only prerequisites behave?



Solution 1:[1]

First, let's recall what the ordinary prerequisites are.

Basically, the prerequisites in Makefile have two functions:

  1. They are checked and, if necessary, are built before the target
  2. If any of the prerequisites gets rebuilt (or is simply newer than the target) then the target will also be rebuilt.

Now, the order-only prerequisites do item 1, i.e. impose build-order, but not item 2.

Solution 2:[2]

Suppose you have a Makefile like this

baby: love
    touch $@

love: 
    touch $@

then make says,

touch love
touch baby

now you rm love and again make say

touch love
touch baby

because in order to make baby you must make love, and every time you make love, you must make baby. However, suppose you don't want to make baby every time you make love. In that case you can use a pipe.

baby: | love
    touch $@

now you rm love and make says only

touch love

because although in order to make baby you must make love, it is not the case that every time you make love, you must make baby.

Solution 3:[3]

I think the documentation does describe how they behave. It describes how a normal prerequisite behaves:

A normal prerequisite makes two statements: first, it imposes an order in which recipes will be invoked: the recipes for all prerequisites of a target will be completed before the recipe for the target is run. Second, it imposes a dependency relationship: if any prerequisite is newer than the target, then the target is considered out-of-date and must be rebuilt.

Then it describes how the behavior of an order-only prerequisite differs from that; it will:

impose a specific ordering on the rules to be invoked without forcing the target to be updated if one of those rules is executed.

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
Solution 2 John Henckel
Solution 3 MadScientist