Noeud:Phony Targets, Noeud « Previous »:Automatic Variables, Noeud « Up »:Special Targets



Phony Targets

Often, for ease of use, you want to capture a useful set of shell commands in a Makefile, yet the commands do not generate a file and thus have no target file; the target is just a convenient handle for a command that you want to run periodically. The clean target from example 5.2 is an example of such a rule, repeated here:

     clean:
             rm -f $(m4_OBJECTS)
     
     Example 5.26: A phony target
     

There is no file clean involved here; but you can clean out the object files in the build directory with:

     $ make clean
     rm -f main.o freeze.o stackovf.o
     

There is nothing special happening here. Asking Make to build the clean target, it finds that the clean file is out of date (missing entirely, infact!) and tries to refresh it by running the commands in the associated rule. No clean file is created by running commands, which means that Make will consider the clean target to still be out of date the next time you invoke make clean.

So what happens if you accidentally drop a file named clean into the build directory, and try to invoke the clean target? Well, Make will see the file, and find that there are no dependencies against the clean target - so clean is up to date already, and there is no need to execute the rm command. In all fairness, if you choose the names of your phony targets carefully, you shouldn't get into this situation. Just the same, Make provides a special target, .PHONY, against which you can list your phony targets as dependencies, like this:

     .PHONY: clean
     

Make now knows that clean does not represent an actual file, and will always execute the command part of the clean rule.

Unlike normal targets, .PHONY can be inserted into a Makefile more than once, in which case Make will append subsequent .PHONY dependencies to the current list, in exactly the same way that .SUFFIXES behaves (voir Suffix Rules).