Discussion:
Phony targets in make
(too old to reply)
Rouben Rostamian
2006-04-27 02:49:39 UTC
Permalink
The GNU "make" has a special target, named .PHONY which is
used thus:

.PHONY: clean

clean:
rm -f *.o

This way, `make clean' will run the "rm -f *.o" command
regardless of whether there is a file named `clean'.

Now, an inspection of the ancient BSD Unix Programmer's Manual
reveals that the original "make" did not have the .PHONY target.
I am guessing that .PHONY is a GNUism.

I have a vague recollection of a hack that achieved the effect
of .PHONY, but in the traditional make. Can anyone remind me
how it was done?
--
Rouben Rostamian
toby
2006-04-27 03:45:38 UTC
Permalink
Post by Rouben Rostamian
The GNU "make" has a special target, named .PHONY which is
.PHONY: clean
rm -f *.o
This way, `make clean' will run the "rm -f *.o" command
regardless of whether there is a file named `clean'.
Now, an inspection of the ancient BSD Unix Programmer's Manual
reveals that the original "make" did not have the .PHONY target.
I am guessing that .PHONY is a GNUism.
I have a vague recollection of a hack that achieved the effect
of .PHONY, but in the traditional make. Can anyone remind me
how it was done?
A different method is described in a section of the GNU make manual
headed "Rules without Commands or Prerequisites". I am not certain this
works in all other makes, but it does work in Solaris 10's:

\\
If a rule has no prerequisites or commands, and the target of the
rule is a nonexistent file, then `make' imagines this target to have
been updated whenever its rule is run. This implies that all targets
depending on this one will always have their commands run.
An example will illustrate this:

clean: FORCE
rm $(objects)
FORCE:
...
As you can see, using `FORCE' this way has the same results as using
`.PHONY: clean'.
//
Post by Rouben Rostamian
--
Rouben Rostamian
Rouben Rostamian
2006-04-27 11:49:55 UTC
Permalink
Post by toby
A different method is described in a section of the GNU make manual
headed "Rules without Commands or Prerequisites". I am not certain this
...
clean: FORCE
rm $(objects)
Thanks, that's what I was looking for. The documentation of the
original "make" is not precise enough to tell if this behavior is
generic, but it's good to know the trick anyway.

I have verified that this works with "make" supplied with
SGI Irix 6.5, SunOS 5.8 (= solaris xxx), and GNU make.
--
Rouben Rostamian
Paul D. Smith
2006-04-27 12:04:53 UTC
Permalink
clean: FORCE ; rm $(objects)
rr> Thanks, that's what I was looking for. The documentation of the
rr> original "make" is not precise enough to tell if this behavior is
rr> generic, but it's good to know the trick anyway.

It will work on any version of make... if it doesn't work, it's not
really make :-).
--
-------------------------------------------------------------------------------
Paul D. Smith <***@gnu.org> Find some GNU make tips at:
http://www.gnu.org http://make.paulandlesley.org
"Please remain calm...I may be mad, but I am a professional." --Mad Scientist
Loading...