jcs's openbsd hax
openbsd
1.\" $OpenBSD: tutorial.ms,v 1.14 2022/03/02 09:39:41 jmc Exp $
2.\" $NetBSD: tutorial.ms,v 1.3 1996/03/06 00:15:31 christos Exp $
3.\" Copyright (c) 1988, 1989 by Adam de Boor
4.\" Copyright (c) 1989 by Berkeley Softworks
5.\" Copyright (c) 1988, 1989, 1993
6.\" The Regents of the University of California. All rights reserved.
7.\"
8.\" This code is derived from software contributed to Berkeley by
9.\" Adam de Boor.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\" notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\" notice, this list of conditions and the following disclaimer in the
18.\" documentation and/or other materials provided with the distribution.
19.\" 3. Neither the name of the University nor the names of its contributors
20.\" may be used to endorse or promote products derived from this software
21.\" without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\" @(#)tutorial.ms 8.1 (Berkeley) 8/18/93
36.\"
37.EH 'PSD:12-%''Make \*- A Tutorial'
38.OH 'Make \*- A Tutorial''PSD:12-%'
39.\" xH is a macro to provide numbered headers that are automatically stuffed
40.\" into a table-of-contents, properly indented, etc. If the first argument
41.\" is numeric, it is taken as the depth for numbering (as for .NH), else
42.\" the default (1) is assumed.
43.\"
44.\" @P The initial paragraph distance.
45.\" @Q The piece of section number to increment (or 0 if none given)
46.\" @R Section header.
47.\" @S Indent for toc entry
48.\" @T Argument to NH (can't use @Q b/c giving 0 to NH resets the counter)
49.de xH
50.NH \\$1
51\\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
52.nr PD .1v
53.XS \\n%
54.ta 0.6i
55\\*(SN \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
56.XE
57.nr PD .3v
58..
59.\" CW is used to place a string in fixed-width or switch to a
60.\" fixed-width font.
61.\" C is a typewriter font for a laserwriter. Use something else if
62.\" you don't have one...
63.de CW
64.ie !\\n(.$ .ft C
65.el \&\\$3\fC\\$1\fP\\$2
66..
67.\" Anything I put in a display I want to be in fixed-width
68.am DS
69.CW
70..
71.\" The stuff in .No produces a little stop sign in the left margin
72.\" that says NOTE in it. Unfortunately, it does cause a break, but
73.\" hey. Can't have everything. In case you're wondering how I came
74.\" up with such weird commands, they came from running grn on a
75.\" gremlin file...
76.de No
77.br
78.ne 0.5i
79.po -0.5i
80.br
81.mk
82.nr g3 \\n(.f
83.nr g4 \\n(.s
84.sp -1
85.\" .st cf
86\D's -1u'\D't 5u'
87.sp -1
88\h'50u'\D'l 71u 0u'\D'l 50u 50u'\D'l 0u 71u'\D'l -50u 50u'\D'l -71u 0u'\D'l -50u -50u'\D'l 0u -71u'\D'l 50u -50u'
89.sp -1
90\D't 3u'
91.sp -1
92.sp 7u
93\h'53u'\D'p 14 68u 0u 46u 46u 0u 68u -46u 46u -68u 0u -47u -46u 0u -68u 47u -46u'
94.sp -1
95.ft R
96.ps 6
97.nr g8 \\n(.d
98.ds g9 "NOTE
99.sp 74u
100\h'85u'\v'0.85n'\h-\w\\*(g9u/2u\&\\*(g9
101.sp |\\n(g8u
102.sp 166u
103\D't 3u'\D's -1u'
104.br
105.po
106.rt
107.ft \\n(g3
108.ps \\n(g4
109..
110.de Bp
111.ie !\\n(.$ .IP \(bu 2
112.el .IP "\&" 2
113..
114.po +.3i
115.TL
116Make \*- A Tutorial
117.AU
118Adam de Boor
119.AI
120Berkeley Softworks
1212150 Shattuck Ave, Penthouse
122Berkeley, CA 94704
123adam@bsw.uu.net
124\&...!uunet!bsw!adam
125.FS
126Permission to use, copy, modify, and distribute this software and its
127documentation for any purpose and without fee is hereby granted,
128provided that the above copyright notice appears in all copies.
129The University of California, Berkeley Softworks, and Adam de Boor make no
130representations about the suitability of this software for any
131purpose. It is provided "as is" without express or implied warranty.
132.FE
133.PP
134.xH 1 Introduction
135.LP
136.LP
137Make is a program for creating other programs, or anything else you
138can think of for it to do. The basic idea behind Make is that, for
139any given system, be it a program or a document or whatever, there
140will be some files that depend on the state of other files (on when
141they were last modified). Make takes these dependencies, which you
142must specify, and uses them to build whatever it is you want it to
143build.
144.LP
145OpenBSD's Make is based upon PMake, a parallel make originally developed
146for the distributed operating system called Sprite. PMake departs from
147usual Make practices in several ways. A large number of those quirks
148are not relevant in a modern POSIX world, and hence development of
149OpenBSD's make has aimed at removing unwanted differences.
150Useful features of OpenBSD's Make which are not POSIX compliant will
151be flagged with a little sign in the left margin, like this:
152.No
153Also note that this tutorial was originally written for PMake, and hence
154may not be totally accurate.
155.LP
156This tutorial is divided into three main sections corresponding to basic,
157intermediate and advanced Make usage. If you already know Make well,
158you will only need to skim chapter 2.
159Things in chapter 3 make life much easier, while those in chapter 4
160are strictly for those who know what they are doing. Chapter 5 has
161definitions for the jargon I use and chapter 6 contains possible
162solutions to the problems presented throughout the tutorial.
163.xH 1 The Basics of Make
164.LP
165Make takes as input a file that tells a) which files depend on which
166other files to be complete and b) what to do about files that are
167``out-of-date.'' This file is known as a ``makefile'' and is usually
168.Ix 0 def makefile
169kept in the top-most directory of the system to be built. While you
170can call the makefile anything you want, Make will look for
171.CW makefile
172and
173.CW Makefile
174(in that order) in the current directory if you don't tell it
175otherwise.
176.Ix 0 def makefile default
177To specify a different makefile, use the
178.B \-f
179flag (e.g.
180.CW "make -f program.mk" ''). ``
181.Ix 0 ref flags -f
182.Ix 0 ref makefile other
183.LP
184A makefile has four different types of lines in it:
185.RS
186.IP \(bu 2
187File dependency specifications
188.IP \(bu 2
189Creation commands
190.IP \(bu 2
191Variable assignments
192.IP \(bu 2
193Comments, include statements and conditional directives
194.RE
195.LP
196Any line may be continued over multiple lines by ending it with a
197backslash.
198.Ix 0 def "continuation line"
199The backslash, following newline and any initial whitespace
200on the following line are compressed into a single space before the
201input line is examined by Make.
202.xH 2 Dependency Lines
203.LP
204As mentioned in the introduction, in any system, there are
205dependencies between the files that make up the system. For instance,
206in a program made up of several C source files and one header file,
207the C files will need to be re-compiled should the header file be
208changed. For a document of several chapters and one macro file, the
209chapters will need to be reprocessed if any of the macros changes.
210.Ix 0 def "dependency"
211These are dependencies and are specified by means of dependency lines in
212the makefile.
213.LP
214.Ix 0 def "dependency line"
215On a dependency line, there are targets and sources, separated by a
216one- or two-character operator.
217The targets ``depend'' on the sources and are usually created from
218them.
219.Ix 0 def target
220.Ix 0 def source
221.Ix 0 ref operator
222Any number of targets and sources may be specified on a dependency line.
223All the targets in the line are made to depend on all the sources.
224Targets and sources need not be actual files, but every source must be
225either an actual file or another target in the makefile.
226If you run out of room, use a backslash at the end of the line to continue onto
227the next one.
228.LP
229Any file may be a target and any file may be a source, but the
230relationship between the two (or however many) is determined by the
231``operator'' that separates them.
232.Ix 0 def operator
233Three types of operators exist: one specifies that the datedness of a
234target is determined by the state of its sources, while another
235specifies other files (the sources) that need to be dealt with before
236the target can be re-created. The third operator is very similar to
237the first, with the additional condition that the target is
238out-of-date if it has no sources. These operations are represented by
239the colon, the exclamation point and the double-colon, respectively, and are
240mutually exclusive. Their exact semantics are as follows:
241.IP ":"
242.Ix 0 def operator colon
243.Ix 0 def :
244If a colon is used, a target on the line is considered to be
245``out-of-date'' (and in need of creation) if
246.RS
247.IP \(bu 2
248any of the sources has been modified more recently than the target, or
249.IP \(bu 2
250the target doesn't exist.
251.RE
252.Ix 0 def out-of-date
253.IP "\&"
254Under this operation, steps will be taken to re-create the target only
255if it is found to be out-of-date by using these two rules.
256.IP "!"
257.Ix 0 def operator force
258.Ix 0 def !
259If an exclamation point is used, the target will always be re-created,
260but this will not happen until all of its sources have been examined
261and re-created, if necessary.
262.IP "::"
263.Ix 0 def operator double-colon
264.Ix 0 def ::
265If a double-colon is used, a target is out-of-date if:
266.RS
267.IP \(bu 2
268any of the sources has been modified more recently than the target, or
269.IP \(bu 2
270the target doesn't exist, or
271.IP \(bu 2
272the target has no sources.
273.RE
274.IP "\&"
275If the target is out-of-date according to these rules, it will be re-created.
276This operator also does something else to the targets, but I'll go
277into that in the next section (``Shell Commands'').
278.LP
279Enough words, now for an example. Take that C program I mentioned
280earlier. Say there are three C files
281.CW a.c , (
282.CW b.c
283and
284.CW c.c )
285each of which
286includes the file
287.CW defs.h .
288The dependencies between the files could then be expressed as follows:
289.DS
290program : a.o b.o c.o
291a.o b.o c.o : defs.h
292a.o : a.c
293b.o : b.c
294c.o : c.c
295.DE
296.LP
297You may be wondering at this point, where
298.CW a.o ,
299.CW b.o
300and
301.CW c.o
302came in and why
303.I they
304depend on
305.CW defs.h
306and the C files don't. The reason is quite simple:
307.CW program
308cannot be made by linking together .c files \*- it must be
309made from .o files. Likewise, if you change
310.CW defs.h ,
311it isn't the .c files that need to be re-created, it's the .o files.
312If you think of dependencies in these terms \*- which files (targets)
313need to be created from which files (sources) \*- you should have no problems.
314.LP
315An important thing to notice about the above example, is that all the
316\&.o files appear as targets on more than one line. This is perfectly
317all right: the target is made to depend on all the sources mentioned
318on all the dependency lines. E.g.
319.CW a.o
320depends on both
321.CW defs.h
322and
323.CW a.c .
324.Ix 0 ref dependency
325.No
326.LP
327The order of the dependency lines in the makefile is
328important: the first target on the first dependency line in the
329makefile will be the one that gets made if you don't say otherwise.
330That's why
331.CW program
332comes first in the example makefile, above.
333.LP
334Both targets and sources may contain the standard C-Shell wildcard
335characters
336.CW { , (
337.CW } ,
338.CW * ,
339.CW ? ,
340.CW [ ,
341and
342.CW ] ),
343but the non-curly-brace ones may only appear in the final component
344(the file portion) of the target or source. The characters mean the
345following things:
346.IP \fB{}\fP
347These enclose a comma-separated list of options and cause the pattern
348to be expanded once for each element of the list. Each expansion
349contains a different element. For example,
350.CW src/{whiffle,beep,fish}.c
351expands to the three words
352.CW src/whiffle.c ,
353.CW src/beep.c ,
354and
355.CW src/fish.c .
356These braces may be nested and, unlike the other wildcard characters,
357the resulting words need not be actual files. All other wildcard
358characters are expanded using the files that exist when Make is
359started.
360.IP \fB*\fP
361This matches zero or more characters of any sort.
362.CW src/*.c
363will expand to the same three words as above as long as
364.CW src
365contains those three files (and no other files that end in
366.CW .c ).
367.IP \fB?\fP
368Matches any single character.
369.IP \fB[]\fP
370This is known as a character class and contains either a list of
371single characters, or a series of character ranges
372.CW a-z , (
373for example means all characters between a and z), or both. It matches
374any single character contained in the list. E.g.
375.CW [A-Za-z]
376will match all letters, while
377.CW [0123456789]
378will match all numbers.
379.xH 2 Shell Commands
380.LP
381``Isn't that nice,'' you say to yourself, ``but how are files
382actually `re-created,' as he likes to spell it?''
383The re-creation is accomplished by commands you place in the makefile.
384These commands are passed to the Bourne shell (better known as
385``/bin/sh'') to be executed and are
386.Ix 0 ref shell
387.Ix 0 ref re-creation
388.Ix 0 ref update
389expected to do what's necessary to update the target file (Make
390doesn't actually check to see if the target was created. It just
391assumes it's there).
392.Ix 0 ref target
393.LP
394Shell commands in a makefile look a lot like shell commands you would
395type at a terminal, with one important exception: each command in a
396makefile
397.I must
398be preceded by at least one tab.
399.LP
400Each target has associated with it a set of one or more of these shell
401commands. The creation script for a target
402should immediately follow the dependency line for that target. While
403any given target may appear on more than one dependency line, only one
404of these dependency lines may be followed by a creation script, unless
405the `::' operator was used on the dependency line.
406.Ix 0 ref operator double-colon
407.Ix 0 ref ::
408.No
409.LP
410If the double-colon was used, each dependency line for the target
411may be followed by a set of shell commands. This set of shell
412commands will only be executed
413if the target on the associated dependency line is out-of-date with
414respect to the sources on that line, according to the rules I gave
415earlier.
416I'll give you a good example of this later on.
417.LP
418To expand on the earlier makefile, you might add commands as follows:
419.DS
420program : a.o b.o c.o
421 cc a.o b.o c.o \-o program
422a.o b.o c.o : defs.h
423a.o : a.c
424 cc \-c a.c
425b.o : b.c
426 cc \-c b.c
427c.o : c.c
428 cc \-c c.c
429.DE
430.LP
431Something you should remember when writing a makefile is, the
432commands will be executed if the
433.I target
434on the dependency line is out-of-date, not the sources.
435.Ix 0 ref target
436.Ix 0 ref source
437.Ix 0 ref out-of-date
438In this example, the command
439.CW "cc \-c a.c" '' ``
440will be executed if
441.CW a.o
442is out-of-date. Because of the `:' operator,
443.Ix 0 ref :
444.Ix 0 ref operator colon
445this means that should
446.CW a.c
447.I or
448.CW defs.h
449have been modified more recently than
450.CW a.o ,
451the command will be executed
452.CW a.o "\&" (
453will be considered out-of-date).
454.Ix 0 ref out-of-date
455.LP
456Remember how I said the only difference between a makefile shell
457command and a regular shell command was the leading tab? I lied. There
458is another way in which makefile commands differ from regular ones.
459The first two characters after the initial whitespace are treated
460specially.
461If they are any combination of `@' and `\-', they cause Make to do
462different things.
463.LP
464In most cases, shell commands are printed before they're
465actually executed. This is to keep you informed of what's going on. If
466an `@' appears, however, this echoing is suppressed. In the case of an
467.CW echo
468command, say
469.CW "echo Linking index" ,'' ``
470it would be
471rather silly to see
472.DS
473echo Linking index
474Linking index
475.DE
476.LP
477so Make allows you to place an `@' before the command
478.CW "@echo Linking index" '') (``
479to prevent the command from being printed.
480.LP
481The other special character is the `\-'. In case you didn't know,
482shell commands finish with a certain ``exit status.'' This status is
483made available by the operating system to whatever program invoked the
484command. Normally this status will be 0 if everything went ok and
485non-zero if something went wrong. For this reason, Make will consider
486an error to have occurred if one of the shells it invokes returns a non-zero
487status. When it detects an error, Make's usual action is to abort
488whatever it's doing and exit with a non-zero status itself (any other
489targets that were being created will continue being made, but nothing
490new will be started. Make will exit after the last job finishes).
491This behavior can be altered, however, by placing a `\-' at the front
492of a command
493.CW "\-mv index index.old" ''), (``
494certain command-line arguments,
495or doing other things, to be detailed later. In such
496a case, the non-zero status is simply ignored and Make keeps chugging
497along.
498.No
499.LP
500In Make
501.B \-j
502mode, a set of shell commands attached to a target is fed to a shell
503as a single script. This is experimental behavior from PMake's period
504which hasn't been fixed yet.
505.LP
506Make has a
507.B \-B
508.Ix 0 ref compatibility
509.Ix 0 ref flags -B
510flag (it stands for backwards-compatible) that forces each command to
511be given to a separate shell. Unfortunately, it also inhibits
512.B \-j .
513.No
514.LP
515A target's shell script is fed to the shell on its (the shell's) input stream.
516This means that any commands, such as
517.CW ci
518that need to get input from the terminal won't work right \*- they'll
519get the shell's input, something they probably won't find to their
520liking. A simple way around this is to give a command like this:
521.DS
522ci $(SRCS) < /dev/tty
523.DE
524This would force the program's input to come from the terminal. If you
525can't do this for some reason, your only other alternative is to use
526Make in its fullest compatibility mode. See
527.B Compatibility
528in chapter 4.
529.Ix 0 ref compatibility
530.LP
531.xH 2 Variables
532.LP
533Make has the ability to save text in variables
534to be recalled later at your convenience. Variables in Make are used
535much like variables in the shell and, by tradition, consist of
536all upper-case letters (you don't
537.I have
538to use all upper-case letters.
539In fact there's nothing to stop you from calling a variable
540.CW @^&$%$ .
541Just tradition). Variables are assigned-to using lines of the form
542.Ix 0 def variable assignment
543.DS
544VARIABLE = value
545.DE
546.Ix 0 def variable assignment
547appended-to by
548.DS
549VARIABLE += value
550.DE
551.Ix 0 def variable appending
552.Ix 0 def variable assignment appended
553.Ix 0 def +=
554conditionally assigned-to (if the variable isn't already defined) by
555.DS
556VARIABLE ?= value
557.DE
558.Ix 0 def variable assignment conditional
559.Ix 0 def ?=
560and assigned-to with expansion (i.e. the value is expanded (see below)
561before being assigned to the variable\*-useful for placing a value at
562the beginning of a variable, or other things) by
563.DS
564VARIABLE := value
565.DE
566.Ix 0 def variable assignment expanded
567.Ix 0 def :=
568.LP
569Any whitespace before
570.I value
571is stripped off. When appending, a space is placed between the old
572value and the stuff being appended.
573.LP
574The final way a variable may be assigned to is using
575.DS
576VARIABLE != shell-command
577.DE
578.Ix 0 def variable assignment shell-output
579.Ix 0 def !=
580In this case,
581.I shell-command
582has all its variables expanded (see below) and is passed off to a
583shell to execute. The output of the shell is then placed in the
584variable. Any newlines (other than the final one) are replaced by
585spaces before the assignment is made. This is typically used to find
586the current directory via a line like:
587.DS
588CWD != pwd
589.DE
590.LP
591.B Note:
592this command will be invoked each time the Makefile is parsed, regardless
593of whether or not the result will actually be used for making targets.
594If the end result is only needed for shell commands, it is much cheaper to
595use
596.DS
597VARIABLE = `shell-command`
598.DE
599.LP
600The value of a variable may be retrieved by enclosing the variable
601name in parentheses or curly braces and prefixing the whole thing with
602a dollar sign.
603.LP
604For example, to set the variable CFLAGS to the string
605.CW "\-I/usr/local/include \-O" ,'' ``
606you would place a line
607.DS
608CFLAGS = \-I/usr/local/include \-O
609.DE
610in the makefile and use the word
611.CW "$(CFLAGS)"
612wherever you would like the string
613.CW "\-I/usr/local/include \-O"
614to appear. This is called variable expansion.
615.Ix 0 def variable expansion
616.LP
617To keep Make from substituting for a variable it knows, precede the
618dollar sign with another dollar sign.
619(e.g. to pass
620.CW "${HOME}"
621to the shell, use
622.CW "$${HOME}" ).
623This causes Make, in effect, to expand the
624.CW $
625macro, which expands to a single
626.CW $ .
627.LP
628.Ix 0 ref variable expansion
629There are two different times at which variable expansion occurs:
630When parsing a dependency line, the expansion occurs immediately
631upon reading the line. If any variable used on a dependency line is
632undefined, Make will print a message and exit.
633Variables in shell commands are expanded when the command is
634executed.
635Variables used inside another variable are expanded whenever the outer
636variable is expanded (the expansion of an inner variable has no effect
637on the outer variable. I.e. if the outer variable is used on a dependency
638line and in a shell command, and the inner variable changes value
639between when the dependency line is read and the shell command is
640executed, two different values will be substituted for the outer
641variable).
642.Ix 0 def variable types
643.LP
644Variables come in four flavors, though they are all expanded the same
645and all look about the same. They are (in order of expanding scope):
646.RS
647.IP \(bu 2
648Local variables.
649.Ix 0 ref variable local
650.IP \(bu 2
651Command-line variables.
652.Ix 0 ref variable command-line
653.IP \(bu 2
654Global variables.
655.Ix 0 ref variable global
656.IP \(bu 2
657Environment variables.
658.Ix 0 ref variable environment
659.RE
660.LP
661The classification of variables doesn't matter much, except that the
662classes are searched from the top (local) to the bottom (environment)
663when looking up a variable. The first one found wins.
664.xH 3 Local Variables
665.LP
666.Ix 0 def variable local
667Each target can have as many as seven local variables. These are
668variables that are only ``visible'' within that target's shell commands
669and contain such things as the target's name, all of its sources (from
670all its dependency lines), those sources that were out-of-date, etc.
671.No
672POSIX defines short names for these variables, which should be used for
673portability. OpenBSD's Make has longer synonyms, which will be used
674in the rest of this tutorial for clarity.
675.LP
676Four local variables are defined for all targets. They are:
677.RS
678.IP ".TARGET"
679.Ix 0 def variable local .TARGET
680.Ix 0 def .TARGET
681The name of the target (POSIX: @).
682.IP ".OODATE"
683.Ix 0 def variable local .OODATE
684.Ix 0 def .OODATE
685The list of the sources for the target that were considered out-of-date.
686The order in the list is not guaranteed to be the same as the order in
687which the dependencies were given. (POSIX: ?)
688.IP ".ALLSRC"
689.Ix 0 def variable local .ALLSRC
690.Ix 0 def .ALLSRC
691The list of all sources for this target in the order in which they
692were given. (shorter: >, not POSIX).
693.IP ".PREFIX"
694.Ix 0 def variable local .PREFIX
695.Ix 0 def .PREFIX
696The target without its suffix and without any leading path. E.g. for
697the target
698.CW ../../lib/compat/fsRead.c ,
699this variable would contain
700.CW fsRead
701(POSIX: *) .
702.RE
703.LP
704Three other local variables are set only for certain targets under
705special circumstances. These are the ``.IMPSRC,''
706.Ix 0 ref variable local .IMPSRC
707.Ix 0 ref .IMPSRC
708``.ARCHIVE,''
709.Ix 0 ref variable local .ARCHIVE
710.Ix 0 ref .ARCHIVE
711and ``.MEMBER''
712.Ix 0 ref variable local .MEMBER
713.Ix 0 ref .MEMBER
714variables. When they are set and how they are used is described later.
715.LP
716Four of these variables may be used in sources as well as in shell
717commands.
718.Ix 0 def "dynamic source"
719.Ix 0 def source dynamic
720These are ``.TARGET'', ``.PREFIX'', ``.ARCHIVE'' and ``.MEMBER''. The
721variables in the sources are expanded once for each target on the
722dependency line, providing what is known as a ``dynamic source,''
723.Rd 0
724allowing you to specify several dependency lines at once. For example,
725.DS
726$(OBJS) : $(.PREFIX).c
727.DE
728will create a dependency between each object file and its
729corresponding C source file.
730.xH 3 Command-line Variables
731.LP
732.Ix 0 def variable command-line
733Command-line variables are set when Make is first invoked by giving a
734variable assignment as one of the arguments. For example,
735.DS
736make "CFLAGS = -I/usr/local/include -O"
737.DE
738would make
739.CW CFLAGS
740be a command-line variable with the given value. Any assignments to
741.CW CFLAGS
742in the makefile will have no effect, because once it
743is set, there is (almost) nothing you can do to change a command-line
744variable (the search order, you see). Command-line variables may be
745set using any of the four assignment operators, though only
746.CW =
747and
748.CW ?=
749behave in a sane way, mostly because assignments to
750command-line variables are performed before the makefile is read, thus
751the values set in the makefile are unavailable at the time.
752.CW +=
753.Ix 0 ref +=
754.Ix 0 ref variable assignment appended
755is the same as
756.CW = ,
757because the old value of the variable is sought only in the scope in
758which the assignment is taking place (you don't want to know).
759.CW :=
760and
761.CW ?=
762.Ix 0 ref :=
763.Ix 0 ref ?=
764.Ix 0 ref variable assignment expanded
765.Ix 0 ref variable assignment conditional
766will work if the only variables used are in the environment.
767.CW !=
768is sort of pointless to use from the command line, since the same
769effect can no doubt be accomplished using the shell's own command
770substitution mechanisms (backquotes and all that).
771.xH 3 Global Variables
772.LP
773.Ix 0 def variable global
774Global variables are those set or appended-to in the makefile.
775There are two classes of global variables: those you set and those Make sets.
776As I said before, the ones you set can have any name you want them to have,
777except they may not contain a colon or an exclamation point.
778The variables Make sets (almost) always begin with a
779period and always contain upper-case letters, only. The variables are
780as follows:
781.RS
782.IP MAKE
783.Ix 0 def variable global MAKE
784.Ix 0 def MAKE
785.Ix 0 def variable global MAKE
786.Ix 0 def MAKE
787The name by which Make was invoked is stored in this variable.
788.IP .MAKEFLAGS
789.Ix 0 def variable global .MAKEFLAGS
790.Ix 0 def .MAKEFLAGS variable
791.Ix 0 def variable global MFLAGS
792.Ix 0 def MFLAGS
793All the relevant flags with which Make was invoked. This does not
794include such things as
795.B \-f .
796.RE
797.LP
798Two other variables, ``.INCLUDES'' and ``.LIBS,'' are covered in the
799section on special targets in chapter 3.
800.Ix 0 ref variable global .INCLUDES
801.Ix 0 ref variable global .LIBS
802.LP
803Global variables may be deleted using lines of the form:
804.Ix 0 def .undef
805.Ix 0 def variable deletion
806.DS
807\&.undef \fIvariable\fP
808.DE
809The
810.CW . ' `
811must be the first character on the line. Note that this may only be
812done on global variables.
813.xH 3 Environment Variables
814.LP
815.Ix 0 def variable environment
816Environment variables are passed by the shell that invoked Make and
817are given by Make to each shell it invokes. They are expanded like
818any other variable, but they cannot be altered in any way.
819.LP
820Using all these variables, you can compress the sample makefile even more:
821.DS
822OBJS = a.o b.o c.o
823program : $(OBJS)
824 cc $(.ALLSRC) \-o $(.TARGET)
825$(OBJS) : defs.h
826a.o : a.c
827 cc \-c a.c
828b.o : b.c
829 cc \-c b.c
830c.o : c.c
831 cc \-c c.c
832.DE
833.Ix 0 ref variable local .ALLSRC
834.Ix 0 ref .ALLSRC
835.Ix 0 ref variable local .TARGET
836.Ix 0 ref .TARGET
837.Rd 3
838.xH 2 Comments
839.LP
840.Ix 0 def comments
841Comments in a makefile start with a `#' character and extend to the
842end of the line. They may appear
843anywhere you want them, except in a shell command (though the shell
844will treat it as a comment, too). If, for some reason, you need to use the `#'
845in a variable or on a dependency line, put a backslash in front of it.
846Make will compress the two into a single `#'.
847.xH 2 Parallelism
848.No
849.LP
850PMake was specifically designed to re-create several targets at once,
851when possible, when
852using the
853.B \-j
854flag (see below),
855.Ix 0 ref flags -j
856but you do have to be careful at times.
857.LP
858There are several problems you are likely to encounter. One is
859that some makefiles (and programs) are written in such a way that it is
860impossible for two targets to be made at once. The program
861.CW xstr ,
862for example,
863always modifies the files
864.CW strings
865and
866.CW x.c .
867There is no way to change it. Thus you cannot run two of them at once
868without something being trashed. Similarly, if you have commands
869in the makefile that always send output to the same file, you will not
870be able to make more than one target at once unless you change the
871file you use. You can, for instance, add a
872.CW $$$$
873to the end of the file name to tack on the process ID of the shell
874executing the command (each
875.CW $$
876expands to a single
877.CW $ ,
878thus giving you the shell variable
879.CW $$ ).
880.LP
881The other problem comes from improperly-specified dependencies that
882worked in sequential mode.
883While I don't want to go into depth on how Make
884works (look in chapter 4 if you're interested), I will warn you that
885files in two different ``levels'' of the dependency tree may be
886examined in a different order in parallel mode than in sequential mode. For
887example, given the makefile
888.DS
889a : b c
890b : d
891.DE
892Make may examine the targets in the order
893.CW c ,
894.CW d ,
895.CW b ,
896.CW a .
897If the makefile's author expected Make to abort before making
898.CW c
899if an error occurred while making
900.CW b ,
901or if
902.CW b
903needed to exist before
904.CW c
905was made,
906s/he will be sorely disappointed. The dependencies are
907incomplete, since in both these cases,
908.CW c
909would depend on
910.CW b .
911So watch out.
912.LP
913Another problem you may face is that, while Make is set up to handle the
914output from multiple jobs in a graceful fashion, the same is not so for input.
915It has no way to regulate input to different jobs,
916so if you use the redirection from
917.CW /dev/tty
918I mentioned earlier, you must be careful not to run two of the jobs at once.
919.xH 2 Writing and Debugging a Makefile
920.LP
921Now you know most of what's in a makefile, what do you do next? There
922are two choices: (1) use one of the uncommonly-available makefile
923generators or (2) write your own makefile (I leave out the third choice of
924ignoring Make and doing everything by hand as being beyond the bounds
925of common sense).
926.LP
927When faced with the writing of a makefile, it is usually best to start
928from first principles: just what
929.I are
930you trying to do? What do you want the makefile finally to produce?
931.LP
932To begin with a somewhat traditional example, let's say you need to
933write a makefile to create a program,
934.CW expr ,
935that takes standard infix expressions and converts them to prefix form (for
936no readily apparent reason). You've got three source files, in C, that
937make up the program:
938.CW main.c ,
939.CW parse.c ,
940and
941.CW output.c .
942Harking back to my pithy advice about dependency lines, you write the
943first line of the file:
944.DS
945expr : main.o parse.o output.o
946.DE
947because you remember
948.CW expr
949is made from
950.CW .o
951files, not
952.CW .c
953files. Similarly for the
954.CW .o
955files you produce the lines:
956.DS
957main.o : main.c
958parse.o : parse.c
959output.o : output.c
960main.o parse.o output.o : defs.h
961.DE
962.LP
963Great. You've now got the dependencies specified. What you need now is
964commands. These commands, remember, must produce the target on the
965dependency line, usually by using the sources you've listed.
966You remember about local variables? Good, so it should come
967to you as no surprise when you write
968.DS
969expr : main.o parse.o output.o
970 cc -o $(.TARGET) $(.ALLSRC)
971.DE
972Why use the variables? If your program grows to produce postfix
973expressions too (which, of course, requires a name change or two), it
974is one fewer place you have to change the file. You cannot do this for
975the object files, however, because they depend on their corresponding
976source files
977.I and
978.CW defs.h ,
979thus if you said
980.DS
981 cc -c $(.ALLSRC)
982.DE
983you'd get (for
984.CW main.o ):
985.DS
986 cc -c main.c defs.h
987.DE
988which is wrong. So you round out the makefile with these lines:
989.DS
990main.o : main.c
991 cc -c main.c
992parse.o : parse.c
993 cc -c parse.c
994output.o : output.c
995 cc -c output.c
996.DE
997.LP
998The makefile is now complete and will, in fact, create the program you
999want it to without unnecessary compilations or excessive typing on
1000your part. There are two things wrong with it, however (aside from it
1001being altogether too long, something I'll address in chapter 3):
1002.IP 1)
1003The string
1004.CW "main.o parse.o output.o" '' ``
1005is repeated twice, necessitating two changes when you add postfix
1006(you were planning on that, weren't you?). This is in direct violation
1007of de Boor's First Rule of writing makefiles:
1008.QP
1009.I
1010Anything that needs to be written more than once
1011should be placed in a variable.
1012.IP "\&"
1013I cannot emphasize this enough as being very important to the
1014maintenance of a makefile and its program.
1015.IP 2)
1016There is no way to alter the way compilations are performed short of
1017editing the makefile and making the change in all places. This is evil
1018and violates de Boor's Second Rule, which follows directly from the
1019first:
1020.QP
1021.I
1022Any flags or programs used inside a makefile should be placed in a variable so
1023they may be changed, temporarily or permanently, with the greatest ease.
1024.LP
1025The makefile should more properly read:
1026.DS
1027OBJS = main.o parse.o output.o
1028expr : $(OBJS)
1029 $(CC) $(CFLAGS) -o $(.TARGET) $(.ALLSRC)
1030main.o : main.c
1031 $(CC) $(CFLAGS) -c main.c
1032parse.o : parse.c
1033 $(CC) $(CFLAGS) -c parse.c
1034output.o : output.c
1035 $(CC) $(CFLAGS) -c output.c
1036$(OBJS) : defs.h
1037.DE
1038Alternatively, if you like the idea of dynamic sources mentioned in
1039section 2.3.1,
1040.Rm 0 2.3.1
1041.Rd 4
1042.Ix 0 ref "dynamic source"
1043.Ix 0 ref source dynamic
1044you could write it like this:
1045.DS
1046OBJS = main.o parse.o output.o
1047expr : $(OBJS)
1048 $(CC) $(CFLAGS) -o $(.TARGET) $(.ALLSRC)
1049$(OBJS) : $(.PREFIX).c defs.h
1050 $(CC) $(CFLAGS) -c $(.PREFIX).c
1051.DE
1052These two rules and examples lead to de Boor's First Corollary:
1053.QP
1054.I
1055Variables are your friends.
1056.LP
1057Once you've written the makefile comes the sometimes-difficult task of
1058.Ix 0 ref debugging
1059making sure the darn thing works. Your most helpful tool to make sure
1060the makefile is at least syntactically correct is the
1061.B \-n
1062.Ix 0 ref flags -n
1063flag, which allows you to see if Make will choke on the makefile. The
1064second thing the
1065.B \-n
1066flag lets you do is see what Make would do without it actually doing
1067it, thus you can make sure the right commands would be executed were
1068you to give Make its head.
1069.LP
1070When you find your makefile isn't behaving as you hoped, the first
1071question that comes to mind (after ``What time is it, anyway?'') is
1072``Why not?'' In answering this, one flag will serve you well:
1073.CW "-d m" .'' ``
1074.Ix 0 ref flags -d
1075This causes Make to tell you as it examines each target in the
1076makefile and indicate why it is deciding whatever it is deciding. You
1077can then use the information printed for other targets to see where
1078you went wrong.
1079.LP
1080Something to be especially careful about is circular dependencies.
1081.Ix 0 def dependency circular
1082E.g.
1083.DS
1084a : b
1085b : c d
1086d : a
1087.DE
1088In this case, because of the way Make works,
1089.CW c
1090is the only thing Make will examine, because
1091.CW d
1092and
1093.CW a
1094will effectively fall off the edge of the universe, making it
1095impossible to examine
1096.CW b
1097(or them, for that matter).
1098Make will tell you (if run in its normal mode) all the targets
1099involved in any cycle it looked at (i.e. if you have two cycles in the
1100graph (naughty, naughty), but only try to make a target in one of
1101them, Make will only tell you about that one. You'll have to try to
1102make the other to find the second cycle). When run as Make, it will
1103only print the first target in the cycle.
1104.xH 2 Invoking Make
1105.LP
1106.Ix 0 ref flags
1107.Ix 0 ref arguments
1108.Ix 0 ref usage
1109Make comes with a wide variety of flags to choose from.
1110They may appear in any order, interspersed with command-line variable
1111assignments and targets to create.
1112Some of these flags are as follows:
1113.IP "\fB\-d\fP \fIwhat\fP"
1114.Ix 0 def flags -d
1115.Ix 0 ref debugging
1116This causes Make to spew out debugging information that
1117may prove useful to you. If you can't
1118figure out why Make is doing what it's doing, you might try using
1119this flag. The
1120.I what
1121parameter is a string of single characters that tell Make what
1122aspects you are interested in. Most of what I describe will make
1123little sense to you, unless you've dealt with Make before. Just
1124remember where this table is and come back to it as you read on.
1125The characters and the information they produce are as follows:
1126.RS
1127.IP a
1128Archive searching and caching.
1129.IP c
1130Conditional evaluation.
1131.IP d
1132The searching and caching of directories.
1133.IP j
1134Various snippets of information related to the running of the multiple
1135shells. Not particularly interesting.
1136.IP m
1137The making of each target: what target is being examined; when it was
1138last modified; whether it is out-of-date; etc.
1139.IP p
1140Makefile parsing.
1141.IP r
1142Remote execution.
1143.IP s
1144The application of suffix-transformation rules. (See chapter 3)
1145.IP t
1146The maintenance of the list of targets.
1147.IP v
1148Variable assignment.
1149.RE
1150.IP "\&"
1151Of these all, the
1152.CW m
1153and
1154.CW s
1155letters will be most useful to you.
1156If the
1157.B \-d
1158is the final argument or the argument from which it would get these
1159key letters (see below for a note about which argument would be used)
1160begins with a
1161.B \- ,
1162all of these debugging flags will be set, resulting in massive amounts
1163of output.
1164.IP "\fB\-f\fP \fImakefile\fP"
1165.Ix 0 def flags -f
1166Specify a makefile to read different from the standard makefiles
1167.CW makefile "\&" (
1168or
1169.CW Makefile ).
1170.Ix 0 ref makefile default
1171.Ix 0 ref makefile other
1172If
1173.I makefile
1174is ``\-'', Make uses the standard input. This is useful for making
1175quick and dirty makefiles.\|.\|.
1176.Ix 0 ref makefile "quick and dirty"
1177.IP \fB\-i\fP
1178.Ix 0 def flags -i
1179If you give this flag, Make will ignore non-zero status returned
1180by any of its shells. It's like placing a `\-' before all the commands
1181in the makefile.
1182.IP \fB\-k\fP
1183.Ix 0 def flags -k
1184This is similar to
1185.B \-i
1186in that it allows Make to continue when it sees an error, but unlike
1187.B \-i ,
1188where Make continues blithely as if nothing went wrong,
1189.B \-k
1190causes it to recognize the error and only continue work on those
1191things that don't depend on the target, either directly or indirectly (through
1192depending on something that depends on it), whose creation returned the error.
1193The `k' is for ``keep going''.\|.\|.
1194.Ix 0 ref target
1195.IP "\fB\-m\fP \fIdirectory\fP"
1196.Ix 0 def flags -m
1197Tells Make another place to search for included makefiles via the <...>
1198style. Several
1199.B \-m
1200options can be given to form a search path. If this construct is used the
1201default system makefile search path is completely overridden.
1202To be explained in chapter 3, section 3.2.
1203.Rm 2 3.2
1204.IP \fB\-n\fP
1205.Ix 0 def flags -n
1206This flag tells Make not to execute the commands needed to update the
1207out-of-date targets in the makefile. Rather, Make will simply print
1208the commands it would have executed and exit. This is particularly
1209useful for checking the correctness of a makefile. If Make doesn't do
1210what you expect it to, it's a good chance the makefile is wrong.
1211.IP \fB\-q\fP
1212.Ix 0 def flags -q
1213If you give Make this flag, it will not try to re-create anything. It
1214will just see if anything is out-of-date and exit non-zero if so.
1215.IP \fB\-r\fP
1216.Ix 0 def flags -r
1217When Make starts up, it reads a default makefile that tells it what
1218sort of system it's on and gives it some idea of what to do if you
1219don't tell it anything. I'll tell you about it in chapter 3. If you
1220give this flag, Make won't read the default makefile.
1221.IP \fB\-s\fP
1222.Ix 0 def flags -s
1223This causes Make to not print commands before they're executed. It
1224is the equivalent of putting an `@' before every command in the
1225makefile.
1226.IP \fB\-t\fP
1227.Ix 0 def flags -t
1228Rather than try to re-create a target, Make will simply ``touch'' it
1229so as to make it appear up-to-date. If the target didn't exist before,
1230it will when Make finishes, but if the target did exist, it will
1231appear to have been updated.
1232.IP \fB\-B\fP
1233.Ix 0 ref compatibility
1234.Ix 0 def flags -B
1235Forces OpenBSD Make to be as POSIX-compatible as possible.
1236This includes:
1237.RS
1238.IP \(bu 2
1239Executing one shell per shell command
1240.IP \(bu 2
1241Using sequential mode.
1242.RE
1243.IP "\fB\-D\fP \fIvariable\fP"
1244.Ix 0 def flags -D
1245Allows you to define a variable to have
1246.CW 1 '' ``
1247as its value. The variable is a global variable, not a command-line
1248variable. This is useful mostly for people who are used to the C
1249compiler arguments and those using conditionals, which I'll get into
1250in section 4.3
1251.Rm 1 4.3
1252.IP "\fB\-I\fP \fIdirectory\fP"
1253.Ix 0 def flags -I
1254Tells Make another place to search for included makefiles. Yet
1255another thing to be explained in chapter 3 (section 3.2, to be
1256precise).
1257.Rm 2 3.2
1258.IP \fB\-P\fP
1259.Ix 0 def flags -P
1260.Ix 0 ref "output control"
1261When creating targets in parallel, several shells are executing at
1262once, each wanting to write its own two cent's-worth to the screen.
1263This output must be captured by Make in some way in order to prevent
1264the screen from being filled with garbage even more indecipherable
1265than you usually see. Make has two ways of doing this, one of which
1266provides for much cleaner output and a clear separation between the
1267output of different jobs, the other of which provides a more immediate
1268response so one can tell what is really happening. The former is done
1269by notifying you when the creation of a target starts, capturing the
1270output and transferring it to the screen all at once when the job
1271finishes. The latter is done by catching the output of the shell (and
1272its children) and buffering it until an entire line is received, then
1273printing that line preceded by an indication of which job produced
1274the output. Since I prefer this second method, it is the one used by
1275default. The first method will be used if you give the
1276.B \-P
1277flag to Make.
1278.LP
1279Flags without arguments may follow a single `\-'.
1280E.g.
1281.DS
1282make -f server.mk -DDEBUG -I/chip2/X/server/include -n
1283.DE
1284will cause Make to read
1285.CW server.mk
1286as the input makefile, define the variable
1287.CW DEBUG
1288as a global variable and look for included makefiles in the directory
1289.CW /chip2/X/server/include .
1290.xH 2 Summary
1291.LP
1292A makefile is made of four types of lines:
1293.RS
1294.IP \(bu 2
1295Dependency lines
1296.IP \(bu 2
1297Creation commands
1298.IP \(bu 2
1299Variable assignments
1300.IP \(bu 2
1301Comments, include statements and conditional directives
1302.RE
1303.LP
1304A dependency line is a list of one or more targets, an operator
1305.CW : ', (`
1306.CW :: ', `
1307or
1308.CW ! '), `
1309and a list of zero or more sources. Sources may contain wildcards and
1310certain local variables.
1311.LP
1312A creation command is a regular shell command preceded by a tab. In
1313addition, if the first two characters after the tab (and other
1314whitespace) are a combination of
1315.CW @ ' `
1316or
1317.CW - ', `
1318Make will cause the command to not be printed (if the character is
1319.CW @ ') `
1320or errors from it to be ignored (if
1321.CW - '). `
1322A blank line, dependency line or variable assignment terminates a
1323creation script. There may be only one creation script for each target
1324with a
1325.CW : ' `
1326or
1327.CW ! ' `
1328operator.
1329.LP
1330Variables are places to store text. They may be unconditionally
1331assigned-to using the
1332.CW = ' `
1333.Ix 0 ref =
1334.Ix 0 ref variable assignment
1335operator, appended-to using the
1336.CW += ' `
1337.Ix 0 ref +=
1338.Ix 0 ref variable assignment appended
1339operator, conditionally (if the variable is undefined) assigned-to
1340with the
1341.CW ?= ' `
1342.Ix 0 ref ?=
1343.Ix 0 ref variable assignment conditional
1344operator, and assigned-to with variable expansion with the
1345.CW := ' `
1346.Ix 0 ref :=
1347.Ix 0 ref variable assignment expanded
1348operator. The output of a shell command may be assigned to a variable
1349using the
1350.CW != ' `
1351.Ix 0 ref !=
1352.Ix 0 ref variable assignment shell-output
1353operator. Variables may be expanded (their value inserted) by enclosing
1354their name in parentheses or curly braces, preceded by a dollar sign.
1355A dollar sign may be escaped with another dollar sign. Variables are
1356not expanded if Make doesn't know about them. There are seven local
1357variables:
1358.CW .TARGET ,
1359.CW .ALLSRC ,
1360.CW .OODATE ,
1361.CW .PREFIX ,
1362.CW .IMPSRC ,
1363.CW .ARCHIVE ,
1364and
1365.CW .MEMBER .
1366Four of them
1367.CW .TARGET , (
1368.CW .PREFIX ,
1369.CW .ARCHIVE ,
1370and
1371.CW .MEMBER )
1372may be used to specify ``dynamic sources.''
1373.Ix 0 ref "dynamic source"
1374.Ix 0 ref source dynamic
1375Variables are good. Know them. Love them. Live them.
1376.LP
1377Debugging of makefiles is best accomplished using the
1378.B \-n ,
1379and
1380.B "\-d m"
1381flags.
1382.xH 1 Short-cuts and Other Nice Things
1383.LP
1384Based on what I've told you so far, you may have gotten the impression
1385that Make is just a way of storing away commands and making sure you
1386don't forget to compile something. Good. That's just what it is.
1387However, the ways I've described have been inelegant, at best, and
1388painful, at worst.
1389This chapter contains things that make the
1390writing of makefiles easier and the makefiles themselves shorter and
1391easier to modify (and, occasionally, simpler). In this chapter, I
1392assume you are somewhat more
1393familiar with Unix than I did
1394in chapter 2, just so you're on your toes.
1395So without further ado...
1396.xH 2 Transformation Rules
1397.LP
1398As you know, a file's name consists of two parts: a base name, which
1399gives some hint as to the contents of the file, and a suffix, which
1400usually indicates the format of the file.
1401Over the years, as
1402.UX
1403has developed,
1404naming conventions, with regard to suffixes, have also developed that have
1405become almost as incontrovertible as Law. E.g. a file ending in
1406.CW .c
1407is assumed to contain C source code; one with a
1408.CW .o
1409suffix is assumed to be a compiled object file that may
1410be linked into any program; a file with a
1411.CW .ms
1412suffix is usually a text file to be processed by Troff with the \-ms
1413macro package, and so on.
1414One of the best aspects of Make comes from its
1415understanding of how the suffix of a file pertains to its contents and
1416Make's ability to do things with a file based solely on its suffix. This
1417ability comes from something known as a transformation rule. A
1418transformation rule specifies how to change a file with one suffix
1419into a file with another suffix.
1420.LP
1421A transformation rule looks much like a dependency line, except the
1422target is made of two known suffixes stuck together. Suffixes are made
1423known to Make by placing them as sources on a dependency line whose
1424target is the special target
1425.CW .SUFFIXES .
1426E.g.
1427.DS
1428\&.SUFFIXES : .o .c
1429\&.c.o :
1430 $(CC) $(CFLAGS) -c $(.IMPSRC)
1431.DE
1432The creation script attached to the target is used to transform a file with
1433the first suffix (in this case,
1434.CW .c )
1435into a file with the second suffix (here,
1436.CW .o ).
1437In addition, the target inherits whatever attributes have been applied
1438to the transformation rule.
1439The simple rule given above says that to transform a C source file
1440into an object file, you compile it using
1441.CW cc
1442with the
1443.CW \-c
1444flag.
1445This rule is taken straight from the system makefile. Many
1446transformation rules (and suffixes) are defined there, and I refer you
1447to it for more examples.
1448.LP
1449There are several things to note about the transformation rule given
1450above:
1451.RS
1452.IP 1)
1453The
1454.CW .IMPSRC
1455variable.
1456.Ix 0 def variable local .IMPSRC
1457.Ix 0 def .IMPSRC
1458This variable is set to the ``implied source'' (the file from which
1459the target is being created; the one with the first suffix), which, in this
1460case, is the .c file.
1461.IP 2)
1462The
1463.CW CFLAGS
1464variable. Almost all of the transformation rules in the system
1465makefile are set up using variables that you can alter in your
1466makefile to tailor the rule to your needs. In this case, if you want
1467all your C files to be compiled with the
1468.B \-g
1469flag, to provide information for
1470.CW dbx ,
1471you would set the
1472.CW CFLAGS
1473variable to contain
1474.CW -g
1475.CW "CFLAGS = -g" '') (``
1476and Make would take care of the rest.
1477.RE
1478.LP
1479To give you a quick example, the makefile in 2.3.4
1480.Rm 3 2.3.4
1481could be changed to this:
1482.DS
1483OBJS = a.o b.o c.o
1484program : $(OBJS)
1485 $(CC) -o $(.TARGET) $(.ALLSRC)
1486$(OBJS) : defs.h
1487.DE
1488The transformation rule I gave above takes the place of the 6 lines\**
1489.FS
1490This is also somewhat cleaner, I think, than the dynamic source
1491solution presented in 2.6
1492.FE
1493.Rm 4 2.6
1494.DS
1495a.o : a.c
1496 cc -c a.c
1497b.o : b.c
1498 cc -c b.c
1499c.o : c.c
1500 cc -c c.c
1501.DE
1502.LP
1503Now you may be wondering about the dependency between the
1504.CW .o
1505and
1506.CW .c
1507files \*- it's not mentioned anywhere in the new makefile. This is
1508because it isn't needed: one of the effects of applying a
1509transformation rule is the target comes to depend on the implied
1510source. That's why it's called the implied
1511.I source .
1512.LP
1513For a more detailed example. Say you have a makefile like this:
1514.DS
1515a.out : a.o b.o
1516 $(CC) $(.ALLSRC)
1517.DE
1518and a directory set up like this:
1519.DS
1520total 4
1521-rw-rw-r-- 1 deboor 34 Sep 7 00:43 Makefile
1522-rw-rw-r-- 1 deboor 119 Oct 3 19:39 a.c
1523-rw-rw-r-- 1 deboor 201 Sep 7 00:43 a.o
1524-rw-rw-r-- 1 deboor 69 Sep 7 00:43 b.c
1525.DE
1526While just typing
1527.CW make '' ``
1528will do the right thing, it's much more informative to type
1529.CW "make -d s" ''. ``
1530This will show you what Make is up to as it processes the files. In
1531this case, Make prints the following:
1532.DS
1533Suff_FindDeps (a.out)
1534 using existing source a.o
1535 applying .o -> .out to "a.o"
1536Suff_FindDeps (a.o)
1537 trying a.c...got it
1538 applying .c -> .o to "a.c"
1539Suff_FindDeps (b.o)
1540 trying b.c...got it
1541 applying .c -> .o to "b.c"
1542Suff_FindDeps (a.c)
1543 trying a.y...not there
1544 trying a.l...not there
1545 trying a.c,v...not there
1546 trying a.y,v...not there
1547 trying a.l,v...not there
1548Suff_FindDeps (b.c)
1549 trying b.y...not there
1550 trying b.l...not there
1551 trying b.c,v...not there
1552 trying b.y,v...not there
1553 trying b.l,v...not there
1554--- a.o ---
1555cc -c a.c
1556--- b.o ---
1557cc -c b.c
1558--- a.out ---
1559cc a.o b.o
1560.DE
1561.LP
1562.CW Suff_FindDeps
1563is the name of a function in Make that is called to check for implied
1564sources for a target using transformation rules.
1565The transformations it tries are, naturally
1566enough, limited to the ones that have been defined (a transformation
1567may be defined multiple times, by the way, but only the most recent
1568one will be used). You will notice, however, that there is a definite
1569order to the suffixes that are tried. This order is set by the
1570relative positions of the suffixes on the
1571.CW .SUFFIXES
1572line \*- the earlier a suffix appears, the earlier it is checked as
1573the source of a transformation. Once a suffix has been defined, the
1574only way to change its position in the pecking order is to remove all
1575the suffixes (by having a
1576.CW .SUFFIXES
1577dependency line with no sources) and redefine them in the order you
1578want. (Previously-defined transformation rules will be automatically
1579redefined as the suffixes they involve are re-entered.)
1580.LP
1581Another way to affect the search order is to make the dependency
1582explicit. In the above example,
1583.CW a.out
1584depends on
1585.CW a.o
1586and
1587.CW b.o .
1588Since a transformation exists from
1589.CW .o
1590to
1591.CW .out ,
1592Make uses that, as indicated by the
1593.CW "using existing source a.o" '' ``
1594message.
1595.LP
1596The search for a transformation starts from the suffix of the target
1597and continues through all the defined transformations, in the order
1598dictated by the suffix ranking, until an existing file with the same
1599base (the target name minus the suffix and any leading directories) is
1600found. At that point, one or more transformation rules will have been
1601found to change the one existing file into the target.
1602.LP
1603For example, ignoring what's in the system makefile for now, say you
1604have a makefile like this:
1605.DS
1606\&.SUFFIXES : .out .o .c .y .l
1607\&.l.c :
1608 lex $(.IMPSRC)
1609 mv lex.yy.c $(.TARGET)
1610\&.y.c :
1611 yacc $(.IMPSRC)
1612 mv y.tab.c $(.TARGET)
1613\&.c.o :
1614 cc -c $(.IMPSRC)
1615\&.o.out :
1616 cc -o $(.TARGET) $(.IMPSRC)
1617.DE
1618and the single file
1619.CW jive.l .
1620If you were to type
1621.CW "make -rd ms jive.out" ,'' ``
1622you would get the following output for
1623.CW jive.out :
1624.DS
1625Suff_FindDeps (jive.out)
1626 trying jive.o...not there
1627 trying jive.c...not there
1628 trying jive.y...not there
1629 trying jive.l...got it
1630 applying .l -> .c to "jive.l"
1631 applying .c -> .o to "jive.c"
1632 applying .o -> .out to "jive.o"
1633.DE
1634and this is why: Make starts with the target
1635.CW jive.out ,
1636figures out its suffix
1637.CW .out ) (
1638and looks for things it can transform to a
1639.CW .out
1640file. In this case, it only finds
1641.CW .o ,
1642so it looks for the file
1643.CW jive.o .
1644It fails to find it, so it looks for transformations into a
1645.CW .o
1646file. Again it has only one choice:
1647.CW .c .
1648So it looks for
1649.CW jive.c
1650and, as you know, fails to find it. At this point it has two choices:
1651it can create the
1652.CW .c
1653file from either a
1654.CW .y
1655file or a
1656.CW .l
1657file. Since
1658.CW .y
1659came first on the
1660.CW .SUFFIXES
1661line, it checks for
1662.CW jive.y
1663first, but can't find it, so it looks for
1664.CW jive.l
1665and, lo and behold, there it is.
1666At this point, it has defined a transformation path as follows:
1667.CW .l
1668\(->
1669.CW .c
1670\(->
1671.CW .o
1672\(->
1673.CW .out
1674and applies the transformation rules accordingly. For completeness,
1675and to give you a better idea of what Make actually did with this
1676three-step transformation, this is what Make printed for the rest of
1677the process:
1678.DS
1679Suff_FindDeps (jive.o)
1680 using existing source jive.c
1681 applying .c -> .o to "jive.c"
1682Suff_FindDeps (jive.c)
1683 using existing source jive.l
1684 applying .l -> .c to "jive.l"
1685Suff_FindDeps (jive.l)
1686Examining jive.l...modified 17:16:01 Oct 4, 1987...up-to-date
1687Examining jive.c...non-existent...out-of-date
1688--- jive.c ---
1689lex jive.l
1690\&.\|.\|. meaningless lex output deleted .\|.\|.
1691mv lex.yy.c jive.c
1692Examining jive.o...non-existent...out-of-date
1693--- jive.o ---
1694cc -c jive.c
1695Examining jive.out...non-existent...out-of-date
1696--- jive.out ---
1697cc -o jive.out jive.o
1698.DE
1699.LP
1700One final question remains: what does Make do with targets that have
1701no known suffix? Make simply pretends it actually has an empty suffix
1702and searches for transformations accordingly. Those special transformation
1703rules involve just one source suffix, like this:
1704.DS
1705\&.o :
1706 cc -o $(.TARGET) $(.IMPSRC)
1707.DE
1708.xH 2 Including Other Makefiles
1709.Ix 0 def makefile inclusion
1710.Rd 2
1711.LP
1712Just as for programs, it is often useful to extract certain parts of a
1713makefile into another file and just include it in other makefiles
1714somehow. Many compilers allow you say something like
1715.DS
1716#include "defs.h"
1717.DE
1718to include the contents of
1719.CW defs.h
1720in the source file. Make allows you to do the same thing for
1721makefiles, with the added ability to use variables in the filenames.
1722An include directive in a makefile looks either like this:
1723.DS
1724\&.include <file>
1725.DE
1726or this
1727.DS
1728\&.include "file"
1729.DE
1730The difference between the two is where Make searches for the file:
1731the first way, Make will look for
1732the file only in the system makefile directory (or directories)
1733The system makefile directory search path can be overridden via the
1734.B \-m
1735option.
1736.Ix 0 ref flags -m
1737For files in double-quotes, the search is more complex:
1738.RS
1739.IP 1)
1740The directory of the makefile that's including the file.
1741.IP 2)
1742The current directory (the one in which you invoked Make).
1743.IP 3)
1744The directories given by you using
1745.B \-I
1746flags, in the order in which you gave them.
1747.IP 4)
1748Directories given by
1749.CW .PATH
1750dependency lines (see chapter 4).
1751.IP 5)
1752The system makefile directory.
1753.RE
1754.LP
1755in that order.
1756.LP
1757You are free to use Make variables in the filename\*-Make will
1758expand them before searching for the file. You must specify the
1759searching method with either angle brackets or double-quotes
1760.I outside
1761of a variable expansion. I.e. the following
1762.DS
1763SYSTEM = <command.mk>
1764
1765#include $(SYSTEM)
1766.DE
1767won't work.
1768.xH 2 Target Attributes
1769.LP
1770Make allows you to give attributes to targets by means of special
1771sources. Like everything else Make uses, these sources begin with a
1772period and are made up of all upper-case letters. There are various
1773reasons for using them, and I will try to give examples for most of
1774them. Others you'll have to find uses for yourself. Think of it as ``an
1775exercise for the reader.'' By placing one (or more) of these as a source on a
1776dependency line, you are ``marking the target(s) with that
1777attribute.'' That's just the way I phrase it, so you know.
1778.LP
1779Any attributes given as sources for a transformation rule are applied
1780to the target of the transformation rule when the rule is applied.
1781.Ix 0 def attributes
1782.Ix 0 ref source
1783.Ix 0 ref target
1784.nr pw 12
1785.IP .EXEC \n(pw
1786.Ix 0 def attributes .EXEC
1787.Ix 0 def .EXEC
1788This attribute causes its shell script to be executed while having no
1789effect on targets that depend on it. This makes the target into a sort
1790of subroutine. An example. Say you have some LISP files that need to
1791be compiled and loaded into a LISP process. To do this, you echo LISP
1792commands into a file and execute a LISP with this file as its input
1793when everything's done. Say also that you have to load other files
1794from another system before you can compile your files and further,
1795that you don't want to go through the loading and dumping unless one
1796of
1797.I your
1798files has changed. Your makefile might look a little bit
1799like this (remember, this is an educational example, and don't worry
1800about the
1801.CW COMPILE
1802rule, all will soon become clear, grasshopper):
1803.DS
1804system : init a.fasl b.fasl c.fasl
1805 for i in $(.ALLSRC);
1806 do
1807 echo -n '(load "' >> input
1808 echo -n ${i} >> input
1809 echo '")' >> input
1810 done
1811 echo '(dump "$(.TARGET)")' >> input
1812 lisp < input
1813
1814a.fasl : a.l init COMPILE
1815b.fasl : b.l init COMPILE
1816c.fasl : c.l init COMPILE
1817COMPILE : .USE
1818 echo '(compile "$(.ALLSRC)")' >> input
1819init : .EXEC
1820 echo '(load-system)' > input
1821.DE
1822.Ix 0 ref .USE
1823.Ix 0 ref attributes .USE
1824.Ix 0 ref variable local .ALLSRC
1825.IP "\&"
1826.CW .EXEC
1827sources, don't appear in the local variables of targets that depend on
1828them (nor are they touched if Make is given the
1829.B \-t
1830flag).
1831.Ix 0 ref flags -t
1832Note that all the rules, not just that for
1833.CW system ,
1834include
1835.CW init
1836as a source. This is because none of the other targets can be made
1837until
1838.CW init
1839has been made, thus they depend on it.
1840.IP .IGNORE \n(pw
1841.Ix 0 def attributes .IGNORE
1842.Ix 0 def .IGNORE attribute
1843Giving a target the
1844.CW .IGNORE
1845attribute causes Make to ignore errors from any of the target's commands, as
1846if they all had `\-' before them.
1847.IP .INVISIBLE \n(pw
1848.Ix 0 def attributes .INVISIBLE
1849.Ix 0 def .INVISIBLE
1850This allows you to specify one target as a source for another without
1851the one affecting the other's local variables. Useful if, say, you
1852have a makefile that creates two programs, one of which is used to
1853create the other, so it must exist before the other is created. You
1854could say
1855.DS
1856prog1 : $(PROG1OBJS) prog2 MAKEINSTALL
1857prog2 : $(PROG2OBJS) .INVISIBLE MAKEINSTALL
1858.DE
1859where
1860.CW MAKEINSTALL
1861is some complex .USE rule (see below) that depends on the
1862.Ix 0 ref .USE
1863.CW .ALLSRC
1864variable containing the right things. Without the
1865.CW .INVISIBLE
1866attribute for
1867.CW prog2 ,
1868the
1869.CW MAKEINSTALL
1870rule couldn't be applied. This is not as useful as it should be, and
1871the semantics may change (or the whole thing go away) in the
1872not-too-distant future.
1873.IP .JOIN \n(pw
1874.Ix 0 def attributes .JOIN
1875.Ix 0 def .JOIN
1876This is another way to avoid performing some operations in parallel
1877while permitting everything else to be done so. Specifically it
1878forces the target's shell script to be executed only if one or more of the
1879sources was out-of-date. In addition, the target's name,
1880in both its
1881.CW .TARGET
1882variable and all the local variables of any target that depends on it,
1883is replaced by the value of its
1884.CW .ALLSRC
1885variable.
1886As an example, suppose you have a program that has four libraries that
1887compile in the same directory along with, and at the same time as, the
1888program. You again have the problem with
1889.CW ranlib
1890that I mentioned earlier, only this time it's more severe: you
1891can't just put the ranlib off to the end since the program
1892will need those libraries before it can be re-created. You can do
1893something like this:
1894.DS
1895program : $(OBJS) libraries
1896 cc -o $(.TARGET) $(.ALLSRC)
1897
1898libraries : lib1.a lib2.a lib3.a lib4.a .JOIN
1899 ranlib $(.OODATE)
1900.DE
1901.Ix 0 ref variable local .TARGET
1902.Ix 0 ref variable local .ALLSRC
1903.Ix 0 ref variable local .OODATE
1904.Ix 0 ref .TARGET
1905.Ix 0 ref .ALLSRC
1906.Ix 0 ref .OODATE
1907In this case, Make will re-create the
1908.CW $(OBJS)
1909as necessary, along with
1910.CW lib1.a ,
1911.CW lib2.a ,
1912.CW lib3.a
1913and
1914.CW lib4.a .
1915It will then execute
1916.CW ranlib
1917on any library that was changed and set
1918.CW program 's
1919.CW .ALLSRC
1920variable to contain what's in
1921.CW $(OBJS)
1922followed by
1923.CW "lib1.a lib2.a lib3.a lib4.a" .'' ``
1924In case you're wondering, it's called
1925.CW .JOIN
1926because it joins together different threads of the ``input graph'' at
1927the target marked with the attribute.
1928Another aspect of the .JOIN attribute is it keeps the target from
1929being created if the
1930.B \-t
1931flag was given.
1932.Ix 0 ref flags -t
1933.IP .MAKE \n(pw
1934.Ix 0 def attributes .MAKE
1935.Ix 0 def .MAKE
1936The
1937.CW .MAKE
1938attribute marks its target as being a recursive invocation of Make.
1939This forces Make to execute the script associated with the target (if
1940it's out-of-date) even if you gave the
1941.B \-n
1942or
1943.B \-t
1944flag. By doing this, you can start at the top of a system and type
1945.DS
1946make -n
1947.DE
1948and have it descend the directory tree (if your makefiles are set up
1949correctly), printing what it would have executed if you hadn't
1950included the
1951.B \-n
1952flag.
1953.IP .NOTMAIN \n(pw
1954.Ix 0 def attributes .NOTMAIN
1955.Ix 0 def .NOTMAIN
1956Normally, if you do not specify a target to make in any other way,
1957Make will take the first target on the first dependency line of a
1958makefile as the target to create. That target is known as the ``Main
1959Target'' and is labeled as such if you print the dependencies out
1960using the
1961.B \-p
1962flag.
1963.Ix 0 ref flags -p
1964Giving a target this attribute tells Make that the target is
1965definitely
1966.I not
1967the Main Target.
1968This allows you to place targets in an included makefile and
1969have Make create something else by default.
1970.IP .PRECIOUS \n(pw
1971.Ix 0 def attributes .PRECIOUS
1972.Ix 0 def .PRECIOUS attribute
1973When Make is interrupted (you type control-C at the keyboard), it
1974will attempt to clean up after itself by removing any half-made
1975targets. If a target has the
1976.CW .PRECIOUS
1977attribute, however, Make will leave it alone. An additional side
1978effect of the `::' operator is to mark the targets as
1979.CW .PRECIOUS .
1980.Ix 0 ref operator double-colon
1981.Ix 0 ref ::
1982.IP .SILENT \n(pw
1983.Ix 0 def attributes .SILENT
1984.Ix 0 def .SILENT attribute
1985Marking a target with this attribute keeps its commands from being
1986printed when they're executed, just as if they had an `@' in front of them.
1987.IP .USE \n(pw
1988.Ix 0 def attributes .USE
1989.Ix 0 def .USE
1990By giving a target this attribute, you turn it into Make's equivalent
1991of a macro. When the target is used as a source for another target,
1992the other target acquires the commands, sources and attributes (except
1993.CW .USE )
1994of the source.
1995If the target already has commands, the
1996.CW .USE
1997target's commands are added to the end. If more than one .USE-marked
1998source is given to a target, the rules are applied sequentially.
1999.IP "\&" \n(pw
2000The typical .USE rule (as I call them) will use the sources of the
2001target to which it is applied (as stored in the
2002.CW .ALLSRC
2003variable for the target) as its ``arguments,'' if you will.
2004For example, you probably noticed that the commands for creating
2005.CW lib1.a
2006and
2007.CW lib2.a
2008in the example in section 3.3
2009.Rm 5 3.3
2010were exactly the same. You can use the
2011.CW .USE
2012attribute to eliminate the repetition, like so:
2013.DS
2014lib1.a : $(LIB1OBJS) MAKELIB
2015lib2.a : $(LIB2OBJS) MAKELIB
2016
2017MAKELIB : .USE
2018 rm -f $(.TARGET)
2019 ar cr $(.TARGET) $(.ALLSRC)
2020 ...
2021 ranlib $(.TARGET)
2022.DE
2023.Ix 0 ref variable local .TARGET
2024.Ix 0 ref variable local .ALLSRC
2025.IP "\&" \n(pw
2026Several system makefiles (not to be confused with The System Makefile)
2027make use of these .USE rules to make your
2028life easier (they're in the default, system makefile directory...take a look).
2029Note that the .USE rule source itself
2030.CW MAKELIB ) (
2031does not appear in any of the target's local variables.
2032There is no limit to the number of times I could use the
2033.CW MAKELIB
2034rule. If there were more libraries, I could continue with
2035.CW "lib3.a : $(LIB3OBJS) MAKELIB" '' ``
2036and so on and so forth.
2037.xH 2 Special Targets
2038.LP
2039As there were in Make, so there are certain targets that have special
2040meaning to Make. When you use one on a dependency line, it is the
2041only target that may appear on the left-hand-side of the operator.
2042.Ix 0 ref target
2043.Ix 0 ref operator
2044As for the attributes and variables, all the special targets
2045begin with a period and consist of upper-case letters only.
2046I won't describe them all in detail because some of them are rather
2047complex and I'll describe them in more detail than you'll want in
2048chapter 4.
2049The targets are as follows:
2050.nr pw 10
2051.IP .BEGIN \n(pw
2052.Ix 0 def .BEGIN
2053Any commands attached to this target are executed before anything else
2054is done. You can use it for any initialization that needs doing.
2055.IP .DEFAULT \n(pw
2056.Ix 0 def .DEFAULT
2057This is sort of a .USE rule for any target (that was used only as a
2058source) that Make can't figure out any other way to create. It's only
2059``sort of'' a .USE rule because only the shell script attached to the
2060.CW .DEFAULT
2061target is used. The
2062.CW .IMPSRC
2063variable of a target that inherits
2064.CW .DEFAULT 's
2065commands is set to the target's own name.
2066.Ix 0 ref .IMPSRC
2067.Ix 0 ref variable local .IMPSRC
2068.IP .END \n(pw
2069.Ix 0 def .END
2070This serves a function similar to
2071.CW .BEGIN ,
2072in that commands attached to it are executed once everything has been
2073re-created (so long as no errors occurred). It also serves the extra
2074function of being a place on which Make can hang commands you put off
2075to the end. Thus the script for this target will be executed before
2076any of the commands you save with the ``.\|.\|.''.
2077.Ix 0 ref ...
2078.IP .IGNORE \n(pw
2079.Ix 0 def .IGNORE target
2080.Ix 0 ref .IGNORE attribute
2081.Ix 0 ref attributes .IGNORE
2082This target marks each of its sources with the
2083.CW .IGNORE
2084attribute. If you don't give it any sources, then it is like
2085giving the
2086.B \-i
2087flag when you invoke Make \*- errors are ignored for all commands.
2088.Ix 0 ref flags -i
2089.IP .INCLUDES \n(pw
2090.Ix 0 def .INCLUDES target
2091.Ix 0 def variable global .INCLUDES
2092.Ix 0 def .INCLUDES variable
2093The sources for this target are taken to be suffixes that indicate a
2094file that can be included in a program source file.
2095The suffix must have already been declared with
2096.CW .SUFFIXES
2097(see below).
2098Any suffix so marked will have the directories on its search path
2099(see
2100.CW .PATH ,
2101below) placed in the
2102.CW .INCLUDES
2103variable, each preceded by a
2104.B \-I
2105flag. This variable can then be used as an argument for the compiler
2106in the normal fashion. The
2107.CW .h
2108suffix is already marked in this way in the system makefile.
2109.Ix 0 ref makefile system
2110E.g. if you have
2111.DS
2112\&.SUFFIXES : .bitmap
2113\&.PATH.bitmap : /usr/local/X/lib/bitmaps
2114\&.INCLUDES : .bitmap
2115.DE
2116Make will place
2117.CW "-I/usr/local/X/lib/bitmaps" '' ``
2118in the
2119.CW .INCLUDES
2120variable and you can then say
2121.DS
2122cc $(.INCLUDES) -c xprogram.c
2123.DE
2124(Note: the
2125.CW .INCLUDES
2126variable is not actually filled in until the entire makefile has been read.)
2127.IP .INTERRUPT \n(pw
2128.Ix 0 def .INTERRUPT
2129When Make is interrupted,
2130it will execute the commands in the script for this target, if it
2131exists.
2132.IP .LIBS \n(pw
2133.Ix 0 def .LIBS target
2134.Ix 0 def .LIBS variable
2135.Ix 0 def variable global .LIBS
2136This does for libraries what
2137.CW .INCLUDES
2138does for include files, except the flag used is
2139.B \-L ,
2140as required by those linkers that allow you to tell them where to find
2141libraries. The variable used is
2142.CW .LIBS .
2143Be forewarned that Make may not have been compiled to do this if the
2144linker on your system doesn't accept the
2145.B \-L
2146flag, though the
2147.CW .LIBS
2148variable will always be defined once the makefile has been read.
2149.IP .MAIN \n(pw
2150.Ix 0 def .MAIN
2151If you didn't give a target (or targets) to create when you invoked
2152Make, it will take the sources of this target as the targets to
2153create.
2154.IP .MAKEFLAGS \n(pw
2155.Ix 0 def .MAKEFLAGS target
2156This target provides a way for you to always specify flags for Make
2157when the makefile is used. The flags are just as they would be typed
2158to the shell (except you can't use shell variables unless they're in
2159the environment),
2160though the
2161.B \-f
2162and
2163.B \-r
2164flags have no effect.
2165.IP .NULL \n(pw
2166.Ix 0 def .NULL
2167.Ix 0 ref suffix null
2168.Ix 0 ref "null suffix"
2169This allows you to specify what suffix Make should pretend a file has
2170if, in fact, it has no known suffix. Only one suffix may be so
2171designated. The last source on the dependency line is the suffix that
2172is used (you should, however, only give one suffix.\|.\|.).
2173.IP .PATH \n(pw
2174.Ix 0 def .PATH
2175If you give sources for this target, Make will take them as
2176directories in which to search for files it cannot find in the current
2177directory. If you give no sources, it will clear out any directories
2178added to the search path before. Since the effects of this all get
2179very complex, I'll leave it til chapter four to give you a complete
2180explanation.
2181.IP .PATH\fIsuffix\fP \n(pw
2182.Ix 0 ref .PATH
2183This does a similar thing to
2184.CW .PATH ,
2185but it does it only for files with the given suffix. The suffix must
2186have been defined already. Look at
2187.B "Search Paths"
2188(section 4.1)
2189.Rm 6 4.1
2190for more information.
2191.IP .PRECIOUS \n(pw
2192.Ix 0 def .PRECIOUS target
2193.Ix 0 ref .PRECIOUS attribute
2194.Ix 0 ref attributes .PRECIOUS
2195Similar to
2196.CW .IGNORE ,
2197this gives the
2198.CW .PRECIOUS
2199attribute to each source on the dependency line, unless there are no
2200sources, in which case the
2201.CW .PRECIOUS
2202attribute is given to every target in the file.
2203.IP .RECURSIVE \n(pw
2204.Ix 0 def .RECURSIVE
2205.Ix 0 ref attributes .MAKE
2206.Ix 0 ref .MAKE
2207This target applies the
2208.CW .MAKE
2209attribute to all its sources. It does nothing if you don't give it any sources.
2210.IP .SILENT \n(pw
2211.Ix 0 def .SILENT target
2212.Ix 0 ref .SILENT attribute
2213.Ix 0 ref attributes .SILENT
2214When you use
2215.CW .SILENT
2216as a target, it applies the
2217.CW .SILENT
2218attribute to each of its sources. If there are no sources on the
2219dependency line, then it is as if you gave Make the
2220.B \-s
2221flag and no commands will be echoed.
2222.IP .SUFFIXES \n(pw
2223.Ix 0 def .SUFFIXES
2224This is used to give new file suffixes for Make to handle. Each
2225source is a suffix Make should recognize. If you give a
2226.CW .SUFFIXES
2227dependency line with no sources, Make will forget about all the
2228suffixes it knew (this also nukes the null suffix).
2229For those targets that need to have suffixes defined, this is how you do it.
2230.LP
2231In addition to these targets, a line of the form
2232.DS
2233\fIattribute\fP : \fIsources\fP
2234.DE
2235applies the
2236.I attribute
2237to all the targets listed as
2238.I sources .
2239.xH 2 Modifying Variable Expansion
2240.LP
2241.Ix 0 def variable expansion modified
2242.Ix 0 ref variable expansion
2243.Ix 0 def variable modifiers
2244Variables need not always be expanded verbatim. Make defines several
2245modifiers that may be applied to a variable's value before it is
2246expanded. You apply a modifier by placing it after the variable name
2247with a colon between the two, like so:
2248.DS
2249${\fIVARIABLE\fP:\fImodifier\fP}
2250.DE
2251Each modifier is a single character followed by something specific to
2252the modifier itself.
2253You may apply as many modifiers as you want \*- each one is applied to
2254the result of the previous and is separated from the previous by
2255another colon.
2256.LP
2257There are seven ways to modify a variable's expansion, most of which
2258come from the C shell variable modification characters:
2259.RS
2260.IP "M\fIpattern\fP"
2261.Ix 0 def :M
2262.Ix 0 def modifier match
2263This is used to select only those words (a word is a series of
2264characters that are neither spaces nor tabs) that match the given
2265.I pattern .
2266The pattern is a wildcard pattern like that used by the shell, where
2267.CW *
2268means 0 or more characters of any sort;
2269.CW ?
2270is any single character;
2271.CW [abcd]
2272matches any single character that is either `a', `b', `c' or `d'
2273(there may be any number of characters between the brackets);
2274.CW [0-9]
2275matches any single character that is between `0' and `9' (i.e. any
2276digit. This form may be freely mixed with the other bracket form), and
2277`\\' is used to escape any of the characters `*', `?', `[' or `:',
2278leaving them as regular characters to match themselves in a word.
2279For example, the system makefile
2280.CW <makedepend.mk>
2281uses
2282.CW "$(CFLAGS:M-[ID]*)" '' ``
2283to extract all the
2284.CW \-I
2285and
2286.CW \-D
2287flags that would be passed to the C compiler. This allows it to
2288properly locate include files and generate the correct dependencies.
2289.IP "N\fIpattern\fP"
2290.Ix 0 def :N
2291.Ix 0 def modifier nomatch
2292This is identical to
2293.CW :M
2294except it substitutes all words that don't match the given pattern.
2295.IP "S/\fIsearch-string\fP/\fIreplacement-string\fP/[g]"
2296.Ix 0 def :S
2297.Ix 0 def modifier substitute
2298Causes the first occurrence of
2299.I search-string
2300in the variable to be replaced by
2301.I replacement-string ,
2302unless the
2303.CW g
2304flag is given at the end, in which case all occurrences of the string
2305are replaced. The substitution is performed on each word in the
2306variable in turn. If
2307.I search-string
2308begins with a
2309.CW ^ ,
2310the string must match starting at the beginning of the word. If
2311.I search-string
2312ends with a
2313.CW $ ,
2314the string must match to the end of the word (these two may be
2315combined to force an exact match). If a backslash precedes these two
2316characters, however, they lose their special meaning. Variable
2317expansion also occurs in the normal fashion inside both the
2318.I search-string
2319and the
2320.I replacement-string ,
2321.B except
2322that a backslash is used to prevent the expansion of a
2323.CW $ ,
2324not another dollar sign, as is usual.
2325Note that
2326.I search-string
2327is just a string, not a pattern, so none of the usual
2328regular-expression/wildcard characters have any special meaning save
2329.CW ^
2330and
2331.CW $ .
2332In the replacement string,
2333the
2334.CW &
2335character is replaced by the
2336.I search-string
2337unless it is preceded by a backslash.
2338You are allowed to use any character except
2339colon or exclamation point to separate the two strings. This so-called
2340delimiter character may be placed in either string by preceding it
2341with a backslash.
2342.IP T
2343.Ix 0 def :T
2344.Ix 0 def modifier tail
2345Replaces each word in the variable expansion by its last
2346component (its ``tail''). For example, given
2347.DS
2348OBJS = ../lib/a.o b /usr/lib/libm.a
2349TAILS = $(OBJS:T)
2350.DE
2351the variable
2352.CW TAILS
2353would expand to
2354.CW "a.o b libm.a" .'' ``
2355.IP H
2356.Ix 0 def :H
2357.Ix 0 def modifier head
2358This is similar to
2359.CW :T ,
2360except that every word is replaced by everything but the tail (the
2361``head''). Using the same definition of
2362.CW OBJS ,
2363the string
2364.CW "$(OBJS:H)" '' ``
2365would expand to
2366.CW "../lib /usr/lib" .'' ``
2367Note that the final slash on the heads is removed and
2368anything without a head is replaced by the empty string.
2369.IP E
2370.Ix 0 def :E
2371.Ix 0 def modifier extension
2372.Ix 0 def modifier suffix
2373.Ix 0 ref suffix "variable modifier"
2374.CW :E
2375replaces each word by its suffix (``extension''). So
2376.CW "$(OBJS:E)" '' ``
2377would give you
2378.CW ".o .a" .'' ``
2379.IP R
2380.Ix 0 def :R
2381.Ix 0 def modifier root
2382.Ix 0 def modifier base
2383This replaces each word by everything but the suffix (the ``root'' of
2384the word).
2385.CW "$(OBJS:R)" '' ``
2386expands to ``
2387.CW "../lib/a b /usr/lib/libm" .''
2388.RE
2389.LP
2390In addition, the System V style of substitution is also supported.
2391This looks like:
2392.DS
2393$(\fIVARIABLE\fP:\fIsearch-string\fP=\fIreplacement\fP)
2394.DE
2395It must be the last modifier in the chain. The search is anchored at
2396the end of each word, so only suffixes or whole words may be replaced.
2397.xH 2 More on Debugging
2398.xH 2 More Exercises
2399.IP (3.1)
2400You've got a set programs, each of which is created from its own
2401assembly-language source file (suffix
2402.CW .asm ).
2403Each program can be assembled into two versions, one with error-checking
2404code assembled in and one without. You could assemble them into files
2405with different suffixes
2406.CW .eobj \& (
2407and
2408.CW .obj ,
2409for instance), but your linker only understands files that end in
2410.CW .obj .
2411To top it all off, the final executables
2412.I must
2413have the suffix
2414.CW .exe .
2415How can you still use transformation rules to make your life easier
2416(Hint: assume the error-checking versions have
2417.CW ec
2418tacked onto their prefix)?
2419.xH 1 Make for Gods
2420.LP
2421This chapter is devoted to those facilities in Make that allow you to
2422do a great deal in a makefile with very little work, as well as do
2423some things you couldn't do in Make without a great deal of work (and
2424perhaps the use of other programs). The problem with these features,
2425is they must be handled with care, or you will end up with a mess.
2426.LP
2427Once more, I assume a greater familiarity with
2428.UX
2429or Sprite than I did in the previous two chapters.
2430.xH 2 Search Paths
2431.Rd 6
2432.LP
2433Make supports the dispersal of files into multiple directories by
2434allowing you to specify places to look for sources with
2435.CW .PATH
2436targets in the makefile. The directories you give as sources for these
2437targets make up a ``search path.'' Only those files used exclusively
2438as sources are actually sought on a search path, the assumption being
2439that anything listed as a target in the makefile can be created by the
2440makefile and thus should be in the current directory.
2441.LP
2442There are two types of search paths
2443in Make: one is used for all types of files (including included
2444makefiles) and is specified with a plain
2445.CW .PATH
2446target (e.g.
2447.CW ".PATH : RCS" ''), ``
2448while the other is specific to a certain type of file, as indicated by
2449the file's suffix. A specific search path is indicated by immediately following
2450the
2451.CW .PATH
2452with the suffix of the file. For instance
2453.DS
2454\&.PATH.h : /sprite/lib/include /sprite/att/lib/include
2455.DE
2456would tell Make to look in the directories
2457.CW /sprite/lib/include
2458and
2459.CW /sprite/att/lib/include
2460for any files whose suffix is
2461.CW .h .
2462.LP
2463The current directory is always consulted first to see if a file
2464exists. Only if it cannot be found there are the directories in the
2465specific search path, followed by those in the general search path,
2466consulted.
2467.LP
2468A search path is also used when expanding wildcard characters. If the
2469pattern has a recognizable suffix on it, the path for that suffix will
2470be used for the expansion. Otherwise the default search path is employed.
2471.LP
2472When a file is found in some directory other than the current one, all
2473local variables that would have contained the target's name
2474.CW .ALLSRC , (
2475and
2476.CW .IMPSRC )
2477will instead contain the path to the file, as found by Make.
2478Thus if you have a file
2479.CW ../lib/mumble.c
2480and a makefile
2481.DS
2482\&.PATH.c : ../lib
2483mumble : mumble.c
2484 $(CC) -o $(.TARGET) $(.ALLSRC)
2485.DE
2486the command executed to create
2487.CW mumble
2488would be
2489.CW "cc -o mumble ../lib/mumble.c" .'' ``
2490(As an aside, the command in this case isn't strictly necessary, since
2491it will be found using transformation rules if it isn't given. This is because
2492.CW .out
2493is the null suffix by default and a transformation exists from
2494.CW .c
2495to
2496.CW .out .
2497Just thought I'd throw that in.)
2498.LP
2499If a file exists in two directories on the same search path, the file
2500in the first directory on the path will be the one Make uses. So if
2501you have a large system spread over many directories, it would behoove
2502you to follow a naming convention that avoids such conflicts.
2503.LP
2504Something you should know about the way search paths are implemented
2505is that each directory is read, and its contents cached, exactly once
2506\&\*- when it is first encountered \*- so any changes to the
2507directories while Make is running will not be noted when searching
2508for implicit sources, nor will they be found when Make attempts to
2509discover when the file was last modified, unless the file was created in the
2510current directory. While people have suggested that Make should read
2511the directories each time, my experience suggests that the caching seldom
2512causes problems. In addition, not caching the directories slows things
2513down enormously because of Make's attempts to apply transformation
2514rules through non-existent files \*- the number of extra file-system
2515searches is truly staggering, especially if many files without
2516suffixes are used and the null suffix isn't changed from
2517.CW .out .
2518.xH 2 Archives and Libraries
2519.LP
2520.UX
2521and Sprite allow you to merge files into an archive using the
2522.CW ar
2523command. Further, if the files are relocatable object files, you can
2524run
2525.CW ranlib
2526on the archive and get yourself a library that you can link into any
2527program you want. The main problem with archives is they double the
2528space you need to store the archived files, since there's one copy in
2529the archive and one copy out by itself. The problem with libraries is
2530you usually think of them as
2531.CW -lm
2532rather than
2533.CW /usr/lib/libm.a
2534and the linker thinks they're out-of-date if you so much as look at
2535them.
2536.LP
2537Make solves the problem with archives by allowing you to tell it to
2538examine the files in the archives (so you can remove the individual
2539files without having to regenerate them later). To handle the problem
2540with libraries, Make adds an additional way of deciding if a library
2541is out-of-date:
2542.IP \(bu 2
2543If the table of contents is older than the library, or is missing, the
2544library is out-of-date.
2545.LP
2546A library is any target that looks like
2547.CW \-l name'' ``
2548or that ends in a suffix that was marked as a library using the
2549.CW .LIBS
2550target.
2551.CW .a
2552is so marked in the system makefile.
2553.LP
2554Members of an archive are specified as
2555``\fIarchive\fP(\fImember\fP[ \fImember\fP...])''.
2556Thus
2557.CW libdix.a(window.o) '' ``'
2558specifies the file
2559.CW window.o
2560in the archive
2561.CW libdix.a .
2562You may also use wildcards to specify the members of the archive. Just
2563remember that most the wildcard characters will only find
2564.I existing
2565files.
2566.LP
2567A file that is a member of an archive is treated specially. If the
2568file doesn't exist, but it is in the archive, the modification time
2569recorded in the archive is used for the file when determining if the
2570file is out-of-date. When figuring out how to make an archived member target
2571(not the file itself, but the file in the archive \*- the
2572\fIarchive\fP(\fImember\fP) target), special care is
2573taken with the transformation rules, as follows:
2574.IP \(bu 2
2575\&\fIarchive\fP(\fImember\fP) is made to depend on \fImember\fP.
2576.IP \(bu 2
2577The transformation from the \fImember\fP's suffix to the
2578\fIarchive\fP's suffix is applied to the \fIarchive\fP(\fImember\fP) target.
2579.IP \(bu 2
2580The \fIarchive\fP(\fImember\fP)'s
2581.CW .TARGET
2582variable is set to the name of the \fImember\fP if \fImember\fP is
2583actually a target, or the path to the member file if \fImember\fP is
2584only a source.
2585.IP \(bu 2
2586The
2587.CW .ARCHIVE
2588variable for the \fIarchive\fP(\fImember\fP) target is set to the name
2589of the \fIarchive\fP.
2590.Ix 0 def variable local .ARCHIVE
2591.Ix 0 def .ARCHIVE
2592.IP \(bu 2
2593The
2594.CW .MEMBER
2595variable is set to the actual string inside the parentheses. In most
2596cases, this will be the same as the
2597.CW .TARGET
2598variable.
2599.Ix 0 def variable local .MEMBER
2600.Ix 0 def .MEMBER
2601.IP \(bu 2
2602The \fIarchive\fP(\fImember\fP)'s place in the local variables of the
2603targets that depend on it is taken by the value of its
2604.CW .TARGET
2605variable.
2606.LP
2607Thus, a program library could be created with the following makefile:
2608.DS
2609\&.o.a :
2610 ...
2611 rm -f $(.TARGET:T)
2612OBJS = obj1.o obj2.o obj3.o
2613libprog.a : libprog.a($(OBJS))
2614 ar cru $(.TARGET) $(.OODATE)
2615 ranlib $(.TARGET)
2616.DE
2617This will cause the three object files to be compiled (if the
2618corresponding source files were modified after the object file or, if
2619that doesn't exist, the archived object file), the out-of-date ones
2620archived in
2621.CW libprog.a ,
2622a table of contents placed in the archive and the newly-archived
2623object files to be removed.
2624.LP
2625All this is used in the
2626.CW makelib.mk
2627system makefile to create a single library with ease. This makefile
2628looks like this:
2629.DS
2630.SM
2631#
2632# Rules for making libraries. The object files that make up the library are
2633# removed once they are archived.
2634#
2635# To make several libraries in parallel, you should define the variable
2636# "many_libraries". This will serialize the invocations of ranlib.
2637#
2638# To use, do something like this:
2639#
2640# OBJECTS = <files in the library>
2641#
2642# fish.a: fish.a($(OBJECTS)) MAKELIB
2643#
2644#
2645
2646#ifndef _MAKELIB_MK
2647_MAKELIB_MK =
2648
2649#include <po.mk>
2650
2651\&.po.a .o.a :
2652 ...
2653 rm -f $(.MEMBER)
2654
2655ARFLAGS ?= crl
2656
2657#
2658# Re-archive the out-of-date members and recreate the library's table of
2659# contents using ranlib. If many_libraries is defined, put the ranlib off
2660# til the end so many libraries can be made at once.
2661#
2662MAKELIB : .USE .PRECIOUS
2663 ar $(ARFLAGS) $(.TARGET) $(.OODATE)
2664#ifndef no_ranlib
2665# ifdef many_libraries
2666 ...
2667# endif many_libraries
2668 ranlib $(.TARGET)
2669#endif no_ranlib
2670
2671#endif _MAKELIB_MK
2672.DE
2673.xH 2 On the Condition...
2674.Rd 1
2675.LP
2676Like the C compiler before it, Make allows you to configure the makefile,
2677based on the current environment, using conditional statements. A
2678conditional looks like this:
2679.DS
2680#if \fIboolean expression\fP
2681\fIlines\fP
2682#elif \fIanother boolean expression\fP
2683\fImore lines\fP
2684#else
2685\fIstill more lines\fP
2686#endif
2687.DE
2688They may be nested to a maximum depth of 30 and may occur anywhere
2689(except in a comment, of course). The
2690.CW # '' ``
2691must the very first character on the line.
2692.LP
2693Each
2694.I "boolean expression"
2695is made up of terms that look like function calls, the standard C
2696boolean operators
2697.CW && ,
2698.CW || ,
2699and
2700.CW ! ,
2701and the standard relational operators
2702.CW == ,
2703.CW != ,
2704.CW > ,
2705.CW >= ,
2706.CW < ,
2707and
2708.CW <= ,
2709with
2710.CW ==
2711and
2712.CW !=
2713being overloaded to allow string comparisons as well.
2714.CW &&
2715represents logical AND;
2716.CW ||
2717is logical OR and
2718.CW !
2719is logical NOT. The arithmetic and string operators take precedence
2720over all three of these operators, while NOT takes precedence over
2721AND, which takes precedence over OR. This precedence may be
2722overridden with parentheses, and an expression may be parenthesized to
2723your heart's content. Each term looks like a call on one of four
2724functions:
2725.nr pw 9
2726.Ix 0 def make
2727.Ix 0 def conditional make
2728.Ix 0 def if make
2729.IP make \n(pw
2730The syntax is
2731.CW make( \fItarget\fP\c
2732.CW )
2733where
2734.I target
2735is a target in the makefile. This is true if the given target was
2736specified on the command line, or as the source for a
2737.CW .MAIN
2738target (note that the sources for
2739.CW .MAIN
2740are only used if no targets were given on the command line).
2741.IP defined \n(pw
2742.Ix 0 def defined
2743.Ix 0 def conditional defined
2744.Ix 0 def if defined
2745The syntax is
2746.CW defined( \fIvariable\fP\c
2747.CW )
2748and is true if
2749.I variable
2750is defined. Certain variables are defined in the system makefile that
2751identify the system on which Make is being run.
2752.IP exists \n(pw
2753.Ix 0 def exists
2754.Ix 0 def conditional exists
2755.Ix 0 def if exists
2756The syntax is
2757.CW exists( \fIfile\fP\c
2758.CW )
2759and is true if the file can be found on the global search path (i.e.
2760that defined by
2761.CW .PATH
2762targets, not by
2763.CW .PATH \fIsuffix\fP
2764targets).
2765.IP empty \n(pw
2766.Ix 0 def empty
2767.Ix 0 def conditional empty
2768.Ix 0 def if empty
2769This syntax is much like the others, except the string inside the
2770parentheses is of the same form as you would put between parentheses
2771when expanding a variable, complete with modifiers and everything. The
2772function returns true if the resulting string is empty (NOTE: an undefined
2773variable in this context will cause at the very least a warning
2774message about a malformed conditional, and at the worst will cause the
2775process to stop once it has read the makefile. If you want to check
2776for a variable being defined or empty, use the expression
2777.CW !defined( \fIvar\fP\c ``
2778.CW ") || empty(" \fIvar\fP\c
2779.CW ) ''
2780as the definition of
2781.CW ||
2782will prevent the
2783.CW empty()
2784from being evaluated and causing an error, if the variable is
2785undefined). This can be used to see if a variable contains a given
2786word, for example:
2787.DS
2788#if !empty(\fIvar\fP:M\fIword\fP)
2789.DE
2790.LP
2791The arithmetic and string operators may only be used to test the value
2792of a variable. The lefthand side must contain the variable expansion,
2793while the righthand side contains either a string, enclosed in
2794double-quotes, or a number. The standard C numeric conventions (except
2795for specifying an octal number) apply to both sides. E.g.
2796.DS
2797#if $(OS) == 4.3
2798
2799#if $(MACHINE) == "sun3"
2800
2801#if $(LOAD_ADDR) < 0xc000
2802.DE
2803are all valid conditionals. In addition, the numeric value of a
2804variable can be tested as a boolean as follows:
2805.DS
2806#if $(LOAD)
2807.DE
2808would see if
2809.CW LOAD
2810contains a non-zero value and
2811.DS
2812#if !$(LOAD)
2813.DE
2814would test if
2815.CW LOAD
2816contains a zero value.
2817.LP
2818In addition to the bare
2819.CW #if ,'' ``
2820there are other forms that apply one of the first two functions to each
2821term. They are as follows:
2822.DS
2823 ifdef \fRdefined\fP
2824 ifndef \fR!defined\fP
2825 ifmake \fRmake\fP
2826 ifnmake \fR!make\fP
2827.DE
2828There are also the ``else if'' forms:
2829.CW elif ,
2830.CW elifdef ,
2831.CW elifndef ,
2832.CW elifmake ,
2833and
2834.CW elifnmake .
2835.LP
2836For instance, if you wish to create two versions of a program, one of which
2837is optimized (the production version) and the other of which is for debugging
2838(has symbols for dbx), you have two choices: you can create two
2839makefiles, one of which uses the
2840.CW \-g
2841flag for the compilation, while the other uses the
2842.CW \-O
2843flag, or you can use another target (call it
2844.CW debug )
2845to create the debug version. The construct below will take care of
2846this for you. I have also made it so defining the variable
2847.CW DEBUG
2848(say with
2849.CW "make -D DEBUG" )
2850will also cause the debug version to be made.
2851.DS
2852#if defined(DEBUG) || make(debug)
2853CFLAGS += -g
2854#else
2855CFLAGS += -O
2856#endif
2857.DE
2858There are, of course, problems with this approach. The most glaring
2859annoyance is that if you want to go from making a debug version to
2860making a production version, you have to remove all the object files,
2861or you will get some optimized and some debug versions in the same
2862program. Another annoyance is you have to be careful not to make two
2863targets that ``conflict'' because of some conditionals in the
2864makefile. For instance
2865.DS
2866#if make(print)
2867FORMATTER = ditroff -Plaser_printer
2868#endif
2869#if make(draft)
2870FORMATTER = nroff -Pdot_matrix_printer
2871#endif
2872.DE
2873would wreak havoc if you tried
2874.CW "make draft print" '' ``
2875since you would use the same formatter for each target. As I said,
2876this all gets somewhat complicated.
2877.xH 2 Compatibility
2878.Ix 0 ref compatibility
2879.LP
2880There are three (well, 3 \(12) levels of backwards-compatibility built
2881into Make. Most makefiles will need none at all. Some may need a
2882little bit of work to operate correctly when run in parallel. Each
2883level encompasses the previous levels (e.g.
2884.B \-B
2885(one shell per command) implies
2886.B \-V )
2887The three levels are described in the following three sections.
2888.xH 3 DEFCON 3 \*- Variable Expansion
2889.Ix 0 ref compatibility
2890.LP
2891As noted before, Make will not expand a variable unless it knows of a
2892value for it. This can cause problems for makefiles that expect to
2893leave variables undefined except in special circumstances (e.g. if
2894more flags need to be passed to the C compiler or the output from a
2895text processor should be sent to a different printer). If the
2896variables are enclosed in curly braces
2897.CW ${PRINTER} ''), (``
2898the shell will let them pass. If they are enclosed in parentheses,
2899however, the shell will declare a syntax error and the make will come
2900to a grinding halt.
2901.LP
2902You have two choices: change the makefile to define the variables
2903(their values can be overridden on the command line, since that's
2904where they would have been set if you used Make, anyway) or always give the
2905.B \-V
2906flag (this can be done with the
2907.CW .MAKEFLAGS
2908target, if you want).
2909.xH 3 DEFCON 2 \*- The Number of the Beast
2910.Ix 0 ref compatibility
2911.LP
2912Then there are the makefiles that expect certain commands, such as
2913changing to a different directory, to not affect other commands in a
2914target's creation script. You can solve this is either by going
2915back to executing one shell per command (which is what the
2916.B \-B
2917flag forces Make to do), which slows the process down a good bit and
2918requires you to use semicolons and escaped newlines for shell constructs, or
2919by changing the makefile to execute the offending command(s) in a subshell
2920(by placing the line inside parentheses), like so:
2921.DS
2922install :: .MAKE
2923 (cd src; $(.MAKE) install)
2924 (cd lib; $(.MAKE) install)
2925 (cd man; $(.MAKE) install)
2926.DE
2927.Ix 0 ref operator double-colon
2928.Ix 0 ref variable global .MAKE
2929.Ix 0 ref .MAKE
2930.Ix 0 ref .MAKE
2931.Ix 0 ref attribute .MAKE
2932This will always execute the three makes (even if the
2933.B \-n
2934flag was given) because of the combination of the ``::'' operator and
2935the
2936.CW .MAKE
2937attribute. Each command will change to the proper directory to perform
2938the install, leaving the main shell in the directory in which it started.
2939.xH 3 "DEFCON 1 \*- Imitation is the Not the Highest Form of Flattery"
2940.Ix 0 ref compatibility
2941.LP
2942The final category of makefile is the one where every command requires
2943input, the dependencies are incompletely specified, or you simply
2944cannot create more than one target at a time, as mentioned earlier. In
2945addition, you may not have the time or desire to upgrade the makefile
2946to run smoothly with Make. If you are the conservative sort, this is
2947the compatibility mode for you. It is entered by giving Make
2948the
2949.B \-B
2950flag.
2951This
2952includes:
2953.IP \(bu 2
2954No parallel execution.
2955.IP \(bu 2
2956Targets are made in the exact order specified by the makefile. The
2957sources for each target are made in strict left-to-right order, etc.
2958.IP \(bu 2
2959A single Bourne shell is used to execute each command, thus the
2960shell's
2961.CW $$
2962variable is useless, changing directories doesn't work across command
2963lines, etc.
2964.IP \(bu 2
2965If no special characters exist in a command line, Make will break the
2966command into words itself and execute the command directly, without
2967executing a shell first. The characters that cause Make to execute a
2968shell are:
2969.CW # ,
2970.CW = ,
2971.CW | ,
2972.CW ^ ,
2973.CW ( ,
2974.CW ) ,
2975.CW { ,
2976.CW } ,
2977.CW ; ,
2978.CW & ,
2979.CW < ,
2980.CW > ,
2981.CW * ,
2982.CW ? ,
2983.CW [ ,
2984.CW ] ,
2985.CW : ,
2986.CW $ ,
2987.CW ` ,
2988and
2989.CW \e .
2990You should notice that these are all the characters that are given
2991special meaning by the shell (except
2992.CW '
2993and
2994.CW " ,
2995which Make deals with all by its lonesome).
2996.xH 2 The Way Things Work
2997.LP
2998When Make reads the makefile, it parses sources and targets into
2999nodes in a graph. The graph is directed only in the sense that Make
3000knows which way is up. Each node contains not only links to all its
3001parents and children (the nodes that depend on it and those on which
3002it depends, respectively), but also a count of the number of its
3003children that have already been processed.
3004.LP
3005The most important thing to know about how Make uses this graph is
3006that the traversal is breadth-first and occurs in two passes.
3007.LP
3008After Make has parsed the makefile, it begins with the nodes the user
3009has told it to make (either on the command line, or via a
3010.CW .MAIN
3011target, or by the target being the first in the file not labeled with
3012the
3013.CW .NOTMAIN
3014attribute) placed in a queue. It continues to take the node off the
3015front of the queue, mark it as something that needs to be made, pass
3016the node to
3017.CW Suff_FindDeps
3018(mentioned earlier) to find any implicit sources for the node, and
3019place all the node's children that have yet to be marked at the end of
3020the queue. If any of the children is a
3021.CW .USE
3022rule, its attributes are applied to the parent, then its commands are
3023appended to the parent's list of commands and its children are linked
3024to its parent. The parent's unmade children counter is then decremented
3025(since the
3026.CW .USE
3027node has been processed). You will note that this allows a
3028.CW .USE
3029node to have children that are
3030.CW .USE
3031nodes and the rules will be applied in sequence.
3032If the node has no children, it is placed at the end of
3033another queue to be examined in the second pass. This process
3034continues until the first queue is empty.
3035.LP
3036At this point, all the leaves of the graph are in the examination
3037queue. Make removes the node at the head of the queue and sees if it
3038is out-of-date. If it is, it is passed to a function that will execute
3039the commands for the node asynchronously. When the commands have
3040completed, all the node's parents have their unmade children counter
3041decremented and, if the counter is then 0, they are placed on the
3042examination queue. Likewise, if the node is up-to-date. Only those
3043parents that were marked on the downward pass are processed in this
3044way. Thus Make traverses the graph back up to the nodes the user
3045instructed it to create. When the examination queue is empty and no
3046shells are running to create a target, Make is finished.
3047.LP
3048Once all targets have been processed, Make executes the commands
3049attached to the
3050.CW .END
3051target, either explicitly or through the use of an ellipsis in a shell
3052script. If there were no errors during the entire process but there
3053are still some targets unmade (Make keeps a running count of how many
3054targets are left to be made), there is a cycle in the graph. Make does
3055a depth-first traversal of the graph to find all the targets that
3056weren't made and prints them out one by one.
3057.xH 1 Answers to Exercises
3058.IP (3.1)
3059This is something of a trick question, for which I apologize. The
3060trick comes from the UNIX definition of a suffix, which Make doesn't
3061necessarily share. You will have noticed that all the suffixes used in
3062this tutorial (and in UNIX in general) begin with a period
3063.CW .ms , (
3064.CW .c ,
3065etc.). Now, Make's idea of a suffix is more like English's: it's the
3066characters at the end of a word. With this in mind, one possible
3067.Ix 0 def suffix
3068solution to this problem goes as follows:
3069.DS I
3070\&.SUFFIXES : ec.exe .exe ec.obj .obj .asm
3071ec.objec.exe .obj.exe :
3072 link -o $(.TARGET) $(.IMPSRC)
3073\&.asmec.obj :
3074 asm -o $(.TARGET) -DDO_ERROR_CHECKING $(.IMPSRC)
3075\&.asm.obj :
3076 asm -o $(.TARGET) $(.IMPSRC)
3077.DE
3078.IP (3.2)
3079The trick to this one lies in the ``:='' variable-assignment operator
3080and the ``:S'' variable-expansion modifier.
3081.Ix 0 ref variable assignment expanded
3082.Ix 0 ref variable expansion modified
3083.Ix 0 ref modifier substitute
3084.Ix 0 ref :S
3085.Ix 0 ref :=
3086Basically what you want is to take the pointer variable, so to speak,
3087and transform it into an invocation of the variable at which it
3088points. You might try something like
3089.DS I
3090$(PTR:S/^/\e$(/:S/$/))
3091.DE
3092which places
3093.CW $( '' ``
3094at the front of the variable name and
3095.CW ) '' ``
3096at the end, thus transforming
3097.CW VAR ,'' ``
3098for example, into
3099.CW $(VAR) ,'' ``
3100which is just what we want. Unfortunately (as you know if you've tried
3101it), since, as it says in the hint, Make does no further substitution
3102on the result of a modified expansion, that's \fIall\fP you get. The
3103solution is to make use of ``:='' to place that string into yet
3104another variable, then invoke the other variable directly:
3105.DS I
3106*PTR := $(PTR:S/^/\e$(/:S/$/)/)
3107.DE
3108You can then use
3109.CW $(*PTR) '' ``
3110to your heart's content.
3111.de Gp
3112.XP
3113\&\fB\\$1:\fP
3114..
3115.xH 1 Glossary of Jargon
3116.Gp "attribute"
3117A property given to a target that causes Make to treat it differently.
3118.Gp "command script"
3119The lines immediately following a dependency line that specify
3120commands to execute to create each of the targets on the dependency
3121line. Each line in the command script must begin with a tab.
3122.Gp "command-line variable"
3123A variable defined in an argument when Make is first executed.
3124Overrides all assignments to the same variable name in the makefile.
3125.Gp "conditional"
3126A construct much like that used in C that allows a makefile to be
3127configured on the fly based on the local environment, or on what is being
3128made by that invocation of Make.
3129.Gp "creation script"
3130Commands used to create a target. See ``command script.''
3131.Gp "dependency"
3132The relationship between a source and a target. This comes in three
3133flavors, as indicated by the operator between the target and the
3134source. `:' gives a straight time-wise dependency (if the target is
3135older than the source, the target is out-of-date), while `!' provides
3136simply an ordering and always considers the target out-of-date. `::'
3137is much like `:', save it creates multiple instances of a target each
3138of which depends on its own list of sources.
3139.Gp "dynamic source"
3140This refers to a source that has a local variable invocation in it. It
3141allows a single dependency line to specify a different source for each
3142target on the line.
3143.Gp "global variable"
3144Any variable defined in a makefile. Takes precedence over variables
3145defined in the environment, but not over command-line or local variables.
3146.Gp "input graph"
3147What Make constructs from a makefile. Consists of nodes made of the
3148targets in the makefile, and the links between them (the
3149dependencies). The links are directed (from source to target) and
3150there may not be any cycles (loops) in the graph.
3151.Gp "local variable"
3152A variable defined by Make visible only in a target's shell script.
3153There are seven local variables, not all of which are defined for
3154every target:
3155.CW .TARGET ,
3156.CW .ALLSRC ,
3157.CW .OODATE ,
3158.CW .PREFIX ,
3159.CW .IMPSRC ,
3160.CW .ARCHIVE ,
3161and
3162.CW .MEMBER .
3163.CW .TARGET ,
3164.CW .PREFIX ,
3165.CW .ARCHIVE ,
3166and
3167.CW .MEMBER
3168may be used on dependency lines to create ``dynamic sources.''
3169.Gp "makefile"
3170A file that describes how a system is built. If you don't know what it
3171is after reading this tutorial.\|.\|.\|.
3172.Gp "modifier"
3173A letter, following a colon, used to alter how a variable is expanded.
3174It has no effect on the variable itself.
3175.Gp "operator"
3176What separates a source from a target (on a dependency line) and specifies
3177the relationship between the two. There are three:
3178.CW : ', `
3179.CW :: ', `
3180and
3181.CW ! '. `
3182.Gp "search path"
3183A list of directories in which a file should be sought. Make's view
3184of the contents of directories in a search path does not change once
3185the makefile has been read. A file is sought on a search path only if
3186it is exclusively a source.
3187.Gp "shell"
3188A program to which commands are passed in order to create targets.
3189.Gp "source"
3190Anything to the right of an operator on a dependency line. Targets on
3191the dependency line are usually created from the sources.
3192.Gp "special target"
3193A target that causes Make to do special things when it's encountered.
3194.Gp "suffix"
3195The tail end of a file name. Usually begins with a period,
3196.CW .c
3197or
3198.CW .ms ,
3199e.g.
3200.Gp "target"
3201A word to the left of the operator on a dependency line. More
3202generally, any file that Make might create. A file may be (and often
3203is) both a target and a source (what it is depends on how Make is
3204looking at it at the time \*- sort of like the wave/particle duality
3205of light, you know).
3206.Gp "transformation rule"
3207A special construct in a makefile that specifies how to create a file
3208of one type from a file of another, as indicated by their suffixes.
3209.Gp "variable expansion"
3210The process of substituting the value of a variable for a reference to
3211it. Expansion may be altered by means of modifiers.
3212.Gp "variable"
3213A place in which to store text that may be retrieved later. Also used
3214to define the local environment. Conditionals exist that test whether
3215a variable is defined or not.
3216.bp
3217.\" Output table of contents last, with an entry for the index, making
3218.\" sure to save and restore the last real page number for the index...
3219.nr @n \n(PN+1
3220.\" We are not generating an index
3221.\" .XS \n(@n
3222.\" Index
3223.\" .XE
3224.nr %% \n%
3225.PX
3226.nr % \n(%%