.PHONY all rules in GNU make file

make

Am I wrong in my interpretation that I should basically just put first before all make rules:

.PHONY: all of my rules

all:
    echo "Executing all ..."

of:
    echo "Executing of ..."

my:
    echo "Executing my ..."

rules:
    echo "Executing rules ..."

Is there ever a case where you would not want to follow this 'formula'?

http://www.gnu.org/software/make/manual/make.html#Phony-Targets

Best Answer

Clark Grubb's Makefile style guide recommends that:

  • All phony targets should be declared by making them prerequisites of .PHONY.
  • add each phony target as a prerequisite of .PHONY immediately before the target declaration, rather than listing all the phony targets in a single place.
  • No file targets should be prerequisites of .PHONY.
  • phony targets should not be prerequisites of file targets.

For your example, this would mean:

.PHONY: all
all:
    echo "Executing all ..."

.PHONY: of
of:
    echo "Executing of ..."

.PHONY: my
my:
    echo "Executing my ..."

.PHONY: rules
rules:
    echo "Executing rules ..."

Multiple PHONY targets are allowed; see also this Stack Overflow question: "Is it possible to have multiple .PHONY targets in a gnu makefile?"

Also, while this isn't mentioned directly in your question, care must be taken not to have a PHONY target with the same name of an actual input or intermediate files in your project. Eg, if your project hypothetically had a source code file named rules (with no suffix), the inclusion of that string in a PHONY target could break expected make behavior.

Related Question