make-stds.texi 46 KB

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