28

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

2 Answers 2

43

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.

1
  • 3
    excellent share on the style guide. thank you.
    – tarabyte
    Commented Jul 27, 2015 at 21:08
2

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 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .