Roland Ver. 4.5 Technical Information Seite 28

  • Herunterladen
  • Zu meinen Handbüchern hinzufügen
  • Drucken
  • Seite
    / 212
  • Inhaltsverzeichnis
  • LESEZEICHEN
  • Bewertet. / 5. Basierend auf Kundenbewertungen
Seitenansicht 27
18 GNU make
That is, the target and prerequisite sections are expanded immediately, and the recipe
used to construct the target is always deferred. This general rule is true for explicit rules,
pattern rules, suffix rules, static pattern rules, and simple prerequisite definitions.
3.8 Secondary Expansion
In the previous section we learned that GNU make works in two distinct phases: a read-in
phase and a target-update phase (see Section 3.7 [How make Reads a Makefile], page 16).
GNU make also has the ability to enable a second expansion of the prerequisites (only) for
some or all targets defined in the makefile. In order for this second expansion to occur,
the special target .SECONDEXPANSION must be defined before the first prerequisite list that
makes use of this feature.
If that special target is defined then in between the two phases mentioned above, right
at the end of the read-in phase, all the prerequisites of the targets defined after the special
target are expanded a second time. In most circumstances this secondary expansion will
have no effect, since all variable and function references will have been expanded during the
initial parsing of the makefiles. In order to take advantage of the secondary expansion phase
of the parser, then, it’s necessary to escape the variable or function reference in the makefile.
In this case the first expansion merely un-escapes the reference but doesn’t expand it, and
expansion is left to the secondary expansion phase. For example, consider this makefile:
.SECONDEXPANSION:
ONEVAR = onefile
TWOVAR = twofile
myfile: $(ONEVAR) $$(TWOVAR)
After the first expansion phase the prerequisites list of the myfile target will be onefile
and $(TWOVAR); the first (unescaped) variable reference to ONEVAR is expanded, while
the second (escaped) variable reference is simply unescaped, without being recognized as a
variable reference. Now during the secondary expansion the first word is expanded again
but since it contains no variable or function references it remains the value onefile, while
the second word is now a normal reference to the variable TWOVAR, which is expanded
to the value twofile. The final result is that there are two prerequisites, onefile and
twofile.
Obviously, this is not a very interesting case since the same result could more easily have
been achieved simply by having both variables appear, unescaped, in the prerequisites list.
One difference becomes apparent if the variables are reset; consider this example:
.SECONDEXPANSION:
AVAR = top
onefile: $(AVAR)
twofile: $$(AVAR)
AVAR = bottom
Here the prerequisite of onefile will be expanded immediately, and resolve to the value
top, while the prerequisite of twofile will not be full expanded until the secondary expan-
sion and yield a value of bottom.
This is marginally more exciting, but the true power of this feature only becomes ap-
parent when you discover that secondary expansions always take place within the scope of
the automatic variables for that target. This means that you can use variables such as $@,
Seitenansicht 27
1 2 ... 23 24 25 26 27 28 29 30 31 32 33 ... 211 212

Kommentare zu diesen Handbüchern

Keine Kommentare