.PHONY all rules in GNU make file?

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

Asked By: tarabyte

||

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.

Answered By: bnewbold

I think it’s a good practice to follow but fragmenting it and putting the fragments even after full rule recipes works too on my gmake (3.81):

E.g.:

#Makefile
all:
   @echo hello world
.PHONY: all

CMD:

$ touch all
$ make all    #this would be a no-op if all weren't a phoney target
  hello world 
Answered By: PSkocik
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.