1612 |
@cindex secondary expansion |
@cindex secondary expansion |
1613 |
@cindex expansion, secondary |
@cindex expansion, secondary |
1614 |
|
|
1615 |
|
@findex .SECONDEXPANSION |
1616 |
In the previous section we learned that GNU @code{make} works in two |
In the previous section we learned that GNU @code{make} works in two |
1617 |
distinct phases: a read-in phase and a target-update phase |
distinct phases: a read-in phase and a target-update phase |
1618 |
(@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}). |
(@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}). GNU |
1619 |
There is an extra wrinkle that comes in between those two phases, |
make also has the ability to enable a @emph{second expansion} of the |
1620 |
right at the end of the read-in phase: at that time, all the |
prerequisites (only) for some or all targets defined in the makefile. |
1621 |
prerequisites of all of the targets are expanded a @emph{second time}. |
In order for this second expansion to occur, the special target |
1622 |
In most circumstances this secondary expansion will have no effect, |
@code{.SECONDEXPANSION} must be defined before the first prerequisite |
1623 |
since all variable and function references will have been expanded |
list that makes use of this feature. |
1624 |
during the initial parsing of the makefiles. In order to take |
|
1625 |
advantage of the secondary expansion phase of the parser, then, it's |
If that special target is defined then in between the two phases |
1626 |
necessary to @emph{escape} the variable or function reference in the |
mentioned above, right at the end of the read-in phase, all the |
1627 |
makefile. In this case the first expansion merely un-escapes the |
prerequisites of the targets defined after the special target are |
1628 |
reference but doesn't expand it, and expansion is left to the |
expanded a @emph{second time}. In most circumstances this secondary |
1629 |
secondary expansion phase. For example, consider this makefile: |
expansion will have no effect, since all variable and function |
1630 |
|
references will have been expanded during the initial parsing of the |
1631 |
|
makefiles. In order to take advantage of the secondary expansion |
1632 |
|
phase of the parser, then, it's necessary to @emph{escape} the |
1633 |
|
variable or function reference in the makefile. In this case the |
1634 |
|
first expansion merely un-escapes the reference but doesn't expand it, |
1635 |
|
and expansion is left to the secondary expansion phase. For example, |
1636 |
|
consider this makefile: |
1637 |
|
|
1638 |
@example |
@example |
1639 |
|
.SECONDEXPANSION: |
1640 |
ONEVAR = onefile |
ONEVAR = onefile |
1641 |
TWOVAR = twofile |
TWOVAR = twofile |
1642 |
myfile: $(ONEVAR) $$(TWOVAR) |
myfile: $(ONEVAR) $$(TWOVAR) |
1660 |
apparent if the variables are reset; consider this example: |
apparent if the variables are reset; consider this example: |
1661 |
|
|
1662 |
@example |
@example |
1663 |
|
.SECONDEXPANSION: |
1664 |
AVAR = top |
AVAR = top |
1665 |
onefile: $(AVAR) |
onefile: $(AVAR) |
1666 |
twofile: $$(AVAR) |
twofile: $$(AVAR) |
1680 |
expected values, just as in the command script. All you have to do is |
expected values, just as in the command script. All you have to do is |
1681 |
defer the expansion by escaping the @code{$}. Also, secondary |
defer the expansion by escaping the @code{$}. Also, secondary |
1682 |
expansion occurs for both explicit and implicit (pattern) rules. |
expansion occurs for both explicit and implicit (pattern) rules. |
1683 |
Knowing this, the possible uses for this feature are almost endless. |
Knowing this, the possible uses for this feature increase |
1684 |
For example: |
dramatically. For example: |
1685 |
|
|
1686 |
@example |
@example |
1687 |
|
.SECONDEXPANSION: |
1688 |
main_OBJS := main.o try.o test.o |
main_OBJS := main.o try.o test.o |
1689 |
lib_OBJS := lib.o api.o |
lib_OBJS := lib.o api.o |
1690 |
|
|
1705 |
main_SRCS := main.c try.c test.c |
main_SRCS := main.c try.c test.c |
1706 |
lib_SRCS := lib.c api.c |
lib_SRCS := lib.c api.c |
1707 |
|
|
1708 |
|
.SECONDEXPANSION: |
1709 |
main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS)) |
main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS)) |
1710 |
@end example |
@end example |
1711 |
|
|
1735 |
without). The following example will help illustrate these behaviors: |
without). The following example will help illustrate these behaviors: |
1736 |
|
|
1737 |
@example |
@example |
1738 |
|
.SECONDEXPANSION: |
1739 |
|
|
1740 |
foo: foo.1 bar.1 $$< $$^ $$+ # line #1 |
foo: foo.1 bar.1 $$< $$^ $$+ # line #1 |
1741 |
|
|
1742 |
foo: foo.2 bar.2 $$< $$^ $$+ # line #2 |
foo: foo.2 bar.2 $$< $$^ $$+ # line #2 |
1777 |
the same fashion as for static pattern rules. As an example: |
the same fashion as for static pattern rules. As an example: |
1778 |
|
|
1779 |
@example |
@example |
1780 |
|
.SECONDEXPANSION: |
1781 |
|
|
1782 |
foo: bar |
foo: bar |
1783 |
|
|
1784 |
foo foz: fo%: bo% |
foo foz: fo%: bo% |
1797 |
example: |
example: |
1798 |
|
|
1799 |
@example |
@example |
1800 |
|
.SECONDEXPANSION: |
1801 |
|
|
1802 |
/tmp/foo.o: |
/tmp/foo.o: |
1803 |
|
|
1804 |
%.o: $$(addsuffix /%.c,foo bar) foo.h |
%.o: $$(addsuffix /%.c,foo bar) foo.h |
2898 |
as secondary (i.e., no target is removed because it is considered |
as secondary (i.e., no target is removed because it is considered |
2899 |
intermediate). |
intermediate). |
2900 |
|
|
2901 |
|
@findex .SECONDEXPANSION |
2902 |
|
@item .SECONDEXPANSION |
2903 |
|
|
2904 |
|
If @code{.SECONDEXPANSION} is mentioned as a target anwyeren in the |
2905 |
|
makefile, then all prerequisite lists defined @emph{after} it appears |
2906 |
|
will be expanded a second time after all makefiles have been read in. |
2907 |
|
@xref{Secondary Expansion, ,Secondary Expansion}. |
2908 |
|
|
2909 |
|
The prerequisites of the special target @code{.SUFFIXES} are the list |
2910 |
|
of suffixes to be used in checking for suffix rules. |
2911 |
|
@xref{Suffix Rules, , Old-Fashioned Suffix Rules}. |
2912 |
|
|
2913 |
@findex .DELETE_ON_ERROR |
@findex .DELETE_ON_ERROR |
2914 |
@item .DELETE_ON_ERROR |
@item .DELETE_ON_ERROR |
2915 |
@cindex removing targets on failure |
@cindex removing targets on failure |