Chapter 4: Writing Rules 31
cleanall : cleanobj cleandiff
rm program
cleanobj :
rm *.o
cleandiff :
rm *.diff
4.6 Rules without Recipes or Prerequisites
If a rule has no prerequisites or recipe, 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 recipe run.
An example will illustrate this:
clean: FORCE
rm $(objects)
FORCE:
Here the target ‘FORCE’ satisfies the special conditions, so the target clean that depends
on it is forced to run its recipe. There is nothing special about the name ‘FORCE’, but that
is one name commonly used this way.
As you can see, using ‘FORCE’ this way has the same results as using ‘.PHONY: clean’.
Using ‘.PHONY’ is more explicit and more efficient. However, other versions of make do
not support ‘.PHONY’; thus ‘FORCE’ appears in many makefiles. See Section 4.5 [Phony
Targets], page 29.
4.7 Empty Target Files to Record Events
The empty target is a variant of the phony target; it is used to hold recipes for an action
that you request explicitly from time to time. Unlike a phony target, this target file can
really exist; but the file’s contents do not matter, and usually are empty.
The purpose of the empty target file is to record, with its last-modification time, when
the rule’s recipe was last executed. It does so because one of the commands in the recipe
is a touch command to update the target file.
The empty target file should have some prerequisites (otherwise it doesn’t make sense).
When you ask to remake the empty target, the recipe is executed if any prerequisite is more
recent than the target; in other words, if a prerequisite has changed since the last time you
remade the target. Here is an example:
print: foo.c bar.c
lpr -p $?
touch print
With this rule, ‘make print’ will execute the lpr command if either source file has changed
since the last ‘make print’. The automatic variable ‘$?’ is used to print only those files
that have changed (see Section 10.5.3 [Automatic Variables], page 120).
Kommentare zu diesen Handbüchern