1 | @comment This file is included by both standards.texi and make.texinfo. |
---|
2 | @comment It was broken out of standards.texi on 1/6/93 by roland. |
---|
3 | |
---|
4 | @node Makefile Conventions |
---|
5 | @chapter Makefile Conventions |
---|
6 | @comment standards.texi does not print an index, but make.texinfo does. |
---|
7 | @cindex makefile, conventions for |
---|
8 | @cindex conventions for makefiles |
---|
9 | @cindex standards for makefiles |
---|
10 | |
---|
11 | This |
---|
12 | @ifinfo |
---|
13 | node |
---|
14 | @end ifinfo |
---|
15 | @iftex |
---|
16 | @ifset CODESTD |
---|
17 | section |
---|
18 | @end ifset |
---|
19 | @ifclear CODESTD |
---|
20 | chapter |
---|
21 | @end ifclear |
---|
22 | @end iftex |
---|
23 | describes conventions for writing the Makefiles for GNU programs. |
---|
24 | |
---|
25 | @menu |
---|
26 | * Makefile Basics:: General Conventions for Makefiles |
---|
27 | * Utilities in Makefiles:: Utilities in Makefiles |
---|
28 | * Command Variables:: Variables for Specifying Commands |
---|
29 | * Directory Variables:: Variables for Installation Directories |
---|
30 | * Standard Targets:: Standard Targets for Users |
---|
31 | * Install Command Categories:: Three categories of commands in the `install' |
---|
32 | rule: normal, pre-install and post-install. |
---|
33 | @end menu |
---|
34 | |
---|
35 | @node Makefile Basics |
---|
36 | @section General Conventions for Makefiles |
---|
37 | |
---|
38 | Every Makefile should contain this line: |
---|
39 | |
---|
40 | @example |
---|
41 | SHELL = /bin/sh |
---|
42 | @end example |
---|
43 | |
---|
44 | @noindent |
---|
45 | to avoid trouble on systems where the @code{SHELL} variable might be |
---|
46 | inherited from the environment. (This is never a problem with GNU |
---|
47 | @code{make}.) |
---|
48 | |
---|
49 | Different @code{make} programs have incompatible suffix lists and |
---|
50 | implicit rules, and this sometimes creates confusion or misbehavior. So |
---|
51 | it is a good idea to set the suffix list explicitly using only the |
---|
52 | suffixes you need in the particular Makefile, like this: |
---|
53 | |
---|
54 | @example |
---|
55 | .SUFFIXES: |
---|
56 | .SUFFIXES: .c .o |
---|
57 | @end example |
---|
58 | |
---|
59 | @noindent |
---|
60 | The first line clears out the suffix list, the second introduces all |
---|
61 | suffixes which may be subject to implicit rules in this Makefile. |
---|
62 | |
---|
63 | Don't assume that @file{.} is in the path for command execution. When |
---|
64 | you need to run programs that are a part of your package during the |
---|
65 | make, please make sure that it uses @file{./} if the program is built as |
---|
66 | part of the make or @file{$(srcdir)/} if the file is an unchanging part |
---|
67 | of the source code. Without one of these prefixes, the current search |
---|
68 | path is used. |
---|
69 | |
---|
70 | The distinction between @file{./} (the @dfn{build directory}) and |
---|
71 | @file{$(srcdir)/} (the @dfn{source directory}) is important because |
---|
72 | users can build in a separate directory using the @samp{--srcdir} option |
---|
73 | to @file{configure}. A rule of the form: |
---|
74 | |
---|
75 | @smallexample |
---|
76 | foo.1 : foo.man sedscript |
---|
77 | sed -e sedscript foo.man > foo.1 |
---|
78 | @end smallexample |
---|
79 | |
---|
80 | @noindent |
---|
81 | will fail when the build directory is not the source directory, because |
---|
82 | @file{foo.man} and @file{sedscript} are in the the source directory. |
---|
83 | |
---|
84 | When using GNU @code{make}, relying on @samp{VPATH} to find the source |
---|
85 | file will work in the case where there is a single dependency file, |
---|
86 | since the @code{make} automatic variable @samp{$<} will represent the |
---|
87 | source file wherever it is. (Many versions of @code{make} set @samp{$<} |
---|
88 | only in implicit rules.) A Makefile target like |
---|
89 | |
---|
90 | @smallexample |
---|
91 | foo.o : bar.c |
---|
92 | $(CC) -I. -I$(srcdir) $(CFLAGS) -c bar.c -o foo.o |
---|
93 | @end smallexample |
---|
94 | |
---|
95 | @noindent |
---|
96 | should instead be written as |
---|
97 | |
---|
98 | @smallexample |
---|
99 | foo.o : bar.c |
---|
100 | $(CC) -I. -I$(srcdir) $(CFLAGS) -c $< -o $@@ |
---|
101 | @end smallexample |
---|
102 | |
---|
103 | @noindent |
---|
104 | in order to allow @samp{VPATH} to work correctly. When the target has |
---|
105 | multiple dependencies, using an explicit @samp{$(srcdir)} is the easiest |
---|
106 | way to make the rule work well. For example, the target above for |
---|
107 | @file{foo.1} is best written as: |
---|
108 | |
---|
109 | @smallexample |
---|
110 | foo.1 : foo.man sedscript |
---|
111 | sed -e $(srcdir)/sedscript $(srcdir)/foo.man > $@@ |
---|
112 | @end smallexample |
---|
113 | |
---|
114 | GNU distributions usually contain some files which are not source |
---|
115 | files---for example, Info files, and the output from Autoconf, Automake, |
---|
116 | Bison or Flex. Since these files normally appear in the source |
---|
117 | directory, they should always appear in the source directory, not in the |
---|
118 | build directory. So Makefile rules to update them should put the |
---|
119 | updated files in the source directory. |
---|
120 | |
---|
121 | However, if a file does not appear in the distribution, then the |
---|
122 | Makefile should not put it in the source directory, because building a |
---|
123 | program in ordinary circumstances should not modify the source directory |
---|
124 | in any way. |
---|
125 | |
---|
126 | Try to make the build and installation targets, at least (and all their |
---|
127 | subtargets) work correctly with a parallel @code{make}. |
---|
128 | |
---|
129 | @node Utilities in Makefiles |
---|
130 | @section Utilities in Makefiles |
---|
131 | |
---|
132 | Write the Makefile commands (and any shell scripts, such as |
---|
133 | @code{configure}) to run in @code{sh}, not in @code{csh}. Don't use any |
---|
134 | special features of @code{ksh} or @code{bash}. |
---|
135 | |
---|
136 | The @code{configure} script and the Makefile rules for building and |
---|
137 | installation should not use any utilities directly except these: |
---|
138 | |
---|
139 | @c dd find |
---|
140 | @c gunzip gzip md5sum |
---|
141 | @c mkfifo mknod tee uname |
---|
142 | |
---|
143 | @example |
---|
144 | cat cmp cp diff echo egrep expr false grep install-info |
---|
145 | ln ls mkdir mv pwd rm rmdir sed sleep sort tar test touch true |
---|
146 | @end example |
---|
147 | |
---|
148 | The compression program @code{gzip} can be used in the @code{dist} rule. |
---|
149 | |
---|
150 | Stick to the generally supported options for these programs. For |
---|
151 | example, don't use @samp{mkdir -p}, convenient as it may be, because |
---|
152 | most systems don't support it. |
---|
153 | |
---|
154 | It is a good idea to avoid creating symbolic links in makefiles, since a |
---|
155 | few systems don't support them. |
---|
156 | |
---|
157 | The Makefile rules for building and installation can also use compilers |
---|
158 | and related programs, but should do so via @code{make} variables so that the |
---|
159 | user can substitute alternatives. Here are some of the programs we |
---|
160 | mean: |
---|
161 | |
---|
162 | @example |
---|
163 | ar bison cc flex install ld ldconfig lex |
---|
164 | make makeinfo ranlib texi2dvi yacc |
---|
165 | @end example |
---|
166 | |
---|
167 | Use the following @code{make} variables to run those programs: |
---|
168 | |
---|
169 | @example |
---|
170 | $(AR) $(BISON) $(CC) $(FLEX) $(INSTALL) $(LD) $(LDCONFIG) $(LEX) |
---|
171 | $(MAKE) $(MAKEINFO) $(RANLIB) $(TEXI2DVI) $(YACC) |
---|
172 | @end example |
---|
173 | |
---|
174 | When you use @code{ranlib} or @code{ldconfig}, you should make sure |
---|
175 | nothing bad happens if the system does not have the program in question. |
---|
176 | Arrange to ignore an error from that command, and print a message before |
---|
177 | the command to tell the user that failure of this command does not mean |
---|
178 | a problem. (The Autoconf @samp{AC_PROG_RANLIB} macro can help with |
---|
179 | this.) |
---|
180 | |
---|
181 | If you use symbolic links, you should implement a fallback for systems |
---|
182 | that don't have symbolic links. |
---|
183 | |
---|
184 | Additional utilities that can be used via Make variables are: |
---|
185 | |
---|
186 | @example |
---|
187 | chgrp chmod chown mknod |
---|
188 | @end example |
---|
189 | |
---|
190 | It is ok to use other utilities in Makefile portions (or scripts) |
---|
191 | intended only for particular systems where you know those utilities |
---|
192 | exist. |
---|
193 | |
---|
194 | @node Command Variables |
---|
195 | @section Variables for Specifying Commands |
---|
196 | |
---|
197 | Makefiles should provide variables for overriding certain commands, options, |
---|
198 | and so on. |
---|
199 | |
---|
200 | In particular, you should run most utility programs via variables. |
---|
201 | Thus, if you use Bison, have a variable named @code{BISON} whose default |
---|
202 | value is set with @samp{BISON = bison}, and refer to it with |
---|
203 | @code{$(BISON)} whenever you need to use Bison. |
---|
204 | |
---|
205 | File management utilities such as @code{ln}, @code{rm}, @code{mv}, and |
---|
206 | so on, need not be referred to through variables in this way, since users |
---|
207 | don't need to replace them with other programs. |
---|
208 | |
---|
209 | Each program-name variable should come with an options variable that is |
---|
210 | used to supply options to the program. Append @samp{FLAGS} to the |
---|
211 | program-name variable name to get the options variable name---for |
---|
212 | example, @code{BISONFLAGS}. (The names @code{CFLAGS} for the C |
---|
213 | compiler, @code{YFLAGS} for yacc, and @code{LFLAGS} for lex, are |
---|
214 | exceptions to this rule, but we keep them because they are standard.) |
---|
215 | Use @code{CPPFLAGS} in any compilation command that runs the |
---|
216 | preprocessor, and use @code{LDFLAGS} in any compilation command that |
---|
217 | does linking as well as in any direct use of @code{ld}. |
---|
218 | |
---|
219 | If there are C compiler options that @emph{must} be used for proper |
---|
220 | compilation of certain files, do not include them in @code{CFLAGS}. |
---|
221 | Users expect to be able to specify @code{CFLAGS} freely themselves. |
---|
222 | Instead, arrange to pass the necessary options to the C compiler |
---|
223 | independently of @code{CFLAGS}, by writing them explicitly in the |
---|
224 | compilation commands or by defining an implicit rule, like this: |
---|
225 | |
---|
226 | @smallexample |
---|
227 | CFLAGS = -g |
---|
228 | ALL_CFLAGS = -I. $(CFLAGS) |
---|
229 | .c.o: |
---|
230 | $(CC) -c $(CPPFLAGS) $(ALL_CFLAGS) $< |
---|
231 | @end smallexample |
---|
232 | |
---|
233 | Do include the @samp{-g} option in @code{CFLAGS}, because that is not |
---|
234 | @emph{required} for proper compilation. You can consider it a default |
---|
235 | that is only recommended. If the package is set up so that it is |
---|
236 | compiled with GCC by default, then you might as well include @samp{-O} |
---|
237 | in the default value of @code{CFLAGS} as well. |
---|
238 | |
---|
239 | Put @code{CFLAGS} last in the compilation command, after other variables |
---|
240 | containing compiler options, so the user can use @code{CFLAGS} to |
---|
241 | override the others. |
---|
242 | |
---|
243 | @code{CFLAGS} should be used in every invocation of the C compiler, |
---|
244 | both those which do compilation and those which do linking. |
---|
245 | |
---|
246 | Every Makefile should define the variable @code{INSTALL}, which is the |
---|
247 | basic command for installing a file into the system. |
---|
248 | |
---|
249 | Every Makefile should also define the variables @code{INSTALL_PROGRAM} |
---|
250 | and @code{INSTALL_DATA}. (The default for each of these should be |
---|
251 | @code{$(INSTALL)}.) Then it should use those variables as the commands |
---|
252 | for actual installation, for executables and nonexecutables |
---|
253 | respectively. Use these variables as follows: |
---|
254 | |
---|
255 | @example |
---|
256 | $(INSTALL_PROGRAM) foo $(bindir)/foo |
---|
257 | $(INSTALL_DATA) libfoo.a $(libdir)/libfoo.a |
---|
258 | @end example |
---|
259 | |
---|
260 | @noindent |
---|
261 | Always use a file name, not a directory name, as the second argument of |
---|
262 | the installation commands. Use a separate command for each file to be |
---|
263 | installed. |
---|
264 | |
---|
265 | @node Directory Variables |
---|
266 | @section Variables for Installation Directories |
---|
267 | |
---|
268 | Installation directories should always be named by variables, so it is |
---|
269 | easy to install in a nonstandard place. The standard names for these |
---|
270 | variables are described below. They are based on a standard filesystem |
---|
271 | layout; variants of it are used in SVR4, 4.4BSD, Linux, Ultrix v4, and |
---|
272 | other modern operating systems. |
---|
273 | |
---|
274 | These two variables set the root for the installation. All the other |
---|
275 | installation directories should be subdirectories of one of these two, |
---|
276 | and nothing should be directly installed into these two directories. |
---|
277 | |
---|
278 | @table @samp |
---|
279 | @item prefix |
---|
280 | A prefix used in constructing the default values of the variables listed |
---|
281 | below. The default value of @code{prefix} should be @file{/usr/local}. |
---|
282 | When building the complete GNU system, the prefix will be empty and |
---|
283 | @file{/usr} will be a symbolic link to @file{/}. |
---|
284 | (If you are using Autoconf, write it as @samp{@@prefix@@}.) |
---|
285 | |
---|
286 | @item exec_prefix |
---|
287 | A prefix used in constructing the default values of some of the |
---|
288 | variables listed below. The default value of @code{exec_prefix} should |
---|
289 | be @code{$(prefix)}. |
---|
290 | (If you are using Autoconf, write it as @samp{@@exec_prefix@@}.) |
---|
291 | |
---|
292 | Generally, @code{$(exec_prefix)} is used for directories that contain |
---|
293 | machine-specific files (such as executables and subroutine libraries), |
---|
294 | while @code{$(prefix)} is used directly for other directories. |
---|
295 | @end table |
---|
296 | |
---|
297 | Executable programs are installed in one of the following directories. |
---|
298 | |
---|
299 | @table @samp |
---|
300 | @item bindir |
---|
301 | The directory for installing executable programs that users can run. |
---|
302 | This should normally be @file{/usr/local/bin}, but write it as |
---|
303 | @file{$(exec_prefix)/bin}. |
---|
304 | (If you are using Autoconf, write it as @samp{@@bindir@@}.) |
---|
305 | |
---|
306 | @item sbindir |
---|
307 | The directory for installing executable programs that can be run from |
---|
308 | the shell, but are only generally useful to system administrators. This |
---|
309 | should normally be @file{/usr/local/sbin}, but write it as |
---|
310 | @file{$(exec_prefix)/sbin}. |
---|
311 | (If you are using Autoconf, write it as @samp{@@sbindir@@}.) |
---|
312 | |
---|
313 | @item libexecdir |
---|
314 | @comment This paragraph adjusted to avoid overfull hbox --roland 5jul94 |
---|
315 | The directory for installing executable programs to be run by other |
---|
316 | programs rather than by users. This directory should normally be |
---|
317 | @file{/usr/local/libexec}, but write it as @file{$(exec_prefix)/libexec}. |
---|
318 | (If you are using Autoconf, write it as @samp{@@libexecdir@@}.) |
---|
319 | @end table |
---|
320 | |
---|
321 | Data files used by the program during its execution are divided into |
---|
322 | categories in two ways. |
---|
323 | |
---|
324 | @itemize @bullet |
---|
325 | @item |
---|
326 | Some files are normally modified by programs; others are never normally |
---|
327 | modified (though users may edit some of these). |
---|
328 | |
---|
329 | @item |
---|
330 | Some files are architecture-independent and can be shared by all |
---|
331 | machines at a site; some are architecture-dependent and can be shared |
---|
332 | only by machines of the same kind and operating system; others may never |
---|
333 | be shared between two machines. |
---|
334 | @end itemize |
---|
335 | |
---|
336 | This makes for six different possibilities. However, we want to |
---|
337 | discourage the use of architecture-dependent files, aside from object |
---|
338 | files and libraries. It is much cleaner to make other data files |
---|
339 | architecture-independent, and it is generally not hard. |
---|
340 | |
---|
341 | Therefore, here are the variables Makefiles should use to specify |
---|
342 | directories: |
---|
343 | |
---|
344 | @table @samp |
---|
345 | @item datadir |
---|
346 | The directory for installing read-only architecture independent data |
---|
347 | files. This should normally be @file{/usr/local/share}, but write it as |
---|
348 | @file{$(prefix)/share}. |
---|
349 | (If you are using Autoconf, write it as @samp{@@datadir@@}.) |
---|
350 | As a special exception, see @file{$(infodir)} |
---|
351 | and @file{$(includedir)} below. |
---|
352 | |
---|
353 | @item sysconfdir |
---|
354 | The directory for installing read-only data files that pertain to a |
---|
355 | single machine--that is to say, files for configuring a host. Mailer |
---|
356 | and network configuration files, @file{/etc/passwd}, and so forth belong |
---|
357 | here. All the files in this directory should be ordinary ASCII text |
---|
358 | files. This directory should normally be @file{/usr/local/etc}, but |
---|
359 | write it as @file{$(prefix)/etc}. |
---|
360 | (If you are using Autoconf, write it as @samp{@@sysconfdir@@}.) |
---|
361 | |
---|
362 | Do not install executables here in this directory (they probably belong |
---|
363 | in @file{$(libexecdir)} or @file{$(sbindir)}). Also do not install |
---|
364 | files that are modified in the normal course of their use (programs |
---|
365 | whose purpose is to change the configuration of the system excluded). |
---|
366 | Those probably belong in @file{$(localstatedir)}. |
---|
367 | |
---|
368 | @item sharedstatedir |
---|
369 | The directory for installing architecture-independent data files which |
---|
370 | the programs modify while they run. This should normally be |
---|
371 | @file{/usr/local/com}, but write it as @file{$(prefix)/com}. |
---|
372 | (If you are using Autoconf, write it as @samp{@@sharedstatedir@@}.) |
---|
373 | |
---|
374 | @item localstatedir |
---|
375 | The directory for installing data files which the programs modify while |
---|
376 | they run, and that pertain to one specific machine. Users should never |
---|
377 | need to modify files in this directory to configure the package's |
---|
378 | operation; put such configuration information in separate files that go |
---|
379 | in @file{$(datadir)} or @file{$(sysconfdir)}. @file{$(localstatedir)} |
---|
380 | should normally be @file{/usr/local/var}, but write it as |
---|
381 | @file{$(prefix)/var}. |
---|
382 | (If you are using Autoconf, write it as @samp{@@localstatedir@@}.) |
---|
383 | |
---|
384 | @item libdir |
---|
385 | The directory for object files and libraries of object code. Do not |
---|
386 | install executables here, they probably ought to go in @file{$(libexecdir)} |
---|
387 | instead. The value of @code{libdir} should normally be |
---|
388 | @file{/usr/local/lib}, but write it as @file{$(exec_prefix)/lib}. |
---|
389 | (If you are using Autoconf, write it as @samp{@@libdir@@}.) |
---|
390 | |
---|
391 | @item infodir |
---|
392 | The directory for installing the Info files for this package. By |
---|
393 | default, it should be @file{/usr/local/info}, but it should be written |
---|
394 | as @file{$(prefix)/info}. |
---|
395 | (If you are using Autoconf, write it as @samp{@@infodir@@}.) |
---|
396 | |
---|
397 | @item lispdir |
---|
398 | The directory for installing any Emacs Lisp files in this package. By |
---|
399 | default, it should be @file{/usr/local/share/emacs/site-lisp}, but it |
---|
400 | should be written as @file{$(prefix)/share/emacs/site-lisp}. |
---|
401 | |
---|
402 | If you are using Autoconf, write the default as @samp{@@lispdir@@}. |
---|
403 | In order to make @samp{@@lispdir@@} work, you need the following lines |
---|
404 | in your @file{configure.in} file: |
---|
405 | |
---|
406 | @example |
---|
407 | lispdir='$@{datadir@}/emacs/site-lisp' |
---|
408 | AC_SUBST(lispdir) |
---|
409 | @end example |
---|
410 | |
---|
411 | @item includedir |
---|
412 | @c rewritten to avoid overfull hbox --roland |
---|
413 | The directory for installing header files to be included by user |
---|
414 | programs with the C @samp{#include} preprocessor directive. This |
---|
415 | should normally be @file{/usr/local/include}, but write it as |
---|
416 | @file{$(prefix)/include}. |
---|
417 | (If you are using Autoconf, write it as @samp{@@includedir@@}.) |
---|
418 | |
---|
419 | Most compilers other than GCC do not look for header files in directory |
---|
420 | @file{/usr/local/include}. So installing the header files this way is |
---|
421 | only useful with GCC. Sometimes this is not a problem because some |
---|
422 | libraries are only really intended to work with GCC. But some libraries |
---|
423 | are intended to work with other compilers. They should install their |
---|
424 | header files in two places, one specified by @code{includedir} and one |
---|
425 | specified by @code{oldincludedir}. |
---|
426 | |
---|
427 | @item oldincludedir |
---|
428 | The directory for installing @samp{#include} header files for use with |
---|
429 | compilers other than GCC. This should normally be @file{/usr/include}. |
---|
430 | (If you are using Autoconf, you can write it as @samp{@@oldincludedir@@}.) |
---|
431 | |
---|
432 | The Makefile commands should check whether the value of |
---|
433 | @code{oldincludedir} is empty. If it is, they should not try to use |
---|
434 | it; they should cancel the second installation of the header files. |
---|
435 | |
---|
436 | A package should not replace an existing header in this directory unless |
---|
437 | the header came from the same package. Thus, if your Foo package |
---|
438 | provides a header file @file{foo.h}, then it should install the header |
---|
439 | file in the @code{oldincludedir} directory if either (1) there is no |
---|
440 | @file{foo.h} there or (2) the @file{foo.h} that exists came from the Foo |
---|
441 | package. |
---|
442 | |
---|
443 | To tell whether @file{foo.h} came from the Foo package, put a magic |
---|
444 | string in the file---part of a comment---and @code{grep} for that string. |
---|
445 | @end table |
---|
446 | |
---|
447 | Unix-style man pages are installed in one of the following: |
---|
448 | |
---|
449 | @table @samp |
---|
450 | @item mandir |
---|
451 | The top-level directory for installing the man pages (if any) for this |
---|
452 | package. It will normally be @file{/usr/local/man}, but you should |
---|
453 | write it as @file{$(prefix)/man}. |
---|
454 | (If you are using Autoconf, write it as @samp{@@mandir@@}.) |
---|
455 | |
---|
456 | @item man1dir |
---|
457 | The directory for installing section 1 man pages. Write it as |
---|
458 | @file{$(mandir)/man1}. |
---|
459 | @item man2dir |
---|
460 | The directory for installing section 2 man pages. Write it as |
---|
461 | @file{$(mandir)/man2} |
---|
462 | @item @dots{} |
---|
463 | |
---|
464 | @strong{Don't make the primary documentation for any GNU software be a |
---|
465 | man page. Write a manual in Texinfo instead. Man pages are just for |
---|
466 | the sake of people running GNU software on Unix, which is a secondary |
---|
467 | application only.} |
---|
468 | |
---|
469 | @item manext |
---|
470 | The file name extension for the installed man page. This should contain |
---|
471 | a period followed by the appropriate digit; it should normally be @samp{.1}. |
---|
472 | |
---|
473 | @item man1ext |
---|
474 | The file name extension for installed section 1 man pages. |
---|
475 | @item man2ext |
---|
476 | The file name extension for installed section 2 man pages. |
---|
477 | @item @dots{} |
---|
478 | Use these names instead of @samp{manext} if the package needs to install man |
---|
479 | pages in more than one section of the manual. |
---|
480 | @end table |
---|
481 | |
---|
482 | And finally, you should set the following variable: |
---|
483 | |
---|
484 | @table @samp |
---|
485 | @item srcdir |
---|
486 | The directory for the sources being compiled. The value of this |
---|
487 | variable is normally inserted by the @code{configure} shell script. |
---|
488 | (If you are using Autconf, use @samp{srcdir = @@srcdir@@}.) |
---|
489 | @end table |
---|
490 | |
---|
491 | For example: |
---|
492 | |
---|
493 | @smallexample |
---|
494 | @c I have changed some of the comments here slightly to fix an overfull |
---|
495 | @c hbox, so the make manual can format correctly. --roland |
---|
496 | # Common prefix for installation directories. |
---|
497 | # NOTE: This directory must exist when you start the install. |
---|
498 | prefix = /usr/local |
---|
499 | exec_prefix = $(prefix) |
---|
500 | # Where to put the executable for the command `gcc'. |
---|
501 | bindir = $(exec_prefix)/bin |
---|
502 | # Where to put the directories used by the compiler. |
---|
503 | libexecdir = $(exec_prefix)/libexec |
---|
504 | # Where to put the Info files. |
---|
505 | infodir = $(prefix)/info |
---|
506 | @end smallexample |
---|
507 | |
---|
508 | If your program installs a large number of files into one of the |
---|
509 | standard user-specified directories, it might be useful to group them |
---|
510 | into a subdirectory particular to that program. If you do this, you |
---|
511 | should write the @code{install} rule to create these subdirectories. |
---|
512 | |
---|
513 | Do not expect the user to include the subdirectory name in the value of |
---|
514 | any of the variables listed above. The idea of having a uniform set of |
---|
515 | variable names for installation directories is to enable the user to |
---|
516 | specify the exact same values for several different GNU packages. In |
---|
517 | order for this to be useful, all the packages must be designed so that |
---|
518 | they will work sensibly when the user does so. |
---|
519 | |
---|
520 | @node Standard Targets |
---|
521 | @section Standard Targets for Users |
---|
522 | |
---|
523 | All GNU programs should have the following targets in their Makefiles: |
---|
524 | |
---|
525 | @table @samp |
---|
526 | @item all |
---|
527 | Compile the entire program. This should be the default target. This |
---|
528 | target need not rebuild any documentation files; Info files should |
---|
529 | normally be included in the distribution, and DVI files should be made |
---|
530 | only when explicitly asked for. |
---|
531 | |
---|
532 | By default, the Make rules should compile and link with @samp{-g}, so |
---|
533 | that executable programs have debugging symbols. Users who don't mind |
---|
534 | being helpless can strip the executables later if they wish. |
---|
535 | |
---|
536 | @item install |
---|
537 | Compile the program and copy the executables, libraries, and so on to |
---|
538 | the file names where they should reside for actual use. If there is a |
---|
539 | simple test to verify that a program is properly installed, this target |
---|
540 | should run that test. |
---|
541 | |
---|
542 | Do not strip executables when installing them. Devil-may-care users can |
---|
543 | use the @code{install-strip} target to do that. |
---|
544 | |
---|
545 | If possible, write the @code{install} target rule so that it does not |
---|
546 | modify anything in the directory where the program was built, provided |
---|
547 | @samp{make all} has just been done. This is convenient for building the |
---|
548 | program under one user name and installing it under another. |
---|
549 | |
---|
550 | The commands should create all the directories in which files are to be |
---|
551 | installed, if they don't already exist. This includes the directories |
---|
552 | specified as the values of the variables @code{prefix} and |
---|
553 | @code{exec_prefix}, as well as all subdirectories that are needed. |
---|
554 | One way to do this is by means of an @code{installdirs} target |
---|
555 | as described below. |
---|
556 | |
---|
557 | Use @samp{-} before any command for installing a man page, so that |
---|
558 | @code{make} will ignore any errors. This is in case there are systems |
---|
559 | that don't have the Unix man page documentation system installed. |
---|
560 | |
---|
561 | The way to install Info files is to copy them into @file{$(infodir)} |
---|
562 | with @code{$(INSTALL_DATA)} (@pxref{Command Variables}), and then run |
---|
563 | the @code{install-info} program if it is present. @code{install-info} |
---|
564 | is a program that edits the Info @file{dir} file to add or update the |
---|
565 | menu entry for the given Info file; it is part of the Texinfo package. |
---|
566 | Here is a sample rule to install an Info file: |
---|
567 | |
---|
568 | @comment This example has been carefully formatted for the Make manual. |
---|
569 | @comment Please do not reformat it without talking to roland@gnu.ai.mit.edu. |
---|
570 | @smallexample |
---|
571 | $(infodir)/foo.info: foo.info |
---|
572 | $(POST_INSTALL) |
---|
573 | # There may be a newer info file in . than in srcdir. |
---|
574 | -if test -f foo.info; then d=.; \ |
---|
575 | else d=$(srcdir); fi; \ |
---|
576 | $(INSTALL_DATA) $$d/foo.info $@@; \ |
---|
577 | # Run install-info only if it exists. |
---|
578 | # Use `if' instead of just prepending `-' to the |
---|
579 | # line so we notice real errors from install-info. |
---|
580 | # We use `$(SHELL) -c' because some shells do not |
---|
581 | # fail gracefully when there is an unknown command. |
---|
582 | if $(SHELL) -c 'install-info --version' \ |
---|
583 | >/dev/null 2>&1; then \ |
---|
584 | install-info --dir-file=$(infodir)/dir \ |
---|
585 | $(infodir)/foo.info; \ |
---|
586 | else true; fi |
---|
587 | @end smallexample |
---|
588 | |
---|
589 | When writing the @code{install} target, you must classify all the |
---|
590 | commands into three categories: normal ones, @dfn{pre-installation} |
---|
591 | commands and @dfn{post-installation} commands. @xref{Install Command |
---|
592 | Categories}. |
---|
593 | |
---|
594 | @item uninstall |
---|
595 | Delete all the installed files---the copies that the @samp{install} |
---|
596 | target creates. |
---|
597 | |
---|
598 | This rule should not modify the directories where compilation is done, |
---|
599 | only the directories where files are installed. |
---|
600 | |
---|
601 | The uninstallation commands are divided into three categories, just like |
---|
602 | the installation commands. @xref{Install Command Categories}. |
---|
603 | |
---|
604 | @item install-strip |
---|
605 | Like @code{install}, but strip the executable files while installing |
---|
606 | them. In many cases, the definition of this target can be very simple: |
---|
607 | |
---|
608 | @smallexample |
---|
609 | install-strip: |
---|
610 | $(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' \ |
---|
611 | install |
---|
612 | @end smallexample |
---|
613 | |
---|
614 | Normally we do not recommend stripping an executable unless you are sure |
---|
615 | the program has no bugs. However, it can be reasonable to install a |
---|
616 | stripped executable for actual execution while saving the unstripped |
---|
617 | executable elsewhere in case there is a bug. |
---|
618 | |
---|
619 | @comment The gratuitous blank line here is to make the table look better |
---|
620 | @comment in the printed Make manual. Please leave it in. |
---|
621 | @item clean |
---|
622 | |
---|
623 | Delete all files from the current directory that are normally created by |
---|
624 | building the program. Don't delete the files that record the |
---|
625 | configuration. Also preserve files that could be made by building, but |
---|
626 | normally aren't because the distribution comes with them. |
---|
627 | |
---|
628 | Delete @file{.dvi} files here if they are not part of the distribution. |
---|
629 | |
---|
630 | @item distclean |
---|
631 | Delete all files from the current directory that are created by |
---|
632 | configuring or building the program. If you have unpacked the source |
---|
633 | and built the program without creating any other files, @samp{make |
---|
634 | distclean} should leave only the files that were in the distribution. |
---|
635 | |
---|
636 | @item mostlyclean |
---|
637 | Like @samp{clean}, but may refrain from deleting a few files that people |
---|
638 | normally don't want to recompile. For example, the @samp{mostlyclean} |
---|
639 | target for GCC does not delete @file{libgcc.a}, because recompiling it |
---|
640 | is rarely necessary and takes a lot of time. |
---|
641 | |
---|
642 | @item maintainer-clean |
---|
643 | Delete almost everything from the current directory that can be |
---|
644 | reconstructed with this Makefile. This typically includes everything |
---|
645 | deleted by @code{distclean}, plus more: C source files produced by |
---|
646 | Bison, tags tables, Info files, and so on. |
---|
647 | |
---|
648 | The reason we say ``almost everything'' is that running the command |
---|
649 | @samp{make maintainer-clean} should not delete @file{configure} even if |
---|
650 | @file{configure} can be remade using a rule in the Makefile. More generally, |
---|
651 | @samp{make maintainer-clean} should not delete anything that needs to |
---|
652 | exist in order to run @file{configure} and then begin to build the |
---|
653 | program. This is the only exception; @code{maintainer-clean} should |
---|
654 | delete everything else that can be rebuilt. |
---|
655 | |
---|
656 | The @samp{maintainer-clean} target is intended to be used by a maintainer of |
---|
657 | the package, not by ordinary users. You may need special tools to |
---|
658 | reconstruct some of the files that @samp{make maintainer-clean} deletes. |
---|
659 | Since these files are normally included in the distribution, we don't |
---|
660 | take care to make them easy to reconstruct. If you find you need to |
---|
661 | unpack the full distribution again, don't blame us. |
---|
662 | |
---|
663 | To help make users aware of this, the commands for the special |
---|
664 | @code{maintainer-clean} target should start with these two: |
---|
665 | |
---|
666 | @smallexample |
---|
667 | @@echo 'This command is intended for maintainers to use; it' |
---|
668 | @@echo 'deletes files that may need special tools to rebuild.' |
---|
669 | @end smallexample |
---|
670 | |
---|
671 | @item TAGS |
---|
672 | Update a tags table for this program. |
---|
673 | @c ADR: how? |
---|
674 | |
---|
675 | @item info |
---|
676 | Generate any Info files needed. The best way to write the rules is as |
---|
677 | follows: |
---|
678 | |
---|
679 | @smallexample |
---|
680 | info: foo.info |
---|
681 | |
---|
682 | foo.info: foo.texi chap1.texi chap2.texi |
---|
683 | $(MAKEINFO) $(srcdir)/foo.texi |
---|
684 | @end smallexample |
---|
685 | |
---|
686 | @noindent |
---|
687 | You must define the variable @code{MAKEINFO} in the Makefile. It should |
---|
688 | run the @code{makeinfo} program, which is part of the Texinfo |
---|
689 | distribution. |
---|
690 | |
---|
691 | Normally a GNU distribution comes with Info files, and that means the |
---|
692 | Info files are present in the source directory. Therefore, the Make |
---|
693 | rule for an info file should update it in the source directory. When |
---|
694 | users build the package, ordinarily Make will not update the Info files |
---|
695 | because they will already be up to date. |
---|
696 | |
---|
697 | @item dvi |
---|
698 | Generate DVI files for all Texinfo documentation. |
---|
699 | For example: |
---|
700 | |
---|
701 | @smallexample |
---|
702 | dvi: foo.dvi |
---|
703 | |
---|
704 | foo.dvi: foo.texi chap1.texi chap2.texi |
---|
705 | $(TEXI2DVI) $(srcdir)/foo.texi |
---|
706 | @end smallexample |
---|
707 | |
---|
708 | @noindent |
---|
709 | You must define the variable @code{TEXI2DVI} in the Makefile. It should |
---|
710 | run the program @code{texi2dvi}, which is part of the Texinfo |
---|
711 | distribution.@footnote{@code{texi2dvi} uses @TeX{} to do the real work |
---|
712 | of formatting. @TeX{} is not distributed with Texinfo.} Alternatively, |
---|
713 | write just the dependencies, and allow GNU @code{make} to provide the command. |
---|
714 | |
---|
715 | @item dist |
---|
716 | Create a distribution tar file for this program. The tar file should be |
---|
717 | set up so that the file names in the tar file start with a subdirectory |
---|
718 | name which is the name of the package it is a distribution for. This |
---|
719 | name can include the version number. |
---|
720 | |
---|
721 | For example, the distribution tar file of GCC version 1.40 unpacks into |
---|
722 | a subdirectory named @file{gcc-1.40}. |
---|
723 | |
---|
724 | The easiest way to do this is to create a subdirectory appropriately |
---|
725 | named, use @code{ln} or @code{cp} to install the proper files in it, and |
---|
726 | then @code{tar} that subdirectory. |
---|
727 | |
---|
728 | Compress the tar file file with @code{gzip}. For example, the actual |
---|
729 | distribution file for GCC version 1.40 is called @file{gcc-1.40.tar.gz}. |
---|
730 | |
---|
731 | The @code{dist} target should explicitly depend on all non-source files |
---|
732 | that are in the distribution, to make sure they are up to date in the |
---|
733 | distribution. |
---|
734 | @ifset CODESTD |
---|
735 | @xref{Releases, , Making Releases}. |
---|
736 | @end ifset |
---|
737 | @ifclear CODESTD |
---|
738 | @xref{Releases, , Making Releases, standards, GNU Coding Standards}. |
---|
739 | @end ifclear |
---|
740 | |
---|
741 | @item check |
---|
742 | Perform self-tests (if any). The user must build the program before |
---|
743 | running the tests, but need not install the program; you should write |
---|
744 | the self-tests so that they work when the program is built but not |
---|
745 | installed. |
---|
746 | @end table |
---|
747 | |
---|
748 | The following targets are suggested as conventional names, for programs |
---|
749 | in which they are useful. |
---|
750 | |
---|
751 | @table @code |
---|
752 | @item installcheck |
---|
753 | Perform installation tests (if any). The user must build and install |
---|
754 | the program before running the tests. You should not assume that |
---|
755 | @file{$(bindir)} is in the search path. |
---|
756 | |
---|
757 | @item installdirs |
---|
758 | It's useful to add a target named @samp{installdirs} to create the |
---|
759 | directories where files are installed, and their parent directories. |
---|
760 | There is a script called @file{mkinstalldirs} which is convenient for |
---|
761 | this; you can find it in the Texinfo package. |
---|
762 | @c It's in /gd/gnu/lib/mkinstalldirs. |
---|
763 | You can use a rule like this: |
---|
764 | |
---|
765 | @comment This has been carefully formatted to look decent in the Make manual. |
---|
766 | @comment Please be sure not to make it extend any further to the right.--roland |
---|
767 | @smallexample |
---|
768 | # Make sure all installation directories (e.g. $(bindir)) |
---|
769 | # actually exist by making them if necessary. |
---|
770 | installdirs: mkinstalldirs |
---|
771 | $(srcdir)/mkinstalldirs $(bindir) $(datadir) \ |
---|
772 | $(libdir) $(infodir) \ |
---|
773 | $(mandir) |
---|
774 | @end smallexample |
---|
775 | |
---|
776 | This rule should not modify the directories where compilation is done. |
---|
777 | It should do nothing but create installation directories. |
---|
778 | @end table |
---|
779 | |
---|
780 | @node Install Command Categories |
---|
781 | @section Install Command Categories |
---|
782 | |
---|
783 | @cindex pre-installation commands |
---|
784 | @cindex post-installation commands |
---|
785 | When writing the @code{install} target, you must classify all the |
---|
786 | commands into three categories: normal ones, @dfn{pre-installation} |
---|
787 | commands and @dfn{post-installation} commands. |
---|
788 | |
---|
789 | Normal commands move files into their proper places, and set their |
---|
790 | modes. They may not alter any files except the ones that come entirely |
---|
791 | from the package they belong to. |
---|
792 | |
---|
793 | Pre-installation and post-installation commands may alter other files; |
---|
794 | in particular, they can edit global configuration files or data bases. |
---|
795 | |
---|
796 | Pre-installation commands are typically executed before the normal |
---|
797 | commands, and post-installation commands are typically run after the |
---|
798 | normal commands. |
---|
799 | |
---|
800 | The most common use for a post-installation command is to run |
---|
801 | @code{install-info}. This cannot be done with a normal command, since |
---|
802 | it alters a file (the Info directory) which does not come entirely and |
---|
803 | solely from the package being installed. It is a post-installation |
---|
804 | command because it needs to be done after the normal command which |
---|
805 | installs the package's Info files. |
---|
806 | |
---|
807 | Most programs don't need any pre-installation commands, but we have the |
---|
808 | feature just in case it is needed. |
---|
809 | |
---|
810 | To classify the commands in the @code{install} rule into these three |
---|
811 | categories, insert @dfn{category lines} among them. A category line |
---|
812 | specifies the category for the commands that follow. |
---|
813 | |
---|
814 | A category line consists of a tab and a reference to a special Make |
---|
815 | variable, plus an optional comment at the end. There are three |
---|
816 | variables you can use, one for each category; the variable name |
---|
817 | specifies the category. Category lines are no-ops in ordinary execution |
---|
818 | because these three Make variables are normally undefined (and you |
---|
819 | @emph{should not} define them in the makefile). |
---|
820 | |
---|
821 | Here are the three possible category lines, each with a comment that |
---|
822 | explains what it means: |
---|
823 | |
---|
824 | @smallexample |
---|
825 | $(PRE_INSTALL) # @r{Pre-install commands follow.} |
---|
826 | $(POST_INSTALL) # @r{Post-install commands follow.} |
---|
827 | $(NORMAL_INSTALL) # @r{Normal commands follow.} |
---|
828 | @end smallexample |
---|
829 | |
---|
830 | If you don't use a category line at the beginning of the @code{install} |
---|
831 | rule, all the commands are classified as normal until the first category |
---|
832 | line. If you don't use any category lines, all the commands are |
---|
833 | classified as normal. |
---|
834 | |
---|
835 | These are the category lines for @code{uninstall}: |
---|
836 | |
---|
837 | @smallexample |
---|
838 | $(PRE_UNINSTALL) # @r{Pre-uninstall commands follow.} |
---|
839 | $(POST_UNINSTALL) # @r{Post-uninstall commands follow.} |
---|
840 | $(NORMAL_UNINSTALL) # @r{Normal commands follow.} |
---|
841 | @end smallexample |
---|
842 | |
---|
843 | Typically, a pre-uninstall command would be used for deleting entries |
---|
844 | from the Info directory. |
---|
845 | |
---|
846 | If the @code{install} or @code{uninstall} target has any dependencies |
---|
847 | which act as subroutines of installation, then you should start |
---|
848 | @emph{each} dependency's commands with a category line, and start the |
---|
849 | main target's commands with a category line also. This way, you can |
---|
850 | ensure that each command is placed in the right category regardless of |
---|
851 | which of the dependencies actually run. |
---|
852 | |
---|
853 | Pre-installation and post-installation commands should not run any |
---|
854 | programs except for these: |
---|
855 | |
---|
856 | @example |
---|
857 | [ basename bash cat chgrp chmod chown cmp cp dd diff echo |
---|
858 | egrep expand expr false fgrep find getopt grep gunzip gzip |
---|
859 | hostname install install-info kill ldconfig ln ls md5sum |
---|
860 | mkdir mkfifo mknod mv printenv pwd rm rmdir sed sort tee |
---|
861 | test touch true uname xargs yes |
---|
862 | @end example |
---|
863 | |
---|
864 | @cindex binary packages |
---|
865 | The reason for distinguishing the commands in this way is for the sake |
---|
866 | of making binary packages. Typically a binary package contains all the |
---|
867 | executables and other files that need to be installed, and has its own |
---|
868 | method of installing them---so it does not need to run the normal |
---|
869 | installation commands. But installing the binary package does need to |
---|
870 | execute the pre-installation and post-installation commands. |
---|
871 | |
---|
872 | Programs to build binary packages work by extracting the |
---|
873 | pre-installation and post-installation commands. Here is one way of |
---|
874 | extracting the pre-installation commands: |
---|
875 | |
---|
876 | @smallexample |
---|
877 | make -n install -o all \ |
---|
878 | PRE_INSTALL=pre-install \ |
---|
879 | POST_INSTALL=post-install \ |
---|
880 | NORMAL_INSTALL=normal-install \ |
---|
881 | | gawk -f pre-install.awk |
---|
882 | @end smallexample |
---|
883 | |
---|
884 | @noindent |
---|
885 | where the file @file{pre-install.awk} could contain this: |
---|
886 | |
---|
887 | @smallexample |
---|
888 | $0 ~ /^\t[ \t]*(normal_install|post_install)[ \t]*$/ @{on = 0@} |
---|
889 | on @{print $0@} |
---|
890 | $0 ~ /^\t[ \t]*pre_install[ \t]*$/ @{on = 1@} |
---|
891 | @end smallexample |
---|
892 | |
---|
893 | The resulting file of pre-installation commands is executed as a shell |
---|
894 | script as part of installing the binary package. |
---|