* NEWS: Whoops! AC_FUNC_STRNLEN isn't obsolescent. Problem
[autoconf.git] / doc / autoconf.texi
blob4f5fcfdee5dc80ec95d24bb1bccc0e09f48a0c29
1 \input texinfo @c -*-texinfo-*-
2 @comment ========================================================
3 @comment %**start of header
4 @setfilename autoconf.info
5 @include version.texi
6 @settitle Autoconf
7 @setchapternewpage odd
8 @ifnothtml
9 @setcontentsaftertitlepage
10 @end ifnothtml
11 @finalout
13 @c @ovar(ARG, DEFAULT)
14 @c -------------------
15 @c The ARG is an optional argument.  To be used for macro arguments in
16 @c their documentation (@defmac).
17 @macro ovar{varname}
18 @r{[}@var{\varname\}@r{]}
19 @end macro
21 @c @dvar(ARG, DEFAULT)
22 @c -------------------
23 @c The ARG is an optional argument, defaulting to DEFAULT.  To be used
24 @c for macro arguments in their documentation (@defmac).
25 @macro dvar{varname, default}
26 @r{[}@var{\varname\} = @samp{\default\}@r{]}
27 @end macro
29 @c Handling the indexes with Texinfo yields several different problems.
31 @c Because we want to drop out the AC_ part of the macro names in the
32 @c printed manual, but not in the other outputs, we need a layer above
33 @c the usual @acindex{} etc.  That's why we first define indexes such as
34 @c acx meant to become the macro @acindex.  First of all, using ``ac_''
35 @c does not work with makeinfo, and using ``ac1'' doesn't work with TeX.
36 @c So use something more regular ``acx''.  Then you finish with a printed
37 @c index saying ``index is not existent''.  Of course: you ought to use
38 @c two letters :(  So you use capitals.
40 @c Second, when defining a macro in the TeX world, following spaces are
41 @c eaten.  But then, since we embed @acxindex commands that use the end
42 @c of line as an end marker, the whole things wrecks itself.  So make
43 @c sure you do *force* an additional end of line, add a ``@c''.
45 @c Finally, you might want to get rid of TeX expansion, using --expand
46 @c with texi2dvi.  But then you wake up an old problem: we use macros
47 @c in @defmac etc. where TeX does perform the expansion, but not makeinfo.
49 @c Define an environment variable index.
50 @defcodeindex ev
51 @c Define an output variable index.
52 @defcodeindex ov
53 @c Define a CPP variable index.
54 @defcodeindex cv
55 @c Define an Autoconf macro index that @defmac doesn't write to.
56 @defcodeindex AC
57 @c Define an Autotest macro index that @defmac doesn't write to.
58 @defcodeindex AT
59 @c Define an M4sugar macro index that @defmac doesn't write to.
60 @defcodeindex MS
61 @c Define an index for *foreign* programs: `mv' etc.  Used for the
62 @c portability sections and so on.
63 @defindex pr
65 @c shortindexflag
66 @c --------------
67 @c Shall we factor AC_ out of the Autoconf macro index etc.?
68 @iftex
69 @set shortindexflag
70 @end iftex
72 @c @acindex{MACRO}
73 @c ---------------
74 @c Registering an AC_\MACRO\.
75 @ifset shortindexflag
76 @macro acindex{macro}
77 @ACindex \macro\
79 @end macro
80 @end ifset
81 @ifclear shortindexflag
82 @macro acindex{macro}
83 @ACindex AC_\macro\
84 @end macro
85 @end ifclear
87 @c @ahindex{MACRO}
88 @c ---------------
89 @c Registering an AH_\MACRO\.
90 @macro ahindex{macro}
91 @ACindex AH_\macro\
93 @end macro
95 @c @asindex{MACRO}
96 @c ---------------
97 @c Registering an AS_\MACRO\.
98 @ifset shortindexflag
99 @macro asindex{macro}
100 @MSindex \macro\
102 @end macro
103 @end ifset
104 @ifclear shortindexflag
105 @macro asindex{macro}
106 @MSindex AS_\macro\
107 @end macro
108 @end ifclear
110 @c @atindex{MACRO}
111 @c ---------------
112 @c Registering an AT_\MACRO\.
113 @ifset shortindexflag
114 @macro atindex{macro}
115 @ATindex \macro\
117 @end macro
118 @end ifset
119 @ifclear shortindexflag
120 @macro atindex{macro}
121 @ATindex AT_\macro\
122 @end macro
123 @end ifclear
125 @c @auindex{MACRO}
126 @c ---------------
127 @c Registering an AU_\MACRO\.
128 @macro auindex{macro}
129 @ACindex AU_\macro\
131 @end macro
133 @c @hdrindex{MACRO}
134 @c ----------------
135 @c Indexing a header.
136 @macro hdrindex{macro}
137 @prindex @file{\macro\}
139 @end macro
141 @c @msindex{MACRO}
142 @c ---------------
143 @c Registering an m4_\MACRO\.
144 @ifset shortindexflag
145 @macro msindex{macro}
146 @MSindex \macro\
148 @end macro
149 @end ifset
150 @ifclear shortindexflag
151 @macro msindex{macro}
152 @MSindex m4_\macro\
153 @end macro
154 @end ifclear
157 @c Define an index for functions: `alloca' etc.  Used for the
158 @c portability sections and so on.  We can't use `fn' (aka `fnindex),
159 @c since `@defmac' goes into it => we'd get all the macros too.
161 @c   FIXME: Aaarg!  It seems there are too many indices for TeX :(
163 @c   ! No room for a new @write .
164 @c   l.112 @defcodeindex fu
166 @c   so don't define yet another one :(  Just put some tags before each
167 @c   @prindex which is actually a @funindex.
169 @c   @defcodeindex fu
172 @c   @c Put the programs and functions into their own index.
173 @c   @syncodeindex fu pr
175 @comment %**end of header
176 @comment ========================================================
178 @copying
180 This manual is for @acronym{GNU} Autoconf
181 (version @value{VERSION}, @value{UPDATED}),
182 a package for creating scripts to configure source code packages using
183 templates and an M4 macro package.
185 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
186 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
188 @quotation
189 Permission is granted to copy, distribute and/or modify this document
190 under the terms of the @acronym{GNU} Free Documentation License,
191 Version 1.2 or any later version published by the Free Software
192 Foundation; with no Invariant Sections, with the Front-Cover texts
193 being ``A @acronym{GNU} Manual,'' and with the Back-Cover Texts as in
194 (a) below.  A copy of the license is included in the section entitled
195 ``@acronym{GNU} Free Documentation License.''
197 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and
198 modify this @acronym{GNU} Manual, like @acronym{GNU} software.  Copies
199 published by the Free Software Foundation raise funds for
200 @acronym{GNU} development.''
201 @end quotation
202 @end copying
206 @dircategory Software development
207 @direntry
208 * Autoconf: (autoconf).         Create source code configuration scripts.
209 @end direntry
211 @dircategory Individual utilities
212 @direntry
213 * autoscan: (autoconf)autoscan Invocation.
214                                 Semi-automatic @file{configure.ac} writing
215 * ifnames: (autoconf)ifnames Invocation.        Listing conditionals in source.
216 * autoconf: (autoconf)autoconf Invocation.
217                                 How to create configuration scripts
218 * autoreconf: (autoconf)autoreconf Invocation.
219                                 Remaking multiple @command{configure} scripts
220 * autoheader: (autoconf)autoheader Invocation.
221                                 How to create configuration templates
222 * autom4te: (autoconf)autom4te Invocation.
223                                 The Autoconf executables backbone
224 * configure: (autoconf)configure Invocation.    Configuring a package.
225 * autoupdate: (autoconf)autoupdate Invocation.
226                                 Automatic update of @file{configure.ac}
227 * config.status: (autoconf)config.status Invocation. Recreating configurations.
228 * testsuite: (autoconf)testsuite Invocation.    Running an Autotest test suite.
229 @end direntry
231 @titlepage
232 @title Autoconf
233 @subtitle Creating Automatic Configuration Scripts
234 @subtitle for version @value{VERSION}, @value{UPDATED}
235 @author David MacKenzie
236 @author Ben Elliston
237 @author Akim Demaille
238 @page
239 @vskip 0pt plus 1filll
240 @insertcopying
241 @end titlepage
243 @contents
246 @ifnottex
247 @node Top
248 @top Autoconf
249 @insertcopying
250 @end ifnottex
252 @c The master menu, created with texinfo-master-menu, goes here.
254 @menu
255 * Introduction::                Autoconf's purpose, strengths, and weaknesses
256 * The GNU Build System::        A set of tools for portable software packages
257 * Making configure Scripts::    How to organize and produce Autoconf scripts
258 * Setup::                       Initialization and output
259 * Existing Tests::              Macros that check for particular features
260 * Writing Tests::               How to write new feature checks
261 * Results::                     What to do with results from feature checks
262 * Programming in M4::           Layers on top of which Autoconf is written
263 * Writing Autoconf Macros::     Adding new macros to Autoconf
264 * Portable Shell::              Shell script portability pitfalls
265 * Portable C and C++::          C and C++ portability pitfalls
266 * Manual Configuration::        Selecting features that can't be guessed
267 * Site Configuration::          Local defaults for @command{configure}
268 * Running configure Scripts::   How to use the Autoconf output
269 * config.status Invocation::    Recreating a configuration
270 * Obsolete Constructs::         Kept for backward compatibility
271 * Using Autotest::              Creating portable test suites
272 * FAQ::                         Frequent Autoconf Questions, with answers
273 * History::                     History of Autoconf
274 * Copying This Manual::         How to make copies of this manual
275 * Indices::                     Indices of symbols, concepts, etc.
277 @detailmenu
278  --- The Detailed Node Listing ---
280 The @acronym{GNU} Build System
282 * Automake::                    Escaping Makefile hell
283 * Gnulib::                      The @acronym{GNU} portability library
284 * Libtool::                     Building libraries portably
285 * Pointers::                    More info on the @acronym{GNU} build system
287 Making @command{configure} Scripts
289 * Writing configure.ac::        What to put in an Autoconf input file
290 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
291 * ifnames Invocation::          Listing the conditionals in source code
292 * autoconf Invocation::         How to create configuration scripts
293 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
295 Writing @file{configure.ac}
297 * Shell Script Compiler::       Autoconf as solution of a problem
298 * Autoconf Language::           Programming in Autoconf
299 * configure.ac Layout::         Standard organization of @file{configure.ac}
301 Initialization and Output Files
303 * Initializing configure::      Option processing etc.
304 * Notices::                     Copyright, version numbers in @command{configure}
305 * Input::                       Where Autoconf should find files
306 * Output::                      Outputting results from the configuration
307 * Configuration Actions::       Preparing the output based on results
308 * Configuration Files::         Creating output files
309 * Makefile Substitutions::      Using output variables in @file{Makefile}s
310 * Configuration Headers::       Creating a configuration header file
311 * Configuration Commands::      Running arbitrary instantiation commands
312 * Configuration Links::         Links depending on the configuration
313 * Subdirectories::              Configuring independent packages together
314 * Default Prefix::              Changing the default installation prefix
316 Substitutions in Makefiles
318 * Preset Output Variables::     Output variables that are always set
319 * Installation Directory Variables::  Other preset output variables
320 * Build Directories::           Supporting multiple concurrent compiles
321 * Automatic Remaking::          Makefile rules for configuring
323 Configuration Header Files
325 * Header Templates::            Input for the configuration headers
326 * autoheader Invocation::       How to create configuration templates
327 * Autoheader Macros::           How to specify CPP templates
329 Existing Tests
331 * Common Behavior::             Macros' standard schemes
332 * Alternative Programs::        Selecting between alternative programs
333 * Files::                       Checking for the existence of files
334 * Libraries::                   Library archives that might be missing
335 * Library Functions::           C library functions that might be missing
336 * Header Files::                Header files that might be missing
337 * Declarations::                Declarations that may be missing
338 * Structures::                  Structures or members that might be missing
339 * Types::                       Types that might be missing
340 * Compilers and Preprocessors::  Checking for compiling programs
341 * System Services::             Operating system services
342 * Posix Variants::              Special kludges for specific Posix variants
343 * Erlang Libraries::            Checking for the existence of Erlang libraries
345 Common Behavior
347 * Standard Symbols::            Symbols defined by the macros
348 * Default Includes::            Includes used by the generic macros
350 Alternative Programs
352 * Particular Programs::         Special handling to find certain programs
353 * Generic Programs::            How to find other programs
355 Library Functions
357 * Function Portability::        Pitfalls with usual functions
358 * Particular Functions::        Special handling to find certain functions
359 * Generic Functions::           How to find other functions
361 Header Files
363 * Header Portability::          Collected knowledge on common headers
364 * Particular Headers::          Special handling to find certain headers
365 * Generic Headers::             How to find other headers
367 Declarations
369 * Particular Declarations::     Macros to check for certain declarations
370 * Generic Declarations::        How to find other declarations
372 Structures
374 * Particular Structures::       Macros to check for certain structure members
375 * Generic Structures::          How to find other structure members
377 Types
379 * Particular Types::            Special handling to find certain types
380 * Generic Types::               How to find other types
382 Compilers and Preprocessors
384 * Specific Compiler Characteristics::  Some portability issues
385 * Generic Compiler Characteristics::  Language independent tests and features
386 * C Compiler::                  Checking its characteristics
387 * C++ Compiler::                Likewise
388 * Objective C Compiler::        Likewise
389 * Erlang Compiler and Interpreter::  Likewise
390 * Fortran Compiler::            Likewise
392 Writing Tests
394 * Language Choice::             Selecting which language to use for testing
395 * Writing Test Programs::       Forging source files for compilers
396 * Running the Preprocessor::    Detecting preprocessor symbols
397 * Running the Compiler::        Detecting language or header features
398 * Running the Linker::          Detecting library features
399 * Runtime::                     Testing for runtime features
400 * Systemology::                 A zoology of operating systems
401 * Multiple Cases::              Tests for several possible values
403 Writing Test Programs
405 * Guidelines::                  General rules for writing test programs
406 * Test Functions::              Avoiding pitfalls in test programs
407 * Generating Sources::          Source program boilerplate
409 Results of Tests
411 * Defining Symbols::            Defining C preprocessor symbols
412 * Setting Output Variables::    Replacing variables in output files
413 * Special Chars in Variables::  Characters to beware of in variables
414 * Caching Results::             Speeding up subsequent @command{configure} runs
415 * Printing Messages::           Notifying @command{configure} users
417 Caching Results
419 * Cache Variable Names::        Shell variables used in caches
420 * Cache Files::                 Files @command{configure} uses for caching
421 * Cache Checkpointing::         Loading and saving the cache file
423 Programming in M4
425 * M4 Quotation::                Protecting macros from unwanted expansion
426 * Using autom4te::              The Autoconf executables backbone
427 * Programming in M4sugar::      Convenient pure M4 macros
428 * Programming in M4sh::         Common shell Constructs
429 * File Descriptor Macros::      File descriptor macros for input and output
431 M4 Quotation
433 * Active Characters::           Characters that change the behavior of M4
434 * One Macro Call::              Quotation and one macro call
435 * Quotation and Nested Macros::  Macros calling macros
436 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
437 * Quadrigraphs::                Another way to escape special characters
438 * Quotation Rule Of Thumb::     One parenthesis, one quote
440 Using @command{autom4te}
442 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
443 * Customizing autom4te::        Customizing the Autoconf package
445 Programming in M4sugar
447 * Redefined M4 Macros::         M4 builtins changed in M4sugar
448 * Looping constructs::          Iteration in M4
449 * Evaluation Macros::           More quotation and evaluation control
450 * Text processing Macros::      String manipulation in M4
451 * Forbidden Patterns::          Catching unexpanded macros
453 Writing Autoconf Macros
455 * Macro Definitions::           Basic format of an Autoconf macro
456 * Macro Names::                 What to call your new macros
457 * Reporting Messages::          Notifying @command{autoconf} users
458 * Dependencies Between Macros::  What to do when macros depend on other macros
459 * Obsoleting Macros::           Warning about old ways of doing things
460 * Coding Style::                Writing Autoconf macros @`a la Autoconf
462 Dependencies Between Macros
464 * Prerequisite Macros::         Ensuring required information
465 * Suggested Ordering::          Warning about possible ordering problems
466 * One-Shot Macros::             Ensuring a macro is called only once
468 Portable Shell Programming
470 * Shellology::                  A zoology of shells
471 * Here-Documents::              Quirks and tricks
472 * File Descriptors::            FDs and redirections
473 * File System Conventions::     File names
474 * Shell Substitutions::         Variable and command expansions
475 * Assignments::                 Varying side effects of assignments
476 * Parentheses::                 Parentheses in shell scripts
477 * Slashes::                     Slashes in shell scripts
478 * Special Shell Variables::     Variables you should not change
479 * Limitations of Builtins::     Portable use of not so portable /bin/sh
480 * Limitations of Usual Tools::  Portable use of portable tools
481 * Limitations of Make::         Portable Makefiles
483 Portable C and C++ Programming
485 * Varieties of Unportability::  How to make your programs unportable
486 * Integer Overflow::            When integers get too large
487 * Null Pointers::               Properties of null pointers
488 * Buffer Overruns::             Subscript errors and the like
489 * Floating Point Portability::  Portable floating-point arithmetic
490 * Exiting Portably::            Exiting and the exit status
492 Manual Configuration
494 * Specifying Names::            Specifying the system type
495 * Canonicalizing::              Getting the canonical system type
496 * Using System Type::           What to do with the system type
498 Site Configuration
500 * Help Formatting::             Customizing @samp{configure --help}
501 * External Software::           Working with other optional software
502 * Package Options::             Selecting optional features
503 * Pretty Help Strings::         Formatting help string
504 * Site Details::                Configuring site details
505 * Transforming Names::          Changing program names when installing
506 * Site Defaults::               Giving @command{configure} local defaults
508 Transforming Program Names When Installing
510 * Transformation Options::      @command{configure} options to transform names
511 * Transformation Examples::     Sample uses of transforming names
512 * Transformation Rules::        @file{Makefile} uses of transforming names
514 Running @command{configure} Scripts
516 * Basic Installation::          Instructions for typical cases
517 * Compilers and Options::       Selecting compilers and optimization
518 * Multiple Architectures::      Compiling for multiple architectures at once
519 * Installation Names::          Installing in different directories
520 * Optional Features::           Selecting optional features
521 * System Type::                 Specifying the system type
522 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
523 * Defining Variables::          Specifying the compiler etc.
524 * configure Invocation::        Changing how @command{configure} runs
526 Obsolete Constructs
528 * Obsolete config.status Use::  Different calling convention
529 * acconfig.h::                  Additional entries in @file{config.h.in}
530 * autoupdate Invocation::       Automatic update of @file{configure.ac}
531 * Obsolete Macros::             Backward compatibility macros
532 * Autoconf 1::                  Tips for upgrading your files
533 * Autoconf 2.13::               Some fresher tips
535 Upgrading From Version 1
537 * Changed File Names::          Files you might rename
538 * Changed Makefiles::           New things to put in @file{Makefile.in}
539 * Changed Macros::              Macro calls you might replace
540 * Changed Results::             Changes in how to check test results
541 * Changed Macro Writing::       Better ways to write your own macros
543 Upgrading From Version 2.13
545 * Changed Quotation::           Broken code which used to work
546 * New Macros::                  Interaction with foreign macros
547 * Hosts and Cross-Compilation::  Bugward compatibility kludges
548 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
549 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
551 Generating Test Suites with Autotest
553 * Using an Autotest Test Suite::  Autotest and the user
554 * Writing testsuite.at::        Autotest macros
555 * testsuite Invocation::        Running @command{testsuite} scripts
556 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
558 Using an Autotest Test Suite
560 * testsuite Scripts::           The concepts of Autotest
561 * Autotest Logs::               Their contents
563 Frequent Autoconf Questions, with answers
565 * Distributing::                Distributing @command{configure} scripts
566 * Why GNU m4::                  Why not use the standard M4?
567 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
568 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
569 * Defining Directories::        Passing @code{datadir} to program
570 * autom4te.cache::              What is it?  Can I remove it?
571 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
573 History of Autoconf
575 * Genesis::                     Prehistory and naming of @command{configure}
576 * Exodus::                      The plagues of M4 and Perl
577 * Leviticus::                   The priestly code of portability arrives
578 * Numbers::                     Growth and contributors
579 * Deuteronomy::                 Approaching the promises of easy configuration
581 Copying This Manual
583 * GNU Free Documentation License::  License for copying this manual
585 Indices
587 * Environment Variable Index::  Index of environment variables used
588 * Output Variable Index::       Index of variables set in output files
589 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
590 * Autoconf Macro Index::        Index of Autoconf macros
591 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
592 * Autotest Macro Index::        Index of Autotest macros
593 * Program & Function Index::    Index of those with portability problems
594 * Concept Index::               General index
596 @end detailmenu
597 @end menu
599 @c ============================================================= Introduction.
601 @node Introduction
602 @chapter Introduction
603 @cindex Introduction
605 @flushright
606 A physicist, an engineer, and a computer scientist were discussing the
607 nature of God.  ``Surely a Physicist,'' said the physicist, ``because
608 early in the Creation, God made Light; and you know, Maxwell's
609 equations, the dual nature of electromagnetic waves, the relativistic
610 consequences@dots{}'' ``An Engineer!,'' said the engineer, ``because
611 before making Light, God split the Chaos into Land and Water; it takes a
612 hell of an engineer to handle that big amount of mud, and orderly
613 separation of solids from liquids@dots{}'' The computer scientist
614 shouted: ``And the Chaos, where do you think it was coming from, hmm?''
616 ---Anonymous
617 @end flushright
618 @c (via Franc,ois Pinard)
620 Autoconf is a tool for producing shell scripts that automatically
621 configure software source code packages to adapt to many kinds of
622 Posix-like systems.  The configuration scripts produced by Autoconf
623 are independent of Autoconf when they are run, so their users do not
624 need to have Autoconf.
626 The configuration scripts produced by Autoconf require no manual user
627 intervention when run; they do not normally even need an argument
628 specifying the system type.  Instead, they individually test for the
629 presence of each feature that the software package they are for might need.
630 (Before each check, they print a one-line message stating what they are
631 checking for, so the user doesn't get too bored while waiting for the
632 script to finish.)  As a result, they deal well with systems that are
633 hybrids or customized from the more common Posix variants.  There is
634 no need to maintain files that list the features supported by each
635 release of each variant of Posix.
637 For each software package that Autoconf is used with, it creates a
638 configuration script from a template file that lists the system features
639 that the package needs or can use.  After the shell code to recognize
640 and respond to a system feature has been written, Autoconf allows it to
641 be shared by many software packages that can use (or need) that feature.
642 If it later turns out that the shell code needs adjustment for some
643 reason, it needs to be changed in only one place; all of the
644 configuration scripts can be regenerated automatically to take advantage
645 of the updated code.
647 The Metaconfig package is similar in purpose to Autoconf, but the
648 scripts it produces require manual user intervention, which is quite
649 inconvenient when configuring large source trees.  Unlike Metaconfig
650 scripts, Autoconf scripts can support cross-compiling, if some care is
651 taken in writing them.
653 Autoconf does not solve all problems related to making portable
654 software packages---for a more complete solution, it should be used in
655 concert with other @acronym{GNU} build tools like Automake and
656 Libtool.  These other tools take on jobs like the creation of a
657 portable, recursive @file{Makefile} with all of the standard targets,
658 linking of shared libraries, and so on.  @xref{The GNU Build System},
659 for more information.
661 Autoconf imposes some restrictions on the names of macros used with
662 @code{#if} in C programs (@pxref{Preprocessor Symbol Index}).
664 Autoconf requires @acronym{GNU} M4 in order to generate the scripts.  It uses
665 features that some versions of M4, including @acronym{GNU} M4 1.3,
666 do not have.  You should use version 1.4.3 or later of @acronym{GNU} M4.
668 @xref{Autoconf 1}, for information about upgrading from version 1.
669 @xref{History}, for the story of Autoconf's development.  @xref{FAQ},
670 for answers to some common questions about Autoconf.
672 See the @uref{http://www.gnu.org/software/autoconf/,
673 Autoconf web page} for up-to-date information, details on the mailing
674 lists, pointers to a list of known bugs, etc.
676 Mail suggestions to @email{autoconf@@gnu.org, the Autoconf mailing
677 list}.  Past suggestions are
678 @uref{http://lists.gnu.org/archive/html/autoconf/, archived}.
680 Mail bug reports to @email{bug-autoconf@@gnu.org, the
681 Autoconf Bugs mailing list}.  Past bug reports are
682 @uref{http://lists.gnu.org/archive/html/bug-autoconf/, archived}.
684 If possible, first check that your bug is
685 not already solved in current development versions, and that it has not
686 been reported yet.  Be sure to include all the needed information and a
687 short @file{configure.ac} that demonstrates the problem.
689 Autoconf's development tree is accessible via anonymous @acronym{CVS}; see the
690 @uref{http://savannah.gnu.org/projects/autoconf/, Autoconf
691 Summary} for details.  Patches relative to the
692 current @acronym{CVS} version can be sent for review to the
693 @email{autoconf-patches@@gnu.org, Autoconf Patches mailing list}.
694 Past patches are
695 @uref{http://lists.gnu.org/@/archive/@/html/@/autoconf-patches/, archived}.
697 Because of its mission, the Autoconf package itself
698 includes only a set of often-used
699 macros that have already demonstrated their usefulness.  Nevertheless,
700 if you wish to share your macros, or find existing ones, see the
701 @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
702 Archive}, which is kindly run by @email{simons@@cryp.to,
703 Peter Simons}.
706 @c ================================================= The GNU Build System
708 @node The GNU Build System
709 @chapter The @acronym{GNU} Build System
710 @cindex GNU build system
712 Autoconf solves an important problem---reliable discovery of
713 system-specific build and runtime information---but this is only one
714 piece of the puzzle for the development of portable software.  To this
715 end, the @acronym{GNU} project has developed a suite of integrated
716 utilities to finish the job Autoconf started: the @acronym{GNU} build
717 system, whose most important components are Autoconf, Automake, and
718 Libtool.  In this chapter, we introduce you to those tools, point you
719 to sources of more information, and try to convince you to use the
720 entire @acronym{GNU} build system for your software.
722 @menu
723 * Automake::                    Escaping Makefile hell
724 * Gnulib::                      The @acronym{GNU} portability library
725 * Libtool::                     Building libraries portably
726 * Pointers::                    More info on the @acronym{GNU} build system
727 @end menu
729 @node Automake
730 @section Automake
732 The ubiquity of @command{make} means that a @file{Makefile} is almost the
733 only viable way to distribute automatic build rules for software, but
734 one quickly runs into @command{make}'s numerous limitations.  Its lack of
735 support for automatic dependency tracking, recursive builds in
736 subdirectories, reliable timestamps (e.g., for network file systems), and
737 so on, mean that developers must painfully (and often incorrectly)
738 reinvent the wheel for each project.  Portability is non-trivial, thanks
739 to the quirks of @command{make} on many systems.  On top of all this is the
740 manual labor required to implement the many standard targets that users
741 have come to expect (@code{make install}, @code{make distclean},
742 @code{make uninstall}, etc.).  Since you are, of course, using Autoconf,
743 you also have to insert repetitive code in your @code{Makefile.in} to
744 recognize @code{@@CC@@}, @code{@@CFLAGS@@}, and other substitutions
745 provided by @command{configure}.  Into this mess steps @dfn{Automake}.
746 @cindex Automake
748 Automake allows you to specify your build needs in a @code{Makefile.am}
749 file with a vastly simpler and more powerful syntax than that of a plain
750 @code{Makefile}, and then generates a portable @code{Makefile.in} for
751 use with Autoconf.  For example, the @code{Makefile.am} to build and
752 install a simple ``Hello world'' program might look like:
754 @example
755 bin_PROGRAMS = hello
756 hello_SOURCES = hello.c
757 @end example
759 @noindent
760 The resulting @code{Makefile.in} (~400 lines) automatically supports all
761 the standard targets, the substitutions provided by Autoconf, automatic
762 dependency tracking, @code{VPATH} building, and so on.  @command{make} will
763 build the @code{hello} program, and @code{make install} will install it
764 in @file{/usr/local/bin} (or whatever prefix was given to
765 @command{configure}, if not @file{/usr/local}).
767 The benefits of Automake increase for larger packages (especially ones
768 with subdirectories), but even for small programs the added convenience
769 and portability can be substantial.  And that's not all@enddots{}
771 @node Gnulib
772 @section Gnulib
774 @acronym{GNU} software has a well-deserved reputation for running on
775 many different types of systems.  While our primary goal is to write
776 software for the @acronym{GNU} system, many users and developers have
777 been introduced to us through the systems that they were already using.
779 @cindex Gnulib
780 Gnulib is a central location for common @acronym{GNU} code, intended to
781 be shared among free software packages.  Its components are typically
782 shared at the source level, rather than being a library that gets built,
783 installed, and linked against.  The idea is to copy files from Gnulib
784 into your own source tree.  There is no distribution tarball; developers
785 should just grab source modules from the repository.  The source files
786 are available online, under various licenses, mostly @acronym{GNU}
787 @acronym{GPL} or @acronym{GNU} @acronym{LGPL}.
789 Gnulib modules typically contain C source code along with Autoconf
790 macros used to configure the source code.  For example, the Gnulib
791 @code{stdbool} module implements a @file{stdbool.h} header that nearly
792 conforms to C99, even on old-fashioned hosts that lack @file{stdbool.h}.
793 This module contains a source file for the replacement header, along
794 with an Autoconf macro that arranges to use the replacement header on
795 old-fashioned systems.
797 @node Libtool
798 @section Libtool
800 Very often, one wants to build not only programs, but libraries, so that
801 other programs can benefit from the fruits of your labor.  Ideally, one
802 would like to produce @emph{shared} (dynamically linked) libraries,
803 which can be used by multiple programs without duplication on disk or in
804 memory and can be updated independently of the linked programs.
805 Producing shared libraries portably, however, is the stuff of
806 nightmares---each system has its own incompatible tools, compiler flags,
807 and magic incantations.  Fortunately, @acronym{GNU} provides a solution:
808 @dfn{Libtool}.
809 @cindex Libtool
811 Libtool handles all the requirements of building shared libraries for
812 you, and at this time seems to be the @emph{only} way to do so with any
813 portability.  It also handles many other headaches, such as: the
814 interaction of @code{Makefile} rules with the variable suffixes of
815 shared libraries, linking reliably with shared libraries before they are
816 installed by the superuser, and supplying a consistent versioning system
817 (so that different versions of a library can be installed or upgraded
818 without breaking binary compatibility).  Although Libtool, like
819 Autoconf, can be used without Automake, it is most simply utilized in
820 conjunction with Automake---there, Libtool is used automatically
821 whenever shared libraries are needed, and you need not know its syntax.
823 @node Pointers
824 @section Pointers
826 Developers who are used to the simplicity of @command{make} for small
827 projects on a single system might be daunted at the prospect of
828 learning to use Automake and Autoconf.  As your software is
829 distributed to more and more users, however, you will otherwise
830 quickly find yourself putting lots of effort into reinventing the
831 services that the @acronym{GNU} build tools provide, and making the
832 same mistakes that they once made and overcame.  (Besides, since
833 you're already learning Autoconf, Automake will be a piece of cake.)
835 There are a number of places that you can go to for more information on
836 the @acronym{GNU} build tools.
838 @itemize @minus
840 @item Web
842 The home pages for
843 @uref{http://www.gnu.org/@/software/@/autoconf/, Autoconf},
844 @uref{http://www.gnu.org/@/software/@/automake/, Automake},
845 @uref{http://www.gnu.org/@/software/@/gnulib/, Gnulib}, and
846 @uref{http://www.gnu.org/@/software/@/libtool/, Libtool}.
848 @item Automake Manual
850 @xref{Top, , Automake, automake, @acronym{GNU} Automake}, for more
851 information on Automake.
853 @item Books
855 The book @cite{@acronym{GNU} Autoconf, Automake and
856 Libtool}@footnote{@cite{@acronym{GNU} Autoconf, Automake and Libtool},
857 by G. V. Vaughan, B. Elliston, T. Tromey, and I. L. Taylor.  SAMS (originally
858 New Riders), 2000, ISBN 1578701902.} describes the complete @acronym{GNU}
859 build environment.  You can also find
860 @uref{http://sources.redhat.com/@/autobook/, the entire book on-line}.
862 @end itemize
864 @c ================================================= Making configure Scripts.
866 @node Making configure Scripts
867 @chapter Making @command{configure} Scripts
868 @cindex @file{aclocal.m4}
869 @cindex @command{configure}
871 The configuration scripts that Autoconf produces are by convention
872 called @command{configure}.  When run, @command{configure} creates several
873 files, replacing configuration parameters in them with appropriate
874 values.  The files that @command{configure} creates are:
876 @itemize @minus
877 @item
878 one or more @file{Makefile} files, usually one in each subdirectory of the
879 package (@pxref{Makefile Substitutions});
881 @item
882 optionally, a C header file, the name of which is configurable,
883 containing @code{#define} directives (@pxref{Configuration Headers});
885 @item
886 a shell script called @file{config.status} that, when run, will recreate
887 the files listed above (@pxref{config.status Invocation});
889 @item
890 an optional shell script normally called @file{config.cache}
891 (created when using @samp{configure --config-cache}) that
892 saves the results of running many of the tests (@pxref{Cache Files});
894 @item
895 a file called @file{config.log} containing any messages produced by
896 compilers, to help debugging if @command{configure} makes a mistake.
897 @end itemize
899 @cindex @file{configure.in}
900 @cindex @file{configure.ac}
901 To create a @command{configure} script with Autoconf, you need to write an
902 Autoconf input file @file{configure.ac} (or @file{configure.in}) and run
903 @command{autoconf} on it.  If you write your own feature tests to
904 supplement those that come with Autoconf, you might also write files
905 called @file{aclocal.m4} and @file{acsite.m4}.  If you use a C header
906 file to contain @code{#define} directives, you might also run
907 @command{autoheader}, and you will distribute the generated file
908 @file{config.h.in} with the package.
910 Here is a diagram showing how the files that can be used in
911 configuration are produced.  Programs that are executed are suffixed by
912 @samp{*}.  Optional files are enclosed in square brackets (@samp{[]}).
913 @command{autoconf} and @command{autoheader} also read the installed Autoconf
914 macro files (by reading @file{autoconf.m4}).
916 @noindent
917 Files used in preparing a software package for distribution:
918 @example
919 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
921 @group
922 configure.ac --.
923                |   .------> autoconf* -----> configure
924 [aclocal.m4] --+---+
925                |   `-----> [autoheader*] --> [config.h.in]
926 [acsite.m4] ---'
927 @end group
929 Makefile.in -------------------------------> Makefile.in
930 @end example
932 @noindent
933 Files used in configuring a software package:
934 @example
935 @group
936                        .-------------> [config.cache]
937 configure* ------------+-------------> config.log
938                        |
939 [config.h.in] -.       v            .-> [config.h] -.
940                +--> config.status* -+               +--> make*
941 Makefile.in ---'                    `-> Makefile ---'
942 @end group
943 @end example
945 @menu
946 * Writing configure.ac::        What to put in an Autoconf input file
947 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
948 * ifnames Invocation::          Listing the conditionals in source code
949 * autoconf Invocation::         How to create configuration scripts
950 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
951 @end menu
953 @node Writing configure.ac
954 @section Writing @file{configure.ac}
956 To produce a @command{configure} script for a software package, create a
957 file called @file{configure.ac} that contains invocations of the
958 Autoconf macros that test the system features your package needs or can
959 use.  Autoconf macros already exist to check for many features; see
960 @ref{Existing Tests}, for their descriptions.  For most other features,
961 you can use Autoconf template macros to produce custom checks; see
962 @ref{Writing Tests}, for information about them.  For especially tricky
963 or specialized features, @file{configure.ac} might need to contain some
964 hand-crafted shell commands; see @ref{Portable Shell}.  The
965 @command{autoscan} program can give you a good start in writing
966 @file{configure.ac} (@pxref{autoscan Invocation}, for more information).
968 Previous versions of Autoconf promoted the name @file{configure.in},
969 which is somewhat ambiguous (the tool needed to process this file is not
970 described by its extension), and introduces a slight confusion with
971 @file{config.h.in} and so on (for which @samp{.in} means ``to be
972 processed by @command{configure}'').  Using @file{configure.ac} is now
973 preferred.
975 @menu
976 * Shell Script Compiler::       Autoconf as solution of a problem
977 * Autoconf Language::           Programming in Autoconf
978 * configure.ac Layout::         Standard organization of @file{configure.ac}
979 @end menu
981 @node Shell Script Compiler
982 @subsection A Shell Script Compiler
984 Just as for any other computer language, in order to properly program
985 @file{configure.ac} in Autoconf you must understand @emph{what} problem
986 the language tries to address and @emph{how} it does so.
988 The problem Autoconf addresses is that the world is a mess.  After all,
989 you are using Autoconf in order to have your package compile easily on
990 all sorts of different systems, some of them being extremely hostile.
991 Autoconf itself bears the price for these differences: @command{configure}
992 must run on all those systems, and thus @command{configure} must limit itself
993 to their lowest common denominator of features.
995 Naturally, you might then think of shell scripts; who needs
996 @command{autoconf}?  A set of properly written shell functions is enough to
997 make it easy to write @command{configure} scripts by hand.  Sigh!
998 Unfortunately, shell functions do not belong to the least common
999 denominator; therefore, where you would like to define a function and
1000 use it ten times, you would instead need to copy its body ten times.
1002 So, what is really needed is some kind of compiler, @command{autoconf},
1003 that takes an Autoconf program, @file{configure.ac}, and transforms it
1004 into a portable shell script, @command{configure}.
1006 How does @command{autoconf} perform this task?
1008 There are two obvious possibilities: creating a brand new language or
1009 extending an existing one.  The former option is very attractive: all
1010 sorts of optimizations could easily be implemented in the compiler and
1011 many rigorous checks could be performed on the Autoconf program
1012 (e.g., rejecting any non-portable construct).  Alternatively, you can
1013 extend an existing language, such as the @code{sh} (Bourne shell)
1014 language.
1016 Autoconf does the latter: it is a layer on top of @code{sh}.  It was
1017 therefore most convenient to implement @command{autoconf} as a macro
1018 expander: a program that repeatedly performs @dfn{macro expansions} on
1019 text input, replacing macro calls with macro bodies and producing a pure
1020 @code{sh} script in the end.  Instead of implementing a dedicated
1021 Autoconf macro expander, it is natural to use an existing
1022 general-purpose macro language, such as M4, and implement the extensions
1023 as a set of M4 macros.
1026 @node Autoconf Language
1027 @subsection The Autoconf Language
1028 @cindex quotation
1030 The Autoconf language is very different from many other computer
1031 languages because it treats actual code the same as plain text.  Whereas
1032 in C, for instance, data and instructions have very different syntactic
1033 status, in Autoconf their status is rigorously the same.  Therefore, we
1034 need a means to distinguish literal strings from text to be expanded:
1035 quotation.
1037 When calling macros that take arguments, there must not be any white
1038 space between the macro name and the open parenthesis.  Arguments should
1039 be enclosed within the M4 quote characters @samp{[} and @samp{]}, and be
1040 separated by commas.  Any leading blanks or newlines in arguments are ignored,
1041 unless they are quoted.  You should always quote an argument that
1042 might contain a macro name, comma, parenthesis, or a leading blank or
1043 newline.  This rule applies recursively for every macro
1044 call, including macros called from other macros.
1046 For instance:
1048 @example
1049 AC_CHECK_HEADER([stdio.h],
1050                 [AC_DEFINE([HAVE_STDIO_H], [1],
1051                    [Define to 1 if you have <stdio.h>.])],
1052                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1053 @end example
1055 @noindent
1056 is quoted properly.  You may safely simplify its quotation to:
1058 @example
1059 AC_CHECK_HEADER([stdio.h],
1060                 [AC_DEFINE([HAVE_STDIO_H], 1,
1061                    [Define to 1 if you have <stdio.h>.])],
1062                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1063 @end example
1065 @noindent
1066 because @samp{1} cannot contain a macro call.  Here, the argument of
1067 @code{AC_MSG_ERROR} must be quoted; otherwise, its comma would be
1068 interpreted as an argument separator.  Also, @samp{AC_CHECK_HEADER}'s
1069 second and third arguments must be quoted, since those arguments contain
1070 macro calls.  The three arguments @samp{HAVE_STDIO_H}, @samp{stdio.h},
1071 and @samp{Define to 1 if you have <stdio.h>.} do not need quoting, but
1072 if you unwisely defined a macro with a name like @samp{Define} or
1073 @samp{stdio} then they would need quoting.  Cautious Autoconf users
1074 would keep the quotes, but many Autoconf users find such precautions
1075 annoying, and would rewrite the example as follows:
1077 @example
1078 AC_CHECK_HEADER(stdio.h,
1079                 [AC_DEFINE(HAVE_STDIO_H, 1,
1080                    [Define to 1 if you have <stdio.h>.])],
1081                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1082 @end example
1084 @noindent
1085 This is safe, so long as you adopt good naming conventions and do not
1086 define macros with names like @samp{HAVE_STDIO_H}, @samp{stdio}, or
1087 @samp{h}.  Though it is also safe here to omit the quotes around
1088 @samp{Define to 1 if you have <stdio.h>.} this is not recommended, as
1089 message strings are more likely to inadvertently contain commas.
1091 The following example is wrong and dangerous, as it is underquoted:
1093 @example
1094 AC_CHECK_HEADER(stdio.h,
1095                 AC_DEFINE(HAVE_STDIO_H, 1,
1096                    Define to 1 if you have <stdio.h>.),
1097                 AC_MSG_ERROR([Sorry, can't do anything for you]))
1098 @end example
1100 In other cases, you may have to use text that also resembles a macro
1101 call.  You must quote that text even when it is not passed as a macro
1102 argument:
1104 @example
1105 echo "Hard rock was here!  --[AC_DC]"
1106 @end example
1108 @noindent
1109 which will result in
1111 @example
1112 echo "Hard rock was here!  --AC_DC"
1113 @end example
1115 @noindent
1116 When you use the same text in a macro argument, you must therefore have
1117 an extra quotation level (since one is stripped away by the macro
1118 substitution).  In general, then, it is a good idea to @emph{use double
1119 quoting for all literal string arguments}:
1121 @example
1122 AC_MSG_WARN([[AC_DC stinks  --Iron Maiden]])
1123 @end example
1125 You are now able to understand one of the constructs of Autoconf that
1126 has been continually misunderstood@dots{}  The rule of thumb is that
1127 @emph{whenever you expect macro expansion, expect quote expansion};
1128 i.e., expect one level of quotes to be lost.  For instance:
1130 @example
1131 AC_COMPILE_IFELSE([char b[10];], [], [AC_MSG_ERROR([you lose])])
1132 @end example
1134 @noindent
1135 is incorrect: here, the first argument of @code{AC_COMPILE_IFELSE} is
1136 @samp{char b[10];} and will be expanded once, which results in
1137 @samp{char b10;}.  (There was an idiom common in Autoconf's past to
1138 address this issue via the M4 @code{changequote} primitive, but do not
1139 use it!)  Let's take a closer look: the author meant the first argument
1140 to be understood as a literal, and therefore it must be quoted twice:
1142 @example
1143 AC_COMPILE_IFELSE([[char b[10];]], [], [AC_MSG_ERROR([you lose])])
1144 @end example
1146 @noindent
1147 Voil@`a, you actually produce @samp{char b[10];} this time!
1149 On the other hand, descriptions (e.g., the last parameter of
1150 @code{AC_DEFINE} or @code{AS_HELP_STRING}) are not literals---they
1151 are subject to line breaking, for example---and should not be double quoted.
1152 Even if these descriptions are short and are not actually broken, double
1153 quoting them yields weird results.
1155 Some macros take optional arguments, which this documentation represents
1156 as @ovar{arg} (not to be confused with the quote characters).  You may
1157 just leave them empty, or use @samp{[]} to make the emptiness of the
1158 argument explicit, or you may simply omit the trailing commas.  The
1159 three lines below are equivalent:
1161 @example
1162 AC_CHECK_HEADERS([stdio.h], [], [], [])
1163 AC_CHECK_HEADERS([stdio.h],,,)
1164 AC_CHECK_HEADERS([stdio.h])
1165 @end example
1167 It is best to put each macro call on its own line in
1168 @file{configure.ac}.  Most of the macros don't add extra newlines; they
1169 rely on the newline after the macro call to terminate the commands.
1170 This approach makes the generated @command{configure} script a little
1171 easier to read by not inserting lots of blank lines.  It is generally
1172 safe to set shell variables on the same line as a macro call, because
1173 the shell allows assignments without intervening newlines.
1175 You can include comments in @file{configure.ac} files by starting them
1176 with the @samp{#}.  For example, it is helpful to begin
1177 @file{configure.ac} files with a line like this:
1179 @example
1180 # Process this file with autoconf to produce a configure script.
1181 @end example
1183 @node configure.ac Layout
1184 @subsection Standard @file{configure.ac} Layout
1186 The order in which @file{configure.ac} calls the Autoconf macros is not
1187 important, with a few exceptions.  Every @file{configure.ac} must
1188 contain a call to @code{AC_INIT} before the checks, and a call to
1189 @code{AC_OUTPUT} at the end (@pxref{Output}).  Additionally, some macros
1190 rely on other macros having been called first, because they check
1191 previously set values of some variables to decide what to do.  These
1192 macros are noted in the individual descriptions (@pxref{Existing
1193 Tests}), and they also warn you when @command{configure} is created if they
1194 are called out of order.
1196 To encourage consistency, here is a suggested order for calling the
1197 Autoconf macros.  Generally speaking, the things near the end of this
1198 list are those that could depend on things earlier in it.  For example,
1199 library functions could be affected by types and libraries.
1201 @display
1202 @group
1203 Autoconf requirements
1204 @code{AC_INIT(@var{package}, @var{version}, @var{bug-report-address})}
1205 information on the package
1206 checks for programs
1207 checks for libraries
1208 checks for header files
1209 checks for types
1210 checks for structures
1211 checks for compiler characteristics
1212 checks for library functions
1213 checks for system services
1214 @code{AC_CONFIG_FILES(@r{[}@var{file@dots{}}@r{]})}
1215 @code{AC_OUTPUT}
1216 @end group
1217 @end display
1220 @node autoscan Invocation
1221 @section Using @command{autoscan} to Create @file{configure.ac}
1222 @cindex @command{autoscan}
1224 The @command{autoscan} program can help you create and/or maintain a
1225 @file{configure.ac} file for a software package.  @command{autoscan}
1226 examines source files in the directory tree rooted at a directory given
1227 as a command line argument, or the current directory if none is given.
1228 It searches the source files for common portability problems and creates
1229 a file @file{configure.scan} which is a preliminary @file{configure.ac}
1230 for that package, and checks a possibly existing @file{configure.ac} for
1231 completeness.
1233 When using @command{autoscan} to create a @file{configure.ac}, you
1234 should manually examine @file{configure.scan} before renaming it to
1235 @file{configure.ac}; it will probably need some adjustments.
1236 Occasionally, @command{autoscan} outputs a macro in the wrong order
1237 relative to another macro, so that @command{autoconf} produces a warning;
1238 you need to move such macros manually.  Also, if you want the package to
1239 use a configuration header file, you must add a call to
1240 @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers}).  You might
1241 also have to change or add some @code{#if} directives to your program in
1242 order to make it work with Autoconf (@pxref{ifnames Invocation}, for
1243 information about a program that can help with that job).
1245 When using @command{autoscan} to maintain a @file{configure.ac}, simply
1246 consider adding its suggestions.  The file @file{autoscan.log} will
1247 contain detailed information on why a macro is requested.
1249 @command{autoscan} uses several data files (installed along with Autoconf)
1250 to determine which macros to output when it finds particular symbols in
1251 a package's source files.  These data files all have the same format:
1252 each line consists of a symbol, one or more blanks, and the Autoconf macro to
1253 output if that symbol is encountered.  Lines starting with @samp{#} are
1254 comments.
1256 @command{autoscan} accepts the following options:
1258 @table @option
1259 @item --help
1260 @itemx -h
1261 Print a summary of the command line options and exit.
1263 @item --version
1264 @itemx -V
1265 Print the version number of Autoconf and exit.
1267 @item --verbose
1268 @itemx -v
1269 Print the names of the files it examines and the potentially interesting
1270 symbols it finds in them.  This output can be voluminous.
1272 @item --include=@var{dir}
1273 @itemx -I @var{dir}
1274 Append @var{dir} to the include path.  Multiple invocations accumulate.
1276 @item --prepend-include=@var{dir}
1277 @item -B @var{dir}
1278 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1279 @end table
1281 @node ifnames Invocation
1282 @section Using @command{ifnames} to List Conditionals
1283 @cindex @command{ifnames}
1285 @command{ifnames} can help you write @file{configure.ac} for a software
1286 package.  It prints the identifiers that the package already uses in C
1287 preprocessor conditionals.  If a package has already been set up to have
1288 some portability, @command{ifnames} can thus help you figure out what its
1289 @command{configure} needs to check for.  It may help fill in some gaps in a
1290 @file{configure.ac} generated by @command{autoscan} (@pxref{autoscan
1291 Invocation}).
1293 @command{ifnames} scans all of the C source files named on the command line
1294 (or the standard input, if none are given) and writes to the standard
1295 output a sorted list of all the identifiers that appear in those files
1296 in @code{#if}, @code{#elif}, @code{#ifdef}, or @code{#ifndef}
1297 directives.  It prints each identifier on a line, followed by a
1298 space-separated list of the files in which that identifier occurs.
1300 @noindent
1301 @command{ifnames} accepts the following options:
1303 @table @option
1304 @item --help
1305 @itemx -h
1306 Print a summary of the command line options and exit.
1308 @item --version
1309 @itemx -V
1310 Print the version number of Autoconf and exit.
1311 @end table
1313 @node autoconf Invocation
1314 @section Using @command{autoconf} to Create @command{configure}
1315 @cindex @command{autoconf}
1317 To create @command{configure} from @file{configure.ac}, run the
1318 @command{autoconf} program with no arguments.  @command{autoconf} processes
1319 @file{configure.ac} with the M4 macro processor, using the
1320 Autoconf macros.  If you give @command{autoconf} an argument, it reads that
1321 file instead of @file{configure.ac} and writes the configuration script
1322 to the standard output instead of to @command{configure}.  If you give
1323 @command{autoconf} the argument @option{-}, it reads from the standard
1324 input instead of @file{configure.ac} and writes the configuration script
1325 to the standard output.
1327 The Autoconf macros are defined in several files.  Some of the files are
1328 distributed with Autoconf; @command{autoconf} reads them first.  Then it
1329 looks for the optional file @file{acsite.m4} in the directory that
1330 contains the distributed Autoconf macro files, and for the optional file
1331 @file{aclocal.m4} in the current directory.  Those files can contain
1332 your site's or the package's own Autoconf macro definitions
1333 (@pxref{Writing Autoconf Macros}, for more information).  If a macro is
1334 defined in more than one of the files that @command{autoconf} reads, the
1335 last definition it reads overrides the earlier ones.
1337 @command{autoconf} accepts the following options:
1339 @table @option
1340 @item --help
1341 @itemx -h
1342 Print a summary of the command line options and exit.
1344 @item --version
1345 @itemx -V
1346 Print the version number of Autoconf and exit.
1348 @item --verbose
1349 @itemx -v
1350 Report processing steps.
1352 @item --debug
1353 @itemx -d
1354 Don't remove the temporary files.
1356 @item --force
1357 @itemx -f
1358 Remake @file{configure} even if newer than its input files.
1360 @item --include=@var{dir}
1361 @itemx -I @var{dir}
1362 Append @var{dir} to the include path.  Multiple invocations accumulate.
1364 @item --prepend-include=@var{dir}
1365 @item -B @var{dir}
1366 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1368 @item --output=@var{file}
1369 @itemx -o @var{file}
1370 Save output (script or trace) to @var{file}.  The file @option{-} stands
1371 for the standard output.
1373 @item --warnings=@var{category}
1374 @itemx -W @var{category}
1375 @evindex WARNINGS
1376 Report the warnings related to @var{category} (which can actually be a
1377 comma separated list).  @xref{Reporting Messages}, macro
1378 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
1379 values include:
1381 @table @samp
1382 @item all
1383 report all the warnings
1385 @item none
1386 report none
1388 @item error
1389 treats warnings as errors
1391 @item no-@var{category}
1392 disable warnings falling into @var{category}
1393 @end table
1395 Warnings about @samp{syntax} are enabled by default, and the environment
1396 variable @env{WARNINGS}, a comma separated list of categories, is
1397 honored as well.  Passing @option{-W @var{category}} will actually behave as if
1398 you had passed @option{--warnings=syntax,$WARNINGS,@var{category}}.  If
1399 you want to disable the defaults and @env{WARNINGS}, but (for example)
1400 enable the warnings about obsolete constructs, you would use @option{-W
1401 none,obsolete}.
1403 @cindex Back trace
1404 @cindex Macro invocation stack
1405 Because @command{autoconf} uses @command{autom4te} behind the scenes, it
1406 displays a back trace for errors, but not for warnings; if you want
1407 them, just pass @option{-W error}.  @xref{autom4te Invocation}, for some
1408 examples.
1410 @item --trace=@var{macro}[:@var{format}]
1411 @itemx -t @var{macro}[:@var{format}]
1412 Do not create the @command{configure} script, but list the calls to
1413 @var{macro} according to the @var{format}.  Multiple @option{--trace}
1414 arguments can be used to list several macros.  Multiple @option{--trace}
1415 arguments for a single macro are not cumulative; instead, you should
1416 just make @var{format} as long as needed.
1418 The @var{format} is a regular string, with newlines if desired, and
1419 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}; see
1420 @ref{autom4te Invocation}, for details on the @var{format}.
1422 @item --initialization
1423 @itemx -i
1424 By default, @option{--trace} does not trace the initialization of the
1425 Autoconf macros (typically the @code{AC_DEFUN} definitions).  This
1426 results in a noticeable speedup, but can be disabled by this option.
1427 @end table
1430 It is often necessary to check the content of a @file{configure.ac}
1431 file, but parsing it yourself is extremely fragile and error-prone.  It
1432 is suggested that you rely upon @option{--trace} to scan
1433 @file{configure.ac}.  For instance, to find the list of variables that
1434 are substituted, use:
1436 @example
1437 @group
1438 $ @kbd{autoconf -t AC_SUBST}
1439 configure.ac:2:AC_SUBST:ECHO_C
1440 configure.ac:2:AC_SUBST:ECHO_N
1441 configure.ac:2:AC_SUBST:ECHO_T
1442 @i{More traces deleted}
1443 @end group
1444 @end example
1446 @noindent
1447 The example below highlights the difference between @samp{$@@},
1448 @samp{$*}, and @samp{$%}.
1450 @example
1451 @group
1452 $ @kbd{cat configure.ac}
1453 AC_DEFINE(This, is, [an
1454 [example]])
1455 $ @kbd{autoconf -t 'AC_DEFINE:@@: $@@}
1456 *: $*
1457 %: $%'
1458 @@: [This],[is],[an
1459 [example]]
1460 *: This,is,an
1461 [example]
1462 %: This:is:an [example]
1463 @end group
1464 @end example
1466 @noindent
1467 The @var{format} gives you a lot of freedom:
1469 @example
1470 @group
1471 $ @kbd{autoconf -t 'AC_SUBST:$$ac_subst@{"$1"@} = "$f:$l";'}
1472 $ac_subst@{"ECHO_C"@} = "configure.ac:2";
1473 $ac_subst@{"ECHO_N"@} = "configure.ac:2";
1474 $ac_subst@{"ECHO_T"@} = "configure.ac:2";
1475 @i{More traces deleted}
1476 @end group
1477 @end example
1479 @noindent
1480 A long @var{separator} can be used to improve the readability of complex
1481 structures, and to ease their parsing (for instance when no single
1482 character is suitable as a separator):
1484 @example
1485 @group
1486 $ @kbd{autoconf -t 'AM_MISSING_PROG:$@{|:::::|@}*'}
1487 ACLOCAL|:::::|aclocal|:::::|$missing_dir
1488 AUTOCONF|:::::|autoconf|:::::|$missing_dir
1489 AUTOMAKE|:::::|automake|:::::|$missing_dir
1490 @i{More traces deleted}
1491 @end group
1492 @end example
1494 @node autoreconf Invocation
1495 @section Using @command{autoreconf} to Update @command{configure} Scripts
1496 @cindex @command{autoreconf}
1498 Installing the various components of the @acronym{GNU} Build System can be
1499 tedious: running @command{autopoint} for Gettext, @command{automake} for
1500 @file{Makefile.in} etc.@: in each directory.  It may be needed either
1501 because some tools such as @command{automake} have been updated on your
1502 system, or because some of the sources such as @file{configure.ac} have
1503 been updated, or finally, simply in order to install the @acronym{GNU} Build
1504 System in a fresh tree.
1506 @command{autoreconf} runs @command{autoconf}, @command{autoheader},
1507 @command{aclocal}, @command{automake}, @command{libtoolize}, and
1508 @command{autopoint} (when appropriate) repeatedly to update the
1509 @acronym{GNU} Build System in the specified directories and their
1510 subdirectories (@pxref{Subdirectories}).  By default, it only remakes
1511 those files that are older than their sources.
1513 If you install a new version of some tool, you can make
1514 @command{autoreconf} remake @emph{all} of the files by giving it the
1515 @option{--force} option.
1517 @xref{Automatic Remaking}, for @file{Makefile} rules to automatically
1518 remake @command{configure} scripts when their source files change.  That
1519 method handles the timestamps of configuration header templates
1520 properly, but does not pass @option{--autoconf-dir=@var{dir}} or
1521 @option{--localdir=@var{dir}}.
1523 @cindex Gettext
1524 @cindex @command{autopoint}
1525 Gettext supplies the @command{autopoint} command to add translation
1526 infrastructure to a source package.  If you use @command{autopoint},
1527 your @file{configure.ac} should invoke both @code{AM_GNU_GETTEXT} and
1528 @code{AM_GNU_GETTEXT_VERSION(@var{gettext-version})}.  @xref{autopoint
1529 Invocation, , Invoking the @code{autopoint} Program, gettext, GNU
1530 @code{gettext} utilities}, for further details.
1532 @noindent
1533 @command{autoreconf} accepts the following options:
1535 @table @option
1536 @item --help
1537 @itemx -h
1538 Print a summary of the command line options and exit.
1540 @item --version
1541 @itemx -V
1542 Print the version number of Autoconf and exit.
1544 @item --verbose
1545 Print the name of each directory @command{autoreconf} examines and the
1546 commands it runs.  If given two or more times, pass @option{--verbose}
1547 to subordinate tools that support it.
1549 @item --debug
1550 @itemx -d
1551 Don't remove the temporary files.
1553 @item --force
1554 @itemx -f
1555 Remake even @file{configure} scripts and configuration headers that are
1556 newer than their input files (@file{configure.ac} and, if present,
1557 @file{aclocal.m4}).
1559 @item --install
1560 @itemx -i
1561 Install the missing auxiliary files in the package.  By default, files
1562 are copied; this can be changed with @option{--symlink}.
1564 If deemed appropriate, this option triggers calls to
1565 @samp{automake --add-missing},
1566 @samp{libtoolize}, @samp{autopoint}, etc.
1568 @item --no-recursive
1569 Do not rebuild files in subdirectories to configure (see @ref{Subdirectories},
1570 macro @code{AC_CONFIG_SUBDIRS}).
1572 @item --symlink
1573 @itemx -s
1574 When used with @option{--install}, install symbolic links to the missing
1575 auxiliary files instead of copying them.
1577 @item --make
1578 @itemx -m
1579 When the directories were configured, update the configuration by
1580 running @samp{./config.status --recheck && ./config.status}, and then
1581 run @samp{make}.
1583 @item --include=@var{dir}
1584 @itemx -I @var{dir}
1585 Append @var{dir} to the include path.  Multiple invocations accumulate.
1586 Passed on to @command{autoconf} and @command{autoheader} internally.
1588 @item --prepend-include=@var{dir}
1589 @item -B @var{dir}
1590 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1591 Passed on to @command{autoconf} and @command{autoheader} internally.
1593 @item --warnings=@var{category}
1594 @itemx -W @var{category}
1595 @evindex WARNINGS
1596 Report the warnings related to @var{category} (which can actually be a
1597 comma separated list).
1599 @table @samp
1600 @item cross
1601 related to cross compilation issues.
1603 @item obsolete
1604 report the uses of obsolete constructs.
1606 @item portability
1607 portability issues
1609 @item syntax
1610 dubious syntactic constructs.
1612 @item all
1613 report all the warnings
1615 @item none
1616 report none
1618 @item error
1619 treats warnings as errors
1621 @item no-@var{category}
1622 disable warnings falling into @var{category}
1623 @end table
1625 Warnings about @samp{syntax} are enabled by default, and the environment
1626 variable @env{WARNINGS}, a comma separated list of categories, is
1627 honored as well.  Passing @option{-W @var{category}} will actually behave as if
1628 you had passed @option{--warnings=syntax,$WARNINGS,@var{category}}.  If
1629 you want to disable the defaults and @env{WARNINGS}, but (for example)
1630 enable the warnings about obsolete constructs, you would use @option{-W
1631 none,obsolete}.
1632 @end table
1634 If you want @command{autoreconf} to pass flags that are not listed here
1635 on to @command{aclocal}, set @code{ACLOCAL_AMFLAGS} in your Makefile.am.
1637 @c ========================================= Initialization and Output Files.
1639 @node Setup
1640 @chapter Initialization and Output Files
1642 Autoconf-generated @command{configure} scripts need some information about
1643 how to initialize, such as how to find the package's source files and
1644 about the output files to produce.  The following sections describe the
1645 initialization and the creation of output files.
1647 @menu
1648 * Initializing configure::      Option processing etc.
1649 * Notices::                     Copyright, version numbers in @command{configure}
1650 * Input::                       Where Autoconf should find files
1651 * Output::                      Outputting results from the configuration
1652 * Configuration Actions::       Preparing the output based on results
1653 * Configuration Files::         Creating output files
1654 * Makefile Substitutions::      Using output variables in @file{Makefile}s
1655 * Configuration Headers::       Creating a configuration header file
1656 * Configuration Commands::      Running arbitrary instantiation commands
1657 * Configuration Links::         Links depending on the configuration
1658 * Subdirectories::              Configuring independent packages together
1659 * Default Prefix::              Changing the default installation prefix
1660 @end menu
1662 @node Initializing configure
1663 @section Initializing @command{configure}
1665 Every @command{configure} script must call @code{AC_INIT} before doing
1666 anything else.  The only other required macro is @code{AC_OUTPUT}
1667 (@pxref{Output}).
1669 @defmac AC_INIT (@var{package}, @var{version}, @ovar{bug-report}, @ovar{tarname})
1670 @acindex{INIT}
1671 Process any command-line arguments and perform various initializations
1672 and verifications.
1674 Set the name of the @var{package} and its @var{version}.  These are
1675 typically used in @option{--version} support, including that of
1676 @command{configure}.  The optional argument @var{bug-report} should be
1677 the email to which users should send bug reports.  The package
1678 @var{tarname} differs from @var{package}: the latter designates the full
1679 package name (e.g., @samp{GNU Autoconf}), while the former is meant for
1680 distribution tar ball names (e.g., @samp{autoconf}).  It defaults to
1681 @var{package} with @samp{GNU } stripped, lower-cased, and all characters
1682 other than alphanumerics and underscores are changed to @samp{-}.
1684 It is preferable that the arguments of @code{AC_INIT} be static, i.e.,
1685 there should not be any shell computation, but they can be computed by
1688 The following M4 macros (e.g., @code{AC_PACKAGE_NAME}), output variables
1689 (e.g., @code{PACKAGE_NAME}), and preprocessor symbols (e.g.,
1690 @code{PACKAGE_NAME}) are defined by @code{AC_INIT}:
1692 @table @asis
1693 @item @code{AC_PACKAGE_NAME}, @code{PACKAGE_NAME}
1694 @acindex{PACKAGE_NAME}
1695 @ovindex PACKAGE_NAME
1696 @cvindex PACKAGE_NAME
1697 Exactly @var{package}.
1699 @item @code{AC_PACKAGE_TARNAME}, @code{PACKAGE_TARNAME}
1700 @acindex{PACKAGE_TARNAME}
1701 @ovindex PACKAGE_TARNAME
1702 @cvindex PACKAGE_TARNAME
1703 Exactly @var{tarname}.
1705 @item @code{AC_PACKAGE_VERSION}, @code{PACKAGE_VERSION}
1706 @acindex{PACKAGE_VERSION}
1707 @ovindex PACKAGE_VERSION
1708 @cvindex PACKAGE_VERSION
1709 Exactly @var{version}.
1711 @item @code{AC_PACKAGE_STRING}, @code{PACKAGE_STRING}
1712 @acindex{PACKAGE_STRING}
1713 @ovindex PACKAGE_STRING
1714 @cvindex PACKAGE_STRING
1715 Exactly @samp{@var{package} @var{version}}.
1717 @item @code{AC_PACKAGE_BUGREPORT}, @code{PACKAGE_BUGREPORT}
1718 @acindex{PACKAGE_BUGREPORT}
1719 @ovindex PACKAGE_BUGREPORT
1720 @cvindex PACKAGE_BUGREPORT
1721 Exactly @var{bug-report}.
1722 @end table
1723 @end defmac
1726 @node Notices
1727 @section Notices in @command{configure}
1728 @cindex Notices in @command{configure}
1730 The following macros manage version numbers for @command{configure}
1731 scripts.  Using them is optional.
1733 @c FIXME: AC_PREREQ should not be here
1734 @defmac AC_PREREQ (@var{version})
1735 @acindex{PREREQ}
1736 @cindex Version
1737 Ensure that a recent enough version of Autoconf is being used.  If the
1738 version of Autoconf being used to create @command{configure} is
1739 earlier than @var{version}, print an error message to the standard
1740 error output and exit with failure (exit status is 63).  For example:
1742 @example
1743 AC_PREREQ([@value{VERSION}])
1744 @end example
1746 This macro is the only macro that may be used before @code{AC_INIT}, but
1747 for consistency, you are invited not to do so.
1748 @end defmac
1750 @defmac AC_COPYRIGHT (@var{copyright-notice})
1751 @acindex{COPYRIGHT}
1752 @cindex Copyright Notice
1753 State that, in addition to the Free Software Foundation's copyright on
1754 the Autoconf macros, parts of your @command{configure} are covered by the
1755 @var{copyright-notice}.
1757 The @var{copyright-notice} will show up in both the head of
1758 @command{configure} and in @samp{configure --version}.
1759 @end defmac
1762 @defmac AC_REVISION (@var{revision-info})
1763 @acindex{REVISION}
1764 @cindex Revision
1765 Copy revision stamp @var{revision-info} into the @command{configure}
1766 script, with any dollar signs or double-quotes removed.  This macro lets
1767 you put a revision stamp from @file{configure.ac} into @command{configure}
1768 without @acronym{RCS} or @acronym{CVS} changing it when you check in
1769 @command{configure}.  That way, you can determine easily which revision of
1770 @file{configure.ac} a particular @command{configure} corresponds to.
1772 For example, this line in @file{configure.ac}:
1774 @c The asis prevents RCS from changing the example in the manual.
1775 @example
1776 AC_REVISION([$@asis{Revision: 1.30 }$])
1777 @end example
1779 @noindent
1780 produces this in @command{configure}:
1782 @example
1783 #!/bin/sh
1784 # From configure.ac Revision: 1.30
1785 @end example
1786 @end defmac
1789 @node Input
1790 @section Finding @command{configure} Input
1793 @defmac AC_CONFIG_SRCDIR (@var{unique-file-in-source-dir})
1794 @acindex{CONFIG_SRCDIR}
1795 @var{unique-file-in-source-dir} is some file that is in the package's
1796 source directory; @command{configure} checks for this file's existence to
1797 make sure that the directory that it is told contains the source code in
1798 fact does.  Occasionally people accidentally specify the wrong directory
1799 with @option{--srcdir}; this is a safety check.  @xref{configure
1800 Invocation}, for more information.
1801 @end defmac
1804 @c FIXME: Remove definitively once --install explained.
1806 @c Small packages may store all their macros in @code{aclocal.m4}.  As the
1807 @c set of macros grows, or for maintenance reasons, a maintainer may prefer
1808 @c to split the macros in several files.  In this case, Autoconf must be
1809 @c told which files to load, and in which order.
1811 @c @defmac AC_INCLUDE (@var{file}@dots{})
1812 @c @acindex{INCLUDE}
1813 @c @c FIXME: There is no longer shell globbing.
1814 @c Read the macro definitions that appear in the listed files.  A list of
1815 @c space-separated file names or shell globbing patterns is expected.  The
1816 @c files will be read in the order they're listed.
1818 @c Because the order of definition of macros is important (only the last
1819 @c definition of a macro is used), beware that it is @code{AC_INIT} that
1820 @c loads @file{acsite.m4} and @file{aclocal.m4}.  Note that
1821 @c @code{AC_INCLUDE}ing a file before @code{AC_INIT} or within
1822 @c @file{aclocal.m4} is different from doing so after @code{AC_INIT}: in
1823 @c the latter case, non-macro lines from included files may end up in the
1824 @c @file{configure} script, whereas in the former case, they'd be discarded
1825 @c just like any text that appear before @code{AC_INIT}.
1826 @c @end defmac
1828 Packages that do manual configuration or use the @command{install} program
1829 might need to tell @command{configure} where to find some other shell
1830 scripts by calling @code{AC_CONFIG_AUX_DIR}, though the default places
1831 it looks are correct for most cases.
1833 @defmac AC_CONFIG_AUX_DIR (@var{dir})
1834 @acindex{CONFIG_AUX_DIR}
1835 Use the auxiliary build tools (e.g., @file{install-sh},
1836 @file{config.sub}, @file{config.guess}, Cygnus @command{configure},
1837 Automake and Libtool scripts, etc.)@: that are in directory @var{dir}.
1838 These are auxiliary files used in configuration.  @var{dir} can be
1839 either absolute or relative to @file{@var{srcdir}}.  The default is
1840 @file{@var{srcdir}} or @file{@var{srcdir}/..} or
1841 @file{@var{srcdir}/../..}, whichever is the first that contains
1842 @file{install-sh}.  The other files are not checked for, so that using
1843 @code{AC_PROG_INSTALL} does not automatically require distributing the
1844 other auxiliary files.  It checks for @file{install.sh} also, but that
1845 name is obsolete because some @code{make} have a rule that creates
1846 @file{install} from it if there is no @file{Makefile}.
1848 The auxiliary directory is commonly named @file{build-aux}.
1849 If you need portability to @acronym{DOS} variants, do not name the
1850 auxiliary directory @file{aux}.  @xref{File System Conventions}.
1851 @end defmac
1853 @defmac AC_REQUIRE_AUX_FILE (@var{file})
1854 @acindex{REQUIRE_AUX_FILE}
1855 Declares that @var{file} is expected in the directory defined above.  In
1856 Autoconf proper, this macro does nothing: its sole purpose is to be
1857 traced by third-party tools to produce a list of expected auxiliary
1858 files.  For instance it is called by macros like @code{AC_PROG_INSTALL}
1859 (@pxref{Particular Programs}) or @code{AC_CANONICAL_BUILD}
1860 (@pxref{Canonicalizing}) to register the auxiliary files they need.
1861 @end defmac
1863 Similarly, packages that use @command{aclocal} should declare where
1864 local macros can be found using @code{AC_CONFIG_MACRO_DIR}.
1866 @defmac AC_CONFIG_MACRO_DIR (@var{dir})
1867 @acindex{CONFIG_MACRO_DIR}
1868 Future versions of @command{autopoint}, @command{libtoolize},
1869 @command{aclocal} and @command{autoreconf} will use directory
1870 @var{dir} as the location of additional local Autoconf macros.  Be
1871 sure to call this macro directly from @file{configure.ac} so that
1872 tools that install macros for @command{aclocal} can find the
1873 declaration before @option{--trace} can be called safely.
1874 @end defmac
1877 @node Output
1878 @section Outputting Files
1879 @cindex Outputting files
1881 Every Autoconf script, e.g., @file{configure.ac}, should finish by
1882 calling @code{AC_OUTPUT}.  That is the macro that generates and runs
1883 @file{config.status}, which will create the @file{Makefile}s and any
1884 other files resulting from configuration.  This is the only required
1885 macro besides @code{AC_INIT} (@pxref{Input}).
1887 @defmac AC_OUTPUT
1888 @acindex{OUTPUT}
1889 @cindex Instantiation
1890 Generate @file{config.status} and launch it.  Call this macro once, at
1891 the end of @file{configure.ac}.
1893 @file{config.status} will perform all the configuration actions: all the
1894 output files (see @ref{Configuration Files}, macro
1895 @code{AC_CONFIG_FILES}), header files (see @ref{Configuration Headers},
1896 macro @code{AC_CONFIG_HEADERS}), commands (see @ref{Configuration
1897 Commands}, macro @code{AC_CONFIG_COMMANDS}), links (see
1898 @ref{Configuration Links}, macro @code{AC_CONFIG_LINKS}), subdirectories
1899 to configure (see @ref{Subdirectories}, macro @code{AC_CONFIG_SUBDIRS})
1900 are honored.
1902 The location of your @code{AC_OUTPUT} invocation is the exact point
1903 where configuration actions are taken: any code afterwards will be
1904 executed by @code{configure} once @command{config.status} was run.  If
1905 you want to bind actions to @command{config.status} itself
1906 (independently of whether @command{configure} is being run), see
1907 @ref{Configuration Commands, , Running Arbitrary Configuration
1908 Commands}.
1909 @end defmac
1911 Historically, the usage of @code{AC_OUTPUT} was somewhat different.
1912 @xref{Obsolete Macros}, for a description of the arguments that
1913 @code{AC_OUTPUT} used to support.
1916 If you run @command{make} in subdirectories, you should run it using the
1917 @code{make} variable @code{MAKE}.  Most versions of @command{make} set
1918 @code{MAKE} to the name of the @command{make} program plus any options it
1919 was given.  (But many do not include in it the values of any variables
1920 set on the command line, so those are not passed on automatically.)
1921 Some old versions of @command{make} do not set this variable.  The
1922 following macro allows you to use it even with those versions.
1924 @defmac AC_PROG_MAKE_SET
1925 @acindex{PROG_MAKE_SET}
1926 @ovindex SET_MAKE
1927 If the Make command, @code{$MAKE} if set or else @samp{make}, predefines
1928 @code{$(MAKE)}, define output variable @code{SET_MAKE} to be empty.
1929 Otherwise, define @code{SET_MAKE} to a macro definition that sets
1930 @code{$(MAKE)}, such as @samp{MAKE=make}.  Calls @code{AC_SUBST} for
1931 @code{SET_MAKE}.
1932 @end defmac
1934 If you use this macro, place a line like this in each @file{Makefile.in}
1935 that runs @code{MAKE} on other directories:
1937 @example
1938 @@SET_MAKE@@
1939 @end example
1943 @node Configuration Actions
1944 @section Performing Configuration Actions
1945 @cindex Configuration actions
1947 @file{configure} is designed so that it appears to do everything itself,
1948 but there is actually a hidden slave: @file{config.status}.
1949 @file{configure} is in charge of examining your system, but it is
1950 @file{config.status} that actually takes the proper actions based on the
1951 results of @file{configure}.  The most typical task of
1952 @file{config.status} is to @emph{instantiate} files.
1954 This section describes the common behavior of the four standard
1955 instantiating macros: @code{AC_CONFIG_FILES}, @code{AC_CONFIG_HEADERS},
1956 @code{AC_CONFIG_COMMANDS} and @code{AC_CONFIG_LINKS}.  They all
1957 have this prototype:
1959 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
1960 @c awful.
1961 @example
1962 AC_CONFIG_FOOS(@var{tag}@dots{}, [@var{commands}], [@var{init-cmds}])
1963 @end example
1965 @noindent
1966 where the arguments are:
1968 @table @var
1969 @item @var{tag}@dots{}
1970 A blank-or-newline-separated list of tags, which are typically the names of
1971 the files to instantiate.
1973 You are encouraged to use literals as @var{tags}.  In particular, you
1974 should avoid
1976 @example
1977 @dots{} && my_foos="$my_foos fooo"
1978 @dots{} && my_foos="$my_foos foooo"
1979 AC_CONFIG_FOOS([$my_foos])
1980 @end example
1982 @noindent
1983 and use this instead:
1985 @example
1986 @dots{} && AC_CONFIG_FOOS([fooo])
1987 @dots{} && AC_CONFIG_FOOS([foooo])
1988 @end example
1990 The macros @code{AC_CONFIG_FILES} and @code{AC_CONFIG_HEADERS} use
1991 special @var{tag}s: they may have the form @samp{@var{output}} or
1992 @samp{@var{output}:@var{inputs}}.  The file @var{output} is instantiated
1993 from its templates, @var{inputs} (defaulting to @samp{@var{output}.in}).
1995 @samp{AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk)]},
1996 for example, asks for
1997 the creation of @file{Makefile} that will be the expansion of the
1998 output variables in the concatenation of @file{boiler/top.mk} and
1999 @file{boiler/bot.mk}.
2001 The special value @samp{-} might be used to denote the standard output
2002 when used in @var{output}, or the standard input when used in the
2003 @var{inputs}.  You most probably don't need to use this in
2004 @file{configure.ac}, but it is convenient when using the command line
2005 interface of @file{./config.status}, see @ref{config.status Invocation},
2006 for more details.
2008 The @var{inputs} may be absolute or relative file names.  In the latter
2009 case they are first looked for in the build tree, and then in the source
2010 tree.
2012 @item commands
2013 Shell commands output literally into @file{config.status}, and
2014 associated with a tag that the user can use to tell @file{config.status}
2015 which the commands to run.  The commands are run each time a @var{tag}
2016 request is given to @file{config.status}, typically each time the file
2017 @file{@var{tag}} is created.
2019 The variables set during the execution of @command{configure} are
2020 @emph{not} available here: you first need to set them via the
2021 @var{init-cmds}.  Nonetheless the following variables are precomputed:
2023 @table @code
2024 @item srcdir
2025 The name of the top source directory, assuming that the working
2026 directory is the top build directory.  This
2027 is what @command{configure}'s option @option{--srcdir} sets.
2029 @item ac_top_srcdir
2030 The name of the top source directory, assuming that the working
2031 directory is the current build directory.
2034 @item ac_top_build_prefix
2035 The name of the top build directory, assuming that the working
2036 directory is the current build directory.
2037 It can be empty, or else ends with a slash, so that you may concatenate
2040 @item ac_srcdir
2041 The name of the corresponding source directory, assuming that the
2042 working directory is the current build directory.
2043 @end table
2045 @noindent
2046 The @dfn{current} directory refers to the directory (or
2047 pseudo-directory) containing the input part of @var{tags}.  For
2048 instance, running
2050 @example
2051 AC_CONFIG_COMMANDS([deep/dir/out:in/in.in], [@dots{}], [@dots{}])
2052 @end example
2054 @noindent
2055  with @option{--srcdir=../package} produces the following values:
2057 @example
2058 # Argument of --srcdir
2059 srcdir='../package'
2060 # Reversing deep/dir
2061 ac_top_build_prefix='../../'
2062 # Concatenation of $ac_top_build_prefix and srcdir
2063 ac_top_srcdir='../../../package'
2064 # Concatenation of $ac_top_srcdir and deep/dir
2065 ac_srcdir='../../../package/deep/dir'
2066 @end example
2068 @noindent
2069 independently of @samp{in/in.in}.
2071 @item init-cmds
2072 Shell commands output @emph{unquoted} near the beginning of
2073 @file{config.status}, and executed each time @file{config.status} runs
2074 (regardless of the tag).  Because they are unquoted, for example,
2075 @samp{$var} will be output as the value of @code{var}.  @var{init-cmds}
2076 is typically used by @file{configure} to give @file{config.status} some
2077 variables it needs to run the @var{commands}.
2079 You should be extremely cautious in your variable names: all the
2080 @var{init-cmds} share the same name space and may overwrite each other
2081 in unpredictable ways.  Sorry@enddots{}
2082 @end table
2084 All these macros can be called multiple times, with different
2085 @var{tag}s, of course!
2088 @node Configuration Files
2089 @section Creating Configuration Files
2090 @cindex Creating configuration files
2091 @cindex Configuration file creation
2093 Be sure to read the previous section, @ref{Configuration Actions}.
2095 @defmac AC_CONFIG_FILES (@var{file}@dots{}, @ovar{cmds}, @ovar{init-cmds})
2096 @acindex{CONFIG_FILES}
2097 Make @code{AC_OUTPUT} create each @file{@var{file}} by copying an input
2098 file (by default @file{@var{file}.in}), substituting the output variable
2099 values.
2100 @c Before we used to have this feature, which was later rejected
2101 @c because it complicates the write of Makefiles:
2102 @c If the file would be unchanged, it is left untouched, to preserve
2103 @c timestamp.
2104 This macro is one of the instantiating macros; see @ref{Configuration
2105 Actions}.  @xref{Makefile Substitutions}, for more information on using
2106 output variables.  @xref{Setting Output Variables}, for more information
2107 on creating them.  This macro creates the directory that the file is in
2108 if it doesn't exist.  Usually, @file{Makefile}s are created this way,
2109 but other files, such as @file{.gdbinit}, can be specified as well.
2111 Typical calls to @code{AC_CONFIG_FILES} look like this:
2113 @example
2114 AC_CONFIG_FILES([Makefile src/Makefile man/Makefile X/Imakefile])
2115 AC_CONFIG_FILES([autoconf], [chmod +x autoconf])
2116 @end example
2118 You can override an input file name by appending to @var{file} a
2119 colon-separated list of input files.  Examples:
2121 @example
2122 AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk]
2123                 [lib/Makefile:boiler/lib.mk])
2124 @end example
2126 @noindent
2127 Doing this allows you to keep your file names acceptable to
2128 @acronym{DOS} variants, or
2129 to prepend and/or append boilerplate to the file.
2130 @end defmac
2134 @node Makefile Substitutions
2135 @section Substitutions in Makefiles
2136 @cindex Substitutions in makefiles
2137 @cindex Makefile substitutions
2139 Each subdirectory in a distribution that contains something to be
2140 compiled or installed should come with a file @file{Makefile.in}, from
2141 which @command{configure} will create a @file{Makefile} in that directory.
2142 To create a @file{Makefile}, @command{configure} performs a simple variable
2143 substitution, replacing occurrences of @samp{@@@var{variable}@@} in
2144 @file{Makefile.in} with the value that @command{configure} has determined
2145 for that variable.  Variables that are substituted into output files in
2146 this way are called @dfn{output variables}.  They are ordinary shell
2147 variables that are set in @command{configure}.  To make @command{configure}
2148 substitute a particular variable into the output files, the macro
2149 @code{AC_SUBST} must be called with that variable name as an argument.
2150 Any occurrences of @samp{@@@var{variable}@@} for other variables are
2151 left unchanged.  @xref{Setting Output Variables}, for more information
2152 on creating output variables with @code{AC_SUBST}.
2154 A software package that uses a @command{configure} script should be
2155 distributed with a file @file{Makefile.in}, but no @file{Makefile}; that
2156 way, the user has to properly configure the package for the local system
2157 before compiling it.
2159 @xref{Makefile Conventions, , Makefile Conventions, standards, The
2160 @acronym{GNU} Coding Standards}, for more information on what to put in
2161 @file{Makefile}s.
2163 @menu
2164 * Preset Output Variables::     Output variables that are always set
2165 * Installation Directory Variables::  Other preset output variables
2166 * Build Directories::           Supporting multiple concurrent compiles
2167 * Automatic Remaking::          Makefile rules for configuring
2168 @end menu
2170 @node Preset Output Variables
2171 @subsection Preset Output Variables
2172 @cindex Output variables
2174 Some output variables are preset by the Autoconf macros.  Some of the
2175 Autoconf macros set additional output variables, which are mentioned in
2176 the descriptions for those macros.  @xref{Output Variable Index}, for a
2177 complete list of output variables.  @xref{Installation Directory
2178 Variables}, for the list of the preset ones related to installation
2179 directories.  Below are listed the other preset ones.  They all are
2180 precious variables (@pxref{Setting Output Variables},
2181 @code{AC_ARG_VAR}).
2183 @c Just say no to ASCII sorting!  We're humans, not computers.
2184 @c These variables are listed as they would be in a dictionary:
2185 @c actor
2186 @c Actress
2187 @c actress
2189 @defvar CFLAGS
2190 @ovindex CFLAGS
2191 Debugging and optimization options for the C compiler.  If it is not set
2192 in the environment when @command{configure} runs, the default value is set
2193 when you call @code{AC_PROG_CC} (or empty if you don't).  @command{configure}
2194 uses this variable when compiling programs to test for C features.
2195 @end defvar
2197 @defvar configure_input
2198 @ovindex configure_input
2199 A comment saying that the file was generated automatically by
2200 @command{configure} and giving the name of the input file.
2201 @code{AC_OUTPUT} adds a comment line containing this variable to the top
2202 of every @file{Makefile} it creates.  For other files, you should
2203 reference this variable in a comment at the top of each input file.  For
2204 example, an input shell script should begin like this:
2206 @example
2207 #!/bin/sh
2208 # @@configure_input@@
2209 @end example
2211 @noindent
2212 The presence of that line also reminds people editing the file that it
2213 needs to be processed by @command{configure} in order to be used.
2214 @end defvar
2216 @defvar CPPFLAGS
2217 @ovindex CPPFLAGS
2218 Header file search directory (@option{-I@var{dir}}) and any other
2219 miscellaneous options for the C and C++ preprocessors and compilers.  If
2220 it is not set in the environment when @command{configure} runs, the default
2221 value is empty.  @command{configure} uses this variable when compiling or
2222 preprocessing programs to test for C and C++ features.
2223 @xref{Special Chars in Variables}, for limitations that @code{CPPFLAGS}
2224 might run into.
2225 @end defvar
2227 @defvar CXXFLAGS
2228 @ovindex CXXFLAGS
2229 Debugging and optimization options for the C++ compiler.  If it is not
2230 set in the environment when @command{configure} runs, the default value is
2231 set when you call @code{AC_PROG_CXX} (or empty if you don't).
2232 @command{configure} uses this variable when compiling programs to test for
2233 C++ features.
2234 @end defvar
2236 @defvar DEFS
2237 @ovindex DEFS
2238 @option{-D} options to pass to the C compiler.  If @code{AC_CONFIG_HEADERS}
2239 is called, @command{configure} replaces @samp{@@DEFS@@} with
2240 @option{-DHAVE_CONFIG_H} instead (@pxref{Configuration Headers}).  This
2241 variable is not defined while @command{configure} is performing its tests,
2242 only when creating the output files.  @xref{Setting Output Variables}, for
2243 how to check the results of previous tests.
2244 @end defvar
2246 @defvar ECHO_C
2247 @defvarx ECHO_N
2248 @defvarx ECHO_T
2249 @ovindex ECHO_C
2250 @ovindex ECHO_N
2251 @ovindex ECHO_T
2252 How does one suppress the trailing newline from @command{echo} for
2253 question-answer message pairs?  These variables provide a way:
2255 @example
2256 echo $ECHO_N "And the winner is... $ECHO_C"
2257 sleep 100000000000
2258 echo "$@{ECHO_T@}dead."
2259 @end example
2261 @noindent
2262 Some old and uncommon @command{echo} implementations offer no means to
2263 achieve this, in which case @code{ECHO_T} is set to tab.  You might not
2264 want to use it.
2265 @end defvar
2267 @defvar ERLCFLAGS
2268 @ovindex ERLCFLAGS
2269 Debugging and optimization options for the Erlang compiler.  If it is not set
2270 in the environment when @command{configure} runs, the default value is empty.
2271 @command{configure} uses this variable when compiling programs to test for
2272 Erlang features.
2273 @end defvar
2275 @defvar FCFLAGS
2276 @ovindex FCFLAGS
2277 Debugging and optimization options for the Fortran compiler.  If it
2278 is not set in the environment when @command{configure} runs, the default
2279 value is set when you call @code{AC_PROG_FC} (or empty if you don't).
2280 @command{configure} uses this variable when compiling programs to test for
2281 Fortran features.
2282 @end defvar
2284 @defvar FFLAGS
2285 @ovindex FFLAGS
2286 Debugging and optimization options for the Fortran 77 compiler.  If it
2287 is not set in the environment when @command{configure} runs, the default
2288 value is set when you call @code{AC_PROG_F77} (or empty if you don't).
2289 @command{configure} uses this variable when compiling programs to test for
2290 Fortran 77 features.
2291 @end defvar
2293 @defvar LDFLAGS
2294 @ovindex LDFLAGS
2295 Stripping (@option{-s}), path (@option{-L}), and any other miscellaneous
2296 options for the linker.  Don't use this variable to pass library names
2297 (@option{-l}) to the linker, use @code{LIBS} instead.  If it is not set
2298 in the environment when @command{configure} runs, the default value is empty.
2299 @command{configure} uses this variable when linking programs to test for
2300 C, C++, and Fortran features.
2301 @end defvar
2303 @defvar LIBS
2304 @ovindex LIBS
2305 @option{-l} options to pass to the linker.  The default value is empty,
2306 but some Autoconf macros may prepend extra libraries to this variable if
2307 those libraries are found and provide necessary functions, see
2308 @ref{Libraries}.  @command{configure} uses this variable when linking
2309 programs to test for C, C++, and Fortran features.
2310 @end defvar
2312 @defvar OBJCFLAGS
2313 @ovindex OBJCFLAGS
2314 Debugging and optimization options for the Objective C compiler.  If it is
2315 not set in the environment when @command{configure} runs, the default value
2316 is set when you call @code{AC_PROG_OBJC} (or empty if you don't).
2317 @command{configure} uses this variable when compiling programs to test for
2318 Objective C features.
2319 @end defvar
2321 @defvar builddir
2322 @ovindex builddir
2323 Rigorously equal to @samp{.}.  Added for symmetry only.
2324 @end defvar
2326 @defvar abs_builddir
2327 @ovindex abs_builddir
2328 Absolute name of @code{builddir}.
2329 @end defvar
2331 @defvar top_builddir
2332 @ovindex top_builddir
2333 The relative name of the top level of the current build tree.  In the
2334 top-level directory, this is the same as @code{builddir}.
2335 @end defvar
2337 @defvar abs_top_builddir
2338 @ovindex abs_top_builddir
2339 Absolute name of @code{top_builddir}.
2340 @end defvar
2342 @defvar srcdir
2343 @ovindex srcdir
2344 The relative name of the directory that contains the source code for
2345 that @file{Makefile}.
2346 @end defvar
2348 @defvar abs_srcdir
2349 @ovindex abs_srcdir
2350 Absolute name of @code{srcdir}.
2351 @end defvar
2353 @defvar top_srcdir
2354 @ovindex top_srcdir
2355 The relative name of the top-level source code directory for the
2356 package.  In the top-level directory, this is the same as @code{srcdir}.
2357 @end defvar
2359 @defvar abs_top_srcdir
2360 @ovindex abs_top_srcdir
2361 Absolute name of @code{top_srcdir}.
2362 @end defvar
2364 @node Installation Directory Variables
2365 @subsection Installation Directory Variables
2366 @cindex Installation directories
2367 @cindex Directories, installation
2369 The following variables specify the directories where the package will
2370 be installed, see @ref{Directory Variables, , Variables for
2371 Installation Directories, standards, The @acronym{GNU} Coding
2372 Standards}, for more information.  See the end of this section for
2373 details on when and how to use these variables.
2375 @defvar bindir
2376 @ovindex bindir
2377 The directory for installing executables that users run.
2378 @end defvar
2380 @defvar datadir
2381 @ovindex datadir
2382 The directory for installing idiosyncratic read-only
2383 architecture-independent data.
2384 @end defvar
2386 @defvar datarootdir
2387 @ovindex datarootdir
2388 The root of the directory tree for read-only architecture-independent
2389 data files.
2390 @end defvar
2392 @defvar docdir
2393 @ovindex docdir
2394 The directory for installing documentation files (other than Info and
2395 man).
2396 @end defvar
2398 @defvar dvidir
2399 @ovindex dvidir
2400 The directory for installing documentation files in DVI format.
2401 @end defvar
2403 @defvar exec_prefix
2404 @ovindex exec_prefix
2405 The installation prefix for architecture-dependent files.  By default
2406 it's the same as @var{prefix}.  You should avoid installing anything
2407 directly to @var{exec_prefix}.  However, the default value for
2408 directories containing architecture-dependent files should be relative
2409 to @var{exec_prefix}.
2410 @end defvar
2412 @defvar htmldir
2413 @ovindex htmldir
2414 The directory for installing HTML documentation.
2415 @end defvar
2417 @defvar includedir
2418 @ovindex includedir
2419 The directory for installing C header files.
2420 @end defvar
2422 @defvar infodir
2423 @ovindex infodir
2424 The directory for installing documentation in Info format.
2425 @end defvar
2427 @defvar libdir
2428 @ovindex libdir
2429 The directory for installing object code libraries.
2430 @end defvar
2432 @defvar libexecdir
2433 @ovindex libexecdir
2434 The directory for installing executables that other programs run.
2435 @end defvar
2437 @defvar localedir
2438 @ovindex localedir
2439 The directory for installing locale-dependent but
2440 architecture-independent data, such as message catalogs.  This directory
2441 usually has a subdirectory per locale.
2442 @end defvar
2444 @defvar localstatedir
2445 @ovindex localstatedir
2446 The directory for installing modifiable single-machine data.
2447 @end defvar
2449 @defvar mandir
2450 @ovindex mandir
2451 The top-level directory for installing documentation in man format.
2452 @end defvar
2454 @defvar oldincludedir
2455 @ovindex oldincludedir
2456 The directory for installing C header files for non-GCC compilers.
2457 @end defvar
2459 @defvar pdfdir
2460 @ovindex pdfdir
2461 The directory for installing PDF documentation.
2462 @end defvar
2464 @defvar prefix
2465 @ovindex prefix
2466 The common installation prefix for all files.  If @var{exec_prefix}
2467 is defined to a different value, @var{prefix} is used only for
2468 architecture-independent files.
2469 @end defvar
2471 @defvar psdir
2472 @ovindex psdir
2473 The directory for installing PostScript documentation.
2474 @end defvar
2476 @defvar sbindir
2477 @ovindex sbindir
2478 The directory for installing executables that system
2479 administrators run.
2480 @end defvar
2482 @defvar sharedstatedir
2483 @ovindex sharedstatedir
2484 The directory for installing modifiable architecture-independent data.
2485 @end defvar
2487 @defvar sysconfdir
2488 @ovindex sysconfdir
2489 The directory for installing read-only single-machine data.
2490 @end defvar
2493 Most of these variables have values that rely on @code{prefix} or
2494 @code{exec_prefix}.  It is deliberate that the directory output
2495 variables keep them unexpanded: typically @samp{@@datarootdir@@} will be
2496 replaced by @samp{$@{prefix@}/share}, not @samp{/usr/local/share}, and
2497 @samp{@@datadir@@} will be replaced by @samp{$@{datarootdir@}}.
2499 This behavior is mandated by the @acronym{GNU} coding standards, so that when
2500 the user runs:
2502 @table @samp
2503 @item make
2504 she can still specify a different prefix from the one specified to
2505 @command{configure}, in which case, if needed, the package should hard
2506 code dependencies corresponding to the make-specified prefix.
2508 @item make install
2509 she can specify a different installation location, in which case the
2510 package @emph{must} still depend on the location which was compiled in
2511 (i.e., never recompile when @samp{make install} is run).  This is an
2512 extremely important feature, as many people may decide to install all
2513 the files of a package grouped together, and then install links from
2514 the final locations to there.
2515 @end table
2517 In order to support these features, it is essential that
2518 @code{datarootdir} remains being defined as @samp{$@{prefix@}/share} to
2519 depend upon the current value of @code{prefix}.
2521 A corollary is that you should not use these variables except in
2522 Makefiles.  For instance, instead of trying to evaluate @code{datadir}
2523 in @file{configure} and hard-coding it in Makefiles using
2524 e.g., @samp{AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])},
2525 you should add
2526 @option{-DDATADIR='$(datadir)'} to your Makefile's definition of
2527 @code{CPPFLAGS} (@code{AM_CPPFLAGS} if you are also using Automake).
2529 Similarly, you should not rely on @code{AC_CONFIG_FILES} to replace
2530 @code{datadir} and friends in your shell scripts and other files, rather
2531 let @command{make} manage their replacement.  For instance Autoconf
2532 ships templates of its shell scripts ending with @samp{.in}, and uses a
2533 Makefile snippet similar to:
2535 @example
2536 @group
2537 edit = sed \
2538         -e 's|@@datadir[@@]|$(pkgdatadir)|g' \
2539         -e 's|@@prefix[@@]|$(prefix)|g'
2540 @end group
2542 @group
2543 autoconf: Makefile $(srcdir)/autoconf.in
2544         rm -f autoconf autoconf.tmp
2545         $(edit) $(srcdir)/autoconf.in >autoconf.tmp
2546         chmod +x autoconf.tmp
2547         mv autoconf.tmp autoconf
2548 @end group
2550 @group
2551 autoheader: Makefile $(srcdir)/autoheader.in
2552         rm -f autoheader autoheader.tmp
2553         $(edit) $(srcdir)/autoconf.in >autoheader.tmp
2554         chmod +x autoheader.tmp
2555         mv autoheader.tmp autoheader
2556 @end group
2557 @end example
2559 Some details are noteworthy:
2561 @table @samp
2562 @item @@datadir[@@]
2563 The brackets prevent @command{configure} from replacing
2564 @samp{@@datadir@@} in the Sed expression itself.
2565 Brackets are preferable to a backslash here, since
2566 Posix says @samp{\@@} is not portable.
2568 @item $(pkgdatadir)
2569 Don't use @samp{@@pkgdatadir@@}!  Use the matching makefile variable
2570 instead.
2572 @item ,
2573 Don't use @samp{/} in the Sed expression(s) since most likely the
2574 variables you use, such as @samp{$(pkgdatadir)}, will contain
2575 some.
2577 @item Dependency on @file{Makefile}
2578 Since @code{edit} uses values that depend on the configuration specific
2579 values (@code{prefix}, etc.)@: and not only on @code{VERSION} and so forth,
2580 the output depends on @file{Makefile}, not @file{configure.ac}.
2582 @item Separated dependencies and Single Suffix Rules
2583 You can't use them!  The above snippet cannot be (portably) rewritten
2586 @example
2587 autoconf autoheader: Makefile
2588 @group
2589 .in:
2590         rm -f $@@ $@@.tmp
2591         $(edit) $< >$@@.tmp
2592         chmod +x $@@.tmp
2593         mv $@@.tmp $@@
2594 @end group
2595 @end example
2597 @xref{Limitations of Make}, for details.
2599 @item @samp{$(srcdir)}
2600 Be sure to specify the name of the source directory,
2601 otherwise the package won't support separated builds.
2602 @end table
2604 For the more specific installation of Erlang libraries, the following variables
2605 are defined:
2607 @defvar ERLANG_INSTALL_LIB_DIR
2608 @ovindex ERLANG_INSTALL_LIB_DIR
2609 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
2610 The common parent directory of Erlang library installation directories.
2611 This variable is set by calling the @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR}
2612 macro in @file{configure.ac}.
2613 @end defvar
2615 @defvar ERLANG_INSTALL_LIB_DIR_@var{library}
2616 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
2617 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
2618 The installation directory for Erlang library @var{library}.
2619 This variable is set by calling the
2620 @samp{AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR(@var{library}, @var{version}}
2621 macro in @file{configure.ac}.
2622 @end defvar
2624 @xref{Erlang Libraries}, for details.
2627 @node Build Directories
2628 @subsection Build Directories
2629 @cindex Build directories
2630 @cindex Directories, build
2632 You can support compiling a software package for several architectures
2633 simultaneously from the same copy of the source code.  The object files
2634 for each architecture are kept in their own directory.
2636 To support doing this, @command{make} uses the @code{VPATH} variable to
2637 find the files that are in the source directory.  @acronym{GNU} Make
2638 and most other recent @command{make} programs can do this.  Older
2639 @command{make} programs do not support @code{VPATH}; when using them, the
2640 source code must be in the same directory as the object files.
2642 To support @code{VPATH}, each @file{Makefile.in} should contain two
2643 lines that look like:
2645 @example
2646 srcdir = @@srcdir@@
2647 VPATH = @@srcdir@@
2648 @end example
2650 Do not set @code{VPATH} to the value of another variable, for example
2651 @samp{VPATH = $(srcdir)}, because some versions of @command{make} do not do
2652 variable substitutions on the value of @code{VPATH}.
2654 @command{configure} substitutes the correct value for @code{srcdir} when
2655 it produces @file{Makefile}.
2657 Do not use the @code{make} variable @code{$<}, which expands to the
2658 file name of the file in the source directory (found with @code{VPATH}),
2659 except in implicit rules.  (An implicit rule is one such as @samp{.c.o},
2660 which tells how to create a @file{.o} file from a @file{.c} file.)  Some
2661 versions of @command{make} do not set @code{$<} in explicit rules; they
2662 expand it to an empty value.
2664 Instead, @file{Makefile} command lines should always refer to source
2665 files by prefixing them with @samp{$(srcdir)/}.  For example:
2667 @example
2668 time.info: time.texinfo
2669         $(MAKEINFO) $(srcdir)/time.texinfo
2670 @end example
2672 @node Automatic Remaking
2673 @subsection Automatic Remaking
2674 @cindex Automatic remaking
2675 @cindex Remaking automatically
2677 You can put rules like the following in the top-level @file{Makefile.in}
2678 for a package to automatically update the configuration information when
2679 you change the configuration files.  This example includes all of the
2680 optional files, such as @file{aclocal.m4} and those related to
2681 configuration header files.  Omit from the @file{Makefile.in} rules for
2682 any of these files that your package does not use.
2684 The @samp{$(srcdir)/} prefix is included because of limitations in the
2685 @code{VPATH} mechanism.
2687 The @file{stamp-} files are necessary because the timestamps of
2688 @file{config.h.in} and @file{config.h} will not be changed if remaking
2689 them does not change their contents.  This feature avoids unnecessary
2690 recompilation.  You should include the file @file{stamp-h.in} your
2691 package's distribution, so @command{make} will consider
2692 @file{config.h.in} up to date.  Don't use @command{touch}
2693 (@pxref{Limitations of Usual Tools}), rather use @command{echo} (using
2694 @command{date} would cause needless differences, hence @acronym{CVS}
2695 conflicts, etc.).
2697 @example
2698 @group
2699 $(srcdir)/configure: configure.ac aclocal.m4
2700         cd $(srcdir) && autoconf
2702 # autoheader might not change config.h.in, so touch a stamp file.
2703 $(srcdir)/config.h.in: stamp-h.in
2704 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
2705         cd $(srcdir) && autoheader
2706         echo timestamp > $(srcdir)/stamp-h.in
2708 config.h: stamp-h
2709 stamp-h: config.h.in config.status
2710         ./config.status
2712 Makefile: Makefile.in config.status
2713         ./config.status
2715 config.status: configure
2716         ./config.status --recheck
2717 @end group
2718 @end example
2720 @noindent
2721 (Be careful if you copy these lines directly into your Makefile, as you
2722 will need to convert the indented lines to start with the tab character.)
2724 In addition, you should use
2726 @example
2727 AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
2728 @end example
2730 @noindent
2731 so @file{config.status} will ensure that @file{config.h} is considered up to
2732 date.  @xref{Output}, for more information about @code{AC_OUTPUT}.
2734 @xref{config.status Invocation}, for more examples of handling
2735 configuration-related dependencies.
2737 @node Configuration Headers
2738 @section Configuration Header Files
2739 @cindex Configuration Header
2740 @cindex @file{config.h}
2742 When a package contains more than a few tests that define C preprocessor
2743 symbols, the command lines to pass @option{-D} options to the compiler
2744 can get quite long.  This causes two problems.  One is that the
2745 @command{make} output is hard to visually scan for errors.  More
2746 seriously, the command lines can exceed the length limits of some
2747 operating systems.  As an alternative to passing @option{-D} options to
2748 the compiler, @command{configure} scripts can create a C header file
2749 containing @samp{#define} directives.  The @code{AC_CONFIG_HEADERS}
2750 macro selects this kind of output.  Though it can be called anywhere
2751 between @code{AC_INIT} and @code{AC_OUTPUT}, it is customary to call
2752 it right after @code{AC_INIT}.
2754 The package should @samp{#include} the configuration header file before
2755 any other header files, to prevent inconsistencies in declarations (for
2756 example, if it redefines @code{const}).
2758 To provide for VPATH builds, remember to pass the C compiler a @option{-I.}
2759 option (or @option{-I..}; whichever directory contains @file{config.h}).
2760 Even if you use @samp{#include "config.h"}, the preprocessor searches only
2761 the directory of the currently read file, i.e., the source directory, not
2762 the build directory.
2764 With the appropriate @option{-I} option, you can use
2765 @samp{#include <config.h>}.  Actually, it's a good habit to use it,
2766 because in the rare case when the source directory contains another
2767 @file{config.h}, the build directory should be searched first.
2770 @defmac AC_CONFIG_HEADERS (@var{header} @dots{}, @ovar{cmds}, @ovar{init-cmds})
2771 @acindex{CONFIG_HEADERS}
2772 @cvindex HAVE_CONFIG_H
2773 This macro is one of the instantiating macros; see @ref{Configuration
2774 Actions}.  Make @code{AC_OUTPUT} create the file(s) in the
2775 blank-or-newline-separated list @var{header} containing C preprocessor
2776 @code{#define} statements, and replace @samp{@@DEFS@@} in generated
2777 files with @option{-DHAVE_CONFIG_H} instead of the value of @code{DEFS}.
2778 The usual name for @var{header} is @file{config.h}.
2780 If @var{header} already exists and its contents are identical to what
2781 @code{AC_OUTPUT} would put in it, it is left alone.  Doing this allows
2782 making some changes in the configuration without needlessly causing
2783 object files that depend on the header file to be recompiled.
2785 Usually the input file is named @file{@var{header}.in}; however, you can
2786 override the input file name by appending to @var{header} a
2787 colon-separated list of input files.  Examples:
2789 @example
2790 AC_CONFIG_HEADERS([config.h:config.hin])
2791 AC_CONFIG_HEADERS([defines.h:defs.pre:defines.h.in:defs.post])
2792 @end example
2794 @noindent
2795 Doing this allows you to keep your file names acceptable to
2796 @acronym{DOS} variants, or
2797 to prepend and/or append boilerplate to the file.
2798 @end defmac
2800 @defmac AH_HEADER
2801 @ahindex{HEADER}
2802 This macro is defined as the name of the first declared config header
2803 and undefined if no config headers have been declared up to this point.
2804 A third-party macro may, for example, require use of a config header
2805 without invoking AC_CONFIG_HEADERS twice, like this:
2807 @example
2808 AC_CONFIG_COMMANDS_PRE(
2809         [m4_ifndef([AH_HEADER], [AC_CONFIG_HEADERS([config.h])])])
2810 @end example
2812 @end defmac
2814 @xref{Configuration Actions}, for more details on @var{header}.
2816 @menu
2817 * Header Templates::            Input for the configuration headers
2818 * autoheader Invocation::       How to create configuration templates
2819 * Autoheader Macros::           How to specify CPP templates
2820 @end menu
2822 @node Header Templates
2823 @subsection Configuration Header Templates
2824 @cindex Configuration Header Template
2825 @cindex Header templates
2826 @cindex @file{config.h.in}
2828 Your distribution should contain a template file that looks as you want
2829 the final header file to look, including comments, with @code{#undef}
2830 statements which are used as hooks.  For example, suppose your
2831 @file{configure.ac} makes these calls:
2833 @example
2834 AC_CONFIG_HEADERS([conf.h])
2835 AC_CHECK_HEADERS([unistd.h])
2836 @end example
2838 @noindent
2839 Then you could have code like the following in @file{conf.h.in}.  On
2840 systems that have @file{unistd.h}, @command{configure} will @samp{#define}
2841 @samp{HAVE_UNISTD_H} to 1.  On other systems, the whole line will be
2842 commented out (in case the system predefines that symbol).
2844 @example
2845 @group
2846 /* Define as 1 if you have unistd.h.  */
2847 #undef HAVE_UNISTD_H
2848 @end group
2849 @end example
2851 Pay attention that @samp{#undef} is in the first column, and there is
2852 nothing after @samp{HAVE_UNISTD_H}, not even white space.  You can
2853 then decode the configuration header using the preprocessor directives:
2855 @example
2856 @group
2857 #include <conf.h>
2859 #if HAVE_UNISTD_H
2860 # include <unistd.h>
2861 #else
2862 /* We are in trouble.  */
2863 #endif
2864 @end group
2865 @end example
2867 The use of old form templates, with @samp{#define} instead of
2868 @samp{#undef} is strongly discouraged.  Similarly with old templates
2869 with comments on the same line as the @samp{#undef}.  Anyway, putting
2870 comments in preprocessor macros has never been a good idea.
2872 Since it is a tedious task to keep a template header up to date, you may
2873 use @command{autoheader} to generate it, see @ref{autoheader Invocation}.
2876 @node autoheader Invocation
2877 @subsection Using @command{autoheader} to Create @file{config.h.in}
2878 @cindex @command{autoheader}
2880 The @command{autoheader} program can create a template file of C
2881 @samp{#define} statements for @command{configure} to use.  If
2882 @file{configure.ac} invokes @code{AC_CONFIG_HEADERS(@var{file})},
2883 @command{autoheader} creates @file{@var{file}.in}; if multiple file
2884 arguments are given, the first one is used.  Otherwise,
2885 @command{autoheader} creates @file{config.h.in}.
2887 In order to do its job, @command{autoheader} needs you to document all
2888 of the symbols that you might use.  Typically this is done via an
2889 @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED} call whose first argument
2890 is a literal symbol and whose third argument describes the symbol
2891 (@pxref{Defining Symbols}).  Alternatively, you can use
2892 @code{AH_TEMPLATE} (@pxref{Autoheader Macros}), or you can supply a
2893 suitable input file for a subsequent configuration header file.
2894 Symbols defined by Autoconf's builtin tests are already documented properly;
2895 you need to document only those that you
2896 define yourself.
2898 You might wonder why @command{autoheader} is needed: after all, why
2899 would @command{configure} need to ``patch'' a @file{config.h.in} to
2900 produce a @file{config.h} instead of just creating @file{config.h} from
2901 scratch?  Well, when everything rocks, the answer is just that we are
2902 wasting our time maintaining @command{autoheader}: generating
2903 @file{config.h} directly is all that is needed.  When things go wrong,
2904 however, you'll be thankful for the existence of @command{autoheader}.
2906 The fact that the symbols are documented is important in order to
2907 @emph{check} that @file{config.h} makes sense.  The fact that there is a
2908 well-defined list of symbols that should be @code{#define}'d (or not) is
2909 also important for people who are porting packages to environments where
2910 @command{configure} cannot be run: they just have to @emph{fill in the
2911 blanks}.
2913 But let's come back to the point: @command{autoheader}'s invocation@dots{}
2915 If you give @command{autoheader} an argument, it uses that file instead
2916 of @file{configure.ac} and writes the header file to the standard output
2917 instead of to @file{config.h.in}.  If you give @command{autoheader} an
2918 argument of @option{-}, it reads the standard input instead of
2919 @file{configure.ac} and writes the header file to the standard output.
2921 @command{autoheader} accepts the following options:
2923 @table @option
2924 @item --help
2925 @itemx -h
2926 Print a summary of the command line options and exit.
2928 @item --version
2929 @itemx -V
2930 Print the version number of Autoconf and exit.
2932 @item --verbose
2933 @itemx -v
2934 Report processing steps.
2936 @item --debug
2937 @itemx -d
2938 Don't remove the temporary files.
2940 @item --force
2941 @itemx -f
2942 Remake the template file even if newer than its input files.
2944 @item --include=@var{dir}
2945 @itemx -I @var{dir}
2946 Append @var{dir} to the include path.  Multiple invocations accumulate.
2948 @item --prepend-include=@var{dir}
2949 @item -B @var{dir}
2950 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
2952 @item --warnings=@var{category}
2953 @itemx -W @var{category}
2954 @evindex WARNINGS
2955 Report the warnings related to @var{category} (which can actually be a
2956 comma separated list).  Current categories include:
2958 @table @samp
2959 @item obsolete
2960 report the uses of obsolete constructs
2962 @item all
2963 report all the warnings
2965 @item none
2966 report none
2968 @item error
2969 treats warnings as errors
2971 @item no-@var{category}
2972 disable warnings falling into @var{category}
2973 @end table
2975 @end table
2979 @node Autoheader Macros
2980 @subsection Autoheader Macros
2981 @cindex Autoheader macros
2983 @command{autoheader} scans @file{configure.ac} and figures out which C
2984 preprocessor symbols it might define.  It knows how to generate
2985 templates for symbols defined by @code{AC_CHECK_HEADERS},
2986 @code{AC_CHECK_FUNCS} etc., but if you @code{AC_DEFINE} any additional
2987 symbol, you must define a template for it.  If there are missing
2988 templates, @command{autoheader} fails with an error message.
2990 The simplest way to create a template for a @var{symbol} is to supply
2991 the @var{description} argument to an @samp{AC_DEFINE(@var{symbol})}; see
2992 @ref{Defining Symbols}.  You may also use one of the following macros.
2994 @defmac AH_VERBATIM (@var{key}, @var{template})
2995 @ahindex{VERBATIM}
2996 Tell @command{autoheader} to include the @var{template} as-is in the header
2997 template file.  This @var{template} is associated with the @var{key},
2998 which is used to sort all the different templates and guarantee their
2999 uniqueness.  It should be a symbol that can be @code{AC_DEFINE}'d.
3001 For example:
3003 @example
3004 AH_VERBATIM([_GNU_SOURCE],
3005 [/* Enable GNU extensions on systems that have them.  */
3006 #ifndef _GNU_SOURCE
3007 # define _GNU_SOURCE
3008 #endif])
3009 @end example
3010 @end defmac
3013 @defmac AH_TEMPLATE (@var{key}, @var{description})
3014 @ahindex{TEMPLATE}
3015 Tell @command{autoheader} to generate a template for @var{key}.  This macro
3016 generates standard templates just like @code{AC_DEFINE} when a
3017 @var{description} is given.
3019 For example:
3021 @example
3022 AH_TEMPLATE([CRAY_STACKSEG_END],
3023             [Define to one of _getb67, GETB67, getb67
3024              for Cray-2 and Cray-YMP systems.  This
3025              function is required for alloca.c support
3026              on those systems.])
3027 @end example
3029 @noindent
3030 will generate the following template, with the description properly
3031 justified.
3033 @example
3034 /* Define to one of _getb67, GETB67, getb67 for Cray-2 and
3035    Cray-YMP systems.  This function is required for alloca.c
3036    support on those systems.  */
3037 #undef CRAY_STACKSEG_END
3038 @end example
3039 @end defmac
3042 @defmac AH_TOP (@var{text})
3043 @ahindex{TOP}
3044 Include @var{text} at the top of the header template file.
3045 @end defmac
3048 @defmac AH_BOTTOM (@var{text})
3049 @ahindex{BOTTOM}
3050 Include @var{text} at the bottom of the header template file.
3051 @end defmac
3054 @node Configuration Commands
3055 @section Running Arbitrary Configuration Commands
3056 @cindex Configuration commands
3057 @cindex Commands for configuration
3059 You can execute arbitrary commands before, during, and after
3060 @file{config.status} is run.  The three following macros accumulate the
3061 commands to run when they are called multiple times.
3062 @code{AC_CONFIG_COMMANDS} replaces the obsolete macro
3063 @code{AC_OUTPUT_COMMANDS}; see @ref{Obsolete Macros}, for details.
3065 @defmac AC_CONFIG_COMMANDS (@var{tag}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3066 @acindex{CONFIG_COMMANDS}
3067 Specify additional shell commands to run at the end of
3068 @file{config.status}, and shell commands to initialize any variables
3069 from @command{configure}.  Associate the commands with @var{tag}.
3070 Since typically the @var{cmds} create a file, @var{tag} should
3071 naturally be the name of that file.  If needed, the directory hosting
3072 @var{tag} is created.  This macro is one of the instantiating macros;
3073 see @ref{Configuration Actions}.
3075 Here is an unrealistic example:
3076 @example
3077 fubar=42
3078 AC_CONFIG_COMMANDS([fubar],
3079                    [echo this is extra $fubar, and so on.],
3080                    [fubar=$fubar])
3081 @end example
3083 Here is a better one:
3084 @example
3085 AC_CONFIG_COMMANDS([time-stamp], [date >time-stamp])
3086 @end example
3087 @end defmac
3089 The following two macros look similar, but in fact they are not of the same
3090 breed: they are executed directly by @file{configure}, so you cannot use
3091 @file{config.status} to re-run them.
3093 @c Yet it is good to leave them here.  The user sees them together and
3094 @c decides which best fits their needs.
3096 @defmac AC_CONFIG_COMMANDS_PRE (@var{cmds})
3097 @acindex{CONFIG_COMMANDS_PRE}
3098 Execute the @var{cmds} right before creating @file{config.status}.
3100 This macro presents the last opportunity to call @code{AC_SUBST},
3101 @code{AC_DEFINE}, or @code{AC_CONFIG_FOOS} macros.
3102 @end defmac
3104 @defmac AC_CONFIG_COMMANDS_POST (@var{cmds})
3105 @acindex{CONFIG_COMMANDS_POST}
3106 Execute the @var{cmds} right after creating @file{config.status}.
3107 @end defmac
3112 @node Configuration Links
3113 @section Creating Configuration Links
3114 @cindex Configuration links
3115 @cindex Links for configuration
3117 You may find it convenient to create links whose destinations depend upon
3118 results of tests.  One can use @code{AC_CONFIG_COMMANDS} but the
3119 creation of relative symbolic links can be delicate when the package is
3120 built in a directory different from the source directory.
3122 @defmac AC_CONFIG_LINKS (@var{dest}:@var{source}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3123 @acindex{CONFIG_LINKS}
3124 @cindex Links
3125 Make @code{AC_OUTPUT} link each of the existing files @var{source} to
3126 the corresponding link name @var{dest}.  Makes a symbolic link if
3127 possible, otherwise a hard link if possible, otherwise a copy.  The
3128 @var{dest} and @var{source} names should be relative to the top level
3129 source or build directory.  This macro is one of the instantiating
3130 macros; see @ref{Configuration Actions}.
3132 For example, this call:
3134 @example
3135 AC_CONFIG_LINKS([host.h:config/$machine.h
3136                 object.h:config/$obj_format.h])
3137 @end example
3139 @noindent
3140 creates in the current directory @file{host.h} as a link to
3141 @file{@var{srcdir}/config/$machine.h}, and @file{object.h} as a
3142 link to @file{@var{srcdir}/config/$obj_format.h}.
3144 The tempting value @samp{.} for @var{dest} is invalid: it makes it
3145 impossible for @samp{config.status} to guess the links to establish.
3147 One can then run:
3148 @example
3149 ./config.status host.h object.h
3150 @end example
3151 @noindent
3152 to create the links.
3153 @end defmac
3157 @node Subdirectories
3158 @section Configuring Other Packages in Subdirectories
3159 @cindex Configure subdirectories
3160 @cindex Subdirectory configure
3162 In most situations, calling @code{AC_OUTPUT} is sufficient to produce
3163 @file{Makefile}s in subdirectories.  However, @command{configure} scripts
3164 that control more than one independent package can use
3165 @code{AC_CONFIG_SUBDIRS} to run @command{configure} scripts for other
3166 packages in subdirectories.
3168 @defmac AC_CONFIG_SUBDIRS (@var{dir} @dots{})
3169 @acindex{CONFIG_SUBDIRS}
3170 @ovindex subdirs
3171 Make @code{AC_OUTPUT} run @command{configure} in each subdirectory
3172 @var{dir} in the given blank-or-newline-separated list.  Each @var{dir} should
3173 be a literal, i.e., please do not use:
3175 @example
3176 if test "$package_foo_enabled" = yes; then
3177   $my_subdirs="$my_subdirs foo"
3179 AC_CONFIG_SUBDIRS([$my_subdirs])
3180 @end example
3182 @noindent
3183 because this prevents @samp{./configure --help=recursive} from
3184 displaying the options of the package @code{foo}.  Rather, you should
3185 write:
3187 @example
3188 if test "$package_foo_enabled" = yes; then
3189   AC_CONFIG_SUBDIRS([foo])
3191 @end example
3193 If a given @var{dir} is not found, an error is reported: if the
3194 subdirectory is optional, write:
3196 @example
3197 if test -d $srcdir/foo; then
3198   AC_CONFIG_SUBDIRS([foo])
3200 @end example
3202 @c NB: Yes, below we mean configure.in, not configure.ac.
3203 If a given @var{dir} contains @command{configure.gnu}, it is run instead
3204 of @command{configure}.  This is for packages that might use a
3205 non-Autoconf script @command{Configure}, which can't be called through a
3206 wrapper @command{configure} since it would be the same file on
3207 case-insensitive file systems.  Likewise, if a @var{dir} contains
3208 @file{configure.in} but no @command{configure}, the Cygnus
3209 @command{configure} script found by @code{AC_CONFIG_AUX_DIR} is used.
3211 The subdirectory @command{configure} scripts are given the same command
3212 line options that were given to this @command{configure} script, with minor
3213 changes if needed, which include:
3215 @itemize @minus
3216 @item
3217 adjusting a relative name for the cache file;
3219 @item
3220 adjusting a relative name for the source directory;
3222 @item
3223 propagating the current value of @code{$prefix}, including if it was
3224 defaulted, and if the default values of the top level and of the subdirectory
3225 @file{configure} differ.
3226 @end itemize
3228 This macro also sets the output variable @code{subdirs} to the list of
3229 directories @samp{@var{dir} @dots{}}.  @file{Makefile} rules can use
3230 this variable to determine which subdirectories to recurse into.
3232 This macro may be called multiple times.
3233 @end defmac
3235 @node Default Prefix
3236 @section Default Prefix
3237 @cindex Install prefix
3238 @cindex Prefix for install
3240 By default, @command{configure} sets the prefix for files it installs to
3241 @file{/usr/local}.  The user of @command{configure} can select a different
3242 prefix using the @option{--prefix} and @option{--exec-prefix} options.
3243 There are two ways to change the default: when creating
3244 @command{configure}, and when running it.
3246 Some software packages might want to install in a directory other than
3247 @file{/usr/local} by default.  To accomplish that, use the
3248 @code{AC_PREFIX_DEFAULT} macro.
3250 @defmac AC_PREFIX_DEFAULT (@var{prefix})
3251 @acindex{PREFIX_DEFAULT}
3252 Set the default installation prefix to @var{prefix} instead of
3253 @file{/usr/local}.
3254 @end defmac
3256 It may be convenient for users to have @command{configure} guess the
3257 installation prefix from the location of a related program that they
3258 have already installed.  If you wish to do that, you can call
3259 @code{AC_PREFIX_PROGRAM}.
3261 @defmac AC_PREFIX_PROGRAM (@var{program})
3262 @acindex{PREFIX_PROGRAM}
3263 If the user did not specify an installation prefix (using the
3264 @option{--prefix} option), guess a value for it by looking for
3265 @var{program} in @env{PATH}, the way the shell does.  If @var{program}
3266 is found, set the prefix to the parent of the directory containing
3267 @var{program}, else default the prefix as described above
3268 (@file{/usr/local} or @code{AC_PREFIX_DEFAULT}).  For example, if
3269 @var{program} is @code{gcc} and the @env{PATH} contains
3270 @file{/usr/local/gnu/bin/gcc}, set the prefix to @file{/usr/local/gnu}.
3271 @end defmac
3275 @c ======================================================== Existing tests
3277 @node Existing Tests
3278 @chapter Existing Tests
3280 These macros test for particular system features that packages might
3281 need or want to use.  If you need to test for a kind of feature that
3282 none of these macros check for, you can probably do it by calling
3283 primitive test macros with appropriate arguments (@pxref{Writing
3284 Tests}).
3286 These tests print messages telling the user which feature they're
3287 checking for, and what they find.  They cache their results for future
3288 @command{configure} runs (@pxref{Caching Results}).
3290 Some of these macros set output variables.  @xref{Makefile
3291 Substitutions}, for how to get their values.  The phrase ``define
3292 @var{name}'' is used below as a shorthand to mean ``define C
3293 preprocessor symbol @var{name} to the value 1''.  @xref{Defining
3294 Symbols}, for how to get those symbol definitions into your program.
3296 @menu
3297 * Common Behavior::             Macros' standard schemes
3298 * Alternative Programs::        Selecting between alternative programs
3299 * Files::                       Checking for the existence of files
3300 * Libraries::                   Library archives that might be missing
3301 * Library Functions::           C library functions that might be missing
3302 * Header Files::                Header files that might be missing
3303 * Declarations::                Declarations that may be missing
3304 * Structures::                  Structures or members that might be missing
3305 * Types::                       Types that might be missing
3306 * Compilers and Preprocessors::  Checking for compiling programs
3307 * System Services::             Operating system services
3308 * Posix Variants::              Special kludges for specific Posix variants
3309 * Erlang Libraries::            Checking for the existence of Erlang libraries
3310 @end menu
3312 @node Common Behavior
3313 @section Common Behavior
3314 @cindex Common autoconf behavior
3316 Much effort has been expended to make Autoconf easy to learn.  The most
3317 obvious way to reach this goal is simply to enforce standard interfaces
3318 and behaviors, avoiding exceptions as much as possible.  Because of
3319 history and inertia, unfortunately, there are still too many exceptions
3320 in Autoconf; nevertheless, this section describes some of the common
3321 rules.
3323 @menu
3324 * Standard Symbols::            Symbols defined by the macros
3325 * Default Includes::            Includes used by the generic macros
3326 @end menu
3328 @node Standard Symbols
3329 @subsection Standard Symbols
3330 @cindex Standard symbols
3332 All the generic macros that @code{AC_DEFINE} a symbol as a result of
3333 their test transform their @var{argument}s to a standard alphabet.
3334 First, @var{argument} is converted to upper case and any asterisks
3335 (@samp{*}) are each converted to @samp{P}.  Any remaining characters
3336 that are not alphanumeric are converted to underscores.
3338 For instance,
3340 @example
3341 AC_CHECK_TYPES([struct $Expensive*])
3342 @end example
3344 @noindent
3345 will define the symbol @samp{HAVE_STRUCT__EXPENSIVEP} if the check
3346 succeeds.
3349 @node Default Includes
3350 @subsection Default Includes
3351 @cindex Default includes
3352 @cindex Includes, default
3354 Several tests depend upon a set of header files.  Since these headers
3355 are not universally available, tests actually have to provide a set of
3356 protected includes, such as:
3358 @example
3359 @group
3360 #if TIME_WITH_SYS_TIME
3361 # include <sys/time.h>
3362 # include <time.h>
3363 #else
3364 # if HAVE_SYS_TIME_H
3365 #  include <sys/time.h>
3366 # else
3367 #  include <time.h>
3368 # endif
3369 #endif
3370 @end group
3371 @end example
3373 @noindent
3374 Unless you know exactly what you are doing, you should avoid using
3375 unconditional includes, and check the existence of the headers you
3376 include beforehand (@pxref{Header Files}).
3378 Most generic macros use the following macro to provide the default set
3379 of includes:
3381 @defmac AC_INCLUDES_DEFAULT (@ovar{include-directives})
3382 @acindex{INCLUDES_DEFAULT}
3383 Expand to @var{include-directives} if defined, otherwise to:
3385 @example
3386 @group
3387 #include <stdio.h>
3388 #if HAVE_SYS_TYPES_H
3389 # include <sys/types.h>
3390 #endif
3391 #if HAVE_SYS_STAT_H
3392 # include <sys/stat.h>
3393 #endif
3394 #if STDC_HEADERS
3395 # include <stdlib.h>
3396 # include <stddef.h>
3397 #else
3398 # if HAVE_STDLIB_H
3399 #  include <stdlib.h>
3400 # endif
3401 #endif
3402 #if HAVE_STRING_H
3403 # if !STDC_HEADERS && HAVE_MEMORY_H
3404 #  include <memory.h>
3405 # endif
3406 # include <string.h>
3407 #endif
3408 #if HAVE_STRINGS_H
3409 # include <strings.h>
3410 #endif
3411 #if HAVE_INTTYPES_H
3412 # include <inttypes.h>
3413 #endif
3414 #if HAVE_STDINT_H
3415 # include <stdint.h>
3416 #endif
3417 #if HAVE_UNISTD_H
3418 # include <unistd.h>
3419 #endif
3420 @end group
3421 @end example
3423 If the default includes are used, then check for the presence of these
3424 headers and their compatibility, i.e., you don't need to run
3425 @code{AC_HEADER_STDC}, nor check for @file{stdlib.h} etc.
3427 These headers are checked for in the same order as they are included.
3428 For instance, on some systems @file{string.h} and @file{strings.h} both
3429 exist, but conflict.  Then @code{HAVE_STRING_H} will be defined, but
3430 @code{HAVE_STRINGS_H} won't.
3431 @end defmac
3433 @node Alternative Programs
3434 @section Alternative Programs
3435 @cindex Programs, checking
3437 These macros check for the presence or behavior of particular programs.
3438 They are used to choose between several alternative programs and to
3439 decide what to do once one has been chosen.  If there is no macro
3440 specifically defined to check for a program you need, and you don't need
3441 to check for any special properties of it, then you can use one of the
3442 general program-check macros.
3444 @menu
3445 * Particular Programs::         Special handling to find certain programs
3446 * Generic Programs::            How to find other programs
3447 @end menu
3449 @node Particular Programs
3450 @subsection Particular Program Checks
3452 These macros check for particular programs---whether they exist, and
3453 in some cases whether they support certain features.
3455 @defmac AC_PROG_AWK
3456 @acindex{PROG_AWK}
3457 @ovindex AWK
3458 Check for @code{gawk}, @code{mawk}, @code{nawk}, and @code{awk}, in that
3459 order, and set output variable @code{AWK} to the first one that is found.
3460 It tries @code{gawk} first because that is reported to be the
3461 best implementation.
3462 @end defmac
3464 @defmac AC_PROG_GREP
3465 @acindex{PROG_GREP}
3466 @ovindex GREP
3467 Look for the best available @code{grep} or @code{ggrep} that accepts the
3468 longest input lines possible, and that supports multiple @option{-e} options.
3469 Set the output variable @code{GREP} to whatever is chosen.
3470 @xref{Limitations of Usual Tools}, for more information about
3471 portability problems with the @command{grep} command family.
3472 @end defmac
3474 @defmac AC_PROG_EGREP
3475 @acindex{PROG_EGREP}
3476 @ovindex EGREP
3477 Check whether @code{$GREP -E} works, or else look for the best available
3478 @code{egrep} or @code{gegrep} that accepts the longest input lines possible.
3479 Set the output variable @code{EGREP} to whatever is chosen.
3480 @end defmac
3482 @defmac AC_PROG_FGREP
3483 @acindex{PROG_FGREP}
3484 @ovindex FGREP
3485 Check whether @code{$GREP -F} works, or else look for the best available
3486 @code{fgrep} or @code{gfgrep} that accepts the longest input lines possible.
3487 Set the output variable @code{FGREP} to whatever is chosen.
3488 @end defmac
3490 @defmac AC_PROG_INSTALL
3491 @acindex{PROG_INSTALL}
3492 @ovindex INSTALL
3493 @ovindex INSTALL_PROGRAM
3494 @ovindex INSTALL_DATA
3495 @ovindex INSTALL_SCRIPT
3496 Set output variable @code{INSTALL} to the name of a @acronym{BSD}-compatible
3497 @command{install} program, if one is found in the current @env{PATH}.
3498 Otherwise, set @code{INSTALL} to @samp{@var{dir}/install-sh -c},
3499 checking the directories specified to @code{AC_CONFIG_AUX_DIR} (or its
3500 default directories) to determine @var{dir} (@pxref{Output}).  Also set
3501 the variables @code{INSTALL_PROGRAM} and @code{INSTALL_SCRIPT} to
3502 @samp{$@{INSTALL@}} and @code{INSTALL_DATA} to @samp{$@{INSTALL@} -m 644}.
3504 This macro screens out various instances of @command{install} known not to
3505 work.  It prefers to find a C program rather than a shell script, for
3506 speed.  Instead of @file{install-sh}, it can also use @file{install.sh},
3507 but that name is obsolete because some @command{make} programs have a rule
3508 that creates @file{install} from it if there is no @file{Makefile}.
3510 Autoconf comes with a copy of @file{install-sh} that you can use.  If
3511 you use @code{AC_PROG_INSTALL}, you must include either
3512 @file{install-sh} or @file{install.sh} in your distribution, or
3513 @command{configure} will produce an error message saying it can't find
3514 them---even if the system you're on has a good @command{install} program.
3515 This check is a safety measure to prevent you from accidentally leaving
3516 that file out, which would prevent your package from installing on
3517 systems that don't have a @acronym{BSD}-compatible @command{install} program.
3519 If you need to use your own installation program because it has features
3520 not found in standard @command{install} programs, there is no reason to use
3521 @code{AC_PROG_INSTALL}; just put the file name of your program into your
3522 @file{Makefile.in} files.
3523 @end defmac
3525 @defmac AC_PROG_MKDIR_P
3526 @acindex{AC_PROG_MKDIR_P}
3527 @ovindex MKDIR_P
3528 Set output variable @code{MKDIR_P} to a program that ensures that for
3529 each argument, a directory named by this argument exists, creating it
3530 and its parent directories if needed, and without race conditions when
3531 two instances of the program attempt to make the same directory at
3532 nearly the same time.
3534 This macro uses the @samp{mkdir -p} command if possible.  Otherwise, it
3535 falls back on invoking @command{install-sh} with the @option{-d} option,
3536 so your package should
3537 contain @file{install-sh} as described under @code{AC_PROG_INSTALL}.
3538 An @file{install-sh} file that predates Autoconf 2.60 or Automake 1.10
3539 is vulnerable to race conditions, so if you want to support parallel
3540 installs from
3541 different packages into the same directory you need to make sure you
3542 have an up-to-date @file{install-sh}.  In particular, be careful about
3543 using @samp{autoreconf -if} if your Automake predates Automake 1.10.
3545 This macro is related to the @code{AS_MKDIR_P} macro (@pxref{Programming
3546 in M4sh}), but it sets an output variable intended for use in other
3547 files, whereas @code{AS_MKDIR_P} is intended for use in scripts like
3548 @command{configure}.  Also, @code{AS_MKDIR_P} does not accept options,
3549 but @code{MKDIR_P} supports the @option{-m} option, e.g., a makefile
3550 might invoke @code{$(MKDIR_P) -m 0 dir} to create an inaccessible
3551 directory, and conversely a makefile should use @code{$(MKDIR_P) --
3552 $(FOO)} if @var{FOO} might yield a value that begins with @samp{-}.
3553 Finally, @code{AS_MKDIR_P} does not check for race condition
3554 vulnerability, whereas @code{AC_PROG_MKDIR_P} does.
3555 @end defmac
3557 @defmac AC_PROG_LEX
3558 @acindex{PROG_LEX}
3559 @ovindex LEX
3560 @ovindex LEXLIB
3561 @cvindex YYTEXT_POINTER
3562 @ovindex LEX_OUTPUT_ROOT
3563 If @code{flex} is found, set output variable @code{LEX} to @samp{flex}
3564 and @code{LEXLIB} to @option{-lfl}, if that library is in a standard
3565 place.  Otherwise set @code{LEX} to @samp{lex} and @code{LEXLIB} to
3566 @option{-ll}.
3568 Define @code{YYTEXT_POINTER} if @code{yytext} is a @samp{char *} instead
3569 of a @samp{char []}.  Also set output variable @code{LEX_OUTPUT_ROOT} to
3570 the base of the file name that the lexer generates; usually
3571 @file{lex.yy}, but sometimes something else.  These results vary
3572 according to whether @code{lex} or @code{flex} is being used.
3574 You are encouraged to use Flex in your sources, since it is both more
3575 pleasant to use than plain Lex and the C source it produces is portable.
3576 In order to ensure portability, however, you must either provide a
3577 function @code{yywrap} or, if you don't use it (e.g., your scanner has
3578 no @samp{#include}-like feature), simply include a @samp{%noyywrap}
3579 statement in the scanner's source.  Once this done, the scanner is
3580 portable (unless @emph{you} felt free to use nonportable constructs) and
3581 does not depend on any library.  In this case, and in this case only, it
3582 is suggested that you use this Autoconf snippet:
3584 @example
3585 AC_PROG_LEX
3586 if test "$LEX" != flex; then
3587   LEX="$SHELL $missing_dir/missing flex"
3588   AC_SUBST([LEX_OUTPUT_ROOT], [lex.yy])
3589   AC_SUBST([LEXLIB], [''])
3591 @end example
3593 The shell script @command{missing} can be found in the Automake
3594 distribution.
3596 To ensure backward compatibility, Automake's @code{AM_PROG_LEX} invokes
3597 (indirectly) this macro twice, which will cause an annoying but benign
3598 ``@code{AC_PROG_LEX} invoked multiple times'' warning.  Future versions
3599 of Automake will fix this issue; meanwhile, just ignore this message.
3601 As part of running the test, this macro may delete any file in the
3602 configuration directory named @file{lex.yy.c} or @file{lexyy.c}.
3603 @end defmac
3605 @defmac AC_PROG_LN_S
3606 @acindex{PROG_LN_S}
3607 @ovindex LN_S
3608 If @samp{ln -s} works on the current file system (the operating system
3609 and file system support symbolic links), set the output variable
3610 @code{LN_S} to @samp{ln -s}; otherwise, if @samp{ln} works, set
3611 @code{LN_S} to @samp{ln}, and otherwise set it to @samp{cp -p}.
3613 If you make a link in a directory other than the current directory, its
3614 meaning depends on whether @samp{ln} or @samp{ln -s} is used.  To safely
3615 create links using @samp{$(LN_S)}, either find out which form is used
3616 and adjust the arguments, or always invoke @code{ln} in the directory
3617 where the link is to be created.
3619 In other words, it does not work to do:
3620 @example
3621 $(LN_S) foo /x/bar
3622 @end example
3624 Instead, do:
3626 @example
3627 (cd /x && $(LN_S) foo bar)
3628 @end example
3629 @end defmac
3631 @defmac AC_PROG_RANLIB
3632 @acindex{PROG_RANLIB}
3633 @ovindex RANLIB
3634 Set output variable @code{RANLIB} to @samp{ranlib} if @code{ranlib}
3635 is found, and otherwise to @samp{:} (do nothing).
3636 @end defmac
3638 @defmac AC_PROG_SED
3639 @acindex{PROG_SED}
3640 @ovindex SED
3641 Set output variable @code{SED} to a Sed implementation that conforms to
3642 Posix and does not have arbitrary length limits.  Report an error if no
3643 acceptable Sed is found.  @xref{Limitations of Usual Tools}, for more
3644 information about portability problems with Sed.
3645 @end defmac
3647 @defmac AC_PROG_YACC
3648 @acindex{PROG_YACC}
3649 @ovindex YACC
3650 If @code{bison} is found, set output variable @code{YACC} to @samp{bison
3651 -y}.  Otherwise, if @code{byacc} is found, set @code{YACC} to
3652 @samp{byacc}.  Otherwise set @code{YACC} to @samp{yacc}.
3653 @end defmac
3655 @node Generic Programs
3656 @subsection Generic Program and File Checks
3658 These macros are used to find programs not covered by the ``particular''
3659 test macros.  If you need to check the behavior of a program as well as
3660 find out whether it is present, you have to write your own test for it
3661 (@pxref{Writing Tests}).  By default, these macros use the environment
3662 variable @env{PATH}.  If you need to check for a program that might not
3663 be in the user's @env{PATH}, you can pass a modified path to use
3664 instead, like this:
3666 @example
3667 AC_PATH_PROG([INETD], [inetd], [/usr/libexec/inetd],
3668              [$PATH:/usr/libexec:/usr/sbin:/usr/etc:/etc])
3669 @end example
3671 You are strongly encouraged to declare the @var{variable} passed to
3672 @code{AC_CHECK_PROG} etc.@: as precious, @xref{Setting Output Variables},
3673 @code{AC_ARG_VAR}, for more details.
3675 @defmac AC_CHECK_PROG (@var{variable}, @var{prog-to-check-for}, @var{value-if-found}, @ovar{value-if-not-found}, @ovar{path},  @ovar{reject})
3676 @acindex{CHECK_PROG}
3677 Check whether program @var{prog-to-check-for} exists in @env{PATH}.  If
3678 it is found, set @var{variable} to @var{value-if-found}, otherwise to
3679 @var{value-if-not-found}, if given.  Always pass over @var{reject} (an
3680 absolute file name) even if it is the first found in the search path; in
3681 that case, set @var{variable} using the absolute file name of the
3682 @var{prog-to-check-for} found that is not @var{reject}.  If
3683 @var{variable} was already set, do nothing.  Calls @code{AC_SUBST} for
3684 @var{variable}.
3685 @end defmac
3687 @defmac AC_CHECK_PROGS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3688 @acindex{CHECK_PROGS}
3689 Check for each program in the blank-separated list
3690 @var{progs-to-check-for} existing in the @env{PATH}.  If one is found, set
3691 @var{variable} to the name of that program.  Otherwise, continue
3692 checking the next program in the list.  If none of the programs in the
3693 list are found, set @var{variable} to @var{value-if-not-found}; if
3694 @var{value-if-not-found} is not specified, the value of @var{variable}
3695 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3696 @end defmac
3698 @defmac AC_CHECK_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3699 @acindex{CHECK_TARGET_TOOL}
3700 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3701 with a prefix of the target type as determined by
3702 @code{AC_CANONICAL_TARGET}, followed by a dash (@pxref{Canonicalizing}).
3703 If the tool cannot be found with a prefix, and if the build and target
3704 types are equal, then it is also searched for without a prefix.
3706 As noted in @ref{Specifying Names, , Specifying the system type}, the
3707 target is rarely specified, because most of the time it is the same
3708 as the host: it is the type of system for which any compiler tools in
3709 the package will produce code.  What this macro will look for is,
3710 for example, @emph{a tool @r{(assembler, linker, etc.)}@: that the
3711 compiler driver @r{(@command{gcc} for the @acronym{GNU} C Compiler)}
3712 will use to produce objects, archives or executables}.
3713 @end defmac
3715 @defmac AC_CHECK_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3716 @acindex{CHECK_TOOL}
3717 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3718 with a prefix of the host type as determined by
3719 @code{AC_CANONICAL_HOST}, followed by a dash (@pxref{Canonicalizing}).
3720 For example, if the user runs @samp{configure --host=i386-gnu}, then
3721 this call:
3722 @example
3723 AC_CHECK_TOOL([RANLIB], [ranlib], [:])
3724 @end example
3725 @noindent
3726 sets @code{RANLIB} to @file{i386-gnu-ranlib} if that program exists in
3727 @env{PATH}, or otherwise to @samp{ranlib} if that program exists in
3728 @env{PATH}, or to @samp{:} if neither program exists.
3730 In the future, when cross-compiling this macro will @emph{only}
3731 accept program names that are prefixed with the host type.
3732 For more information, see @ref{Specifying Names, , Specifying the
3733 system type}.
3734 @end defmac
3736 @defmac AC_CHECK_TARGET_TOOLS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3737 @acindex{CHECK_TARGET_TOOLS}
3738 Like @code{AC_CHECK_TARGET_TOOL}, each of the tools in the list
3739 @var{progs-to-check-for} are checked with a prefix of the target type as
3740 determined by @code{AC_CANONICAL_TARGET}, followed by a dash
3741 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3742 prefix, and if the build and target types are equal, then the first one
3743 without a prefix is used.  If a tool is found, set @var{variable} to
3744 the name of that program.  If none of the tools in the list are found,
3745 set @var{variable} to @var{value-if-not-found}; if @var{value-if-not-found}
3746 is not specified, the value of @var{variable} is not changed.  Calls
3747 @code{AC_SUBST} for @var{variable}.
3748 @end defmac
3750 @defmac AC_CHECK_TOOLS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3751 @acindex{CHECK_TOOLS}
3752 Like @code{AC_CHECK_TOOL}, each of the tools in the list
3753 @var{progs-to-check-for} are checked with a prefix of the host type as
3754 determined by @code{AC_CANONICAL_HOST}, followed by a dash
3755 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3756 prefix, then the first one without a prefix is used.  If a tool is found,
3757 set @var{variable} to the name of that program.  If none of the tools in
3758 the list are found, set @var{variable} to @var{value-if-not-found}; if
3759 @var{value-if-not-found} is not specified, the value of @var{variable}
3760 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3762 In the future, when cross-compiling this macro will @emph{not}
3763 accept program names that are not prefixed with the host type.
3764 @end defmac
3766 @defmac AC_PATH_PROG (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3767 @acindex{PATH_PROG}
3768 Like @code{AC_CHECK_PROG}, but set @var{variable} to the absolute
3769 name of @var{prog-to-check-for} if found.
3770 @end defmac
3772 @defmac AC_PATH_PROGS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3773 @acindex{PATH_PROGS}
3774 Like @code{AC_CHECK_PROGS}, but if any of @var{progs-to-check-for}
3775 are found, set @var{variable} to the absolute name of the program
3776 found.
3777 @end defmac
3779 @defmac AC_PATH_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3780 @acindex{PATH_TARGET_TOOL}
3781 Like @code{AC_CHECK_TARGET_TOOL}, but set @var{variable} to the absolute
3782 name of the program if it is found.
3783 @end defmac
3785 @defmac AC_PATH_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3786 @acindex{PATH_TOOL}
3787 Like @code{AC_CHECK_TOOL}, but set @var{variable} to the absolute
3788 name of the program if it is found.
3790 In the future, when cross-compiling this macro will @emph{not}
3791 accept program names that are not prefixed with the host type.
3792 @end defmac
3795 @node Files
3796 @section Files
3797 @cindex File, checking
3799 You might also need to check for the existence of files.  Before using
3800 these macros, ask yourself whether a runtime test might not be a better
3801 solution.  Be aware that, like most Autoconf macros, they test a feature
3802 of the host machine, and therefore, they die when cross-compiling.
3804 @defmac AC_CHECK_FILE (@var{file}, @ovar{action-if-found}, @ovar{action-if-not-found})
3805 @acindex{CHECK_FILE}
3806 Check whether file @var{file} exists on the native system.  If it is
3807 found, execute @var{action-if-found}, otherwise do
3808 @var{action-if-not-found}, if given.
3809 @end defmac
3811 @defmac AC_CHECK_FILES (@var{files}, @ovar{action-if-found}, @ovar{action-if-not-found})
3812 @acindex{CHECK_FILES}
3813 Executes @code{AC_CHECK_FILE} once for each file listed in @var{files}.
3814 Additionally, defines @samp{HAVE_@var{file}} (@pxref{Standard Symbols})
3815 for each file found.
3816 @end defmac
3819 @node Libraries
3820 @section Library Files
3821 @cindex Library, checking
3823 The following macros check for the presence of certain C, C++, or Fortran
3824 library archive files.
3826 @defmac AC_CHECK_LIB (@var{library}, @var{function}, @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
3827 @acindex{CHECK_LIB}
3828 Test whether the library @var{library} is available by trying to link
3829 a test program that calls function @var{function} with the library.
3830 @var{function} should be a function provided by the library.
3831 Use the base
3832 name of the library; e.g., to check for @option{-lmp}, use @samp{mp} as
3833 the @var{library} argument.
3835 @var{action-if-found} is a list of shell commands to run if the link
3836 with the library succeeds; @var{action-if-not-found} is a list of shell
3837 commands to run if the link fails.  If @var{action-if-found} is not
3838 specified, the default action will prepend @option{-l@var{library}} to
3839 @code{LIBS} and define @samp{HAVE_LIB@var{library}} (in all
3840 capitals).  This macro is intended to support building @code{LIBS} in
3841 a right-to-left (least-dependent to most-dependent) fashion such that
3842 library dependencies are satisfied as a natural side-effect of
3843 consecutive tests.  Some linkers are very sensitive to library ordering
3844 so the order in which @code{LIBS} is generated is important to reliable
3845 detection of libraries.
3847 If linking with @var{library} results in unresolved symbols that would
3848 be resolved by linking with additional libraries, give those libraries
3849 as the @var{other-libraries} argument, separated by spaces:
3850 e.g., @option{-lXt -lX11}.  Otherwise, this macro will fail to detect
3851 that @var{library} is present, because linking the test program will
3852 always fail with unresolved symbols.  The @var{other-libraries} argument
3853 should be limited to cases where it is desirable to test for one library
3854 in the presence of another that is not already in @code{LIBS}.
3856 @code{AC_CHECK_LIB} requires some care in usage, and should be avoided
3857 in some common cases.  Many standard functions like @code{gethostbyname}
3858 appear the standard C library on some hosts, and in special libraries
3859 like @code{nsl} on other hosts.  On some hosts the special libraries
3860 contain variant implementations that you may not want to use.  These
3861 days it is normally better to use @code{AC_SEARCH_LIBS([gethostbyname],
3862 [nsl])} instead of @code{AC_CHECK_LIB([nsl], [gethostbyname])}.
3863 @end defmac
3866 @defmac AC_SEARCH_LIBS (@var{function}, @var{search-libs}, @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
3867 @acindex{SEARCH_LIBS}
3868 Search for a library defining @var{function} if it's not already
3869 available.  This equates to calling
3870 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])])} first with
3871 no libraries, then for each library listed in @var{search-libs}.
3873 Add @option{-l@var{library}} to @code{LIBS} for the first library found
3874 to contain @var{function}, and run @var{action-if-found}.  If the
3875 function is not found, run @var{action-if-not-found}.
3877 If linking with @var{library} results in unresolved symbols that would
3878 be resolved by linking with additional libraries, give those libraries
3879 as the @var{other-libraries} argument, separated by spaces:
3880 e.g., @option{-lXt -lX11}.  Otherwise, this macro will fail to detect
3881 that @var{function} is present, because linking the test program will
3882 always fail with unresolved symbols.
3883 @end defmac
3887 @node Library Functions
3888 @section Library Functions
3890 The following macros check for particular C library functions.
3891 If there is no macro specifically defined to check for a function you need,
3892 and you don't need to check for any special properties of
3893 it, then you can use one of the general function-check macros.
3895 @menu
3896 * Function Portability::        Pitfalls with usual functions
3897 * Particular Functions::        Special handling to find certain functions
3898 * Generic Functions::           How to find other functions
3899 @end menu
3901 @node Function Portability
3902 @subsection Portability of C Functions
3903 @cindex Portability of C functions
3904 @cindex C function portability
3906 Most usual functions can either be missing, or be buggy, or be limited
3907 on some architectures.  This section tries to make an inventory of these
3908 portability issues.  By definition, this list will always require
3909 additions.  Please help us keeping it as complete as possible.
3911 @table @asis
3912 @item @code{exit}
3913 @c @fuindex exit
3914 @prindex @code{exit}
3915 On ancient hosts, @code{exit} returned @code{int}.
3916 This is because @code{exit} predates @code{void}, and there was a long
3917 tradition of it returning @code{int}.
3919 On current hosts, the problem more likely is that @code{exit} is not
3920 declared, due to C++ problems of some sort or another.  For this reason
3921 we suggest that test programs not invoke @code{exit}, but return from
3922 @code{main} instead.
3924 @item @code{free}
3925 @c @fuindex free
3926 @prindex @code{free}
3927 The C standard says a call @code{free (NULL)} does nothing, but
3928 some old systems don't support this (e.g., NextStep).
3930 @item @code{isinf}
3931 @itemx @code{isnan}
3932 @c @fuindex isinf
3933 @c @fuindex isnan
3934 @prindex @code{isinf}
3935 @prindex @code{isnan}
3936 The C99 standard says that @code{isinf} and @code{isnan} are
3937 macros.  On some systems just macros are available
3938 (e.g., HP-UX and Solaris 10), on
3939 some systems both macros and functions (e.g., glibc 2.3.2), and on some
3940 systems only functions (e.g., IRIX 6 and Solaris 9).  In some cases
3941 these functions are declared in nonstandard headers like
3942 @code{<sunmath.h>} and defined in non-default libraries like
3943 @option{-lm} or @option{-lsunmath}.
3945 The C99 @code{isinf} and @code{isnan} macros work correctly with
3946 @code{long double} arguments, but pre-C99 systems that use functions
3947 typically assume @code{double} arguments.  On such a system,
3948 @code{isinf} incorrectly returns true for a finite @code{long double}
3949 argument that is outside the range of @code{double}.
3951 To work around this porting mess, you can use code like the following.
3953 @smallexample
3954 #include <math.h>
3956 #ifndef isnan
3957 # define isnan(x) \
3958     (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
3959      : sizeof (x) == sizeof (double) ? isnan_d (x) \
3960      : isnan_f (x))
3961 static inline int isnan_f  (float       x) @{ return x != x; @}
3962 static inline int isnan_d  (double      x) @{ return x != x; @}
3963 static inline int isnan_ld (long double x) @{ return x != x; @}
3964 #endif
3966 #ifndef isinf
3967 # define isinf(x) \
3968     (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
3969      : sizeof (x) == sizeof (double) ? isinf_d (x) \
3970      : isinf_f (x))
3971 static inline int isinf_f  (float       x) @{ return isnan (x - x); @}
3972 static inline int isinf_d  (double      x) @{ return isnan (x - x); @}
3973 static inline int isinf_ld (long double x) @{ return isnan (x - x); @}
3974 #endif
3975 @end smallexample
3977 Use @code{AC_C_INLINE} (@pxref{C Compiler}) so that this code works on
3978 compilers that lack the @code{inline} keyword.  Some optimizing
3979 compilers mishandle these definitions, but systems with that bug
3980 typically have missing or broken @code{isnan} functions anyway, so it's
3981 probably not worth worrying about.
3983 @item @code{malloc}
3984 @c @fuindex malloc
3985 @prindex @code{malloc}
3986 The C standard says a call @code{malloc (0)} is implementation
3987 dependent.  It may either return @code{NULL} (e.g., OSF 4) or
3988 non-@code{NULL} (e.g., @acronym{GNU} C Library).  @code{AC_FUNC_MALLOC}
3989 can be used to insist on non-@code{NULL} (@pxref{Particular Functions}).
3991 @item @code{putenv}
3992 @c @fuindex putenv
3993 @prindex @code{putenv}
3994 Posix prefers @code{setenv} to @code{putenv}; among other things,
3995 @code{putenv} is not required of all Posix implementations, but
3996 @code{setenv} is.
3998 Posix specifies that @code{putenv} puts the given string directly in
3999 @code{environ}, but some systems make a copy of it instead (e.g.,
4000 glibc 2.0, or @acronym{BSD}).  And when a copy is made, @code{unsetenv} might
4001 not free it, causing a memory leak (e.g., Free@acronym{BSD} 4).
4003 On some systems @code{putenv ("FOO")} removes @samp{FOO} from the
4004 environment, but this is not standard usage and it dumps core
4005 on some systems (e.g., AIX).
4007 On MinGW, a call @code{putenv ("FOO=")} removes @samp{FOO} from the
4008 environment, rather than inserting it with an empty value.
4010 @item @code{realloc}
4011 @c @fuindex realloc
4012 @prindex @code{realloc}
4013 The C standard says a call @code{realloc (NULL, size)} is equivalent
4014 to @code{malloc (size)}, but some old systems don't support this (e.g.,
4015 NextStep).
4017 @item @code{signal} handler
4018 @c @fuindex signal
4019 @prindex @code{signal}
4020 Normally @code{signal} takes a handler function with a return type of
4021 @code{void}, but some old systems required @code{int} instead.  Any
4022 actual @code{int} value returned is not used; this is only a
4023 difference in the function prototype demanded.
4025 All systems we know of in current use return @code{void}.  The
4026 @code{int} was to support K&R C, where of course @code{void} is not
4027 available.  @code{AC_TYPE_SIGNAL} (@pxref{Particular Types}) can be
4028 used to establish the correct type in all cases.
4030 @item @code{snprintf}
4031 @c @fuindex snprintf
4032 @prindex @code{snprintf}
4033 @c @fuindex vsnprintf
4034 @prindex @code{vsnprintf}
4035 The C99 standard says that if the output array isn't big enough
4036 and if no other errors occur, @code{snprintf} and @code{vsnprintf}
4037 truncate the output and return the number of bytes that ought to have
4038 been produced.  Some older systems return the truncated length (e.g.,
4039 @acronym{GNU} C Library 2.0.x or @sc{irix} 6.5), some a negative value
4040 (e.g., earlier @acronym{GNU} C Library versions), and some the buffer
4041 length without truncation (e.g., 32-bit Solaris 7).  Also, some buggy
4042 older systems ignore the length and overrun the buffer (e.g., 64-bit
4043 Solaris 7).
4045 @item @code{sprintf}
4046 @c @fuindex sprintf
4047 @prindex @code{sprintf}
4048 @c @fuindex vsprintf
4049 @prindex @code{vsprintf}
4050 The C standard says @code{sprintf} and @code{vsprintf} return the
4051 number of bytes written.  On some ancient systems (SunOS 4 for
4052 instance) they return the buffer pointer instead, but these no
4053 longer need to be worried about.
4055 @item @code{sscanf}
4056 @c @fuindex sscanf
4057 @prindex @code{sscanf}
4058 On various old systems, e.g., HP-UX 9, @code{sscanf} requires that its
4059 input string be writable (though it doesn't actually change it).  This
4060 can be a problem when using @command{gcc} since it normally puts
4061 constant strings in read-only memory
4062 (@pxref{Incompatibilities, Incompatibilities of GCC, , gcc, Using and
4063 Porting the @acronym{GNU} Compiler Collection}).  Apparently in some cases even
4064 having format strings read-only can be a problem.
4066 @item @code{strerror_r}
4067 @c @fuindex strerror_r
4068 @prindex @code{strerror_r}
4069 Posix specifies that @code{strerror_r} returns an @code{int}, but many
4070 systems (e.g., @acronym{GNU} C Library version 2.2.4) provide a
4071 different version returning a @code{char *}.  @code{AC_FUNC_STRERROR_R}
4072 can detect which is in use (@pxref{Particular Functions}).
4074 @item @code{strnlen}
4075 @c @fuindex strnlen
4076 @prindex @code{strnlen}
4077 @acronym{AIX} 4.3 provides a broken version which produces the
4078 following results:
4080 @example
4081 strnlen ("foobar", 0) = 0
4082 strnlen ("foobar", 1) = 3
4083 strnlen ("foobar", 2) = 2
4084 strnlen ("foobar", 3) = 1
4085 strnlen ("foobar", 4) = 0
4086 strnlen ("foobar", 5) = 6
4087 strnlen ("foobar", 6) = 6
4088 strnlen ("foobar", 7) = 6
4089 strnlen ("foobar", 8) = 6
4090 strnlen ("foobar", 9) = 6
4091 @end example
4093 @item @code{sysconf}
4094 @c @fuindex sysconf
4095 @prindex @code{sysconf}
4096 @code{_SC_PAGESIZE} is standard, but some older systems (e.g., HP-UX
4097 9) have @code{_SC_PAGE_SIZE} instead.  This can be tested with
4098 @code{#ifdef}.
4100 @item @code{unlink}
4101 @c @fuindex unlink
4102 @prindex @code{unlink}
4103 The Posix spec says that @code{unlink} causes the given file to be
4104 removed only after there are no more open file handles for it.  Some
4105 non-Posix hosts have trouble with this requirement, though,
4106 and some @acronym{DOS} variants even corrupt the file system.
4108 @item @code{unsetenv}
4109 @c @fuindex unsetenv
4110 @prindex @code{unsetenv}
4111 On MinGW, @code{unsetenv} is not available, but a variable @samp{FOO}
4112 can be removed with a call @code{putenv ("FOO=")}, as described under
4113 @code{putenv} above.
4115 @item @code{va_copy}
4116 @c @fuindex va_copy
4117 @prindex @code{va_copy}
4118 The C99 standard provides @code{va_copy} for copying
4119 @code{va_list} variables.  It may be available in older environments
4120 too, though possibly as @code{__va_copy} (e.g., @command{gcc} in strict
4121 pre-C99 mode).  These can be tested with @code{#ifdef}.  A fallback to
4122 @code{memcpy (&dst, &src, sizeof (va_list))} will give maximum
4123 portability.
4125 @item @code{va_list}
4126 @c @fuindex va_list
4127 @prindex @code{va_list}
4128 @code{va_list} is not necessarily just a pointer.  It can be a
4129 @code{struct} (e.g., @command{gcc} on Alpha), which means @code{NULL} is
4130 not portable.  Or it can be an array (e.g., @command{gcc} in some
4131 PowerPC configurations), which means as a function parameter it can be
4132 effectively call-by-reference and library routines might modify the
4133 value back in the caller (e.g., @code{vsnprintf} in the @acronym{GNU} C Library
4134 2.1).
4136 @item Signed @code{>>}
4137 Normally the C @code{>>} right shift of a signed type replicates the
4138 high bit, giving a so-called ``arithmetic'' shift.  But care should be
4139 taken since Standard C doesn't require that behavior.  On those
4140 few processors without a native arithmetic shift (for instance Cray
4141 vector systems) zero bits may be shifted in, the same as a shift of an
4142 unsigned type.
4144 @item Integer @code{/}
4145 C divides signed integers by truncating their quotient toward zero,
4146 yielding the same result as Fortran.  However, before C99 the standard
4147 allowed C implementations to take the floor or ceiling of the quotient
4148 in some cases.  Hardly any implementations took advantage of this
4149 freedom, though, and it's probably not worth worrying about this issue
4150 nowadays.
4151 @end table
4154 @node Particular Functions
4155 @subsection Particular Function Checks
4156 @cindex Function, checking
4158 These macros check for particular C functions---whether they exist, and
4159 in some cases how they respond when given certain arguments.
4161 @defmac AC_FUNC_ALLOCA
4162 @acindex{FUNC_ALLOCA}
4163 @cvindex C_ALLOCA
4164 @cvindex HAVE_ALLOCA_H
4165 @ovindex ALLOCA
4166 @c @fuindex alloca
4167 @prindex @code{alloca}
4168 @hdrindex{alloca.h}
4169 Check how to get @code{alloca}.  Tries to get a builtin version by
4170 checking for @file{alloca.h} or the predefined C preprocessor macros
4171 @code{__GNUC__} and @code{_AIX}.  If this macro finds @file{alloca.h},
4172 it defines @code{HAVE_ALLOCA_H}.
4174 If those attempts fail, it looks for the function in the standard C
4175 library.  If any of those methods succeed, it defines
4176 @code{HAVE_ALLOCA}.  Otherwise, it sets the output variable
4177 @code{ALLOCA} to @samp{$@{LIBOBJDIR@}alloca.o} and defines
4178 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
4179 garbage collect).  This variable is separate from @code{LIBOBJS} so
4180 multiple programs can share the value of @code{ALLOCA} without needing
4181 to create an actual library, in case only some of them use the code in
4182 @code{LIBOBJS}.  The @samp{$@{LIBOBJDIR@}} prefix serves the same
4183 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
4185 This macro does not try to get @code{alloca} from the System V R3
4186 @file{libPW} or the System V R4 @file{libucb} because those libraries
4187 contain some incompatible functions that cause trouble.  Some versions
4188 do not even contain @code{alloca} or contain a buggy version.  If you
4189 still want to use their @code{alloca}, use @code{ar} to extract
4190 @file{alloca.o} from them instead of compiling @file{alloca.c}.
4192 Source files that use @code{alloca} should start with a piece of code
4193 like the following, to declare it properly.
4195 @example
4196 @group
4197 #if HAVE_ALLOCA_H
4198 # include <alloca.h>
4199 #elif defined __GNUC__
4200 # define alloca __builtin_alloca
4201 #elif defined _AIX
4202 # define alloca __alloca
4203 #elif defined _MSC_VER
4204 # include <malloc.h>
4205 # define alloca _alloca
4206 #else
4207 # include <stddef.h>
4208 # ifdef  __cplusplus
4209 extern "C"
4210 # endif
4211 void *alloca (size_t);
4212 #endif
4213 @end group
4214 @end example
4215 @end defmac
4217 @defmac AC_FUNC_CHOWN
4218 @acindex{FUNC_CHOWN}
4219 @c @fuindex chown
4220 @prindex @code{chown}
4221 If the @code{chown} function is available and works (in particular, it
4222 should accept @option{-1} for @code{uid} and @code{gid}), define
4223 @code{HAVE_CHOWN}.
4224 @end defmac
4227 @defmac AC_FUNC_CLOSEDIR_VOID
4228 @acindex{FUNC_CLOSEDIR_VOID}
4229 @cvindex CLOSEDIR_VOID
4230 @c @fuindex closedir
4231 @prindex @code{closedir}
4232 If the @code{closedir} function does not return a meaningful value,
4233 define @code{CLOSEDIR_VOID}.  Otherwise, callers ought to check its
4234 return value for an error indicator.
4236 Currently this test is implemented by running a test program.  When
4237 cross compiling the pessimistic assumption that @code{closedir} does not
4238 return a meaningful value is made.
4240 This macro is obsolescent, as @code{closedir} returns a meaningful value
4241 on current systems.  New programs need not use this macro.
4242 @end defmac
4244 @defmac AC_FUNC_ERROR_AT_LINE
4245 @acindex{FUNC_ERROR_AT_LINE}
4246 @c @fuindex error_at_line
4247 @prindex @code{error_at_line}
4248 If the @code{error_at_line} function is not found, require an
4249 @code{AC_LIBOBJ} replacement of @samp{error}.
4250 @end defmac
4252 @defmac AC_FUNC_FNMATCH
4253 @acindex{FUNC_FNMATCH}
4254 @c @fuindex fnmatch
4255 @prindex @code{fnmatch}
4256 If the @code{fnmatch} function conforms to Posix, define
4257 @code{HAVE_FNMATCH}.  Detect common implementation bugs, for example,
4258 the bugs in Solaris 2.4.
4260 Note that for historical reasons, contrary to the other specific
4261 @code{AC_FUNC} macros, @code{AC_FUNC_FNMATCH} does not replace a
4262 broken/missing @code{fnmatch}.  See @code{AC_REPLACE_FNMATCH} below.
4263 @end defmac
4265 @defmac AC_FUNC_FNMATCH_GNU
4266 @acindex{FUNC_FNMATCH_GNU}
4267 @c @fuindex fnmatch
4268 @prindex @code{fnmatch}
4269 Behave like @code{AC_REPLACE_FNMATCH} (@emph{replace}) but also test
4270 whether @code{fnmatch} supports @acronym{GNU} extensions.  Detect common
4271 implementation bugs, for example, the bugs in the @acronym{GNU} C
4272 Library 2.1.
4273 @end defmac
4275 @defmac AC_FUNC_FORK
4276 @acindex{FUNC_FORK}
4277 @cvindex HAVE_VFORK_H
4278 @cvindex HAVE_WORKING_FORK
4279 @cvindex HAVE_WORKING_VFORK
4280 @cvindex vfork
4281 @c @fuindex fork
4282 @prindex @code{fork}
4283 @c @fuindex vfork
4284 @prindex @code{vfork}
4285 @hdrindex{vfork.h}
4286 This macro checks for the @code{fork} and @code{vfork} functions.  If a
4287 working @code{fork} is found, define @code{HAVE_WORKING_FORK}.  This macro
4288 checks whether @code{fork} is just a stub by trying to run it.
4290 If @file{vfork.h} is found, define @code{HAVE_VFORK_H}.  If a working
4291 @code{vfork} is found, define @code{HAVE_WORKING_VFORK}.  Otherwise,
4292 define @code{vfork} to be @code{fork} for backward compatibility with
4293 previous versions of @command{autoconf}.  This macro checks for several known
4294 errors in implementations of @code{vfork} and considers the system to not
4295 have a working @code{vfork} if it detects any of them.  It is not considered
4296 to be an implementation error if a child's invocation of @code{signal}
4297 modifies the parent's signal handler, since child processes rarely change
4298 their signal handlers.
4300 Since this macro defines @code{vfork} only for backward compatibility with
4301 previous versions of @command{autoconf} you're encouraged to define it
4302 yourself in new code:
4303 @example
4304 @group
4305 #if !HAVE_WORKING_VFORK
4306 # define vfork fork
4307 #endif
4308 @end group
4309 @end example
4310 @end defmac
4312 @defmac AC_FUNC_FSEEKO
4313 @acindex{FUNC_FSEEKO}
4314 @cvindex _LARGEFILE_SOURCE
4315 @c @fuindex fseeko
4316 @prindex @code{fseeko}
4317 If the @code{fseeko} function is available, define @code{HAVE_FSEEKO}.
4318 Define @code{_LARGEFILE_SOURCE} if necessary to make the prototype
4319 visible on some systems (e.g., glibc 2.2).  Otherwise linkage problems
4320 may occur when compiling with @code{AC_SYS_LARGEFILE} on
4321 largefile-sensitive systems where @code{off_t} does not default to a
4322 64bit entity.
4323 @end defmac
4325 @defmac AC_FUNC_GETGROUPS
4326 @acindex{FUNC_GETGROUPS}
4327 @ovindex GETGROUPS_LIBS
4328 @c @fuindex getgroups
4329 @prindex @code{getgroups}
4330 If the @code{getgroups} function is available and works (unlike on
4331 Ultrix 4.3, where @samp{getgroups (0, 0)} always fails), define
4332 @code{HAVE_GETGROUPS}.  Set @code{GETGROUPS_LIBS} to any libraries
4333 needed to get that function.  This macro runs @code{AC_TYPE_GETGROUPS}.
4334 @end defmac
4336 @defmac AC_FUNC_GETLOADAVG
4337 @acindex{FUNC_GETLOADAVG}
4338 @cvindex SVR4
4339 @cvindex DGUX
4340 @cvindex UMAX
4341 @cvindex UMAX4_3
4342 @cvindex HAVE_NLIST_H
4343 @cvindex NLIST_NAME_UNION
4344 @cvindex GETLODAVG_PRIVILEGED
4345 @cvindex NEED_SETGID
4346 @cvindex C_GETLOADAVG
4347 @ovindex LIBOBJS
4348 @ovindex NEED_SETGID
4349 @ovindex KMEM_GROUP
4350 @ovindex GETLOADAVG_LIBS
4351 @c @fuindex getloadavg
4352 @prindex @code{getloadavg}
4353 Check how to get the system load averages.  To perform its tests
4354 properly, this macro needs the file @file{getloadavg.c}; therefore, be
4355 sure to set the @code{AC_LIBOBJ} replacement directory properly (see
4356 @ref{Generic Functions}, @code{AC_CONFIG_LIBOBJ_DIR}).
4358 If the system has the @code{getloadavg} function, define
4359 @code{HAVE_GETLOADAVG}, and set @code{GETLOADAVG_LIBS} to any libraries
4360 necessary to get that function.  Also add @code{GETLOADAVG_LIBS} to
4361 @code{LIBS}.  Otherwise, require an @code{AC_LIBOBJ} replacement for
4362 @samp{getloadavg} with source code in @file{@var{dir}/getloadavg.c}, and
4363 possibly define several other C preprocessor macros and output
4364 variables:
4366 @enumerate
4367 @item
4368 Define @code{C_GETLOADAVG}.
4370 @item
4371 Define @code{SVR4}, @code{DGUX}, @code{UMAX}, or @code{UMAX4_3} if on
4372 those systems.
4374 @item
4375 @hdrindex{nlist.h}
4376 If @file{nlist.h} is found, define @code{HAVE_NLIST_H}.
4378 @item
4379 If @samp{struct nlist} has an @samp{n_un.n_name} member, define
4380 @code{HAVE_STRUCT_NLIST_N_UN_N_NAME}.  The obsolete symbol
4381 @code{NLIST_NAME_UNION} is still defined, but do not depend upon it.
4383 @item
4384 Programs may need to be installed set-group-ID (or set-user-ID) for
4385 @code{getloadavg} to work.  In this case, define
4386 @code{GETLOADAVG_PRIVILEGED}, set the output variable @code{NEED_SETGID}
4387 to @samp{true} (and otherwise to @samp{false}), and set
4388 @code{KMEM_GROUP} to the name of the group that should own the installed
4389 program.
4390 @end enumerate
4391 @end defmac
4393 @defmac AC_FUNC_GETMNTENT
4394 @acindex{FUNC_GETMNTENT}
4395 @cvindex HAVE_GETMNTENT
4396 @c @fuindex getmntent
4397 @prindex @code{getmntent}
4398 Check for @code{getmntent} in the standard C library, and then in the
4399 @file{sun}, @file{seq}, and @file{gen} libraries, for @sc{unicos},
4400 @sc{irix} 4, @sc{ptx}, and UnixWare, respectively.  Then, if
4401 @code{getmntent} is available, define @code{HAVE_GETMNTENT}.
4402 @end defmac
4404 @defmac AC_FUNC_GETPGRP
4405 @acindex{FUNC_GETPGRP}
4406 @cvindex GETPGRP_VOID
4407 @c @fuindex getpgid
4408 @c @fuindex getpgrp
4409 @prindex @code{getpgid}
4410 @prindex @code{getpgrp}
4411 Define @code{GETPGRP_VOID} if it is an error to pass 0 to
4412 @code{getpgrp}; this is the Posix behavior.  On older @acronym{BSD}
4413 systems, you must pass 0 to @code{getpgrp}, as it takes an argument and
4414 behaves like Posix's @code{getpgid}.
4416 @example
4417 #if GETPGRP_VOID
4418   pid = getpgrp ();
4419 #else
4420   pid = getpgrp (0);
4421 #endif
4422 @end example
4424 This macro does not check whether
4425 @code{getpgrp} exists at all; if you need to work in that situation,
4426 first call @code{AC_CHECK_FUNC} for @code{getpgrp}.
4428 This macro is obsolescent, as current systems have a @code{getpgrp}
4429 whose signature conforms to Posix.  New programs need not use this macro.
4430 @end defmac
4432 @defmac AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
4433 @acindex{FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK}
4434 @cvindex LSTAT_FOLLOWS_SLASHED_SYMLINK
4435 @c @fuindex lstat
4436 @prindex @code{lstat}
4437 If @file{link} is a symbolic link, then @code{lstat} should treat
4438 @file{link/} the same as @file{link/.}.  However, many older
4439 @code{lstat} implementations incorrectly ignore trailing slashes.
4441 It is safe to assume that if @code{lstat} incorrectly ignores
4442 trailing slashes, then other symbolic-link-aware functions like
4443 @code{unlink} also incorrectly ignore trailing slashes.
4445 If @code{lstat} behaves properly, define
4446 @code{LSTAT_FOLLOWS_SLASHED_SYMLINK}, otherwise require an
4447 @code{AC_LIBOBJ} replacement of @code{lstat}.
4448 @end defmac
4450 @defmac AC_FUNC_MALLOC
4451 @acindex{FUNC_MALLOC}
4452 @cvindex HAVE_MALLOC
4453 @cvindex malloc
4454 @c @fuindex malloc
4455 @prindex @code{malloc}
4456 If the @code{malloc} function is compatible with the @acronym{GNU} C
4457 library @code{malloc} (i.e., @samp{malloc (0)} returns a valid
4458 pointer), define @code{HAVE_MALLOC} to 1.  Otherwise define
4459 @code{HAVE_MALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4460 @samp{malloc}, and define @code{malloc} to @code{rpl_malloc} so that the
4461 native @code{malloc} is not used in the main project.
4463 Typically, the replacement file @file{malloc.c} should look like (note
4464 the @samp{#undef malloc}):
4466 @verbatim
4467 #if HAVE_CONFIG_H
4468 # include <config.h>
4469 #endif
4470 #undef malloc
4472 #include <sys/types.h>
4474 void *malloc ();
4476 /* Allocate an N-byte block of memory from the heap.
4477    If N is zero, allocate a 1-byte block.  */
4479 void *
4480 rpl_malloc (size_t n)
4482   if (n == 0)
4483     n = 1;
4484   return malloc (n);
4486 @end verbatim
4487 @end defmac
4489 @defmac AC_FUNC_MEMCMP
4490 @acindex{FUNC_MEMCMP}
4491 @ovindex LIBOBJS
4492 @c @fuindex memcmp
4493 @prindex @code{memcmp}
4494 If the @code{memcmp} function is not available, or does not work on
4495 8-bit data (like the one on SunOS 4.1.3), or fails when comparing 16
4496 bytes or more and with at least one buffer not starting on a 4-byte
4497 boundary (such as the one on NeXT x86 OpenStep), require an
4498 @code{AC_LIBOBJ} replacement for @samp{memcmp}.
4500 This macro is obsolescent, as current systems have a working
4501 @code{memcmp}.  New programs need not use this macro.
4502 @end defmac
4504 @defmac AC_FUNC_MBRTOWC
4505 @acindex{FUNC_MBRTOWC}
4506 @cvindex HAVE_MBRTOWC
4507 @c @fuindex mbrtowc
4508 @prindex @code{mbrtowc}
4509 Define @code{HAVE_MBRTOWC} to 1 if the function @code{mbrtowc} and the
4510 type @code{mbstate_t} are properly declared.
4511 @end defmac
4513 @defmac AC_FUNC_MKTIME
4514 @acindex{FUNC_MKTIME}
4515 @ovindex LIBOBJS
4516 @c @fuindex mktime
4517 @prindex @code{mktime}
4518 If the @code{mktime} function is not available, or does not work
4519 correctly, require an @code{AC_LIBOBJ} replacement for @samp{mktime}.
4520 For the purposes of this test, @code{mktime} should conform to the
4521 Posix standard and should be the inverse of
4522 @code{localtime}.
4523 @end defmac
4525 @defmac AC_FUNC_MMAP
4526 @acindex{FUNC_MMAP}
4527 @cvindex HAVE_MMAP
4528 @c @fuindex mmap
4529 @prindex @code{mmap}
4530 If the @code{mmap} function exists and works correctly, define
4531 @code{HAVE_MMAP}.  This checks only private fixed mapping of already-mapped
4532 memory.
4533 @end defmac
4535 @defmac AC_FUNC_OBSTACK
4536 @acindex{FUNC_OBSTACK}
4537 @cvindex HAVE_OBSTACK
4538 @cindex obstack
4539 If the obstacks are found, define @code{HAVE_OBSTACK}, else require an
4540 @code{AC_LIBOBJ} replacement for @samp{obstack}.
4541 @end defmac
4543 @defmac AC_FUNC_REALLOC
4544 @acindex{FUNC_REALLOC}
4545 @cvindex HAVE_REALLOC
4546 @cvindex realloc
4547 @c @fuindex realloc
4548 @prindex @code{realloc}
4549 If the @code{realloc} function is compatible with the @acronym{GNU} C
4550 library @code{realloc} (i.e., @samp{realloc (NULL, 0)} returns a
4551 valid pointer), define @code{HAVE_REALLOC} to 1.  Otherwise define
4552 @code{HAVE_REALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4553 @samp{realloc}, and define @code{realloc} to @code{rpl_realloc} so that
4554 the native @code{realloc} is not used in the main project.  See
4555 @code{AC_FUNC_MALLOC} for details.
4556 @end defmac
4558 @defmac AC_FUNC_SELECT_ARGTYPES
4559 @acindex{FUNC_SELECT_ARGTYPES}
4560 @cvindex SELECT_TYPE_ARG1
4561 @cvindex SELECT_TYPE_ARG234
4562 @cvindex SELECT_TYPE_ARG5
4563 @c @fuindex select
4564 @prindex @code{select}
4565 Determines the correct type to be passed for each of the
4566 @code{select} function's arguments, and defines those types
4567 in @code{SELECT_TYPE_ARG1}, @code{SELECT_TYPE_ARG234}, and
4568 @code{SELECT_TYPE_ARG5} respectively.  @code{SELECT_TYPE_ARG1} defaults
4569 to @samp{int}, @code{SELECT_TYPE_ARG234} defaults to @samp{int *},
4570 and @code{SELECT_TYPE_ARG5} defaults to @samp{struct timeval *}.
4572 This macro is obsolescent, as current systems have a @code{select} whose
4573 signature conforms to Posix.  New programs need not use this macro.
4574 @end defmac
4576 @defmac AC_FUNC_SETPGRP
4577 @acindex{FUNC_SETPGRP}
4578 @cvindex SETPGRP_VOID
4579 @c @fuindex setpgrp
4580 @prindex @code{setpgrp}
4581 If @code{setpgrp} takes no argument (the Posix version), define
4582 @code{SETPGRP_VOID}.  Otherwise, it is the @acronym{BSD} version, which takes
4583 two process IDs as arguments.  This macro does not check whether
4584 @code{setpgrp} exists at all; if you need to work in that situation,
4585 first call @code{AC_CHECK_FUNC} for @code{setpgrp}.
4587 This macro is obsolescent, as current systems have a @code{setpgrp}
4588 whose signature conforms to Posix.  New programs need not use this macro.
4589 @end defmac
4591 @defmac AC_FUNC_STAT
4592 @defmacx AC_FUNC_LSTAT
4593 @acindex{FUNC_STAT}
4594 @acindex{FUNC_LSTAT}
4595 @cvindex HAVE_STAT_EMPTY_STRING_BUG
4596 @cvindex HAVE_LSTAT_EMPTY_STRING_BUG
4597 @c @fuindex stat
4598 @prindex @code{stat}
4599 @c @fuindex lstat
4600 @prindex @code{lstat}
4601 Determine whether @code{stat} or @code{lstat} have the bug that it
4602 succeeds when given the zero-length file name as argument.  The @code{stat}
4603 and @code{lstat} from SunOS 4.1.4 and the Hurd (as of 1998-11-01) do
4604 this.
4606 If it does, then define @code{HAVE_STAT_EMPTY_STRING_BUG} (or
4607 @code{HAVE_LSTAT_EMPTY_STRING_BUG}) and ask for an @code{AC_LIBOBJ}
4608 replacement of it.
4610 These macros are obsolescent, as no current systems have the bug.
4611 New programs need not use these macros.
4612 @end defmac
4614 @defmac AC_FUNC_SETVBUF_REVERSED
4615 @acindex{FUNC_SETVBUF_REVERSED}
4616 @cvindex SETVBUF_REVERSED
4617 @c @fuindex setvbuf
4618 @prindex @code{setvbuf}
4619 If @code{setvbuf} takes the buffering type as its second argument and
4620 the buffer pointer as the third, instead of the other way around, define
4621 @code{SETVBUF_REVERSED}.
4623 This macro is obsolescent, as no current systems have the bug.
4624 New programs need not use this macro.
4625 @end defmac
4627 @defmac AC_FUNC_STRCOLL
4628 @acindex{FUNC_STRCOLL}
4629 @cvindex HAVE_STRCOLL
4630 @c @fuindex strcoll
4631 @prindex @code{strcoll}
4632 If the @code{strcoll} function exists and works correctly, define
4633 @code{HAVE_STRCOLL}.  This does a bit more than
4634 @samp{AC_CHECK_FUNCS(strcoll)}, because some systems have incorrect
4635 definitions of @code{strcoll} that should not be used.
4636 @end defmac
4638 @defmac AC_FUNC_STRERROR_R
4639 @acindex{FUNC_STRERROR_R}
4640 @cvindex HAVE_STRERROR_R
4641 @cvindex HAVE_DECL_STRERROR_R
4642 @cvindex STRERROR_R_CHAR_P
4643 @c @fuindex strerror_r
4644 @prindex @code{strerror_r}
4645 If @code{strerror_r} is available, define @code{HAVE_STRERROR_R}, and if
4646 it is declared, define @code{HAVE_DECL_STRERROR_R}.  If it returns a
4647 @code{char *} message, define @code{STRERROR_R_CHAR_P}; otherwise it
4648 returns an @code{int} error number.  The Thread-Safe Functions option of
4649 Posix requires @code{strerror_r} to return @code{int}, but
4650 many systems (including, for example, version 2.2.4 of the @acronym{GNU} C
4651 Library) return a @code{char *} value that is not necessarily equal to
4652 the buffer argument.
4653 @end defmac
4655 @defmac AC_FUNC_STRFTIME
4656 @acindex{FUNC_STRFTIME}
4657 @cvindex HAVE_STRFTIME
4658 @c @fuindex strftime
4659 @prindex @code{strftime}
4660 Check for @code{strftime} in the @file{intl} library, for SCO Unix.
4661 Then, if @code{strftime} is available, define @code{HAVE_STRFTIME}.
4663 This macro is obsolescent, as no current systems require the @file{intl}
4664 library for @code{strftime}.  New programs need not use this macro.
4665 @end defmac
4667 @defmac AC_FUNC_STRTOD
4668 @acindex{FUNC_STRTOD}
4669 @ovindex POW_LIB
4670 @c @fuindex strtod
4671 @prindex @code{strtod}
4672 If the @code{strtod} function does not exist or doesn't work correctly,
4673 ask for an @code{AC_LIBOBJ} replacement of @samp{strtod}.  In this case,
4674 because @file{strtod.c} is likely to need @samp{pow}, set the output
4675 variable @code{POW_LIB} to the extra library needed.
4676 @end defmac
4678 @defmac AC_FUNC_STRTOLD
4679 @acindex{FUNC_STRTOLD}
4680 @prindex @code{strtold}
4681 If the @code{strtold} function exists and conforms to C99, define
4682 @code{HAVE_STRTOLD}.
4683 @end defmac
4685 @defmac AC_FUNC_STRNLEN
4686 @acindex{FUNC_STRNLEN}
4687 @cvindex HAVE_STRNLEN
4688 @c @fuindex strnlen
4689 @prindex @code{strnlen}
4690 If the @code{strnlen} function is not available, or is buggy (like the one
4691 from @acronym{AIX} 4.3), require an @code{AC_LIBOBJ} replacement for it.
4692 @end defmac
4694 @defmac AC_FUNC_UTIME_NULL
4695 @acindex{FUNC_UTIME_NULL}
4696 @cvindex HAVE_UTIME_NULL
4697 @c @fuindex utime
4698 @prindex @code{utime}
4699 If @samp{utime (@var{file}, NULL)} sets @var{file}'s timestamp to
4700 the present, define @code{HAVE_UTIME_NULL}.
4702 This macro is obsolescent, as all current systems have a @code{utime}
4703 that behaves this way.  New programs need not use this macro.
4704 @end defmac
4706 @defmac AC_FUNC_VPRINTF
4707 @acindex{FUNC_VPRINTF}
4708 @cvindex HAVE_VPRINTF
4709 @cvindex HAVE_DOPRNT
4710 @c @fuindex vprintf
4711 @prindex @code{vprintf}
4712 If @code{vprintf} is found, define @code{HAVE_VPRINTF}.  Otherwise, if
4713 @code{_doprnt} is found, define @code{HAVE_DOPRNT}.  (If @code{vprintf}
4714 is available, you may assume that @code{vfprintf} and @code{vsprintf}
4715 are also available.)
4717 This macro is obsolescent, as all current systems have @code{vprintf}.
4718 New programs need not use this macro.
4719 @end defmac
4721 @defmac AC_REPLACE_FNMATCH
4722 @acindex{REPLACE_FNMATCH}
4723 @c @fuindex fnmatch
4724 @prindex @code{fnmatch}
4725 @hdrindex{fnmatch.h}
4726 If the @code{fnmatch} function does not conform to Posix (see
4727 @code{AC_FUNC_FNMATCH}), ask for its @code{AC_LIBOBJ} replacement.
4729 The files @file{fnmatch.c}, @file{fnmatch_loop.c}, and @file{fnmatch_.h}
4730 in the @code{AC_LIBOBJ} replacement directory are assumed to contain a
4731 copy of the source code of @acronym{GNU} @code{fnmatch}.  If necessary,
4732 this source code is compiled as an @code{AC_LIBOBJ} replacement, and the
4733 @file{fnmatch_.h} file is linked to @file{fnmatch.h} so that it can be
4734 included in place of the system @code{<fnmatch.h>}.
4735 @end defmac
4739 @node Generic Functions
4740 @subsection Generic Function Checks
4742 These macros are used to find functions not covered by the ``particular''
4743 test macros.  If the functions might be in libraries other than the
4744 default C library, first call @code{AC_CHECK_LIB} for those libraries.
4745 If you need to check the behavior of a function as well as find out
4746 whether it is present, you have to write your own test for
4747 it (@pxref{Writing Tests}).
4749 @defmac AC_CHECK_FUNC (@var{function}, @ovar{action-if-found}, @ovar{action-if-not-found})
4750 @acindex{CHECK_FUNC}
4751 If C function @var{function} is available, run shell commands
4752 @var{action-if-found}, otherwise @var{action-if-not-found}.  If you just
4753 want to define a symbol if the function is available, consider using
4754 @code{AC_CHECK_FUNCS} instead.  This macro checks for functions with C
4755 linkage even when @code{AC_LANG(C++)} has been called, since C is more
4756 standardized than C++.  (@pxref{Language Choice}, for more information
4757 about selecting the language for checks.)
4758 @end defmac
4760 @defmac AC_CHECK_FUNCS (@var{function}@dots{}, @ovar{action-if-found}, @ovar{action-if-not-found})
4761 @acindex{CHECK_FUNCS}
4762 @cvindex HAVE_@var{function}
4763 For each @var{function} enumerated in the blank-or-newline-separated argument
4764 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
4765 If @var{action-if-found} is given, it is additional shell code to
4766 execute when one of the functions is found.  You can give it a value of
4767 @samp{break} to break out of the loop on the first match.  If
4768 @var{action-if-not-found} is given, it is executed when one of the
4769 functions is not found.
4770 @end defmac
4772 @defmac AC_CHECK_FUNCS_ONCE (@var{function}@dots{})
4773 @acindex{CHECK_FUNCS_ONCE}
4774 @cvindex HAVE_@var{function}
4775 For each @var{function} enumerated in the blank-or-newline-separated argument
4776 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
4777 This is a once-only variant of @code{AC_CHECK_FUNCS}.  It generates the
4778 checking code at most once, so that @command{configure} is smaller and
4779 faster; but the checks cannot be conditionalized and are always done once,
4780 early during the @command{configure} run.
4781 @end defmac
4783 @sp 1
4785 Autoconf follows a philosophy that was formed over the years by those
4786 who have struggled for portability: isolate the portability issues in
4787 specific files, and then program as if you were in a Posix
4788 environment.  Some functions may be missing or unfixable, and your
4789 package must be ready to replace them.
4791 Suitable replacements for many such problem functions are available from
4792 Gnulib (@pxref{Gnulib}).
4794 @defmac AC_LIBOBJ (@var{function})
4795 @acindex{LIBOBJ}
4796 @ovindex LIBOBJS
4797 Specify that @samp{@var{function}.c} must be included in the executables
4798 to replace a missing or broken implementation of @var{function}.
4800 Technically, it adds @samp{@var{function}.$ac_objext} to the output
4801 variable @code{LIBOBJS} if it is not already in, and calls
4802 @code{AC_LIBSOURCE} for @samp{@var{function}.c}.  You should not
4803 directly change @code{LIBOBJS}, since this is not traceable.
4804 @end defmac
4806 @defmac AC_LIBSOURCE (@var{file})
4807 @acindex{LIBSOURCE}
4808 Specify that @var{file} might be needed to compile the project.  If you
4809 need to know what files might be needed by a @file{configure.ac}, you
4810 should trace @code{AC_LIBSOURCE}.  @var{file} must be a literal.
4812 This macro is called automatically from @code{AC_LIBOBJ}, but you must
4813 call it explicitly if you pass a shell variable to @code{AC_LIBOBJ}.  In
4814 that case, since shell variables cannot be traced statically, you must
4815 pass to @code{AC_LIBSOURCE} any possible files that the shell variable
4816 might cause @code{AC_LIBOBJ} to need.  For example, if you want to pass
4817 a variable @code{$foo_or_bar} to @code{AC_LIBOBJ} that holds either
4818 @code{"foo"} or @code{"bar"}, you should do:
4820 @example
4821 AC_LIBSOURCE([foo.c])
4822 AC_LIBSOURCE([bar.c])
4823 AC_LIBOBJ([$foo_or_bar])
4824 @end example
4826 @noindent
4827 There is usually a way to avoid this, however, and you are encouraged to
4828 simply call @code{AC_LIBOBJ} with literal arguments.
4830 Note that this macro replaces the obsolete @code{AC_LIBOBJ_DECL}, with
4831 slightly different semantics: the old macro took the function name,
4832 e.g., @code{foo}, as its argument rather than the file name.
4833 @end defmac
4835 @defmac AC_LIBSOURCES (@var{files})
4836 @acindex{LIBSOURCES}
4837 Like @code{AC_LIBSOURCE}, but accepts one or more @var{files} in a
4838 comma-separated M4 list.  Thus, the above example might be rewritten:
4840 @example
4841 AC_LIBSOURCES([foo.c, bar.c])
4842 AC_LIBOBJ([$foo_or_bar])
4843 @end example
4844 @end defmac
4846 @defmac AC_CONFIG_LIBOBJ_DIR (@var{directory})
4847 @acindex{CONFIG_LIBOBJ_DIR}
4848 Specify that @code{AC_LIBOBJ} replacement files are to be found in
4849 @var{directory}, a name relative to the top level of the
4850 source tree.  The replacement directory defaults to @file{.}, the top
4851 level directory, and the most typical value is @file{lib}, corresponding
4852 to @samp{AC_CONFIG_LIBOBJ_DIR([lib])}.
4854 @command{configure} might need to know the replacement directory for the
4855 following reasons: (i) some checks use the replacement files, (ii) some
4856 macros bypass broken system headers by installing links to the
4857 replacement headers (iii) when used in conjunction with Automake,
4858 within each @file{Makefile}, @var{directory} is used as a relative path
4859 from @code{$(top_srcdir)} to each object named in @code{LIBOBJS} and
4860 @code{LTLIBOBJS}, etc.
4861 @end defmac
4863 @sp 1
4865 It is common to merely check for the existence of a function, and ask
4866 for its @code{AC_LIBOBJ} replacement if missing.  The following macro is
4867 a convenient shorthand.
4869 @defmac AC_REPLACE_FUNCS (@var{function}@dots{})
4870 @acindex{REPLACE_FUNCS}
4871 @ovindex LIBOBJS
4872 Like @code{AC_CHECK_FUNCS}, but uses @samp{AC_LIBOBJ(@var{function})} as
4873 @var{action-if-not-found}.  You can declare your replacement function by
4874 enclosing the prototype in @samp{#if !HAVE_@var{function}}.  If the
4875 system has the function, it probably declares it in a header file you
4876 should be including, so you shouldn't redeclare it lest your declaration
4877 conflict.
4878 @end defmac
4880 @node Header Files
4881 @section Header Files
4882 @cindex Header, checking
4884 The following macros check for the presence of certain C header files.
4885 If there is no macro specifically defined to check for a header file you need,
4886 and you don't need to check for any special properties of
4887 it, then you can use one of the general header-file check macros.
4889 @menu
4890 * Header Portability::          Collected knowledge on common headers
4891 * Particular Headers::          Special handling to find certain headers
4892 * Generic Headers::             How to find other headers
4893 @end menu
4895 @node Header Portability
4896 @subsection Portability of Headers
4897 @cindex Portability of headers
4898 @cindex Header portability
4900 This section tries to collect knowledge about common headers, and the
4901 problems they cause.  By definition, this list will always require
4902 additions.  Please help us keeping it as complete as possible.
4904 @table @asis
4906 @item @file{limits.h}
4907 C99 says that @file{limits.h} defines @code{LLONG_MIN},
4908 @code{LLONG_MAX}, and @code{ULLONG_MAX}, but many almost-C99
4909 environments (e.g., default GCC 4.0.2 + glibc 2.4) do not define them.
4911 @item @file{inttypes.h} vs.@: @file{stdint.h}
4912 @hdrindex{inttypes.h}
4913 @hdrindex{stdint.h}
4914 The C99 standard says that @file{inttypes.h} includes
4915 @file{stdint.h}, so there's no need to include @file{stdint.h}
4916 separately in a standard environment.  Some implementations have
4917 @file{inttypes.h} but not @file{stdint.h} (e.g., Solaris 7), but we don't
4918 know of any implementation that has @file{stdint.h} but not
4919 @file{inttypes.h}.
4921 @item @file{linux/irda.h}
4922 @hdrindex{linux/irda.h}
4923 It requires @file{linux/types.h} and @file{sys/socket.h}.
4925 @item @file{linux/random.h}
4926 @hdrindex{linux/random.h}
4927 It requires @file{linux/types.h}.
4929 @item @file{net/if.h}
4930 @hdrindex{net/if.h}
4931 On Darwin, this file requires that @file{sys/socket.h} be included
4932 beforehand.  One should run:
4934 @example
4935 AC_CHECK_HEADERS([sys/socket.h])
4936 AC_CHECK_HEADERS([net/if.h], [], [],
4937 [#include <stdio.h>
4938 #if STDC_HEADERS
4939 # include <stdlib.h>
4940 # include <stddef.h>
4941 #else
4942 # if HAVE_STDLIB_H
4943 #  include <stdlib.h>
4944 # endif
4945 #endif
4946 #if HAVE_SYS_SOCKET_H
4947 # include <sys/socket.h>
4948 #endif
4950 @end example
4952 @item @file{netinet/if_ether.h}
4953 @hdrindex{netinet/if_ether.h}
4954 On Darwin, this file requires that @file{stdio.h} and
4955 @file{sys/socket.h} be included beforehand.  One should run:
4957 @example
4958 AC_CHECK_HEADERS([sys/socket.h])
4959 AC_CHECK_HEADERS([netinet/if_ether.h], [], [],
4960 [#include <stdio.h>
4961 #if STDC_HEADERS
4962 # include <stdlib.h>
4963 # include <stddef.h>
4964 #else
4965 # if HAVE_STDLIB_H
4966 #  include <stdlib.h>
4967 # endif
4968 #endif
4969 #if HAVE_SYS_SOCKET_H
4970 # include <sys/socket.h>
4971 #endif
4973 @end example
4975 @item @file{stdint.h}
4976 See above, item @file{inttypes.h} vs.@: @file{stdint.h}.
4978 @item @file{stdlib.h}
4979 @hdrindex{stdlib.h}
4980 On many systems (e.g., Darwin), @file{stdio.h} is a prerequisite.
4982 @item @file{sys/mount.h}
4983 @hdrindex{sys/mount.h}
4984 On Free@acronym{BSD} 4.8 on ia32 and using gcc version 2.95.4,
4985 @file{sys/params.h} is a prerequisite.
4987 @item @file{sys/ptem.h}
4988 @hdrindex{sys/ptem.h}
4989 On Solaris 8, @file{sys/stream.h} is a prerequisite.
4991 @item @file{sys/socket.h}
4992 @hdrindex{sys/socket.h}
4993 On Darwin, @file{stdlib.h} is a prerequisite.
4995 @item @file{sys/ucred.h}
4996 @hdrindex{sys/ucred.h}
4997 On HP Tru64 5.1, @file{sys/types.h} is a prerequisite.
4999 @item @file{X11/extensions/scrnsaver.h}
5000 @hdrindex{X11/extensions/scrnsaver.h}
5001 Using XFree86, this header requires @file{X11/Xlib.h}, which is probably
5002 so required that you might not even consider looking for it.
5004 @example
5005 AC_CHECK_HEADERS([X11/extensions/scrnsaver.h], [], [],
5006 [[#include <X11/Xlib.h>
5008 @end example
5009 @end table
5012 @node Particular Headers
5013 @subsection Particular Header Checks
5015 These macros check for particular system header files---whether they
5016 exist, and in some cases whether they declare certain symbols.
5018 @defmac AC_HEADER_ASSERT
5019 @acindex{HEADER_ASSERT}
5020 @cvindex NDEBUG
5021 @hdrindex{assert.h}
5022 Check whether to enable assertions in the style of @file{assert.h}.
5023 Assertions are enabled by default, but the user can override this by
5024 invoking @command{configure} with the @option{--disable-assert} option.
5025 @end defmac
5027 @defmac AC_HEADER_DIRENT
5028 @acindex{HEADER_DIRENT}
5029 @cvindex HAVE_DIRENT_H
5030 @cvindex HAVE_NDIR_H
5031 @cvindex HAVE_SYS_DIR_H
5032 @cvindex HAVE_SYS_NDIR_H
5033 @hdrindex{dirent.h}
5034 @hdrindex{sys/ndir.h}
5035 @hdrindex{sys/dir.h}
5036 @hdrindex{ndir.h}
5037 Check for the following header files.  For the first one that is
5038 found and defines @samp{DIR}, define the listed C preprocessor macro:
5040 @multitable {@file{sys/ndir.h}} {@code{HAVE_SYS_NDIR_H}}
5041 @item @file{dirent.h}   @tab @code{HAVE_DIRENT_H}
5042 @item @file{sys/ndir.h} @tab @code{HAVE_SYS_NDIR_H}
5043 @item @file{sys/dir.h}  @tab @code{HAVE_SYS_DIR_H}
5044 @item @file{ndir.h}     @tab @code{HAVE_NDIR_H}
5045 @end multitable
5047 The directory-library declarations in your source code should look
5048 something like the following:
5050 @example
5051 @group
5052 #include <sys/types.h>
5053 #ifdef HAVE_DIRENT_H
5054 # include <dirent.h>
5055 # define NAMLEN(dirent) strlen ((dirent)->d_name)
5056 #else
5057 # define dirent direct
5058 # define NAMLEN(dirent) ((dirent)->d_namlen)
5059 # if HAVE_SYS_NDIR_H
5060 #  include <sys/ndir.h>
5061 # endif
5062 # if HAVE_SYS_DIR_H
5063 #  include <sys/dir.h>
5064 # endif
5065 # if HAVE_NDIR_H
5066 #  include <ndir.h>
5067 # endif
5068 #endif
5069 @end group
5070 @end example
5072 Using the above declarations, the program would declare variables to be
5073 of type @code{struct dirent}, not @code{struct direct}, and would access
5074 the length of a directory entry name by passing a pointer to a
5075 @code{struct dirent} to the @code{NAMLEN} macro.
5077 This macro also checks for the SCO Xenix @file{dir} and @file{x} libraries.
5079 This macro is obsolescent, as all current systems with directory
5080 libraries have @code{<dirent.h>}.  New programs need not use this macro.
5082 Also see @code{AC_STRUCT_DIRENT_D_INO} and
5083 @code{AC_STRUCT_DIRENT_D_TYPE} (@pxref{Particular Structures}).
5084 @end defmac
5086 @defmac AC_HEADER_MAJOR
5087 @acindex{HEADER_MAJOR}
5088 @cvindex MAJOR_IN_MKDEV
5089 @cvindex MAJOR_IN_SYSMACROS
5090 @hdrindex{sys/mkdev.h}
5091 @hdrindex{sys/sysmacros.h}
5092 If @file{sys/types.h} does not define @code{major}, @code{minor}, and
5093 @code{makedev}, but @file{sys/mkdev.h} does, define
5094 @code{MAJOR_IN_MKDEV}; otherwise, if @file{sys/sysmacros.h} does, define
5095 @code{MAJOR_IN_SYSMACROS}.
5096 @end defmac
5098 @defmac AC_HEADER_RESOLV
5099 @acindex{HEADER_RESOLV}
5100 @cvindex HAVE_RESOLV_H
5101 @hdrindex{resolv.h}
5102 Checks for header @file{resolv.h}, checking for prerequisites first.
5103 To properly use @file{resolv.h}, your code should contain something like
5104 the following:
5106 @verbatim
5107 #if HAVE_SYS_TYPES_H
5108 #  include <sys/types.h>
5109 #endif
5110 #ifdef HAVE_NETINET_IN_H
5111 #  include <netinet/in.h>   /* inet_ functions / structs */
5112 #endif
5113 #ifdef HAVE_ARPA_NAMESER_H
5114 #  include <arpa/nameser.h> /* DNS HEADER struct */
5115 #endif
5116 #ifdef HAVE_NETDB_H
5117 #  include <netdb.h>
5118 #endif
5119 #include <resolv.h>
5120 @end verbatim
5121 @end defmac
5123 @defmac AC_HEADER_STAT
5124 @acindex{HEADER_STAT}
5125 @cvindex STAT_MACROS_BROKEN
5126 @hdrindex{sys/stat.h}
5127 If the macros @code{S_ISDIR}, @code{S_ISREG}, etc.@: defined in
5128 @file{sys/stat.h} do not work properly (returning false positives),
5129 define @code{STAT_MACROS_BROKEN}.  This is the case on Tektronix UTekV,
5130 Amdahl UTS and Motorola System V/88.
5132 This macro is obsolescent, as no current systems have the bug.
5133 New programs need not use this macro.
5134 @end defmac
5136 @defmac AC_HEADER_STDBOOL
5137 @acindex{HEADER_STDBOOL}
5138 @cvindex HAVE_STDBOOL_H
5139 @cvindex HAVE__BOOL
5140 @hdrindex{stdbool.h}
5141 @hdrindex{system.h}
5142 If @file{stdbool.h} exists and conforms to C99, define
5143 @code{HAVE_STDBOOL_H} to 1; if the type @code{_Bool} is defined, define
5144 @code{HAVE__BOOL} to 1.  To fulfill the C99 requirements, your
5145 @file{system.h} could contain the following code:
5147 @verbatim
5148 #if HAVE_STDBOOL_H
5149 # include <stdbool.h>
5150 #else
5151 # if ! HAVE__BOOL
5152 #  ifdef __cplusplus
5153 typedef bool _Bool;
5154 #  else
5155 #   define _Bool signed char
5156 #  endif
5157 # endif
5158 # define bool _Bool
5159 # define false 0
5160 # define true 1
5161 # define __bool_true_false_are_defined 1
5162 #endif
5163 @end verbatim
5165 Alternatively you can use the @samp{stdbool} package of Gnulib
5166 (@pxref{Gnulib}); it packages the above code into a replacement header
5167 and contains a few other bells and whistles.
5169 @end defmac
5172 @defmac AC_HEADER_STDC
5173 @acindex{HEADER_STDC}
5174 @cvindex STDC_HEADERS
5175 @hdrindex{stdlib.h}
5176 @hdrindex{stdarg.h}
5177 @hdrindex{string.h}
5178 @hdrindex{float.h}
5179 @hdrindex{ctype.h}
5180 Define @code{STDC_HEADERS} if the system has C header files
5181 conforming to @acronym{ANSI} C89 (@acronym{ISO} C90).
5182 Specifically, this macro checks for @file{stdlib.h}, @file{stdarg.h},
5183 @file{string.h}, and @file{float.h}; if the system has those, it
5184 probably has the rest of the C89 header files.  This macro also
5185 checks whether @file{string.h} declares @code{memchr} (and thus
5186 presumably the other @code{mem} functions), whether @file{stdlib.h}
5187 declare @code{free} (and thus presumably @code{malloc} and other related
5188 functions), and whether the @file{ctype.h} macros work on characters
5189 with the high bit set, as the C standard requires.
5191 If you use this macro, your code can refer to @code{STDC_HEADERS} to
5192 determine whether the system has conforming header files (and probably C
5193 library functions).
5195 This macro is obsolescent, as current systems have conforming header
5196 files.  New programs need not use this macro.
5198 @hdrindex{string.h}
5199 @hdrindex{strings.h}
5200 Nowadays @file{string.h} is part of the C standard and declares functions like
5201 @code{strcpy}, and @file{strings.h} is standardized by Posix and declares
5202 @acronym{BSD} functions like @code{bcopy}; but
5203 historically, string functions were a major sticking point in this area.
5204 If you still want to worry about portability to ancient systems without
5205 standard headers, there is so much variation
5206 that it is probably easier to declare the functions you use than to
5207 figure out exactly what the system header files declare.  Some ancient systems
5208 contained a mix of functions from the C standard and from @acronym{BSD};
5209 some were mostly standard but lacked @samp{memmove}; some defined the
5210 @acronym{BSD} functions as macros in @file{string.h} or
5211 @file{strings.h}; some had only the @acronym{BSD} functions but
5212 @file{string.h}; some declared the memory functions in @file{memory.h},
5213 some in @file{string.h}; etc.  It is probably sufficient to check for
5214 one string function and one memory function; if the library had the
5215 standard versions of those then it probably had most of the others.
5216 If you put the following in @file{configure.ac}:
5218 @example
5219 # This example is obsolescent.
5220 # Nowadays you can omit these macro calls.
5221 AC_HEADER_STDC
5222 AC_CHECK_FUNCS([strchr memcpy])
5223 @end example
5225 @noindent
5226 then, in your code, you can use declarations like this:
5228 @example
5229 @group
5230 /* This example is obsolescent.
5231    Nowadays you can just #include <string.h>.  */
5232 #if STDC_HEADERS
5233 # include <string.h>
5234 #else
5235 # if !HAVE_STRCHR
5236 #  define strchr index
5237 #  define strrchr rindex
5238 # endif
5239 char *strchr (), *strrchr ();
5240 # if !HAVE_MEMCPY
5241 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
5242 #  define memmove(d, s, n) bcopy ((s), (d), (n))
5243 # endif
5244 #endif
5245 @end group
5246 @end example
5248 @noindent
5249 If you use a function like @code{memchr}, @code{memset}, @code{strtok},
5250 or @code{strspn}, which have no @acronym{BSD} equivalent, then macros won't
5251 suffice to port to ancient hosts; you must provide an implementation of
5252 each function.  An easy
5253 way to incorporate your implementations only when needed (since the ones
5254 in system C libraries may be hand optimized) is to, taking @code{memchr}
5255 for example, put it in @file{memchr.c} and use
5256 @samp{AC_REPLACE_FUNCS([memchr])}.
5257 @end defmac
5259 @defmac AC_HEADER_SYS_WAIT
5260 @acindex{HEADER_SYS_WAIT}
5261 @cvindex HAVE_SYS_WAIT_H
5262 @hdrindex{sys/wait.h}
5263 If @file{sys/wait.h} exists and is compatible with Posix, define
5264 @code{HAVE_SYS_WAIT_H}.  Incompatibility can occur if @file{sys/wait.h}
5265 does not exist, or if it uses the old @acronym{BSD} @code{union wait} instead
5266 of @code{int} to store a status value.  If @file{sys/wait.h} is not
5267 Posix compatible, then instead of including it, define the
5268 Posix macros with their usual interpretations.  Here is an
5269 example:
5271 @example
5272 @group
5273 #include <sys/types.h>
5274 #if HAVE_SYS_WAIT_H
5275 # include <sys/wait.h>
5276 #endif
5277 #ifndef WEXITSTATUS
5278 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
5279 #endif
5280 #ifndef WIFEXITED
5281 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
5282 #endif
5283 @end group
5284 @end example
5286 @noindent
5287 This macro is obsolescent, as current systems are compatible with Posix.
5288 New programs need not use this macro.
5289 @end defmac
5291 @cvindex _POSIX_VERSION
5292 @hdrindex{unistd.h}
5293 @code{_POSIX_VERSION} is defined when @file{unistd.h} is included on
5294 Posix systems.  If there is no @file{unistd.h}, it is definitely
5295 not a Posix system.  However, some non-Posix systems do
5296 have @file{unistd.h}.
5298 The way to check whether the system supports Posix is:
5300 @example
5301 @group
5302 #if HAVE_UNISTD_H
5303 # include <sys/types.h>
5304 # include <unistd.h>
5305 #endif
5307 #ifdef _POSIX_VERSION
5308 /* Code for Posix systems.  */
5309 #endif
5310 @end group
5311 @end example
5313 @defmac AC_HEADER_TIME
5314 @acindex{HEADER_TIME}
5315 @cvindex TIME_WITH_SYS_TIME
5316 @hdrindex{time.h}
5317 @hdrindex{sys/time.h}
5318 If a program may include both @file{time.h} and @file{sys/time.h},
5319 define @code{TIME_WITH_SYS_TIME}.  On some ancient systems,
5320 @file{sys/time.h} included @file{time.h}, but @file{time.h} was not
5321 protected against multiple inclusion, so programs could not explicitly
5322 include both files.  This macro is useful in programs that use, for
5323 example, @code{struct timeval} as well as
5324 @code{struct tm}.  It is best used in conjunction with
5325 @code{HAVE_SYS_TIME_H}, which can be checked for using
5326 @code{AC_CHECK_HEADERS([sys/time.h])}.
5328 @example
5329 @group
5330 #if TIME_WITH_SYS_TIME
5331 # include <sys/time.h>
5332 # include <time.h>
5333 #else
5334 # if HAVE_SYS_TIME_H
5335 #  include <sys/time.h>
5336 # else
5337 #  include <time.h>
5338 # endif
5339 #endif
5340 @end group
5341 @end example
5343 @noindent
5344 This macro is obsolescent, as current systems can include both files
5345 when they exist.  New programs need not use this macro.
5346 @end defmac
5349 @defmac AC_HEADER_TIOCGWINSZ
5350 @acindex{HEADER_TIOCGWINSZ}
5351 @cvindex GWINSZ_IN_SYS_IOCTL
5352 @hdrindex{sys/ioctl.h}
5353 @hdrindex{termios.h}
5354 @c FIXME: I need clarifications from Jim.
5355 If the use of @code{TIOCGWINSZ} requires @file{<sys/ioctl.h>}, then
5356 define @code{GWINSZ_IN_SYS_IOCTL}.  Otherwise @code{TIOCGWINSZ} can be
5357 found in @file{<termios.h>}.
5359 Use:
5361 @example
5362 @group
5363 #if HAVE_TERMIOS_H
5364 # include <termios.h>
5365 #endif
5367 #if GWINSZ_IN_SYS_IOCTL
5368 # include <sys/ioctl.h>
5369 #endif
5370 @end group
5371 @end example
5372 @end defmac
5374 @node Generic Headers
5375 @subsection Generic Header Checks
5377 These macros are used to find system header files not covered by the
5378 ``particular'' test macros.  If you need to check the contents of a header
5379 as well as find out whether it is present, you have to write your own
5380 test for it (@pxref{Writing Tests}).
5382 @defmac AC_CHECK_HEADER (@var{header-file}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5383 @acindex{CHECK_HEADER}
5384 If the system header file @var{header-file} is compilable, execute shell
5385 commands @var{action-if-found}, otherwise execute
5386 @var{action-if-not-found}.  If you just want to define a symbol if the
5387 header file is available, consider using @code{AC_CHECK_HEADERS}
5388 instead.
5390 For compatibility issues with older versions of Autoconf, please read
5391 below.
5392 @end defmac
5394 @defmac AC_CHECK_HEADERS (@var{header-file}@dots{}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5395 @acindex{CHECK_HEADERS}
5396 @cvindex HAVE_@var{header}
5397 For each given system header file @var{header-file} in the
5398 blank-separated argument list that exists, define
5399 @code{HAVE_@var{header-file}} (in all capitals).  If @var{action-if-found}
5400 is given, it is additional shell code to execute when one of the header
5401 files is found.  You can give it a value of @samp{break} to break out of
5402 the loop on the first match.  If @var{action-if-not-found} is given, it
5403 is executed when one of the header files is not found.
5405 For compatibility issues with older versions of Autoconf, please read
5406 below.
5407 @end defmac
5409 Previous versions of Autoconf merely checked whether the header was
5410 accepted by the preprocessor.  This was changed because the old test was
5411 inappropriate for typical uses.  Headers are typically used to compile,
5412 not merely to preprocess, and the old behavior sometimes accepted
5413 headers that clashed at compile-time.  If you need to check whether a
5414 header is preprocessable, you can use @code{AC_PREPROC_IFELSE}
5415 (@pxref{Running the Preprocessor}).
5417 This scheme, which improves the robustness of the test, also requires
5418 that you make sure that headers that must be included before the
5419 @var{header-file} be part of the @var{includes}, (@pxref{Default
5420 Includes}).  If looking for @file{bar.h}, which requires that
5421 @file{foo.h} be included before if it exists, we suggest the following
5422 scheme:
5424 @verbatim
5425 AC_CHECK_HEADERS([foo.h])
5426 AC_CHECK_HEADERS([bar.h], [], [],
5427 [#if HAVE_FOO_H
5428 # include <foo.h>
5429 # endif
5431 @end verbatim
5433 The following variant generates smaller, faster @command{configure}
5434 files if you do not need the full power of @code{AC_CHECK_HEADERS}.
5436 @defmac AC_CHECK_HEADERS_ONCE (@var{header-file}@dots{})
5437 @acindex{CHECK_HEADERS_ONCE}
5438 @cvindex HAVE_@var{header}
5439 For each given system header file @var{header-file} in the
5440 blank-separated argument list that exists, define
5441 @code{HAVE_@var{header-file}} (in all capitals).
5442 This is a once-only variant of @code{AC_CHECK_HEADERS}.  It generates the
5443 checking code at most once, so that @command{configure} is smaller and
5444 faster; but the checks cannot be conditionalized and are always done once,
5445 early during the @command{configure} run.
5446 @end defmac
5448 @node Declarations
5449 @section Declarations
5450 @cindex Declaration, checking
5452 The following macros check for the declaration of variables and
5453 functions.  If there is no macro specifically defined to check for a
5454 symbol you need, then you can use the general macros (@pxref{Generic
5455 Declarations}) or, for more complex tests, you may use
5456 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5458 @menu
5459 * Particular Declarations::     Macros to check for certain declarations
5460 * Generic Declarations::        How to find other declarations
5461 @end menu
5463 @node Particular Declarations
5464 @subsection Particular Declaration Checks
5466 There are no specific macros for declarations.
5468 @node Generic Declarations
5469 @subsection Generic Declaration Checks
5471 These macros are used to find declarations not covered by the ``particular''
5472 test macros.
5474 @defmac AC_CHECK_DECL (@var{symbol}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5475 @acindex{CHECK_DECL}
5476 If @var{symbol} (a function or a variable) is not declared in
5477 @var{includes} and a declaration is needed, run the shell commands
5478 @var{action-if-not-found}, otherwise @var{action-if-found}.  If no
5479 @var{includes} are specified, the default includes are used
5480 (@pxref{Default Includes}).
5482 This macro actually tests whether it is valid to use @var{symbol} as an
5483 r-value, not if it is really declared, because it is much safer to avoid
5484 introducing extra declarations when they are not needed.
5485 @end defmac
5487 @defmac AC_CHECK_DECLS (@var{symbols}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5488 @acindex{CHECK_DECLS}
5489 @cvindex HAVE_DECL_@var{symbol}
5490 For each of the @var{symbols} (@emph{comma}-separated list), define
5491 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5492 @var{symbol} is declared, otherwise to @samp{0}.  If
5493 @var{action-if-not-found} is given, it is additional shell code to
5494 execute when one of the function declarations is needed, otherwise
5495 @var{action-if-found} is executed.
5497 This macro uses an m4 list as first argument:
5498 @example
5499 AC_CHECK_DECLS([strdup])
5500 AC_CHECK_DECLS([strlen])
5501 AC_CHECK_DECLS([malloc, realloc, calloc, free])
5502 @end example
5504 Unlike the other @samp{AC_CHECK_*S} macros, when a @var{symbol} is not
5505 declared, @code{HAVE_DECL_@var{symbol}} is defined to @samp{0} instead
5506 of leaving @code{HAVE_DECL_@var{symbol}} undeclared.  When you are
5507 @emph{sure} that the check was performed, use
5508 @code{HAVE_DECL_@var{symbol}} just like any other result of Autoconf:
5510 @example
5511 #if !HAVE_DECL_SYMBOL
5512 extern char *symbol;
5513 #endif
5514 @end example
5516 @noindent
5517 If the test may have not been performed, however, because it is safer
5518 @emph{not} to declare a symbol than to use a declaration that conflicts
5519 with the system's one, you should use:
5521 @example
5522 #if defined HAVE_DECL_MALLOC && !HAVE_DECL_MALLOC
5523 void *malloc (size_t *s);
5524 #endif
5525 @end example
5527 @noindent
5528 You fall into the second category only in extreme situations: either
5529 your files may be used without being configured, or they are used during
5530 the configuration.  In most cases the traditional approach is enough.
5531 @end defmac
5533 @defmac AC_CHECK_DECLS_ONCE (@var{symbols})
5534 @acindex{CHECK_DECLS_ONCE}
5535 @cvindex HAVE_DECL_@var{symbol}
5536 For each of the @var{symbols} (@emph{comma}-separated list), define
5537 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5538 @var{symbol} is declared in the default include files, otherwise to
5539 @samp{0}.  This is a once-only variant of @code{AC_CHECK_DECLS}.  It
5540 generates the checking code at most once, so that @command{configure} is
5541 smaller and faster; but the checks cannot be conditionalized and are
5542 always done once, early during the @command{configure} run.
5543 @end defmac
5546 @node Structures
5547 @section Structures
5548 @cindex Structure, checking
5550 The following macros check for the presence of certain members in C
5551 structures.  If there is no macro specifically defined to check for a
5552 member you need, then you can use the general structure-member macros
5553 (@pxref{Generic Structures}) or, for more complex tests, you may use
5554 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5556 @menu
5557 * Particular Structures::       Macros to check for certain structure members
5558 * Generic Structures::          How to find other structure members
5559 @end menu
5561 @node Particular Structures
5562 @subsection Particular Structure Checks
5564 The following macros check for certain structures or structure members.
5566 @defmac AC_STRUCT_DIRENT_D_INO
5567 @acindex{STRUCT_DIRENT_D_INO}
5568 @cvindex HAVE_STRUCT_DIRENT_D_INO
5569 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5570 Headers}).  Then, if @code{struct dirent} contains a @code{d_ino}
5571 member, define @code{HAVE_STRUCT_DIRENT_D_INO}.
5573 @code{HAVE_STRUCT_DIRENT_D_INO} indicates only the presence of
5574 @code{d_ino}, not whether its contents are always reliable.
5575 Traditionally, a zero @code{d_ino} indicated a deleted directory entry,
5576 though current systems hide this detail from the user and never return
5577 zero @code{d_ino} values.
5578 Many current systems report an incorrect @code{d_ino} for a directory
5579 entry that is a mount point.
5580 @end defmac
5582 @defmac AC_STRUCT_DIRENT_D_TYPE
5583 @acindex{STRUCT_DIRENT_D_TYPE}
5584 @cvindex HAVE_STRUCT_DIRENT_D_TYPE
5585 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5586 Headers}).  Then, if @code{struct dirent} contains a @code{d_type}
5587 member, define @code{HAVE_STRUCT_DIRENT_D_TYPE}.
5588 @end defmac
5590 @defmac AC_STRUCT_ST_BLKSIZE
5591 @acindex{STRUCT_ST_BLKSIZE}
5592 @cvindex HAVE_STRUCT_STAT_ST_BLKSIZE
5593 @cvindex HAVE_ST_BLKSIZE
5594 If @code{struct stat} contains an @code{st_blksize} member, define
5595 @code{HAVE_STRUCT_STAT_ST_BLKSIZE}.  The former name,
5596 @code{HAVE_ST_BLKSIZE} is to be avoided, as its support will cease in
5597 the future.  This macro is obsoleted, and should be replaced by
5599 @example
5600 AC_CHECK_MEMBERS([struct stat.st_blksize])
5601 @end example
5602 @end defmac
5604 @defmac AC_STRUCT_ST_BLOCKS
5605 @acindex{STRUCT_ST_BLOCKS}
5606 @cvindex HAVE_STRUCT_STAT_ST_BLOCKS
5607 @cvindex HAVE_ST_BLOCKS
5608 @ovindex LIBOBJS
5609 If @code{struct stat} contains an @code{st_blocks} member, define
5610 @code{HAVE_STRUCT_STAT_ST_BLOCKS}.  Otherwise, require an
5611 @code{AC_LIBOBJ} replacement of @samp{fileblocks}.  The former name,
5612 @code{HAVE_ST_BLOCKS} is to be avoided, as its support will cease in the
5613 future.
5614 @end defmac
5616 @defmac AC_STRUCT_ST_RDEV
5617 @acindex{STRUCT_ST_RDEV}
5618 @cvindex HAVE_ST_RDEV
5619 @cvindex HAVE_STRUCT_STAT_ST_RDEV
5620 If @code{struct stat} contains an @code{st_rdev} member, define
5621 @code{HAVE_STRUCT_STAT_ST_RDEV}.  The former name for this macro,
5622 @code{HAVE_ST_RDEV}, is to be avoided as it will cease to be supported
5623 in the future.  Actually, even the new macro is obsolete and should be
5624 replaced by:
5625 @example
5626 AC_CHECK_MEMBERS([struct stat.st_rdev])
5627 @end example
5628 @end defmac
5630 @defmac AC_STRUCT_TM
5631 @acindex{STRUCT_TM}
5632 @cvindex TM_IN_SYS_TIME
5633 @hdrindex{time.h}
5634 @hdrindex{sys/time.h}
5635 If @file{time.h} does not define @code{struct tm}, define
5636 @code{TM_IN_SYS_TIME}, which means that including @file{sys/time.h}
5637 had better define @code{struct tm}.
5639 This macro is obsolescent, as @file{time.h} defines @code{struct tm} in
5640 current systems.  New programs need not use this macro.
5641 @end defmac
5643 @defmac AC_STRUCT_TIMEZONE
5644 @acindex{STRUCT_TIMEZONE}
5645 @cvindex HAVE_TM_ZONE
5646 @cvindex HAVE_TZNAME
5647 Figure out how to get the current timezone.  If @code{struct tm} has a
5648 @code{tm_zone} member, define @code{HAVE_STRUCT_TM_TM_ZONE} (and the
5649 obsoleted @code{HAVE_TM_ZONE}).  Otherwise, if the external array
5650 @code{tzname} is found, define @code{HAVE_TZNAME}; if it is declared,
5651 define @code{HAVE_DECL_TZNAME}.
5652 @end defmac
5654 @node Generic Structures
5655 @subsection Generic Structure Checks
5657 These macros are used to find structure members not covered by the
5658 ``particular'' test macros.
5660 @defmac AC_CHECK_MEMBER (@var{aggregate}.@var{member}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5661 @acindex{CHECK_MEMBER}
5662 Check whether @var{member} is a member of the aggregate @var{aggregate}.
5663 If no @var{includes} are specified, the default includes are used
5664 (@pxref{Default Includes}).
5666 @example
5667 AC_CHECK_MEMBER([struct passwd.pw_gecos], [],
5668                 [AC_MSG_ERROR([We need `passwd.pw_gecos'!])],
5669                 [#include <pwd.h>])
5670 @end example
5672 You can use this macro for sub-members:
5674 @example
5675 AC_CHECK_MEMBER(struct top.middle.bot)
5676 @end example
5677 @end defmac
5679 @defmac AC_CHECK_MEMBERS (@var{members}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5680 @acindex{CHECK_MEMBERS}
5681 Check for the existence of each @samp{@var{aggregate}.@var{member}} of
5682 @var{members} using the previous macro.  When @var{member} belongs to
5683 @var{aggregate}, define @code{HAVE_@var{aggregate}_@var{member}} (in all
5684 capitals, with spaces and dots replaced by underscores).  If
5685 @var{action-if-found} is given, it is executed for each of the found
5686 members.  If @var{action-if-not-found} is given, it is executed for each
5687 of the members that could not be found.
5689 This macro uses m4 lists:
5690 @example
5691 AC_CHECK_MEMBERS([struct stat.st_rdev, struct stat.st_blksize])
5692 @end example
5693 @end defmac
5696 @node Types
5697 @section Types
5698 @cindex Types
5699 @cindex C types
5701 The following macros check for C types, either builtin or typedefs.  If
5702 there is no macro specifically defined to check for a type you need, and
5703 you don't need to check for any special properties of it, then you can
5704 use a general type-check macro.
5706 @menu
5707 * Particular Types::            Special handling to find certain types
5708 * Generic Types::               How to find other types
5709 @end menu
5711 @node Particular Types
5712 @subsection Particular Type Checks
5714 @hdrindex{sys/types.h}
5715 @hdrindex{stdlib.h}
5716 @hdrindex{stdint.h}
5717 @hdrindex{inttypes.h}
5718 These macros check for particular C types in @file{sys/types.h},
5719 @file{stdlib.h}, @file{stdint.h}, @file{inttypes.h} and others, if they
5720 exist.
5722 The Gnulib @code{stdint} module is an alternate way to define many of
5723 these symbols; it is useful if you prefer your code to assume a
5724 C99-or-better environment.  @xref{Gnulib}.
5726 @defmac AC_TYPE_GETGROUPS
5727 @acindex{TYPE_GETGROUPS}
5728 @cvindex GETGROUPS_T
5729 Define @code{GETGROUPS_T} to be whichever of @code{gid_t} or @code{int}
5730 is the base type of the array argument to @code{getgroups}.
5731 @end defmac
5733 @defmac AC_TYPE_INT8_T
5734 @acindex{TYPE_INT8_T}
5735 @cvindex HAVE_INT8_T
5736 @cvindex int8_t
5737 If @file{stdint.h} or @file{inttypes.h} defines the type @code{int8_t},
5738 define @code{HAVE_INT8_T}.  Otherwise, define @code{int8_t} to a signed
5739 integer type that is exactly 8 bits wide and that uses two's complement
5740 representation, if such a type exists.
5741 @end defmac
5743 @defmac AC_TYPE_INT16_T
5744 @acindex{TYPE_INT16_T}
5745 @cvindex HAVE_INT16_T
5746 @cvindex int16_t
5747 This is like @code{AC_TYPE_INT8_T}, except for 16-bit integers.
5748 @end defmac
5750 @defmac AC_TYPE_INT32_T
5751 @acindex{TYPE_INT32_T}
5752 @cvindex HAVE_INT32_T
5753 @cvindex int32_t
5754 This is like @code{AC_TYPE_INT8_T}, except for 32-bit integers.
5755 @end defmac
5757 @defmac AC_TYPE_INT64_T
5758 @acindex{TYPE_INT64_T}
5759 @cvindex HAVE_INT64_T
5760 @cvindex int64_t
5761 This is like @code{AC_TYPE_INT8_T}, except for 64-bit integers.
5762 @end defmac
5764 @defmac AC_TYPE_INTMAX_T
5765 @acindex{TYPE_INTMAX_T}
5766 @cvindex HAVE_INTMAX_T
5767 @cvindex intmax_t
5768 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intmax_t},
5769 define @code{HAVE_INTMAX_T}.  Otherwise, define @code{intmax_t} to the
5770 widest signed integer type.
5771 @end defmac
5773 @defmac AC_TYPE_INTPTR_T
5774 @acindex{TYPE_INTPTR_T}
5775 @cvindex HAVE_INTPTR_T
5776 @cvindex intptr_t
5777 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intptr_t},
5778 define @code{HAVE_INTPTR_T}.  Otherwise, define @code{intptr_t} to a
5779 signed integer type wide enough to hold a pointer, if such a type
5780 exists.
5781 @end defmac
5783 @defmac AC_TYPE_LONG_DOUBLE
5784 @acindex{TYPE_LONG_DOUBLE}
5785 @cvindex HAVE_LONG_DOUBLE
5786 If the C compiler supports a working @code{long double} type, define
5787 @code{HAVE_LONG_DOUBLE}.  The @code{long double} type might have the
5788 same range and precision as @code{double}.
5789 @end defmac
5791 @defmac AC_TYPE_LONG_DOUBLE_WIDER
5792 @acindex{TYPE_LONG_DOUBLE_WIDER}
5793 @cvindex HAVE_LONG_DOUBLE_WIDER
5794 If the C compiler supports a working @code{long double} type with more
5795 range or precision than the @code{double} type, define
5796 @code{HAVE_LONG_DOUBLE_WIDER}.
5797 @end defmac
5799 @defmac AC_TYPE_LONG_LONG_INT
5800 @acindex{TYPE_LONG_LONG_INT}
5801 @cvindex HAVE_LONG_LONG_INT
5802 If the C compiler supports a working @code{long long int} type, define
5803 @code{HAVE_LONG_LONG_INT}.
5804 @end defmac
5806 @defmac AC_TYPE_MBSTATE_T
5807 @acindex{TYPE_MBSTATE_T}
5808 @cvindex mbstate_t
5809 @hdrindex{wchar.h}
5810 Define @code{HAVE_MBSTATE_T} if @code{<wchar.h>} declares the
5811 @code{mbstate_t} type.  Also, define @code{mbstate_t} to be a type if
5812 @code{<wchar.h>} does not declare it.
5813 @end defmac
5815 @defmac AC_TYPE_MODE_T
5816 @acindex{TYPE_MODE_T}
5817 @cvindex mode_t
5818 Define @code{mode_t} to a suitable type, if standard headers do not
5819 define it.
5820 @end defmac
5822 @defmac AC_TYPE_OFF_T
5823 @acindex{TYPE_OFF_T}
5824 @cvindex off_t
5825 Define @code{off_t} to a suitable type, if standard headers do not
5826 define it.
5827 @end defmac
5829 @defmac AC_TYPE_PID_T
5830 @acindex{TYPE_PID_T}
5831 @cvindex pid_t
5832 Define @code{pid_t} to a suitable type, if standard headers do not
5833 define it.
5834 @end defmac
5836 @defmac AC_TYPE_SIGNAL
5837 @acindex{TYPE_SIGNAL}
5838 @cvindex RETSIGTYPE
5839 @hdrindex{signal.h}
5840 If @file{signal.h} declares @code{signal} as returning a pointer to a
5841 function returning @code{void}, define @code{RETSIGTYPE} to be
5842 @code{void}; otherwise, define it to be @code{int}.
5844 Define signal handlers as returning type @code{RETSIGTYPE}:
5846 @example
5847 @group
5848 RETSIGTYPE
5849 hup_handler ()
5851 @dots{}
5853 @end group
5854 @end example
5855 @end defmac
5857 @defmac AC_TYPE_SIZE_T
5858 @acindex{TYPE_SIZE_T}
5859 @cvindex size_t
5860 Define @code{size_t} to a suitable type, if standard headers do not
5861 define it.
5862 @end defmac
5864 @defmac AC_TYPE_SSIZE_T
5865 @acindex{TYPE_SSIZE_T}
5866 @cvindex ssize_t
5867 Define @code{ssize_t} to a suitable type, if standard headers do not
5868 define it.
5869 @end defmac
5871 @defmac AC_TYPE_UID_T
5872 @acindex{TYPE_UID_T}
5873 @cvindex uid_t
5874 @cvindex gid_t
5875 Define @code{uid_t} and @code{gid_t} to suitable types, if standard
5876 headers do not define them.
5877 @end defmac
5879 @defmac AC_TYPE_UINT8_T
5880 @acindex{TYPE_UINT8_T}
5881 @cvindex HAVE_UINT8_T
5882 @cvindex uint8_t
5883 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uint8_t},
5884 define @code{HAVE_UINT8_T}.  Otherwise, define @code{uint8_t} to an
5885 unsigned integer type that is exactly 8 bits wide, if such a type
5886 exists.
5887 @end defmac
5889 @defmac AC_TYPE_UINT16_T
5890 @acindex{TYPE_UINT16_T}
5891 @cvindex HAVE_UINT16_T
5892 @cvindex uint16_t
5893 This is like @code{AC_TYPE_UINT8_T}, except for 16-bit unsigned integers.
5894 @end defmac
5896 @defmac AC_TYPE_UINT32_T
5897 @acindex{TYPE_UINT32_T}
5898 @cvindex HAVE_UINT32_T
5899 @cvindex uint32_t
5900 This is like @code{AC_TYPE_UINT8_T}, except for 32-bit unsigned integers.
5901 @end defmac
5903 @defmac AC_TYPE_UINT64_T
5904 @acindex{TYPE_UINT64_T}
5905 @cvindex HAVE_UINT64_T
5906 @cvindex uint64_t
5907 This is like @code{AC_TYPE_UINT8_T}, except for 64-bit unsigned integers.
5908 @end defmac
5910 @defmac AC_TYPE_UINTMAX_T
5911 @acindex{TYPE_UINTMAX_T}
5912 @cvindex HAVE_UINTMAX_T
5913 @cvindex uintmax_t
5914 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintmax_t},
5915 define @code{HAVE_UINTMAX_T}.  Otherwise, define @code{uintmax_t} to the
5916 widest unsigned integer type.
5917 @end defmac
5919 @defmac AC_TYPE_UINTPTR_T
5920 @acindex{TYPE_UINTPTR_T}
5921 @cvindex HAVE_UINTPTR_T
5922 @cvindex uintptr_t
5923 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintptr_t},
5924 define @code{HAVE_UINTPTR_T}.  Otherwise, define @code{uintptr_t} to an
5925 unsigned integer type wide enough to hold a pointer, if such a type
5926 exists.
5927 @end defmac
5929 @defmac AC_TYPE_UNSIGNED_LONG_LONG_INT
5930 @acindex{TYPE_UNSIGNED_LONG_LONG_INT}
5931 @cvindex HAVE_UNSIGNED_LONG_LONG_INT
5932 If the C compiler supports a working @code{unsigned long long int} type,
5933 define @code{HAVE_UNSIGNED_LONG_LONG_INT}.
5934 @end defmac
5936 @node Generic Types
5937 @subsection Generic Type Checks
5939 These macros are used to check for types not covered by the ``particular''
5940 test macros.
5942 @defmac AC_CHECK_TYPE (@var{type}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5943 @acindex{CHECK_TYPE}
5944 Check whether @var{type} is defined.  It may be a compiler builtin type
5945 or defined by the @var{includes} (@pxref{Default Includes}).
5946 @end defmac
5949 @defmac AC_CHECK_TYPES (@var{types}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5950 @acindex{CHECK_TYPES}
5951 For each @var{type} of the @var{types} that is defined, define
5952 @code{HAVE_@var{type}} (in all capitals).  If no @var{includes} are
5953 specified, the default includes are used (@pxref{Default Includes}).  If
5954 @var{action-if-found} is given, it is additional shell code to execute
5955 when one of the types is found.  If @var{action-if-not-found} is given,
5956 it is executed when one of the types is not found.
5958 This macro uses m4 lists:
5959 @example
5960 AC_CHECK_TYPES([ptrdiff_t])
5961 AC_CHECK_TYPES([unsigned long long int, uintmax_t])
5962 @end example
5964 @end defmac
5966 Autoconf, up to 2.13, used to provide to another version of
5967 @code{AC_CHECK_TYPE}, broken by design.  In order to keep backward
5968 compatibility, a simple heuristics, quite safe but not totally, is
5969 implemented.  In case of doubt, read the documentation of the former
5970 @code{AC_CHECK_TYPE}, see @ref{Obsolete Macros}.
5973 @node Compilers and Preprocessors
5974 @section Compilers and Preprocessors
5975 @cindex Compilers
5976 @cindex Preprocessors
5978 @ovindex EXEEXT
5979 All the tests for compilers (@code{AC_PROG_CC}, @code{AC_PROG_CXX},
5980 @code{AC_PROG_F77}) define the output variable @code{EXEEXT} based on
5981 the output of the compiler, typically to the empty string if
5982 Posix and @samp{.exe} if a @acronym{DOS} variant.
5984 @ovindex OBJEXT
5985 They also define the output variable @code{OBJEXT} based on the
5986 output of the compiler, after @file{.c} files have been excluded, typically
5987 to @samp{o} if Posix, @samp{obj} if a @acronym{DOS} variant.
5989 If the compiler being used does not produce executables, the tests fail.  If
5990 the executables can't be run, and cross-compilation is not enabled, they
5991 fail too.  @xref{Manual Configuration}, for more on support for cross
5992 compiling.
5994 @menu
5995 * Specific Compiler Characteristics::  Some portability issues
5996 * Generic Compiler Characteristics::  Language independent tests and features
5997 * C Compiler::                  Checking its characteristics
5998 * C++ Compiler::                Likewise
5999 * Objective C Compiler::        Likewise
6000 * Erlang Compiler and Interpreter::  Likewise
6001 * Fortran Compiler::            Likewise
6002 @end menu
6004 @node Specific Compiler Characteristics
6005 @subsection Specific Compiler Characteristics
6007 Some compilers exhibit different behaviors.
6009 @table @asis
6010 @item Static/Dynamic Expressions
6011 Autoconf relies on a trick to extract one bit of information from the C
6012 compiler: using negative array sizes.  For instance the following
6013 excerpt of a C source demonstrates how to test whether @samp{int}s are 4
6014 bytes wide:
6016 @example
6018 main (void)
6020   static int test_array [sizeof (int) == 4 ? 1 : -1];
6021   test_array [0] = 0;
6022   return 0;
6024 @end example
6026 @noindent
6027 To our knowledge, there is a single compiler that does not support this
6028 trick: the HP C compilers (the real one, not only the ``bundled'') on
6029 HP-UX 11.00.  They incorrectly reject the above program with the diagnostic
6030 ``Variable-length arrays cannot have static storage.''
6031 This bug comes from HP compilers' mishandling of @code{sizeof (int)},
6032 not from the @code{? 1 : -1}, and
6033 Autoconf works around this problem by casting @code{sizeof (int)} to
6034 @code{long int} before comparing it.
6035 @end table
6037 @node Generic Compiler Characteristics
6038 @subsection Generic Compiler Characteristics
6040 @defmac AC_CHECK_SIZEOF (@var{type}, @ovar{unused}, @dvar{includes, default-includes})
6041 @acindex{CHECK_SIZEOF}
6042 Define @code{SIZEOF_@var{type}} (@pxref{Standard Symbols}) to be the
6043 size in bytes of @var{type}.  If @samp{type} is unknown, it gets a size
6044 of 0.  If no @var{includes} are specified, the default includes are used
6045 (@pxref{Default Includes}).  If you provide @var{include}, be sure to
6046 include @file{stdio.h} which is required for this macro to run.
6048 This macro now works even when cross-compiling.  The @var{unused}
6049 argument was used when cross-compiling.
6051 For example, the call
6053 @example
6054 AC_CHECK_SIZEOF([int *])
6055 @end example
6057 @noindent
6058 defines @code{SIZEOF_INT_P} to be 8 on DEC Alpha AXP systems.
6059 @end defmac
6061 @defmac AC_CHECK_ALIGNOF (@var{type}, @dvar{includes, default-includes})
6062 @acindex{CHECK_ALIGNOF}
6063 Define @code{ALIGNOF_@var{type}} (@pxref{Standard Symbols}) to be the
6064 alignment in bytes of @var{type}.  If @samp{type} is unknown, it gets a size
6065 of 0.  If no @var{includes} are specified, the default includes are used
6066 (@pxref{Default Includes}).  If you provide @var{include}, be sure to
6067 include @file{stddef.h} and @file{stdio.h} which are required for this
6068 macro to work correctly.
6069 @end defmac
6071 @defmac AC_LANG_WERROR
6072 @acindex{LANG_WERROR}
6073 Normally Autoconf ignores warnings generated by the compiler, linker, and
6074 preprocessor.  If this macro is used, warnings will be treated as fatal
6075 errors instead for the current language.  This macro is useful when the
6076 results of configuration will be used where warnings are unacceptable; for
6077 instance, if parts of a program are built with the GCC @option{-Werror}
6078 option.  If the whole program will be built using @option{-Werror} it is
6079 often simpler to put @option{-Werror} in the compiler flags (@code{CFLAGS},
6080 etc.).
6081 @end defmac
6083 @node C Compiler
6084 @subsection C Compiler Characteristics
6086 The following macros provide ways to find and exercise a C Compiler.
6087 There are a few constructs that ought to be avoided, but do not deserve
6088 being checked for, since they can easily be worked around.
6090 @table @asis
6091 @item Don't use lines containing solitary backslashes
6092 They tickle a bug in the HP-UX C compiler (checked on HP-UX 10.20,
6093 11.00, and 11i).  When given the following source:
6095 @example
6096 #ifdef __STDC__
6098 * A comment with backslash-newlines in it.  %@{ %@} *\
6101 char str[] = "\\
6102 " A string with backslash-newlines in it %@{ %@} \\
6104 char apostrophe = '\\
6108 #endif
6109 @end example
6111 @noindent
6112 the compiler incorrectly fails with the diagnostics ``Non-terminating
6113 comment at end of file'' and ``Missing @samp{#endif} at end of file.''
6114 Removing the lines with solitary backslashes solves the problem.
6116 @item Don't compile several files at once if output matters to you
6117 Some compilers, such as the HP's, reports the name of the file it is
6118 compiling @emph{when} they are several.  For instance:
6120 @example
6121 $ @kbd{cc a.c b.c}
6122 a.c:
6123 b.c:
6124 @end example
6126 @noindent
6127 This can cause problems if you observe the output of the compiler to
6128 detect failures.  Invoking @samp{cc -c a.c && cc -c b.c && cc -o c a.o
6129 b.o} solves the issue.
6131 @item Don't rely on @code{#error} failing
6132 The @sc{irix} C compiler does not fail when #error is preprocessed; it
6133 simply emits a diagnostic and continues, exiting successfully.  So,
6134 instead of an error directive like @code{#error "Unsupported word size"}
6135 it is more portable to use an invalid directive like @code{#Unsupported
6136 word size} in Autoconf tests.  In ordinary source code, @code{#error} is
6137 OK, since installers with inadequate compilers like @sc{irix} can simply
6138 examine these compilers' diagnostic output.
6140 @item Don't rely on correct @code{#line} support
6141 On Solaris, @command{c89} (at least Sun C 5.3 through 5.8)
6142 diagnoses @code{#line} directives whose line
6143 numbers are greater than 32767.  Nothing in Posix
6144 makes this invalid.  That is why Autoconf stopped issuing
6145 @code{#line} directives.
6146 @end table
6148 @defmac AC_PROG_CC (@ovar{compiler-search-list})
6149 @acindex{PROG_CC}
6150 @ovindex CC
6151 @ovindex CFLAGS
6152 Determine a C compiler to use.  If @code{CC} is not already set in the
6153 environment, check for @code{gcc} and @code{cc}, then for other C
6154 compilers.  Set output variable @code{CC} to the name of the compiler
6155 found.
6157 This macro may, however, be invoked with an optional first argument
6158 which, if specified, must be a blank-separated list of C compilers to
6159 search for.  This just gives the user an opportunity to specify an
6160 alternative search list for the C compiler.  For example, if you didn't
6161 like the default order, then you could invoke @code{AC_PROG_CC} like
6162 this:
6164 @example
6165 AC_PROG_CC([gcc cl cc])
6166 @end example
6168 If the C compiler does not handle function prototypes correctly by
6169 default, try to add an option to output variable @code{CC} to make it
6170 so.  This macro tries various options that select standard-conformance
6171 modes on various systems.
6173 After calling this macro you can check whether the C compiler has been
6174 set to accept @acronym{ANSI} C89 (@acronym{ISO} C90); if not, the shell
6175 variable
6176 @code{ac_cv_prog_cc_c89} is set to @samp{no}.  See also
6177 @code{AC_C_PROTOTYPES} below.
6179 If using the @acronym{GNU} C compiler, set shell variable @code{GCC} to
6180 @samp{yes}.  If output variable @code{CFLAGS} was not already set, set
6181 it to @option{-g -O2} for the @acronym{GNU} C compiler (@option{-O2} on systems
6182 where GCC does not accept @option{-g}), or @option{-g} for other compilers.
6183 @end defmac
6185 @defmac AC_PROG_CC_C_O
6186 @acindex{PROG_CC_C_O}
6187 @cvindex NO_MINUS_C_MINUS_O
6188 If the C compiler does not accept the @option{-c} and @option{-o} options
6189 simultaneously, define @code{NO_MINUS_C_MINUS_O}.  This macro actually
6190 tests both the compiler found by @code{AC_PROG_CC}, and, if different,
6191 the first @code{cc} in the path.  The test fails if one fails.  This
6192 macro was created for @acronym{GNU} Make to choose the default C compilation
6193 rule.
6194 @end defmac
6197 @defmac AC_PROG_CPP
6198 @acindex{PROG_CPP}
6199 @ovindex CPP
6200 Set output variable @code{CPP} to a command that runs the
6201 C preprocessor.  If @samp{$CC -E} doesn't work, @file{/lib/cpp} is used.
6202 It is only portable to run @code{CPP} on files with a @file{.c}
6203 extension.
6205 Some preprocessors don't indicate missing include files by the error
6206 status.  For such preprocessors an internal variable is set that causes
6207 other macros to check the standard error from the preprocessor and
6208 consider the test failed if any warnings have been reported.
6209 For most preprocessors, though, warnings do not cause include-file
6210 tests to fail unless @code{AC_PROG_CPP_WERROR} is also specified.
6211 @end defmac
6213 @defmac AC_PROG_CPP_WERROR
6214 @acindex{PROG_CPP_WERROR}
6215 @ovindex CPP
6216 This acts like @code{AC_PROG_CPP}, except it treats warnings from the
6217 preprocessor as errors even if the preprocessor exit status indicates
6218 success.  This is useful for avoiding headers that generate mandatory
6219 warnings, such as deprecation notices.
6220 @end defmac
6223 The following macros check for C compiler or machine architecture
6224 features.  To check for characteristics not listed here, use
6225 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
6226 @code{AC_RUN_IFELSE} (@pxref{Runtime}).
6228 @defmac AC_PROG_CC_STDC
6229 @acindex{PROG_CC_STDC}
6230 If the C compiler cannot compile @acronym{ISO} Standard C (currently
6231 C99), try to add an option to output variable @code{CC} to make it work.
6232 If the compiler does not support C99, fall back to supporting
6233 @acronym{ANSI} C89 (@acronym{ISO} C90).
6235 After calling this macro you can check whether the C compiler has been
6236 set to accept Standard C; if not, the shell variable
6237 @code{ac_cv_prog_cc_stdc} is set to @samp{no}.
6238 @end defmac
6240 @defmac AC_PROG_CC_C89
6241 @acindex{PROG_CC_C89}
6242 If the C compiler is not in @acronym{ANSI} C89 (@acronym{ISO} C90) mode by
6243 default, try to add an option to output variable @code{CC} to make it
6244 so.  This macro tries various options that select @acronym{ANSI} C89 on
6245 some system or another.  It considers the compiler to be in
6246 @acronym{ANSI} C89 mode if it handles function prototypes correctly.
6248 After calling this macro you can check whether the C compiler has been
6249 set to accept @acronym{ANSI} C89; if not, the shell variable
6250 @code{ac_cv_prog_cc_c89} is set to @samp{no}.
6252 This macro is called automatically by @code{AC_PROG_CC}.
6253 @end defmac
6255 @defmac AC_PROG_CC_C99
6256 @acindex{PROG_CC_C99}
6257 If the C compiler is not in C99 mode by default, try to add an
6258 option to output variable @code{CC} to make it so.  This macro tries
6259 various options that select C99 on some system or another.  It
6260 considers the compiler to be in C99 mode if it handles @code{_Bool},
6261 flexible arrays, @code{inline}, @code{long long int}, mixed code and
6262 declarations, named initialization of structs, @code{restrict}, varargs
6263 macros, variable declarations in @code{for} loops and variable length
6264 arrays.
6266 After calling this macro you can check whether the C compiler has been
6267 set to accept C99; if not, the shell variable
6268 @code{ac_cv_prog_cc_c99} is set to @samp{no}.
6269 @end defmac
6271 @defmac AC_C_BACKSLASH_A
6272 @acindex{HAVE_C_BACKSLASH_A}
6273 Define @samp{HAVE_C_BACKSLASH_A} to 1 if the C compiler understands
6274 @samp{\a}.
6276 This macro is obsolescent, as current C compilers understand @samp{\a}.
6277 New programs need not use this macro.
6278 @end defmac
6280 @defmac AC_C_BIGENDIAN (@ovar{action-if-true}, @ovar{action-if-false}, @ovar{action-if-unknown})
6281 @acindex{C_BIGENDIAN}
6282 @cvindex WORDS_BIGENDIAN
6283 @cindex Endianness
6284 If words are stored with the most significant byte first (like Motorola
6285 and SPARC CPUs), execute @var{action-if-true}.  If words are stored with
6286 the least significant byte first (like Intel and VAX CPUs), execute
6287 @var{action-if-false}.
6289 This macro runs a test-case if endianness cannot be determined from the
6290 system header files.  When cross-compiling, the test-case is not run but
6291 grep'ed for some magic values.  @var{action-if-unknown} is executed if
6292 the latter case fails to determine the byte sex of the host system.
6294 The default for @var{action-if-true} is to define
6295 @samp{WORDS_BIGENDIAN}.  The default for @var{action-if-false} is to do
6296 nothing.  And finally, the default for @var{action-if-unknown} is to
6297 abort configure and tell the installer which variable he should preset
6298 to bypass this test.
6299 @end defmac
6301 @defmac AC_C_CONST
6302 @acindex{C_CONST}
6303 @cvindex const
6304 If the C compiler does not fully support the @code{const} keyword,
6305 define @code{const} to be empty.  Some C compilers that do
6306 not define @code{__STDC__} do support @code{const}; some compilers that
6307 define @code{__STDC__} do not completely support @code{const}.  Programs
6308 can simply use @code{const} as if every C compiler supported it; for
6309 those that don't, the @file{Makefile} or configuration header file will
6310 define it as empty.
6312 Occasionally installers use a C++ compiler to compile C code, typically
6313 because they lack a C compiler.  This causes problems with @code{const},
6314 because C and C++ treat @code{const} differently.  For example:
6316 @example
6317 const int foo;
6318 @end example
6320 @noindent
6321 is valid in C but not in C++.  These differences unfortunately cannot be
6322 papered over by defining @code{const} to be empty.
6324 If @command{autoconf} detects this situation, it leaves @code{const} alone,
6325 as this generally yields better results in practice.  However, using a
6326 C++ compiler to compile C code is not recommended or supported, and
6327 installers who run into trouble in this area should get a C compiler
6328 like GCC to compile their C code.
6330 This macro is obsolescent, as current C compilers support @code{const}.
6331 New programs need not use this macro.
6332 @end defmac
6334 @defmac AC_C_RESTRICT
6335 @acindex{C_RESTRICT}
6336 @cvindex restrict
6337 If the C compiler recognizes the @code{restrict} keyword, don't do anything.
6338 If it recognizes only a variant spelling (@code{__restrict},
6339 @code{__restrict__}, or @code{_Restrict}), then define
6340 @code{restrict} to that.
6341 Otherwise, define @code{restrict} to be empty.
6342 Thus, programs may simply use @code{restrict} as if every C compiler
6343 supported it; for those that do not, the @file{Makefile}
6344 or configuration header defines it away.
6346 Although support in C++ for the @code{restrict} keyword is not
6347 required, several C++ compilers do accept the keyword.
6348 This macro works for them, too.
6349 @end defmac
6351 @defmac AC_C_VOLATILE
6352 @acindex{C_VOLATILE}
6353 @cvindex volatile
6354 If the C compiler does not understand the keyword @code{volatile},
6355 define @code{volatile} to be empty.  Programs can simply use
6356 @code{volatile} as if every C compiler supported it; for those that do
6357 not, the @file{Makefile} or configuration header will define it as
6358 empty.
6360 If the correctness of your program depends on the semantics of
6361 @code{volatile}, simply defining it to be empty does, in a sense, break
6362 your code.  However, given that the compiler does not support
6363 @code{volatile}, you are at its mercy anyway.  At least your
6364 program will compile, when it wouldn't before.
6366 In general, the @code{volatile} keyword is a standard C feature, so
6367 you might expect that @code{volatile} is available only when
6368 @code{__STDC__} is defined.  However, Ultrix 4.3's native compiler does
6369 support volatile, but does not define @code{__STDC__}.
6371 This macro is obsolescent, as current C compilers support @code{volatile}.
6372 New programs need not use this macro.
6373 @end defmac
6375 @defmac AC_C_INLINE
6376 @acindex{C_INLINE}
6377 @cvindex inline
6378 If the C compiler supports the keyword @code{inline}, do nothing.
6379 Otherwise define @code{inline} to @code{__inline__} or @code{__inline}
6380 if it accepts one of those, otherwise define @code{inline} to be empty.
6381 @end defmac
6383 @defmac AC_C_CHAR_UNSIGNED
6384 @acindex{C_CHAR_UNSIGNED}
6385 @cvindex __CHAR_UNSIGNED__
6386 If the C type @code{char} is unsigned, define @code{__CHAR_UNSIGNED__},
6387 unless the C compiler predefines it.
6388 @end defmac
6390 @defmac AC_C_STRINGIZE
6391 @acindex{C_STRINGIZE}
6392 @cvindex HAVE_STRINGIZE
6393 If the C preprocessor supports the stringizing operator, define
6394 @code{HAVE_STRINGIZE}.  The stringizing operator is @samp{#} and is
6395 found in macros such as this:
6397 @example
6398 #define x(y) #y
6399 @end example
6401 This macro is obsolescent, as current C compilers support the
6402 stringizing operator.  New programs need not use this macro.
6403 @end defmac
6405 @defmac AC_C_TYPEOF
6406 @acindex{C_TYPEOF}
6407 @cvindex HAVE_TYPEOF
6408 @cvindex typeof
6409 If the C compiler supports GCC's @code{typeof} syntax either directly or
6410 through a different spelling of the keyword (e.g., @code{__typeof__}),
6411 define @code{HAVE_TYPEOF}.  If the support is available only through a
6412 different spelling, define @code{typeof} to that spelling.
6413 @end defmac
6415 @defmac AC_C_PROTOTYPES
6416 @acindex{C_PROTOTYPES}
6417 @cvindex PROTOTYPES
6418 @cvindex __PROTOTYPES
6419 @cvindex PARAMS
6420 If function prototypes are understood by the compiler (as determined by
6421 @code{AC_PROG_CC}), define @code{PROTOTYPES} and @code{__PROTOTYPES}.
6422 Defining @code{__PROTOTYPES} is for the benefit of
6423 header files that cannot use macros that infringe on user name space.
6425 This macro is obsolescent, as current C compilers support prototypes.
6426 New programs need not use this macro.
6427 @end defmac
6429 @defmac AC_PROG_GCC_TRADITIONAL
6430 @acindex{PROG_GCC_TRADITIONAL}
6431 @ovindex CC
6432 Add @option{-traditional} to output variable @code{CC} if using the
6433 @acronym{GNU} C compiler and @code{ioctl} does not work properly without
6434 @option{-traditional}.  That usually happens when the fixed header files
6435 have not been installed on an old system.
6437 This macro is obsolescent, since current versions of the @acronym{GNU} C
6438 compiler fix the header files automatically when installed.
6439 @end defmac
6442 @node C++ Compiler
6443 @subsection C++ Compiler Characteristics
6446 @defmac AC_PROG_CXX (@ovar{compiler-search-list})
6447 @acindex{PROG_CXX}
6448 @ovindex CXX
6449 @ovindex CXXFLAGS
6450 Determine a C++ compiler to use.  Check whether the environment variable
6451 @code{CXX} or @code{CCC} (in that order) is set; if so, then set output
6452 variable @code{CXX} to its value.
6454 Otherwise, if the macro is invoked without an argument, then search for
6455 a C++ compiler under the likely names (first @code{g++} and @code{c++}
6456 then other names).  If none of those checks succeed, then as a last
6457 resort set @code{CXX} to @code{g++}.
6459 This macro may, however, be invoked with an optional first argument
6460 which, if specified, must be a blank-separated list of C++ compilers to
6461 search for.  This just gives the user an opportunity to specify an
6462 alternative search list for the C++ compiler.  For example, if you
6463 didn't like the default order, then you could invoke @code{AC_PROG_CXX}
6464 like this:
6466 @example
6467 AC_PROG_CXX([gcc cl KCC CC cxx cc++ xlC aCC c++ g++])
6468 @end example
6470 If using the @acronym{GNU} C++ compiler, set shell variable @code{GXX} to
6471 @samp{yes}.  If output variable @code{CXXFLAGS} was not already set, set
6472 it to @option{-g -O2} for the @acronym{GNU} C++ compiler (@option{-O2} on
6473 systems where G++ does not accept @option{-g}), or @option{-g} for other
6474 compilers.
6475 @end defmac
6477 @defmac AC_PROG_CXXCPP
6478 @acindex{PROG_CXXCPP}
6479 @ovindex CXXCPP
6480 Set output variable @code{CXXCPP} to a command that runs the C++
6481 preprocessor.  If @samp{$CXX -E} doesn't work, @file{/lib/cpp} is used.
6482 It is portable to run @code{CXXCPP} only on files with a @file{.c},
6483 @file{.C}, @file{.cc}, or @file{.cpp} extension.
6485 Some preprocessors don't indicate missing include files by the error
6486 status.  For such preprocessors an internal variable is set that causes
6487 other macros to check the standard error from the preprocessor and
6488 consider the test failed if any warnings have been reported.  However,
6489 it is not known whether such broken preprocessors exist for C++.
6490 @end defmac
6492 @defmac AC_PROG_CXX_C_O
6493 @acindex{PROG_CXX_C_O}
6494 @cvindex CXX_NO_MINUS_C_MINUS_O
6495 Test whether the C++ compiler accepts the options @option{-c} and
6496 @option{-o} simultaneously, and define @code{CXX_NO_MINUS_C_MINUS_O},
6497 if it does not.
6498 @end defmac
6501 @node Objective C Compiler
6502 @subsection Objective C Compiler Characteristics
6505 @defmac AC_PROG_OBJC (@ovar{compiler-search-list})
6506 @acindex{PROG_OBJC}
6507 @ovindex OBJC
6508 @ovindex OBJCFLAGS
6509 Determine an Objective C compiler to use.  If @code{OBJC} is not already
6510 set in the environment, check for Objective C compilers.  Set output
6511 variable @code{OBJC} to the name of the compiler found.
6513 This macro may, however, be invoked with an optional first argument
6514 which, if specified, must be a blank-separated list of Objective C compilers to
6515 search for.  This just gives the user an opportunity to specify an
6516 alternative search list for the Objective C compiler.  For example, if you
6517 didn't like the default order, then you could invoke @code{AC_PROG_OBJC}
6518 like this:
6520 @example
6521 AC_PROG_OBJC([gcc objcc objc])
6522 @end example
6524 If using the @acronym{GNU} Objective C compiler, set shell variable
6525 @code{GOBJC} to @samp{yes}.  If output variable @code{OBJCFLAGS} was not
6526 already set, set it to @option{-g -O2} for the @acronym{GNU} Objective C
6527 compiler (@option{-O2} on systems where @command{gcc} does not accept
6528 @option{-g}), or @option{-g} for other compilers.
6529 @end defmac
6531 @defmac AC_PROG_OBJCCPP
6532 @acindex{PROG_OBJCCPP}
6533 @ovindex OBJCCPP
6534 Set output variable @code{OBJCCPP} to a command that runs the Objective C
6535 preprocessor.  If @samp{$OBJC -E} doesn't work, @file{/lib/cpp} is used.
6536 @end defmac
6539 @node Erlang Compiler and Interpreter
6540 @subsection Erlang Compiler and Interpreter Characteristics
6541 @cindex Erlang
6543 Autoconf defines the following macros for determining paths to the essential
6544 Erlang/OTP programs:
6546 @defmac AC_ERLANG_PATH_ERLC (@ovar{value-if-not-found}, @ovar{path})
6547 @acindex{ERLANG_PATH_ERLC}
6548 @ovindex ERLC
6549 @ovindex ERLCFLAGS
6550 Determine an Erlang compiler to use.  If @code{ERLC} is not already set in the
6551 environment, check for @command{erlc}.  Set output variable @code{ERLC} to the
6552 complete path of the compiler command found.  In addition, if @code{ERLCFLAGS}
6553 is not set in the environment, set it to an empty value.
6555 The two optional arguments have the same meaning as the two last arguments of
6556 macro @code{AC_PROG_PATH} for looking for the @command{erlc} program.  For
6557 example, to look for @command{erlc} only in the @file{/usr/lib/erlang/bin}
6558 directory:
6560 @example
6561 AC_ERLANG_PATH_ERLC([not found], [/usr/lib/erlang/bin])
6562 @end example
6563 @end defmac
6565 @defmac AC_ERLANG_NEED_ERLC (@ovar{path})
6566 @acindex{ERLANG_NEED_ERLC}
6567 A simplified variant of the @code{AC_ERLANG_PATH_ERLC} macro, that prints an
6568 error message and exits the @command{configure} script if the @command{erlc}
6569 program is not found.
6570 @end defmac
6572 @defmac AC_ERLANG_PATH_ERL (@ovar{value-if-not-found}, @ovar{path})
6573 @acindex{ERLANG_PATH_ERL}
6574 @ovindex ERL
6575 Determine an Erlang interpreter to use.  If @code{ERL} is not already set in the
6576 environment, check for @command{erl}.  Set output variable @code{ERL} to the
6577 complete path of the interpreter command found.
6579 The two optional arguments have the same meaning as the two last arguments of
6580 macro @code{AC_PROG_PATH} for looking for the @command{erl} program.  For
6581 example, to look for @command{erl} only in the @file{/usr/lib/erlang/bin}
6582 directory:
6584 @example
6585 AC_ERLANG_PATH_ERL([not found], [/usr/lib/erlang/bin])
6586 @end example
6587 @end defmac
6589 @defmac AC_ERLANG_NEED_ERL (@ovar{path})
6590 @acindex{ERLANG_NEED_ERL}
6591 A simplified variant of the @code{AC_ERLANG_PATH_ERL} macro, that prints an
6592 error message and exits the @command{configure} script if the @command{erl}
6593 program is not found.
6594 @end defmac
6597 @node Fortran Compiler
6598 @subsection Fortran Compiler Characteristics
6599 @cindex Fortran
6600 @cindex F77
6602 The Autoconf Fortran support is divided into two categories: legacy
6603 Fortran 77 macros (@code{F77}), and modern Fortran macros (@code{FC}).
6604 The former are intended for traditional Fortran 77 code, and have output
6605 variables like @code{F77}, @code{FFLAGS}, and @code{FLIBS}.  The latter
6606 are for newer programs that can (or must) compile under the newer
6607 Fortran standards, and have output variables like @code{FC},
6608 @code{FCFLAGS}, and @code{FCLIBS}.
6610 Except for two new macros @code{AC_FC_SRCEXT} and
6611 @code{AC_FC_FREEFORM} (see below), the @code{FC} and @code{F77} macros
6612 behave almost identically, and so they are documented together in this
6613 section.
6616 @defmac AC_PROG_F77 (@ovar{compiler-search-list})
6617 @acindex{PROG_F77}
6618 @ovindex F77
6619 @ovindex FFLAGS
6620 Determine a Fortran 77 compiler to use.  If @code{F77} is not already
6621 set in the environment, then check for @code{g77} and @code{f77}, and
6622 then some other names.  Set the output variable @code{F77} to the name
6623 of the compiler found.
6625 This macro may, however, be invoked with an optional first argument
6626 which, if specified, must be a blank-separated list of Fortran 77
6627 compilers to search for.  This just gives the user an opportunity to
6628 specify an alternative search list for the Fortran 77 compiler.  For
6629 example, if you didn't like the default order, then you could invoke
6630 @code{AC_PROG_F77} like this:
6632 @example
6633 AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
6634 @end example
6636 If using @code{g77} (the @acronym{GNU} Fortran 77 compiler), then
6637 @code{AC_PROG_F77} will set the shell variable @code{G77} to @samp{yes}.
6638 If the output variable @code{FFLAGS} was not already set in the
6639 environment, then set it to @option{-g -02} for @code{g77} (or @option{-O2}
6640 where @code{g77} does not accept @option{-g}).  Otherwise, set
6641 @code{FFLAGS} to @option{-g} for all other Fortran 77 compilers.
6642 @end defmac
6644 @defmac AC_PROG_FC (@ovar{compiler-search-list}, @ovar{dialect})
6645 @acindex{PROG_FC}
6646 @ovindex FC
6647 @ovindex FCFLAGS
6648 Determine a Fortran compiler to use.  If @code{FC} is not already set in
6649 the environment, then @code{dialect} is a hint to indicate what Fortran
6650 dialect to search for; the default is to search for the newest available
6651 dialect.  Set the output variable @code{FC} to the name of the compiler
6652 found.
6654 By default, newer dialects are preferred over older dialects, but if
6655 @code{dialect} is specified then older dialects are preferred starting
6656 with the specified dialect.  @code{dialect} can currently be one of
6657 Fortran 77, Fortran 90, or Fortran 95.  However, this is only a hint of
6658 which compiler @emph{name} to prefer (e.g., @code{f90} or @code{f95}),
6659 and no attempt is made to guarantee that a particular language standard
6660 is actually supported.  Thus, it is preferable that you avoid the
6661 @code{dialect} option, and use AC_PROG_FC only for code compatible with
6662 the latest Fortran standard.
6664 This macro may, alternatively, be invoked with an optional first argument
6665 which, if specified, must be a blank-separated list of Fortran
6666 compilers to search for, just as in @code{AC_PROG_F77}.
6668 If the output variable @code{FCFLAGS} was not already set in the
6669 environment, then set it to @option{-g -02} for @acronym{GNU} @code{g77} (or
6670 @option{-O2} where @code{g77} does not accept @option{-g}).  Otherwise,
6671 set @code{FCFLAGS} to @option{-g} for all other Fortran compilers.
6672 @end defmac
6674 @defmac AC_PROG_F77_C_O
6675 @defmacx AC_PROG_FC_C_O
6676 @acindex{PROG_F77_C_O}
6677 @acindex{PROG_FC_C_O}
6678 @cvindex F77_NO_MINUS_C_MINUS_O
6679 @cvindex FC_NO_MINUS_C_MINUS_O
6680 Test whether the Fortran compiler accepts the options @option{-c} and
6681 @option{-o} simultaneously, and define @code{F77_NO_MINUS_C_MINUS_O} or
6682 @code{FC_NO_MINUS_C_MINUS_O}, respectively, if it does not.
6683 @end defmac
6685 The following macros check for Fortran compiler characteristics.
6686 To check for characteristics not listed here, use
6687 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
6688 @code{AC_RUN_IFELSE} (@pxref{Runtime}), making sure to first set the
6689 current language to Fortran 77 or Fortran via @code{AC_LANG([Fortran 77])}
6690 or @code{AC_LANG(Fortran)} (@pxref{Language Choice}).
6693 @defmac AC_F77_LIBRARY_LDFLAGS
6694 @defmacx AC_FC_LIBRARY_LDFLAGS
6695 @acindex{F77_LIBRARY_LDFLAGS}
6696 @ovindex FLIBS
6697 @acindex{FC_LIBRARY_LDFLAGS}
6698 @ovindex FCLIBS
6699 Determine the linker flags (e.g., @option{-L} and @option{-l}) for the
6700 @dfn{Fortran intrinsic and runtime libraries} that are required to
6701 successfully link a Fortran program or shared library.  The output
6702 variable @code{FLIBS} or @code{FCLIBS} is set to these flags (which
6703 should be included after @code{LIBS} when linking).
6705 This macro is intended to be used in those situations when it is
6706 necessary to mix, e.g., C++ and Fortran source code in a single
6707 program or shared library (@pxref{Mixing Fortran 77 With C and C++, , ,
6708 automake, @acronym{GNU} Automake}).
6710 For example, if object files from a C++ and Fortran compiler must be
6711 linked together, then the C++ compiler/linker must be used for linking
6712 (since special C++-ish things need to happen at link time like calling
6713 global constructors, instantiating templates, enabling exception
6714 support, etc.).
6716 However, the Fortran intrinsic and runtime libraries must be linked in
6717 as well, but the C++ compiler/linker doesn't know by default how to add
6718 these Fortran 77 libraries.  Hence, this macro was created to determine
6719 these Fortran libraries.
6721 The macros @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
6722 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} will probably also be necessary to
6723 link C/C++ with Fortran; see below.
6724 @end defmac
6726 @defmac AC_F77_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
6727 @defmacx AC_FC_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
6728 @acindex{F77_DUMMY_MAIN}
6729 @cvindex F77_DUMMY_MAIN
6730 With many compilers, the Fortran libraries detected by
6731 @code{AC_F77_LIBRARY_LDFLAGS} or @code{AC_FC_LIBRARY_LDFLAGS} provide
6732 their own @code{main} entry function that initializes things like
6733 Fortran I/O, and which then calls a user-provided entry function named
6734 (say) @code{MAIN__} to run the user's program.  The
6735 @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
6736 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros figure out how to deal with
6737 this interaction.
6739 When using Fortran for purely numerical functions (no I/O, etc.)@: often
6740 one prefers to provide one's own @code{main} and skip the Fortran
6741 library initializations.  In this case, however, one may still need to
6742 provide a dummy @code{MAIN__} routine in order to prevent linking errors
6743 on some systems.  @code{AC_F77_DUMMY_MAIN} or @code{AC_FC_DUMMY_MAIN}
6744 detects whether any such routine is @emph{required} for linking, and
6745 what its name is; the shell variable @code{F77_DUMMY_MAIN} or
6746 @code{FC_DUMMY_MAIN} holds this name, @code{unknown} when no solution
6747 was found, and @code{none} when no such dummy main is needed.
6749 By default, @var{action-if-found} defines @code{F77_DUMMY_MAIN} or
6750 @code{FC_DUMMY_MAIN} to the name of this routine (e.g., @code{MAIN__})
6751 @emph{if} it is required.  @var{action-if-not-found} defaults to
6752 exiting with an error.
6754 In order to link with Fortran routines, the user's C/C++ program should
6755 then include the following code to define the dummy main if it is
6756 needed:
6758 @example
6759 #ifdef F77_DUMMY_MAIN
6760 #  ifdef __cplusplus
6761      extern "C"
6762 #  endif
6763    int F77_DUMMY_MAIN() @{ return 1; @}
6764 #endif
6765 @end example
6767 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
6769 Note that this macro is called automatically from @code{AC_F77_WRAPPERS}
6770 or @code{AC_FC_WRAPPERS}; there is generally no need to call it
6771 explicitly unless one wants to change the default actions.
6772 @end defmac
6774 @defmac AC_F77_MAIN
6775 @defmacx AC_FC_MAIN
6776 @acindex{F77_MAIN}
6777 @cvindex F77_MAIN
6778 @acindex{FC_MAIN}
6779 @cvindex FC_MAIN
6780 As discussed above, many Fortran libraries allow you to provide an entry
6781 point called (say) @code{MAIN__} instead of the usual @code{main}, which
6782 is then called by a @code{main} function in the Fortran libraries that
6783 initializes things like Fortran I/O@.  The
6784 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros detect whether it is
6785 @emph{possible} to utilize such an alternate main function, and defines
6786 @code{F77_MAIN} and @code{FC_MAIN} to the name of the function.  (If no
6787 alternate main function name is found, @code{F77_MAIN} and @code{FC_MAIN} are
6788 simply defined to @code{main}.)
6790 Thus, when calling Fortran routines from C that perform things like I/O,
6791 one should use this macro and name the "main" function
6792 @code{F77_MAIN} or @code{FC_MAIN} instead of @code{main}.
6793 @end defmac
6795 @defmac AC_F77_WRAPPERS
6796 @defmacx AC_FC_WRAPPERS
6797 @acindex{F77_WRAPPERS}
6798 @cvindex F77_FUNC
6799 @cvindex F77_FUNC_
6800 @acindex{FC_WRAPPERS}
6801 @cvindex FC_FUNC
6802 @cvindex FC_FUNC_
6803 Defines C macros @code{F77_FUNC (name, NAME)}, @code{FC_FUNC (name, NAME)},
6804 @code{F77_FUNC_(name, NAME)}, and @code{FC_FUNC_(name, NAME)} to properly
6805 mangle the names of C/C++ identifiers, and identifiers with underscores,
6806 respectively, so that they match the name-mangling scheme used by the
6807 Fortran compiler.
6809 Fortran is case-insensitive, and in order to achieve this the Fortran
6810 compiler converts all identifiers into a canonical case and format.  To
6811 call a Fortran subroutine from C or to write a C function that is
6812 callable from Fortran, the C program must explicitly use identifiers in
6813 the format expected by the Fortran compiler.  In order to do this, one
6814 simply wraps all C identifiers in one of the macros provided by
6815 @code{AC_F77_WRAPPERS} or @code{AC_FC_WRAPPERS}.  For example, suppose
6816 you have the following Fortran 77 subroutine:
6818 @example
6819       subroutine foobar (x, y)
6820       double precision x, y
6821       y = 3.14159 * x
6822       return
6823       end
6824 @end example
6826 You would then declare its prototype in C or C++ as:
6828 @example
6829 #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
6830 #ifdef __cplusplus
6831 extern "C"  /* prevent C++ name mangling */
6832 #endif
6833 void FOOBAR_F77(double *x, double *y);
6834 @end example
6836 Note that we pass both the lowercase and uppercase versions of the
6837 function name to @code{F77_FUNC} so that it can select the right one.
6838 Note also that all parameters to Fortran 77 routines are passed as
6839 pointers (@pxref{Mixing Fortran 77 With C and C++, , , automake, @acronym{GNU}
6840 Automake}).
6842 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
6844 Although Autoconf tries to be intelligent about detecting the
6845 name-mangling scheme of the Fortran compiler, there may be Fortran
6846 compilers that it doesn't support yet.  In this case, the above code
6847 will generate a compile-time error, but some other behavior
6848 (e.g., disabling Fortran-related features) can be induced by checking
6849 whether @code{F77_FUNC} or @code{FC_FUNC} is defined.
6851 Now, to call that routine from a C program, we would do something like:
6853 @example
6855     double x = 2.7183, y;
6856     FOOBAR_F77 (&x, &y);
6858 @end example
6860 If the Fortran identifier contains an underscore (e.g., @code{foo_bar}),
6861 you should use @code{F77_FUNC_} or @code{FC_FUNC_} instead of
6862 @code{F77_FUNC} or @code{FC_FUNC} (with the same arguments).  This is
6863 because some Fortran compilers mangle names differently if they contain
6864 an underscore.
6865 @end defmac
6867 @defmac AC_F77_FUNC (@var{name}, @ovar{shellvar})
6868 @defmacx AC_FC_FUNC (@var{name}, @ovar{shellvar})
6869 @acindex{F77_FUNC}
6870 @acindex{FC_FUNC}
6871 Given an identifier @var{name}, set the shell variable @var{shellvar} to
6872 hold the mangled version @var{name} according to the rules of the
6873 Fortran linker (see also @code{AC_F77_WRAPPERS} or
6874 @code{AC_FC_WRAPPERS}).  @var{shellvar} is optional; if it is not
6875 supplied, the shell variable will be simply @var{name}.  The purpose of
6876 this macro is to give the caller a way to access the name-mangling
6877 information other than through the C preprocessor as above, for example,
6878 to call Fortran routines from some language other than C/C++.
6879 @end defmac
6881 @defmac AC_FC_SRCEXT (@var{ext}, @ovar{action-if-success}, @ovar{action-if-failure})
6882 @acindex{FC_SRCEXT}
6883 By default, the @code{FC} macros perform their tests using a @file{.f}
6884 extension for source-code files.  Some compilers, however, only enable
6885 newer language features for appropriately named files, e.g., Fortran 90
6886 features only for @file{.f90} files.  On the other hand, some other
6887 compilers expect all source files to end in @file{.f} and require
6888 special flags to support other file name extensions.  The
6889 @code{AC_FC_SRCEXT} macro deals with both of these issues.
6891 The @code{AC_FC_SRCEXT} tries to get the @code{FC} compiler to accept files
6892 ending with the extension .@var{ext} (i.e., @var{ext} does @emph{not}
6893 contain the dot).  If any special compiler flags are needed for this, it
6894 stores them in the output variable @code{FCFLAGS_}@var{ext}.  This
6895 extension and these flags are then used for all subsequent @code{FC} tests
6896 (until @code{AC_FC_SRCEXT} is called again).
6898 For example, you would use @code{AC_FC_SRCEXT(f90)} to employ the
6899 @file{.f90} extension in future tests, and it would set a
6900 @code{FCFLAGS_f90} output variable with any extra flags that are needed
6901 to compile such files.
6903 The @code{FCFLAGS_}@var{ext} can @emph{not} be simply absorbed into
6904 @code{FCFLAGS}, for two reasons based on the limitations of some
6905 compilers.  First, only one @code{FCFLAGS_}@var{ext} can be used at a
6906 time, so files with different extensions must be compiled separately.
6907 Second, @code{FCFLAGS_}@var{ext} must appear @emph{immediately} before
6908 the source-code file name when compiling.  So, continuing the example
6909 above, you might compile a @file{foo.f90} file in your Makefile with the
6910 command:
6912 @example
6913 foo.o: foo.f90
6914      $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) foo.f90
6915 @end example
6917 If @code{AC_FC_SRCEXT} succeeds in compiling files with the @var{ext}
6918 extension, it calls @var{action-if-success} (defaults to nothing).  If
6919 it fails, and cannot find a way to make the @code{FC} compiler accept such
6920 files, it calls @var{action-if-failure} (defaults to exiting with an
6921 error message).
6923 @end defmac
6925 @defmac AC_FC_FREEFORM (@ovar{action-if-success}, @ovar{action-if-failure})
6926 @acindex{FC_FREEFORM}
6928 The @code{AC_FC_FREEFORM} tries to ensure that the Fortran compiler
6929 (@code{$FC}) allows free-format source code (as opposed to the older
6930 fixed-format style from Fortran 77).  If necessary, it may add some
6931 additional flags to @code{FCFLAGS}.
6933 This macro is most important if you are using the default @file{.f}
6934 extension, since many compilers interpret this extension as indicating
6935 fixed-format source unless an additional flag is supplied.  If you
6936 specify a different extension with @code{AC_FC_SRCEXT}, such as
6937 @file{.f90} or @file{.f95}, then @code{AC_FC_FREEFORM} will ordinarily
6938 succeed without modifying @code{FCFLAGS}.
6940 If @code{AC_FC_FREEFORM} succeeds in compiling free-form source, it
6941 calls @var{action-if-success} (defaults to nothing).  If it fails, it
6942 calls @var{action-if-failure} (defaults to exiting with an error
6943 message).
6944 @end defmac
6946 @node System Services
6947 @section System Services
6949 The following macros check for operating system services or capabilities.
6951 @defmac AC_PATH_X
6952 @acindex{PATH_X}
6953 @evindex XMKMF
6954 @cindex X Window System
6955 Try to locate the X Window System include files and libraries.  If the
6956 user gave the command line options @option{--x-includes=@var{dir}} and
6957 @option{--x-libraries=@var{dir}}, use those directories.
6959 If either or both were not given, get the missing values by running
6960 @code{xmkmf} (or an executable pointed to by the @code{XMKMF}
6961 environment variable) on a trivial @file{Imakefile} and examining the
6962 @file{Makefile} that it produces.  Setting @code{XMKMF} to @samp{false}
6963 will disable this method.
6965 If this method fails to find the X Window System, @command{configure}
6966 will look for the files in several directories where they often reside.
6967 If either method is successful, set the shell variables
6968 @code{x_includes} and @code{x_libraries} to their locations, unless they
6969 are in directories the compiler searches by default.
6971 If both methods fail, or the user gave the command line option
6972 @option{--without-x}, set the shell variable @code{no_x} to @samp{yes};
6973 otherwise set it to the empty string.
6974 @end defmac
6976 @defmac AC_PATH_XTRA
6977 @acindex{PATH_XTRA}
6978 @ovindex X_CFLAGS
6979 @ovindex X_LIBS
6980 @ovindex X_EXTRA_LIBS
6981 @ovindex X_PRE_LIBS
6982 @cvindex X_DISPLAY_MISSING
6983 An enhanced version of @code{AC_PATH_X}.  It adds the C compiler flags
6984 that X needs to output variable @code{X_CFLAGS}, and the X linker flags
6985 to @code{X_LIBS}.  Define @code{X_DISPLAY_MISSING} if X is not
6986 available.
6988 This macro also checks for special libraries that some systems need in
6989 order to compile X programs.  It adds any that the system needs to
6990 output variable @code{X_EXTRA_LIBS}.  And it checks for special X11R6
6991 libraries that need to be linked with before @option{-lX11}, and adds
6992 any found to the output variable @code{X_PRE_LIBS}.
6994 @c This is an incomplete kludge.  Make a real way to do it.
6995 @c If you need to check for other X functions or libraries yourself, then
6996 @c after calling this macro, add the contents of @code{X_EXTRA_LIBS} to
6997 @c @code{LIBS} temporarily, like this: (FIXME - add example)
6998 @end defmac
7000 @defmac AC_SYS_INTERPRETER
7001 @acindex{SYS_INTERPRETER}
7002 Check whether the system supports starting scripts with a line of the
7003 form @samp{#!/bin/sh} to select the interpreter to use for the script.
7004 After running this macro, shell code in @file{configure.ac} can check
7005 the shell variable @code{interpval}; it will be set to @samp{yes}
7006 if the system supports @samp{#!}, @samp{no} if not.
7007 @end defmac
7009 @defmac AC_SYS_LARGEFILE
7010 @acindex{SYS_LARGEFILE}
7011 @cvindex _FILE_OFFSET_BITS
7012 @cvindex _LARGE_FILES
7013 @ovindex CC
7014 @cindex Large file support
7015 @cindex LFS
7016 Arrange for
7017 @uref{http://www.unix-systems.org/@/version2/@/whatsnew/@/lfs20mar.html,
7018 large-file support}.  On some hosts, one must use special compiler
7019 options to build programs that can access large files.  Append any such
7020 options to the output variable @code{CC}.  Define
7021 @code{_FILE_OFFSET_BITS} and @code{_LARGE_FILES} if necessary.
7023 Large-file support can be disabled by configuring with the
7024 @option{--disable-largefile} option.
7026 If you use this macro, check that your program works even when
7027 @code{off_t} is wider than @code{long int}, since this is common when
7028 large-file support is enabled.  For example, it is not correct to print
7029 an arbitrary @code{off_t} value @code{X} with @code{printf ("%ld",
7030 (long int) X)}.
7032 The LFS introduced the @code{fseeko} and @code{ftello} functions to
7033 replace their C counterparts @code{fseek} and @code{ftell} that do not
7034 use @code{off_t}.  Take care to use @code{AC_FUNC_FSEEKO} to make their
7035 prototypes available when using them and large-file support is
7036 enabled.
7037 @end defmac
7039 @defmac AC_SYS_LONG_FILE_NAMES
7040 @acindex{SYS_LONG_FILE_NAMES}
7041 @cvindex HAVE_LONG_FILE_NAMES
7042 If the system supports file names longer than 14 characters, define
7043 @code{HAVE_LONG_FILE_NAMES}.
7044 @end defmac
7046 @defmac AC_SYS_POSIX_TERMIOS
7047 @acindex{SYS_POSIX_TERMIOS}
7048 @cindex Posix termios headers
7049 @cindex termios Posix headers
7050 Check to see if the Posix termios headers and functions are available on the
7051 system.  If so, set the shell variable @code{ac_cv_sys_posix_termios} to
7052 @samp{yes}.  If not, set the variable to @samp{no}.
7053 @end defmac
7055 @node Posix Variants
7056 @section Posix Variants
7058 The following macros check for certain operating systems that need
7059 special treatment for some programs, due to exceptional oddities in
7060 their header files or libraries.  These macros are warts; they will be
7061 replaced by a more systematic approach, based on the functions they make
7062 available or the environments they provide.
7064 @defmac AC_AIX
7065 @acindex{AIX}
7066 @cvindex _ALL_SOURCE
7067 If on @acronym{AIX}, define @code{_ALL_SOURCE}.
7068 Allows the use of some @acronym{BSD}
7069 functions.  Should be called before any macros that run the C compiler.
7070 @end defmac
7072 @defmac AC_GNU_SOURCE
7073 @acindex{GNU_SOURCE}
7074 @cvindex _GNU_SOURCE
7075 If using the @acronym{GNU} C library, define @code{_GNU_SOURCE}.
7076 Allows the use of some @acronym{GNU} functions.  Should be called
7077 before any macros that run the C compiler.
7078 @end defmac
7080 @defmac AC_ISC_POSIX
7081 @acindex{ISC_POSIX}
7082 @ovindex LIBS
7083 For @sc{interactive} Systems Corporation Unix, add @option{-lcposix} to output
7084 variable @code{LIBS} if necessary for Posix facilities.  Call this
7085 after @code{AC_PROG_CC} and before any other macros that use Posix
7086 interfaces.  @sc{interactive} Unix is no longer sold, and Sun says that
7087 they will drop support for it on 2006-07-23, so this macro is becoming
7088 obsolescent.
7089 @end defmac
7091 @defmac AC_MINIX
7092 @acindex{MINIX}
7093 @cvindex _MINIX
7094 @cvindex _POSIX_SOURCE
7095 @cvindex _POSIX_1_SOURCE
7096 If on Minix, define @code{_MINIX} and @code{_POSIX_SOURCE} and define
7097 @code{_POSIX_1_SOURCE} to be 2.  This allows the use of Posix
7098 facilities.  Should be called before any macros that run the C compiler.
7099 @end defmac
7101 @defmac AC_USE_SYSTEM_EXTENSIONS
7102 @acindex{USE_SYSTEM_EXTENSIONS}
7103 @cvindex _ALL_SOURCE
7104 @cvindex _GNU_SOURCE
7105 @cvindex _MINIX
7106 @cvindex _POSIX_1_SOURCE
7107 @cvindex _POSIX_PTHREAD_SEMANTICS
7108 @cvindex _POSIX_SOURCE
7109 @cvindex __EXTENSIONS__
7110 If possible, enable extensions to Posix on hosts that normally disable
7111 the extensions, typically due to standards-conformance namespace issues.
7112 This may involve defining @code{__EXTENSIONS__} and
7113 @code{_POSIX_PTHREAD_SEMANTICS}, which are macros used by Solaris.  This
7114 macro also has the combined effects of @code{AC_GNU_SOURCE},
7115 @code{AC_AIX}, and @code{AC_MINIX}.
7116 @end defmac
7119 @node Erlang Libraries
7120 @section Erlang Libraries
7121 @cindex Erlang, Library, checking
7123 The following macros check for an installation of Erlang/OTP, and for the
7124 presence of certain Erlang libraries.  All those macros require the
7125 configuration of an Erlang interpreter and an Erlang compiler
7126 (@pxref{Erlang Compiler and Interpreter}).
7128 @defmac AC_ERLANG_SUBST_ROOT_DIR
7129 @acindex{ERLANG_SUBST_ROOT_DIR}
7130 @ovindex ERLANG_ROOT_DIR
7132 Set the output variable @code{ERLANG_ROOT_DIR} to the path to the base directory
7133 in which Erlang/OTP is installed (as returned by Erlang's @code{code:root_dir/0}
7134 function).  The result of this test is cached if caching is enabled when running
7135 @command{configure}.
7136 @end defmac
7138 @defmac AC_ERLANG_SUBST_LIB_DIR
7139 @acindex{ERLANG_SUBST_LIB_DIR}
7140 @ovindex ERLANG_LIB_DIR
7142 Set the output variable @code{ERLANG_LIB_DIR} to the path of the library
7143 directory of Erlang/OTP (as returned by Erlang's
7144 @code{code:lib_dir/0} function), which subdirectories each contain an installed
7145 Erlang/OTP library.  The result of this test is cached if caching is enabled
7146 when running @command{configure}.
7147 @end defmac
7149 @defmac AC_ERLANG_CHECK_LIB (@var{library}, @ovar{action-if-found}, @ovar{action-if-not-found})
7150 @acindex{ERLANG_CHECK_LIB}
7151 @ovindex ERLANG_LIB_DIR_@var{library}
7153 Test whether the Erlang/OTP library @var{library} is installed by calling
7154 Erlang's @code{code:lib_dir/1} function.  The result of this test is cached if
7155 caching is enabled when running @command{configure}.  @var{action-if-found} is a
7156 list of shell commands to run if the library is installed;
7157 @var{action-if-not-found} is a list of shell commands to run if it is not.
7158 Additionally, if the library is installed, the output variable
7159 @samp{ERLANG_LIB_DIR_@var{library}} is set to the path to the library
7160 installation directory.  For example, to check if library @code{stdlib} is
7161 installed:
7163 @example
7164 AC_ERLANG_CHECK_LIB([stdlib],
7165   [echo "stdlib is installed in $ERLANG_LIB_DIR_stdlib"],
7166   [AC_MSG_ERROR([stdlib was not found!])])
7167 @end example
7168 @end defmac
7170 In addition to the above macros, which test installed Erlang libraries, the
7171 following macros determine the paths to the directories into which newly built
7172 Erlang libraries are to be installed:
7174 @defmac AC_ERLANG_SUBST_INSTALL_LIB_DIR
7175 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
7176 @ovindex ERLANG_INSTALL_LIB_DIR
7178 Set the @code{ERLANG_INSTALL_LIB_DIR} output variable to the directory into
7179 which every built Erlang library should be installed in a separate subdirectory.
7180 If this variable is not set in the environment when @command{configure} runs,
7181 its default value is @code{$ERLANG_LIB_DIR}, which value is set by the
7182 @code{AC_ERLANG_SUBST_LIB_DIR} macro.
7183 @end defmac
7185 @defmac AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR (@var{library}, @var{version})
7186 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
7187 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
7189 Set the @samp{ERLANG_INSTALL_LIB_DIR_@var{library}} output variable to the
7190 directory into which the built Erlang library @var{library} version
7191 @var{version} should be installed.  If this variable is not set in the
7192 environment when @command{configure} runs, its default value is
7193 @samp{$ERLANG_INSTALL_LIB_DIR/@var{library}-@var{version}}, the value of the
7194 @code{ERLANG_INSTALL_LIB_DIR} variable being set by the
7195 @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR} macro.
7196 @end defmac
7202 @c ========================================================= Writing Tests
7204 @node Writing Tests
7205 @chapter Writing Tests
7207 If the existing feature tests don't do something you need, you have to
7208 write new ones.  These macros are the building blocks.  They provide
7209 ways for other macros to check whether various kinds of features are
7210 available and report the results.
7212 This chapter contains some suggestions and some of the reasons why the
7213 existing tests are written the way they are.  You can also learn a lot
7214 about how to write Autoconf tests by looking at the existing ones.  If
7215 something goes wrong in one or more of the Autoconf tests, this
7216 information can help you understand the assumptions behind them, which
7217 might help you figure out how to best solve the problem.
7219 These macros check the output of the compiler system of the current
7220 language (@pxref{Language Choice}).  They do not cache the results of
7221 their tests for future use (@pxref{Caching Results}), because they don't
7222 know enough about the information they are checking for to generate a
7223 cache variable name.  They also do not print any messages, for the same
7224 reason.  The checks for particular kinds of features call these macros
7225 and do cache their results and print messages about what they're
7226 checking for.
7228 When you write a feature test that could be applicable to more than one
7229 software package, the best thing to do is encapsulate it in a new macro.
7230 @xref{Writing Autoconf Macros}, for how to do that.
7232 @menu
7233 * Language Choice::             Selecting which language to use for testing
7234 * Writing Test Programs::       Forging source files for compilers
7235 * Running the Preprocessor::    Detecting preprocessor symbols
7236 * Running the Compiler::        Detecting language or header features
7237 * Running the Linker::          Detecting library features
7238 * Runtime::                     Testing for runtime features
7239 * Systemology::                 A zoology of operating systems
7240 * Multiple Cases::              Tests for several possible values
7241 @end menu
7243 @node Language Choice
7244 @section Language Choice
7245 @cindex Language
7247 Autoconf-generated @command{configure} scripts check for the C compiler and
7248 its features by default.  Packages that use other programming languages
7249 (maybe more than one, e.g., C and C++) need to test features of the
7250 compilers for the respective languages.  The following macros determine
7251 which programming language is used in the subsequent tests in
7252 @file{configure.ac}.
7254 @defmac AC_LANG (@var{language})
7255 Do compilation tests using the compiler, preprocessor, and file
7256 extensions for the specified @var{language}.
7258 Supported languages are:
7260 @table @samp
7261 @item C
7262 Do compilation tests using @code{CC} and @code{CPP} and use extension
7263 @file{.c} for test programs.  Use compilation flags: @code{CPPFLAGS} with
7264 @code{CPP}, and both @code{CPPFLAGS} and @code{CFLAGS} with @code{CC}.
7266 @item C++
7267 Do compilation tests using @code{CXX} and @code{CXXCPP} and use
7268 extension @file{.C} for test programs.  Use compilation flags:
7269 @code{CPPFLAGS} with @code{CXXPP}, and both @code{CPPFLAGS} and
7270 @code{CXXFLAGS} with @code{CXX}.
7272 @item Fortran 77
7273 Do compilation tests using @code{F77} and use extension @file{.f} for
7274 test programs.  Use compilation flags: @code{FFLAGS}.
7276 @item Fortran
7277 Do compilation tests using @code{FC} and use extension @file{.f} (or
7278 whatever has been set by @code{AC_FC_SRCEXT}) for test programs.  Use
7279 compilation flags: @code{FCFLAGS}.
7281 @item Erlang
7282 @ovindex ERLC
7283 @ovindex ERL
7284 @ovindex ERLCFLAGS
7285 Compile and execute tests using @code{ERLC} and @code{ERL} and use extension
7286 @file{.erl} for test Erlang modules.  Use compilation flags: @code{ERLCFLAGS}.
7288 @item Objective C
7289 Do compilation tests using @code{OBJC} and @code{OBJCCPP} and use
7290 extension @file{.m} for test programs.  Use compilation flags:
7291 @code{CPPFLAGS} with @code{OBJCPP}, and both @code{CPPFLAGS} and
7292 @code{OBJCFLAGS} with @code{OBJC}.
7293 @end table
7294 @end defmac
7296 @defmac AC_LANG_PUSH (@var{language})
7297 @acindex{LANG_PUSH}
7298 Remember the current language (as set by @code{AC_LANG}) on a stack, and
7299 then select the @var{language}.  Use this macro and @code{AC_LANG_POP}
7300 in macros that need to temporarily switch to a particular language.
7301 @end defmac
7303 @defmac AC_LANG_POP (@ovar{language})
7304 @acindex{LANG_POP}
7305 Select the language that is saved on the top of the stack, as set by
7306 @code{AC_LANG_PUSH}, and remove it from the stack.
7308 If given, @var{language} specifies the language we just @emph{quit}.  It
7309 is a good idea to specify it when it's known (which should be the
7310 case@dots{}), since Autoconf will detect inconsistencies.
7312 @example
7313 AC_LANG_PUSH([Fortran 77])
7314 # Perform some tests on Fortran 77.
7315 # @dots{}
7316 AC_LANG_POP([Fortran 77])
7317 @end example
7318 @end defmac
7320 @defmac AC_LANG_ASSERT (@var{language})
7321 @acindex{LANG_ASSERT} Check statically that the current language is
7322 @var{language}.  You should use this in your language specific macros
7323 to avoid that they be called with an inappropriate language.
7325 This macro runs only at @command{autoconf} time, and incurs no cost at
7326 @command{configure} time.  Sadly enough and because Autoconf is a two
7327 layer language @footnote{Because M4 is not aware of Sh code,
7328 especially conditionals, some optimizations that look nice statically
7329 may produce incorrect results at runtime.}, the macros
7330 @code{AC_LANG_PUSH} and @code{AC_LANG_POP} cannot be ``optimizing'',
7331 therefore as much as possible you ought to avoid using them to wrap
7332 your code, rather, require from the user to run the macro with a
7333 correct current language, and check it with @code{AC_LANG_ASSERT}.
7334 And anyway, that may help the user understand she is running a Fortran
7335 macro while expecting a result about her Fortran 77 compiler@dots{}
7336 @end defmac
7339 @defmac AC_REQUIRE_CPP
7340 @acindex{REQUIRE_CPP}
7341 Ensure that whichever preprocessor would currently be used for tests has
7342 been found.  Calls @code{AC_REQUIRE} (@pxref{Prerequisite Macros}) with an
7343 argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
7344 depending on which language is current.
7345 @end defmac
7348 @node Writing Test Programs
7349 @section Writing Test Programs
7351 Autoconf tests follow a common scheme: feed some program with some
7352 input, and most of the time, feed a compiler with some source file.
7353 This section is dedicated to these source samples.
7355 @menu
7356 * Guidelines::                  General rules for writing test programs
7357 * Test Functions::              Avoiding pitfalls in test programs
7358 * Generating Sources::          Source program boilerplate
7359 @end menu
7361 @node Guidelines
7362 @subsection Guidelines for Test Programs
7364 The most important rule to follow when writing testing samples is:
7366 @center @emph{Look for realism.}
7368 This motto means that testing samples must be written with the same
7369 strictness as real programs are written.  In particular, you should
7370 avoid ``shortcuts'' and simplifications.
7372 Don't just play with the preprocessor if you want to prepare a
7373 compilation.  For instance, using @command{cpp} to check whether a header is
7374 functional might let your @command{configure} accept a header which will
7375 cause some @emph{compiler} error.  Do not hesitate checking header with
7376 other headers included before, especially required headers.
7378 Make sure the symbols you use are properly defined, i.e., refrain for
7379 simply declaring a function yourself instead of including the proper
7380 header.
7382 Test programs should not write to standard output.  They
7383 should exit with status 0 if the test succeeds, and with status 1
7384 otherwise, so that success
7385 can be distinguished easily from a core dump or other failure;
7386 segmentation violations and other failures produce a nonzero exit
7387 status.  Unless you arrange for @code{exit} to be declared, test
7388 programs should @code{return}, not @code{exit}, from @code{main},
7389 because on many systems @code{exit} is not declared by default.
7391 Test programs can use @code{#if} or @code{#ifdef} to check the values of
7392 preprocessor macros defined by tests that have already run.  For
7393 example, if you call @code{AC_HEADER_STDBOOL}, then later on in
7394 @file{configure.ac} you can have a test program that includes
7395 @file{stdbool.h} conditionally:
7397 @example
7398 @group
7399 #if HAVE_STDBOOL_H
7400 # include <stdbool.h>
7401 #endif
7402 @end group
7403 @end example
7405 If a test program needs to use or create a data file, give it a name
7406 that starts with @file{conftest}, such as @file{conftest.data}.  The
7407 @command{configure} script cleans up by running @samp{rm -f -r conftest*}
7408 after running test programs and if the script is interrupted.
7410 @node Test Functions
7411 @subsection Test Functions
7413 These days it's safe to assume support for function prototypes
7414 (introduced in C89).
7416 Functions that test programs declare should also be conditionalized for
7417 C++, which requires @samp{extern "C"} prototypes.  Make sure to not
7418 include any header files containing clashing prototypes.
7420 @example
7421 #ifdef __cplusplus
7422 extern "C"
7423 #endif
7424 void *valloc (size_t);
7425 @end example
7427 If a test program calls a function with invalid parameters (just to see
7428 whether it exists), organize the program to ensure that it never invokes
7429 that function.  You can do this by calling it in another function that is
7430 never invoked.  You can't do it by putting it after a call to
7431 @code{exit}, because GCC version 2 knows that @code{exit} never returns
7432 and optimizes out any code that follows it in the same block.
7434 If you include any header files, be sure to call the functions
7435 relevant to them with the correct number of arguments, even if they are
7436 just 0, to avoid compilation errors due to prototypes.  GCC version 2
7437 has internal prototypes for several functions that it automatically
7438 inlines; for example, @code{memcpy}.  To avoid errors when checking for
7439 them, either pass them the correct number of arguments or redeclare them
7440 with a different return type (such as @code{char}).
7443 @node Generating Sources
7444 @subsection Generating Sources
7446 Autoconf provides a set of macros that can be used to generate test
7447 source files.  They are written to be language generic, i.e., they
7448 actually depend on the current language (@pxref{Language Choice}) to
7449 ``format'' the output properly.
7452 @defmac AC_LANG_CONFTEST (@var{source})
7453 @acindex{LANG_CONFTEST}
7454 Save the @var{source} text in the current test source file:
7455 @file{conftest.@var{extension}} where the @var{extension} depends on the
7456 current language.
7458 Note that the @var{source} is evaluated exactly once, like regular
7459 Autoconf macro arguments, and therefore (i) you may pass a macro
7460 invocation, (ii) if not, be sure to double quote if needed.
7461 @end defmac
7463 @defmac AC_LANG_SOURCE (@var{source})
7464 @acindex{LANG_SOURCE}
7465 Expands into the @var{source}, with the definition of
7466 all the @code{AC_DEFINE} performed so far.
7467 @end defmac
7469 For instance executing (observe the double quotation!):
7471 @example
7472 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7473 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7474   [Greetings string.])
7475 AC_LANG(C)
7476 AC_LANG_CONFTEST(
7477    [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
7478 gcc -E -dD -o - conftest.c
7479 @end example
7481 @noindent
7482 results in:
7484 @example
7485 @dots{}
7486 # 1 "conftest.c"
7488 #define PACKAGE_NAME "Hello"
7489 #define PACKAGE_TARNAME "hello"
7490 #define PACKAGE_VERSION "1.0"
7491 #define PACKAGE_STRING "Hello 1.0"
7492 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7493 #define HELLO_WORLD "Hello, World\n"
7495 const char hw[] = "Hello, World\n";
7496 @end example
7498 When the test language is Fortran or Erlang, the @code{AC_DEFINE} definitions
7499 are not automatically translated into constants in the source code by this
7500 macro.
7502 @defmac AC_LANG_PROGRAM (@var{prologue}, @var{body})
7503 @acindex{LANG_PROGRAM}
7504 Expands into a source file which consists of the @var{prologue}, and
7505 then @var{body} as body of the main function (e.g., @code{main} in
7506 C).  Since it uses @code{AC_LANG_SOURCE}, the features of the latter are
7507 available.
7508 @end defmac
7510 For instance:
7512 @example
7513 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7514 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7515   [Greetings string.])
7516 AC_LANG_CONFTEST(
7517 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
7518                  [[fputs (hw, stdout);]])])
7519 gcc -E -dD -o - conftest.c
7520 @end example
7522 @noindent
7523 results in:
7525 @example
7526 @dots{}
7527 # 1 "conftest.c"
7529 #define PACKAGE_NAME "Hello"
7530 #define PACKAGE_TARNAME "hello"
7531 #define PACKAGE_VERSION "1.0"
7532 #define PACKAGE_STRING "Hello 1.0"
7533 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7534 #define HELLO_WORLD "Hello, World\n"
7536 const char hw[] = "Hello, World\n";
7538 main ()
7540 fputs (hw, stdout);
7541   ;
7542   return 0;
7544 @end example
7546 In Erlang tests, the created source file is that of an Erlang module called
7547 @code{conftest} (@file{conftest.erl}).  This module defines and exports at least
7548 one @code{start/0} function, which is called to perform the test.  The
7549 @var{prologue} is optional code that is inserted between the module header and
7550 the @code{start/0} function definition.  @var{body} is the body of the
7551 @code{start/0} function without the final period (@pxref{Runtime}, about
7552 constraints on this function's behaviour).
7554 For instance:
7556 @example
7557 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7558 AC_LANG(Erlang)
7559 AC_LANG_CONFTEST(
7560 [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
7561                  [[io:format("~s~n", [?HELLO_WORLD])]])])
7562 cat conftest.erl
7563 @end example
7565 @noindent
7566 results in:
7568 @example
7569 -module(conftest).
7570 -export([start/0]).
7571 -define(HELLO_WORLD, "Hello, world!").
7572 start() ->
7573 io:format("~s~n", [?HELLO_WORLD])
7575 @end example
7577 @defmac AC_LANG_CALL (@var{prologue}, @var{function})
7578 @acindex{LANG_CALL}
7579 Expands into a source file which consists of the @var{prologue}, and
7580 then a call to the @var{function} as body of the main function (e.g.,
7581 @code{main} in C).  Since it uses @code{AC_LANG_PROGRAM}, the feature
7582 of the latter are available.
7584 This function will probably be replaced in the future by a version
7585 which would enable specifying the arguments.  The use of this macro is
7586 not encouraged, as it violates strongly the typing system.
7588 This macro cannot be used for Erlang tests.
7589 @end defmac
7591 @defmac AC_LANG_FUNC_LINK_TRY (@var{function})
7592 @acindex{LANG_FUNC_LINK_TRY}
7593 Expands into a source file which uses the @var{function} in the body of
7594 the main function (e.g., @code{main} in C).  Since it uses
7595 @code{AC_LANG_PROGRAM}, the features of the latter are available.
7597 As @code{AC_LANG_CALL}, this macro is documented only for completeness.
7598 It is considered to be severely broken, and in the future will be
7599 removed in favor of actual function calls (with properly typed
7600 arguments).
7602 This macro cannot be used for Erlang tests.
7603 @end defmac
7605 @node Running the Preprocessor
7606 @section Running the Preprocessor
7608 Sometimes one might need to run the preprocessor on some source file.
7609 @emph{Usually it is a bad idea}, as you typically need to @emph{compile}
7610 your project, not merely run the preprocessor on it; therefore you
7611 certainly want to run the compiler, not the preprocessor.  Resist the
7612 temptation of following the easiest path.
7614 Nevertheless, if you need to run the preprocessor, then use
7615 @code{AC_PREPROC_IFELSE}.
7617 The macros described in this section cannot be used for tests in Erlang or
7618 Fortran, since those languages require no preprocessor.
7620 @defmac AC_PREPROC_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
7621 @acindex{PREPROC_IFELSE}
7622 Run the preprocessor of the current language (@pxref{Language Choice})
7623 on the @var{input}, run the shell commands @var{action-if-true} on
7624 success, @var{action-if-false} otherwise.  The @var{input} can be made
7625 by @code{AC_LANG_PROGRAM} and friends.
7627 This macro uses @code{CPPFLAGS}, but not @code{CFLAGS}, because
7628 @option{-g}, @option{-O}, etc.@: are not valid options to many C
7629 preprocessors.
7631 It is customary to report unexpected failures with
7632 @code{AC_MSG_FAILURE}.
7633 @end defmac
7635 For instance:
7637 @example
7638 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7639 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7640   [Greetings string.])
7641 AC_PREPROC_IFELSE(
7642    [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
7643                     [[fputs (hw, stdout);]])],
7644    [AC_MSG_RESULT([OK])],
7645    [AC_MSG_FAILURE([unexpected preprocessor failure])])
7646 @end example
7648 @noindent
7649 results in:
7651 @example
7652 checking for gcc... gcc
7653 checking for C compiler default output file name... a.out
7654 checking whether the C compiler works... yes
7655 checking whether we are cross compiling... no
7656 checking for suffix of executables...
7657 checking for suffix of object files... o
7658 checking whether we are using the GNU C compiler... yes
7659 checking whether gcc accepts -g... yes
7660 checking for gcc option to accept ISO C89... none needed
7661 checking how to run the C preprocessor... gcc -E
7663 @end example
7665 @sp 1
7667 The macro @code{AC_TRY_CPP} (@pxref{Obsolete Macros}) used to play the
7668 role of @code{AC_PREPROC_IFELSE}, but double quotes its argument, making
7669 it impossible to use it to elaborate sources.  You are encouraged to
7670 get rid of your old use of the macro @code{AC_TRY_CPP} in favor of
7671 @code{AC_PREPROC_IFELSE}, but, in the first place, are you sure you need
7672 to run the @emph{preprocessor} and not the compiler?
7674 @defmac AC_EGREP_HEADER (@var{pattern}, @var{header-file}, @var{action-if-found}, @ovar{action-if-not-found})
7675 @acindex{EGREP_HEADER}
7676 If the output of running the preprocessor on the system header file
7677 @var{header-file} matches the extended regular expression
7678 @var{pattern}, execute shell commands @var{action-if-found}, otherwise
7679 execute @var{action-if-not-found}.
7680 @end defmac
7682 @defmac AC_EGREP_CPP (@var{pattern}, @var{program}, @ovar{action-if-found}, @ovar{action-if-not-found})
7683 @acindex{EGREP_CPP}
7684 @var{program} is the text of a C or C++ program, on which shell
7685 variable, back quote, and backslash substitutions are performed.  If the
7686 output of running the preprocessor on @var{program} matches the
7687 extended regular expression @var{pattern}, execute shell commands
7688 @var{action-if-found}, otherwise execute @var{action-if-not-found}.
7689 @end defmac
7693 @node Running the Compiler
7694 @section Running the Compiler
7696 To check for a syntax feature of the current language's (@pxref{Language
7697 Choice}) compiler, such as whether it recognizes a certain keyword, or
7698 simply to try some library feature, use @code{AC_COMPILE_IFELSE} to try
7699 to compile a small program that uses that feature.
7701 @defmac AC_COMPILE_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
7702 @acindex{COMPILE_IFELSE}
7703 Run the compiler and compilation flags of the current language
7704 (@pxref{Language Choice}) on the @var{input}, run the shell commands
7705 @var{action-if-true} on success, @var{action-if-false} otherwise.  The
7706 @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
7708 It is customary to report unexpected failures with
7709 @code{AC_MSG_FAILURE}.  This macro does not try to link; use
7710 @code{AC_LINK_IFELSE} if you need to do that (@pxref{Running the
7711 Linker}).
7712 @end defmac
7714 @ovindex ERL
7715 For tests in Erlang, the @var{input} must be the source code of a module named
7716 @code{conftest}.  @code{AC_COMPILE_IFELSE} generates a @file{conftest.beam}
7717 file that can be interpreted by the Erlang virtual machine (@code{ERL}).  It is
7718 recommended to use @code{AC_LANG_PROGRAM} to specify the test program, to ensure
7719 that the Erlang module has the right name.
7721 @node Running the Linker
7722 @section Running the Linker
7724 To check for a library, a function, or a global variable, Autoconf
7725 @command{configure} scripts try to compile and link a small program that
7726 uses it.  This is unlike Metaconfig, which by default uses @code{nm} or
7727 @code{ar} on the C library to try to figure out which functions are
7728 available.  Trying to link with the function is usually a more reliable
7729 approach because it avoids dealing with the variations in the options
7730 and output formats of @code{nm} and @code{ar} and in the location of the
7731 standard libraries.  It also allows configuring for cross-compilation or
7732 checking a function's runtime behavior if needed.  On the other hand,
7733 it can be slower than scanning the libraries once, but accuracy is more
7734 important than speed.
7736 @code{AC_LINK_IFELSE} is used to compile test programs to test for
7737 functions and global variables.  It is also used by @code{AC_CHECK_LIB}
7738 to check for libraries (@pxref{Libraries}), by adding the library being
7739 checked for to @code{LIBS} temporarily and trying to link a small
7740 program.
7743 @defmac AC_LINK_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
7744 @acindex{LINK_IFELSE}
7745 Run the compiler (and compilation flags) and the linker of the current
7746 language (@pxref{Language Choice}) on the @var{input}, run the shell
7747 commands @var{action-if-true} on success, @var{action-if-false}
7748 otherwise.  The @var{input} can be made by @code{AC_LANG_PROGRAM} and
7749 friends.
7751 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
7752 current compilation flags.
7754 It is customary to report unexpected failures with
7755 @code{AC_MSG_FAILURE}.  This macro does not try to execute the program;
7756 use @code{AC_RUN_IFELSE} if you need to do that (@pxref{Runtime}).
7757 @end defmac
7759 The @code{AC_LINK_IFELSE} macro cannot be used for Erlang tests, since Erlang
7760 programs are interpreted and do not require linking.
7764 @node Runtime
7765 @section Checking Runtime Behavior
7767 Sometimes you need to find out how a system performs at runtime, such
7768 as whether a given function has a certain capability or bug.  If you
7769 can, make such checks when your program runs instead of when it is
7770 configured.  You can check for things like the machine's endianness when
7771 your program initializes itself.
7773 If you really need to test for a runtime behavior while configuring,
7774 you can write a test program to determine the result, and compile and
7775 run it using @code{AC_RUN_IFELSE}.  Avoid running test programs if
7776 possible, because this prevents people from configuring your package for
7777 cross-compiling.
7779 @defmac AC_RUN_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false}, @ovar{action-if-cross-compiling})
7780 @acindex{RUN_IFELSE}
7781 If @var{program} compiles and links successfully and returns an exit
7782 status of 0 when executed, run shell commands @var{action-if-true}.
7783 Otherwise, run shell commands @var{action-if-false}.
7785 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
7786 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
7787 compilation flags of the current language (@pxref{Language Choice}).
7789 If the compiler being used does not produce executables that run on the
7790 system where @command{configure} is being run, then the test program is
7791 not run.  If the optional shell commands @var{action-if-cross-compiling}
7792 are given, they are run instead.  Otherwise, @command{configure} prints
7793 an error message and exits.
7795 In the @var{action-if-false} section, the failing exit status is
7796 available in the shell variable @samp{$?}.  This exit status might be
7797 that of a failed compilation, or it might be that of a failed program
7798 execution.
7800 It is customary to report unexpected failures with
7801 @code{AC_MSG_FAILURE}.
7802 @end defmac
7804 Try to provide a pessimistic default value to use when cross-compiling
7805 makes runtime tests impossible.  You do this by passing the optional
7806 last argument to @code{AC_RUN_IFELSE}.  @command{autoconf} prints a
7807 warning message when creating @command{configure} each time it
7808 encounters a call to @code{AC_RUN_IFELSE} with no
7809 @var{action-if-cross-compiling} argument given.  You may ignore the
7810 warning, though users will not be able to configure your package for
7811 cross-compiling.  A few of the macros distributed with Autoconf produce
7812 this warning message.
7814 To configure for cross-compiling you can also choose a value for those
7815 parameters based on the canonical system name (@pxref{Manual
7816 Configuration}).  Alternatively, set up a test results cache file with
7817 the correct values for the host system (@pxref{Caching Results}).
7819 @ovindex cross_compiling
7820 To provide a default for calls of @code{AC_RUN_IFELSE} that are embedded
7821 in other macros, including a few of the ones that come with Autoconf,
7822 you can test whether the shell variable @code{cross_compiling} is set to
7823 @samp{yes}, and then use an alternate method to get the results instead
7824 of calling the macros.
7826 A C or C++ runtime test should be portable.
7827 @xref{Portable C and C++}.
7829 Erlang tests must exit themselves the Erlang VM by calling the @code{halt/1}
7830 function: the given status code is used to determine the success of the test
7831 (status is @code{0}) or its failure (status is different than @code{0}), as
7832 explained above.  It must be noted that data output through the standard output
7833 (e.g. using @code{io:format/2}) may be truncated when halting the VM.
7834 Therefore, if a test must output configuration information, it is recommended
7835 to create and to output data into the temporary file named @file{conftest.out},
7836 using the functions of module @code{file}.  The @code{conftest.out} file is
7837 automatically deleted by the @code{AC_RUN_IFELSE} macro.  For instance, a
7838 simplified implementation of Autoconf's @code{AC_ERLANG_SUBST_LIB_DIR} macro is:
7840 @example
7841 AC_INIT([LibdirTest], [1.0], [bug-libdirtest@@example.org])
7842 AC_ERLANG_NEED_ERL
7843 AC_LANG(Erlang)
7844 AC_RUN_IFELSE(
7845   [AC_LANG_PROGRAM([], [dnl
7846     file:write_file("conftest.out", code:lib_dir()),
7847     halt(0)])],
7848   [echo "code:lib_dir() returned: `cat conftest.out`"],
7849   [AC_MSG_FAILURE([test Erlang program execution failed])])
7850 @end example
7853 @node Systemology
7854 @section Systemology
7855 @cindex Systemology
7857 This section aims at presenting some systems and pointers to
7858 documentation.  It may help you addressing particular problems reported
7859 by users.
7861 @uref{http://www.opengroup.org/susv3, Posix-conforming systems} are
7862 derived from the @uref{http://www.bell-labs.com/history/unix/, Unix
7863 operating system}.
7865 The @uref{http://bhami.com/rosetta.html, Rosetta Stone for Unix}
7866 contains a table correlating the features of various Posix-conforming
7867 systems.  @uref{http://www.levenez.com/unix/, Unix History} is a
7868 simplified diagram of how many Unix systems were derived from each
7869 other.
7871 @uref{http://heirloom.sourceforge.net/, The Heirloom Project}
7872 provides some variants of traditional implementations of Unix utilities.
7874 @table @asis
7875 @item Darwin
7876 @cindex Darwin
7877 Darwin is also known as Mac OS X@.  Beware that the file system @emph{can} be
7878 case-preserving, but case insensitive.  This can cause nasty problems,
7879 since for instance the installation attempt for a package having an
7880 @file{INSTALL} file can result in @samp{make install} report that
7881 nothing was to be done!
7883 That's all dependent on whether the file system is a UFS (case
7884 sensitive) or HFS+ (case preserving).  By default Apple wants you to
7885 install the OS on HFS+.  Unfortunately, there are some pieces of
7886 software which really need to be built on UFS@.  We may want to rebuild
7887 Darwin to have both UFS and HFS+ available (and put the /local/build
7888 tree on the UFS).
7890 @item @acronym{QNX} 4.25
7891 @cindex @acronym{QNX} 4.25
7892 @c FIXME: Please, if you feel like writing something more precise,
7893 @c it'd be great.  In particular, I can't understand the difference with
7894 @c QNX Neutrino.
7895 @acronym{QNX} is a realtime operating system running on Intel architecture
7896 meant to be scalable from the small embedded systems to the hundred
7897 processor super-computer.  It claims to be Posix certified.  More
7898 information is available on the
7899 @uref{http://www.qnx.com/, @acronym{QNX} home page}.
7901 @item Tru64
7902 @cindex Tru64
7903 @uref{http://h30097.www3.hp.com/@/docs/,
7904 Documentation of several versions of Tru64} is available in different
7905 formats.
7907 @item Unix version 7
7908 @cindex Unix version 7
7909 @cindex V7
7910 Officially this was called the ``Seventh Edition'' of ``the @sc{unix}
7911 time-sharing system'' but we use the more-common name ``Unix version 7''.
7912 Documentation is available in the
7913 @uref{http://plan9.bell-labs.com/7thEdMan/, Unix Seventh Edition Manual}.
7914 Previous versions of Unix are called ``Unix version 6'', etc., but
7915 they were not as widely used.
7916 @end table
7919 @node Multiple Cases
7920 @section Multiple Cases
7922 Some operations are accomplished in several possible ways, depending on
7923 the OS variant.  Checking for them essentially requires a ``case
7924 statement''.  Autoconf does not directly provide one; however, it is
7925 easy to simulate by using a shell variable to keep track of whether a
7926 way to perform the operation has been found yet.
7928 Here is an example that uses the shell variable @code{fstype} to keep
7929 track of whether the remaining cases need to be checked.
7931 @example
7932 @group
7933 AC_MSG_CHECKING([how to get file system type])
7934 fstype=no
7935 # The order of these tests is important.
7936 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statvfs.h>
7937 #include <sys/fstyp.h>]])],
7938                   [AC_DEFINE([FSTYPE_STATVFS], [1],
7939                      [Define if statvfs exists.])
7940                    fstype=SVR4])
7941 if test $fstype = no; then
7942   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
7943 #include <sys/fstyp.h>]])],
7944                   [AC_DEFINE([FSTYPE_USG_STATFS], [1],
7945                      [Define if USG statfs.])
7946                    fstype=SVR3])
7948 if test $fstype = no; then
7949   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
7950 #include <sys/vmount.h>]])]),
7951                   [AC_DEFINE([FSTYPE_AIX_STATFS], [1],
7952                      [Define if AIX statfs.])
7953                    fstype=AIX])
7955 # (more cases omitted here)
7956 AC_MSG_RESULT([$fstype])
7957 @end group
7958 @end example
7960 @c ====================================================== Results of Tests.
7962 @node Results
7963 @chapter Results of Tests
7965 Once @command{configure} has determined whether a feature exists, what can
7966 it do to record that information?  There are four sorts of things it can
7967 do: define a C preprocessor symbol, set a variable in the output files,
7968 save the result in a cache file for future @command{configure} runs, and
7969 print a message letting the user know the result of the test.
7971 @menu
7972 * Defining Symbols::            Defining C preprocessor symbols
7973 * Setting Output Variables::    Replacing variables in output files
7974 * Special Chars in Variables::  Characters to beware of in variables
7975 * Caching Results::             Speeding up subsequent @command{configure} runs
7976 * Printing Messages::           Notifying @command{configure} users
7977 @end menu
7979 @node Defining Symbols
7980 @section Defining C Preprocessor Symbols
7982 A common action to take in response to a feature test is to define a C
7983 preprocessor symbol indicating the results of the test.  That is done by
7984 calling @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}.
7986 By default, @code{AC_OUTPUT} places the symbols defined by these macros
7987 into the output variable @code{DEFS}, which contains an option
7988 @option{-D@var{symbol}=@var{value}} for each symbol defined.  Unlike in
7989 Autoconf version 1, there is no variable @code{DEFS} defined while
7990 @command{configure} is running.  To check whether Autoconf macros have
7991 already defined a certain C preprocessor symbol, test the value of the
7992 appropriate cache variable, as in this example:
7994 @example
7995 AC_CHECK_FUNC([vprintf], [AC_DEFINE([HAVE_VPRINTF], [1],
7996                           [Define if vprintf exists.])])
7997 if test "$ac_cv_func_vprintf" != yes; then
7998   AC_CHECK_FUNC([_doprnt], [AC_DEFINE([HAVE_DOPRNT], [1],
7999                             [Define if _doprnt exists.])])
8001 @end example
8003 If @code{AC_CONFIG_HEADERS} has been called, then instead of creating
8004 @code{DEFS}, @code{AC_OUTPUT} creates a header file by substituting the
8005 correct values into @code{#define} statements in a template file.
8006 @xref{Configuration Headers}, for more information about this kind of
8007 output.
8009 @defmac AC_DEFINE (@var{variable}, @var{value}, @ovar{description})
8010 @defmacx AC_DEFINE (@var{variable})
8011 @acindex{DEFINE}
8012 Define the C preprocessor variable @var{variable} to @var{value} (verbatim).
8013 @var{value} should not contain literal newlines, and if you are not
8014 using @code{AC_CONFIG_HEADERS} it should not contain any @samp{#}
8015 characters, as @command{make} tends to eat them.  To use a shell variable,
8016 use @code{AC_DEFINE_UNQUOTED} instead.
8017 @var{description} is only useful if you are using
8018 @code{AC_CONFIG_HEADERS}.  In this case, @var{description} is put into
8019 the generated @file{config.h.in} as the comment before the macro define.
8020 The following example defines the C preprocessor variable
8021 @code{EQUATION} to be the string constant @samp{"$a > $b"}:
8023 @example
8024 AC_DEFINE([EQUATION], ["$a > $b"],
8025   [Equation string.])
8026 @end example
8028 If neither @var{value} nor @var{description} are given, then
8029 @var{value} defaults to 1 instead of to the empty string.  This is for
8030 backwards compatibility with older versions of Autoconf, but this usage
8031 is obsolescent and may be withdrawn in future versions of Autoconf.
8033 If the @var{variable} is a literal string, it is passed to
8034 @code{m4_pattern_allow} (@pxref{Forbidden Patterns}).
8035 @end defmac
8037 @defmac AC_DEFINE_UNQUOTED (@var{variable}, @var{value}, @ovar{description})
8038 @defmacx AC_DEFINE_UNQUOTED (@var{variable})
8039 @acindex{DEFINE_UNQUOTED}
8040 Like @code{AC_DEFINE}, but three shell expansions are
8041 performed---once---on @var{variable} and @var{value}: variable expansion
8042 (@samp{$}), command substitution (@samp{`}), and backslash escaping
8043 (@samp{\}).  Single and double quote characters in the value have no
8044 special meaning.  Use this macro instead of @code{AC_DEFINE} when
8045 @var{variable} or @var{value} is a shell variable.  Examples:
8047 @example
8048 AC_DEFINE_UNQUOTED([config_machfile], ["$machfile"],
8049   [Configuration machine file.])
8050 AC_DEFINE_UNQUOTED([GETGROUPS_T], [$ac_cv_type_getgroups],
8051   [getgroups return type.])
8052 AC_DEFINE_UNQUOTED([$ac_tr_hdr], [1],
8053   [Translated header name.])
8054 @end example
8055 @end defmac
8057 Due to a syntactical bizarreness of the Bourne shell, do not use
8058 semicolons to separate @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}
8059 calls from other macro calls or shell code; that can cause syntax errors
8060 in the resulting @command{configure} script.  Use either blanks or
8061 newlines.  That is, do this:
8063 @example
8064 AC_CHECK_HEADER([elf.h],
8065   [AC_DEFINE([SVR4], [1], [System V Release 4]) LIBS="-lelf $LIBS"])
8066 @end example
8068 @noindent
8069 or this:
8071 @example
8072 AC_CHECK_HEADER([elf.h],
8073   [AC_DEFINE([SVR4], [1], [System V Release 4])
8074    LIBS="-lelf $LIBS"])
8075 @end example
8077 @noindent
8078 instead of this:
8080 @example
8081 AC_CHECK_HEADER([elf.h],
8082   [AC_DEFINE([SVR4], [1], [System V Release 4]); LIBS="-lelf $LIBS"])
8083 @end example
8085 @node Setting Output Variables
8086 @section Setting Output Variables
8087 @cindex Output variables
8089 Another way to record the results of tests is to set @dfn{output
8090 variables}, which are shell variables whose values are substituted into
8091 files that @command{configure} outputs.  The two macros below create new
8092 output variables.  @xref{Preset Output Variables}, for a list of output
8093 variables that are always available.
8095 @defmac AC_SUBST (@var{variable}, @ovar{value})
8096 @acindex{SUBST}
8097 Create an output variable from a shell variable.  Make @code{AC_OUTPUT}
8098 substitute the variable @var{variable} into output files (typically one
8099 or more @file{Makefile}s).  This means that @code{AC_OUTPUT} will
8100 replace instances of @samp{@@@var{variable}@@} in input files with the
8101 value that the shell variable @var{variable} has when @code{AC_OUTPUT}
8102 is called.  The value can contain newlines.
8103 The substituted value is not rescanned for more output variables;
8104 occurrences of @samp{@@@var{variable}@@} in the value are inserted
8105 literally into the output file.  (The algorithm uses the special marker
8106 @code{|#_!!_#|} internally, so the substituted value cannot contain
8107 @code{|#_!!_#|}.)
8109 If @var{value} is given, in addition assign it to @var{variable}.
8111 The string @var{variable} is passed to @code{m4_pattern_allow}
8112 (@pxref{Forbidden Patterns}).
8113 @end defmac
8115 @defmac AC_SUBST_FILE (@var{variable})
8116 @acindex{SUBST_FILE}
8117 Another way to create an output variable from a shell variable.  Make
8118 @code{AC_OUTPUT} insert (without substitutions) the contents of the file
8119 named by shell variable @var{variable} into output files.  This means
8120 that @code{AC_OUTPUT} will replace instances of
8121 @samp{@@@var{variable}@@} in output files (such as @file{Makefile.in})
8122 with the contents of the file that the shell variable @var{variable}
8123 names when @code{AC_OUTPUT} is called.  Set the variable to
8124 @file{/dev/null} for cases that do not have a file to insert.
8125 This substitution occurs only when the @samp{@@@var{variable}@@} is on a
8126 line by itself, optionally surrounded by spaces and tabs.  The
8127 substitution replaces the whole line, including the spaces, tabs, and
8128 the terminating newline.
8130 This macro is useful for inserting @file{Makefile} fragments containing
8131 special dependencies or other @code{make} directives for particular host
8132 or target types into @file{Makefile}s.  For example, @file{configure.ac}
8133 could contain:
8135 @example
8136 AC_SUBST_FILE([host_frag])
8137 host_frag=$srcdir/conf/sun4.mh
8138 @end example
8140 @noindent
8141 and then a @file{Makefile.in} could contain:
8143 @example
8144 @@host_frag@@
8145 @end example
8147 The string @var{variable} is passed to @code{m4_pattern_allow}
8148 (@pxref{Forbidden Patterns}).
8149 @end defmac
8151 @cindex Previous Variable
8152 @cindex Variable, Precious
8153 Running @command{configure} in varying environments can be extremely
8154 dangerous.  If for instance the user runs @samp{CC=bizarre-cc
8155 ./configure}, then the cache, @file{config.h}, and many other output
8156 files will depend upon @command{bizarre-cc} being the C compiler.  If
8157 for some reason the user runs @command{./configure} again, or if it is
8158 run via @samp{./config.status --recheck}, (@xref{Automatic Remaking},
8159 and @pxref{config.status Invocation}), then the configuration can be
8160 inconsistent, composed of results depending upon two different
8161 compilers.
8163 Environment variables that affect this situation, such as @samp{CC}
8164 above, are called @dfn{precious variables}, and can be declared as such
8165 by @code{AC_ARG_VAR}.
8167 @defmac AC_ARG_VAR (@var{variable}, @var{description})
8168 @acindex{ARG_VAR}
8169 Declare @var{variable} is a precious variable, and include its
8170 @var{description} in the variable section of @samp{./configure --help}.
8172 Being precious means that
8173 @itemize @minus
8174 @item
8175 @var{variable} is @code{AC_SUBST}'d.
8177 @item
8178 The value of @var{variable} when @command{configure} was launched is
8179 saved in the cache, including if it was not specified on the command
8180 line but via the environment.  Indeed, while @command{configure} can
8181 notice the definition of @code{CC} in @samp{./configure CC=bizarre-cc},
8182 it is impossible to notice it in @samp{CC=bizarre-cc ./configure},
8183 which, unfortunately, is what most users do.
8185 We emphasize that it is the @emph{initial} value of @var{variable} which
8186 is saved, not that found during the execution of @command{configure}.
8187 Indeed, specifying @samp{./configure FOO=foo} and letting
8188 @samp{./configure} guess that @code{FOO} is @code{foo} can be two very
8189 different runs.
8191 @item
8192 @var{variable} is checked for consistency between two
8193 @command{configure} runs.  For instance:
8195 @example
8196 $ @kbd{./configure --silent --config-cache}
8197 $ @kbd{CC=cc ./configure --silent --config-cache}
8198 configure: error: `CC' was not set in the previous run
8199 configure: error: changes in the environment can compromise \
8200 the build
8201 configure: error: run `make distclean' and/or \
8202 `rm config.cache' and start over
8203 @end example
8205 @noindent
8206 and similarly if the variable is unset, or if its content is changed.
8209 @item
8210 @var{variable} is kept during automatic reconfiguration
8211 (@pxref{config.status Invocation}) as if it had been passed as a command
8212 line argument, including when no cache is used:
8214 @example
8215 $ @kbd{CC=/usr/bin/cc ./configure undeclared_var=raboof --silent}
8216 $ @kbd{./config.status --recheck}
8217 running /bin/sh ./configure undeclared_var=raboof --silent \
8218   CC=/usr/bin/cc  --no-create --no-recursion
8219 @end example
8220 @end itemize
8221 @end defmac
8223 @node Special Chars in Variables
8224 @section Special Characters in Output Variables
8225 @cindex Output variables, special characters in
8227 Many output variables are intended to be evaluated both by
8228 @command{make} and by the shell.  Some characters are expanded
8229 differently in these two contexts, so to avoid confusion these
8230 variables' values should not contain any of the following characters:
8232 @example
8233 " # $ & ' ( ) * ; < > ? [ \ ^ ` |
8234 @end example
8236 Also, these variables' values should neither contain newlines, nor start
8237 with @samp{~}, nor contain white space or @samp{:} immediately followed
8238 by @samp{~}.  The values can contain nonempty sequences of white space
8239 characters like tabs and spaces, but each such sequence might
8240 arbitrarily be replaced by a single space during substitution.
8242 These restrictions apply both to the values that @command{configure}
8243 computes, and to the values set directly by the user.  For example, the
8244 following invocations of @command{configure} are problematic, since they
8245 attempt to use special characters within @code{CPPFLAGS}:
8247 @example
8248 CPPFLAGS='-DOUCH="&\"#$*?"' ./configure
8250 ./configure CPPFLAGS='-DOUCH="&\"#$*?"'
8251 @end example
8253 @node Caching Results
8254 @section Caching Results
8255 @cindex Cache
8257 To avoid checking for the same features repeatedly in various
8258 @command{configure} scripts (or in repeated runs of one script),
8259 @command{configure} can optionally save the results of many checks in a
8260 @dfn{cache file} (@pxref{Cache Files}).  If a @command{configure} script
8261 runs with caching enabled and finds a cache file, it reads the results
8262 of previous runs from the cache and avoids rerunning those checks.  As a
8263 result, @command{configure} can then run much faster than if it had to
8264 perform all of the checks every time.
8266 @defmac AC_CACHE_VAL (@var{cache-id}, @var{commands-to-set-it})
8267 @acindex{CACHE_VAL}
8268 Ensure that the results of the check identified by @var{cache-id} are
8269 available.  If the results of the check were in the cache file that was
8270 read, and @command{configure} was not given the @option{--quiet} or
8271 @option{--silent} option, print a message saying that the result was
8272 cached; otherwise, run the shell commands @var{commands-to-set-it}.  If
8273 the shell commands are run to determine the value, the value will be
8274 saved in the cache file just before @command{configure} creates its output
8275 files.  @xref{Cache Variable Names}, for how to choose the name of the
8276 @var{cache-id} variable.
8278 The @var{commands-to-set-it} @emph{must have no side effects} except for
8279 setting the variable @var{cache-id}, see below.
8280 @end defmac
8282 @defmac AC_CACHE_CHECK (@var{message}, @var{cache-id}, @var{commands-to-set-it})
8283 @acindex{CACHE_CHECK}
8284 A wrapper for @code{AC_CACHE_VAL} that takes care of printing the
8285 messages.  This macro provides a convenient shorthand for the most
8286 common way to use these macros.  It calls @code{AC_MSG_CHECKING} for
8287 @var{message}, then @code{AC_CACHE_VAL} with the @var{cache-id} and
8288 @var{commands} arguments, and @code{AC_MSG_RESULT} with @var{cache-id}.
8290 The @var{commands-to-set-it} @emph{must have no side effects} except for
8291 setting the variable @var{cache-id}, see below.
8292 @end defmac
8294 It is very common to find buggy macros using @code{AC_CACHE_VAL} or
8295 @code{AC_CACHE_CHECK}, because people are tempted to call
8296 @code{AC_DEFINE} in the @var{commands-to-set-it}.  Instead, the code that
8297 @emph{follows} the call to @code{AC_CACHE_VAL} should call
8298 @code{AC_DEFINE}, by examining the value of the cache variable.  For
8299 instance, the following macro is broken:
8301 @example
8302 @group
8303 AC_DEFUN([AC_SHELL_TRUE],
8304 [AC_CACHE_CHECK([whether true(1) works], [ac_cv_shell_true_works],
8305                 [ac_cv_shell_true_works=no
8306                  (true) 2>/dev/null && ac_cv_shell_true_works=yes
8307                  if test "$ac_cv_shell_true_works" = yes; then
8308                    AC_DEFINE([TRUE_WORKS], [1],
8309                              [Define if `true(1)' works properly.])
8310                  fi])
8312 @end group
8313 @end example
8315 @noindent
8316 This fails if the cache is enabled: the second time this macro is run,
8317 @code{TRUE_WORKS} @emph{will not be defined}.  The proper implementation
8320 @example
8321 @group
8322 AC_DEFUN([AC_SHELL_TRUE],
8323 [AC_CACHE_CHECK([whether true(1) works], [ac_cv_shell_true_works],
8324                 [ac_cv_shell_true_works=no
8325                  (true) 2>/dev/null && ac_cv_shell_true_works=yes])
8326  if test "$ac_cv_shell_true_works" = yes; then
8327    AC_DEFINE([TRUE_WORKS], [1],
8328              [Define if `true(1)' works properly.])
8329  fi
8331 @end group
8332 @end example
8334 Also, @var{commands-to-set-it} should not print any messages, for
8335 example with @code{AC_MSG_CHECKING}; do that before calling
8336 @code{AC_CACHE_VAL}, so the messages are printed regardless of whether
8337 the results of the check are retrieved from the cache or determined by
8338 running the shell commands.
8340 @menu
8341 * Cache Variable Names::        Shell variables used in caches
8342 * Cache Files::                 Files @command{configure} uses for caching
8343 * Cache Checkpointing::         Loading and saving the cache file
8344 @end menu
8346 @node Cache Variable Names
8347 @subsection Cache Variable Names
8348 @cindex Cache variable
8350 The names of cache variables should have the following format:
8352 @example
8353 @var{package-prefix}_cv_@var{value-type}_@var{specific-value}_@ovar{additional-options}
8354 @end example
8356 @noindent
8357 for example, @samp{ac_cv_header_stat_broken} or
8358 @samp{ac_cv_prog_gcc_traditional}.  The parts of the variable name are:
8360 @table @asis
8361 @item @var{package-prefix}
8362 An abbreviation for your package or organization; the same prefix you
8363 begin local Autoconf macros with, except lowercase by convention.
8364 For cache values used by the distributed Autoconf macros, this value is
8365 @samp{ac}.
8367 @item @code{_cv_}
8368 Indicates that this shell variable is a cache value.  This string
8369 @emph{must} be present in the variable name, including the leading
8370 underscore.
8372 @item @var{value-type}
8373 A convention for classifying cache values, to produce a rational naming
8374 system.  The values used in Autoconf are listed in @ref{Macro Names}.
8376 @item @var{specific-value}
8377 Which member of the class of cache values this test applies to.
8378 For example, which function (@samp{alloca}), program (@samp{gcc}), or
8379 output variable (@samp{INSTALL}).
8381 @item @var{additional-options}
8382 Any particular behavior of the specific member that this test applies to.
8383 For example, @samp{broken} or @samp{set}.  This part of the name may
8384 be omitted if it does not apply.
8385 @end table
8387 The values assigned to cache variables may not contain newlines.
8388 Usually, their values will be Boolean (@samp{yes} or @samp{no}) or the
8389 names of files or functions; so this is not an important restriction.
8391 @node Cache Files
8392 @subsection Cache Files
8394 A cache file is a shell script that caches the results of configure
8395 tests run on one system so they can be shared between configure scripts
8396 and configure runs.  It is not useful on other systems.  If its contents
8397 are invalid for some reason, the user may delete or edit it.
8399 By default, @command{configure} uses no cache file,
8400 to avoid problems caused by accidental
8401 use of stale cache files.
8403 To enable caching, @command{configure} accepts @option{--config-cache} (or
8404 @option{-C}) to cache results in the file @file{config.cache}.
8405 Alternatively, @option{--cache-file=@var{file}} specifies that
8406 @var{file} be the cache file.  The cache file is created if it does not
8407 exist already.  When @command{configure} calls @command{configure} scripts in
8408 subdirectories, it uses the @option{--cache-file} argument so that they
8409 share the same cache.  @xref{Subdirectories}, for information on
8410 configuring subdirectories with the @code{AC_CONFIG_SUBDIRS} macro.
8412 @file{config.status} only pays attention to the cache file if it is
8413 given the @option{--recheck} option, which makes it rerun
8414 @command{configure}.
8416 It is wrong to try to distribute cache files for particular system types.
8417 There is too much room for error in doing that, and too much
8418 administrative overhead in maintaining them.  For any features that
8419 can't be guessed automatically, use the standard method of the canonical
8420 system type and linking files (@pxref{Manual Configuration}).
8422 The site initialization script can specify a site-wide cache file to
8423 use, instead of the usual per-program cache.  In this case, the cache
8424 file will gradually accumulate information whenever someone runs a new
8425 @command{configure} script.  (Running @command{configure} merges the new cache
8426 results with the existing cache file.)  This may cause problems,
8427 however, if the system configuration (e.g., the installed libraries or
8428 compilers) changes and the stale cache file is not deleted.
8430 @node Cache Checkpointing
8431 @subsection Cache Checkpointing
8433 If your configure script, or a macro called from @file{configure.ac}, happens
8434 to abort the configure process, it may be useful to checkpoint the cache
8435 a few times at key points using @code{AC_CACHE_SAVE}.  Doing so will
8436 reduce the amount of time it takes to re-run the configure script with
8437 (hopefully) the error that caused the previous abort corrected.
8439 @c FIXME: Do we really want to document this guy?
8440 @defmac AC_CACHE_LOAD
8441 @acindex{CACHE_LOAD}
8442 Loads values from existing cache file, or creates a new cache file if a
8443 cache file is not found.  Called automatically from @code{AC_INIT}.
8444 @end defmac
8446 @defmac AC_CACHE_SAVE
8447 @acindex{CACHE_SAVE}
8448 Flushes all cached values to the cache file.  Called automatically from
8449 @code{AC_OUTPUT}, but it can be quite useful to call
8450 @code{AC_CACHE_SAVE} at key points in @file{configure.ac}.
8451 @end defmac
8453 For instance:
8455 @example
8456 @r{ @dots{} AC_INIT, etc. @dots{}}
8457 @group
8458 # Checks for programs.
8459 AC_PROG_CC
8460 AC_PROG_AWK
8461 @r{ @dots{} more program checks @dots{}}
8462 AC_CACHE_SAVE
8463 @end group
8465 @group
8466 # Checks for libraries.
8467 AC_CHECK_LIB([nsl], [gethostbyname])
8468 AC_CHECK_LIB([socket], [connect])
8469 @r{ @dots{} more lib checks @dots{}}
8470 AC_CACHE_SAVE
8471 @end group
8473 @group
8474 # Might abort@dots{}
8475 AM_PATH_GTK([1.0.2], [], [AC_MSG_ERROR([GTK not in path])])
8476 AM_PATH_GTKMM([0.9.5], [], [AC_MSG_ERROR([GTK not in path])])
8477 @end group
8478 @r{ @dots{} AC_OUTPUT, etc. @dots{}}
8479 @end example
8481 @node Printing Messages
8482 @section Printing Messages
8483 @cindex Messages, from @command{configure}
8485 @command{configure} scripts need to give users running them several kinds
8486 of information.  The following macros print messages in ways appropriate
8487 for each kind.  The arguments to all of them get enclosed in shell
8488 double quotes, so the shell performs variable and back-quote
8489 substitution on them.
8491 These macros are all wrappers around the @command{echo} shell command,
8492 and will direct output to the appropriate file descriptor (@pxref{File
8493 Descriptor Macros}).
8494 @command{configure} scripts should rarely need to run @command{echo} directly
8495 to print messages for the user.  Using these macros makes it easy to
8496 change how and when each kind of message is printed; such changes need
8497 only be made to the macro definitions and all the callers will change
8498 automatically.
8500 To diagnose static issues, i.e., when @command{autoconf} is run, see
8501 @ref{Reporting Messages}.
8503 @defmac AC_MSG_CHECKING (@var{feature-description})
8504 @acindex{MSG_CHECKING}
8505 Notify the user that @command{configure} is checking for a particular
8506 feature.  This macro prints a message that starts with @samp{checking }
8507 and ends with @samp{...} and no newline.  It must be followed by a call
8508 to @code{AC_MSG_RESULT} to print the result of the check and the
8509 newline.  The @var{feature-description} should be something like
8510 @samp{whether the Fortran compiler accepts C++ comments} or @samp{for
8511 c89}.
8513 This macro prints nothing if @command{configure} is run with the
8514 @option{--quiet} or @option{--silent} option.
8515 @end defmac
8517 @defmac AC_MSG_RESULT (@var{result-description})
8518 @acindex{MSG_RESULT}
8519 Notify the user of the results of a check.  @var{result-description} is
8520 almost always the value of the cache variable for the check, typically
8521 @samp{yes}, @samp{no}, or a file name.  This macro should follow a call
8522 to @code{AC_MSG_CHECKING}, and the @var{result-description} should be
8523 the completion of the message printed by the call to
8524 @code{AC_MSG_CHECKING}.
8526 This macro prints nothing if @command{configure} is run with the
8527 @option{--quiet} or @option{--silent} option.
8528 @end defmac
8530 @defmac AC_MSG_NOTICE (@var{message})
8531 @acindex{MSG_NOTICE}
8532 Deliver the @var{message} to the user.  It is useful mainly to print a
8533 general description of the overall purpose of a group of feature checks,
8534 e.g.,
8536 @example
8537 AC_MSG_NOTICE([checking if stack overflow is detectable])
8538 @end example
8540 This macro prints nothing if @command{configure} is run with the
8541 @option{--quiet} or @option{--silent} option.
8542 @end defmac
8544 @defmac AC_MSG_ERROR (@var{error-description}, @ovar{exit-status})
8545 @acindex{MSG_ERROR}
8546 Notify the user of an error that prevents @command{configure} from
8547 completing.  This macro prints an error message to the standard error
8548 output and exits @command{configure} with @var{exit-status} (1 by default).
8549 @var{error-description} should be something like @samp{invalid value
8550 $HOME for \$HOME}.
8552 The @var{error-description} should start with a lower-case letter, and
8553 ``cannot'' is preferred to ``can't''.
8554 @end defmac
8556 @defmac AC_MSG_FAILURE (@var{error-description}, @ovar{exit-status})
8557 @acindex{MSG_FAILURE}
8558 This @code{AC_MSG_ERROR} wrapper notifies the user of an error that
8559 prevents @command{configure} from completing @emph{and} that additional
8560 details are provided in @file{config.log}.  This is typically used when
8561 abnormal results are found during a compilation.
8562 @end defmac
8564 @defmac AC_MSG_WARN (@var{problem-description})
8565 @acindex{MSG_WARN}
8566 Notify the @command{configure} user of a possible problem.  This macro
8567 prints the message to the standard error output; @command{configure}
8568 continues running afterward, so macros that call @code{AC_MSG_WARN} should
8569 provide a default (back-up) behavior for the situations they warn about.
8570 @var{problem-description} should be something like @samp{ln -s seems to
8571 make hard links}.
8572 @end defmac
8576 @c ====================================================== Programming in M4.
8578 @node Programming in M4
8579 @chapter Programming in M4
8580 @cindex M4
8582 Autoconf is written on top of two layers: @dfn{M4sugar}, which provides
8583 convenient macros for pure M4 programming, and @dfn{M4sh}, which
8584 provides macros dedicated to shell script generation.
8586 As of this version of Autoconf, these two layers are still experimental,
8587 and their interface might change in the future.  As a matter of fact,
8588 @emph{anything that is not documented must not be used}.
8590 @menu
8591 * M4 Quotation::                Protecting macros from unwanted expansion
8592 * Using autom4te::              The Autoconf executables backbone
8593 * Programming in M4sugar::      Convenient pure M4 macros
8594 * Programming in M4sh::         Common shell Constructs
8595 * File Descriptor Macros::      File descriptor macros for input and output
8596 @end menu
8598 @node M4 Quotation
8599 @section M4 Quotation
8600 @cindex M4 quotation
8601 @cindex quotation
8603 @c FIXME: Grmph, yet another quoting myth: quotation has *never*
8604 @c prevented `expansion' of $1.  Unless it refers to the expansion
8605 @c of the value of $1?  Anyway, we need a rewrite here@enddots{}
8607 The most common problem with existing macros is an improper quotation.
8608 This section, which users of Autoconf can skip, but which macro writers
8609 @emph{must} read, first justifies the quotation scheme that was chosen
8610 for Autoconf and then ends with a rule of thumb.  Understanding the
8611 former helps one to follow the latter.
8613 @menu
8614 * Active Characters::           Characters that change the behavior of M4
8615 * One Macro Call::              Quotation and one macro call
8616 * Quotation and Nested Macros::  Macros calling macros
8617 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
8618 * Quadrigraphs::                Another way to escape special characters
8619 * Quotation Rule Of Thumb::     One parenthesis, one quote
8620 @end menu
8622 @node Active Characters
8623 @subsection Active Characters
8625 To fully understand where proper quotation is important, you first need
8626 to know what the special characters are in Autoconf: @samp{#} introduces
8627 a comment inside which no macro expansion is performed, @samp{,}
8628 separates arguments, @samp{[} and @samp{]} are the quotes themselves,
8629 and finally @samp{(} and @samp{)} (which M4 tries to match by
8630 pairs).
8632 In order to understand the delicate case of macro calls, we first have
8633 to present some obvious failures.  Below they are ``obvious-ified'',
8634 but when you find them in real life, they are usually in disguise.
8636 Comments, introduced by a hash and running up to the newline, are opaque
8637 tokens to the top level: active characters are turned off, and there is
8638 no macro expansion:
8640 @example
8641 # define([def], ine)
8642 @result{}# define([def], ine)
8643 @end example
8645 Each time there can be a macro expansion, there is a quotation
8646 expansion, i.e., one level of quotes is stripped:
8648 @example
8649 int tab[10];
8650 @result{}int tab10;
8651 [int tab[10];]
8652 @result{}int tab[10];
8653 @end example
8655 Without this in mind, the reader will try hopelessly to use her macro
8656 @code{array}:
8658 @example
8659 define([array], [int tab[10];])
8660 array
8661 @result{}int tab10;
8662 [array]
8663 @result{}array
8664 @end example
8666 @noindent
8667 How can you correctly output the intended results@footnote{Using
8668 @code{defn}.}?
8671 @node One Macro Call
8672 @subsection One Macro Call
8674 Let's proceed on the interaction between active characters and macros
8675 with this small macro, which just returns its first argument:
8677 @example
8678 define([car], [$1])
8679 @end example
8681 @noindent
8682 The two pairs of quotes above are not part of the arguments of
8683 @code{define}; rather, they are understood by the top level when it
8684 tries to find the arguments of @code{define}.  Therefore, assuming
8685 @code{car} is not already defined, it is equivalent to write:
8687 @example
8688 define(car, $1)
8689 @end example
8691 @noindent
8692 But, while it is acceptable for a @file{configure.ac} to avoid unnecessary
8693 quotes, it is bad practice for Autoconf macros which must both be more
8694 robust and also advocate perfect style.
8696 At the top level, there are only two possibilities: either you
8697 quote or you don't:
8699 @example
8700 car(foo, bar, baz)
8701 @result{}foo
8702 [car(foo, bar, baz)]
8703 @result{}car(foo, bar, baz)
8704 @end example
8706 Let's pay attention to the special characters:
8708 @example
8709 car(#)
8710 @error{}EOF in argument list
8711 @end example
8713 The closing parenthesis is hidden in the comment; with a hypothetical
8714 quoting, the top level understood it this way:
8716 @example
8717 car([#)]
8718 @end example
8720 @noindent
8721 Proper quotation, of course, fixes the problem:
8723 @example
8724 car([#])
8725 @result{}#
8726 @end example
8728 Here are more examples:
8730 @example
8731 car(foo, bar)
8732 @result{}foo
8733 car([foo, bar])
8734 @result{}foo, bar
8735 car((foo, bar))
8736 @result{}(foo, bar)
8737 car([(foo], [bar)])
8738 @result{}(foo
8739 define([a], [b])
8740 @result{}
8741 car(a)
8742 @result{}b
8743 car([a])
8744 @result{}b
8745 car([[a]])
8746 @result{}a
8747 car([[[a]]])
8748 @result{}[a]
8749 @end example
8751 With this in mind, we can explore the cases where macros invoke
8752 macros@enddots{}
8755 @node Quotation and Nested Macros
8756 @subsection Quotation and Nested Macros
8758 The examples below use the following macros:
8760 @example
8761 define([car], [$1])
8762 define([active], [ACT, IVE])
8763 define([array], [int tab[10]])
8764 @end example
8766 Each additional embedded macro call introduces other possible
8767 interesting quotations:
8769 @example
8770 car(active)
8771 @result{}ACT
8772 car([active])
8773 @result{}ACT, IVE
8774 car([[active]])
8775 @result{}active
8776 @end example
8778 In the first case, the top level looks for the arguments of @code{car},
8779 and finds @samp{active}.  Because M4 evaluates its arguments
8780 before applying the macro, @samp{active} is expanded, which results in:
8782 @example
8783 car(ACT, IVE)
8784 @result{}ACT
8785 @end example
8787 @noindent
8788 In the second case, the top level gives @samp{active} as first and only
8789 argument of @code{car}, which results in:
8791 @example
8792 active
8793 @result{}ACT, IVE
8794 @end example
8796 @noindent
8797 i.e., the argument is evaluated @emph{after} the macro that invokes it.
8798 In the third case, @code{car} receives @samp{[active]}, which results in:
8800 @example
8801 [active]
8802 @result{}active
8803 @end example
8805 @noindent
8806 exactly as we already saw above.
8808 The example above, applied to a more realistic example, gives:
8810 @example
8811 car(int tab[10];)
8812 @result{}int tab10;
8813 car([int tab[10];])
8814 @result{}int tab10;
8815 car([[int tab[10];]])
8816 @result{}int tab[10];
8817 @end example
8819 @noindent
8820 Huh?  The first case is easily understood, but why is the second wrong,
8821 and the third right?  To understand that, you must know that after
8822 M4 expands a macro, the resulting text is immediately subjected
8823 to macro expansion and quote removal.  This means that the quote removal
8824 occurs twice---first before the argument is passed to the @code{car}
8825 macro, and second after the @code{car} macro expands to the first
8826 argument.
8828 As the author of the Autoconf macro @code{car}, you then consider it to
8829 be incorrect that your users have to double-quote the arguments of
8830 @code{car}, so you ``fix'' your macro.  Let's call it @code{qar} for
8831 quoted car:
8833 @example
8834 define([qar], [[$1]])
8835 @end example
8837 @noindent
8838 and check that @code{qar} is properly fixed:
8840 @example
8841 qar([int tab[10];])
8842 @result{}int tab[10];
8843 @end example
8845 @noindent
8846 Ahhh!  That's much better.
8848 But note what you've done: now that the arguments are literal strings,
8849 if the user wants to use the results of expansions as arguments, she has
8850 to use an @emph{unquoted} macro call:
8852 @example
8853 qar(active)
8854 @result{}ACT
8855 @end example
8857 @noindent
8858 where she wanted to reproduce what she used to do with @code{car}:
8860 @example
8861 car([active])
8862 @result{}ACT, IVE
8863 @end example
8865 @noindent
8866 Worse yet: she wants to use a macro that produces a set of @code{cpp}
8867 macros:
8869 @example
8870 define([my_includes], [#include <stdio.h>])
8871 car([my_includes])
8872 @result{}#include <stdio.h>
8873 qar(my_includes)
8874 @error{}EOF in argument list
8875 @end example
8877 This macro, @code{qar}, because it double quotes its arguments, forces
8878 its users to leave their macro calls unquoted, which is dangerous.
8879 Commas and other active symbols are interpreted by M4 before
8880 they are given to the macro, often not in the way the users expect.
8881 Also, because @code{qar} behaves differently from the other macros,
8882 it's an exception that should be avoided in Autoconf.
8884 @node Changequote is Evil
8885 @subsection @code{changequote} is Evil
8886 @cindex @code{changequote}
8888 The temptation is often high to bypass proper quotation, in particular
8889 when it's late at night.  Then, many experienced Autoconf hackers
8890 finally surrender to the dark side of the force and use the ultimate
8891 weapon: @code{changequote}.
8893 The M4 builtin @code{changequote} belongs to a set of primitives that
8894 allow one to adjust the syntax of the language to adjust it to one's
8895 needs.  For instance, by default M4 uses @samp{`} and @samp{'} as
8896 quotes, but in the context of shell programming (and actually of most
8897 programming languages), that's about the worst choice one can make:
8898 because of strings and back-quoted expressions in shell code (such as
8899 @samp{'this'} and @samp{`that`}), because of literal characters in usual
8900 programming languages (as in @samp{'0'}), there are many unbalanced
8901 @samp{`} and @samp{'}.  Proper M4 quotation then becomes a nightmare, if
8902 not impossible.  In order to make M4 useful in such a context, its
8903 designers have equipped it with @code{changequote}, which makes it
8904 possible to choose another pair of quotes.  M4sugar, M4sh, Autoconf, and
8905 Autotest all have chosen to use @samp{[} and @samp{]}.  Not especially
8906 because they are unlikely characters, but @emph{because they are
8907 characters unlikely to be unbalanced}.
8909 There are other magic primitives, such as @code{changecom} to specify
8910 what syntactic forms are comments (it is common to see
8911 @samp{changecom(<!--, -->)} when M4 is used to produce HTML pages),
8912 @code{changeword} and @code{changesyntax} to change other syntactic
8913 details (such as the character to denote the @var{n}th argument, @samp{$} by
8914 default, the parenthesis around arguments, etc.).
8916 These primitives are really meant to make M4 more useful for specific
8917 domains: they should be considered like command line options:
8918 @option{--quotes}, @option{--comments}, @option{--words}, and
8919 @option{--syntax}.  Nevertheless, they are implemented as M4 builtins, as
8920 it makes M4 libraries self contained (no need for additional options).
8922 There lies the problem@enddots{}
8924 @sp 1
8926 The problem is that it is then tempting to use them in the middle of an
8927 M4 script, as opposed to its initialization.  This, if not carefully
8928 thought out, can lead to disastrous effects: @emph{you are changing the
8929 language in the middle of the execution}.  Changing and restoring the
8930 syntax is often not enough: if you happened to invoke macros in between,
8931 these macros will be lost, as the current syntax will probably not be
8932 the one they were implemented with.
8934 @c FIXME: I've been looking for a short, real case example, but I
8935 @c lost them all :(
8938 @node Quadrigraphs
8939 @subsection Quadrigraphs
8940 @cindex quadrigraphs
8941 @cindex @samp{@@S|@@}
8942 @cindex @samp{@@&t@@}
8943 @c Info cannot handle `:' in index entries.
8944 @c @cindex @samp{@@<:@@}
8945 @c @cindex @samp{@@:>@@}
8946 @c @cindex @samp{@@%:@@}
8948 When writing an Autoconf macro you may occasionally need to generate
8949 special characters that are difficult to express with the standard
8950 Autoconf quoting rules.  For example, you may need to output the regular
8951 expression @samp{[^[]}, which matches any character other than @samp{[}.
8952 This expression contains unbalanced brackets so it cannot be put easily
8953 into an M4 macro.
8955 You can work around this problem by using one of the following
8956 @dfn{quadrigraphs}:
8958 @table @samp
8959 @item @@<:@@
8960 @samp{[}
8961 @item @@:>@@
8962 @samp{]}
8963 @item @@S|@@
8964 @samp{$}
8965 @item @@%:@@
8966 @samp{#}
8967 @item @@&t@@
8968 Expands to nothing.
8969 @end table
8971 Quadrigraphs are replaced at a late stage of the translation process,
8972 after @command{m4} is run, so they do not get in the way of M4 quoting.
8973 For example, the string @samp{^@@<:@@}, independently of its quotation,
8974 will appear as @samp{^[} in the output.
8976 The empty quadrigraph can be used:
8978 @itemize @minus
8979 @item to mark trailing spaces explicitly
8981 Trailing spaces are smashed by @command{autom4te}.  This is a feature.
8983 @item to produce other quadrigraphs
8985 For instance @samp{@@<@@&t@@:@@} produces @samp{@@<:@@}.
8987 @item to escape @emph{occurrences} of forbidden patterns
8989 For instance you might want to mention @code{AC_FOO} in a comment, while
8990 still being sure that @command{autom4te} will still catch unexpanded
8991 @samp{AC_*}.  Then write @samp{AC@@&t@@_FOO}.
8992 @end itemize
8994 The name @samp{@@&t@@} was suggested by Paul Eggert:
8996 @quotation
8997 I should give some credit to the @samp{@@&t@@} pun.  The @samp{&} is my
8998 own invention, but the @samp{t} came from the source code of the
8999 @sc{algol68c} compiler, written by Steve Bourne (of Bourne shell fame),
9000 and which used @samp{mt} to denote the empty string.  In C, it would
9001 have looked like something like:
9003 @example
9004 char const mt[] = "";
9005 @end example
9007 @noindent
9008 but of course the source code was written in Algol 68.
9010 I don't know where he got @samp{mt} from: it could have been his own
9011 invention, and I suppose it could have been a common pun around the
9012 Cambridge University computer lab at the time.
9013 @end quotation
9015 @node Quotation Rule Of Thumb
9016 @subsection Quotation Rule Of Thumb
9018 To conclude, the quotation rule of thumb is:
9020 @center @emph{One pair of quotes per pair of parentheses.}
9022 Never over-quote, never under-quote, in particular in the definition of
9023 macros.  In the few places where the macros need to use brackets
9024 (usually in C program text or regular expressions), properly quote
9025 @emph{the arguments}!
9027 It is common to read Autoconf programs with snippets like:
9029 @example
9030 AC_TRY_LINK(
9031 changequote(<<, >>)dnl
9032 <<#include <time.h>
9033 #ifndef tzname /* For SGI.  */
9034 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9035 #endif>>,
9036 changequote([, ])dnl
9037 [atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
9038 @end example
9040 @noindent
9041 which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
9042 double quoting, so you just need:
9044 @example
9045 AC_TRY_LINK(
9046 [#include <time.h>
9047 #ifndef tzname /* For SGI.  */
9048 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9049 #endif],
9050             [atoi (*tzname);],
9051             [ac_cv_var_tzname=yes],
9052             [ac_cv_var_tzname=no])
9053 @end example
9055 @noindent
9056 The M4-fluent reader will note that these two examples are rigorously
9057 equivalent, since M4 swallows both the @samp{changequote(<<, >>)}
9058 and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
9059 quotes are not part of the arguments!
9061 Simplified, the example above is just doing this:
9063 @example
9064 changequote(<<, >>)dnl
9065 <<[]>>
9066 changequote([, ])dnl
9067 @end example
9069 @noindent
9070 instead of simply:
9072 @example
9073 [[]]
9074 @end example
9076 With macros that do not double quote their arguments (which is the
9077 rule), double-quote the (risky) literals:
9079 @example
9080 AC_LINK_IFELSE([AC_LANG_PROGRAM(
9081 [[#include <time.h>
9082 #ifndef tzname /* For SGI.  */
9083 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9084 #endif]],
9085                                 [atoi (*tzname);])],
9086                [ac_cv_var_tzname=yes],
9087                [ac_cv_var_tzname=no])
9088 @end example
9090 Please note that the macro @code{AC_TRY_LINK} is obsolete, so you really
9091 should be using @code{AC_LINK_IFELSE} instead.
9093 @xref{Quadrigraphs}, for what to do if you run into a hopeless case
9094 where quoting does not suffice.
9096 When you create a @command{configure} script using newly written macros,
9097 examine it carefully to check whether you need to add more quotes in
9098 your macros.  If one or more words have disappeared in the M4
9099 output, you need more quotes.  When in doubt, quote.
9101 However, it's also possible to put on too many layers of quotes.  If
9102 this happens, the resulting @command{configure} script may contain
9103 unexpanded macros.  The @command{autoconf} program checks for this problem
9104 by looking for the string @samp{AC_} in @file{configure}.  However, this
9105 heuristic does not work in general: for example, it does not catch
9106 overquoting in @code{AC_DEFINE} descriptions.
9109 @c ---------------------------------------- Using autom4te
9111 @node Using autom4te
9112 @section Using @command{autom4te}
9114 The Autoconf suite, including M4sugar, M4sh, and Autotest, in addition
9115 to Autoconf per se, heavily rely on M4.  All these different uses
9116 revealed common needs factored into a layer over @command{m4}:
9117 @command{autom4te}@footnote{
9119 Yet another great name from Lars J. Aas.
9123 @command{autom4te} is a preprocessor that is like @command{m4}.
9124 It supports M4 extensions designed for use in tools like Autoconf.
9126 @menu
9127 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
9128 * Customizing autom4te::        Customizing the Autoconf package
9129 @end menu
9131 @node autom4te Invocation
9132 @subsection Invoking @command{autom4te}
9134 The command line arguments are modeled after M4's:
9136 @example
9137 autom4te @var{options} @var{files}
9138 @end example
9140 @noindent
9141 @evindex M4
9142 where the @var{files} are directly passed to @command{m4}.  By default,
9143 @acronym{GNU} m4 is found during configure, but the environment variable
9144 @env{M4} can be set to tell @command{autom4te} where to look.  In addition
9145 to the regular expansion, it handles the replacement of the quadrigraphs
9146 (@pxref{Quadrigraphs}), and of @samp{__oline__}, the current line in the
9147 output.  It supports an extended syntax for the @var{files}:
9149 @table @file
9150 @item @var{file}.m4f
9151 This file is an M4 frozen file.  Note that @emph{all the previous files
9152 are ignored}.  See the option @option{--melt} for the rationale.
9154 @item @var{file}?
9155 If found in the library path, the @var{file} is included for expansion,
9156 otherwise it is ignored instead of triggering a failure.
9157 @end table
9159 @sp 1
9161 Of course, it supports the Autoconf common subset of options:
9163 @table @option
9164 @item --help
9165 @itemx -h
9166 Print a summary of the command line options and exit.
9168 @item --version
9169 @itemx -V
9170 Print the version number of Autoconf and exit.
9172 @item --verbose
9173 @itemx -v
9174 Report processing steps.
9176 @item --debug
9177 @itemx -d
9178 Don't remove the temporary files and be even more verbose.
9180 @item --include=@var{dir}
9181 @itemx -I @var{dir}
9182 Also look for input files in @var{dir}.  Multiple invocations
9183 accumulate.
9185 @item --output=@var{file}
9186 @itemx -o @var{file}
9187 Save output (script or trace) to @var{file}.  The file @option{-} stands
9188 for the standard output.
9189 @end table
9191 @sp 1
9193 As an extension of @command{m4}, it includes the following options:
9195 @table @option
9196 @item --warnings=@var{category}
9197 @itemx -W @var{category}
9198 @evindex WARNINGS
9199 @c FIXME: Point to the M4sugar macros, not Autoconf's.
9200 Report the warnings related to @var{category} (which can actually be a
9201 comma separated list).  @xref{Reporting Messages}, macro
9202 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
9203 values include:
9205 @table @samp
9206 @item all
9207 report all the warnings
9209 @item none
9210 report none
9212 @item error
9213 treats warnings as errors
9215 @item no-@var{category}
9216 disable warnings falling into @var{category}
9217 @end table
9219 Warnings about @samp{syntax} are enabled by default, and the environment
9220 variable @env{WARNINGS}, a comma separated list of categories, is
9221 honored.  @command{autom4te -W @var{category}} will actually
9222 behave as if you had run:
9224 @example
9225 autom4te --warnings=syntax,$WARNINGS,@var{category}
9226 @end example
9228 @noindent
9229 For example, if you want to disable @command{autom4te}'s defaults and
9230 @env{WARNINGS}, but enable the warnings about obsolete
9231 constructs, you would use @option{-W none,obsolete}.
9233 @cindex Back trace
9234 @cindex Macro invocation stack
9235 @command{autom4te} displays a back trace for errors, but not for
9236 warnings; if you want them, just pass @option{-W error}.
9238 @item --melt
9239 @itemx -m
9240 Do not use frozen files.  Any argument @code{@var{file}.m4f} will be
9241 replaced with @code{@var{file}.m4}.  This helps tracing the macros which
9242 are executed only when the files are frozen, typically
9243 @code{m4_define}.  For instance, running:
9245 @example
9246 autom4te --melt 1.m4 2.m4f 3.m4 4.m4f input.m4
9247 @end example
9249 @noindent
9250 is roughly equivalent to running:
9252 @example
9253 m4 1.m4 2.m4 3.m4 4.m4 input.m4
9254 @end example
9256 @noindent
9257 while
9259 @example
9260 autom4te 1.m4 2.m4f 3.m4 4.m4f input.m4
9261 @end example
9263 @noindent
9264 is equivalent to:
9266 @example
9267 m4 --reload-state=4.m4f input.m4
9268 @end example
9270 @item --freeze
9271 @itemx -f
9272 Produce a frozen state file.  @command{autom4te} freezing is stricter
9273 than M4's: it must produce no warnings, and no output other than empty
9274 lines (a line with white space is @emph{not} empty) and comments
9275 (starting with @samp{#}).  Please, note that contrary to @command{m4},
9276 this options takes no argument:
9278 @example
9279 autom4te 1.m4 2.m4 3.m4 --freeze --output=3.m4f
9280 @end example
9282 @noindent
9283 corresponds to
9285 @example
9286 m4 1.m4 2.m4 3.m4 --freeze-state=3.m4f
9287 @end example
9289 @item --mode=@var{octal-mode}
9290 @itemx -m @var{octal-mode}
9291 Set the mode of the non-traces output to @var{octal-mode}; by default
9292 @samp{0666}.
9293 @end table
9295 @sp 1
9297 @cindex @file{autom4te.cache}
9298 As another additional feature over @command{m4}, @command{autom4te}
9299 caches its results.  @acronym{GNU} M4 is able to produce a regular
9300 output and traces at the same time.  Traces are heavily used in the
9301 @acronym{GNU} Build System: @command{autoheader} uses them to build
9302 @file{config.h.in}, @command{autoreconf} to determine what
9303 @acronym{GNU} Build System components are used, @command{automake} to
9304 ``parse'' @file{configure.ac} etc.  To save the long runs of
9305 @command{m4}, traces are cached while performing regular expansion,
9306 and conversely.  This cache is (actually, the caches are) stored in
9307 the directory @file{autom4te.cache}.  @emph{It can safely be removed}
9308 at any moment (especially if for some reason @command{autom4te}
9309 considers it is trashed).
9311 @table @option
9312 @item --cache=@var{directory}
9313 @itemx -C @var{directory}
9314 Specify the name of the directory where the result should be cached.
9315 Passing an empty value disables caching.  Be sure to pass a relative
9316 file name, as for the time being, global caches are not supported.
9318 @item --no-cache
9319 Don't cache the results.
9321 @item --force
9322 @itemx -f
9323 If a cache is used, consider it obsolete (but update it anyway).
9324 @end table
9326 @sp 1
9328 Because traces are so important to the @acronym{GNU} Build System,
9329 @command{autom4te} provides high level tracing features as compared to
9330 M4, and helps exploiting the cache:
9332 @table @option
9333 @item --trace=@var{macro}[:@var{format}]
9334 @itemx -t @var{macro}[:@var{format}]
9335 Trace the invocations of @var{macro} according to the @var{format}.
9336 Multiple @option{--trace} arguments can be used to list several macros.
9337 Multiple @option{--trace} arguments for a single macro are not
9338 cumulative; instead, you should just make @var{format} as long as
9339 needed.
9341 The @var{format} is a regular string, with newlines if desired, and
9342 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}.  It can
9343 use the following special escapes:
9345 @table @samp
9346 @item $$
9347 The character @samp{$}.
9349 @item $f
9350 The file name from which @var{macro} is called.
9352 @item $l
9353 The line number from which @var{macro} is called.
9355 @item $d
9356 The depth of the @var{macro} call.  This is an M4 technical detail that
9357 you probably don't want to know about.
9359 @item $n
9360 The name of the @var{macro}.
9362 @item $@var{num}
9363 The @var{num}th argument of the call to @var{macro}.
9365 @item $@@
9366 @itemx $@var{sep}@@
9367 @itemx $@{@var{separator}@}@@
9368 All the arguments passed to @var{macro}, separated by the character
9369 @var{sep} or the string @var{separator} (@samp{,} by default).  Each
9370 argument is quoted, i.e., enclosed in a pair of square brackets.
9372 @item $*
9373 @itemx $@var{sep}*
9374 @itemx $@{@var{separator}@}*
9375 As above, but the arguments are not quoted.
9377 @item $%
9378 @itemx $@var{sep}%
9379 @itemx $@{@var{separator}@}%
9380 As above, but the arguments are not quoted, all new line characters in
9381 the arguments are smashed, and the default separator is @samp{:}.
9383 The escape @samp{$%} produces single-line trace outputs (unless you put
9384 newlines in the @samp{separator}), while @samp{$@@} and @samp{$*} do
9385 not.
9386 @end table
9388 @xref{autoconf Invocation}, for examples of trace uses.
9390 @item --preselect=@var{macro}
9391 @itemx -p @var{macro}
9392 Cache the traces of @var{macro}, but do not enable traces.  This is
9393 especially important to save CPU cycles in the future.  For instance,
9394 when invoked, @command{autoconf} preselects all the macros that
9395 @command{autoheader}, @command{automake}, @command{autoreconf} etc.@: will
9396 trace, so that running @command{m4} is not needed to trace them: the
9397 cache suffices.  This results in a huge speed-up.
9398 @end table
9400 @sp 1
9402 @cindex Autom4te Library
9403 Finally, @command{autom4te} introduces the concept of @dfn{Autom4te
9404 libraries}.  They consists in a powerful yet extremely simple feature:
9405 sets of combined command line arguments:
9407 @table @option
9408 @item --language=@var{language}
9409 @itemx -l @var{language}
9410 Use the @var{language} Autom4te library.  Current languages include:
9412 @table @code
9413 @item M4sugar
9414 create M4sugar output.
9416 @item M4sh
9417 create M4sh executable shell scripts.
9419 @item Autotest
9420 create Autotest executable test suites.
9422 @item Autoconf-without-aclocal-m4
9423 create Autoconf executable configure scripts without
9424 reading @file{aclocal.m4}.
9426 @item Autoconf
9427 create Autoconf executable configure scripts.  This language inherits
9428 all the characteristics of @code{Autoconf-without-aclocal-m4} and will
9429 additionally read @file{aclocal.m4}.
9430 @end table
9432 @item --prepend-include=@var{dir}
9433 @item -B @var{dir}
9434 Prepend directory @var{dir} to the search path.  This is used to include
9435 the language-specific files before any third-party macros.
9437 @end table
9439 @cindex @file{autom4te.cfg}
9440 As an example, if Autoconf is installed in its default location,
9441 @file{/usr/local}, the command @samp{autom4te -l m4sugar foo.m4} is
9442 strictly equivalent to the command:
9444 @example
9445 autom4te --prepend-include /usr/local/share/autoconf \
9446   m4sugar/m4sugar.m4f --warnings syntax foo.m4
9447 @end example
9449 @noindent
9450 Recursive expansion applies here: the command @samp{autom4te -l m4sh foo.m4}
9451 is the same as @samp{autom4te --language M4sugar m4sugar/m4sh.m4f
9452 foo.m4}, i.e.:
9454 @example
9455 autom4te --prepend-include /usr/local/share/autoconf \
9456   m4sugar/m4sugar.m4f m4sugar/m4sh.m4f --mode 777 foo.m4
9457 @end example
9459 @noindent
9460 The definition of the languages is stored in @file{autom4te.cfg}.
9462 @node Customizing autom4te
9463 @subsection Customizing @command{autom4te}
9465 One can customize @command{autom4te} via @file{~/.autom4te.cfg} (i.e.,
9466 as found in the user home directory), and @file{./.autom4te.cfg} (i.e.,
9467 as found in the directory from which @command{autom4te} is run).  The
9468 order is first reading @file{autom4te.cfg}, then @file{~/.autom4te.cfg},
9469 then @file{./.autom4te.cfg}, and finally the command line arguments.
9471 In these text files, comments are introduced with @code{#}, and empty
9472 lines are ignored.  Customization is performed on a per-language basis,
9473 wrapped in between a @samp{begin-language: "@var{language}"},
9474 @samp{end-language: "@var{language}"} pair.
9476 Customizing a language stands for appending options (@pxref{autom4te
9477 Invocation}) to the current definition of the language.  Options, and
9478 more generally arguments, are introduced by @samp{args:
9479 @var{arguments}}.  You may use the traditional shell syntax to quote the
9480 @var{arguments}.
9482 As an example, to disable Autoconf caches (@file{autom4te.cache})
9483 globally, include the following lines in @file{~/.autom4te.cfg}:
9485 @verbatim
9486 ## ------------------ ##
9487 ## User Preferences.  ##
9488 ## ------------------ ##
9490 begin-language: "Autoconf-without-aclocal-m4"
9491 args: --no-cache
9492 end-language: "Autoconf-without-aclocal-m4"
9493 @end verbatim
9496 @node Programming in M4sugar
9497 @section Programming in M4sugar
9499 @cindex M4sugar
9500 M4 by itself provides only a small, but sufficient, set of all-purpose
9501 macros.  M4sugar introduces additional generic macros.  Its name was
9502 coined by Lars J. Aas: ``Readability And Greater Understanding Stands 4
9503 M4sugar''.
9505 @menu
9506 * Redefined M4 Macros::         M4 builtins changed in M4sugar
9507 * Looping constructs::          Iteration in M4
9508 * Evaluation Macros::           More quotation and evaluation control
9509 * Text processing Macros::      String manipulation in M4
9510 * Forbidden Patterns::          Catching unexpanded macros
9511 @end menu
9513 @node Redefined M4 Macros
9514 @subsection Redefined M4 Macros
9516 @msindex{builtin}
9517 @msindex{decr}
9518 @msindex{define}
9519 @msindex{dumpdef}
9520 @msindex{errprint}
9521 @msindex{esyscmd}
9522 @msindex{eval}
9523 @msindex{format}
9524 @msindex{ifdef}
9525 @msindex{incr}
9526 @msindex{index}
9527 @msindex{indir}
9528 @msindex{len}
9529 @msindex{maketemp}
9530 @msindex{pushdef}
9531 @msindex{shift}
9532 @msindex{substr}
9533 @msindex{syscmd}
9534 @msindex{sysval}
9535 @msindex{translit}
9536 @msindex{undefine}
9537 With a few exceptions, all the M4 native macros are moved in the
9538 @samp{m4_} pseudo-namespace, e.g., M4sugar renames @code{define} as
9539 @code{m4_define} etc.
9541 Some M4 macros are redefined, and are slightly incompatible with their
9542 native equivalent.
9544 @defmac dnl
9545 @msindex{dnl}
9546 This macro kept its original name: no @code{m4_dnl} is defined.
9547 @end defmac
9549 @defmac m4_defn (@var{macro})
9550 @msindex{defn}
9551 Contrary to the M4 builtin, this macro fails if @var{macro} is not
9552 defined.  See @code{m4_undefine}.
9553 @end defmac
9555 @defmac m4_exit (@var{exit-status})
9556 @msindex{exit}
9557 This macro corresponds to @code{m4exit}.
9558 @end defmac
9560 @defmac m4_if (@var{comment})
9561 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @ovar{not-equal})
9562 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @dots{})
9563 @msindex{if}
9564 This macro corresponds to @code{ifelse}.
9565 @end defmac
9567 @defmac m4_include (@var{file})
9568 @defmacx m4_sinclude (@var{file})
9569 @msindex{include}
9570 @msindex{sinclude}
9571 Like the M4 builtins, but warn against multiple inclusions of @var{file}.
9572 @end defmac
9574 @defmac m4_bpatsubst (@var{string}, @var{regexp}, @ovar{replacement})
9575 @msindex{bpatsubst}
9576 This macro corresponds to @code{patsubst}.  The name @code{m4_patsubst}
9577 is kept for future versions of M4sh, on top of @acronym{GNU} M4 which will
9578 provide extended regular expression syntax via @code{epatsubst}.
9579 @end defmac
9581 @defmac m4_popdef (@var{macro})
9582 @msindex{popdef}
9583 Contrary to the M4 builtin, this macro fails if @var{macro} is not
9584 defined.  See @code{m4_undefine}.
9585 @end defmac
9587 @defmac m4_bregexp (@var{string}, @var{regexp}, @ovar{replacement})
9588 @msindex{bregexp}
9589 This macro corresponds to @code{regexp}.  The name @code{m4_regexp}
9590 is kept for future versions of M4sh, on top of @acronym{GNU} M4 which will
9591 provide extended regular expression syntax via @code{eregexp}.
9592 @end defmac
9594 @defmac m4_wrap (@var{text})
9595 @msindex{wrap}
9596 This macro corresponds to @code{m4wrap}.
9598 You are encouraged to end @var{text} with @samp{[]}, so that there are
9599 no risks that two consecutive invocations of @code{m4_wrap} result in an
9600 unexpected pasting of tokens, as in
9602 @example
9603 m4_define([foo], [Foo])
9604 m4_define([bar], [Bar])
9605 m4_define([foobar], [FOOBAR])
9606 m4_wrap([bar])
9607 m4_wrap([foo])
9608 @result{}FOOBAR
9609 @end example
9610 @end defmac
9612 @defmac m4_undefine (@var{macro})
9613 @msindex{undefine}
9614 Contrary to the M4 builtin, this macro fails if @var{macro} is not
9615 defined.  Use
9617 @example
9618 m4_ifdef([@var{macro}], [m4_undefine([@var{macro}])])
9619 @end example
9621 @noindent
9622 to recover the behavior of the builtin.
9623 @end defmac
9627 @node Looping constructs
9628 @subsection Looping constructs
9630 The following macros implement loops in M4.
9632 @defmac m4_for (@var{var}, @var{first}, @var{last}, @ovar{step}, @var{expression})
9633 @msindex{for}
9634 Loop over the numeric values between @var{first} and @var{last}
9635 including bounds by increments of @var{step}.  For each iteration,
9636 expand @var{expression} with the numeric value assigned to @var{var}.
9637 If @var{step} is omitted, it defaults to @samp{1} or @samp{-1} depending
9638 on the order of the limits.  If given, @var{step} has to match this
9639 order.
9640 @end defmac
9642 @defmac m4_foreach (@var{var}, @var{list}, @var{expression})
9643 @msindex{foreach}
9644 Loop over the comma-separated m4 list @var{list}, assigning each value
9645 to @var{var}, and expand @var{expression}.  The following example will
9646 output two lines:
9648 @example
9649 m4_foreach([myvar], [[foo], [bar, baz]],
9650            [echo myvar
9653 @end example
9654 @end defmac
9656 @defmac m4_foreach_w (@var{var}, @var{list}, @var{expression})
9657 @msindex{foreach_w}
9658 Loop over the whitespace-separated list @var{list}, assigning each value
9659 to @var{var}, and expand @var{expression}.
9661 The deprecated macro @code{AC_FOREACH} is an alias of
9662 @code{m4_foreach_w}.
9663 @end defmac
9667 @node Evaluation Macros
9668 @subsection Evaluation Macros
9670 The following macros give some control over the order of the evaluation
9671 by adding or removing levels of quotes.  They are meant for hard-core M4
9672 programmers.
9674 @defmac m4_dquote (@var{arg1}, @dots{})
9675 @msindex{dquote}
9676 Return the arguments as a quoted list of quoted arguments.
9677 @end defmac
9679 @defmac m4_quote (@var{arg1}, @dots{})
9680 @msindex{quote}
9681 Return the arguments as a single entity, i.e., wrap them into a pair of
9682 quotes.
9683 @end defmac
9685 The following example aims at emphasizing the difference between (i), not
9686 using these macros, (ii), using @code{m4_quote}, and (iii), using
9687 @code{m4_dquote}.
9689 @example
9690 $ @kbd{cat example.m4}
9691 # Overquote, so that quotes are visible.
9692 m4_define([show], [$[]1 = [$1], $[]@@ = [$@@]])
9693 m4_divert(0)dnl
9694 show(a, b)
9695 show(m4_quote(a, b))
9696 show(m4_dquote(a, b))
9697 $ @kbd{autom4te -l m4sugar example.m4}
9698 $1 = a, $@@ = [a],[b]
9699 $1 = a,b, $@@ = [a,b]
9700 $1 = [a],[b], $@@ = [[a],[b]]
9701 @end example
9705 @node Text processing Macros
9706 @subsection Text processing Macros
9708 The following macros may be used to manipulate strings in M4.
9709 They are not intended for casual use.
9711 @defmac m4_re_escape (@var{string})
9712 @msindex{re_escape}
9713 Backslash-escape all characters in @var{string} that are active in
9714 regexps.
9715 @end defmac
9717 @defmac m4_tolower (@var{string})
9718 @defmacx m4_toupper (@var{string})
9719 @msindex{tolower}
9720 @msindex{toupper}
9721 Return @var{string} with letters converted to upper or lower case,
9722 respectively.
9723 @end defmac
9725 @defmac m4_split (@var{string}, @ovar{regexp})
9726 @msindex{split}
9727 Split @var{string} into an M4 list of elements quoted by @samp{[} and
9728 @samp{]}, while keeping white space at the beginning and at the end.
9729 If @var{regexp} is given, use it instead of @samp{[\t ]+} for splitting.
9730 If @var{string} is empty, the result is an empty list.
9731 @end defmac
9733 @defmac m4_normalize (@var{string})
9734 @msindex{normalize}
9735 Remove leading and trailing spaces and tabs, sequences of
9736 backslash-then-newline, and replace multiple spaces and tabs with a
9737 single space.
9738 @end defmac
9740 @defmac m4_append (@var{macro-name}, @var{string}, @ovar{separator})
9741 @defmacx m4_append_uniq (@var{macro-name}, @var{string}, @ovar{separator})
9742 @msindex{append}
9743 @msindex{append_uniq}
9744 Redefine @var{macro-name} to its former contents with @var{separator}
9745 and @var{string} added at the end.  If @var{macro-name} was undefined
9746 before (but not if it was defined but empty), then no @var{separator} is
9747 added.  @code{m4_append} can be used to grow strings, and
9748 @code{m4_append_uniq} to grow strings without duplicating substrings.
9749 @end defmac
9753 @node Forbidden Patterns
9754 @subsection Forbidden Patterns
9755 @cindex Forbidden patterns
9756 @cindex Patterns, forbidden
9758 M4sugar provides a means to define suspicious patterns, patterns
9759 describing tokens which should not be found in the output.  For
9760 instance, if an Autoconf @file{configure} script includes tokens such as
9761 @samp{AC_DEFINE}, or @samp{dnl}, then most probably something went
9762 wrong (typically a macro was not evaluated because of overquotation).
9764 M4sugar forbids all the tokens matching @samp{^m4_} and @samp{^dnl$}.
9766 @defmac m4_pattern_forbid (@var{pattern})
9767 @msindex{pattern_forbid}
9768 Declare that no token matching @var{pattern} must be found in the output.
9769 Comments are not checked; this can be a problem if, for instance, you
9770 have some macro left unexpanded after an @samp{#include}.  No consensus
9771 is currently found in the Autoconf community, as some people consider it
9772 should be valid to name macros in comments (which doesn't make sense to
9773 the author of this documentation, as @samp{#}-comments should document
9774 the output, not the input, documented by @samp{dnl} comments).
9775 @end defmac
9777 Of course, you might encounter exceptions to these generic rules, for
9778 instance you might have to refer to @samp{$m4_flags}.
9780 @defmac m4_pattern_allow (@var{pattern})
9781 @msindex{pattern_allow}
9782 Any token matching @var{pattern} is allowed, including if it matches an
9783 @code{m4_pattern_forbid} pattern.
9784 @end defmac
9786 @node Programming in M4sh
9787 @section Programming in M4sh
9789 @c FIXME: Eventually will become a chapter, as it is not related to
9790 @c programming in M4 per se.
9792 M4sh, pronounced ``mash'', is aiming at producing portable Bourne shell
9793 scripts.  This name was coined by Lars J. Aas, who notes that,
9794 according to the Webster's Revised Unabridged Dictionary (1913):
9796 @quotation
9797 Mash \Mash\, n.  [Akin to G. meisch, maisch, meische, maische, mash,
9798 wash, and prob.@: to AS. miscian to mix.  See ``Mix''.]
9800 @enumerate 1
9801 @item
9802 A mass of mixed ingredients reduced to a soft pulpy state by beating or
9803 pressure@enddots{}
9805 @item
9806 A mixture of meal or bran and water fed to animals.
9808 @item
9809 A mess; trouble.  [Obs.] --Beau.@: & Fl.
9810 @end enumerate
9811 @end quotation
9814 For the time being, it is not mature enough to be widely used.
9816 M4sh provides portable alternatives for some common shell constructs
9817 that unfortunately are not portable in practice.
9819 @c Deprecated, to be replaced by a better API
9820 @ignore
9821 @defmac AS_BASENAME (@var{file-name})
9822 @asindex{BASENAME}
9823 Output the non-directory portion of @var{file-name}.  For example,
9824 if @code{$file} is @samp{/one/two/three}, the command
9825 @code{base=`AS_BASENAME(["$file"])`} sets @code{base} to @samp{three}.
9826 @end defmac
9827 @end ignore
9829 @defmac AS_BOURNE_COMPATIBLE
9830 @asindex{BOURNE_COMPATIBLE}
9831 Set up the shell to be more compatible with the Bourne shell as
9832 standardized by Posix, if possible.  This may involve setting
9833 environment variables, or setting options, or similar
9834 implementation-specific actions.
9835 @end defmac
9837 @defmac AS_CASE (@var{word}, @ovar{pattern1}, @ovar{if-matched1}, @dots{}, @ovar{default})
9838 @asindex{CASE}
9839 Expand into a shell @samp{case} statement, where @var{word} is matched
9840 against one or more patterns.  @var{if-matched} is run if the
9841 corresponding pattern matched @var{word}, else @var{default} is run.
9842 @end defmac
9844 @defmac AS_DIRNAME (@var{file-name})
9845 @asindex{DIRNAME}
9846 Output the directory portion of @var{file-name}.  For example,
9847 if @code{$file} is @samp{/one/two/three}, the command
9848 @code{dir=`AS_DIRNAME(["$file"])`} sets @code{dir} to @samp{/one/two}.
9849 @end defmac
9851 @defmac AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{}, @ovar{run-if-false})
9852 @asindex{IF}
9853 Run shell code @var{test1}.  If @var{test1} exits with a zero status then
9854 run shell code @var{run-if-true1}, else examine further tests.  If no test
9855 exits with a zero status, run shell code @var{run-if-false}, with
9856 simplifications if either @var{run-if-true1} or @var{run-if-false1}
9857 is empty.  For example,
9859 @example
9860 AS_IF([test "$foo" = yes], [HANDLE_FOO([yes])],
9861       [test "$foo" != no], [HANDLE_FOO([maybe])],
9862       [echo foo not specified])
9863 @end example
9865 @noindent
9866 will make sure any @code{AC_REQUIRE}'s macros of @code{HANDLE_FOO} will
9867 be expanded before the first test.
9868 @end defmac
9870 @defmac AS_MKDIR_P (@var{file-name})
9871 @asindex{MKDIR_P}
9872 Make the directory @var{file-name}, including intervening directories
9873 as necessary.  This is equivalent to @samp{mkdir -p @var{file-name}},
9874 except that it is portable to older versions of @command{mkdir} that
9875 lack support for the @option{-p} option.  Also, @code{AS_MKDIR_P}
9876 succeeds if @var{file-name} is a symbolic link to an existing directory,
9877 even though Posix is unclear whether @samp{mkdir -p} should
9878 succeed in that case.  If creation of @var{file-name} fails, exit the
9879 script.
9881 Also see the @code{AC_PROG_MKDIR_P} macro (@pxref{Particular Programs}).
9882 @end defmac
9884 @defmac AS_SHELL_SANITIZE
9885 @asindex{SHELL_SANITIZE}
9886 Initialize the shell suitably for @code{configure} scripts.  This has
9887 the effect of @code{AS_BOURNE_COMPATIBLE}, and sets some other
9888 environment variables for predictable results from configuration tests.
9889 For example, it sets @env{LC_ALL} to change to the default C locale.
9890 @xref{Special Shell Variables}.
9891 @end defmac
9893 @defmac AS_TR_CPP (@var{expression})
9894 @asindex{TR_CPP}
9895 Transform @var{expression} into a valid right-hand side for a C @code{#define}.
9896 For example:
9898 @example
9899 # This outputs "#define HAVE_CHAR_P 1".
9900 type="char *"
9901 echo "#define AS_TR_CPP([HAVE_$type]) 1"
9902 @end example
9903 @end defmac
9905 @defmac AS_TR_SH (@var{expression})
9906 @asindex{TR_SH}
9907 Transform @var{expression} into a valid shell variable name.  For example:
9909 @example
9910 # This outputs "Have it!".
9911 header="sys/some file.h"
9912 AS_TR_SH([HAVE_$header])=yes
9913 if test "$HAVE_sys_some_file_h" = yes; then echo "Have it!"; fi
9914 @end example
9915 @end defmac
9917 @defmac AS_SET_CATFILE (@var{var}, @var{dir}, @var{file})
9918 @asindex{SET_CATFILE}
9919 Set the shell variable @var{var} to @var{dir}/@var{file}, but
9920 optimizing the common cases (@var{dir} or @var{file} is @samp{.},
9921 @var{file} is absolute, etc.).
9922 @end defmac
9925 @node File Descriptor Macros
9926 @section File Descriptor Macros
9927 @cindex input
9928 @cindex standard input
9929 @cindex file descriptors
9930 @cindex descriptors
9931 @cindex low-level output
9932 @cindex output, low-level
9934 The following macros define file descriptors used to output messages
9935 (or input values) from @file{configure} scripts.
9936 For example:
9938 @example
9939 echo "$wombats found" >&AS_MESSAGE_LOG_FD
9940 echo 'Enter desired kangaroo count:' >&AS_MESSAGE_FD
9941 read kangaroos <&AS_ORIGINAL_STDIN_FD`
9942 @end example
9944 @noindent
9945 However doing so is seldom needed, because Autoconf provides higher
9946 level macros as described below.
9948 @defmac AS_MESSAGE_FD
9949 @asindex{MESSAGE_FD}
9950 The file descriptor for @samp{checking for...}  messages and results.
9951 Normally this directs messages to the standard output, however when
9952 @command{configure} is run with the @option{-q} option, messages sent to
9953 @code{AS_MESSAGE_FD} will be discarded.
9955 If you want to display some messages, consider using one of the printing
9956 macros (@pxref{Printing Messages}) instead.  Copies of messages output
9957 via these macros will additionally be recorded in @file{config.log}.
9958 @end defmac
9960 @defmac AS_MESSAGE_LOG_FD
9961 @asindex{MESSAGE_LOG_FD}
9963 The file descriptor for messages logged to @file{config.log}.  Macros
9964 that run tools, like @code{AC_COMPILE_IFELSE} (@pxref{Running the
9965 Compiler}), redirect all output to this descriptor.  You may want to do
9966 so if you develop such a low-level macro.
9967 @end defmac
9969 @defmac AS_ORIGINAL_STDIN_FD
9970 @asindex{ORIGINAL_STDIN_FD}
9971 The file descriptor for the original standard input.
9973 When @command{configure} runs, it may accidentally execute an
9974 interactive command that has the same name as the non-interactive meant
9975 to be used or checked.  If the standard input was the terminal, such
9976 interactive programs would cause @command{configure} to stop, pending
9977 some user input.  Therefore @command{configure} redirects its standard
9978 input from @file{/dev/null} during its initialization.  This is not
9979 normally a problem, since @command{configure} normally does not need
9980 user input.
9982 In the extreme case where your @file{configure} script really needs to
9983 obtain some values from the original standard input, you can read them
9984 explicitly from @code{AS_ORIGINAL_STDIN_FD}.
9985 @end defmac
9988 @c =================================================== Writing Autoconf Macros.
9990 @node Writing Autoconf Macros
9991 @chapter Writing Autoconf Macros
9993 When you write a feature test that could be applicable to more than one
9994 software package, the best thing to do is encapsulate it in a new macro.
9995 Here are some instructions and guidelines for writing Autoconf macros.
9997 @menu
9998 * Macro Definitions::           Basic format of an Autoconf macro
9999 * Macro Names::                 What to call your new macros
10000 * Reporting Messages::          Notifying @command{autoconf} users
10001 * Dependencies Between Macros::  What to do when macros depend on other macros
10002 * Obsoleting Macros::           Warning about old ways of doing things
10003 * Coding Style::                Writing Autoconf macros @`a la Autoconf
10004 @end menu
10006 @node Macro Definitions
10007 @section Macro Definitions
10009 @acindex{DEFUN}
10010 Autoconf macros are defined using the @code{AC_DEFUN} macro, which is
10011 similar to the M4 builtin @code{m4_define} macro.  In addition to
10012 defining a macro, @code{AC_DEFUN} adds to it some code that is used to
10013 constrain the order in which macros are called (@pxref{Prerequisite
10014 Macros}).
10016 An Autoconf macro definition looks like this:
10018 @example
10019 AC_DEFUN(@var{macro-name}, @var{macro-body})
10020 @end example
10022 You can refer to any arguments passed to the macro as @samp{$1},
10023 @samp{$2}, etc.  @xref{Definitions, , How to define new macros, m4.info,
10024 @acronym{GNU} m4}, for more complete information on writing M4 macros.
10026 Be sure to properly quote both the @var{macro-body} @emph{and} the
10027 @var{macro-name} to avoid any problems if the macro happens to have
10028 been previously defined.
10030 Each macro should have a header comment that gives its prototype, and a
10031 brief description.  When arguments have default values, display them in
10032 the prototype.  For example:
10034 @example
10035 # AC_MSG_ERROR(ERROR, [EXIT-STATUS = 1])
10036 # --------------------------------------
10037 m4_define([AC_MSG_ERROR],
10038   [@{ AS_MESSAGE([error: $1], [2])
10039      exit m4_default([$2], [1]); @}])
10040 @end example
10042 Comments about the macro should be left in the header comment.  Most
10043 other comments will make their way into @file{configure}, so just keep
10044 using @samp{#} to introduce comments.
10046 @cindex @code{dnl}
10047 If you have some very special comments about pure M4 code, comments
10048 that make no sense in @file{configure} and in the header comment, then
10049 use the builtin @code{dnl}: it causes M4 to discard the text
10050 through the next newline.
10052 Keep in mind that @code{dnl} is rarely needed to introduce comments;
10053 @code{dnl} is more useful to get rid of the newlines following macros
10054 that produce no output, such as @code{AC_REQUIRE}.
10057 @node Macro Names
10058 @section Macro Names
10060 All of the Autoconf macros have all-uppercase names starting with
10061 @samp{AC_} to prevent them from accidentally conflicting with other
10062 text.  All shell variables that they use for internal purposes have
10063 mostly-lowercase names starting with @samp{ac_}.  To ensure that your
10064 macros don't conflict with present or future Autoconf macros, you should
10065 prefix your own macro names and any shell variables they use with some
10066 other sequence.  Possibilities include your initials, or an abbreviation
10067 for the name of your organization or software package.
10069 Most of the Autoconf macros' names follow a structured naming convention
10070 that indicates the kind of feature check by the name.  The macro names
10071 consist of several words, separated by underscores, going from most
10072 general to most specific.  The names of their cache variables use the
10073 same convention (@pxref{Cache Variable Names}, for more information on
10074 them).
10076 The first word of the name after @samp{AC_} usually tells the category
10077 of the feature being tested.  Here are the categories used in Autoconf for
10078 specific test macros, the kind of macro that you are more likely to
10079 write.  They are also used for cache variables, in all-lowercase.  Use
10080 them where applicable; where they're not, invent your own categories.
10082 @table @code
10083 @item C
10084 C language builtin features.
10085 @item DECL
10086 Declarations of C variables in header files.
10087 @item FUNC
10088 Functions in libraries.
10089 @item GROUP
10090 Posix group owners of files.
10091 @item HEADER
10092 Header files.
10093 @item LIB
10094 C libraries.
10095 @item PATH
10096 Absolute names of files, including programs.
10097 @item PROG
10098 The base names of programs.
10099 @item MEMBER
10100 Members of aggregates.
10101 @item SYS
10102 Operating system features.
10103 @item TYPE
10104 C builtin or declared types.
10105 @item VAR
10106 C variables in libraries.
10107 @end table
10109 After the category comes the name of the particular feature being
10110 tested.  Any further words in the macro name indicate particular aspects
10111 of the feature.  For example, @code{AC_FUNC_FNMATCH_GNU} checks whether
10112 the @code{fnmatch} function supports @acronym{GNU} extensions.
10114 An internal macro should have a name that starts with an underscore;
10115 Autoconf internals should therefore start with @samp{_AC_}.
10116 Additionally, a macro that is an internal subroutine of another macro
10117 should have a name that starts with an underscore and the name of that
10118 other macro, followed by one or more words saying what the internal
10119 macro does.  For example, @code{AC_PATH_X} has internal macros
10120 @code{_AC_PATH_X_XMKMF} and @code{_AC_PATH_X_DIRECT}.
10122 @node Reporting Messages
10123 @section Reporting Messages
10124 @cindex Messages, from @command{autoconf}
10126 When macros statically diagnose abnormal situations, benign or fatal,
10127 they should report them using these macros.  For dynamic issues, i.e.,
10128 when @command{configure} is run, see @ref{Printing Messages}.
10130 @defmac AC_DIAGNOSE (@var{category}, @var{message})
10131 @acindex{DIAGNOSE}
10132 Report @var{message} as a warning (or as an error if requested by the
10133 user) if warnings of the @var{category} are turned on.  You are
10134 encouraged to use standard categories, which currently include:
10136 @table @samp
10137 @item all
10138 messages that don't fall into one of the following categories.  Use of an
10139 empty @var{category} is equivalent.
10141 @item cross
10142 related to cross compilation issues.
10144 @item obsolete
10145 use of an obsolete construct.
10147 @item syntax
10148 dubious syntactic constructs, incorrectly ordered macro calls.
10149 @end table
10150 @end defmac
10152 @defmac AC_WARNING (@var{message})
10153 @acindex{WARNING}
10154 Equivalent to @samp{AC_DIAGNOSE([syntax], @var{message})}, but you are
10155 strongly encouraged to use a finer grained category.
10156 @end defmac
10158 @defmac AC_FATAL (@var{message})
10159 @acindex{FATAL}
10160 Report a severe error @var{message}, and have @command{autoconf} die.
10161 @end defmac
10163 When the user runs @samp{autoconf -W error}, warnings from
10164 @code{AC_DIAGNOSE} and @code{AC_WARNING} are reported as error, see
10165 @ref{autoconf Invocation}.
10167 @node Dependencies Between Macros
10168 @section Dependencies Between Macros
10169 @cindex Dependencies between macros
10171 Some Autoconf macros depend on other macros having been called first in
10172 order to work correctly.  Autoconf provides a way to ensure that certain
10173 macros are called if needed and a way to warn the user if macros are
10174 called in an order that might cause incorrect operation.
10176 @menu
10177 * Prerequisite Macros::         Ensuring required information
10178 * Suggested Ordering::          Warning about possible ordering problems
10179 * One-Shot Macros::             Ensuring a macro is called only once
10180 @end menu
10182 @node Prerequisite Macros
10183 @subsection Prerequisite Macros
10184 @cindex Prerequisite macros
10185 @cindex Macros, prerequisites
10187 A macro that you write might need to use values that have previously
10188 been computed by other macros.  For example, @code{AC_DECL_YYTEXT}
10189 examines the output of @code{flex} or @code{lex}, so it depends on
10190 @code{AC_PROG_LEX} having been called first to set the shell variable
10191 @code{LEX}.
10193 Rather than forcing the user of the macros to keep track of the
10194 dependencies between them, you can use the @code{AC_REQUIRE} macro to do
10195 it automatically.  @code{AC_REQUIRE} can ensure that a macro is only
10196 called if it is needed, and only called once.
10198 @defmac AC_REQUIRE (@var{macro-name})
10199 @acindex{REQUIRE}
10200 If the M4 macro @var{macro-name} has not already been called, call it
10201 (without any arguments).  Make sure to quote @var{macro-name} with
10202 square brackets.  @var{macro-name} must have been defined using
10203 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
10204 that it has been called.
10206 @code{AC_REQUIRE} must be used inside an @code{AC_DEFUN}'d macro; it
10207 must not be called from the top level.
10208 @end defmac
10210 @code{AC_REQUIRE} is often misunderstood.  It really implements
10211 dependencies between macros in the sense that if one macro depends upon
10212 another, the latter will be expanded @emph{before} the body of the
10213 former.  To be more precise, the required macro will be expanded before
10214 the outermost @code{AC_DEFUN}'d macro in the current expansion stack.
10215 In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
10216 @code{FOO}.  For instance, this definition of macros:
10218 @example
10219 @group
10220 AC_DEFUN([TRAVOLTA],
10221 [test "$body_temperature_in_celsius" -gt "38" &&
10222   dance_floor=occupied])
10223 AC_DEFUN([NEWTON_JOHN],
10224 [test "$hair_style" = "curly" &&
10225   dance_floor=occupied])
10226 @end group
10228 @group
10229 AC_DEFUN([RESERVE_DANCE_FLOOR],
10230 [if date | grep '^Sat.*pm' >/dev/null 2>&1; then
10231   AC_REQUIRE([TRAVOLTA])
10232   AC_REQUIRE([NEWTON_JOHN])
10233 fi])
10234 @end group
10235 @end example
10237 @noindent
10238 with this @file{configure.ac}
10240 @example
10241 AC_INIT([Dance Manager], [1.0], [bug-dance@@example.org])
10242 RESERVE_DANCE_FLOOR
10243 if test "$dance_floor" = occupied; then
10244   AC_MSG_ERROR([cannot pick up here, let's move])
10246 @end example
10248 @noindent
10249 will not leave you with a better chance to meet a kindred soul at
10250 other times than Saturday night since it expands into:
10252 @example
10253 @group
10254 test "$body_temperature_in_Celsius" -gt "38" &&
10255   dance_floor=occupied
10256 test "$hair_style" = "curly" &&
10257   dance_floor=occupied
10259 if date | grep '^Sat.*pm' >/dev/null 2>&1; then
10263 @end group
10264 @end example
10266 This behavior was chosen on purpose: (i) it prevents messages in
10267 required macros from interrupting the messages in the requiring macros;
10268 (ii) it avoids bad surprises when shell conditionals are used, as in:
10270 @example
10271 @group
10272 if @dots{}; then
10273   AC_REQUIRE([SOME_CHECK])
10275 @dots{}
10276 SOME_CHECK
10277 @end group
10278 @end example
10280 The helper macros @code{AS_IF} and @code{AS_CASE} may be used to
10281 enforce expansion of required macros outside of shell conditional
10282 constructs.  You are furthermore encouraged to put all @code{AC_REQUIRE}s
10283 at the beginning of a macro.  You can use @code{dnl} to avoid the empty
10284 lines they leave.
10286 @node Suggested Ordering
10287 @subsection Suggested Ordering
10288 @cindex Macros, ordering
10289 @cindex Ordering macros
10291 Some macros should be run before another macro if both are called, but
10292 neither @emph{requires} that the other be called.  For example, a macro
10293 that changes the behavior of the C compiler should be called before any
10294 macros that run the C compiler.  Many of these dependencies are noted in
10295 the documentation.
10297 Autoconf provides the @code{AC_BEFORE} macro to warn users when macros
10298 with this kind of dependency appear out of order in a
10299 @file{configure.ac} file.  The warning occurs when creating
10300 @command{configure} from @file{configure.ac}, not when running
10301 @command{configure}.
10303 For example, @code{AC_PROG_CPP} checks whether the C compiler
10304 can run the C preprocessor when given the @option{-E} option.  It should
10305 therefore be called after any macros that change which C compiler is
10306 being used, such as @code{AC_PROG_CC}.  So @code{AC_PROG_CC} contains:
10308 @example
10309 AC_BEFORE([$0], [AC_PROG_CPP])dnl
10310 @end example
10312 @noindent
10313 This warns the user if a call to @code{AC_PROG_CPP} has already occurred
10314 when @code{AC_PROG_CC} is called.
10316 @defmac AC_BEFORE (@var{this-macro-name}, @var{called-macro-name})
10317 @acindex{BEFORE}
10318 Make M4 print a warning message to the standard error output if
10319 @var{called-macro-name} has already been called.  @var{this-macro-name}
10320 should be the name of the macro that is calling @code{AC_BEFORE}.  The
10321 macro @var{called-macro-name} must have been defined using
10322 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
10323 that it has been called.
10324 @end defmac
10326 @node One-Shot Macros
10327 @subsection One-Shot Macros
10328 @cindex One-shot macros
10329 @cindex Macros, called once
10331 Some macros should be called only once, either because calling them
10332 multiple time is unsafe, or because it is bad style.  For instance
10333 Autoconf ensures that @code{AC_CANONICAL_BUILD} and cousins
10334 (@pxref{Canonicalizing}) are evaluated only once, because it makes no
10335 sense to run these expensive checks more than once.  Such one-shot
10336 macros can be defined using @code{AC_DEFUN_ONCE}.
10338 @defmac AC_DEFUN_ONCE (@var{macro-name}, @var{macro-body})
10339 @acindex{DEFUN_ONCE}
10341 Declare macro @var{macro-name} like @code{AC_DEFUN} would (@pxref{Macro
10342 Definitions}), and emit a warning any time the macro is called more than
10343 once.
10344 @end defmac
10346 Obviously it is not sensible to evaluate a macro defined by
10347 @code{AC_DEFUN_ONCE} in a macro defined by @code{AC_DEFUN}, most of the
10348 times you will want to use @code{AC_REQUIRE} (@pxref{Prerequisite
10349 Macros}).
10351 @node Obsoleting Macros
10352 @section Obsoleting Macros
10353 @cindex Obsoleting macros
10354 @cindex Macros, obsoleting
10356 Configuration and portability technology has evolved over the years.
10357 Often better ways of solving a particular problem are developed, or
10358 ad-hoc approaches are systematized.  This process has occurred in many
10359 parts of Autoconf.  One result is that some of the macros are now
10360 considered @dfn{obsolete}; they still work, but are no longer considered
10361 the best thing to do, hence they should be replaced with more modern
10362 macros.  Ideally, @command{autoupdate} should replace the old macro calls
10363 with their modern implementation.
10365 Autoconf provides a simple means to obsolete a macro.
10367 @defmac AU_DEFUN (@var{old-macro}, @var{implementation}, @ovar{message})
10368 @auindex{DEFUN}
10369 Define @var{old-macro} as @var{implementation}.  The only difference
10370 with @code{AC_DEFUN} is that the user will be warned that
10371 @var{old-macro} is now obsolete.
10373 If she then uses @command{autoupdate}, the call to @var{old-macro} will be
10374 replaced by the modern @var{implementation}.  @var{message} should
10375 include information on what to do after running @command{autoupdate};
10376 @command{autoupdate} will print it as a warning, and include it
10377 in the updated @file{configure.ac} file.
10379 The details of this macro are hairy: if @command{autoconf} encounters an
10380 @code{AU_DEFUN}ed macro, all macros inside its second argument are expanded
10381 as usual.  However, when @command{autoupdate} is run, only M4 and M4sugar
10382 macros will be expanded here, while all other macros are disabled and will
10383 appear literally in the updated @file{configure.ac}.
10384 @end defmac
10386 @defmac AU_ALIAS (@var{old-name}, @var{new-name})
10387 @auindex{ALIAS}
10388 Used if the @var{old-name} is to be replaced by a call to @var{new-macro}
10389 with the same parameters.  This happens for example if the macro was renamed.
10390 @end defmac
10392 @node Coding Style
10393 @section Coding Style
10394 @cindex Coding style
10396 The Autoconf macros follow a strict coding style.  You are encouraged to
10397 follow this style, especially if you intend to distribute your macro,
10398 either by contributing it to Autoconf itself, or via other means.
10400 The first requirement is to pay great attention to the quotation.  For
10401 more details, see @ref{Autoconf Language}, and @ref{M4 Quotation}.
10403 Do not try to invent new interfaces.  It is likely that there is a macro
10404 in Autoconf that resembles the macro you are defining: try to stick to
10405 this existing interface (order of arguments, default values, etc.).  We
10406 @emph{are} conscious that some of these interfaces are not perfect;
10407 nevertheless, when harmless, homogeneity should be preferred over
10408 creativity.
10410 Be careful about clashes both between M4 symbols and between shell
10411 variables.
10413 If you stick to the suggested M4 naming scheme (@pxref{Macro Names}),
10414 you are unlikely to generate conflicts.  Nevertheless, when you need to
10415 set a special value, @emph{avoid using a regular macro name}; rather,
10416 use an ``impossible'' name.  For instance, up to version 2.13, the macro
10417 @code{AC_SUBST} used to remember what @var{symbol}s were already defined
10418 by setting @code{AC_SUBST_@var{symbol}}, which is a regular macro name.
10419 But since there is a macro named @code{AC_SUBST_FILE}, it was just
10420 impossible to @samp{AC_SUBST(FILE)}!  In this case,
10421 @code{AC_SUBST(@var{symbol})} or @code{_AC_SUBST(@var{symbol})} should
10422 have been used (yes, with the parentheses).
10423 @c or better yet, high-level macros such as @code{m4_expand_once}
10425 No Autoconf macro should ever enter the user-variable name space; i.e.,
10426 except for the variables that are the actual result of running the
10427 macro, all shell variables should start with @code{ac_}.  In
10428 addition, small macros or any macro that is likely to be embedded in
10429 other macros should be careful not to use obvious names.
10431 @cindex @code{dnl}
10432 Do not use @code{dnl} to introduce comments: most of the comments you
10433 are likely to write are either header comments which are not output
10434 anyway, or comments that should make their way into @file{configure}.
10435 There are exceptional cases where you do want to comment special M4
10436 constructs, in which case @code{dnl} is right, but keep in mind that it
10437 is unlikely.
10439 M4 ignores the leading blanks and newlines before each argument.
10440 Use this feature to
10441 indent in such a way that arguments are (more or less) aligned with the
10442 opening parenthesis of the macro being called.  For instance, instead of
10444 @example
10445 AC_CACHE_CHECK(for EMX OS/2 environment,
10446 ac_cv_emxos2,
10447 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [return __EMX__;])],
10448 [ac_cv_emxos2=yes], [ac_cv_emxos2=no])])
10449 @end example
10451 @noindent
10452 write
10454 @example
10455 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
10456 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
10457                    [ac_cv_emxos2=yes],
10458                    [ac_cv_emxos2=no])])
10459 @end example
10461 @noindent
10462 or even
10464 @example
10465 AC_CACHE_CHECK([for EMX OS/2 environment],
10466                [ac_cv_emxos2],
10467                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
10468                                                    [return __EMX__;])],
10469                                   [ac_cv_emxos2=yes],
10470                                   [ac_cv_emxos2=no])])
10471 @end example
10473 When using @code{AC_RUN_IFELSE} or any macro that cannot work when
10474 cross-compiling, provide a pessimistic value (typically @samp{no}).
10476 Feel free to use various tricks to prevent auxiliary tools, such as
10477 syntax-highlighting editors, from behaving improperly.  For instance,
10478 instead of:
10480 @example
10481 m4_bpatsubst([$1], [$"])
10482 @end example
10484 @noindent
10487 @example
10488 m4_bpatsubst([$1], [$""])
10489 @end example
10491 @noindent
10492 so that Emacsen do not open an endless ``string'' at the first quote.
10493 For the same reasons, avoid:
10495 @example
10496 test $[#] != 0
10497 @end example
10499 @noindent
10500 and use:
10502 @example
10503 test $[@@%:@@] != 0
10504 @end example
10506 @noindent
10507 Otherwise, the closing bracket would be hidden inside a @samp{#}-comment,
10508 breaking the bracket-matching highlighting from Emacsen.  Note the
10509 preferred style to escape from M4: @samp{$[1]}, @samp{$[@@]}, etc.  Do
10510 not escape when it is unnecessary.  Common examples of useless quotation
10511 are @samp{[$]$1} (write @samp{$$1}), @samp{[$]var} (use @samp{$var}),
10512 etc.  If you add portability issues to the picture, you'll prefer
10513 @samp{$@{1+"$[@@]"@}} to @samp{"[$]@@"}, and you'll prefer do something
10514 better than hacking Autoconf @code{:-)}.
10516 When using @command{sed}, don't use @option{-e} except for indenting
10517 purposes.  With the @code{s} and @code{y} commands, the preferred
10518 separator is @samp{/} unless @samp{/} itself might appear in the pattern
10519 or replacement, in which case you should use @samp{|}, or optionally
10520 @samp{,} if you know the pattern and replacement cannot contain a file
10521 name.  If none of these characters will do, choose a printable character
10522 that cannot appear in the pattern or replacement.  Characters from the
10523 set @samp{"#$&'()*;<=>?`|~} are good choices if the pattern or
10524 replacement might contain a file name, since they have special meaning
10525 to the shell and are less likely to occur in file names.
10527 @xref{Macro Definitions}, for details on how to define a macro.  If a
10528 macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
10529 of an @code{AC_REQUIRE} directive, and macros required by other macros
10530 inside arguments will not need to be expanded before this macro, then
10531 use @code{m4_define}.  In case of doubt, use @code{AC_DEFUN}.
10532 All the @code{AC_REQUIRE} statements should be at the beginning of the
10533 macro, @code{dnl}'ed.
10535 You should not rely on the number of arguments: instead of checking
10536 whether an argument is missing, test that it is not empty.  It provides
10537 both a simpler and a more predictable interface to the user, and saves
10538 room for further arguments.
10540 Unless the macro is short, try to leave the closing @samp{])} at the
10541 beginning of a line, followed by a comment that repeats the name of the
10542 macro being defined.  This introduces an additional newline in
10543 @command{configure}; normally, that is not a problem, but if you want to
10544 remove it you can use @samp{[]dnl} on the last line.  You can similarly
10545 use @samp{[]dnl} after a macro call to remove its newline.  @samp{[]dnl}
10546 is recommended instead of @samp{dnl} to ensure that M4 does not
10547 interpret the @samp{dnl} as being attached to the preceding text or
10548 macro output.  For example, instead of:
10550 @example
10551 AC_DEFUN([AC_PATH_X],
10552 [AC_MSG_CHECKING([for X])
10553 AC_REQUIRE_CPP()
10554 @r{# @dots{}omitted@dots{}}
10555   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
10556 fi])
10557 @end example
10559 @noindent
10560 you would write:
10562 @example
10563 AC_DEFUN([AC_PATH_X],
10564 [AC_REQUIRE_CPP()[]dnl
10565 AC_MSG_CHECKING([for X])
10566 @r{# @dots{}omitted@dots{}}
10567   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
10568 fi[]dnl
10569 ])# AC_PATH_X
10570 @end example
10572 If the macro is long, try to split it into logical chunks.  Typically,
10573 macros that check for a bug in a function and prepare its
10574 @code{AC_LIBOBJ} replacement should have an auxiliary macro to perform
10575 this setup.  Do not hesitate to introduce auxiliary macros to factor
10576 your code.
10578 In order to highlight the recommended coding style, here is a macro
10579 written the old way:
10581 @example
10582 dnl Check for EMX on OS/2.
10583 dnl _AC_EMXOS2
10584 AC_DEFUN(_AC_EMXOS2,
10585 [AC_CACHE_CHECK(for EMX OS/2 environment, ac_cv_emxos2,
10586 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, return __EMX__;)],
10587 ac_cv_emxos2=yes, ac_cv_emxos2=no)])
10588 test "$ac_cv_emxos2" = yes && EMXOS2=yes])
10589 @end example
10591 @noindent
10592 and the new way:
10594 @example
10595 # _AC_EMXOS2
10596 # ----------
10597 # Check for EMX on OS/2.
10598 m4_define([_AC_EMXOS2],
10599 [AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
10600 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
10601                    [ac_cv_emxos2=yes],
10602                    [ac_cv_emxos2=no])])
10603 test "$ac_cv_emxos2" = yes && EMXOS2=yes[]dnl
10604 ])# _AC_EMXOS2
10605 @end example
10610 @c ============================================= Portable Shell Programming
10612 @node Portable Shell
10613 @chapter Portable Shell Programming
10614 @cindex Portable shell programming
10616 When writing your own checks, there are some shell-script programming
10617 techniques you should avoid in order to make your code portable.  The
10618 Bourne shell and upward-compatible shells like the Korn shell and Bash
10619 have evolved over the years, but to prevent trouble, do not take
10620 advantage of features that were added after Unix version 7, circa
10621 1977 (@pxref{Systemology}).
10623 You should not use shell functions, aliases, negated character
10624 classes, or other features that are not found in all Bourne-compatible
10625 shells; restrict yourself to the lowest common denominator.  Even
10626 @code{unset} is not supported by all shells!
10628 Some ancient systems have quite
10629 small limits on the length of the @samp{#!} line; for instance, 32
10630 bytes (not including the newline) on SunOS 4.
10631 A few ancient 4.2@acronym{BSD} based systems (such as Dynix circa 1984)
10632 required a single space between the @samp{#!} and the @samp{/}.
10633 However, these ancient systems are no longer of practical concern.
10635 The set of external programs you should run in a @command{configure} script
10636 is fairly small.  @xref{Utilities in Makefiles, , Utilities in
10637 Makefiles, standards, @acronym{GNU} Coding Standards}, for the list.  This
10638 restriction allows users to start out with a fairly small set of
10639 programs and build the rest, avoiding too many interdependencies between
10640 packages.
10642 Some of these external utilities have a portable subset of features; see
10643 @ref{Limitations of Usual Tools}.
10645 There are other sources of documentation about shells.  The
10646 specification for the Posix
10647 @uref{http://www.opengroup.org/@/susv3/@/utilities/@/xcu_chap02.html, Shell
10648 Command Language}, though more generous than the restrictive shell
10649 subset described above, is fairly portable nowadays.  Also please see
10650 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/, the Shell FAQs}.
10652 @menu
10653 * Shellology::                  A zoology of shells
10654 * Here-Documents::              Quirks and tricks
10655 * File Descriptors::            FDs and redirections
10656 * File System Conventions::     File names
10657 * Shell Substitutions::         Variable and command expansions
10658 * Assignments::                 Varying side effects of assignments
10659 * Parentheses::                 Parentheses in shell scripts
10660 * Slashes::                     Slashes in shell scripts
10661 * Special Shell Variables::     Variables you should not change
10662 * Limitations of Builtins::     Portable use of not so portable /bin/sh
10663 * Limitations of Usual Tools::  Portable use of portable tools
10664 * Limitations of Make::         Portable Makefiles
10665 @end menu
10667 @node Shellology
10668 @section Shellology
10669 @cindex Shellology
10671 There are several families of shells, most prominently the Bourne family
10672 and the C shell family which are deeply incompatible.  If you want to
10673 write portable shell scripts, avoid members of the C shell family.  The
10674 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/@/shell-differences/, the
10675 Shell difference FAQ} includes a small history of Posix shells, and a
10676 comparison between several of them.
10678 Below we describe some of the members of the Bourne shell family.
10680 @table @asis
10681 @item Ash
10682 @cindex Ash
10683 Ash is often used on @acronym{GNU}/Linux and @acronym{BSD}
10684 systems as a light-weight Bourne-compatible shell.  Ash 0.2 has some
10685 bugs that are fixed in the 0.3.x series, but portable shell scripts
10686 should work around them, since version 0.2 is still shipped with many
10687 @acronym{GNU}/Linux distributions.
10689 To be compatible with Ash 0.2:
10691 @itemize @minus
10692 @item
10693 don't use @samp{$?} after expanding empty or unset variables,
10694 or at the start of an @command{eval}:
10696 @example
10697 foo=
10698 false
10699 $foo
10700 echo "Do not use it: $?"
10701 false
10702 eval 'echo "Do not use it: $?"'
10703 @end example
10705 @item
10706 don't use command substitution within variable expansion:
10708 @example
10709 cat $@{FOO=`bar`@}
10710 @end example
10712 @item
10713 beware that single builtin substitutions are not performed by a
10714 subshell, hence their effect applies to the current shell!  @xref{Shell
10715 Substitutions}, item ``Command Substitution''.
10716 @end itemize
10718 @item Bash
10719 @cindex Bash
10720 To detect whether you are running Bash, test whether
10721 @code{BASH_VERSION} is set.  To require
10722 Posix compatibility, run @samp{set -o posix}.  @xref{Bash POSIX
10723 Mode, , Bash Posix Mode, bash, The @acronym{GNU} Bash Reference
10724 Manual}, for details.
10726 @item Bash 2.05 and later
10727 @cindex Bash 2.05 and later
10728 Versions 2.05 and later of Bash use a different format for the
10729 output of the @command{set} builtin, designed to make evaluating its
10730 output easier.  However, this output is not compatible with earlier
10731 versions of Bash (or with many other shells, probably).  So if
10732 you use Bash 2.05 or higher to execute @command{configure},
10733 you'll need to use Bash 2.05 for all other build tasks as well.
10735 @item Ksh
10736 @cindex Ksh
10737 @cindex Korn shell
10738 @prindex @samp{ksh}
10739 @prindex @samp{ksh88}
10740 @prindex @samp{ksh93}
10741 The Korn shell is compatible with the Bourne family and it mostly
10742 conforms to Posix.  It has two major variants commonly
10743 called @samp{ksh88} and @samp{ksh93}, named after the years of initial
10744 release.  It is usually called @command{ksh}, but is called @command{sh}
10745 on some hosts if you set your path appropriately.
10747 Solaris systems have three variants:
10748 @prindex @command{/usr/bin/ksh} on Solaris
10749 @command{/usr/bin/ksh} is @samp{ksh88}; it is
10750 standard on Solaris 2.0 and later.
10751 @prindex @command{/usr/xpg4/bin/sh} on Solaris
10752 @command{/usr/xpg4/bin/sh} is a Posix-compliant variant of
10753 @samp{ksh88}; it is standard on Solaris 9 and later.
10754 @prindex @command{/usr/dt/bin/dtksh} on Solaris
10755 @command{/usr/dt/bin/dtksh} is @samp{ksh93}.
10756 Variants that are not standard may be parts of optional
10757 packages.  There is no extra charge for these packages, but they are
10758 not part of a minimal OS install and therefore some installations may
10759 not have it.
10761 Starting with Tru64 Version 4.0, the Korn shell @command{/usr/bin/ksh}
10762 is also available as @command{/usr/bin/posix/sh}.  If the environment
10763 variable @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
10764 the standard shell conform to Posix.
10766 @item Pdksh
10767 @prindex @samp{pdksh}
10768 A public-domain clone of the Korn shell called @command{pdksh} is widely
10769 available: it has most of the @samp{ksh88} features along with a few of
10770 its own.  It will usually set @code{KSH_VERSION}, except if invoked as
10771 @command{/bin/sh} on Open@acronym{BSD}, and similarly to Bash you can require
10772 Posix compatibility by running @samp{set -o posix}.  Unfortunately, with
10773 @command{pdksh} 5.2.14 (the latest stable version as of February 2006)
10774 Posix mode is buggy and causes @command{pdksh} to depart from Posix in
10775 at least one respect:
10777 @example
10778 $ @kbd{echo "`echo \"hello\"`"}
10779 hello
10780 $ @kbd{set -o posix}
10781 $ @kbd{echo "`echo \"hello\"`"}
10782 "hello"
10783 @end example
10785 The last line of output contains spurious quotes.  This is yet another
10786 reason why portable shell code should not contain
10787 @code{"`@dots{}\"@dots{}\"@dots{}`"} constructs (@pxref{Shell
10788 Substitutions}).
10790 @item Zsh
10791 @cindex Zsh
10792 To detect whether you are running @command{zsh}, test whether
10793 @code{ZSH_VERSION} is set.  By default @command{zsh} is @emph{not}
10794 compatible with the Bourne shell: you must execute @samp{emulate sh},
10795 and for @command{zsh} versions before 3.1.6-dev-18 you must also
10796 set @code{NULLCMD} to @samp{:}.  @xref{Compatibility, , Compatibility,
10797 zsh, The Z Shell Manual}, for details.
10799 The default Mac OS X @command{sh} was originally Zsh; it was changed to
10800 Bash in Mac OS X 10.2.
10801 @end table
10803 The following discussion between Russ Allbery and Robert Lipe is worth
10804 reading:
10806 @noindent
10807 Russ Allbery:
10809 @quotation
10810 The @acronym{GNU} assumption that @command{/bin/sh} is the one and only shell
10811 leads to a permanent deadlock.  Vendors don't want to break users'
10812 existing shell scripts, and there are some corner cases in the Bourne
10813 shell that are not completely compatible with a Posix shell.  Thus,
10814 vendors who have taken this route will @emph{never} (OK@dots{}``never say
10815 never'') replace the Bourne shell (as @command{/bin/sh}) with a
10816 Posix shell.
10817 @end quotation
10819 @noindent
10820 Robert Lipe:
10822 @quotation
10823 This is exactly the problem.  While most (at least most System V's) do
10824 have a Bourne shell that accepts shell functions most vendor
10825 @command{/bin/sh} programs are not the Posix shell.
10827 So while most modern systems do have a shell @emph{somewhere} that meets the
10828 Posix standard, the challenge is to find it.
10829 @end quotation
10831 @node Here-Documents
10832 @section Here-Documents
10833 @cindex Here-documents
10834 @cindex Shell here-documents
10836 Don't rely on @samp{\} being preserved just because it has no special
10837 meaning together with the next symbol.  In the native @command{sh}
10838 on Open@acronym{BSD} 2.7 @samp{\"} expands to @samp{"} in here-documents with
10839 unquoted delimiter.  As a general rule, if @samp{\\} expands to @samp{\}
10840 use @samp{\\} to get @samp{\}.
10842 With Open@acronym{BSD} 2.7's @command{sh}
10844 @example
10845 @group
10846 $ @kbd{cat <<EOF
10847 > \" \\
10848 > EOF}
10849 " \
10850 @end group
10851 @end example
10853 @noindent
10854 and with Bash:
10856 @example
10857 @group
10858 bash-2.04$ @kbd{cat <<EOF
10859 > \" \\
10860 > EOF}
10861 \" \
10862 @end group
10863 @end example
10865 Some shells mishandle large here-documents: for example,
10866 Solaris 10 @command{dtksh} and the UnixWare 7.1.1 Posix shell, which are
10867 derived from Korn shell version M-12/28/93d, mishandle braced variable
10868 expansion that crosses a 1024- or 4096-byte buffer boundary
10869 within a here-document.  Only the part of the variable name after the boundary
10870 is used.  For example, @code{$@{variable@}} could be replaced by the expansion
10871 of @code{$@{ble@}}.  If the end of the variable name is aligned with the block
10872 boundary, the shell reports an error, as if you used @code{$@{@}}.
10873 Instead of @code{$@{variable-default@}}, the shell may expand
10874 @code{$@{riable-default@}}, or even @code{$@{fault@}}.  This bug can often
10875 be worked around by omitting the braces: @code{$variable}.  The bug was fixed in
10876 @samp{ksh93g} (1998-04-30) but as of 2006 many operating systems were
10877 still shipping older versions with the bug.
10879 Many older shells (including the Bourne shell) implement here-documents
10880 inefficiently.  In particular, some shells can be extremely inefficient when
10881 a single statement contains many here-documents.  For instance if your
10882 @file{configure.ac} includes something like:
10884 @example
10885 @group
10886 if <cross_compiling>; then
10887   assume this and that
10888 else
10889   check this
10890   check that
10891   check something else
10892   @dots{}
10893   on and on forever
10894   @dots{}
10896 @end group
10897 @end example
10899 A shell parses the whole @code{if}/@code{fi} construct, creating
10900 temporary files for each here-document in it.  Some shells create links
10901 for such here-documents on every @code{fork}, so that the clean-up code
10902 they had installed correctly removes them.  It is creating the links
10903 that can take the shell forever.
10905 Moving the tests out of the @code{if}/@code{fi}, or creating multiple
10906 @code{if}/@code{fi} constructs, would improve the performance
10907 significantly.  Anyway, this kind of construct is not exactly the
10908 typical use of Autoconf.  In fact, it's even not recommended, because M4
10909 macros can't look into shell conditionals, so we may fail to expand a
10910 macro when it was expanded before in a conditional path, and the
10911 condition turned out to be false at runtime, and we end up not
10912 executing the macro at all.
10914 @node File Descriptors
10915 @section File Descriptors
10916 @cindex Descriptors
10917 @cindex File descriptors
10918 @cindex Shell file descriptors
10920 Don't redirect the same file descriptor several times, as you are doomed
10921 to failure under Ultrix.
10923 @example
10924 ULTRIX V4.4 (Rev. 69) System #31: Thu Aug 10 19:42:23 GMT 1995
10925 UWS V4.4 (Rev. 11)
10926 $ @kbd{eval 'echo matter >fullness' >void}
10927 illegal io
10928 $ @kbd{eval '(echo matter >fullness)' >void}
10929 illegal io
10930 $ @kbd{(eval '(echo matter >fullness)') >void}
10931 Ambiguous output redirect.
10932 @end example
10934 @noindent
10935 In each case the expected result is of course @file{fullness} containing
10936 @samp{matter} and @file{void} being empty.
10938 Don't try to redirect the standard error of a command substitution: it
10939 must be done @emph{inside} the command substitution: when running
10940 @samp{: `cd /zorglub` 2>/dev/null} expect the error message to
10941 escape, while @samp{: `cd /zorglub 2>/dev/null`} works properly.
10943 It is worth noting that Zsh (but not Ash nor Bash) makes it possible
10944 in assignments though: @samp{foo=`cd /zorglub` 2>/dev/null}.
10946 Most shells, if not all (including Bash, Zsh, Ash), output traces on
10947 stderr, even for sub-shells.  This might result in undesirable content
10948 if you meant to capture the standard-error output of the inner command:
10950 @example
10951 $ @kbd{ash -x -c '(eval "echo foo >&2") 2>stderr'}
10952 $ @kbd{cat stderr}
10953 + eval echo foo >&2
10954 + echo foo
10956 $ @kbd{bash -x -c '(eval "echo foo >&2") 2>stderr'}
10957 $ @kbd{cat stderr}
10958 + eval 'echo foo >&2'
10959 ++ echo foo
10961 $ @kbd{zsh -x -c '(eval "echo foo >&2") 2>stderr'}
10962 @i{# Traces on startup files deleted here.}
10963 $ @kbd{cat stderr}
10964 +zsh:1> eval echo foo >&2
10965 +zsh:1> echo foo
10967 @end example
10969 @noindent
10970 You'll appreciate the various levels of detail@enddots{}
10972 One workaround is to grep out uninteresting lines, hoping not to remove
10973 good ones@enddots{}
10975 Don't try to move/delete open files, such as in @samp{exec >foo; mv foo
10976 bar}; see @ref{Limitations of Builtins}, @command{mv} for more details.
10978 Don't rely on file descriptors 0, 1, and 2 remaining closed in a
10979 subsidiary program.  If any of these descriptors is closed, the
10980 operating system may open an unspecified file for the descriptor in the
10981 new process image.  Posix says this may be done only if the subsidiary
10982 program is set-user-ID or set-group-ID, but HP-UX 11.23 does it even for
10983 ordinary programs.
10985 Don't rely on open file descriptors being open in child processes.  In
10986 @command{ksh}, file descriptors above 2 which are opened using
10987 @samp{exec @var{n}>file} are closed by a subsequent @samp{exec} (such as
10988 that involved in the fork-and-exec which runs a program or script).
10989 Thus, using @command{sh}, we have:
10991 @example
10992 $ @kbd{cat ./descrips}
10993 #!/bin/sh -
10994 echo hello >&5
10995 $ @kbd{exec 5>t}
10996 $ @kbd{./descrips}
10997 $ @kbd{cat t}
10999 hello
11000 @end example
11002 @noindent
11003 But using ksh:
11005 @example
11006 $ @kbd{exec 5>t}
11007 $ @kbd{./descrips}
11008 hello
11009 $ @kbd{cat t}
11011 @end example
11013 @noindent
11014 Within the process which runs the @samp{descrips} script, file
11015 descriptor 5 is closed.
11017 A few ancient systems reserved some file descriptors.  By convention,
11018 file descriptor 3 was opened to @file{/dev/tty} when you logged into
11019 Eighth Edition (1985) through Tenth Edition Unix (1989).  File
11020 descriptor 4 had a special use on the Stardent/Kubota Titan (circa
11021 1990), though we don't now remember what it was.  Both these systems are
11022 obsolete, so it's now safe to treat file descriptors 3 and 4 like any
11023 other file descriptors.
11025 @node File System Conventions
11026 @section File System Conventions
11027 @cindex File system conventions
11029 Autoconf uses shell-script processing extensively, so the file names
11030 that it processes should not contain characters that are special to the
11031 shell.  Special characters include space, tab, newline, @sc{nul}, and
11032 the following:
11034 @example
11035 " # $ & ' ( ) * ; < = > ? [ \ ` |
11036 @end example
11038 Also, file names should not begin with @samp{~} or @samp{-}, and should
11039 contain neither @samp{-} immediately after @samp{/} nor @samp{~}
11040 immediately after @samp{:}.  On Posix-like platforms, directory names
11041 should not contain @samp{:}, as this runs afoul of @samp{:} used as the
11042 path separator.
11044 These restrictions apply not only to the files that you distribute, but
11045 also to the absolute file names of your source, build, and destination
11046 directories.
11048 On some Posix-like platforms, @samp{!} and @samp{^} are special too, so
11049 they should be avoided.
11051 Posix lets implementations treat leading @file{//} specially, but
11052 requires leading @file{///} and beyond to be equivalent to @file{/}.
11053 Most Unix variants treat @file{//} like @file{/}.  However, some treat
11054 @file{//} as a ``super-root'' that can provide access to files that are
11055 not otherwise reachable from @file{/}.  The super-root tradition began
11056 with Apollo Domain/OS, which died out long ago, but unfortunately Cygwin
11057 has revived it.
11059 While @command{autoconf} and friends will usually be run on some Posix
11060 variety, it can and will be used on other systems, most notably @acronym{DOS}
11061 variants.  This impacts several assumptions regarding file names.
11063 @noindent
11064 For example, the following code:
11066 @example
11067 case $foo_dir in
11068   /*) # Absolute
11069      ;;
11070   *)
11071      foo_dir=$dots$foo_dir ;;
11072 esac
11073 @end example
11075 @noindent
11076 will fail to properly detect absolute file names on those systems, because
11077 they can use a drivespec, and will usually use a backslash as directory
11078 separator.  If you want to be portable to @acronym{DOS} variants (at the
11079 price of rejecting valid but oddball Posix file names like @file{a:\b}),
11080 you can check for absolute file names like this:
11082 @example
11083 case $foo_dir in
11084   [\\/]* | ?:[\\/]* ) # Absolute
11085      ;;
11086   *)
11087      foo_dir=$dots$foo_dir ;;
11088 esac
11089 @end example
11091 @noindent
11092 Make sure you quote the brackets if appropriate and keep the backslash as
11093 first character (@pxref{Limitations of Builtins}).
11095 Also, because the colon is used as part of a drivespec, these systems don't
11096 use it as path separator.  When creating or accessing paths, you can use the
11097 @code{PATH_SEPARATOR} output variable instead.  @command{configure} sets this
11098 to the appropriate value (@samp{:} or @samp{;}) when it starts up.
11100 File names need extra care as well.  While @acronym{DOS} variants
11101 that are Posixy enough to run @command{autoconf} (such as @acronym{DJGPP}) will
11102 usually be able to handle long file names properly, there are still
11103 limitations that can seriously break packages.  Several of these issues
11104 can be easily detected by the
11105 @uref{ftp://ftp.gnu.org/gnu/non-gnu/doschk/doschk-1.1.tar.gz, doschk}
11106 package.
11108 A short overview follows; problems are marked with @sc{sfn}/@sc{lfn} to
11109 indicate where they apply: @sc{sfn} means the issues are only relevant to
11110 plain @acronym{DOS}, not to @acronym{DOS} under Microsoft Windows
11111 variants, while @sc{lfn} identifies problems that exist even under
11112 Microsoft Windows variants.
11114 @table @asis
11115 @item No multiple dots (@sc{sfn})
11116 @acronym{DOS} cannot handle multiple dots in file names.  This is an especially
11117 important thing to remember when building a portable configure script,
11118 as @command{autoconf} uses a .in suffix for template files.
11120 This is perfectly OK on Posix variants:
11122 @example
11123 AC_CONFIG_HEADERS([config.h])
11124 AC_CONFIG_FILES([source.c foo.bar])
11125 AC_OUTPUT
11126 @end example
11128 @noindent
11129 but it causes problems on @acronym{DOS}, as it requires @samp{config.h.in},
11130 @samp{source.c.in} and @samp{foo.bar.in}.  To make your package more portable
11131 to @acronym{DOS}-based environments, you should use this instead:
11133 @example
11134 AC_CONFIG_HEADERS([config.h:config.hin])
11135 AC_CONFIG_FILES([source.c:source.cin foo.bar:foobar.in])
11136 AC_OUTPUT
11137 @end example
11139 @item No leading dot (@sc{sfn})
11140 @acronym{DOS} cannot handle file names that start with a dot.  This is usually
11141 not a very important issue for @command{autoconf}.
11143 @item Case insensitivity (@sc{lfn})
11144 @acronym{DOS} is case insensitive, so you cannot, for example, have both a
11145 file called @samp{INSTALL} and a directory called @samp{install}.  This
11146 also affects @command{make}; if there's a file called @samp{INSTALL} in
11147 the directory, @samp{make install} will do nothing (unless the
11148 @samp{install} target is marked as PHONY).
11150 @item The 8+3 limit (@sc{sfn})
11151 Because the @acronym{DOS} file system only stores the first 8 characters of
11152 the file name and the first 3 of the extension, those must be unique.
11153 That means that @file{foobar-part1.c}, @file{foobar-part2.c} and
11154 @file{foobar-prettybird.c} all resolve to the same file name
11155 (@file{FOOBAR-P.C}).  The same goes for @file{foo.bar} and
11156 @file{foo.bartender}.
11158 The 8+3 limit is not usually a problem under Microsoft Windows, as it
11159 uses numeric
11160 tails in the short version of file names to make them unique.  However, a
11161 registry setting can turn this behavior off.  While this makes it
11162 possible to share file trees containing long file names between @sc{sfn}
11163 and @sc{lfn} environments, it also means the above problem applies there
11164 as well.
11166 @item Invalid characters (@sc{lfn})
11167 Some characters are invalid in @acronym{DOS} file names, and should therefore
11168 be avoided.  In a @sc{lfn} environment, these are @samp{/}, @samp{\},
11169 @samp{?}, @samp{*}, @samp{:}, @samp{<}, @samp{>}, @samp{|} and @samp{"}.
11170 In a @sc{sfn} environment, other characters are also invalid.  These
11171 include @samp{+}, @samp{,}, @samp{[} and @samp{]}.
11173 @item Invalid names (@sc{lfn})
11174 Some @acronym{DOS} file names are reserved, and cause problems if you
11175 try to use files with those names.  These names include @file{CON},
11176 @file{AUX}, @file{COM1}, @file{COM2}, @file{COM3}, @file{COM4},
11177 @file{LPT1}, @file{LPT2}, @file{LPT3}, @file{NUL}, and @file{PRN}.
11178 File names are case insensitive, so even names like
11179 @file{aux/config.guess} are disallowed.
11181 @end table
11183 @node Shell Substitutions
11184 @section Shell Substitutions
11185 @cindex Shell substitutions
11187 Contrary to a persistent urban legend, the Bourne shell does not
11188 systematically split variables and back-quoted expressions, in particular
11189 on the right-hand side of assignments and in the argument of @code{case}.
11190 For instance, the following code:
11192 @example
11193 case "$given_srcdir" in
11194 .)  top_srcdir="`echo "$dots" | sed 's,/$,,'`" ;;
11195 *)  top_srcdir="$dots$given_srcdir" ;;
11196 esac
11197 @end example
11199 @noindent
11200 is more readable when written as:
11202 @example
11203 case $given_srcdir in
11204 .)  top_srcdir=`echo "$dots" | sed 's,/$,,'` ;;
11205 *)  top_srcdir=$dots$given_srcdir ;;
11206 esac
11207 @end example
11209 @noindent
11210 and in fact it is even @emph{more} portable: in the first case of the
11211 first attempt, the computation of @code{top_srcdir} is not portable,
11212 since not all shells properly understand @code{"`@dots{}"@dots{}"@dots{}`"}.
11213 Worse yet, not all shells understand @code{"`@dots{}\"@dots{}\"@dots{}`"}
11214 the same way.  There is just no portable way to use double-quoted
11215 strings inside double-quoted back-quoted expressions (pfew!).
11217 @table @code
11218 @item $@@
11219 @cindex @samp{"$@@"}
11220 One of the most famous shell-portability issues is related to
11221 @samp{"$@@"}.  When there are no positional arguments, Posix says
11222 that @samp{"$@@"} is supposed to be equivalent to nothing, but the
11223 original Unix version 7 Bourne shell treated it as equivalent to
11224 @samp{""} instead, and this behavior survives in later implementations
11225 like Digital Unix 5.0.
11227 The traditional way to work around this portability problem is to use
11228 @samp{$@{1+"$@@"@}}.  Unfortunately this method does not work with
11229 Zsh (3.x and 4.x), which is used on Mac OS X@.  When emulating
11230 the Bourne shell, Zsh performs word splitting on @samp{$@{1+"$@@"@}}:
11232 @example
11233 zsh $ @kbd{emulate sh}
11234 zsh $ @kbd{for i in "$@@"; do echo $i; done}
11235 Hello World
11237 zsh $ @kbd{for i in $@{1+"$@@"@}; do echo $i; done}
11238 Hello
11239 World
11241 @end example
11243 @noindent
11244 Zsh handles plain @samp{"$@@"} properly, but we can't use plain
11245 @samp{"$@@"} because of the portability problems mentioned above.
11246 One workaround relies on Zsh's ``global aliases'' to convert
11247 @samp{$@{1+"$@@"@}} into @samp{"$@@"} by itself:
11249 @example
11250 test "$@{ZSH_VERSION+set@}" = set && alias -g '$@{1+"$@@"@}'='"$@@"'
11251 @end example
11253 A more conservative workaround is to avoid @samp{"$@@"} if it is
11254 possible that there may be no positional arguments.  For example,
11255 instead of:
11257 @example
11258 cat conftest.c "$@@"
11259 @end example
11261 you can use this instead:
11263 @example
11264 case $# in
11265 0) cat conftest.c;;
11266 *) cat conftest.c "$@@";;
11267 esac
11268 @end example
11271 @item $@{10@}
11272 @cindex positional parameters
11273 The 10th, 11th, @dots{} positional parameters can be accessed only after
11274 a @code{shift}.  The 7th Edition shell reported an error if given
11275 @code{$@{10@}}, and
11276 Solaris 10 @command{/bin/sh} still acts that way:
11278 @example
11279 $ @kbd{set 1 2 3 4 5 6 7 8 9 10}
11280 $ @kbd{echo $@{10@}}
11281 bad substitution
11282 @end example
11284 @item $@{@var{var}:-@var{value}@}
11285 @c Info cannot handle `:' in index entries.
11286 @c @cindex $@{@var{var}:-@var{value}@}
11287 Old @acronym{BSD} shells, including the Ultrix @code{sh}, don't accept the
11288 colon for any shell substitution, and complain and die.
11290 @item $@{@var{var}=@var{literal}@}
11291 @cindex $@{@var{var}=@var{literal}@}
11292 Be sure to quote:
11294 @example
11295 : $@{var='Some words'@}
11296 @end example
11298 @noindent
11299 otherwise some shells, such as on Digital Unix V 5.0, will die because
11300 of a ``bad substitution''.
11302 @sp 1
11304 Solaris @command{/bin/sh} has a frightening bug in its interpretation
11305 of this.  Imagine you need set a variable to a string containing
11306 @samp{@}}.  This @samp{@}} character confuses Solaris @command{/bin/sh}
11307 when the affected variable was already set.  This bug can be exercised
11308 by running:
11310 @example
11311 $ @kbd{unset foo}
11312 $ @kbd{foo=$@{foo='@}'@}}
11313 $ @kbd{echo $foo}
11315 $ @kbd{foo=$@{foo='@}'   # no error; this hints to what the bug is}
11316 $ @kbd{echo $foo}
11318 $ @kbd{foo=$@{foo='@}'@}}
11319 $ @kbd{echo $foo}
11320 @}@}
11321  ^ ugh!
11322 @end example
11324 It seems that @samp{@}} is interpreted as matching @samp{$@{}, even
11325 though it is enclosed in single quotes.  The problem doesn't happen
11326 using double quotes.
11328 @item $@{@var{var}=@var{expanded-value}@}
11329 @cindex $@{@var{var}=@var{expanded-value}@}
11330 On Ultrix,
11331 running
11333 @example
11334 default="yu,yaa"
11335 : $@{var="$default"@}
11336 @end example
11338 @noindent
11339 will set @var{var} to @samp{M-yM-uM-,M-yM-aM-a}, i.e., the 8th bit of
11340 each char will be set.  You won't observe the phenomenon using a simple
11341 @samp{echo $var} since apparently the shell resets the 8th bit when it
11342 expands $var.  Here are two means to make this shell confess its sins:
11344 @example
11345 $ @kbd{cat -v <<EOF
11346 $var
11347 EOF}
11348 @end example
11350 @noindent
11353 @example
11354 $ @kbd{set | grep '^var=' | cat -v}
11355 @end example
11357 One classic incarnation of this bug is:
11359 @example
11360 default="a b c"
11361 : $@{list="$default"@}
11362 for c in $list; do
11363   echo $c
11364 done
11365 @end example
11367 @noindent
11368 You'll get @samp{a b c} on a single line.  Why?  Because there are no
11369 spaces in @samp{$list}: there are @samp{M- }, i.e., spaces with the 8th
11370 bit set, hence no IFS splitting is performed!!!
11372 One piece of good news is that Ultrix works fine with @samp{:
11373 $@{list=$default@}}; i.e., if you @emph{don't} quote.  The bad news is
11374 then that @acronym{QNX} 4.25 then sets @var{list} to the @emph{last} item of
11375 @var{default}!
11377 The portable way out consists in using a double assignment, to switch
11378 the 8th bit twice on Ultrix:
11380 @example
11381 list=$@{list="$default"@}
11382 @end example
11384 @noindent
11385 @dots{}but beware of the @samp{@}} bug from Solaris (see above).  For safety,
11386 use:
11388 @example
11389 test "$@{var+set@}" = set || var=@var{@{value@}}
11390 @end example
11393 @item `@var{commands}`
11394 @cindex `@var{commands}`
11395 @cindex Command Substitution
11396 Posix requires shells to trim all trailing newlines from command
11397 output before substituting it, so assignments like
11398 @samp{dir=`echo "$file" | tr a A`} will not work as expected if
11399 @samp{$file} ends in a newline.
11401 While in general it makes no sense, do not substitute a single builtin
11402 with side effects, because Ash 0.2, trying to optimize, does not fork a
11403 subshell to perform the command.
11405 For instance, if you wanted to check that @command{cd} is silent, do not
11406 use @samp{test -z "`cd /`"} because the following can happen:
11408 @example
11409 $ @kbd{pwd}
11410 /tmp
11411 $ @kbd{test -z "`cd /`" && pwd}
11413 @end example
11415 @noindent
11416 The result of @samp{foo=`exit 1`} is left as an exercise to the reader.
11418 The MSYS shell leaves a stray byte in the expansion of a double-quoted
11419 command substitution of a native program, if the end of the substution
11420 is not aligned with the end of the double quote.  This may be worked
11421 around by inserting another pair of quotes:
11423 @example
11424 $ @kbd{echo "`printf 'foo\r\n'` bar" > broken}
11425 $ @kbd{echo "`printf 'foo\r\n'`"" bar" | cmp - broken}
11426 - broken differ: char 4, line 1
11427 @end example
11430 @item $(@var{commands})
11431 @cindex $(@var{commands})
11432 This construct is meant to replace @samp{`@var{commands}`},
11433 and it has most of the problems listed under @code{`@var{commands}`}.
11435 This construct can be
11436 nested while this is impossible to do portably with back quotes.
11437 Unfortunately it is not yet universally supported.  Most notably, even recent
11438 releases of Solaris don't support it:
11440 @example
11441 $ @kbd{showrev -c /bin/sh | grep version}
11442 Command version: SunOS 5.10 Generic 121004-01 Oct 2005
11443 $ @kbd{echo $(echo blah)}
11444 syntax error: `(' unexpected
11445 @end example
11447 @noindent
11448 nor does @sc{irix} 6.5's Bourne shell:
11449 @example
11450 $ @kbd{uname -a}
11451 IRIX firebird-image 6.5 07151432 IP22
11452 $ @kbd{echo $(echo blah)}
11453 $(echo blah)
11454 @end example
11456 If you do use @samp{$(@var{commands})}, make sure that the commands
11457 do not start with a parenthesis, as that would cause confusion with
11458 a different notation @samp{$((@var{expression}))} that in modern
11459 shells is an arithmetic expression not a command.  To avoid the
11460 confusion, insert a space between the two opening parentheses.
11462 Avoid @var{commands} that contain unbalanced parentheses in
11463 here-documents, comments, or case statement patterns, as many shells
11464 mishandle them.  For example, Bash 3.1, @samp{ksh88}, @command{pdksh}
11465 5.2.14, and Zsh 4.2.6 all mishandle the following valid command:
11467 @example
11468 echo $(case x in x) echo hello;; esac)
11469 @end example
11471 @item ^
11472 @cindex ^ quoting
11473 Always quote @samp{^}, otherwise traditional shells such as
11474 @command{/bin/sh} on Solaris 10 treat this like @samp{|}.
11476 @end table
11479 @node Assignments
11480 @section Assignments
11481 @cindex Shell assignments
11483 When setting several variables in a row, be aware that the order of the
11484 evaluation is undefined.  For instance @samp{foo=1 foo=2; echo $foo}
11485 gives @samp{1} with Solaris @command{/bin/sh}, but @samp{2} with Bash.
11486 You must use
11487 @samp{;} to enforce the order: @samp{foo=1; foo=2; echo $foo}.
11489 Don't rely on the following to find @file{subdir/program}:
11491 @example
11492 PATH=subdir$PATH_SEPARATOR$PATH program
11493 @end example
11495 @noindent
11496 as this does not work with Zsh 3.0.6.  Use something like this
11497 instead:
11499 @example
11500 (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)
11501 @end example
11503 Don't rely on the exit status of an assignment: Ash 0.2 does not change
11504 the status and propagates that of the last statement:
11506 @example
11507 $ @kbd{false || foo=bar; echo $?}
11509 $ @kbd{false || foo=`:`; echo $?}
11511 @end example
11513 @noindent
11514 and to make things even worse, @acronym{QNX} 4.25 just sets the exit status
11515 to 0 in any case:
11517 @example
11518 $ @kbd{foo=`exit 1`; echo $?}
11520 @end example
11522 To assign default values, follow this algorithm:
11524 @enumerate
11525 @item
11526 If the default value is a literal and does not contain any closing
11527 brace, use:
11529 @example
11530 : $@{var='my literal'@}
11531 @end example
11533 @item
11534 If the default value contains no closing brace, has to be expanded, and
11535 the variable being initialized will never be IFS-split (i.e., it's not a
11536 list), then use:
11538 @example
11539 : $@{var="$default"@}
11540 @end example
11542 @item
11543 If the default value contains no closing brace, has to be expanded, and
11544 the variable being initialized will be IFS-split (i.e., it's a list),
11545 then use:
11547 @example
11548 var=$@{var="$default"@}
11549 @end example
11551 @item
11552 If the default value contains a closing brace, then use:
11554 @example
11555 test "$@{var+set@}" = set || var='$@{indirection@}'
11556 @end example
11557 @end enumerate
11559 In most cases @samp{var=$@{var="$default"@}} is fine, but in case of
11560 doubt, just use the latter.  @xref{Shell Substitutions}, items
11561 @samp{$@{@var{var}:-@var{value}@}} and @samp{$@{@var{var}=@var{value}@}}
11562 for the rationale.
11564 @node Parentheses
11565 @section Parentheses in Shell Scripts
11566 @cindex Shell parentheses
11568 Beware of two opening parentheses in a row, as some shell
11569 implementations mishandle them.  For example, @samp{pdksh} 5.2.14
11570 misparses the following code:
11572 @example
11573 if ((true) || false); then
11574   echo ok
11576 @end example
11578 @noindent
11579 To work around this problem, insert a space between the two opening
11580 parentheses.  There is a similar problem and workaround with
11581 @samp{$((}; see @ref{Shell Substitutions}.
11583 Posix requires support for @code{case} patterns with opening
11584 parentheses like this:
11586 @example
11587 case $file_name in
11588 (*.c) echo "C source code";;
11589 esac
11590 @end example
11592 @noindent
11593 but the @code{(} in this example is not portable to many older Bourne
11594 shell implementations.  It can be omitted safely.
11596 @node Slashes
11597 @section Slashes in Shell Scripts
11598 @cindex Shell slashes
11600 Unpatched Tru64 5.1 @command{sh} omits the last slash of command-line
11601 arguments that contain two trailing slashes:
11603 @example
11604 $ @kbd{echo / // /// //// .// //.}
11605 / / // /// ./ //.
11606 $ @kbd{x=//}
11607 $ @kbd{eval "echo \$x"}
11609 $ @kbd{set -x}
11610 $ @kbd{echo abc | tr -t ab //}
11611 + echo abc
11612 + tr -t ab /
11614 @end example
11616 However, our understanding is that patches are available, so perhaps
11617 it's not worth worrying about working around this horrendous bug.
11619 @node Special Shell Variables
11620 @section Special Shell Variables
11621 @cindex Shell variables
11622 @cindex Special shell variables
11624 Some shell variables should not be used, since they can have a deep
11625 influence on the behavior of the shell.  In order to recover a sane
11626 behavior from the shell, some variables should be unset, but
11627 @command{unset} is not portable (@pxref{Limitations of Builtins}) and a
11628 fallback value is needed.
11630 As a general rule, shell variable names containing a lower-case letter
11631 are safe; you can define and use these variables without worrying about
11632 their effect on the underlying system, and without worrying about
11633 whether the shell will change them unexpectedly.  (The exception is the
11634 shell variable @code{status}, as described below.)
11636 Here is a list of names that are known to cause trouble.  This list is
11637 not exhaustive, but you should be safe if you avoid the name
11638 @code{status} and names containing only upper-case letters and
11639 underscores.
11641 @c Alphabetical order, case insensitive, `A' before `a'.
11642 @table @code
11643 @item _
11644 Many shells reserve @samp{$_} for various purposes, e.g., the name of
11645 the last command executed.
11647 @item BIN_SH
11648 @evindex BIN_SH
11649 In Tru64, if @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
11650 the standard shell conform to Posix.
11651 Autoconf-generated scripts export this variable when they start up.
11653 @item CDPATH
11654 @evindex CDPATH
11655 When this variable is set it specifies a list of directories to search
11656 when invoking @code{cd} with a relative file name.  Posix
11657 1003.1-2001 says that if a nonempty directory name from @env{CDPATH}
11658 is used successfully, @code{cd} prints the resulting absolute
11659 file name.  Unfortunately this output can break idioms like
11660 @samp{abs=`cd src && pwd`} because @code{abs} receives the name twice.
11661 Also, many shells do not conform to this part of Posix; for
11662 example, @command{zsh} prints the result only if a directory name
11663 other than @file{.} was chosen from @env{CDPATH}.
11665 In practice the shells that have this problem also support
11666 @command{unset}, so you can work around the problem as follows:
11668 @example
11669 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
11670 @end example
11672 Autoconf-generated scripts automatically unset @env{CDPATH} if
11673 possible, so you need not worry about this problem in those scripts.
11675 @item DUALCASE
11676 @evindex DUALCASE
11677 In the MKS shell, case statements and file name generation are
11678 case-insensitive unless @env{DUALCASE} is nonzero.
11679 Autoconf-generated scripts export this variable when they start up.
11681 @item ENV
11682 @itemx MAIL
11683 @itemx MAILPATH
11684 @itemx PS1
11685 @itemx PS2
11686 @itemx PS4
11687 @evindex ENV
11688 @evindex MAIL
11689 @evindex MAILPATH
11690 @evindex PS1
11691 @evindex PS2
11692 @evindex PS4
11693 These variables should not matter for shell scripts, since they are
11694 supposed to affect only interactive shells.  However, at least one
11695 shell (the pre-3.0 @sc{uwin} Korn shell) gets confused about
11696 whether it is interactive, which means that (for example) a @env{PS1}
11697 with a side effect can unexpectedly modify @samp{$?}.  To work around
11698 this bug, Autoconf-generated scripts do something like this:
11700 @example
11701 (unset ENV) >/dev/null 2>&1 && unset ENV MAIL MAILPATH
11702 PS1='$ '
11703 PS2='> '
11704 PS4='+ '
11705 @end example
11707 @item IFS
11708 @evindex IFS
11709 Long ago, shell scripts inherited @env{IFS} from the environment,
11710 but this caused many problems so modern shells ignore any environment
11711 settings for @env{IFS}.
11713 Don't set the first character of @code{IFS} to backslash.  Indeed,
11714 Bourne shells use the first character (backslash) when joining the
11715 components in @samp{"$@@"} and some shells then re-interpret (!)@: the
11716 backslash escapes, so you can end up with backspace and other strange
11717 characters.
11719 The proper value for @code{IFS} (in regular code, not when performing
11720 splits) is @samp{@key{SPC}@key{TAB}@key{RET}}.  The first character is
11721 especially important, as it is used to join the arguments in @samp{$*};
11722 however, note that traditional shells, but also bash-2.04, fail to adhere
11723 to this and join with a space anyway.
11725 @item LANG
11726 @itemx LC_ALL
11727 @itemx LC_COLLATE
11728 @itemx LC_CTYPE
11729 @itemx LC_MESSAGES
11730 @itemx LC_MONETARY
11731 @itemx LC_NUMERIC
11732 @itemx LC_TIME
11733 @evindex LANG
11734 @evindex LC_ALL
11735 @evindex LC_COLLATE
11736 @evindex LC_CTYPE
11737 @evindex LC_MESSAGES
11738 @evindex LC_MONETARY
11739 @evindex LC_NUMERIC
11740 @evindex LC_TIME
11742 Autoconf-generated scripts normally set all these variables to
11743 @samp{C} because so much configuration code assumes the C locale and
11744 Posix requires that locale environment variables be set to
11745 @samp{C} if the C locale is desired.  However, some older, nonstandard
11746 systems (notably @acronym{SCO}) break if locale environment variables
11747 are set to @samp{C}, so when running on these systems
11748 Autoconf-generated scripts unset the variables instead.
11750 @item LANGUAGE
11751 @evindex LANGUAGE
11753 @env{LANGUAGE} is not specified by Posix, but it is a @acronym{GNU}
11754 extension that overrides @env{LC_ALL} in some cases, so
11755 Autoconf-generated scripts set it too.
11757 @item LC_ADDRESS
11758 @itemx LC_IDENTIFICATION
11759 @itemx LC_MEASUREMENT
11760 @itemx LC_NAME
11761 @itemx LC_PAPER
11762 @itemx LC_TELEPHONE
11763 @evindex LC_ADDRESS
11764 @evindex LC_IDENTIFICATION
11765 @evindex LC_MEASUREMENT
11766 @evindex LC_NAME
11767 @evindex LC_PAPER
11768 @evindex LC_TELEPHONE
11770 These locale environment variables are @acronym{GNU} extensions.  They
11771 are treated like their Posix brethren (@env{LC_COLLATE},
11772 etc.)@: as described above.
11774 @item LINENO
11775 Most modern shells provide the current line number in @code{LINENO}.
11776 Its value is the line number of the beginning of the current command.
11777 Autoconf attempts to execute @command{configure} with a shell that
11778 supports @code{LINENO}.
11779 If no such shell is available, it attempts to implement @code{LINENO}
11780 with a Sed prepass that replaces each instance of the string
11781 @code{$LINENO} (not followed by an alphanumeric character) with the
11782 line's number.
11784 You should not rely on @code{LINENO} within @command{eval}, as the
11785 behavior differs in practice.  Also, the possibility of the Sed
11786 prepass means that you should not rely on @code{$LINENO} when quoted,
11787 when in here-documents, or when in long commands that cross line
11788 boundaries.  Subshells should be OK, though.  In the following
11789 example, lines 1, 6, and 9 are portable, but the other instances of
11790 @code{LINENO} are not:
11792 @example
11793 @group
11794 $ @kbd{cat lineno}
11795 echo 1. $LINENO
11796 cat <<EOF
11797 3. $LINENO
11798 4. $LINENO
11800 ( echo 6. $LINENO )
11801 eval 'echo 7. $LINENO'
11802 echo 8. '$LINENO'
11803 echo 9. $LINENO '
11804 10.' $LINENO
11805 @end group
11806 @group
11807 $ @kbd{bash-2.05 lineno}
11808 1. 1
11809 3. 2
11810 4. 2
11811 6. 6
11812 7. 1
11813 8. $LINENO
11814 9. 9
11815 10. 9
11816 @end group
11817 @group
11818 $ @kbd{zsh-3.0.6 lineno}
11819 1. 1
11820 3. 2
11821 4. 2
11822 6. 6
11823 7. 7
11824 8. $LINENO
11825 9. 9
11826 10. 9
11827 @end group
11828 @group
11829 $ @kbd{pdksh-5.2.14 lineno}
11830 1. 1
11831 3. 2
11832 4. 2
11833 6. 6
11834 7. 0
11835 8. $LINENO
11836 9. 9
11837 10. 9
11838 @end group
11839 @group
11840 $ @kbd{sed '=' <lineno |}
11841 > @kbd{  sed '}
11842 > @kbd{    N}
11843 > @kbd{    s,$,-,}
11844 > @kbd{    t loop}
11845 > @kbd{    :loop}
11846 > @kbd{    s,^\([0-9]*\)\(.*\)[$]LINENO\([^a-zA-Z0-9_]\),\1\2\1\3,}
11847 > @kbd{    t loop}
11848 > @kbd{    s,-$,,}
11849 > @kbd{    s,^[0-9]*\n,,}
11850 > @kbd{  ' |}
11851 > @kbd{  sh}
11852 1. 1
11853 3. 3
11854 4. 4
11855 6. 6
11856 7. 7
11857 8. 8
11858 9. 9
11859 10. 10
11860 @end group
11861 @end example
11863 @item NULLCMD
11864 @evindex NULLCMD
11865 When executing the command @samp{>foo}, @command{zsh} executes
11866 @samp{$NULLCMD >foo} unless it is operating in Bourne shell
11867 compatibility mode and the @command{zsh} version is newer
11868 than 3.1.6-dev-18.  If you are using an older @command{zsh}
11869 and forget to set @env{NULLCMD},
11870 your script might be suspended waiting for data on its standard input.
11872 @item PATH_SEPARATOR
11873 @evindex PATH_SEPARATOR
11874 On @acronym{DJGPP} systems, the @env{PATH_SEPARATOR} environment
11875 variable can be set to either @samp{:} or @samp{;} to control the path
11876 separator Bash uses to set up certain environment variables (such as
11877 @env{PATH}).  You can set this variable to @samp{;} if you want
11878 @command{configure} to use @samp{;} as a separator; this might be useful
11879 if you plan to use non-Posix shells to execute files.  @xref{File System
11880 Conventions}, for more information about @code{PATH_SEPARATOR}.
11882 @item PWD
11883 @evindex PWD
11884 Posix 1003.1-2001 requires that @command{cd} and
11885 @command{pwd} must update the @env{PWD} environment variable to point
11886 to the logical name of the current directory, but traditional shells
11887 do not support this.  This can cause confusion if one shell instance
11888 maintains @env{PWD} but a subsidiary and different shell does not know
11889 about @env{PWD} and executes @command{cd}; in this case @env{PWD} will
11890 point to the wrong directory.  Use @samp{`pwd`} rather than
11891 @samp{$PWD}.
11893 @item RANDOM
11894 Many shells provide @code{RANDOM}, a variable that returns a different
11895 integer each time it is used.  Most of the time, its value does not
11896 change when it is not used, but on @sc{irix} 6.5 the value changes all
11897 the time.  This can be observed by using @command{set}.  It is common
11898 practice to use @code{$RANDOM} as part of a file name, but code
11899 shouldn't rely on @code{$RANDOM} expanding to a nonempty string.
11901 @item status
11902 This variable is an alias to @samp{$?} for @code{zsh} (at least 3.1.6),
11903 hence read-only.  Do not use it.
11904 @end table
11906 @node Limitations of Builtins
11907 @section Limitations of Shell Builtins
11908 @cindex Shell builtins
11909 @cindex Limitations of shell builtins
11911 No, no, we are serious: some shells do have limitations!  :)
11913 You should always keep in mind that any builtin or command may support
11914 options, and therefore have a very different behavior with arguments
11915 starting with a dash.  For instance, the innocent @samp{echo "$word"}
11916 can give unexpected results when @code{word} starts with a dash.  It is
11917 often possible to avoid this problem using @samp{echo "x$word"}, taking
11918 the @samp{x} into account later in the pipe.
11920 @table @asis
11921 @item @command{.}
11922 @prindex @command{.}
11923 Use @command{.} only with regular files (use @samp{test -f}).  Bash
11924 2.03, for instance, chokes on @samp{. /dev/null}.  Also, remember that
11925 @command{.} uses @env{PATH} if its argument contains no slashes, so if
11926 you want to use @command{.} on a file @file{foo} in the current
11927 directory, you must use @samp{. ./foo}.
11929 @item @command{!}
11930 @prindex @command{!}
11931 The Unix version 7 shell did not support
11932 negating the exit status of commands with @command{!}, and this feature
11933 is still absent from some shells (e.g., Solaris @command{/bin/sh}).
11934 Shell code like this:
11936 @example
11937 if ! cmp file1 file2 >/dev/null 2>&1; then
11938   echo files differ or trouble
11940 @end example
11942 is therefore not portable in practice.  Typically it is easy to rewrite
11943 such code, e.g.:
11945 @example
11946 cmp file1 file2 >/dev/null 2>&1 ||
11947   echo files differ or trouble
11948 @end example
11950 More generally, one can always rewrite @samp{! @var{command}} as:
11952 @example
11953 if @var{command}; then (exit 1); else :; fi
11954 @end example
11956 @item @command{break}
11957 @c ------------------
11958 @prindex @command{break}
11959 The use of @samp{break 2} etc.@: is safe.
11962 @item @command{case}
11963 @c -----------------
11964 @prindex @command{case}
11965 You don't need to quote the argument; no splitting is performed.
11967 You don't need the final @samp{;;}, but you should use it.
11969 Because of a bug in its @code{fnmatch}, Bash fails to properly
11970 handle backslashes in character classes:
11972 @example
11973 bash-2.02$ @kbd{case /tmp in [/\\]*) echo OK;; esac}
11974 bash-2.02$
11975 @end example
11977 @noindent
11978 This is extremely unfortunate, since you are likely to use this code to
11979 handle Posix or @sc{ms-dos} absolute file names.  To work around this
11980 bug, always put the backslash first:
11982 @example
11983 bash-2.02$ @kbd{case '\TMP' in [\\/]*) echo OK;; esac}
11985 bash-2.02$ @kbd{case /tmp in [\\/]*) echo OK;; esac}
11987 @end example
11989 Many Bourne shells cannot handle closing brackets in character classes
11990 correctly.
11992 Some shells also have problems with backslash escaping in case you do not want
11993 to match the backslash: both a backslash and the escaped character match this
11994 pattern.  To work around this, specify the character class in a variable, so
11995 that quote removal does not apply afterwards, and the special characters don't
11996 have to be backslash-escaped:
11998 @example
11999 $ @kbd{case '\' in [\<]) echo OK;; esac}
12001 $ @kbd{scanset='[<]'; case '\' in $scanset) echo OK;; esac}
12003 @end example
12005 Even with this, Solaris @command{ksh} matches a backslash if the set
12006 contains any
12007 of the characters @samp{|}, @samp{&}, @samp{(}, or @samp{)}.
12009 Conversely, Tru64 @command{ksh} (circa 2003) erroneously always matches
12010 a closing parenthesis if not specified in a character class:
12012 @example
12013 $ @kbd{case foo in *\)*) echo fail ;; esac}
12014 fail
12015 $ @kbd{case foo in *')'*) echo fail ;; esac}
12016 fail
12017 @end example
12019 Some shells, such as Ash 0.3.8, are confused by an empty
12020 @code{case}/@code{esac}:
12022 @example
12023 ash-0.3.8 $ @kbd{case foo in esac;}
12024 @error{}Syntax error: ";" unexpected (expecting ")")
12025 @end example
12027 Many shells still do not support parenthesized cases, which is a pity
12028 for those of us using tools that rely on balanced parentheses.  For
12029 instance, Solaris @command{/bin/sh}:
12031 @example
12032 $ @kbd{case foo in (foo) echo foo;; esac}
12033 @error{}syntax error: `(' unexpected
12034 @end example
12037 @item @command{cd}
12038 @c ---------------
12039 @prindex @command{cd}
12040 Posix 1003.1-2001 requires that @command{cd} must support
12041 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
12042 with @option{-L} being the default.  However, traditional shells do
12043 not support these options, and their @command{cd} command has the
12044 @option{-P} behavior.
12046 Portable scripts should assume neither option is supported, and should
12047 assume neither behavior is the default.  This can be a bit tricky,
12048 since the Posix default behavior means that, for example,
12049 @samp{ls ..} and @samp{cd ..} may refer to different directories if
12050 the current logical directory is a symbolic link.  It is safe to use
12051 @command{cd @var{dir}} if @var{dir} contains no @file{..} components.
12052 Also, Autoconf-generated scripts check for this problem when computing
12053 variables like @code{ac_top_srcdir} (@pxref{Configuration Actions}),
12054 so it is safe to @command{cd} to these variables.
12056 Also please see the discussion of the @command{pwd} command.
12059 @item @command{echo}
12060 @c -----------------
12061 @prindex @command{echo}
12062 The simple @command{echo} is probably the most surprising source of
12063 portability troubles.  It is not possible to use @samp{echo} portably
12064 unless both options and escape sequences are omitted.  New applications
12065 which are not aiming at portability should use @samp{printf} instead of
12066 @samp{echo}.
12068 Don't expect any option.  @xref{Preset Output Variables}, @code{ECHO_N}
12069 etc.@: for a means to simulate @option{-n}.
12071 Do not use backslashes in the arguments, as there is no consensus on
12072 their handling.  For @samp{echo '\n' | wc -l}, the @command{sh} of
12073 Solaris outputs 2, but Bash and Zsh (in @command{sh} emulation mode) output 1.
12074 The problem is truly @command{echo}: all the shells
12075 understand @samp{'\n'} as the string composed of a backslash and an
12076 @samp{n}.
12078 Because of these problems, do not pass a string containing arbitrary
12079 characters to @command{echo}.  For example, @samp{echo "$foo"} is safe
12080 if you know that @var{foo}'s value cannot contain backslashes and cannot
12081 start with @samp{-}, but otherwise you should use a here-document like
12082 this:
12084 @example
12085 cat <<EOF
12086 $foo
12088 @end example
12091 @item @command{eval}
12092 @c -----------------
12093 @prindex @command{eval}
12094 The @command{eval} command is useful in limited circumstances, e.g.,
12095 using commands like @samp{eval table_$key=\$value} and @samp{eval
12096 value=table_$key} to simulate a hash table when the key is known to be
12097 alphanumeric.  However, @command{eval} is tricky to use on arbitrary
12098 arguments, even when it is implemented correctly.
12100 It is obviously unwise to use @samp{eval $cmd} if the string value of
12101 @samp{cmd} was derived from an untrustworthy source.  But even if the
12102 string value is valid, @samp{eval $cmd} might not work as intended,
12103 since it causes field splitting and file name expansion to occur twice,
12104 once for the @command{eval} and once for the command itself.  It is
12105 therefore safer to use @samp{eval "$cmd"}.  For example, if @var{cmd}
12106 has the value @samp{cat test?.c}, @samp{eval $cmd} might expand to the
12107 equivalent of @samp{cat test;.c} if there happens to be a file named
12108 @file{test;.c} in the current directory; and this in turn will
12109 mistakenly attempt to invoke @command{cat} on the file @file{test} and
12110 then execute the command @command{.c}.  To avoid this problem, use
12111 @samp{eval "$cmd"} rather than @samp{eval $cmd}.
12113 However, suppose that you want to output the text of the evaluated
12114 command just before executing it.  Assuming the previous example,
12115 @samp{echo "Executing: $cmd"} outputs @samp{Executing: cat test?.c}, but
12116 this output doesn't show the user that @samp{test;.c} is the actual name
12117 of the copied file.  Conversely, @samp{eval "echo Executing: $cmd"} will
12118 work on this example, but it will fail with @samp{cmd='cat foo >bar'},
12119 since it will mistakenly replace the contents of @file{bar} by the
12120 string @samp{cat foo}.  No simple, general, and portable solution to
12121 this problem is known.
12123 You should also be wary of common bugs in @command{eval} implementations.
12124 In some shell implementations (e.g., older @command{ash}, Open@acronym{BSD} 3.8
12125 @command{sh}, @command{pdksh} v5.2.14 99/07/13.2, and @command{zsh}
12126 4.2.5), the arguments of @samp{eval} are evaluated in a context where
12127 @samp{$?} is 0, so they exhibit behavior like this:
12129 @example
12130 $ @kbd{false; eval 'echo $?'}
12132 @end example
12134 The correct behavior here is to output a nonzero value,
12135 but portable scripts should not rely on this.
12137 You should not rely on @code{LINENO} within @command{eval}.
12138 @xref{Special Shell Variables}.
12140 @item @command{exit}
12141 @c -----------------
12142 @prindex @command{exit}
12143 The default value of @command{exit} is supposed to be @code{$?};
12144 unfortunately, some shells, such as the @acronym{DJGPP} port of Bash 2.04, just
12145 perform @samp{exit 0}.
12147 @example
12148 bash-2.04$ @kbd{foo=`exit 1` || echo fail}
12149 fail
12150 bash-2.04$ @kbd{foo=`(exit 1)` || echo fail}
12151 fail
12152 bash-2.04$ @kbd{foo=`(exit 1); exit` || echo fail}
12153 bash-2.04$
12154 @end example
12156 Using @samp{exit $?} restores the expected behavior.
12158 Some shell scripts, such as those generated by @command{autoconf}, use a
12159 trap to clean up before exiting.  If the last shell command exited with
12160 nonzero status, the trap also exits with nonzero status so that the
12161 invoker can tell that an error occurred.
12163 Unfortunately, in some shells, such as Solaris @command{/bin/sh}, an exit
12164 trap ignores the @code{exit} command's argument.  In these shells, a trap
12165 cannot determine whether it was invoked by plain @code{exit} or by
12166 @code{exit 1}.  Instead of calling @code{exit} directly, use the
12167 @code{AC_MSG_ERROR} macro that has a workaround for this problem.
12170 @item @command{export}
12171 @c -------------------
12172 @prindex @command{export}
12173 The builtin @command{export} dubs a shell variable @dfn{environment
12174 variable}.  Each update of exported variables corresponds to an update
12175 of the environment variables.  Conversely, each environment variable
12176 received by the shell when it is launched should be imported as a shell
12177 variable marked as exported.
12179 Alas, many shells, such as Solaris @command{/bin/sh},
12180 @sc{irix} 6.3, @sc{irix} 5.2,
12181 @acronym{AIX} 4.1.5, and Digital Unix 4.0, forget to
12182 @command{export} the environment variables they receive.  As a result,
12183 two variables coexist: the environment variable and the shell
12184 variable.  The following code demonstrates this failure:
12186 @example
12187 #!/bin/sh
12188 echo $FOO
12189 FOO=bar
12190 echo $FOO
12191 exec /bin/sh $0
12192 @end example
12194 @noindent
12195 when run with @samp{FOO=foo} in the environment, these shells will print
12196 alternately @samp{foo} and @samp{bar}, although it should only print
12197 @samp{foo} and then a sequence of @samp{bar}s.
12199 Therefore you should @command{export} again each environment variable
12200 that you update.
12203 @item @command{false}
12204 @c ------------------
12205 @prindex @command{false}
12206 Don't expect @command{false} to exit with status 1: in native
12207 Solaris @file{/bin/false} exits with status 255.
12210 @item @command{for}
12211 @c ----------------
12212 @prindex @command{for}
12213 To loop over positional arguments, use:
12215 @example
12216 for arg
12218   echo "$arg"
12219 done
12220 @end example
12222 @noindent
12223 You may @emph{not} leave the @code{do} on the same line as @code{for},
12224 since some shells improperly grok:
12226 @example
12227 for arg; do
12228   echo "$arg"
12229 done
12230 @end example
12232 If you want to explicitly refer to the positional arguments, given the
12233 @samp{$@@} bug (@pxref{Shell Substitutions}), use:
12235 @example
12236 for arg in $@{1+"$@@"@}; do
12237   echo "$arg"
12238 done
12239 @end example
12241 @noindent
12242 But keep in mind that Zsh, even in Bourne shell emulation mode, performs
12243 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
12244 item @samp{$@@}, for more.
12247 @item @command{if}
12248 @c ---------------
12249 @prindex @command{if}
12250 Using @samp{!} is not portable.  Instead of:
12252 @example
12253 if ! cmp -s file file.new; then
12254   mv file.new file
12256 @end example
12258 @noindent
12259 use:
12261 @example
12262 if cmp -s file file.new; then :; else
12263   mv file.new file
12265 @end example
12267 There are shells that do not reset the exit status from an @command{if}:
12269 @example
12270 $ @kbd{if (exit 42); then true; fi; echo $?}
12272 @end example
12274 @noindent
12275 whereas a proper shell should have printed @samp{0}.  This is especially
12276 bad in Makefiles since it produces false failures.  This is why properly
12277 written Makefiles, such as Automake's, have such hairy constructs:
12279 @example
12280 if test -f "$file"; then
12281   install "$file" "$dest"
12282 else
12283   :
12285 @end example
12288 @item @command{printf}
12289 @c ------------------
12290 @prindex @command{printf}
12291 A format string starting with a @samp{-} can cause problems.
12292 Bash (e.g., 2.05b) will interpret it as an options string and
12293 give an error.  And @samp{--} to mark the end of options is not good
12294 in the Net@acronym{BSD} Almquist shell (e.g., 0.4.6) which will take that
12295 literally as the format string.  Putting the @samp{-} in a @samp{%c}
12296 or @samp{%s} is probably the easiest way to avoid doubt,
12298 @example
12299 printf %s -foo
12300 @end example
12303 @item @command{read}
12304 @c ------------------
12305 @prindex @command{read}
12306 Not all shells support @option{-r} (Solaris @command{/bin/sh} for example).
12309 @item @command{pwd}
12310 @c ----------------
12311 @prindex @command{pwd}
12312 With modern shells, plain @command{pwd} outputs a ``logical''
12313 directory name, some of whose components may be symbolic links.  These
12314 directory names are in contrast to ``physical'' directory names, whose
12315 components are all directories.
12317 Posix 1003.1-2001 requires that @command{pwd} must support
12318 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
12319 with @option{-L} being the default.  However, traditional shells do
12320 not support these options, and their @command{pwd} command has the
12321 @option{-P} behavior.
12323 Portable scripts should assume neither option is supported, and should
12324 assume neither behavior is the default.  Also, on many hosts
12325 @samp{/bin/pwd} is equivalent to @samp{pwd -P}, but Posix
12326 does not require this behavior and portable scripts should not rely on
12329 Typically it's best to use plain @command{pwd}.  On modern hosts this
12330 outputs logical directory names, which have the following advantages:
12332 @itemize @bullet
12333 @item
12334 Logical names are what the user specified.
12335 @item
12336 Physical names may not be portable from one installation
12337 host to another due to network file system gymnastics.
12338 @item
12339 On modern hosts @samp{pwd -P} may fail due to lack of permissions to
12340 some parent directory, but plain @command{pwd} cannot fail for this
12341 reason.
12342 @end itemize
12344 Also please see the discussion of the @command{cd} command.
12347 @item @command{set}
12348 @c ----------------
12349 @prindex @command{set}
12350 With the Free@acronym{BSD} 6.0 shell, the @command{set} command (without
12351 any options) does not sort its output.
12353 The @command{set} builtin faces the usual problem with arguments starting with a
12354 dash.  Modern shells such as Bash or Zsh understand @option{--} to specify
12355 the end of the options (any argument after @option{--} is a parameter,
12356 even @samp{-x} for instance), but many traditional shells (e.g., Solaris
12357 10 @command{/bin/sh}) simply stop option
12358 processing as soon as a non-option argument is found.  Therefore, use
12359 @samp{dummy} or simply @samp{x} to end the option processing, and use
12360 @command{shift} to pop it out:
12362 @example
12363 set x $my_list; shift
12364 @end example
12366 Avoid @samp{set -}, e.g., @samp{set - $my_list}.  Posix no
12367 longer requires support for this command, and in traditional shells
12368 @samp{set - $my_list} resets the @option{-v} and @option{-x} options, which
12369 makes scripts harder to debug.
12371 Some nonstandard shells do not recognize more than one option
12372 (e.g., @samp{set -e -x} assigns @samp{-x} to the command line).  It is
12373 better to combine them:
12375 @example
12376 set -ex
12377 @end example
12379 The @acronym{BSD} shell has had several problems with the @option{-e}
12380 option, partly because @acronym{BSD} @command{make} traditionally used
12381 @option{-e} even though this was incompatible with Posix
12382 (@pxref{Limitations of Make}).  Older versions of the @acronym{BSD}
12383 shell (circa 1990) mishandled @samp{&&}, @samp{||}, @samp{if}, and
12384 @samp{case} when @option{-e} was in effect, causing the shell to exit
12385 unexpectedly in some cases.  This was particularly a problem with
12386 makefiles, and led to circumlocutions like @samp{sh -c 'test -f file ||
12387 touch file'}, where the seemingly-unnecessary @samp{sh -c '@dots{}'}
12388 wrapper works around the bug.
12390 Even relatively-recent versions of the @acronym{BSD} shell (e.g.,
12391 Open@acronym{BSD} 3.4) wrongly exit with @option{-e} if a command within
12392 @samp{&&} fails inside a compound statement.  For example:
12394 @example
12395 #! /bin/sh
12396 set -e
12397 foo=''
12398 test -n "$foo" && exit 1
12399 echo one
12400 if :; then
12401   test -n "$foo" && exit 1
12403 echo two
12404 @end example
12406 @noindent
12407 does not print @samp{two}.  One workaround is to use @samp{if test -n
12408 "$foo"; then exit 1; fi} rather than @samp{test -n "$foo" && exit 1}.
12409 Another possibility is to warn @acronym{BSD} users not to use @samp{sh -e}.
12412 @item @command{shift}
12413 @c ------------------
12414 @prindex @command{shift}
12415 Not only is @command{shift}ing a bad idea when there is nothing left to
12416 shift, but in addition it is not portable: the shell of @acronym{MIPS
12417 RISC/OS} 4.52 refuses to do it.
12419 Don't use @samp{shift 2} etc.; it was not in the 7th Edition Bourne shell,
12420 and it is also absent in many pre-Posix shells.
12423 @item @command{source}
12424 @c -------------------
12425 @prindex @command{source}
12426 This command is not portable, as Posix does not require it; use
12427 @command{.} instead.
12430 @item @command{test}
12431 @c -----------------
12432 @prindex @command{test}
12433 The @code{test} program is the way to perform many file and string
12434 tests.  It is often invoked by the alternate name @samp{[}, but using
12435 that name in Autoconf code is asking for trouble since it is an M4 quote
12436 character.
12438 If you need to make multiple checks using @code{test}, combine them with
12439 the shell operators @samp{&&} and @samp{||} instead of using the
12440 @code{test} operators @option{-a} and @option{-o}.  On System V, the
12441 precedence of @option{-a} and @option{-o} is wrong relative to the unary
12442 operators; consequently, Posix does not specify them, so using them
12443 is nonportable.  If you combine @samp{&&} and @samp{||} in the same
12444 statement, keep in mind that they have equal precedence.
12446 It is safe to use @samp{!} as a @command{test} operator.  For example,
12447 @samp{if test ! -d foo; @dots{}} is portable even though @samp{if ! test
12448 -d foo; @dots{}} is not.
12451 @item @command{test} (files)
12452 @c -------------------------
12453 To enable @command{configure} scripts to support cross-compilation, they
12454 shouldn't do anything that tests features of the build system instead of
12455 the host system.  But occasionally you may find it necessary to check
12456 whether some arbitrary file exists.  To do so, use @samp{test -f} or
12457 @samp{test -r}.  Do not use @samp{test -x}, because 4.3@acronym{BSD} does not
12458 have it.  Do not use @samp{test -e} either, because Solaris @command{/bin/sh}
12459 lacks it.  To test for symbolic links on systems that have them, use
12460 @samp{test -h} rather than @samp{test -L}; either form conforms to
12461 Posix 1003.1-2001, but older shells like Solaris 8
12462 @code{/bin/sh} support only @option{-h}.
12464 @item @command{test} (strings)
12465 @c ---------------------------
12466 Avoid @samp{test "@var{string}"}, in particular if @var{string} might
12467 start with a dash, since @code{test} might interpret its argument as an
12468 option (e.g., @samp{@var{string} = "-n"}).
12470 Contrary to a common belief, @samp{test -n @var{string}} and
12471 @samp{test -z @var{string}} @strong{are} portable.  Nevertheless many
12472 shells (such as Solaris, @acronym{AIX} 3.2, @sc{unicos} 10.0.0.6,
12473 Digital Unix 4, etc.)@: have bizarre precedence and may be confused if
12474 @var{string} looks like an operator:
12476 @example
12477 $ @kbd{test -n =}
12478 test: argument expected
12479 @end example
12481 If there are risks, use @samp{test "x@var{string}" = x} or @samp{test
12482 "x@var{string}" != x} instead.
12484 It is common to find variations of the following idiom:
12486 @example
12487 test -n "`echo $ac_feature | sed 's/[-a-zA-Z0-9_]//g'`" &&
12488   @var{action}
12489 @end example
12491 @noindent
12492 to take an action when a token matches a given pattern.  Such constructs
12493 should always be avoided by using:
12495 @example
12496 echo "$ac_feature" | grep '[^-a-zA-Z0-9_]' >/dev/null 2>&1 &&
12497   @var{action}
12498 @end example
12500 @noindent
12501 Use @code{case} where possible since it is faster, being a shell builtin:
12504 @example
12505 case $ac_feature in
12506   *[!-a-zA-Z0-9_]*) @var{action};;
12507 esac
12508 @end example
12510 Alas, negated character classes are probably not portable, although no
12511 shell is known to not support the Posix syntax @samp{[!@dots{}]}
12512 (when in interactive mode, @command{zsh} is confused by the
12513 @samp{[!@dots{}]} syntax and looks for an event in its history because of
12514 @samp{!}).  Many shells do not support the alternative syntax
12515 @samp{[^@dots{}]} (Solaris, Digital Unix, etc.).
12517 One solution can be:
12519 @example
12520 expr "$ac_feature" : '.*[^-a-zA-Z0-9_]' >/dev/null &&
12521   @var{action}
12522 @end example
12524 @noindent
12525 or better yet
12527 @example
12528 expr "X$ac_feature" : '.*[^-a-zA-Z0-9_]' >/dev/null &&
12529   @var{action}
12530 @end example
12532 @samp{expr "X@var{foo}" : "X@var{bar}"} is more robust than @samp{echo
12533 "X@var{foo}" | grep "^X@var{bar}"}, because it avoids problems when
12534 @samp{@var{foo}} contains backslashes.
12537 @item @command{trap}
12538 @c -----------------
12539 @prindex @command{trap}
12540 It is safe to trap at least the signals 1, 2, 13, and 15.  You can also
12541 trap 0, i.e., have the @command{trap} run when the script ends (either via an
12542 explicit @command{exit}, or the end of the script).
12544 Posix says that @samp{trap - 1 2 13 15} resets the traps for the
12545 specified signals to their default values, but many common shells (e.g.,
12546 Solaris @command{/bin/sh}) misinterpret this and attempt to execute a
12547 ``command'' named @command{-} when the specified conditions arise.
12548 There is no portable workaround, except for @samp{trap - 0}, for which
12549 @samp{trap '' 0} is a portable substitute.
12551 Although Posix is not absolutely clear on this point, it is widely
12552 admitted that when entering the trap @samp{$?} should be set to the exit
12553 status of the last command run before the trap.  The ambiguity can be
12554 summarized as: ``when the trap is launched by an @command{exit}, what is
12555 the @emph{last} command run: that before @command{exit}, or
12556 @command{exit} itself?''
12558 Bash considers @command{exit} to be the last command, while Zsh and
12559 Solaris @command{/bin/sh} consider that when the trap is run it is
12560 @emph{still} in the @command{exit}, hence it is the previous exit status
12561 that the trap receives:
12563 @example
12564 $ @kbd{cat trap.sh}
12565 trap 'echo $?' 0
12566 (exit 42); exit 0
12567 $ @kbd{zsh trap.sh}
12569 $ @kbd{bash trap.sh}
12571 @end example
12573 The portable solution is then simple: when you want to @samp{exit 42},
12574 run @samp{(exit 42); exit 42}, the first @command{exit} being used to
12575 set the exit status to 42 for Zsh, and the second to trigger the trap
12576 and pass 42 as exit status for Bash.
12578 The shell in Free@acronym{BSD} 4.0 has the following bug: @samp{$?} is
12579 reset to 0 by empty lines if the code is inside @command{trap}.
12581 @example
12582 $ @kbd{trap 'false}
12584 echo $?' 0
12585 $ @kbd{exit}
12587 @end example
12589 @noindent
12590 Fortunately, this bug only affects @command{trap}.
12592 @item @command{true}
12593 @c -----------------
12594 @prindex @command{true}
12595 @c Info cannot handle `:' in index entries.
12596 @c @prindex @command{:}
12597 Don't worry: as far as we know @command{true} is portable.
12598 Nevertheless, it's not always a builtin (e.g., Bash 1.x), and the
12599 portable shell community tends to prefer using @command{:}.  This has a
12600 funny side effect: when asked whether @command{false} is more portable
12601 than @command{true} Alexandre Oliva answered:
12603 @quotation
12604 In a sense, yes, because if it doesn't exist, the shell will produce an
12605 exit status of failure, which is correct for @command{false}, but not
12606 for @command{true}.
12607 @end quotation
12610 @item @command{unset}
12611 @c ------------------
12612 @prindex @command{unset}
12613 You cannot assume the support of @command{unset}.  Nevertheless, because
12614 it is extremely useful to disable embarrassing variables such as
12615 @code{PS1}, you can test for its existence and use
12616 it @emph{provided} you give a neutralizing value when @command{unset} is
12617 not supported:
12619 @example
12620 if (unset FOO) >/dev/null 2>&1; then
12621   unset=unset
12622 else
12623   unset=false
12625 $unset PS1 || PS1='$ '
12626 @end example
12628 @xref{Special Shell Variables}, for some neutralizing values.  Also, see
12629 @ref{Limitations of Builtins}, documentation of @command{export}, for
12630 the case of environment variables.
12631 @end table
12633 @node Limitations of Usual Tools
12634 @section Limitations of Usual Tools
12635 @cindex Limitations of usual tools
12637 The small set of tools you can expect to find on any machine can still
12638 include some limitations you should be aware of.
12640 @table @asis
12641 @item Awk
12642 @c ----------------
12643 @prindex Awk
12644 Don't leave white space before the opening parenthesis in a user function call.
12645 Posix does not allow this and @acronym{GNU} Awk rejects it:
12647 @example
12648 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
12649         BEGIN @{ die () @}'}
12650 gawk: cmd. line:2:         BEGIN @{ die () @}
12651 gawk: cmd. line:2:                      ^ parse error
12652 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
12653         BEGIN @{ die() @}'}
12654 Aaaaarg!
12655 @end example
12657 If you want your program to be deterministic, don't depend on @code{for}
12658 on arrays:
12660 @example
12661 $ @kbd{cat for.awk}
12662 END @{
12663   arr["foo"] = 1
12664   arr["bar"] = 1
12665   for (i in arr)
12666     print i
12668 $ @kbd{gawk -f for.awk </dev/null}
12671 $ @kbd{nawk -f for.awk </dev/null}
12674 @end example
12676 Some Awk implementations, such as HP-UX 11.0's native one, mishandle anchors:
12678 @example
12679 $ @kbd{echo xfoo | $AWK '/foo|^bar/ @{ print @}'}
12680 $ @kbd{echo bar | $AWK '/foo|^bar/ @{ print @}'}
12682 $ @kbd{echo xfoo | $AWK '/^bar|foo/ @{ print @}'}
12683 xfoo
12684 $ @kbd{echo bar | $AWK '/^bar|foo/ @{ print @}'}
12686 @end example
12688 @noindent
12689 Either do not depend on such patterns (i.e., use @samp{/^(.*foo|bar)/},
12690 or use a simple test to reject such implementations.
12692 @acronym{AIX} version 5.2 has an arbitrary limit of 399 on the the
12693 length of regular expressions and literal strings in an Awk program.
12695 Traditional Awk implementations derived from Unix version 7, such as
12696 Solaris @command{/bin/awk}, have many limitations and do not
12697 conform to Posix.  Nowadays @code{AC_PROG_AWK} (@pxref{Particular
12698 Programs}) will find you an Awk that doesn't have these problems, but if
12699 for some reason you prefer not to use @code{AC_PROG_AWK} you may need to
12700 address them.
12702 Traditional Awk does not support multidimensional arrays or user-defined
12703 functions.
12705 Traditional Awk does not support the @option{-v} option.  You can use
12706 assignments after the program instead, e.g., @command{$AWK '@{print v
12707 $1@}' v=x}; however, don't forget that such assignments are not
12708 evaluated until they are encountered (e.g., after any @code{BEGIN}
12709 action).
12711 Traditional Awk does not support the keywords @code{delete} or @code{do}.
12713 Traditional Awk does not support the expressions
12714 @code{@var{a}?@var{b}:@var{c}}, @code{!@var{a}}, @code{@var{a}^@var{b}},
12715 or @code{@var{a}^=@var{b}}.
12717 Traditional Awk does not support the predefined @code{CONVFMT} variable.
12719 Traditional Awk supports only the predefined functions @code{exp},
12720 @code{int}, @code{length}, @code{log}, @code{split}, @code{sprintf},
12721 @code{sqrt}, and @code{substr}.
12723 Traditional Awk @code{getline} is not at all compatible with Posix;
12724 avoid it.
12726 Traditional Awk @code{split} supports only two arguments.
12728 Traditional Awk has a limit of 99
12729 fields in a record.  You may be able to circumvent this problem by using
12730 @code{split}.
12733 @item @command{basename}
12734 @c ---------------------
12735 @prindex @command{basename}
12736 Not all hosts have a working @command{basename}.
12737 You can use @command{expr} instead.
12739 @c AS_BASENAME is to be replaced by a better API.
12740 @ignore
12741 Not all hosts have a working @command{basename}, and you should instead
12742 use @code{AS_BASENAME} (@pxref{Programming in M4sh}), followed by
12743 @command{expr} if you need to strip a suffix.  For example:
12745 @example
12746 a=`basename "$aname"`       # This is not portable.
12747 a=`AS_BASENAME(["$aname"])` # This is more portable.
12749 # This is not portable.
12750 c=`basename "$cname" .c`
12752 # This is more portable.
12753 c=`AS_BASENAME(["$cname"])`
12754 case $c in
12755 ?*.c) c=`expr "X$c" : 'X\(.*\)\.c'`;;
12756 esac
12757 @end example
12758 @end ignore
12761 @item @command{cat}
12762 @c ----------------
12763 @prindex @command{cat}
12764 Don't rely on any option.
12767 @item @command{cc}
12768 @c ---------------
12769 @prindex @command{cc}
12770 The command @samp{cc -c foo.c} traditionally produces an object file
12771 named @file{foo.o}.  Most compilers allow @option{-c} to be combined
12772 with @option{-o} to specify a different object file name, but
12773 Posix does not require this combination and a few compilers
12774 lack support for it.  @xref{C Compiler}, for how @acronym{GNU} Make
12775 tests for this feature with @code{AC_PROG_CC_C_O}.
12777 When a compilation such as @samp{cc -o foo foo.c} fails, some compilers
12778 (such as @sc{cds} on Reliant Unix) leave a @file{foo.o}.
12780 HP-UX @command{cc} doesn't accept @file{.S} files to preprocess and
12781 assemble.  @samp{cc -c foo.S} will appear to succeed, but in fact does
12782 nothing.
12784 The default executable, produced by @samp{cc foo.c}, can be
12786 @itemize
12787 @item @file{a.out} --- usual Posix convention.
12788 @item @file{b.out} --- i960 compilers (including @command{gcc}).
12789 @item @file{a.exe} --- @acronym{DJGPP} port of @command{gcc}.
12790 @item @file{a_out.exe} --- GNV @command{cc} wrapper for DEC C on OpenVMS.
12791 @item @file{foo.exe} --- various MS-DOS compilers.
12792 @end itemize
12794 The C compiler's traditional name is @command{cc}, but other names like
12795 @command{gcc} are common.  Posix 1003.1-2001 specifies the
12796 name @command{c99}, but older Posix editions specified
12797 @command{c89} and anyway these standard names are rarely used in
12798 practice.  Typically the C compiler is invoked from makefiles that use
12799 @samp{$(CC)}, so the value of the @samp{CC} make variable selects the
12800 compiler name.
12803 @item @command{chmod}
12804 @c ------------------
12805 @prindex @command{chmod}
12806 Avoid usages like @samp{chmod -w file}; use @samp{chmod a-w file}
12807 instead, for two reasons.  First, plain @option{-w} does not necessarily
12808 make the file unwritable, since it does not affect mode bits that
12809 correspond to bits in the file mode creation mask.  Second,
12810 Posix says that the @option{-w} might be interpreted as an
12811 implementation-specific option, not as a mode; Posix suggests
12812 using @samp{chmod -- -w file} to avoid this confusion, but unfortunately
12813 @samp{--} does not work on some older hosts.
12816 @item @command{cmp}
12817 @c ----------------
12818 @prindex @command{cmp}
12819 @command{cmp} performs a raw data comparison of two files, while
12820 @command{diff} compares two text files.  Therefore, if you might compare
12821 DOS files, even if only checking whether two files are different, use
12822 @command{diff} to avoid spurious differences due to differences of
12823 newline encoding.
12826 @item @command{cp}
12827 @c ---------------
12828 @prindex @command{cp}
12829 Avoid the @option{-r} option, since Posix 1003.1-2004 marks it as
12830 obsolescent and its behavior on special files is implementation-defined.
12831 Use @option{-R} instead.  On @acronym{GNU} hosts the two options
12832 are equivalent, but on Solaris hosts (for example) @command{cp -r}
12833 reads from pipes instead of replicating them.
12835 Some @command{cp} implementations (e.g., @acronym{BSD/OS} 4.2) do not allow
12836 trailing slashes at the end of nonexistent destination directories.  To
12837 avoid this problem, omit the trailing slashes.  For example, use
12838 @samp{cp -R source /tmp/newdir} rather than @samp{cp -R source
12839 /tmp/newdir/} if @file{/tmp/newdir} does not exist.
12841 @c This is thanks to Ian.
12842 The ancient SunOS 4 @command{cp} does not support @option{-f}, although
12843 its @command{mv} does.
12845 @cindex timestamp resolution
12846 Traditionally, file timestamps had 1-second resolution, and @samp{cp
12847 -p} copied the timestamps exactly.  However, many modern file systems
12848 have timestamps with 1-nanosecond resolution.  Unfortunately, @samp{cp
12849 -p} implementations truncate timestamps when copying files, so this
12850 can result in the destination file appearing to be older than the
12851 source.  The exact amount of truncation depends on the resolution of
12852 the system calls that @command{cp} uses; traditionally this was
12853 @code{utime}, which has 1-second resolution, but some newer
12854 @command{cp} implementations use @code{utimes}, which has
12855 1-microsecond resolution.  These newer implementations include @acronym{GNU}
12856 Core Utilities 5.0.91 or later, and Solaris 8 (sparc) patch 109933-02 or
12857 later.  Unfortunately as of January 2006 there is still no system
12858 call to set time stamps to the full nanosecond resolution.
12860 Bob Proulx notes that @samp{cp -p} always @emph{tries} to copy
12861 ownerships.  But whether it actually does copy ownerships or not is a
12862 system dependent policy decision implemented by the kernel.  If the
12863 kernel allows it then it happens.  If the kernel does not allow it then
12864 it does not happen.  It is not something @command{cp} itself has control
12865 over.
12867 In Unix System V any user can chown files to any other user, and System
12868 V also has a non-sticky @file{/tmp}.  That probably derives from the
12869 heritage of System V in a business environment without hostile users.
12870 @acronym{BSD} changed this
12871 to be a more secure model where only root can @command{chown} files and
12872 a sticky @file{/tmp} is used.  That undoubtedly derives from the heritage
12873 of @acronym{BSD} in a campus environment.
12875 @acronym{GNU}/Linux and Solaris by default follow @acronym{BSD}, but
12876 can be configured to allow a System V style @command{chown}.  On the
12877 other hand, @acronym{HP-UX} follows System V, but can
12878 be configured to use the modern security model and disallow
12879 @command{chown}.  Since it is an administrator-configurable parameter
12880 you can't use the name of the kernel as an indicator of the behavior.
12884 @item @command{date}
12885 @c -----------------
12886 @prindex @command{date}
12887 Some versions of @command{date} do not recognize special @samp{%} directives,
12888 and unfortunately, instead of complaining, they just pass them through,
12889 and exit with success:
12891 @example
12892 $ @kbd{uname -a}
12893 OSF1 medusa.sis.pasteur.fr V5.1 732 alpha
12894 $ @kbd{date "+%s"}
12896 @end example
12899 @item @command{diff}
12900 @c -----------------
12901 @prindex @command{diff}
12902 Option @option{-u} is nonportable.
12904 Some implementations, such as Tru64's, fail when comparing to
12905 @file{/dev/null}.  Use an empty file instead.
12908 @item @command{dirname}
12909 @c --------------------
12910 @prindex @command{dirname}
12911 Not all hosts have a working @command{dirname}, and you should instead
12912 use @code{AS_DIRNAME} (@pxref{Programming in M4sh}).  For example:
12914 @example
12915 dir=`dirname "$file"`       # This is not portable.
12916 dir=`AS_DIRNAME(["$file"])` # This is more portable.
12917 @end example
12920 @item @command{egrep}
12921 @c ------------------
12922 @prindex @command{egrep}
12923 Posix 1003.1-2001 no longer requires @command{egrep},
12924 but many older hosts do not yet support the Posix
12925 replacement @code{grep -E}.  Also, some traditional implementations do
12926 not work on long input lines.  To work around these problems, invoke
12927 @code{AC_PROG_EGREP} and then use @code{$EGREP}.
12929 Portable extended regular expressions should use @samp{\} only to escape
12930 characters in the string @samp{$()*+.?[\^@{|}.  For example, @samp{\@}}
12931 is not portable, even though it typically matches @samp{@}}.
12933 The empty alternative is not portable.  Use @samp{?} instead.  For
12934 instance with Digital Unix v5.0:
12936 @example
12937 > printf "foo\n|foo\n" | $EGREP '^(|foo|bar)$'
12938 |foo
12939 > printf "bar\nbar|\n" | $EGREP '^(foo|bar|)$'
12940 bar|
12941 > printf "foo\nfoo|\n|bar\nbar\n" | $EGREP '^(foo||bar)$'
12943 |bar
12944 @end example
12946 @command{$EGREP} also suffers the limitations of @command{grep}.
12948 @item @command{expr}
12949 @c -----------------
12950 @prindex @command{expr}
12951 No @command{expr} keyword starts with @samp{X}, so use @samp{expr
12952 X"@var{word}" : 'X@var{regex}'} to keep @command{expr} from
12953 misinterpreting @var{word}.
12955 Don't use @code{length}, @code{substr}, @code{match} and @code{index}.
12957 @item @command{expr} (@samp{|})
12958 @prindex @command{expr} (@samp{|})
12959 You can use @samp{|}.  Although Posix does require that @samp{expr
12960 ''} return the empty string, it does not specify the result when you
12961 @samp{|} together the empty string (or zero) with the empty string.  For
12962 example:
12964 @example
12965 expr '' \| ''
12966 @end example
12968 Posix 1003.2-1992 returns the empty string
12969 for this case, but traditional Unix returns @samp{0} (Solaris is
12970 one such example).  In Posix 1003.1-2001, the specification was
12971 changed to match traditional Unix's behavior (which is
12972 bizarre, but it's too late to fix this).  Please note that the same
12973 problem does arise when the empty string results from a computation,
12974 as in:
12976 @example
12977 expr bar : foo \| foo : bar
12978 @end example
12980 @noindent
12981 Avoid this portability problem by avoiding the empty string.
12984 @item @command{expr} (@samp{:})
12985 @c ----------------------------
12986 @prindex @command{expr}
12987 Portable @command{expr} regular expressions should use @samp{\} to
12988 escape only characters in the string @samp{$()*.0123456789[\^n@{@}}.
12989 For example, alternation, @samp{\|}, is common but Posix does not
12990 require its support, so it should be avoided in portable scripts.
12991 Similarly, @samp{\+} and @samp{\?} should be avoided.
12993 Portable @command{expr} regular expressions should not begin with
12994 @samp{^}.  Patterns are automatically anchored so leading @samp{^} is
12995 not needed anyway.
12997 The Posix standard is ambiguous as to whether
12998 @samp{expr 'a' : '\(b\)'} outputs @samp{0} or the empty string.
12999 In practice, it outputs the empty string on most platforms, but portable
13000 scripts should not assume this.  For instance, the @acronym{QNX} 4.25 native
13001 @command{expr} returns @samp{0}.
13003 One might think that a way to get a uniform behavior would be to use
13004 the empty string as a default value:
13006 @example
13007 expr a : '\(b\)' \| ''
13008 @end example
13010 @noindent
13011 Unfortunately this behaves exactly as the original expression; see the
13012 @samp{@command{expr} (@samp{|})} entry for more information.
13014 Ancient @command{expr} implementations (e.g., SunOS 4 @command{expr} and
13015 Solaris 8 @command{/usr/ucb/expr}) have a silly length limit that causes
13016 @command{expr} to fail if the matched substring is longer than 120
13017 bytes.  In this case, you might want to fall back on @samp{echo|sed} if
13018 @command{expr} fails.  Nowadays this is of practical importance only for
13019 the rare installer who mistakenly puts @file{/usr/ucb} before
13020 @file{/usr/bin} in @env{PATH}.
13022 On Mac OS X 10.4, @command{expr} mishandles the pattern @samp{[^-]} in
13023 some cases.  For example, the command
13024 @example
13025 expr Xpowerpc-apple-darwin8.1.0 : 'X[^-]*-[^-]*-\(.*\)'
13026 @end example
13028 @noindent
13029 outputs @samp{apple-darwin8.1.0} rather than the correct @samp{darwin8.1.0}.
13030 This particular case can be worked around by substituting @samp{[^--]}
13031 for @samp{[^-]}.
13033 Don't leave, there is some more!
13035 The @acronym{QNX} 4.25 @command{expr}, in addition of preferring @samp{0} to
13036 the empty string, has a funny behavior in its exit status: it's always 1
13037 when parentheses are used!
13039 @example
13040 $ @kbd{val=`expr 'a' : 'a'`; echo "$?: $val"}
13041 0: 1
13042 $ @kbd{val=`expr 'a' : 'b'`; echo "$?: $val"}
13043 1: 0
13045 $ @kbd{val=`expr 'a' : '\(a\)'`; echo "?: $val"}
13046 1: a
13047 $ @kbd{val=`expr 'a' : '\(b\)'`; echo "?: $val"}
13048 1: 0
13049 @end example
13051 @noindent
13052 In practice this can be a big problem if you are ready to catch failures
13053 of @command{expr} programs with some other method (such as using
13054 @command{sed}), since you may get twice the result.  For instance
13056 @example
13057 $ @kbd{expr 'a' : '\(a\)' || echo 'a' | sed 's/^\(a\)$/\1/'}
13058 @end example
13060 @noindent
13061 will output @samp{a} on most hosts, but @samp{aa} on @acronym{QNX} 4.25.  A
13062 simple workaround consists in testing @command{expr} and use a variable
13063 set to @command{expr} or to @command{false} according to the result.
13065 Tru64 @command{expr} incorrectly treats the result as a number, if it
13066 can be interpreted that way:
13068 @example
13069 $ @kbd{expr 00001 : '.*\(...\)'}
13071 @end example
13074 @item @command{fgrep}
13075 @c ------------------
13076 @prindex @command{fgrep}
13077 Posix 1003.1-2001 no longer requires @command{fgrep},
13078 but many older hosts do not yet support the Posix
13079 replacement @code{grep -F}.  Also, some traditional implementations do
13080 not work on long input lines.  To work around these problems, invoke
13081 @code{AC_PROG_FGREP} and then use @code{$FGREP}.
13084 @item @command{find}
13085 @c -----------------
13086 @prindex @command{find}
13087 The option @option{-maxdepth} seems to be @acronym{GNU} specific.
13088 Tru64 v5.1, Net@acronym{BSD} 1.5 and Solaris @command{find}
13089 commands do not understand it.
13091 The replacement of @samp{@{@}} is guaranteed only if the argument is
13092 exactly @emph{@{@}}, not if it's only a part of an argument.  For
13093 instance on DU, and HP-UX 10.20 and HP-UX 11:
13095 @example
13096 $ @kbd{touch foo}
13097 $ @kbd{find . -name foo -exec echo "@{@}-@{@}" \;}
13098 @{@}-@{@}
13099 @end example
13101 @noindent
13102 while @acronym{GNU} @command{find} reports @samp{./foo-./foo}.
13105 @item @command{grep}
13106 @c -----------------
13107 @prindex @command{grep}
13108 Portable scripts can rely on the @command{grep} options @option{-c},
13109 @option{-l}, @option{-n}, and @option{-v}, but should avoid other
13110 options.  For example, don't use @option{-w}, as Posix does not require
13111 it and Irix 6.5.16m's @command{grep} does not support it.  Also,
13112 portable scripts should not combine @option{-c} with @option{-l},
13113 as Posix does not allow this.
13115 Some of the options required by Posix are not portable in practice.
13116 Don't use @samp{grep -q} to suppress output, because many @command{grep}
13117 implementations (e.g., Solaris) do not support @option{-q}.
13118 Don't use @samp{grep -s} to suppress output either, because Posix
13119 says @option{-s} does not suppress output, only some error messages;
13120 also, the @option{-s} option of traditional @command{grep} behaved
13121 like @option{-q} does in most modern implementations.  Instead,
13122 redirect the standard output and standard error (in case the file
13123 doesn't exist) of @code{grep} to @file{/dev/null}.  Check the exit
13124 status of @code{grep} to determine whether it found a match.
13126 Some traditional @command{grep} implementations do not work on long
13127 input lines.  On AIX the default @code{grep} silently truncates long
13128 lines on the input before matching.
13130 Also, many implementations do not support multiple regexps
13131 with @option{-e}: they either reject @option{-e} entirely (e.g., Solaris)
13132 or honor only the last pattern (e.g., @acronym{IRIX} 6.5 and NeXT).  To
13133 work around these problems, invoke @code{AC_PROG_GREP} and then use
13134 @code{$GREP}.
13136 Another possible workaround for the multiple @option{-e} problem is to
13137 separate the patterns by newlines, for example:
13139 @example
13140 grep 'foo
13141 bar' in.txt
13142 @end example
13144 @noindent
13145 except that this will fail with traditional @command{grep}
13146 implementations and with Open@acronym{BSD} 3.8 @command{grep}.
13148 Traditional @command{grep} implementations (e.g., Solaris) do not
13149 support the @option{-E} or @option{-F} options.  To work around these
13150 problems, invoke @code{AC_PROG_EGREP} and then use @code{$EGREP}, and
13151 similarly for @code{AC_PROG_FGREP} and @code{$FGREP}.  Even if you are
13152 willing to require support for Posix @command{grep}, your script should
13153 not use both @option{-E} and @option{-F}, since Posix does not allow
13154 this combination.
13156 Portable @command{grep} regular expressions should use @samp{\} only to
13157 escape characters in the string @samp{$()*.0123456789[\^@{@}}.  For example,
13158 alternation, @samp{\|}, is common but Posix does not require its
13159 support in basic regular expressions, so it should be avoided in
13160 portable scripts.  Solaris @command{grep} does not support it.
13161 Similarly, @samp{\+} and @samp{\?} should be avoided.
13164 @item @command{join}
13165 @c -----------------
13166 @prindex @command{join}
13167 Solaris 8 @command{join} has bugs when the second operand is standard
13168 input, and when standard input is a pipe.  For example, the following
13169 shell script causes Solaris 8 @command{join} to loop forever:
13171 @example
13172 cat >file <<'EOF'
13173 1 x
13174 2 y
13176 cat file | join file -
13177 @end example
13179 Use @samp{join - file} instead.
13182 @item @command{ln}
13183 @c ---------------
13184 @prindex @command{ln}
13185 @cindex Symbolic links
13186 Don't rely on @command{ln} having a @option{-f} option.  Symbolic links
13187 are not available on old systems; use @samp{$(LN_S)} as a portable substitute.
13189 For versions of the @acronym{DJGPP} before 2.04,
13190 @command{ln} emulates symbolic links
13191 to executables by generating a stub that in turn calls the real
13192 program.  This feature also works with nonexistent files like in the
13193 Posix spec.  So @samp{ln -s file link} will generate @file{link.exe},
13194 which will attempt to call @file{file.exe} if run.  But this feature only
13195 works for executables, so @samp{cp -p} is used instead for these
13196 systems.  @acronym{DJGPP} versions 2.04 and later have full support
13197 for symbolic links.
13200 @item @command{ls}
13201 @c ---------------
13202 @prindex @command{ls}
13203 @cindex Listing directories
13204 The portable options are @option{-acdilrtu}.  Current practice is for
13205 @option{-l} to output both owner and group, even though ancient versions
13206 of @command{ls} omitted the group.
13208 On ancient hosts, @samp{ls foo} sent the diagnostic @samp{foo not found}
13209 to standard output if @file{foo} did not exist.  Hence a shell command
13210 like @samp{sources=`ls *.c 2>/dev/null`} did not always work, since it
13211 was equivalent to @samp{sources='*.c not found'} in the absence of
13212 @samp{.c} files.  This is no longer a practical problem, since current
13213 @command{ls} implementations send diagnostics to standard error.
13215 @item @command{mkdir}
13216 @c ------------------
13217 @prindex @command{mkdir}
13218 @cindex Making directories
13219 None of @command{mkdir}'s options are portable to older systems.  Instead of
13220 @samp{mkdir -p @var{file-name}}, you should use use
13221 @code{AS_MKDIR_P(@var{file-name})} (@pxref{Programming in M4sh})
13222 or @code{AC_PROG_MKDIR_P} (@pxref{Particular Programs}).
13224 Posix does not clearly specify whether @samp{mkdir -p foo}
13225 should succeed when @file{foo} is a symbolic link to an already-existing
13226 directory.  The @acronym{GNU} Core Utilities 5.1.0 @command{mkdir}
13227 succeeds, but Solaris @command{mkdir} fails.
13229 Traditional @code{mkdir -p} implementations suffer from race conditions.
13230 For example, if you invoke @code{mkdir -p a/b} and @code{mkdir -p a/c}
13231 at the same time, both processes might detect that @file{a} is missing,
13232 one might create @file{a}, then the other might try to create @file{a}
13233 and fail with a @code{File exists} diagnostic.  The @acronym{GNU} Core
13234 Utilities (@samp{fileutils} version 4.1), Free@acronym{BSD} 5.0,
13235 Net@acronym{BSD} 2.0.2, and Open@acronym{BSD} 2.4 are known to be
13236 race-free when two processes invoke @code{mkdir -p} simultaneously, but
13237 earlier versions are vulnerable.  Solaris @command{mkdir} is still
13238 vulnerable as of Solaris 10, and other traditional Unix systems are
13239 probably vulnerable too.  This possible race is harmful in parallel
13240 builds when several @file{Makefile} rules call @code{mkdir -p} to
13241 construct directories.  You may use
13242 @code{install-sh -d} as a safe replacement, provided this script is
13243 recent enough; the copy shipped with Autoconf 2.60 and Automake 1.10 is
13244 OK, but copies from older versions are vulnerable.
13247 @item @command{mktemp}
13248 @c -------------------
13249 @prindex @command{mktemp}
13250 @cindex Creating temporary files
13251 Shell scripts can use temporary files safely with @command{mktemp}, but
13252 it does not exist on all systems.  A portable way to create a safe
13253 temporary file name is to create a temporary directory with mode 700 and
13254 use a file inside this directory.  Both methods prevent attackers from
13255 gaining control, though @command{mktemp} is far less likely to fail
13256 gratuitously under attack.
13258 Here is sample code to create a new temporary directory safely:
13260 @example
13261 # Create a temporary directory $tmp in $TMPDIR (default /tmp).
13262 # Use mktemp if possible; otherwise fall back on mkdir,
13263 # with $RANDOM to make collisions less likely.
13264 : $@{TMPDIR=/tmp@}
13266   tmp=`
13267     (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
13268   ` &&
13269   test -n "$tmp" && test -d "$tmp"
13270 @} || @{
13271   tmp=$TMPDIR/foo$$-$RANDOM
13272   (umask 077 && mkdir "$tmp")
13273 @} || exit $?
13274 @end example
13277 @item @command{mv}
13278 @c ---------------
13279 @prindex @command{mv}
13280 @cindex Moving open files
13281 The only portable options are @option{-f} and @option{-i}.
13283 Moving individual files between file systems is portable (it was in Unix
13284 version 6),
13285 but it is not always atomic: when doing @samp{mv new existing}, there's
13286 a critical section where neither the old nor the new version of
13287 @file{existing} actually exists.
13289 On some systems moving files from @file{/tmp} can sometimes cause
13290 undesirable (but perfectly valid) warnings, even if you created these
13291 files.  This is because @file{/tmp} belongs to a group that ordinary
13292 users are not members of, and files created in @file{/tmp} inherit
13293 @file{/tmp}'s group.  When the file is copied, @command{mv} issues
13294 a diagnostic without failing:
13296 @smallexample
13297 $ @kbd{touch /tmp/foo}
13298 $ @kbd{mv /tmp/foo .}
13299 @error{}mv: ./foo: set owner/group (was: 100/0): Operation not permitted
13300 $ @kbd{echo $?}
13302 $ @kbd{ls foo}
13304 @end smallexample
13306 @noindent
13307 This annoying behavior conforms to Posix, unfortunately.
13309 Moving directories across mount points is not portable, use @command{cp}
13310 and @command{rm}.
13312 Moving/Deleting open files isn't portable.  The following can't be done
13313 on @acronym{DOS} variants:
13315 @example
13316 exec > foo
13317 mv foo bar
13318 @end example
13320 @noindent
13321 nor can
13323 @example
13324 exec > foo
13325 rm -f foo
13326 @end example
13329 @item @command{od}
13330 @c ---------------
13331 @prindex @command{od}
13333 In Mac OS X 10.3, @command{od} does not support the
13334 standard Posix options @option{-A}, @option{-j}, @option{-N}, or
13335 @option{-t}, or the @acronym{XSI} option @option{-s}.  The only
13336 supported Posix option is @option{-v}, and the only supported
13337 @acronym{XSI} options are those in @option{-bcdox}.  The BSD
13338 @command{hexdump} program can be used instead.
13340 This problem no longer exists in Mac OS X 10.4.3.
13343 @item @command{sed}
13344 @c ----------------
13345 @prindex @command{sed}
13346 Patterns should not include the separator (unless escaped), even as part
13347 of a character class.  In conformance with Posix, the Cray
13348 @command{sed} will reject @samp{s/[^/]*$//}: use @samp{s,[^/]*$,,}.
13350 Avoid empty patterns within parentheses (i.e., @samp{\(\)}).  Posix does
13351 not require support for empty patterns, and Unicos 9 @command{sed} rejects
13352 them.
13354 Unicos 9 @command{sed} loops endlessly on patterns like @samp{.*\n.*}.
13356 Sed scripts should not use branch labels longer than 8 characters and
13357 should not contain comments.  HP-UX sed has a limit of 99 commands
13358 (not counting @samp{:} commands) and
13359 48 labels, which can not be circumvented by using more than one script
13360 file.  It can execute up to 19 reads with the @samp{r} command per cycle.
13361 Solaris @command{/usr/ucb/sed} rejects usages that exceed an limit of
13362 about 6000 bytes for the internal representation of commands.
13364 Avoid redundant @samp{;}, as some @command{sed} implementations, such as
13365 Net@acronym{BSD} 1.4.2's, incorrectly try to interpret the second
13366 @samp{;} as a command:
13368 @example
13369 $ @kbd{echo a | sed 's/x/x/;;s/x/x/'}
13370 sed: 1: "s/x/x/;;s/x/x/": invalid command code ;
13371 @end example
13373 Input should not have unreasonably long lines, since some @command{sed}
13374 implementations have an input buffer limited to 4000 bytes.
13376 Portable @command{sed} regular expressions should use @samp{\} only to escape
13377 characters in the string @samp{$()*.0123456789[\^n@{@}}.  For example,
13378 alternation, @samp{\|}, is common but Posix does not require its
13379 support, so it should be avoided in portable scripts.  Solaris
13380 @command{sed} does not support alternation; e.g., @samp{sed '/a\|b/d'}
13381 deletes only lines that contain the literal string @samp{a|b}.
13382 Similarly, @samp{\+} and @samp{\?} should be avoided.
13384 Anchors (@samp{^} and @samp{$}) inside groups are not portable.
13386 Nested parenthesization in patterns (e.g., @samp{\(\(a*\)b*)\)}) is
13387 quite portable to current hosts, but was not supported by some ancient
13388 @command{sed} implementations like SVR3.
13390 Some @command{sed} implementations, e.g., Solaris,
13391 restrict the special role of the asterisk to one-character regular expressions.
13392 This may lead to unexpected behavior:
13394 @example
13395 $ @kbd{echo '1*23*4' | /usr/bin/sed 's/\(.\)*/x/g'}
13396 x2x4
13397 $ @kbd{echo '1*23*4' | /usr/xpg4/bin/sed 's/\(.\)*/x/g'}
13399 @end example
13401 The @option{-e} option is portable.
13402 Some people prefer to use it:
13404 @example
13405 sed -e '@var{command-1}' \
13406     -e '@var{command-2}'
13407 @end example
13409 @noindent
13410 as opposed to the equivalent:
13412 @example
13413 sed '
13414   @var{command-1}
13415   @var{command-2}
13417 @end example
13419 @noindent
13420 The following usage is sometimes equivalent:
13422 @example
13423 sed '@var{command-1};@var{command-2}'
13424 @end example
13426 but Posix says that this use of a semicolon has undefined effect if
13427 @var{command-1}'s verb is @samp{@{}, @samp{a}, @samp{b}, @samp{c},
13428 @samp{i}, @samp{r}, @samp{t}, @samp{w}, @samp{:}, or @samp{#}, so you
13429 should use semicolon only with simple scripts that do not use these
13430 verbs.
13432 Commands inside @{ @} brackets are further restricted.  Posix says that
13433 they cannot be preceded by addresses, @samp{!}, or @samp{;}, and that
13434 each command must be followed immediately by a newline, without any
13435 intervening blanks or semicolons.  The closing bracket must be alone on
13436 a line, other than white space preceding or following it.
13438 Contrary to yet another urban legend, you may portably use @samp{&} in
13439 the replacement part of the @code{s} command to mean ``what was
13440 matched''.  All descendants of Unix version 7 @command{sed}
13441 (at least; we
13442 don't have first hand experience with older @command{sed}s) have
13443 supported it.
13445 Posix requires that you must not have any white space between
13446 @samp{!} and the following command.  It is OK to have blanks between
13447 the address and the @samp{!}.  For instance, on Solaris:
13449 @example
13450 $ @kbd{echo "foo" | sed -n '/bar/ ! p'}
13451 @error{}Unrecognized command: /bar/ ! p
13452 $ @kbd{echo "foo" | sed -n '/bar/! p'}
13453 @error{}Unrecognized command: /bar/! p
13454 $ @kbd{echo "foo" | sed -n '/bar/ !p'}
13456 @end example
13458 Posix also says that you should not combine @samp{!} and @samp{;}.  If
13459 you use @samp{!}, it is best to put it on a command that is delimited by
13460 newlines rather than @samp{;}.
13462 Also note that Posix requires that the @samp{b}, @samp{t}, @samp{r}, and
13463 @samp{w} commands be followed by exactly one space before their argument.
13464 On the other hand, no white space is allowed between @samp{:} and the
13465 subsequent label name.
13467 @item @command{sed} (@samp{t})
13468 @c ---------------------------
13469 @prindex @command{sed} (@samp{t})
13470 Some old systems have @command{sed} that ``forget'' to reset their
13471 @samp{t} flag when starting a new cycle.  For instance on @acronym{MIPS
13472 RISC/OS}, and on @sc{irix} 5.3, if you run the following @command{sed}
13473 script (the line numbers are not actual part of the texts):
13475 @example
13476 s/keep me/kept/g  # a
13477 t end             # b
13478 s/.*/deleted/g    # c
13479 :end              # d
13480 @end example
13482 @noindent
13485 @example
13486 delete me         # 1
13487 delete me         # 2
13488 keep me           # 3
13489 delete me         # 4
13490 @end example
13492 @noindent
13493 you get
13495 @example
13496 deleted
13497 delete me
13498 kept
13499 deleted
13500 @end example
13502 @noindent
13503 instead of
13505 @example
13506 deleted
13507 deleted
13508 kept
13509 deleted
13510 @end example
13512 Why?  When processing line 1, (c) matches, therefore sets the @samp{t}
13513 flag, and the output is produced.  When processing
13514 line 2, the @samp{t} flag is still set (this is the bug).  Command (a)
13515 fails to match, but @command{sed} is not supposed to clear the @samp{t}
13516 flag when a substitution fails.  Command (b) sees that the flag is set,
13517 therefore it clears it, and jumps to (d), hence you get @samp{delete me}
13518 instead of @samp{deleted}.  When processing line (3), @samp{t} is clear,
13519 (a) matches, so the flag is set, hence (b) clears the flags and jumps.
13520 Finally, since the flag is clear, line 4 is processed properly.
13522 There are two things one should remember about @samp{t} in @command{sed}.
13523 Firstly, always remember that @samp{t} jumps if @emph{some} substitution
13524 succeeded, not only the immediately preceding substitution.  Therefore,
13525 always use a fake @samp{t clear} followed by a @samp{:clear} on the next
13526 line, to reset the @samp{t} flag where needed.
13528 Secondly, you cannot rely on @command{sed} to clear the flag at each new
13529 cycle.
13531 One portable implementation of the script above is:
13533 @example
13534 t clear
13535 :clear
13536 s/keep me/kept/g
13537 t end
13538 s/.*/deleted/g
13539 :end
13540 @end example
13542 @item @command{touch}
13543 @c ------------------
13544 @prindex @command{touch}
13545 @cindex timestamp resolution
13546 If you specify the desired timestamp (e.g., with the @option{-r}
13547 option), @command{touch} typically uses the @code{utime} or
13548 @code{utimes} system call, which can result in the same kind of
13549 timestamp truncation problems that @samp{cp -p} has.
13551 On ancient @acronym{BSD} systems, @command{touch} or any command that
13552 results in an empty file does not update the timestamps, so use a
13553 command like @command{echo} as a workaround.
13554 Also,
13555 @acronym{GNU} @command{touch} 3.16r (and presumably all before that)
13556 fails to work on SunOS 4.1.3 when the empty file is on an
13557 @acronym{NFS}-mounted 4.2 volume.
13558 However, these problems are no longer of practical concern.
13560 @end table
13563 @node Limitations of Make
13564 @section Limitations of Make
13565 @prindex @command{make}
13566 @cindex Limitations of @command{make}
13568 @command{make} itself suffers a great number of limitations, only a few
13569 of which are listed here.  First of all, remember that since commands
13570 are executed by the shell, all its weaknesses are inherited@enddots{}
13572 @table @asis
13574 @item @code{$<}
13575 Posix says that the @samp{$<} construct in makefiles can be
13576 used only in inference rules and in the @samp{.DEFAULT} rule; its
13577 meaning in ordinary rules is unspecified.  Solaris @command{make}
13578 for instance will replace it with the empty string.  Open@acronym{BSD} (3.0 and
13579 later) @command{make} will diagnose these uses and error out.
13581 @item Command execution
13582 Since 1992 Posix has required that @command{make} must invoke
13583 each command with the equivalent of a @samp{sh -c} subshell.  However,
13584 many @command{make} implementations, including @acronym{BSD} make through 2004,
13585 use @samp{sh -e -c} instead, and the @option{-e} option causes the
13586 subshell to exit immediately if a subsidiary simple-command fails.  For
13587 example, the command @samp{touch T; rm -f U} will always attempt to
13588 remove @file{U} with Posix make, but incompatible
13589 @command{make} implementations skip the @command{rm} if the
13590 @command{touch} fails.  One way to work around this is to reword the
13591 affected simple-commands so that they always succeed, e.g., @samp{touch
13592 T || :; rm -f U}.
13593 However, even this approach can run into common bugs in BSD
13594 implementations of the @option{-e} option of @command{sh} and
13595 @command{set} (@pxref{Limitations of Builtins}), so if you are worried
13596 about porting to buggy BSD shells it may be simpler to migrate
13597 complicated @command{make} actions into separate scripts.
13599 @item Leading underscore in macro names
13600 Some @command{make}s don't support leading underscores in macro names,
13601 such as on NEWS-OS 4.2R.
13603 @example
13604 $ @kbd{cat Makefile}
13605 _am_include = #
13606 _am_quote =
13607 all:; @@echo this is test
13608 $ @kbd{make}
13609 Make: Must be a separator on rules line 2.  Stop.
13610 $ @kbd{cat Makefile2}
13611 am_include = #
13612 am_quote =
13613 all:; @@echo this is test
13614 $ @kbd{make -f Makefile2}
13615 this is test
13616 @end example
13618 @item Trailing backslash in macro
13619 @c  This has been seen on ia64 hpux 11.20, and on one hppa hpux 10.20,
13620 @c  but another hppa hpux 10.20 didn't have it.  Bob Proulx
13621 @c  <bob@proulx.com> thinks it was in hpux 8.0 too.
13622 On some versions of HP-UX, @command{make} will read multiple newlines
13623 following a backslash, continuing to the next non-empty line.  For
13624 example,
13626 @example
13627 FOO = one \
13629 BAR = two
13631 test:
13632         : FOO is "$(FOO)"
13633         : BAR is "$(BAR)"
13634 @end example
13636 @noindent
13637 shows @code{FOO} equal to @code{one BAR = two}.  Other @command{make}s
13638 sensibly let a backslash continue only to the immediately following
13639 line.
13641 @item Escaped newline in comments
13643 According to Posix, @file{Makefile} comments start with @code{#}
13644 and continue until an unescaped newline is reached.
13646 @example
13647 $ @kbd{cat Makefile}
13648 # A = foo \
13649       bar \
13650       baz
13652 all:
13653         @@echo ok
13654 $ @kbd{make}   # GNU make
13656 @end example
13658 @noindent
13659 However in Real World this is not always the case.  Some implementations
13660 discards anything from @code{#} up to the end of line, ignoring any
13661 trailing backslash.
13663 @example
13664 $ @kbd{pmake}  # BSD make
13665 "Makefile", line 3: Need an operator
13666 Fatal errors encountered -- cannot continue
13667 @end example
13669 @noindent
13670 Therefore, if you want to comment out a multi-line definition, prefix each
13671 line with @code{#}, not only the first.
13673 @example
13674 # A = foo \
13675 #     bar \
13676 #     baz
13677 @end example
13679 @item Long lines.
13681 OSF/1 4.0d's @command{make} cannot process @file{Makefile}s with lines
13682 longer than 38912 bytes.  It exits with a @code{Line too long}
13683 diagnostic.  A later version, Tru64 5.1's @command{make} has been
13684 reported to crash with lines around 20 kB.
13686 @item @code{make macro=value} and sub-@command{make}s.
13688 A command-line variable definition such as @code{foo=bar} overrides any
13689 definition of @code{foo} in the @file{Makefile}.  Some @command{make}
13690 implementations (such as @acronym{GNU} @command{make}) will propagate this
13691 override to sub-invocations of @command{make}.  Some other implementation
13692 will not pass the substitution along to sub-@command{make}s.
13694 @example
13695 $ @kbd{cat Makefile}
13696 foo = foo
13697 one:
13698         @@echo $(foo)
13699         $(MAKE) two
13700 two:
13701         @@echo $(foo)
13702 $ @kbd{make foo=bar}            # GNU make 3.79.1
13704 make two
13705 make[1]: Entering directory `/home/adl'
13707 make[1]: Leaving directory `/home/adl'
13708 $ @kbd{pmake foo=bar}           # BSD make
13710 pmake two
13712 @end example
13714 You have a few possibilities if you do want the @code{foo=bar} override
13715 to propagate to sub-@command{make}s.  One is to use the @option{-e}
13716 option, which causes all environment variables to have precedence over
13717 the @file{Makefile} macro definitions, and declare foo as an environment
13718 variable:
13720 @example
13721 $ @kbd{env foo=bar make -e}
13722 @end example
13724 The @option{-e} option is propagated to sub-@command{make}s automatically,
13725 and since the environment is inherited between @command{make}
13726 invocations, the @code{foo} macro will be overridden in
13727 sub-@code{make}s as expected.
13729 This syntax (@code{foo=bar make -e}) is portable only when used
13730 outside of a @file{Makefile}, for instance from a script or from the
13731 command line.  When run inside a @command{make} rule, @acronym{GNU}
13732 @command{make} 3.80 and prior versions forget to propagate the
13733 @option{-e} option to sub-@command{make}s.
13735 Moreover, using @option{-e} could have unexpected side-effects if your
13736 environment contains some other macros usually defined by the
13737 Makefile.  (See also the note about @code{make -e} and @code{SHELL}
13738 below.)
13740 Another way to propagate overrides to sub-@command{make}s is to do it
13741 manually, from your @file{Makefile}:
13743 @example
13744 foo = foo
13745 one:
13746         @@echo $(foo)
13747         $(MAKE) foo=$(foo) two
13748 two:
13749         @@echo $(foo)
13750 @end example
13752 You need to foresee all macros that a user might want to override if
13753 you do that.
13755 @item The @code{SHELL} macro
13756 @cindex @code{SHELL} and @command{make}
13757 @cindex @command{make} and @code{SHELL}
13759 Posix-compliant @command{make}s internally use the @code{$(SHELL)}
13760 macro to spawn shell processes and execute @file{Makefile} rules.  This
13761 is a builtin macro supplied by @command{make}, but it can be modified
13762 from the @file{Makefile} or a command-line argument.
13764 Not all @command{make}s will define this @code{SHELL} macro.  OSF/Tru64
13765 @command{make} is an example; this implementation will always use
13766 @code{/bin/sh}.  So it's a good idea to always define @code{SHELL} in
13767 your @file{Makefile}s.  If you use Autoconf, do
13769 @example
13770 SHELL = @@SHELL@@
13771 @end example
13773 Do not force @code{SHELL = /bin/sh} because that is not correct
13774 everywhere.  For instance @acronym{DJGPP} lacks @code{/bin/sh}, and when
13775 its @acronym{GNU} @code{make} port sees such a setting it enters a special
13776 emulation mode where features like pipes and redirections are emulated
13777 on top of DOS's @command{command.com}.  Unfortunately this emulation is
13778 incomplete; for instance it does not handle command substitutions.
13779 On @acronym{DJGPP} @code{SHELL} should point to Bash.
13781 Posix-compliant @command{make}s should never acquire the value of
13782 $(SHELL) from the environment, even when @code{make -e} is used
13783 (otherwise, think about what would happen to your rules if
13784 @code{SHELL=/bin/tcsh}).
13786 However not all @command{make} implementations will make this exception.
13787 For instance it's not surprising that OSF/Tru64 @command{make} doesn't
13788 protect @code{SHELL}, since it doesn't use it.
13790 @example
13791 $ @kbd{cat Makefile}
13792 SHELL = /bin/sh
13793 FOO = foo
13794 all:
13795         @@echo $(SHELL)
13796         @@echo $(FOO)
13797 $ @kbd{env SHELL=/bin/tcsh FOO=bar make -e}   # OSF1 V4.0 Make
13798 /bin/tcsh
13800 $ @kbd{env SHELL=/bin/tcsh FOO=bar gmake -e}  # GNU make
13801 /bin/sh
13803 @end example
13805 @item Comments in rules
13806 @cindex Comments in @file{Makefile} rules
13807 @cindex @file{Makefile} rules and comments
13809 Never put comments in a rule.
13811 Some @command{make} treat anything starting with a tab as a command for
13812 the current rule, even if the tab is immediately followed by a @code{#}.
13813 The @command{make} from Tru64 Unix V5.1 is one of them.  The following
13814 @file{Makefile} will run @code{# foo} through the shell.
13816 @example
13817 all:
13818         # foo
13819 @end example
13821 @item The @file{obj/} subdirectory.
13822 @cindex @file{obj/}, subdirectory
13823 @cindex @acronym{BSD} @command{make} and @file{obj/}
13825 Never name one of your subdirectories @file{obj/} if you don't like
13826 surprises.
13828 If an @file{obj/} directory exists, @acronym{BSD} @command{make} will enter it
13829 before reading @file{Makefile}.  Hence the @file{Makefile} in the
13830 current directory will not be read.
13832 @example
13833 $ @kbd{cat Makefile}
13834 all:
13835         echo Hello
13836 $ @kbd{cat obj/Makefile}
13837 all:
13838         echo World
13839 $ @kbd{make}      # GNU make
13840 echo Hello
13841 Hello
13842 $ @kbd{pmake}     # BSD make
13843 echo World
13844 World
13845 @end example
13847 @item @code{make -k}
13848 @cindex @code{make -k}
13850 Do not rely on the exit status of @code{make -k}.  Some implementations
13851 reflect whether they encountered an error in their exit status; other
13852 implementations always succeed.
13854 @example
13855 $ @kbd{cat Makefile}
13856 all:
13857         false
13858 $ @kbd{make -k; echo exit status: $?}    # GNU make
13859 false
13860 make: *** [all] Error 1
13861 exit status: 2
13862 $ @kbd{pmake -k; echo exit status: $?}   # BSD make
13863 false
13864 *** Error code 1 (continuing)
13865 exit status: 0
13866 @end example
13868 @item @code{VPATH}
13869 @cindex @code{VPATH}
13871 There is no @code{VPATH} support specified in Posix.  Many
13872 @command{make}s have a form of @code{VPATH} support, but its
13873 implementation is not consistent amongst @command{make}s.
13875 Maybe the best suggestion to give to people who need the @code{VPATH}
13876 feature is to choose a @command{make} implementation and stick to it.
13877 Since the resulting @file{Makefile}s are not portable anyway, better
13878 choose a portable @command{make} (hint, hint).
13880 Here are a couple of known issues with some @code{VPATH}
13881 implementations.
13883 @table @asis
13885 @item @code{VPATH} and double-colon rules
13886 @cindex @code{VPATH} and double-colon rules
13887 @cindex double-colon rules and @code{VPATH}
13889 With ancient versions of Sun @command{make},
13890 any assignment to @code{VPATH} @command{make} to only execute
13891 the first set of double-colon rules.
13892 However, this problem is no longer of practical concern.
13894 @item @code{$<} not supported in explicit rules
13895 @cindex explicit rules, @code{$<}, and @code{VPATH}
13896 @cindex @code{$<}, explicit rules, and @code{VPATH}
13897 @cindex @code{VPATH}, explicit rules, and @code{$<}
13899 As said elsewhere, using @code{$<} in explicit rules is not portable.
13900 The prerequisite file must be named explicitly in the rule.  If you want
13901 to find the prerequisite via a @code{VPATH} search, you have to code the
13902 whole thing manually.  For instance, using the following pattern:
13904 @example
13905 VPATH = ../pkg/src
13906 f.c: if.c
13907         cp `test -f if.c || echo $(VPATH)/`if.c f.c
13908 @end example
13910 @item Automatic rule rewriting
13911 @cindex @code{VPATH} and automatic rule rewriting
13912 @cindex automatic rule rewriting and @code{VPATH}
13914 Some @command{make} implementations, such as Solaris @command{make} and
13915 OSF1/Tru64 @command{make}, search for prerequisites in @code{VPATH} and
13916 then rewrite each occurrence as a plain word in the rule.
13917 For instance:
13919 @example
13920 # This isn't portable to GNU make.
13921 VPATH = ../pkg/src
13922 f.c: if.c
13923         cp if.c f.c
13924 @end example
13926 @noindent
13927 executes @code{cp ../pkg/src/if.c f.c} if @file{if.c} is
13928 found in @file{../pkg/src}.
13930 However, portable makefiles cannot rely on this behavior, and should
13931 search @code{VPATH} manually.  It is tempting to write this:
13933 @smallexample
13934 # This isn't portable to Solaris make.
13935 VPATH = ../pkg/src
13936 f.c: if.c
13937         cp `test -f if.c || echo $(VPATH)/`if.c f.c
13938 @end smallexample
13940 @noindent
13941 However the ``prerequisite rewriting'' still applies here.  So if
13942 @file{if.c} is in @file{../pkg/src}, Solaris @command{make} and OSF1/Tru64
13943 @command{make} will execute
13945 @smallexample
13946 cp `test -f ../pkg/src/if.c || echo ../pkg/src/`if.c f.c
13947 @end smallexample
13949 @noindent
13950 which reduces to
13952 @example
13953 cp if.c f.c
13954 @end example
13956 @noindent
13957 and thus fails.  Oops.
13959 A simple workaround, and good practice anyway, is to use @samp{$?} and
13960 @samp{$@@} when possible:
13962 @smallexample
13963 VPATH = ../pkg/src
13964 f.c: if.c
13965         cp $? $@@
13966 @end smallexample
13968 @noindent
13969 but this does not generalize well to commands with multiple
13970 prerequisites.  A more general workaround is to rewrite the rule so that
13971 the prerequisite @file{if.c} never appears as a plain word.  For
13972 example, these three rules would be safe, assuming @file{if.c} is in
13973 @file{../pkg/src} and the other files are in the working directory:
13975 @smallexample
13976 VPATH = ../pkg/src
13977 f.c: if.c f1.c
13978         cat `test -f ./if.c || echo $(VPATH)/`if.c f1.c >$@@
13979 g.c: if.c g1.c
13980         cat `test -f 'if.c' || echo $(VPATH)/`if.c g1.c >$@@
13981 h.c: if.c h1.c
13982         cat `test -f "if.c" || echo $(VPATH)/`if.c h1.c >$@@
13983 @end smallexample
13985 Things get worse when your prerequisites are in a macro.
13987 @example
13988 VPATH = ../pkg/src
13989 HEADERS = f.h g.h h.h
13990 install-HEADERS: $(HEADERS)
13991         for i in $(HEADERS); do \
13992           $(INSTALL) -m 644 \
13993             `test -f $$i || echo $(VPATH)/`$$i \
13994             $(DESTDIR)$(includedir)/$$i; \
13995         done
13996 @end example
13998 The above @code{install-HEADERS} rule is not Solaris-proof because @code{for
13999 i in $(HEADERS);} will be expanded as @code{for i in f.h g.h h.h;}
14000 where @code{f.h} and @code{g.h} are plain words and are hence
14001 subject to @code{VPATH} adjustments.
14003 If the three files are in @file{../pkg/src}, the rule is run as:
14005 @example
14006 for i in ../pkg/src/f.h ../pkg/src/g.h h.h; do \
14007   install -m 644 \
14008      `test -f $i || echo ../pkg/src/`$i \
14009      /usr/local/include/$i; \
14010 done
14011 @end example
14013 where the two first @command{install} calls will fail.  For instance,
14014 consider the @code{f.h} installation:
14016 @example
14017 install -m 644 \
14018   `test -f ../pkg/src/f.h || \
14019     echo ../pkg/src/ \
14020   `../pkg/src/f.h \
14021   /usr/local/include/../pkg/src/f.h;
14022 @end example
14023 @noindent
14024 It reduces to:
14026 @example
14027 install -m 644 \
14028   ../pkg/src/f.h \
14029   /usr/local/include/../pkg/src/f.h;
14030 @end example
14032 Note that the manual @code{VPATH} search did not cause any problems here;
14033 however this command installs @file{f.h} in an incorrect directory.
14035 Trying to quote @code{$(HEADERS)} in some way, as we did for
14036 @code{foo.c} a few @file{Makefile}s ago, does not help:
14038 @example
14039 install-HEADERS: $(HEADERS)
14040         headers='$(HEADERS)'; \
14041         for i in $$headers; do \
14042           $(INSTALL) -m 644 \
14043             `test -f $$i || echo $(VPATH)/`$$i \
14044             $(DESTDIR)$(includedir)/$$i; \
14045         done
14046 @end example
14048 Now, @code{headers='$(HEADERS)'} macroexpands to:
14050 @example
14051 headers='f.h g.h h.h'
14052 @end example
14054 @noindent
14055 but @code{g.h} is still a plain word.  (As an aside, the idiom
14056 @code{headers='$(HEADERS)'; for i in $$headers;} is a good
14057 idea if @code{$(HEADERS)} can be empty, because some shells diagnose a
14058 syntax error on @code{for i in;}.)
14060 One workaround is to strip this unwanted @file{../pkg/src/} prefix manually:
14061 @example
14062 VPATH = ../pkg/src
14063 HEADERS = f.h g.h h.h
14064 install-HEADERS: $(HEADERS)
14065         headers='$(HEADERS)'; \
14066         for i in $$headers; do \
14067           i=`expr "$$i" : '$(VPATH)/\(.*\)'`;
14068           $(INSTALL) -m 644 \
14069             `test -f $$i || echo $(VPATH)/`$$i \
14070             $(DESTDIR)$(includedir)/$$i; \
14071         done
14072 @end example
14074 Automake does something similar.  However the above hack works only if
14075 the files listed in @code{HEADERS} are in the current directory or a
14076 subdirectory; they should not be in an enclosing directory.  If we had
14077 @code{HEADERS = ../f.h}, the above fragment would fail in a VPATH
14078 build with OSF1/Tru64 @command{make}.  The reason is that not only does
14079 OSF1/Tru64 @command{make} rewrite dependencies, but it also simplifies
14080 them.  Hence @code{../f.h} will become @code{../pkg/f.h} instead of
14081 @code{../pkg/src/../f.h}.  This obviously defeats any attempt to strip
14082 a leading @file{../pkg/src/} component.
14084 The following example makes the behavior of OSF1/Tru64 @command{make}
14085 more apparent.
14086 @example
14087 $ @kbd{cat Makefile}
14088 VPATH = sub
14089 all: ../foo
14090         echo ../foo
14091 $ @kbd{ls}
14092 Makefile foo
14093 $ @kbd{make}
14094 echo foo
14096 @end example
14097 @noindent
14098 Dependency @file{../foo} was found in @file{sub/../foo}, but OSF1/Tru64
14099 @command{make} simplified it as @file{foo}.  (Note that the @file{sub/}
14100 directory does not even exist, this just means that the simplification
14101 occurred before the file was checked for.)
14103 For the record here is how SunOS 4 @command{make} behaves on this
14104 very same example.
14105 @smallexample
14106 $ @kbd{make}
14107 make: Fatal error: Don't know how to make target `../foo'
14108 $ @kbd{mkdir sub}
14109 $ @kbd{make}
14110 echo sub/../foo
14111 sub/../foo
14112 @end smallexample
14115 @item OSF/Tru64 @command{make} creates prerequisite directories magically
14116 @cindex @code{VPATH} and prerequisite directories
14117 @cindex prerequisite directories and @code{VPATH}
14119 When a prerequisite is a sub-directory of @code{VPATH}, Tru64
14120 @command{make} will create it in the current directory.
14122 @example
14123 $ @kbd{mkdir -p foo/bar build}
14124 $ @kbd{cd build}
14125 $ @kbd{cat >Makefile <<END
14126 VPATH = ..
14127 all: foo/bar
14128 END}
14129 $ @kbd{make}
14130 mkdir foo
14131 mkdir foo/bar
14132 @end example
14134 This can yield unexpected results if a rule uses a manual @code{VPATH}
14135 search as presented before.
14137 @example
14138 VPATH = ..
14139 all : foo/bar
14140         command `test -d foo/bar || echo ../`foo/bar
14141 @end example
14143 The above @command{command} will be run on the empty @file{foo/bar}
14144 directory that was created in the current directory.
14146 @item target lookup
14147 @cindex @code{VPATH}, resolving target pathnames
14149 @acronym{GNU} @command{make} uses a rather complex algorithm to decide when it
14150 should use files found via a @code{VPATH} search.  @xref{Search
14151 Algorithm, , How Directory Searches are Performed, make, The @acronym{GNU} Make
14152 Manual}.
14154 If a target needs to be rebuilt, @acronym{GNU} @command{make} discards the
14155 file name found during the @code{VPATH} search for this target, and
14156 builds the file locally using the file name given in the @file{Makefile}.
14157 If a target does not need to be rebuilt, @acronym{GNU} @command{make} uses the
14158 file name found during the @code{VPATH} search.
14160 Other @command{make} implementations, like Net@acronym{BSD} @command{make}, are
14161 easier to describe: the file name found during the @code{VPATH} search
14162 will be used whether the target needs to be rebuilt or not.  Therefore
14163 new files are created locally, but existing files are updated at their
14164 @code{VPATH} location.
14166 Open@acronym{BSD} and Free@acronym{BSD} @command{make}s, however, will
14167 never perform a
14168 @code{VPATH} search for a dependency which has an explicit rule.
14169 This is extremely annoying.
14171 When attempting a @code{VPATH} build for an autoconfiscated package
14172 (e.g., @code{mkdir build && cd build && ../configure}), this means the
14173 @acronym{GNU}
14174 @command{make} will build everything locally in the @file{build}
14175 directory, while @acronym{BSD} @command{make} will build new files locally and
14176 update existing files in the source directory.
14178 @example
14179 $ @kbd{cat Makefile}
14180 VPATH = ..
14181 all: foo.x bar.x
14182 foo.x bar.x: newer.x
14183         @@echo Building $@@
14184 $ @kbd{touch ../bar.x}
14185 $ @kbd{touch ../newer.x}
14186 $ @kbd{make}        # GNU make
14187 Building foo.x
14188 Building bar.x
14189 $ @kbd{pmake}       # NetBSD make
14190 Building foo.x
14191 Building ../bar.x
14192 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14193 Building foo.x
14194 Building bar.x
14195 $ @kbd{tmake}       # Tru64 make
14196 Building foo.x
14197 Building bar.x
14198 $ @kbd{touch ../bar.x}
14199 $ @kbd{make}        # GNU make
14200 Building foo.x
14201 $ @kbd{pmake}       # NetBSD make
14202 Building foo.x
14203 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14204 Building foo.x
14205 Building bar.x
14206 $ @kbd{tmake}       # Tru64 make
14207 Building foo.x
14208 Building bar.x
14209 @end example
14211 Note how Net@acronym{BSD} @command{make} updates @file{../bar.x} in its
14212 VPATH location, and how Free@acronym{BSD}, Open@acronym{BSD}, and Tru64
14213 @command{make} always
14214 update @file{bar.x}, even when @file{../bar.x} is up to date.
14216 Another point worth mentioning is that once @acronym{GNU} @command{make} has
14217 decided to ignore a @code{VPATH} file name (e.g., it ignored
14218 @file{../bar.x} in the above example) it will continue to ignore it when
14219 the target occurs as a prerequisite of another rule.
14221 The following example shows that @acronym{GNU} @command{make} does not look up
14222 @file{bar.x} in @code{VPATH} before performing the @code{.x.y} rule,
14223 because it ignored the @code{VPATH} result of @file{bar.x} while running
14224 the @code{bar.x: newer.x} rule.
14226 @example
14227 $ @kbd{cat Makefile}
14228 VPATH = ..
14229 all: bar.y
14230 bar.x: newer.x
14231         @@echo Building $@@
14232 .SUFFIXES: .x .y
14233 .x.y:
14234         cp $< $@@
14235 $ @kbd{touch ../bar.x}
14236 $ @kbd{touch ../newer.x}
14237 $ @kbd{make}        # GNU make
14238 Building bar.x
14239 cp bar.x bar.y
14240 cp: cannot stat `bar.x': No such file or directory
14241 make: *** [bar.y] Error 1
14242 $ @kbd{pmake}       # NetBSD make
14243 Building ../bar.x
14244 cp ../bar.x bar.y
14245 $ @kbd{rm bar.y}
14246 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14247 echo Building bar.x
14248 cp bar.x bar.y
14249 cp: cannot stat `bar.x': No such file or directory
14250 *** Error code 1
14251 $ @kbd{tmake}       # Tru64 make
14252 Building bar.x
14253 cp: bar.x: No such file or directory
14254 *** Exit 1
14255 @end example
14257 Note that if you drop away the command from the @code{bar.x: newer.x}
14258 rule, @acronym{GNU} @command{make} will magically start to work: it
14259 knows that @code{bar.x} hasn't been updated, therefore it doesn't
14260 discard the result from @code{VPATH} (@file{../bar.x}) in succeeding
14261 uses.  Tru64 will also work, but Free@acronym{BSD} and Open@acronym{BSD}
14262 still don't.
14264 @example
14265 $ @kbd{cat Makefile}
14266 VPATH = ..
14267 all: bar.y
14268 bar.x: newer.x
14269 .SUFFIXES: .x .y
14270 .x.y:
14271         cp $< $@@
14272 $ @kbd{touch ../bar.x}
14273 $ @kbd{touch ../newer.x}
14274 $ @kbd{make}        # GNU make
14275 cp ../bar.x bar.y
14276 $ @kbd{rm bar.y}
14277 $ @kbd{pmake}       # NetBSD make
14278 cp ../bar.x bar.y
14279 $ @kbd{rm bar.y}
14280 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14281 cp bar.x bar.y
14282 cp: cannot stat `bar.x': No such file or directory
14283 *** Error code 1
14284 $ @kbd{tmake}       # Tru64 make
14285 cp ../bar.x bar.y
14286 @end example
14288 It seems the sole solution that would please every @command{make}
14289 implementation is to never rely on @code{VPATH} searches for targets.
14290 In other words, @code{VPATH} should be reserved to unbuilt sources.
14292 @end table
14293 @c end item about VPATH
14295 @item Single Suffix Rules and Separated Dependencies
14296 @cindex Single Suffix Inference Rule
14297 @cindex Rule, Single Suffix Inference
14298 A @dfn{Single Suffix Rule} is basically a usual suffix (inference) rule
14299 (@samp{.from.to:}), but which @emph{destination} suffix is empty
14300 (@samp{.from:}).
14302 @cindex Separated Dependencies
14303 @dfn{Separated dependencies} simply refers to listing the prerequisite
14304 of a target, without defining a rule.  Usually one can list on the one
14305 hand side, the rules, and on the other hand side, the dependencies.
14307 Solaris @command{make} does not support separated dependencies for
14308 targets defined by single suffix rules:
14310 @example
14311 $ @kbd{cat Makefile}
14312 .SUFFIXES: .in
14313 foo: foo.in
14314 .in:
14315         cp $< $@@
14316 $ @kbd{touch foo.in}
14317 $ @kbd{make}
14318 $ @kbd{ls}
14319 Makefile  foo.in
14320 @end example
14322 @noindent
14323 while @acronym{GNU} Make does:
14325 @example
14326 $ @kbd{gmake}
14327 cp foo.in foo
14328 $ @kbd{ls}
14329 Makefile  foo       foo.in
14330 @end example
14332 Note it works without the @samp{foo: foo.in} dependency.
14334 @example
14335 $ @kbd{cat Makefile}
14336 .SUFFIXES: .in
14337 .in:
14338         cp $< $@@
14339 $ @kbd{make foo}
14340 cp foo.in foo
14341 @end example
14343 @noindent
14344 and it works with double suffix inference rules:
14346 @example
14347 $ @kbd{cat Makefile}
14348 foo.out: foo.in
14349 .SUFFIXES: .in .out
14350 .in.out:
14351         cp $< $@@
14352 $ @kbd{make}
14353 cp foo.in foo.out
14354 @end example
14356 As a result, in such a case, you have to write target rules.
14358 @item Timestamp Resolution
14359 @cindex timestamp resolution
14360 Traditionally, file timestamps had 1-second resolution, and
14361 @command{make} used those timestamps to determine whether one file was
14362 newer than the other.  However, many modern file systems have
14363 timestamps with 1-nanosecond resolution.  Some @command{make}
14364 implementations look at the entire timestamp; others ignore the
14365 fractional part, which can lead to incorrect results.  Normally this
14366 is not a problem, but in some extreme cases you may need to use tricks
14367 like @samp{sleep 1} to work around timestamp truncation bugs.
14369 Commands like @samp{cp -p} and @samp{touch -r} typically do not copy
14370 file timestamps to their full resolutions (@pxref{Limitations of Usual
14371 Tools}).  Hence you should be wary of rules like this:
14373 @example
14374 dest: src
14375         cp -p src dest
14376 @end example
14378 as @file{dest} will often appear to be older than @file{src} after the
14379 timestamp is truncated, and this can cause @command{make} to do
14380 needless rework the next time it is invoked.  To work around this
14381 problem, you can use a timestamp file, e.g.:
14383 @example
14384 dest-stamp: src
14385         cp -p src dest
14386         date >dest-stamp
14387 @end example
14389 @end table
14394 @c ======================================== Portable C and C++ Programming
14396 @node Portable C and C++
14397 @chapter Portable C and C++ Programming
14398 @cindex Portable C and C++ programming
14400 C and C++ programs often use low-level features of the underlying
14401 system, and therefore are often more difficult to make portable to other
14402 platforms.
14404 Several standards have been developed to help make your programs more
14405 portable.  If you write programs with these standards in mind, you can
14406 have greater confidence that your programs will work on a wide variety
14407 of systems.  @xref{Standards, , Language Standards Supported by GCC,
14408 gcc, Using the GNU Compiler Collection (GCC)}, for a list of C-related
14409 standards.  Many programs also assume the
14410 @uref{http://www.opengroup.org/susv3, Posix standard}.
14412 Some old code is written to be portable to K&R C, which predates any C
14413 standard.  K&R C compilers are no longer of practical interest, though,
14414 and the rest of section assumes at least C89, the first C standard.
14416 Program portability is a huge topic, and this section can only briefly
14417 introduce common pitfalls.  @xref{System Portability, , Portability
14418 between System Types, standards, @acronym{GNU} Coding Standards}, for
14419 more information.
14421 @menu
14422 * Varieties of Unportability::  How to make your programs unportable
14423 * Integer Overflow::            When integers get too large
14424 * Null Pointers::               Properties of null pointers
14425 * Buffer Overruns::             Subscript errors and the like
14426 * Floating Point Portability::  Portable floating-point arithmetic
14427 * Exiting Portably::            Exiting and the exit status
14428 @end menu
14430 @node Varieties of Unportability
14431 @section Varieties of Unportability
14432 @cindex portability
14434 Autoconf tests and ordinary programs often need to test what is allowed
14435 on a system, and therefore they may need to deliberately exceed the
14436 boundaries of what the standards allow, if only to see whether an
14437 optional feature is present.  When you write such a program, you should
14438 keep in mind the difference between constraints, unspecified behavior,
14439 and undefined behavior.
14441 In C, a @dfn{constraint} is a rule that the compiler must enforce.  An
14442 example constraint is that C programs must not declare a bit-field with
14443 negative width.  Tests can therefore reliably assume that programs with
14444 negative-width bit-fields will be rejected by a compiler that conforms
14445 to the standard.
14447 @dfn{Unspecified behavior} is valid behavior, where the standard allows
14448 multiple possibilities.  For example, the order of evaluation of
14449 function arguments is unspecified.  Some unspecified behavior is
14450 @dfn{implementation-defined}, i.e., documented by the implementation,
14451 but since Autoconf tests cannot read the documentation they cannot
14452 distinguish between implementation-defined and other unspecified
14453 behavior.  It is common for Autoconf tests to probe implementations to
14454 determine otherwise-unspecified behavior.
14456 @dfn{Undefined behavior} is invalid behavior, where the standard allows
14457 the implementation to do anything it pleases.  For example,
14458 dereferencing a null pointer leads to undefined behavior.  If possible,
14459 test programs should avoid undefined behavior, since a program with
14460 undefined behavior might succeed on a test that should fail.
14462 The above rules apply to programs that are intended to conform to the
14463 standard.  However, strictly-conforming programs are quite rare, since
14464 the standards are so limiting.  A major goal of Autoconf is to support
14465 programs that use implementation features not described by the standard,
14466 and it is fairly common for test programs to violate the above rules, if
14467 the programs work well enough in practice.
14469 @node Integer Overflow
14470 @section Integer Overflow
14471 @cindex overflow, arithmetic
14473 In C, signed integer overflow leads to undefined behavior.  However,
14474 many programs and Autoconf tests assume that signed integer overflow after
14475 addition, subtraction, or multiplication silently
14476 wraps around modulo a power of two, using two's complement arithmetic,
14477 so long as you cast the resulting value
14478 to an integer type or store it into an integer variable.  Such programs
14479 are portable to the vast majority of modern platforms.  However, signed
14480 integer division is not always harmless: for example, on CPUs of the
14481 i386 family, dividing @code{INT_MIN} by @code{-1} yields a SIGFPE signal
14482 which by default terminates the program.
14484 GCC users might consider using the
14485 @option{-ftrapv} option if they are worried about porting their code to
14486 the rare platforms where signed integer overflow does not wrap around
14487 after addition, subtraction, or multiplication.
14489 Unsigned integer overflow reliably wraps around modulo the word size.
14490 This is guaranteed by the C standard and is portable in practice.
14492 @node Null Pointers
14493 @section Properties of Null Pointers
14494 @cindex null pointers
14496 Most modern hosts reliably fail when you attempt to dereference a null
14497 pointer.
14499 On almost all modern hosts, null pointers use an all-bits-zero internal
14500 representation, so you can reliably use @code{memset} with 0 to set all
14501 the pointers in an array to null values.
14503 If @code{p} is a null pointer to an object type, the C expression
14504 @code{p + 0} always evaluates to @code{p} on modern hosts, even though
14505 the standard says that it has undefined behavior.
14507 @node Buffer Overruns
14508 @section Buffer Overruns and Subscript Errors
14509 @cindex buffer overruns
14511 Buffer overruns and subscript errors are the most common dangerous
14512 errors in C programs.  They result in undefined behavior because storing
14513 outside an array typically modifies storage that is used by some other
14514 object, and most modern systems lack runtime checks to catch these
14515 errors.  Programs should not rely on buffer overruns being caught.
14517 There is one exception to the usual rule that a portable program cannot
14518 address outside an array.  In C, it is valid to compute the address just
14519 past an object, e.g., @code{&a[N]} where @code{a} has @code{N} elements,
14520 so long as you do not dereference the resulting pointer.  But it is not
14521 valid to compute the address just before an object, e.g., @code{&a[-1]};
14522 nor is it valid to compute two past the end, e.g., @code{&a[N+1]}.  On
14523 most platforms @code{&a[-1] < &a[0] && &a[N] < &a[N+1]}, but this is not
14524 reliable in general, and it is usually easy enough to avoid the
14525 potential portability problem, e.g., by allocating an extra unused array
14526 element at the start or end.
14528 @uref{http://valgrind.org/, Valgrind} can catch many overruns.  GCC
14529 users might also consider using the @option{-fmudflap} option to catch
14530 overruns.
14532 Buffer overruns are usually caused by off-by-one errors, but there are
14533 more subtle ways to get them.
14535 Using @code{int} values to index into an array or compute array sizes
14536 will cause problems on typical 64-bit hosts where an array index might
14537 be @math{2^31} or larger.
14539 If you add or multiply two numbers to calculate an array size, e.g.,
14540 @code{malloc (x * sizeof y + z)}, havoc will ensue if the addition or
14541 multiplication overflows.
14543 Many implementations of the @code{alloca} function silently misbehave
14544 and can generate buffer overflows if given sizes that are too large.
14545 The size limits are implementation dependent, but are at least 4000
14546 bytes on all platforms that we know about.
14548 The standard functions @code{asctime}, @code{asctime_r}, @code{ctime},
14549 @code{ctime_r}, and @code{gets} are prone to buffer overflows, and
14550 portable code should not use them unless the inputs are known to be
14551 within certain limits.  The time-related functions can overflow their
14552 buffers if given time stamps out of range (e.g., a year less than -999
14553 or greater than 9999).  Time-related buffer overflows cannot happen with
14554 recent-enough versions of the GNU C library, but are possible with other
14555 implementations.  The @code{gets} function is the worst, since it almost
14556 invariably overflows its buffer when presented with an input line larger
14557 than the buffer.
14559 @node Floating Point Portability
14560 @section Floating Point Portability
14561 @cindex floating point
14563 Almost all modern systems use IEEE-754 floating point, and it is safe to
14564 assume IEEE-754 in most portable code these days.  For more information,
14565 please see David Goldberg's classic paper
14566 @uref{http://www.validlab.com/goldberg/paper.pdf, What Every Computer
14567 Scientist Should Know About Floating-Point Arithmetic}.
14569 @node Exiting Portably
14570 @section Exiting Portably
14571 @cindex exiting portably
14573 A C or C++ program can exit with status @var{N} by returning
14574 @var{N} from the @code{main} function.  Portable programs are supposed
14575 to exit either with status 0 or @code{EXIT_SUCCESS} to succeed, or with
14576 status @code{EXIT_FAILURE} to fail, but in practice it is portable to
14577 fail by exiting with status 1, and test programs that assume Posix can
14578 fail by exiting with status values from 1 through 255.  Programs on
14579 SunOS 2.0 (1985) through 3.5.2 (1988) incorrectly exited with zero
14580 status when @code{main} returned nonzero, but ancient systems like these
14581 are no longer of practical concern.
14583 A program can also exit with status @var{N} by passing @var{N} to the
14584 @code{exit} function, and a program can fail by calling the @code{abort}
14585 function.  If a program is specialized to just some platforms, it can fail
14586 by calling functions specific to those platforms, e.g., @code{_exit}
14587 (Posix) and @code{_Exit} (C99).  However, like other functions, an exit
14588 function should be declared, typically by including a header.  For
14589 example, if a C program calls @code{exit}, it should include @file{stdlib.h}
14590 either directly or via the default includes (@pxref{Default Includes}).
14592 A program can fail due to undefined behavior such as dereferencing a null
14593 pointer, but this is not recommended as undefined behavior allows an
14594 implementation to do whatever it pleases and this includes exiting
14595 successfully.
14598 @c ================================================== Manual Configuration
14600 @node Manual Configuration
14601 @chapter Manual Configuration
14603 A few kinds of features can't be guessed automatically by running test
14604 programs.  For example, the details of the object-file format, or
14605 special options that need to be passed to the compiler or linker.  You
14606 can check for such features using ad-hoc means, such as having
14607 @command{configure} check the output of the @code{uname} program, or
14608 looking for libraries that are unique to particular systems.  However,
14609 Autoconf provides a uniform method for handling unguessable features.
14611 @menu
14612 * Specifying Names::            Specifying the system type
14613 * Canonicalizing::              Getting the canonical system type
14614 * Using System Type::           What to do with the system type
14615 @end menu
14617 @node Specifying Names
14618 @section Specifying the System Type
14619 @cindex System type
14621 Like other @acronym{GNU} @command{configure} scripts, Autoconf-generated
14622 @command{configure} scripts can make decisions based on a canonical name
14623 for the system type, which has the form:
14624 @samp{@var{cpu}-@var{vendor}-@var{os}}, where @var{os} can be
14625 @samp{@var{system}} or @samp{@var{kernel}-@var{system}}
14627 @command{configure} can usually guess the canonical name for the type of
14628 system it's running on.  To do so it runs a script called
14629 @command{config.guess}, which infers the name using the @code{uname}
14630 command or symbols predefined by the C preprocessor.
14632 Alternately, the user can specify the system type with command line
14633 arguments to @command{configure}.  Doing so is necessary when
14634 cross-compiling.  In the most complex case of cross-compiling, three
14635 system types are involved.  The options to specify them are:
14637 @table @option
14638 @item --build=@var{build-type}
14639 the type of system on which the package is being configured and
14640 compiled.  It defaults to the result of running @command{config.guess}.
14642 @item --host=@var{host-type}
14643 the type of system on which the package will run.  By default it is the
14644 same as the build machine.  Specifying it enables the cross-compilation
14645 mode.
14647 @item --target=@var{target-type}
14648 the type of system for which any compiler tools in the package will
14649 produce code (rarely needed).  By default, it is the same as host.
14650 @end table
14652 If you mean to override the result of @command{config.guess}, use
14653 @option{--build}, not @option{--host}, since the latter enables
14654 cross-compilation.  For historical reasons, passing @option{--host} also
14655 changes the build type.  Therefore, whenever you specify @option{--host},
14656 be sure to specify @option{--build} too; this will be fixed in the
14657 future.  So, to enter cross-compilation mode, use a command like this
14659 @example
14660 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
14661 @end example
14663 @noindent
14664 Note that if you do not specify @option{--host}, @command{configure} will
14665 fail if it can't run the code generated by the specified compiler.  For
14666 example, configuring as follows will fail:
14668 @example
14669 ./configure CC=m68k-coff-gcc
14670 @end example
14672 In the future, when cross-compiling Autoconf will @emph{not}
14673 accept tools (compilers, linkers, assemblers) whose name is not
14674 prefixed with the host type.  The only case when this may be
14675 useful is when you really are not cross-compiling, but only
14676 building for a least-common-denominator architecture: an example
14677 is building for @code{i386-pc-linux-gnu} while running on an
14678 @code{i686-pc-linux-gnu} architecture.  In this case, some particular
14679 pairs might be similar enough to let you get away with the system
14680 compilers, but in general the compiler might make bogus assumptions
14681 on the host: if you know what you are doing, please create symbolic
14682 links from the host compiler to the build compiler.
14684 @cindex @command{config.sub}
14685 @command{configure} recognizes short aliases for many system types; for
14686 example, @samp{decstation} can be used instead of
14687 @samp{mips-dec-ultrix4.2}.  @command{configure} runs a script called
14688 @command{config.sub} to canonicalize system type aliases.
14690 This section deliberately omits the description of the obsolete
14691 interface; see @ref{Hosts and Cross-Compilation}.
14694 @node Canonicalizing
14695 @section Getting the Canonical System Type
14696 @cindex System type
14697 @cindex Canonical system type
14699 The following macros make the system type available to @command{configure}
14700 scripts.
14702 @ovindex build_alias
14703 @ovindex host_alias
14704 @ovindex target_alias
14706 The variables @samp{build_alias}, @samp{host_alias}, and
14707 @samp{target_alias} are always exactly the arguments of @option{--build},
14708 @option{--host}, and @option{--target}; in particular, they are left empty
14709 if the user did not use them, even if the corresponding
14710 @code{AC_CANONICAL} macro was run.  Any configure script may use these
14711 variables anywhere.  These are the variables that should be used when in
14712 interaction with the user.
14714 If you need to recognize some special environments based on their system
14715 type, run the following macros to get canonical system names.  These
14716 variables are not set before the macro call.
14718 If you use these macros, you must distribute @command{config.guess} and
14719 @command{config.sub} along with your source code.  @xref{Output}, for
14720 information about the @code{AC_CONFIG_AUX_DIR} macro which you can use
14721 to control in which directory @command{configure} looks for those scripts.
14724 @defmac AC_CANONICAL_BUILD
14725 @acindex{CANONICAL_BUILD}
14726 @ovindex build
14727 @ovindex build_cpu
14728 @ovindex build_vendor
14729 @ovindex build_os
14730 Compute the canonical build-system type variable, @code{build}, and its
14731 three individual parts @code{build_cpu}, @code{build_vendor}, and
14732 @code{build_os}.
14734 If @option{--build} was specified, then @code{build} is the
14735 canonicalization of @code{build_alias} by @command{config.sub},
14736 otherwise it is determined by the shell script @command{config.guess}.
14737 @end defmac
14739 @defmac AC_CANONICAL_HOST
14740 @acindex{CANONICAL_HOST}
14741 @ovindex host
14742 @ovindex host_cpu
14743 @ovindex host_vendor
14744 @ovindex host_os
14745 Compute the canonical host-system type variable, @code{host}, and its
14746 three individual parts @code{host_cpu}, @code{host_vendor}, and
14747 @code{host_os}.
14749 If @option{--host} was specified, then @code{host} is the
14750 canonicalization of @code{host_alias} by @command{config.sub},
14751 otherwise it defaults to @code{build}.
14752 @end defmac
14754 @defmac AC_CANONICAL_TARGET
14755 @acindex{CANONICAL_TARGET}
14756 @ovindex target
14757 @ovindex target_cpu
14758 @ovindex target_vendor
14759 @ovindex target_os
14760 Compute the canonical target-system type variable, @code{target}, and its
14761 three individual parts @code{target_cpu}, @code{target_vendor}, and
14762 @code{target_os}.
14764 If @option{--target} was specified, then @code{target} is the
14765 canonicalization of @code{target_alias} by @command{config.sub},
14766 otherwise it defaults to @code{host}.
14767 @end defmac
14769 Note that there can be artifacts due to the backward compatibility
14770 code.  See @xref{Hosts and Cross-Compilation}, for more.
14772 @node Using System Type
14773 @section Using the System Type
14775 In @file{configure.ac} the system type is generally used by one or more
14776 @code{case} statements to select system-specifics.  Shell wildcards can
14777 be used to match a group of system types.
14779 For example, an extra assembler code object file could be chosen, giving
14780 access to a CPU cycle counter register.  @code{$(CYCLE_OBJ)} in the
14781 following would be used in a @file{Makefile} to add the object to a
14782 program or library.
14784 @example
14785 case $host in
14786   alpha*-*-*) CYCLE_OBJ=rpcc.o ;;
14787   i?86-*-*)   CYCLE_OBJ=rdtsc.o ;;
14788   *)          CYCLE_OBJ= ;;
14789 esac
14790 AC_SUBST([CYCLE_OBJ])
14791 @end example
14793 @code{AC_CONFIG_LINKS} (@pxref{Configuration Links}) is another good way
14794 to select variant source files, for example optimized code for some
14795 CPUs.  The configured CPU type doesn't always indicate exact CPU types,
14796 so some runtime capability checks may be necessary too.
14798 @example
14799 case $host in
14800   alpha*-*-*)   AC_CONFIG_LINKS([dither.c:alpha/dither.c]) ;;
14801   powerpc*-*-*) AC_CONFIG_LINKS([dither.c:powerpc/dither.c]) ;;
14802   *-*-*)        AC_CONFIG_LINKS([dither.c:generic/dither.c]) ;;
14803 esac
14804 @end example
14806 The host system type can also be used to find cross-compilation tools
14807 with @code{AC_CHECK_TOOL} (@pxref{Generic Programs}).
14809 The above examples all show @samp{$host}, since this is where the code
14810 is going to run.  Only rarely is it necessary to test @samp{$build}
14811 (which is where the build is being done).
14813 Whenever you're tempted to use @samp{$host} it's worth considering
14814 whether some sort of probe would be better.  New system types come along
14815 periodically or previously missing features are added.  Well-written
14816 probes can adapt themselves to such things, but hard-coded lists of
14817 names won't.  Here are some guidelines,
14819 @itemize @bullet
14820 @item
14821 Availability of libraries and library functions should always be checked
14822 by probing.
14823 @item
14824 Variant behavior of system calls is best identified with runtime tests
14825 if possible, but bug workarounds or obscure difficulties might have to
14826 be driven from @samp{$host}.
14827 @item
14828 Assembler code is inevitably highly CPU-specific and is best selected
14829 according to @samp{$host_cpu}.
14830 @item
14831 Assembler variations like underscore prefix on globals or ELF versus
14832 COFF type directives are however best determined by probing, perhaps
14833 even examining the compiler output.
14834 @end itemize
14836 @samp{$target} is for use by a package creating a compiler or similar.
14837 For ordinary packages it's meaningless and should not be used.  It
14838 indicates what the created compiler should generate code for, if it can
14839 cross-compile.  @samp{$target} generally selects various hard-coded CPU
14840 and system conventions, since usually the compiler or tools under
14841 construction will themselves determine how the target will work.
14844 @c ===================================================== Site Configuration.
14846 @node Site Configuration
14847 @chapter Site Configuration
14849 @command{configure} scripts support several kinds of local configuration
14850 decisions.  There are ways for users to specify where external software
14851 packages are, include or exclude optional features, install programs
14852 under modified names, and set default values for @command{configure}
14853 options.
14855 @menu
14856 * Help Formatting::             Customizing @samp{configure --help}
14857 * External Software::           Working with other optional software
14858 * Package Options::             Selecting optional features
14859 * Pretty Help Strings::         Formatting help string
14860 * Site Details::                Configuring site details
14861 * Transforming Names::          Changing program names when installing
14862 * Site Defaults::               Giving @command{configure} local defaults
14863 @end menu
14865 @node Help Formatting
14866 @section Controlling Help Output
14868 Users will consult @samp{configure --help} to learn of configuration
14869 decisions specific to your package.  By default, @command{configure}
14870 breaks this output into sections for each type of option; within each
14871 section, help strings appear in the order @file{configure.ac} defines
14872 them:
14874 @example
14875 Optional Features:
14876   @dots{}
14877   --enable-bar            include bar
14879 Optional Packages:
14880   @dots{}
14881   --with-foo              use foo
14882 @end example
14884 @defmac AC_PRESERVE_HELP_ORDER
14885 @acindex{PRESERVE_HELP_ORDER}
14887 Request an alternate @option{--help} format, in which options of all
14888 types appear together, in the order defined.  Call this macro before any
14889 @code{AC_ARG_ENABLE} or @code{AC_ARG_WITH}.
14891 @example
14892 Optional Features and Packages:
14893   @dots{}
14894   --enable-bar            include bar
14895   --with-foo              use foo
14896 @end example
14898 @end defmac
14900 @node External Software
14901 @section Working With External Software
14902 @cindex External software
14904 Some packages require, or can optionally use, other software packages
14905 that are already installed.  The user can give @command{configure}
14906 command line options to specify which such external software to use.
14907 The options have one of these forms:
14909 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
14910 @c awful.
14911 @example
14912 --with-@var{package}[=@var{arg}]
14913 --without-@var{package}
14914 @end example
14916 For example, @option{--with-gnu-ld} means work with the @acronym{GNU} linker
14917 instead of some other linker.  @option{--with-x} means work with The X
14918 Window System.
14920 The user can give an argument by following the package name with
14921 @samp{=} and the argument.  Giving an argument of @samp{no} is for
14922 packages that are used by default; it says to @emph{not} use the
14923 package.  An argument that is neither @samp{yes} nor @samp{no} could
14924 include a name or number of a version of the other package, to specify
14925 more precisely which other package this program is supposed to work
14926 with.  If no argument is given, it defaults to @samp{yes}.
14927 @option{--without-@var{package}} is equivalent to
14928 @option{--with-@var{package}=no}.
14930 @command{configure} scripts do not complain about
14931 @option{--with-@var{package}} options that they do not support.  This
14932 behavior permits configuring a source tree containing multiple packages
14933 with a top-level @command{configure} script when the packages support
14934 different options, without spurious error messages about options that
14935 some of the packages support.  An unfortunate side effect is that option
14936 spelling errors are not diagnosed.  No better approach to this problem
14937 has been suggested so far.
14939 For each external software package that may be used, @file{configure.ac}
14940 should call @code{AC_ARG_WITH} to detect whether the @command{configure}
14941 user asked to use it.  Whether each package is used or not by default,
14942 and which arguments are valid, is up to you.
14944 @defmac AC_ARG_WITH (@var{package}, @var{help-string}, @ovar{action-if-given}, @ovar{action-if-not-given})
14945 @acindex{ARG_WITH}
14946 If the user gave @command{configure} the option @option{--with-@var{package}}
14947 or @option{--without-@var{package}}, run shell commands
14948 @var{action-if-given}.  If neither option was given, run shell commands
14949 @var{action-if-not-given}.  The name @var{package} indicates another
14950 software package that this program should work with.  It should consist
14951 only of alphanumeric characters and dashes.
14953 The option's argument is available to the shell commands
14954 @var{action-if-given} in the shell variable @code{withval}, which is
14955 actually just the value of the shell variable @code{with_@var{package}},
14956 with any @option{-} characters changed into @samp{_}.  You may use that
14957 variable instead, if you wish.
14959 The argument @var{help-string} is a description of the option that
14960 looks like this:
14961 @example
14962   --with-readline         support fancy command line editing
14963 @end example
14965 @noindent
14966 @var{help-string} may be more than one line long, if more detail is
14967 needed.  Just make sure the columns line up in @samp{configure
14968 --help}.  Avoid tabs in the help string.  You'll need to enclose the
14969 help string in @samp{[} and @samp{]} in order to produce the leading
14970 blanks.
14972 You should format your @var{help-string} with the macro
14973 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
14975 The following example shows how to use the @code{AC_ARG_WITH} macro in
14976 a common situation.  You want to let the user decide whether to enable
14977 support for an external library (e.g., the readline library); if the user
14978 specified neither @option{--with-readline} nor @option{--without-readline},
14979 you want to enable support for readline only if the library is available
14980 on the system.
14982 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
14983 @example
14984 AC_ARG_WITH([readline],
14985   [AS_HELP_STRING([--with-readline],
14986     [support fancy command line editing @@<:@@default=check@@:>@@])],
14987   [],
14988   [with_readline=check])
14990 LIBREADLINE=
14991 AS_IF([test "x$with_readline" != xno],
14992   [AC_CHECK_LIB([readline], [main],
14993     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
14994      AC_DEFINE([HAVE_LIBREADLINE], [1],
14995                [Define if you have libreadline])
14996     ],
14997     [if test "x$with_readline" != xcheck; then
14998        AC_MSG_FAILURE(
14999          [--with-readline was given, but test for readline failed])
15000      fi
15001     ], -lncurses)])
15002 @end example
15004 The next example shows how to use @code{AC_ARG_WITH} to give the user the
15005 possibility to enable support for the readline library, in case it is still
15006 experimental and not well tested, and is therefore disabled by default.
15008 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
15009 @example
15010 AC_ARG_WITH([readline],
15011   [AS_HELP_STRING([--with-readline],
15012     [enable experimental support for readline])],
15013   [],
15014   [with_readline=no])
15016 LIBREADLINE=
15017 AS_IF([test "x$with_readline" != xno],
15018   [AC_CHECK_LIB([readline], [main],
15019     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
15020      AC_DEFINE([HAVE_LIBREADLINE], [1],
15021                [Define if you have libreadline])
15022     ],
15023     [AC_MSG_FAILURE(
15024        [--with-readline was given, but test for readline failed])],
15025     [-lncurses])])
15026 @end example
15028 The last example shows how to use @code{AC_ARG_WITH} to give the user the
15029 possibility to disable support for the readline library, given that it is
15030 an important feature and that it should be enabled by default.
15032 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
15033 @example
15034 AC_ARG_WITH([readline],
15035   [AS_HELP_STRING([--without-readline],
15036     [disable support for readline])],
15037   [],
15038   [with_readline=yes])
15040 LIBREADLINE=
15041 AS_IF([test "x$with_readline" != xno],
15042   [AC_CHECK_LIB([readline], [main],
15043     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
15044      AC_DEFINE([HAVE_LIBREADLINE], [1],
15045                [Define if you have libreadline])
15046     ],
15047     [AC_MSG_FAILURE(
15048        [readline test failed (--without-readline to disable)])],
15049     [-lncurses])])
15050 @end example
15052 These three examples can be easily adapted to the case where
15053 @code{AC_ARG_ENABLE} should be preferred to @code{AC_ARG_WITH} (see
15054 @ref{Package Options}).
15055 @end defmac
15057 @defmac AC_WITH (@var{package}, @var{action-if-given}, @ovar{action-if-not-given})
15058 @acindex{WITH}
15059 This is an obsolete version of @code{AC_ARG_WITH} that does not
15060 support providing a help string.
15061 @end defmac
15063 @node Package Options
15064 @section Choosing Package Options
15065 @cindex Package options
15066 @cindex Options, package
15068 If a software package has optional compile-time features, the user can
15069 give @command{configure} command line options to specify whether to
15070 compile them.  The options have one of these forms:
15072 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
15073 @c awful.
15074 @example
15075 --enable-@var{feature}[=@var{arg}]
15076 --disable-@var{feature}
15077 @end example
15079 These options allow users to choose which optional features to build and
15080 install.  @option{--enable-@var{feature}} options should never make a
15081 feature behave differently or cause one feature to replace another.
15082 They should only cause parts of the program to be built rather than left
15083 out.
15085 The user can give an argument by following the feature name with
15086 @samp{=} and the argument.  Giving an argument of @samp{no} requests
15087 that the feature @emph{not} be made available.  A feature with an
15088 argument looks like @option{--enable-debug=stabs}.  If no argument is
15089 given, it defaults to @samp{yes}.  @option{--disable-@var{feature}} is
15090 equivalent to @option{--enable-@var{feature}=no}.
15092 @command{configure} scripts do not complain about
15093 @option{--enable-@var{feature}} options that they do not support.
15094 This behavior permits configuring a source tree containing multiple
15095 packages with a top-level @command{configure} script when the packages
15096 support different options, without spurious error messages about options
15097 that some of the packages support.
15098 An unfortunate side effect is that option spelling errors are not diagnosed.
15099 No better approach to this problem has been suggested so far.
15101 For each optional feature, @file{configure.ac} should call
15102 @code{AC_ARG_ENABLE} to detect whether the @command{configure} user asked
15103 to include it.  Whether each feature is included or not by default, and
15104 which arguments are valid, is up to you.
15106 @defmac AC_ARG_ENABLE (@var{feature}, @var{help-string}, @ovar{action-if-given}, @ovar{action-if-not-given})
15107 @acindex{ARG_ENABLE}
15108 If the user gave @command{configure} the option
15109 @option{--enable-@var{feature}} or @option{--disable-@var{feature}}, run
15110 shell commands @var{action-if-given}.  If neither option was given, run
15111 shell commands @var{action-if-not-given}.  The name @var{feature}
15112 indicates an optional user-level facility.  It should consist only of
15113 alphanumeric characters and dashes.
15115 The option's argument is available to the shell commands
15116 @var{action-if-given} in the shell variable @code{enableval}, which is
15117 actually just the value of the shell variable
15118 @code{enable_@var{feature}}, with any @option{-} characters changed into
15119 @samp{_}.  You may use that variable instead, if you wish.  The
15120 @var{help-string} argument is like that of @code{AC_ARG_WITH}
15121 (@pxref{External Software}).
15123 You should format your @var{help-string} with the macro
15124 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
15126 See the examples suggested with the definition of @code{AC_ARG_WITH}
15127 (@pxref{External Software}) to get an idea of possible applications of
15128 @code{AC_ARG_ENABLE}.
15129 @end defmac
15131 @defmac AC_ENABLE (@var{feature}, @var{action-if-given}, @ovar{action-if-not-given})
15132 @acindex{ENABLE}
15133 This is an obsolete version of @code{AC_ARG_ENABLE} that does not
15134 support providing a help string.
15135 @end defmac
15138 @node Pretty Help Strings
15139 @section Making Your Help Strings Look Pretty
15140 @cindex Help strings
15142 Properly formatting the @samp{help strings} which are used in
15143 @code{AC_ARG_WITH} (@pxref{External Software}) and @code{AC_ARG_ENABLE}
15144 (@pxref{Package Options}) can be challenging.  Specifically, you want
15145 your own @samp{help strings} to line up in the appropriate columns of
15146 @samp{configure --help} just like the standard Autoconf @samp{help
15147 strings} do.  This is the purpose of the @code{AS_HELP_STRING} macro.
15149 @defmac AS_HELP_STRING (@var{left-hand-side}, @var{right-hand-side})
15150 @acindex{HELP_STRING}
15152 Expands into an help string that looks pretty when the user executes
15153 @samp{configure --help}.  It is typically used in @code{AC_ARG_WITH}
15154 (@pxref{External Software}) or @code{AC_ARG_ENABLE} (@pxref{Package
15155 Options}).  The following example will make this clearer.
15157 @example
15158 AC_ARG_WITH([foo],
15159   [AS_HELP_STRING([--with-foo],
15160      [use foo (default is no)])],
15161   [use_foo=$withval],
15162   [use_foo=no])
15163 @end example
15165 The second argument of @code{AS_HELP_STRING} is
15166 not a literal, and should not be double quoted.
15167 @xref{Autoconf Language}, for a more detailed explanation.
15168 Then the last few lines of @samp{configure --help} will appear like
15169 this:
15171 @example
15172 --enable and --with options recognized:
15173   --with-foo              use foo (default is no)
15174 @end example
15176 The @code{AS_HELP_STRING} macro is particularly helpful when the
15177 @var{left-hand-side} and/or @var{right-hand-side} are composed of macro
15178 arguments, as shown in the following example.
15180 @example
15181 AC_DEFUN([MY_ARG_WITH],
15182   [AC_ARG_WITH([$1],
15183      [AS_HELP_STRING([--with-$1], [use $1 (default is $2)])],
15184      [use_[]$1=$withval],
15185      [use_[]$1=$2])])
15186 @end example
15187 @end defmac
15190 @node Site Details
15191 @section Configuring Site Details
15192 @cindex Site details
15194 Some software packages require complex site-specific information.  Some
15195 examples are host names to use for certain services, company names, and
15196 email addresses to contact.  Since some configuration scripts generated
15197 by Metaconfig ask for such information interactively, people sometimes
15198 wonder how to get that information in Autoconf-generated configuration
15199 scripts, which aren't interactive.
15201 Such site configuration information should be put in a file that is
15202 edited @emph{only by users}, not by programs.  The location of the file
15203 can either be based on the @code{prefix} variable, or be a standard
15204 location such as the user's home directory.  It could even be specified
15205 by an environment variable.  The programs should examine that file at
15206 runtime, rather than at compile time.  Runtime configuration is more
15207 convenient for users and makes the configuration process simpler than
15208 getting the information while configuring.  @xref{Directory Variables, ,
15209 Variables for Installation Directories, standards, @acronym{GNU} Coding
15210 Standards}, for more information on where to put data files.
15212 @node Transforming Names
15213 @section Transforming Program Names When Installing
15214 @cindex Transforming program names
15215 @cindex Program names, transforming
15217 Autoconf supports changing the names of programs when installing them.
15218 In order to use these transformations, @file{configure.ac} must call the
15219 macro @code{AC_ARG_PROGRAM}.
15221 @defmac AC_ARG_PROGRAM
15222 @acindex{ARG_PROGRAM}
15223 @ovindex program_transform_name
15224 Place in output variable @code{program_transform_name} a sequence of
15225 @code{sed} commands for changing the names of installed programs.
15227 If any of the options described below are given to @command{configure},
15228 program names are transformed accordingly.  Otherwise, if
15229 @code{AC_CANONICAL_TARGET} has been called and a @option{--target} value
15230 is given, the target type followed by a dash is used as a prefix.
15231 Otherwise, no program name transformation is done.
15232 @end defmac
15234 @menu
15235 * Transformation Options::      @command{configure} options to transform names
15236 * Transformation Examples::     Sample uses of transforming names
15237 * Transformation Rules::        @file{Makefile} uses of transforming names
15238 @end menu
15240 @node Transformation Options
15241 @subsection Transformation Options
15243 You can specify name transformations by giving @command{configure} these
15244 command line options:
15246 @table @option
15247 @item --program-prefix=@var{prefix}
15248 prepend @var{prefix} to the names;
15250 @item --program-suffix=@var{suffix}
15251 append @var{suffix} to the names;
15253 @item --program-transform-name=@var{expression}
15254 perform @code{sed} substitution @var{expression} on the names.
15255 @end table
15257 @node Transformation Examples
15258 @subsection Transformation Examples
15260 These transformations are useful with programs that can be part of a
15261 cross-compilation development environment.  For example, a
15262 cross-assembler running on a Sun 4 configured with
15263 @option{--target=i960-vxworks} is normally installed as
15264 @file{i960-vxworks-as}, rather than @file{as}, which could be confused
15265 with a native Sun 4 assembler.
15267 You can force a program name to begin with @file{g}, if you don't want
15268 @acronym{GNU} programs installed on your system to shadow other programs with
15269 the same name.  For example, if you configure @acronym{GNU} @code{diff} with
15270 @option{--program-prefix=g}, then when you run @samp{make install} it is
15271 installed as @file{/usr/local/bin/gdiff}.
15273 As a more sophisticated example, you could use
15275 @example
15276 --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/'
15277 @end example
15278 @noindent
15280 to prepend @samp{g} to most of the program names in a source tree,
15281 excepting those like @code{gdb} that already have one and those like
15282 @code{less} and @code{lesskey} that aren't @acronym{GNU} programs.  (That is
15283 assuming that you have a source tree containing those programs that is
15284 set up to use this feature.)
15286 One way to install multiple versions of some programs simultaneously is
15287 to append a version number to the name of one or both.  For example, if
15288 you want to keep Autoconf version 1 around for awhile, you can configure
15289 Autoconf version 2 using @option{--program-suffix=2} to install the
15290 programs as @file{/usr/local/bin/autoconf2},
15291 @file{/usr/local/bin/autoheader2}, etc.  Nevertheless, pay attention
15292 that only the binaries are renamed, therefore you'd have problems with
15293 the library files which might overlap.
15295 @node Transformation Rules
15296 @subsection Transformation Rules
15298 Here is how to use the variable @code{program_transform_name} in a
15299 @file{Makefile.in}:
15301 @example
15302 PROGRAMS = cp ls rm
15303 transform = @@program_transform_name@@
15304 install:
15305         for p in $(PROGRAMS); do \
15306           $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p | \
15307                                               sed '$(transform)'`; \
15308         done
15310 uninstall:
15311         for p in $(PROGRAMS); do \
15312           rm -f $(DESTDIR)$(bindir)/`echo $$p | sed '$(transform)'`; \
15313         done
15314 @end example
15316 It is guaranteed that @code{program_transform_name} is never empty, and
15317 that there are no useless separators.  Therefore you may safely embed
15318 @code{program_transform_name} within a sed program using @samp{;}:
15320 @example
15321 transform = @@program_transform_name@@
15322 transform_exe = s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/
15323 @end example
15325 Whether to do the transformations on documentation files (Texinfo or
15326 @code{man}) is a tricky question; there seems to be no perfect answer,
15327 due to the several reasons for name transforming.  Documentation is not
15328 usually particular to a specific architecture, and Texinfo files do not
15329 conflict with system documentation.  But they might conflict with
15330 earlier versions of the same files, and @code{man} pages sometimes do
15331 conflict with system documentation.  As a compromise, it is probably
15332 best to do name transformations on @code{man} pages but not on Texinfo
15333 manuals.
15335 @node Site Defaults
15336 @section Setting Site Defaults
15337 @cindex Site defaults
15339 Autoconf-generated @command{configure} scripts allow your site to provide
15340 default values for some configuration values.  You do this by creating
15341 site- and system-wide initialization files.
15343 @evindex CONFIG_SITE
15344 If the environment variable @code{CONFIG_SITE} is set, @command{configure}
15345 uses its value as the name of a shell script to read.  Otherwise, it
15346 reads the shell script @file{@var{prefix}/share/config.site} if it exists,
15347 then @file{@var{prefix}/etc/config.site} if it exists.  Thus,
15348 settings in machine-specific files override those in machine-independent
15349 ones in case of conflict.
15351 Site files can be arbitrary shell scripts, but only certain kinds of
15352 code are really appropriate to be in them.  Because @command{configure}
15353 reads any cache file after it has read any site files, a site file can
15354 define a default cache file to be shared between all Autoconf-generated
15355 @command{configure} scripts run on that system (@pxref{Cache Files}).  If
15356 you set a default cache file in a site file, it is a good idea to also
15357 set the output variable @code{CC} in that site file, because the cache
15358 file is only valid for a particular compiler, but many systems have
15359 several available.
15361 You can examine or override the value set by a command line option to
15362 @command{configure} in a site file; options set shell variables that have
15363 the same names as the options, with any dashes turned into underscores.
15364 The exceptions are that @option{--without-} and @option{--disable-} options
15365 are like giving the corresponding @option{--with-} or @option{--enable-}
15366 option and the value @samp{no}.  Thus, @option{--cache-file=localcache}
15367 sets the variable @code{cache_file} to the value @samp{localcache};
15368 @option{--enable-warnings=no} or @option{--disable-warnings} sets the variable
15369 @code{enable_warnings} to the value @samp{no}; @option{--prefix=/usr} sets the
15370 variable @code{prefix} to the value @samp{/usr}; etc.
15372 Site files are also good places to set default values for other output
15373 variables, such as @code{CFLAGS}, if you need to give them non-default
15374 values: anything you would normally do, repetitively, on the command
15375 line.  If you use non-default values for @var{prefix} or
15376 @var{exec_prefix} (wherever you locate the site file), you can set them
15377 in the site file if you specify it with the @code{CONFIG_SITE}
15378 environment variable.
15380 You can set some cache values in the site file itself.  Doing this is
15381 useful if you are cross-compiling, where it is impossible to check features
15382 that require running a test program.  You could ``prime the cache'' by
15383 setting those values correctly for that system in
15384 @file{@var{prefix}/etc/config.site}.  To find out the names of the cache
15385 variables you need to set, look for shell variables with @samp{_cv_} in
15386 their names in the affected @command{configure} scripts, or in the Autoconf
15387 M4 source code for those macros.
15389 The cache file is careful to not override any variables set in the site
15390 files.  Similarly, you should not override command-line options in the
15391 site files.  Your code should check that variables such as @code{prefix}
15392 and @code{cache_file} have their default values (as set near the top of
15393 @command{configure}) before changing them.
15395 Here is a sample file @file{/usr/share/local/gnu/share/config.site}.  The
15396 command @samp{configure --prefix=/usr/share/local/gnu} would read this
15397 file (if @code{CONFIG_SITE} is not set to a different file).
15399 @example
15400 # config.site for configure
15402 # Change some defaults.
15403 test "$prefix" = NONE && prefix=/usr/share/local/gnu
15404 test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu
15405 test "$sharedstatedir" = '$prefix/com' && sharedstatedir=/var
15406 test "$localstatedir" = '$prefix/var' && localstatedir=/var
15408 # Give Autoconf 2.x generated configure scripts a shared default
15409 # cache file for feature test results, architecture-specific.
15410 if test "$cache_file" = /dev/null; then
15411   cache_file="$prefix/var/config.cache"
15412   # A cache file is only valid for one C compiler.
15413   CC=gcc
15415 @end example
15418 @c ============================================== Running configure Scripts.
15420 @node Running configure Scripts
15421 @chapter Running @command{configure} Scripts
15422 @cindex @command{configure}
15424 Below are instructions on how to configure a package that uses a
15425 @command{configure} script, suitable for inclusion as an @file{INSTALL}
15426 file in the package.  A plain-text version of @file{INSTALL} which you
15427 may use comes with Autoconf.
15429 @menu
15430 * Basic Installation::          Instructions for typical cases
15431 * Compilers and Options::       Selecting compilers and optimization
15432 * Multiple Architectures::      Compiling for multiple architectures at once
15433 * Installation Names::          Installing in different directories
15434 * Optional Features::           Selecting optional features
15435 * System Type::                 Specifying the system type
15436 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
15437 * Defining Variables::          Specifying the compiler etc.
15438 * configure Invocation::        Changing how @command{configure} runs
15439 @end menu
15441 @set autoconf
15442 @include install.texi
15445 @c ============================================== Recreating a Configuration
15447 @node config.status Invocation
15448 @chapter Recreating a Configuration
15449 @cindex @command{config.status}
15451 The @command{configure} script creates a file named @file{config.status},
15452 which actually configures, @dfn{instantiates}, the template files.  It
15453 also records the configuration options that were specified when the
15454 package was last configured in case reconfiguring is needed.
15456 Synopsis:
15457 @example
15458 ./config.status @var{option}@dots{} [@var{file}@dots{}]
15459 @end example
15461 It configures the @var{files}; if none are specified, all the templates
15462 are instantiated.  The files must be specified without their
15463 dependencies, as in
15465 @example
15466 ./config.status foobar
15467 @end example
15469 @noindent
15472 @example
15473 ./config.status foobar:foo.in:bar.in
15474 @end example
15476 The supported @var{option}s are:
15478 @table @option
15479 @item --help
15480 @itemx -h
15481 Print a summary of the command line options, the list of the template
15482 files, and exit.
15484 @item --version
15485 @itemx -V
15486 Print the version number of Autoconf and exit.
15488 @item --silent
15489 @itemx --quiet
15490 @itemx -q
15491 Do not print progress messages.
15493 @item --debug
15494 @itemx -d
15495 Don't remove the temporary files.
15497 @item --file=@var{file}[:@var{template}]
15498 Require that @var{file} be instantiated as if
15499 @samp{AC_CONFIG_FILES(@var{file}:@var{template})} was used.  Both
15500 @var{file} and @var{template} may be @samp{-} in which case the standard
15501 output and/or standard input, respectively, is used.  If a
15502 @var{template} file name is relative, it is first looked for in the build
15503 tree, and then in the source tree.  @xref{Configuration Actions}, for
15504 more details.
15506 This option and the following ones provide one way for separately
15507 distributed packages to share the values computed by @command{configure}.
15508 Doing so can be useful if some of the packages need a superset of the
15509 features that one of them, perhaps a common library, does.  These
15510 options allow a @file{config.status} file to create files other than the
15511 ones that its @file{configure.ac} specifies, so it can be used for a
15512 different package.
15514 @item --header=@var{file}[:@var{template}]
15515 Same as @option{--file} above, but with @samp{AC_CONFIG_HEADERS}.
15517 @item --recheck
15518 Ask @file{config.status} to update itself and exit (no instantiation).
15519 This option is useful if you change @command{configure}, so that the
15520 results of some tests might be different from the previous run.  The
15521 @option{--recheck} option re-runs @command{configure} with the same arguments
15522 you used before, plus the @option{--no-create} option, which prevents
15523 @command{configure} from running @file{config.status} and creating
15524 @file{Makefile} and other files, and the @option{--no-recursion} option,
15525 which prevents @command{configure} from running other @command{configure}
15526 scripts in subdirectories.  (This is so other @file{Makefile} rules can
15527 run @file{config.status} when it changes; @pxref{Automatic Remaking},
15528 for an example).
15529 @end table
15531 @file{config.status} checks several optional environment variables that
15532 can alter its behavior:
15534 @defvar CONFIG_SHELL
15535 @evindex CONFIG_SHELL
15536 The shell with which to run @command{configure} for the @option{--recheck}
15537 option.  It must be Bourne-compatible.  The default is a shell that
15538 supports @code{LINENO} if available, and @file{/bin/sh} otherwise.
15539 Invoking @command{configure} by hand bypasses this setting, so you may
15540 need to use a command like @samp{CONFIG_SHELL=/bin/bash /bin/bash ./configure}
15541 to insure that the same shell is used everywhere.  The absolute name of the
15542 shell should be passed.
15543 @end defvar
15545 @defvar CONFIG_STATUS
15546 @evindex CONFIG_STATUS
15547 The file name to use for the shell script that records the
15548 configuration.  The default is @file{./config.status}.  This variable is
15549 useful when one package uses parts of another and the @command{configure}
15550 scripts shouldn't be merged because they are maintained separately.
15551 @end defvar
15553 You can use @file{./config.status} in your Makefiles.  For example, in
15554 the dependencies given above (@pxref{Automatic Remaking}),
15555 @file{config.status} is run twice when @file{configure.ac} has changed.
15556 If that bothers you, you can make each run only regenerate the files for
15557 that rule:
15558 @example
15559 @group
15560 config.h: stamp-h
15561 stamp-h: config.h.in config.status
15562         ./config.status config.h
15563         echo > stamp-h
15565 Makefile: Makefile.in config.status
15566         ./config.status Makefile
15567 @end group
15568 @end example
15570 The calling convention of @file{config.status} has changed; see
15571 @ref{Obsolete config.status Use}, for details.
15574 @c =================================================== Obsolete Constructs
15576 @node Obsolete Constructs
15577 @chapter Obsolete Constructs
15578 @cindex Obsolete constructs
15580 Autoconf changes, and throughout the years some constructs have been
15581 obsoleted.  Most of the changes involve the macros, but in some cases
15582 the tools themselves, or even some concepts, are now considered
15583 obsolete.
15585 You may completely skip this chapter if you are new to Autoconf.  Its
15586 intention is mainly to help maintainers updating their packages by
15587 understanding how to move to more modern constructs.
15589 @menu
15590 * Obsolete config.status Use::  Different calling convention
15591 * acconfig.h::                  Additional entries in @file{config.h.in}
15592 * autoupdate Invocation::       Automatic update of @file{configure.ac}
15593 * Obsolete Macros::             Backward compatibility macros
15594 * Autoconf 1::                  Tips for upgrading your files
15595 * Autoconf 2.13::               Some fresher tips
15596 @end menu
15598 @node Obsolete config.status Use
15599 @section Obsolete @file{config.status} Invocation
15601 @file{config.status} now supports arguments to specify the files to
15602 instantiate; see @ref{config.status Invocation}, for more details.
15603 Before, environment variables had to be used.
15605 @defvar CONFIG_COMMANDS
15606 @evindex CONFIG_COMMANDS
15607 The tags of the commands to execute.  The default is the arguments given
15608 to @code{AC_OUTPUT} and @code{AC_CONFIG_COMMANDS} in
15609 @file{configure.ac}.
15610 @end defvar
15612 @defvar CONFIG_FILES
15613 @evindex CONFIG_FILES
15614 The files in which to perform @samp{@@@var{variable}@@} substitutions.
15615 The default is the arguments given to @code{AC_OUTPUT} and
15616 @code{AC_CONFIG_FILES} in @file{configure.ac}.
15617 @end defvar
15619 @defvar CONFIG_HEADERS
15620 @evindex CONFIG_HEADERS
15621 The files in which to substitute C @code{#define} statements.  The
15622 default is the arguments given to @code{AC_CONFIG_HEADERS}; if that
15623 macro was not called, @file{config.status} ignores this variable.
15624 @end defvar
15626 @defvar CONFIG_LINKS
15627 @evindex CONFIG_LINKS
15628 The symbolic links to establish.  The default is the arguments given to
15629 @code{AC_CONFIG_LINKS}; if that macro was not called,
15630 @file{config.status} ignores this variable.
15631 @end defvar
15633 In @ref{config.status Invocation}, using this old interface, the example
15634 would be:
15636 @example
15637 @group
15638 config.h: stamp-h
15639 stamp-h: config.h.in config.status
15640         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_FILES= \
15641           CONFIG_HEADERS=config.h ./config.status
15642         echo > stamp-h
15644 Makefile: Makefile.in config.status
15645         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_HEADERS= \
15646           CONFIG_FILES=Makefile ./config.status
15647 @end group
15648 @end example
15650 @noindent
15651 (If @file{configure.ac} does not call @code{AC_CONFIG_HEADERS}, there is
15652 no need to set @code{CONFIG_HEADERS} in the @code{make} rules.  Equally
15653 for @code{CONFIG_COMMANDS}, etc.)
15656 @node acconfig.h
15657 @section @file{acconfig.h}
15659 @cindex @file{acconfig.h}
15660 @cindex @file{config.h.top}
15661 @cindex @file{config.h.bot}
15663 In order to produce @file{config.h.in}, @command{autoheader} needs to
15664 build or to find templates for each symbol.  Modern releases of Autoconf
15665 use @code{AH_VERBATIM} and @code{AH_TEMPLATE} (@pxref{Autoheader
15666 Macros}), but in older releases a file, @file{acconfig.h}, contained the
15667 list of needed templates.  @command{autoheader} copied comments and
15668 @code{#define} and @code{#undef} statements from @file{acconfig.h} in
15669 the current directory, if present.  This file used to be mandatory if
15670 you @code{AC_DEFINE} any additional symbols.
15672 Modern releases of Autoconf also provide @code{AH_TOP} and
15673 @code{AH_BOTTOM} if you need to prepend/append some information to
15674 @file{config.h.in}.  Ancient versions of Autoconf had a similar feature:
15675 if @file{./acconfig.h} contains the string @samp{@@TOP@@},
15676 @command{autoheader} copies the lines before the line containing
15677 @samp{@@TOP@@} into the top of the file that it generates.  Similarly,
15678 if @file{./acconfig.h} contains the string @samp{@@BOTTOM@@},
15679 @command{autoheader} copies the lines after that line to the end of the
15680 file it generates.  Either or both of those strings may be omitted.  An
15681 even older alternate way to produce the same effect in ancient versions
15682 of Autoconf is to create the files @file{@var{file}.top} (typically
15683 @file{config.h.top}) and/or @file{@var{file}.bot} in the current
15684 directory.  If they exist, @command{autoheader} copies them to the
15685 beginning and end, respectively, of its output.
15687 In former versions of Autoconf, the files used in preparing a software
15688 package for distribution were:
15689 @example
15690 @group
15691 configure.ac --.   .------> autoconf* -----> configure
15692                +---+
15693 [aclocal.m4] --+   `---.
15694 [acsite.m4] ---'       |
15695                        +--> [autoheader*] -> [config.h.in]
15696 [acconfig.h] ----.     |
15697                  +-----'
15698 [config.h.top] --+
15699 [config.h.bot] --'
15700 @end group
15701 @end example
15703 Using only the @code{AH_} macros, @file{configure.ac} should be
15704 self-contained, and should not depend upon @file{acconfig.h} etc.
15707 @node autoupdate Invocation
15708 @section Using @command{autoupdate} to Modernize @file{configure.ac}
15709 @cindex @command{autoupdate}
15711 The @command{autoupdate} program updates a @file{configure.ac} file that
15712 calls Autoconf macros by their old names to use the current macro names.
15713 In version 2 of Autoconf, most of the macros were renamed to use a more
15714 uniform and descriptive naming scheme.  @xref{Macro Names}, for a
15715 description of the new scheme.  Although the old names still work
15716 (@pxref{Obsolete Macros}, for a list of the old macros and the corresponding
15717 new names), you can make your @file{configure.ac} files more readable
15718 and make it easier to use the current Autoconf documentation if you
15719 update them to use the new macro names.
15721 @evindex SIMPLE_BACKUP_SUFFIX
15722 If given no arguments, @command{autoupdate} updates @file{configure.ac},
15723 backing up the original version with the suffix @file{~} (or the value
15724 of the environment variable @code{SIMPLE_BACKUP_SUFFIX}, if that is
15725 set).  If you give @command{autoupdate} an argument, it reads that file
15726 instead of @file{configure.ac} and writes the updated file to the
15727 standard output.
15729 @noindent
15730 @command{autoupdate} accepts the following options:
15732 @table @option
15733 @item --help
15734 @itemx -h
15735 Print a summary of the command line options and exit.
15737 @item --version
15738 @itemx -V
15739 Print the version number of Autoconf and exit.
15741 @item --verbose
15742 @itemx -v
15743 Report processing steps.
15745 @item --debug
15746 @itemx -d
15747 Don't remove the temporary files.
15749 @item --force
15750 @itemx -f
15751 Force the update even if the file has not changed.  Disregard the cache.
15753 @item --include=@var{dir}
15754 @itemx -I @var{dir}
15755 Also look for input files in @var{dir}.  Multiple invocations accumulate.
15756 Directories are browsed from last to first.
15757 @end table
15759 @node Obsolete Macros
15760 @section Obsolete Macros
15762 Several macros are obsoleted in Autoconf, for various reasons (typically
15763 they failed to quote properly, couldn't be extended for more recent
15764 issues, etc.).  They are still supported, but deprecated: their use
15765 should be avoided.
15767 During the jump from Autoconf version 1 to version 2, most of the
15768 macros were renamed to use a more uniform and descriptive naming scheme,
15769 but their signature did not change.  @xref{Macro Names}, for a
15770 description of the new naming scheme.  Below, if there is just the mapping
15771 from old names to new names for these macros, the reader is invited to
15772 refer to the definition of the new macro for the signature and the
15773 description.
15775 @defmac AC_ALLOCA
15776 @acindex{ALLOCA}
15777 @code{AC_FUNC_ALLOCA}
15778 @end defmac
15780 @defmac AC_ARG_ARRAY
15781 @acindex{ARG_ARRAY}
15782 removed because of limited usefulness
15783 @end defmac
15785 @defmac AC_C_CROSS
15786 @acindex{C_CROSS}
15787 This macro is obsolete; it does nothing.
15788 @end defmac
15790 @defmac AC_C_LONG_DOUBLE
15791 @acindex{C_LONG_DOUBLE}
15792 @cvindex HAVE_LONG_DOUBLE
15793 If the C compiler supports a working @code{long double} type with more
15794 range or precision than the @code{double} type, define
15795 @code{HAVE_LONG_DOUBLE}.
15797 You should use @code{AC_TYPE_LONG_DOUBLE} or
15798 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
15799 @end defmac
15801 @defmac AC_CANONICAL_SYSTEM
15802 @acindex{CANONICAL_SYSTEM}
15803 Determine the system type and set output variables to the names of the
15804 canonical system types.  @xref{Canonicalizing}, for details about the
15805 variables this macro sets.
15807 The user is encouraged to use either @code{AC_CANONICAL_BUILD}, or
15808 @code{AC_CANONICAL_HOST}, or @code{AC_CANONICAL_TARGET}, depending on
15809 the needs.  Using @code{AC_CANONICAL_TARGET} is enough to run the two
15810 other macros.
15811 @end defmac
15813 @defmac AC_CHAR_UNSIGNED
15814 @acindex{CHAR_UNSIGNED}
15815 @code{AC_C_CHAR_UNSIGNED}
15816 @end defmac
15818 @defmac AC_CHECK_TYPE (@var{type}, @var{default})
15819 @acindex{CHECK_TYPE}
15820 Autoconf, up to 2.13, used to provide this version of
15821 @code{AC_CHECK_TYPE}, deprecated because of its flaws.  Firstly, although
15822 it is a member of the @code{CHECK} clan, singular sub-family, it does
15823 more than just checking.  Secondly, missing types are not
15824 @code{typedef}'d, they are @code{#define}'d, which can lead to
15825 incompatible code in the case of pointer types.
15827 This use of @code{AC_CHECK_TYPE} is obsolete and discouraged; see
15828 @ref{Generic Types}, for the description of the current macro.
15830 If the type @var{type} is not defined, define it to be the C (or C++)
15831 builtin type @var{default}, e.g., @samp{short int} or @samp{unsigned int}.
15833 This macro is equivalent to:
15835 @example
15836 AC_CHECK_TYPE([@var{type}], [],
15837   [AC_DEFINE_UNQUOTED([@var{type}], [@var{default}],
15838      [Define to `@var{default}'
15839       if <sys/types.h> does not define.])])
15840 @end example
15842 In order to keep backward compatibility, the two versions of
15843 @code{AC_CHECK_TYPE} are implemented, selected by a simple heuristics:
15845 @enumerate
15846 @item
15847 If there are three or four arguments, the modern version is used.
15849 @item
15850 If the second argument appears to be a C or C++ type, then the
15851 obsolete version is used.  This happens if the argument is a C or C++
15852 @emph{builtin} type or a C identifier ending in @samp{_t}, optionally
15853 followed by one of @samp{[(* } and then by a string of zero or more
15854 characters taken from the set @samp{[]()* _a-zA-Z0-9}.
15856 @item
15857 If the second argument is spelled with the alphabet of valid C and C++
15858 types, the user is warned and the modern version is used.
15860 @item
15861 Otherwise, the modern version is used.
15862 @end enumerate
15864 @noindent
15865 You are encouraged either to use a valid builtin type, or to use the
15866 equivalent modern code (see above), or better yet, to use
15867 @code{AC_CHECK_TYPES} together with
15869 @example
15870 #if !HAVE_LOFF_T
15871 typedef loff_t off_t;
15872 #endif
15873 @end example
15874 @end defmac
15875 @c end of AC_CHECK_TYPE
15877 @defmac AC_CHECKING (@var{feature-description})
15878 @acindex{CHECKING}
15879 Same as @samp{AC_MSG_NOTICE([checking @var{feature-description}@dots{}]}.
15880 @end defmac
15882 @defmac AC_COMPILE_CHECK (@var{echo-text}, @var{includes}, @var{function-body}, @var{action-if-true}, @ovar{action-if-false})
15883 @acindex{COMPILE_CHECK}
15884 This is an obsolete version of @code{AC_TRY_COMPILE} itself replaced by
15885 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}), with the
15886 addition that it prints @samp{checking for @var{echo-text}} to the
15887 standard output first, if @var{echo-text} is non-empty.  Use
15888 @code{AC_MSG_CHECKING} and @code{AC_MSG_RESULT} instead to print
15889 messages (@pxref{Printing Messages}).
15890 @end defmac
15892 @defmac AC_CONST
15893 @acindex{CONST}
15894 @code{AC_C_CONST}
15895 @end defmac
15897 @defmac AC_CROSS_CHECK
15898 @acindex{CROSS_CHECK}
15899 Same as @code{AC_C_CROSS}, which is obsolete too, and does nothing
15900 @code{:-)}.
15901 @end defmac
15903 @defmac AC_CYGWIN
15904 @acindex{CYGWIN}
15905 Check for the Cygwin environment in which case the shell variable
15906 @code{CYGWIN} is set to @samp{yes}.  Don't use this macro, the dignified
15907 means to check the nature of the host is using
15908 @code{AC_CANONICAL_HOST}.  As a matter of fact this macro is defined as:
15910 @example
15911 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
15912 case $host_os in
15913   *cygwin* ) CYGWIN=yes;;
15914          * ) CYGWIN=no;;
15915 esac
15916 @end example
15918 Beware that the variable @code{CYGWIN} has a very special meaning when
15919 running Cygwin, and should not be changed.  That's yet another reason
15920 not to use this macro.
15921 @end defmac
15923 @defmac AC_DECL_SYS_SIGLIST
15924 @acindex{DECL_SYS_SIGLIST}
15925 @cvindex SYS_SIGLIST_DECLARED
15926 Same as:
15928 @example
15929 AC_CHECK_DECLS([sys_siglist], [], [],
15930 [#include <signal.h>
15931 /* NetBSD declares sys_siglist in unistd.h.  */
15932 #if HAVE_UNISTD_H
15933 # include <unistd.h>
15934 #endif
15936 @end example
15937 @end defmac
15939 @defmac AC_DECL_YYTEXT
15940 @acindex{DECL_YYTEXT}
15941 Does nothing, now integrated in @code{AC_PROG_LEX}.
15942 @end defmac
15944 @defmac AC_DIR_HEADER
15945 @acindex{DIR_HEADER}
15946 @cvindex DIRENT
15947 @cvindex SYSNDIR
15948 @cvindex SYSDIR
15949 @cvindex NDIR
15950 Like calling @code{AC_FUNC_CLOSEDIR_VOID} and@code{AC_HEADER_DIRENT},
15951 but defines a different set of C preprocessor macros to indicate which
15952 header file is found:
15954 @multitable {@file{sys/ndir.h}} {Old Symbol} {@code{HAVE_SYS_NDIR_H}}
15955 @item Header            @tab Old Symbol     @tab New Symbol
15956 @item @file{dirent.h}   @tab @code{DIRENT}  @tab @code{HAVE_DIRENT_H}
15957 @item @file{sys/ndir.h} @tab @code{SYSNDIR} @tab @code{HAVE_SYS_NDIR_H}
15958 @item @file{sys/dir.h}  @tab @code{SYSDIR}  @tab @code{HAVE_SYS_DIR_H}
15959 @item @file{ndir.h}     @tab @code{NDIR}    @tab @code{HAVE_NDIR_H}
15960 @end multitable
15961 @end defmac
15963 @defmac AC_DYNIX_SEQ
15964 @acindex{DYNIX_SEQ}
15965 If on DYNIX/ptx, add @option{-lseq} to output variable
15966 @code{LIBS}.  This macro used to be defined as
15968 @example
15969 AC_CHECK_LIB([seq], [getmntent], [LIBS="-lseq $LIBS"])
15970 @end example
15972 @noindent
15973 now it is just @code{AC_FUNC_GETMNTENT}.
15974 @end defmac
15976 @defmac AC_EXEEXT
15977 @acindex{EXEEXT}
15978 @ovindex EXEEXT
15979 Defined the output variable @code{EXEEXT} based on the output of the
15980 compiler, which is now done automatically.  Typically set to empty
15981 string if Posix and @samp{.exe} if a @acronym{DOS} variant.
15982 @end defmac
15984 @defmac AC_EMXOS2
15985 @acindex{EMXOS2}
15986 Similar to @code{AC_CYGWIN} but checks for the EMX environment on OS/2
15987 and sets @code{EMXOS2}.
15988 @end defmac
15990 @defmac AC_ERROR
15991 @acindex{ERROR}
15992 @code{AC_MSG_ERROR}
15993 @end defmac
15995 @defmac AC_FIND_X
15996 @acindex{FIND_X}
15997 @code{AC_PATH_X}
15998 @end defmac
16000 @defmac AC_FIND_XTRA
16001 @acindex{FIND_XTRA}
16002 @code{AC_PATH_XTRA}
16003 @end defmac
16005 @defmac AC_FOREACH
16006 @acindex{FOREACH}
16007 @code{m4_foreach_w}
16008 @end defmac
16010 @defmac AC_FUNC_CHECK
16011 @acindex{FUNC_CHECK}
16012 @code{AC_CHECK_FUNC}
16013 @end defmac
16015 @defmac AC_FUNC_WAIT3
16016 @acindex{FUNC_WAIT3}
16017 @cvindex HAVE_WAIT3
16018 If @code{wait3} is found and fills in the contents of its third argument
16019 (a @samp{struct rusage *}), which HP-UX does not do, define
16020 @code{HAVE_WAIT3}.
16022 These days portable programs should use @code{waitpid}, not
16023 @code{wait3}, as @code{wait3} has been removed from Posix.
16024 @end defmac
16026 @defmac AC_GCC_TRADITIONAL
16027 @acindex{GCC_TRADITIONAL}
16028 @code{AC_PROG_GCC_TRADITIONAL}
16029 @end defmac
16031 @defmac AC_GETGROUPS_T
16032 @acindex{GETGROUPS_T}
16033 @code{AC_TYPE_GETGROUPS}
16034 @end defmac
16036 @defmac AC_GETLOADAVG
16037 @acindex{GETLOADAVG}
16038 @code{AC_FUNC_GETLOADAVG}
16039 @end defmac
16041 @defmac AC_HAVE_FUNCS
16042 @acindex{HAVE_FUNCS}
16043 @code{AC_CHECK_FUNCS}
16044 @end defmac
16046 @defmac AC_HAVE_HEADERS
16047 @acindex{HAVE_HEADERS}
16048 @code{AC_CHECK_HEADERS}
16049 @end defmac
16051 @defmac AC_HAVE_LIBRARY (@var{library}, @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
16052 @acindex{HAVE_LIBRARY}
16053 This macro is equivalent to calling @code{AC_CHECK_LIB} with a
16054 @var{function} argument of @code{main}.  In addition, @var{library} can
16055 be written as any of @samp{foo}, @option{-lfoo}, or @samp{libfoo.a}.  In
16056 all of those cases, the compiler is passed @option{-lfoo}.  However,
16057 @var{library} cannot be a shell variable; it must be a literal name.
16058 @end defmac
16060 @defmac AC_HAVE_POUNDBANG
16061 @acindex{HAVE_POUNDBANG}
16062 @code{AC_SYS_INTERPRETER} (different calling convention)
16063 @end defmac
16065 @defmac AC_HEADER_CHECK
16066 @acindex{HEADER_CHECK}
16067 @code{AC_CHECK_HEADER}
16068 @end defmac
16070 @defmac AC_HEADER_EGREP
16071 @acindex{HEADER_EGREP}
16072 @code{AC_EGREP_HEADER}
16073 @end defmac
16075 @defmac AC_HELP_STRING
16076 @acindex{HELP_STRING}
16077 @code{AS_HELP_STRING}
16078 @end defmac
16080 @defmac AC_INIT (@var{unique-file-in-source-dir})
16081 @acindex{INIT}
16082 Formerly @code{AC_INIT} used to have a single argument, and was
16083 equivalent to:
16085 @example
16086 AC_INIT
16087 AC_CONFIG_SRCDIR(@var{unique-file-in-source-dir})
16088 @end example
16089 @end defmac
16091 @defmac AC_INLINE
16092 @acindex{INLINE}
16093 @code{AC_C_INLINE}
16094 @end defmac
16096 @defmac AC_INT_16_BITS
16097 @acindex{INT_16_BITS}
16098 @cvindex INT_16_BITS
16099 If the C type @code{int} is 16 bits wide, define @code{INT_16_BITS}.
16100 Use @samp{AC_CHECK_SIZEOF(int)} instead.
16101 @end defmac
16103 @defmac AC_IRIX_SUN
16104 @acindex{IRIX_SUN}
16105 If on @sc{irix} (Silicon Graphics Unix), add @option{-lsun} to output
16106 @code{LIBS}.  If you were using it to get @code{getmntent}, use
16107 @code{AC_FUNC_GETMNTENT} instead.  If you used it for the NIS versions
16108 of the password and group functions, use @samp{AC_CHECK_LIB(sun,
16109 getpwnam)}.  Up to Autoconf 2.13, it used to be
16111 @example
16112 AC_CHECK_LIB([sun], [getmntent], [LIBS="-lsun $LIBS"])
16113 @end example
16115 @noindent
16116 now it is defined as
16118 @example
16119 AC_FUNC_GETMNTENT
16120 AC_CHECK_LIB([sun], [getpwnam])
16121 @end example
16122 @end defmac
16124 @defmac AC_LANG_C
16125 @acindex{LANG_C}
16126 Same as @samp{AC_LANG([C])}.
16127 @end defmac
16129 @defmac AC_LANG_CPLUSPLUS
16130 @acindex{LANG_CPLUSPLUS}
16131 Same as @samp{AC_LANG([C++])}.
16132 @end defmac
16134 @defmac AC_LANG_FORTRAN77
16135 @acindex{LANG_FORTRAN77}
16136 Same as @samp{AC_LANG([Fortran 77])}.
16137 @end defmac
16139 @defmac AC_LANG_RESTORE
16140 @acindex{LANG_RESTORE}
16141 Select the @var{language} that is saved on the top of the stack, as set
16142 by @code{AC_LANG_SAVE}, remove it from the stack, and call
16143 @code{AC_LANG(@var{language})}.
16144 @end defmac
16146 @defmac AC_LANG_SAVE
16147 @acindex{LANG_SAVE}
16148 Remember the current language (as set by @code{AC_LANG}) on a stack.
16149 The current language does not change.  @code{AC_LANG_PUSH} is preferred.
16150 @end defmac
16152 @defmac AC_LINK_FILES (@var{source}@dots{}, @var{dest}@dots{})
16153 @acindex{LINK_FILES}
16154 This is an obsolete version of @code{AC_CONFIG_LINKS}.  An updated
16155 version of:
16157 @example
16158 AC_LINK_FILES(config/$machine.h config/$obj_format.h,
16159               host.h            object.h)
16160 @end example
16162 @noindent
16165 @example
16166 AC_CONFIG_LINKS([host.h:config/$machine.h
16167                 object.h:config/$obj_format.h])
16168 @end example
16169 @end defmac
16171 @defmac AC_LN_S
16172 @acindex{LN_S}
16173 @code{AC_PROG_LN_S}
16174 @end defmac
16176 @defmac AC_LONG_64_BITS
16177 @acindex{LONG_64_BITS}
16178 @cvindex LONG_64_BITS
16179 Define @code{LONG_64_BITS} if the C type @code{long int} is 64 bits wide.
16180 Use the generic macro @samp{AC_CHECK_SIZEOF([long int])} instead.
16181 @end defmac
16183 @defmac AC_LONG_DOUBLE
16184 @acindex{LONG_DOUBLE}
16185 If the C compiler supports a working @code{long double} type with more
16186 range or precision than the @code{double} type, define
16187 @code{HAVE_LONG_DOUBLE}.
16189 You should use @code{AC_TYPE_LONG_DOUBLE} or
16190 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
16191 @end defmac
16193 @defmac AC_LONG_FILE_NAMES
16194 @acindex{LONG_FILE_NAMES}
16195 @code{AC_SYS_LONG_FILE_NAMES}
16196 @end defmac
16198 @defmac AC_MAJOR_HEADER
16199 @acindex{MAJOR_HEADER}
16200 @code{AC_HEADER_MAJOR}
16201 @end defmac
16203 @defmac AC_MEMORY_H
16204 @acindex{MEMORY_H}
16205 @cvindex NEED_MEMORY_H
16206 Used to define @code{NEED_MEMORY_H} if the @code{mem} functions were
16207 defined in @file{memory.h}.  Today it is equivalent to
16208 @samp{AC_CHECK_HEADERS([memory.h])}.  Adjust your code to depend upon
16209 @code{HAVE_MEMORY_H}, not @code{NEED_MEMORY_H}; see @ref{Standard
16210 Symbols}.
16211 @end defmac
16213 @defmac AC_MINGW32
16214 @acindex{MINGW32}
16215 Similar to @code{AC_CYGWIN} but checks for the MinGW compiler
16216 environment and sets @code{MINGW32}.
16217 @end defmac
16219 @defmac AC_MINUS_C_MINUS_O
16220 @acindex{MINUS_C_MINUS_O}
16221 @code{AC_PROG_CC_C_O}
16222 @end defmac
16224 @defmac AC_MMAP
16225 @acindex{MMAP}
16226 @code{AC_FUNC_MMAP}
16227 @end defmac
16229 @defmac AC_MODE_T
16230 @acindex{MODE_T}
16231 @code{AC_TYPE_MODE_T}
16232 @end defmac
16234 @defmac AC_OBJEXT
16235 @acindex{OBJEXT}
16236 @ovindex OBJEXT
16237 Defined the output variable @code{OBJEXT} based on the output of the
16238 compiler, after .c files have been excluded.  Typically set to @samp{o}
16239 if Posix, @samp{obj} if a @acronym{DOS} variant.
16240 Now the compiler checking macros handle
16241 this automatically.
16242 @end defmac
16244 @defmac AC_OBSOLETE (@var{this-macro-name}, @ovar{suggestion})
16245 @acindex{OBSOLETE}
16246 Make M4 print a message to the standard error output warning that
16247 @var{this-macro-name} is obsolete, and giving the file and line number
16248 where it was called.  @var{this-macro-name} should be the name of the
16249 macro that is calling @code{AC_OBSOLETE}.  If @var{suggestion} is given,
16250 it is printed at the end of the warning message; for example, it can be
16251 a suggestion for what to use instead of @var{this-macro-name}.
16253 For instance
16255 @example
16256 AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl
16257 @end example
16259 You are encouraged to use @code{AU_DEFUN} instead, since it gives better
16260 services to the user.
16261 @end defmac
16263 @defmac AC_OFF_T
16264 @acindex{OFF_T}
16265 @code{AC_TYPE_OFF_T}
16266 @end defmac
16268 @defmac AC_OUTPUT (@ovar{file}@dots{}, @ovar{extra-cmds}, @ovar{init-cmds})
16269 @acindex{OUTPUT}
16270 The use of @code{AC_OUTPUT} with argument is deprecated.  This obsoleted
16271 interface is equivalent to:
16273 @example
16274 @group
16275 AC_CONFIG_FILES(@var{file}@dots{})
16276 AC_CONFIG_COMMANDS([default],
16277                    @var{extra-cmds}, @var{init-cmds})
16278 AC_OUTPUT
16279 @end group
16280 @end example
16281 @end defmac
16283 @defmac AC_OUTPUT_COMMANDS (@var{extra-cmds}, @ovar{init-cmds})
16284 @acindex{OUTPUT_COMMANDS}
16285 Specify additional shell commands to run at the end of
16286 @file{config.status}, and shell commands to initialize any variables
16287 from @command{configure}.  This macro may be called multiple times.  It is
16288 obsolete, replaced by @code{AC_CONFIG_COMMANDS}.
16290 Here is an unrealistic example:
16292 @example
16293 fubar=27
16294 AC_OUTPUT_COMMANDS([echo this is extra $fubar, and so on.],
16295                    [fubar=$fubar])
16296 AC_OUTPUT_COMMANDS([echo this is another, extra, bit],
16297                    [echo init bit])
16298 @end example
16300 Aside from the fact that @code{AC_CONFIG_COMMANDS} requires an
16301 additional key, an important difference is that
16302 @code{AC_OUTPUT_COMMANDS} is quoting its arguments twice, unlike
16303 @code{AC_CONFIG_COMMANDS}.  This means that @code{AC_CONFIG_COMMANDS}
16304 can safely be given macro calls as arguments:
16306 @example
16307 AC_CONFIG_COMMANDS(foo, [my_FOO()])
16308 @end example
16310 @noindent
16311 Conversely, where one level of quoting was enough for literal strings
16312 with @code{AC_OUTPUT_COMMANDS}, you need two with
16313 @code{AC_CONFIG_COMMANDS}.  The following lines are equivalent:
16315 @example
16316 @group
16317 AC_OUTPUT_COMMANDS([echo "Square brackets: []"])
16318 AC_CONFIG_COMMANDS([default], [[echo "Square brackets: []"]])
16319 @end group
16320 @end example
16321 @end defmac
16323 @defmac AC_PID_T
16324 @acindex{PID_T}
16325 @code{AC_TYPE_PID_T}
16326 @end defmac
16328 @defmac AC_PREFIX
16329 @acindex{PREFIX}
16330 @code{AC_PREFIX_PROGRAM}
16331 @end defmac
16333 @defmac AC_PROGRAMS_CHECK
16334 @acindex{PROGRAMS_CHECK}
16335 @code{AC_CHECK_PROGS}
16336 @end defmac
16338 @defmac AC_PROGRAMS_PATH
16339 @acindex{PROGRAMS_PATH}
16340 @code{AC_PATH_PROGS}
16341 @end defmac
16343 @defmac AC_PROGRAM_CHECK
16344 @acindex{PROGRAM_CHECK}
16345 @code{AC_CHECK_PROG}
16346 @end defmac
16348 @defmac AC_PROGRAM_EGREP
16349 @acindex{PROGRAM_EGREP}
16350 @code{AC_EGREP_CPP}
16351 @end defmac
16353 @defmac AC_PROGRAM_PATH
16354 @acindex{PROGRAM_PATH}
16355 @code{AC_PATH_PROG}
16356 @end defmac
16358 @defmac AC_REMOTE_TAPE
16359 @acindex{REMOTE_TAPE}
16360 removed because of limited usefulness
16361 @end defmac
16363 @defmac AC_RESTARTABLE_SYSCALLS
16364 @acindex{RESTARTABLE_SYSCALLS}
16365 @code{AC_SYS_RESTARTABLE_SYSCALLS}
16366 @end defmac
16368 @defmac AC_RETSIGTYPE
16369 @acindex{RETSIGTYPE}
16370 @code{AC_TYPE_SIGNAL}
16371 @end defmac
16373 @defmac AC_RSH
16374 @acindex{RSH}
16375 removed because of limited usefulness
16376 @end defmac
16378 @defmac AC_SCO_INTL
16379 @acindex{SCO_INTL}
16380 @ovindex LIBS
16381 If on SCO Unix, add @option{-lintl} to output variable @code{LIBS}.  This
16382 macro used to do this:
16384 @example
16385 AC_CHECK_LIB([intl], [strftime], [LIBS="-lintl $LIBS"])
16386 @end example
16388 @noindent
16389 Now it just calls @code{AC_FUNC_STRFTIME} instead.
16390 @end defmac
16392 @defmac AC_SETVBUF_REVERSED
16393 @acindex{SETVBUF_REVERSED}
16394 @code{AC_FUNC_SETVBUF_REVERSED}
16395 @end defmac
16397 @defmac AC_SET_MAKE
16398 @acindex{SET_MAKE}
16399 @code{AC_PROG_MAKE_SET}
16400 @end defmac
16402 @defmac AC_SIZEOF_TYPE
16403 @acindex{SIZEOF_TYPE}
16404 @code{AC_CHECK_SIZEOF}
16405 @end defmac
16407 @defmac AC_SIZE_T
16408 @acindex{SIZE_T}
16409 @code{AC_TYPE_SIZE_T}
16410 @end defmac
16412 @defmac AC_STAT_MACROS_BROKEN
16413 @acindex{STAT_MACROS_BROKEN}
16414 @code{AC_HEADER_STAT}
16415 @end defmac
16417 @defmac AC_STDC_HEADERS
16418 @acindex{STDC_HEADERS}
16419 @code{AC_HEADER_STDC}
16420 @end defmac
16422 @defmac AC_STRCOLL
16423 @acindex{STRCOLL}
16424 @code{AC_FUNC_STRCOLL}
16425 @end defmac
16427 @defmac AC_ST_BLKSIZE
16428 @acindex{ST_BLKSIZE}
16429 @code{AC_CHECK_MEMBERS}
16430 @end defmac
16432 @defmac AC_ST_BLOCKS
16433 @acindex{ST_BLOCKS}
16434 @code{AC_STRUCT_ST_BLOCKS}
16435 @end defmac
16437 @defmac AC_ST_RDEV
16438 @acindex{ST_RDEV}
16439 @code{AC_CHECK_MEMBERS}
16440 @end defmac
16442 @defmac AC_SYS_RESTARTABLE_SYSCALLS
16443 @acindex{SYS_RESTARTABLE_SYSCALLS}
16444 @cvindex HAVE_RESTARTABLE_SYSCALLS
16445 If the system automatically restarts a system call that is interrupted
16446 by a signal, define @code{HAVE_RESTARTABLE_SYSCALLS}.  This macro does
16447 not check whether system calls are restarted in general---it checks whether a
16448 signal handler installed with @code{signal} (but not @code{sigaction})
16449 causes system calls to be restarted.  It does not check whether system calls
16450 can be restarted when interrupted by signals that have no handler.
16452 These days portable programs should use @code{sigaction} with
16453 @code{SA_RESTART} if they want restartable system calls.  They should
16454 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
16455 system call is restartable is a dynamic issue, not a configuration-time
16456 issue.
16457 @end defmac
16459 @defmac AC_SYS_SIGLIST_DECLARED
16460 @acindex{SYS_SIGLIST_DECLARED}
16461 @code{AC_DECL_SYS_SIGLIST}
16462 @end defmac
16464 @defmac AC_TEST_CPP
16465 @acindex{TEST_CPP}
16466 @code{AC_TRY_CPP}, replaced by @code{AC_PREPROC_IFELSE}.
16467 @end defmac
16469 @defmac AC_TEST_PROGRAM
16470 @acindex{TEST_PROGRAM}
16471 @code{AC_TRY_RUN}, replaced by @code{AC_RUN_IFELSE}.
16472 @end defmac
16474 @defmac AC_TIMEZONE
16475 @acindex{TIMEZONE}
16476 @code{AC_STRUCT_TIMEZONE}
16477 @end defmac
16479 @defmac AC_TIME_WITH_SYS_TIME
16480 @acindex{TIME_WITH_SYS_TIME}
16481 @code{AC_HEADER_TIME}
16482 @end defmac
16484 @defmac AC_TRY_COMPILE (@var{includes}, @var{function-body}, @ovar{action-if-true}, @ovar{action-if-false})
16485 @acindex{TRY_COMPILE}
16486 Same as:
16488 @example
16489 AC_COMPILE_IFELSE(
16490   [AC_LANG_PROGRAM([[@var{includes}]],
16491      [[@var{function-body}]])],
16492   [@var{action-if-true}],
16493   [@var{action-if-false}])
16494 @end example
16496 @noindent
16497 @xref{Running the Compiler}.
16499 This macro double quotes both @var{includes} and @var{function-body}.
16501 For C and C++, @var{includes} is any @code{#include} statements needed
16502 by the code in @var{function-body} (@var{includes} will be ignored if
16503 the currently selected language is Fortran or Fortran 77).  The compiler
16504 and compilation flags are determined by the current language
16505 (@pxref{Language Choice}).
16506 @end defmac
16508 @defmac AC_TRY_CPP (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
16509 @acindex{TRY_CPP}
16510 Same as:
16512 @example
16513 AC_PREPROC_IFELSE(
16514   [AC_LANG_SOURCE([[@var{input}]])],
16515   [@var{action-if-true}],
16516   [@var{action-if-false}])
16517 @end example
16519 @noindent
16520 @xref{Running the Preprocessor}.
16522 This macro double quotes the @var{input}.
16523 @end defmac
16525 @defmac AC_TRY_LINK (@var{includes}, @var{function-body}, @ovar{action-if-true}, @ovar{action-if-false})
16526 @acindex{TRY_LINK}
16527 Same as:
16529 @example
16530 AC_LINK_IFELSE(
16531   [AC_LANG_PROGRAM([[@var{includes}]],
16532      [[@var{function-body}]])],
16533   [@var{action-if-true}],
16534   [@var{action-if-false}])
16535 @end example
16537 @noindent
16538 @xref{Running the Compiler}.
16540 This macro double quotes both @var{includes} and @var{function-body}.
16542 Depending on the current language (@pxref{Language Choice}), create a
16543 test program to see whether a function whose body consists of
16544 @var{function-body} can be compiled and linked.  If the file compiles
16545 and links successfully, run shell commands @var{action-if-found},
16546 otherwise run @var{action-if-not-found}.
16548 This macro double quotes both @var{includes} and @var{function-body}.
16550 For C and C++, @var{includes} is any @code{#include} statements needed
16551 by the code in @var{function-body} (@var{includes} will be ignored if
16552 the currently selected language is Fortran or Fortran 77).  The compiler
16553 and compilation flags are determined by the current language
16554 (@pxref{Language Choice}), and in addition @code{LDFLAGS} and
16555 @code{LIBS} are used for linking.
16556 @end defmac
16558 @defmac AC_TRY_LINK_FUNC (@var{function}, @ovar{action-if-found}, @ovar{action-if-not-found})
16559 @acindex{TRY_LINK_FUNC}
16560 This macro is equivalent to
16561 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])],
16562 [@var{action-if-found}], [@var{action-if-not-found}])}.
16563 @end defmac
16565 @defmac AC_TRY_RUN (@var{program}, @ovar{action-if-true}, @ovar{action-if-false}, @ovar{action-if-cross-compiling})
16566 @acindex{TRY_RUN}
16567 Same as:
16569 @example
16570 AC_RUN_IFELSE(
16571   [AC_LANG_SOURCE([[@var{program}]])],
16572   [@var{action-if-true}],
16573   [@var{action-if-false}],
16574   [@var{action-if-cross-compiling}])
16575 @end example
16577 @noindent
16578 @xref{Runtime}.
16579 @end defmac
16581 @defmac AC_UID_T
16582 @acindex{UID_T}
16583 @code{AC_TYPE_UID_T}
16584 @end defmac
16586 @defmac AC_UNISTD_H
16587 @acindex{UNISTD_H}
16588 Same as @samp{AC_CHECK_HEADERS([unistd.h])}.
16589 @end defmac
16591 @defmac AC_USG
16592 @acindex{USG}
16593 @cvindex USG
16594 Define @code{USG} if the @acronym{BSD} string functions are defined in
16595 @file{strings.h}.  You should no longer depend upon @code{USG}, but on
16596 @code{HAVE_STRING_H}; see @ref{Standard Symbols}.
16597 @end defmac
16599 @defmac AC_UTIME_NULL
16600 @acindex{UTIME_NULL}
16601 @code{AC_FUNC_UTIME_NULL}
16602 @end defmac
16604 @defmac AC_VALIDATE_CACHED_SYSTEM_TUPLE (@ovar{cmd})
16605 @acindex{VALIDATE_CACHED_SYSTEM_TUPLE}
16606 If the cache file is inconsistent with the current host, target and
16607 build system types, it used to execute @var{cmd} or print a default
16608 error message.  This is now handled by default.
16609 @end defmac
16611 @defmac AC_VERBOSE (@var{result-description})
16612 @acindex{VERBOSE}
16613 @code{AC_MSG_RESULT}.
16614 @end defmac
16616 @defmac AC_VFORK
16617 @acindex{VFORK}
16618 @code{AC_FUNC_VFORK}
16619 @end defmac
16621 @defmac AC_VPRINTF
16622 @acindex{VPRINTF}
16623 @code{AC_FUNC_VPRINTF}
16624 @end defmac
16626 @defmac AC_WAIT3
16627 @acindex{WAIT3}
16628 @code{AC_FUNC_WAIT3}
16629 @end defmac
16631 @defmac AC_WARN
16632 @acindex{WARN}
16633 @code{AC_MSG_WARN}
16634 @end defmac
16636 @defmac AC_WORDS_BIGENDIAN
16637 @acindex{WORDS_BIGENDIAN}
16638 @code{AC_C_BIGENDIAN}
16639 @end defmac
16641 @defmac AC_XENIX_DIR
16642 @acindex{XENIX_DIR}
16643 @ovindex LIBS
16644 This macro used to add @option{-lx} to output variable @code{LIBS} if on
16645 Xenix.  Also, if @file{dirent.h} is being checked for, added
16646 @option{-ldir} to @code{LIBS}.  Now it is merely an alias of
16647 @code{AC_HEADER_DIRENT} instead, plus some code to detect whether
16648 running @sc{xenix} on which you should not depend:
16650 @example
16651 AC_MSG_CHECKING([for Xenix])
16652 AC_EGREP_CPP([yes],
16653 [#if defined M_XENIX && !defined M_UNIX
16654   yes
16655 #endif],
16656              [AC_MSG_RESULT([yes]); XENIX=yes],
16657              [AC_MSG_RESULT([no]); XENIX=])
16658 @end example
16659 @end defmac
16661 @defmac AC_YYTEXT_POINTER
16662 @acindex{YYTEXT_POINTER}
16663 @code{AC_DECL_YYTEXT}
16664 @end defmac
16666 @node Autoconf 1
16667 @section Upgrading From Version 1
16668 @cindex Upgrading autoconf
16669 @cindex Autoconf upgrading
16671 Autoconf version 2 is mostly backward compatible with version 1.
16672 However, it introduces better ways to do some things, and doesn't
16673 support some of the ugly things in version 1.  So, depending on how
16674 sophisticated your @file{configure.ac} files are, you might have to do
16675 some manual work in order to upgrade to version 2.  This chapter points
16676 out some problems to watch for when upgrading.  Also, perhaps your
16677 @command{configure} scripts could benefit from some of the new features in
16678 version 2; the changes are summarized in the file @file{NEWS} in the
16679 Autoconf distribution.
16681 @menu
16682 * Changed File Names::          Files you might rename
16683 * Changed Makefiles::           New things to put in @file{Makefile.in}
16684 * Changed Macros::              Macro calls you might replace
16685 * Changed Results::             Changes in how to check test results
16686 * Changed Macro Writing::       Better ways to write your own macros
16687 @end menu
16689 @node Changed File Names
16690 @subsection Changed File Names
16692 If you have an @file{aclocal.m4} installed with Autoconf (as opposed to
16693 in a particular package's source directory), you must rename it to
16694 @file{acsite.m4}.  @xref{autoconf Invocation}.
16696 If you distribute @file{install.sh} with your package, rename it to
16697 @file{install-sh} so @code{make} builtin rules won't inadvertently
16698 create a file called @file{install} from it.  @code{AC_PROG_INSTALL}
16699 looks for the script under both names, but it is best to use the new name.
16701 If you were using @file{config.h.top}, @file{config.h.bot}, or
16702 @file{acconfig.h}, you still can, but you will have less clutter if you
16703 use the @code{AH_} macros.  @xref{Autoheader Macros}.
16705 @node Changed Makefiles
16706 @subsection Changed Makefiles
16708 Add @samp{@@CFLAGS@@}, @samp{@@CPPFLAGS@@}, and @samp{@@LDFLAGS@@} in
16709 your @file{Makefile.in} files, so they can take advantage of the values
16710 of those variables in the environment when @command{configure} is run.
16711 Doing this isn't necessary, but it's a convenience for users.
16713 Also add @samp{@@configure_input@@} in a comment to each input file for
16714 @code{AC_OUTPUT}, so that the output files will contain a comment saying
16715 they were produced by @command{configure}.  Automatically selecting the
16716 right comment syntax for all the kinds of files that people call
16717 @code{AC_OUTPUT} on became too much work.
16719 Add @file{config.log} and @file{config.cache} to the list of files you
16720 remove in @code{distclean} targets.
16722 If you have the following in @file{Makefile.in}:
16724 @example
16725 prefix = /usr/local
16726 exec_prefix = $(prefix)
16727 @end example
16729 @noindent
16730 you must change it to:
16732 @example
16733 prefix = @@prefix@@
16734 exec_prefix = @@exec_prefix@@
16735 @end example
16737 @noindent
16738 The old behavior of replacing those variables without @samp{@@}
16739 characters around them has been removed.
16741 @node Changed Macros
16742 @subsection Changed Macros
16744 Many of the macros were renamed in Autoconf version 2.  You can still
16745 use the old names, but the new ones are clearer, and it's easier to find
16746 the documentation for them.  @xref{Obsolete Macros}, for a table showing the
16747 new names for the old macros.  Use the @command{autoupdate} program to
16748 convert your @file{configure.ac} to using the new macro names.
16749 @xref{autoupdate Invocation}.
16751 Some macros have been superseded by similar ones that do the job better,
16752 but are not call-compatible.  If you get warnings about calling obsolete
16753 macros while running @command{autoconf}, you may safely ignore them, but
16754 your @command{configure} script will generally work better if you follow
16755 the advice that is printed about what to replace the obsolete macros with.  In
16756 particular, the mechanism for reporting the results of tests has
16757 changed.  If you were using @command{echo} or @code{AC_VERBOSE} (perhaps
16758 via @code{AC_COMPILE_CHECK}), your @command{configure} script's output will
16759 look better if you switch to @code{AC_MSG_CHECKING} and
16760 @code{AC_MSG_RESULT}.  @xref{Printing Messages}.  Those macros work best
16761 in conjunction with cache variables.  @xref{Caching Results}.
16765 @node Changed Results
16766 @subsection Changed Results
16768 If you were checking the results of previous tests by examining the
16769 shell variable @code{DEFS}, you need to switch to checking the values of
16770 the cache variables for those tests.  @code{DEFS} no longer exists while
16771 @command{configure} is running; it is only created when generating output
16772 files.  This difference from version 1 is because properly quoting the
16773 contents of that variable turned out to be too cumbersome and
16774 inefficient to do every time @code{AC_DEFINE} is called.  @xref{Cache
16775 Variable Names}.
16777 For example, here is a @file{configure.ac} fragment written for Autoconf
16778 version 1:
16780 @example
16781 AC_HAVE_FUNCS(syslog)
16782 case "$DEFS" in
16783 *-DHAVE_SYSLOG*) ;;
16784 *) # syslog is not in the default libraries.  See if it's in some other.
16785   saved_LIBS="$LIBS"
16786   for lib in bsd socket inet; do
16787     AC_CHECKING(for syslog in -l$lib)
16788     LIBS="-l$lib $saved_LIBS"
16789     AC_HAVE_FUNCS(syslog)
16790     case "$DEFS" in
16791     *-DHAVE_SYSLOG*) break ;;
16792     *) ;;
16793     esac
16794     LIBS="$saved_LIBS"
16795   done ;;
16796 esac
16797 @end example
16799 Here is a way to write it for version 2:
16801 @example
16802 AC_CHECK_FUNCS([syslog])
16803 if test $ac_cv_func_syslog = no; then
16804   # syslog is not in the default libraries.  See if it's in some other.
16805   for lib in bsd socket inet; do
16806     AC_CHECK_LIB([$lib], [syslog], [AC_DEFINE([HAVE_SYSLOG])
16807       LIBS="-l$lib $LIBS"; break])
16808   done
16810 @end example
16812 If you were working around bugs in @code{AC_DEFINE_UNQUOTED} by adding
16813 backslashes before quotes, you need to remove them.  It now works
16814 predictably, and does not treat quotes (except back quotes) specially.
16815 @xref{Setting Output Variables}.
16817 All of the Boolean shell variables set by Autoconf macros now use
16818 @samp{yes} for the true value.  Most of them use @samp{no} for false,
16819 though for backward compatibility some use the empty string instead.  If
16820 you were relying on a shell variable being set to something like 1 or
16821 @samp{t} for true, you need to change your tests.
16823 @node Changed Macro Writing
16824 @subsection Changed Macro Writing
16826 When defining your own macros, you should now use @code{AC_DEFUN}
16827 instead of @code{define}.  @code{AC_DEFUN} automatically calls
16828 @code{AC_PROVIDE} and ensures that macros called via @code{AC_REQUIRE}
16829 do not interrupt other macros, to prevent nested @samp{checking@dots{}}
16830 messages on the screen.  There's no actual harm in continuing to use the
16831 older way, but it's less convenient and attractive.  @xref{Macro
16832 Definitions}.
16834 You probably looked at the macros that came with Autoconf as a guide for
16835 how to do things.  It would be a good idea to take a look at the new
16836 versions of them, as the style is somewhat improved and they take
16837 advantage of some new features.
16839 If you were doing tricky things with undocumented Autoconf internals
16840 (macros, variables, diversions), check whether you need to change
16841 anything to account for changes that have been made.  Perhaps you can
16842 even use an officially supported technique in version 2 instead of
16843 kludging.  Or perhaps not.
16845 To speed up your locally written feature tests, add caching to them.
16846 See whether any of your tests are of general enough usefulness to
16847 encapsulate them into macros that you can share.
16850 @node Autoconf 2.13
16851 @section Upgrading From Version 2.13
16852 @cindex Upgrading autoconf
16853 @cindex Autoconf upgrading
16855 The introduction of the previous section (@pxref{Autoconf 1}) perfectly
16856 suits this section@enddots{}
16858 @quotation
16859 Autoconf version 2.50 is mostly backward compatible with version 2.13.
16860 However, it introduces better ways to do some things, and doesn't
16861 support some of the ugly things in version 2.13.  So, depending on how
16862 sophisticated your @file{configure.ac} files are, you might have to do
16863 some manual work in order to upgrade to version 2.50.  This chapter
16864 points out some problems to watch for when upgrading.  Also, perhaps
16865 your @command{configure} scripts could benefit from some of the new
16866 features in version 2.50; the changes are summarized in the file
16867 @file{NEWS} in the Autoconf distribution.
16868 @end quotation
16870 @menu
16871 * Changed Quotation::           Broken code which used to work
16872 * New Macros::                  Interaction with foreign macros
16873 * Hosts and Cross-Compilation::  Bugward compatibility kludges
16874 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
16875 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
16876 @end menu
16878 @node Changed Quotation
16879 @subsection Changed Quotation
16881 The most important changes are invisible to you: the implementation of
16882 most macros have completely changed.  This allowed more factorization of
16883 the code, better error messages, a higher uniformity of the user's
16884 interface etc.  Unfortunately, as a side effect, some construct which
16885 used to (miraculously) work might break starting with Autoconf 2.50.
16886 The most common culprit is bad quotation.
16888 For instance, in the following example, the message is not properly
16889 quoted:
16891 @example
16892 AC_INIT
16893 AC_CHECK_HEADERS(foo.h, ,
16894   AC_MSG_ERROR(cannot find foo.h, bailing out))
16895 AC_OUTPUT
16896 @end example
16898 @noindent
16899 Autoconf 2.13 simply ignores it:
16901 @example
16902 $ @kbd{autoconf-2.13; ./configure --silent}
16903 creating cache ./config.cache
16904 configure: error: cannot find foo.h
16906 @end example
16908 @noindent
16909 while Autoconf 2.50 will produce a broken @file{configure}:
16911 @example
16912 $ @kbd{autoconf-2.50; ./configure --silent}
16913 configure: error: cannot find foo.h
16914 ./configure: exit: bad non-numeric arg `bailing'
16915 ./configure: exit: bad non-numeric arg `bailing'
16917 @end example
16919 The message needs to be quoted, and the @code{AC_MSG_ERROR} invocation
16920 too!
16922 @example
16923 AC_INIT([Example], [1.0], [bug-example@@example.org])
16924 AC_CHECK_HEADERS([foo.h], [],
16925   [AC_MSG_ERROR([cannot find foo.h, bailing out])])
16926 AC_OUTPUT
16927 @end example
16929 Many many (and many more) Autoconf macros were lacking proper quotation,
16930 including no less than@dots{} @code{AC_DEFUN} itself!
16932 @example
16933 $ @kbd{cat configure.in}
16934 AC_DEFUN([AC_PROG_INSTALL],
16935 [# My own much better version
16937 AC_INIT
16938 AC_PROG_INSTALL
16939 AC_OUTPUT
16940 $ @kbd{autoconf-2.13}
16941 autoconf: Undefined macros:
16942 ***BUG in Autoconf--please report*** AC_FD_MSG
16943 ***BUG in Autoconf--please report*** AC_EPI
16944 configure.in:1:AC_DEFUN([AC_PROG_INSTALL],
16945 configure.in:5:AC_PROG_INSTALL
16946 $ @kbd{autoconf-2.50}
16948 @end example
16951 @node New Macros
16952 @subsection New Macros
16954 @cindex undefined macro
16955 @cindex @code{_m4_divert_diversion}
16957 While Autoconf was relatively dormant in the late 1990s, Automake
16958 provided Autoconf-like macros for a while.  Starting with Autoconf 2.50
16959 in 2001, Autoconf provided
16960 versions of these macros, integrated in the @code{AC_} namespace,
16961 instead of @code{AM_}.  But in order to ease the upgrading via
16962 @command{autoupdate}, bindings to such @code{AM_} macros are provided.
16964 Unfortunately older versions of Automake (e.g., Automake 1.4)
16965 did not quote the names of these macros.
16966 Therefore, when @command{m4} finds something like
16967 @samp{AC_DEFUN(AM_TYPE_PTRDIFF_T, @dots{})} in @file{aclocal.m4},
16968 @code{AM_TYPE_PTRDIFF_T} is
16969 expanded, replaced with its Autoconf definition.
16971 Fortunately Autoconf catches pre-@code{AC_INIT} expansions, and will
16972 complain, in its own words:
16974 @example
16975 $ @kbd{cat configure.ac}
16976 AC_INIT([Example], [1.0], [bug-example@@example.org])
16977 AM_TYPE_PTRDIFF_T
16978 $ @kbd{aclocal-1.4}
16979 $ @kbd{autoconf}
16980 aclocal.m4:17: error: m4_defn: undefined macro: _m4_divert_diversion
16981 aclocal.m4:17: the top level
16982 autom4te: m4 failed with exit status: 1
16984 @end example
16986 Modern versions of Automake no longer define most of these
16987 macros, and will properly quote the names of the remaining macros.
16988 If you must use an old Automake, do not depend upon macros from Automake
16989 as it is simply not its job
16990 to provide macros (but the one it requires itself):
16992 @example
16993 $ @kbd{cat configure.ac}
16994 AC_INIT([Example], [1.0], [bug-example@@example.org])
16995 AM_TYPE_PTRDIFF_T
16996 $ @kbd{rm aclocal.m4}
16997 $ @kbd{autoupdate}
16998 autoupdate: `configure.ac' is updated
16999 $ @kbd{cat configure.ac}
17000 AC_INIT([Example], [1.0], [bug-example@@example.org])
17001 AC_CHECK_TYPES([ptrdiff_t])
17002 $ @kbd{aclocal-1.4}
17003 $ @kbd{autoconf}
17005 @end example
17008 @node Hosts and Cross-Compilation
17009 @subsection Hosts and Cross-Compilation
17010 @cindex Cross compilation
17012 Based on the experience of compiler writers, and after long public
17013 debates, many aspects of the cross-compilation chain have changed:
17015 @itemize @minus
17016 @item
17017 the relationship between the build, host, and target architecture types,
17019 @item
17020 the command line interface for specifying them to @command{configure},
17022 @item
17023 the variables defined in @command{configure},
17025 @item
17026 the enabling of cross-compilation mode.
17027 @end itemize
17029 @sp 1
17031 The relationship between build, host, and target have been cleaned up:
17032 the chain of default is now simply: target defaults to host, host to
17033 build, and build to the result of @command{config.guess}.  Nevertheless,
17034 in order to ease the transition from 2.13 to 2.50, the following
17035 transition scheme is implemented.  @emph{Do not rely on it}, as it will
17036 be completely disabled in a couple of releases (we cannot keep it, as it
17037 proves to cause more problems than it cures).
17039 They all default to the result of running @command{config.guess}, unless
17040 you specify either @option{--build} or @option{--host}.  In this case,
17041 the default becomes the system type you specified.  If you specify both,
17042 and they're different, @command{configure} will enter cross compilation
17043 mode, so it won't run any tests that require execution.
17045 Hint: if you mean to override the result of @command{config.guess},
17046 prefer @option{--build} over @option{--host}.  In the future,
17047 @option{--host} will not override the name of the build system type.
17048 Whenever you specify @option{--host}, be sure to specify @option{--build}
17049 too.
17051 @sp 1
17053 For backward compatibility, @command{configure} will accept a system
17054 type as an option by itself.  Such an option will override the
17055 defaults for build, host, and target system types.  The following
17056 configure statement will configure a cross toolchain that will run on
17057 Net@acronym{BSD}/alpha but generate code for @acronym{GNU} Hurd/sparc, which is
17058 also the build platform.
17060 @example
17061 ./configure --host=alpha-netbsd sparc-gnu
17062 @end example
17064 @sp 1
17066 In Autoconf 2.13 and before, the variables @code{build}, @code{host},
17067 and @code{target} had a different semantics before and after the
17068 invocation of @code{AC_CANONICAL_BUILD} etc.  Now, the argument of
17069 @option{--build} is strictly copied into @code{build_alias}, and is left
17070 empty otherwise.  After the @code{AC_CANONICAL_BUILD}, @code{build} is
17071 set to the canonicalized build type.  To ease the transition, before,
17072 its contents is the same as that of @code{build_alias}.  Do @emph{not}
17073 rely on this broken feature.
17075 For consistency with the backward compatibility scheme exposed above,
17076 when @option{--host} is specified but @option{--build} isn't, the build
17077 system will be assumed to be the same as @option{--host}, and
17078 @samp{build_alias} will be set to that value.  Eventually, this
17079 historically incorrect behavior will go away.
17081 @sp 1
17083 The former scheme to enable cross-compilation proved to cause more harm
17084 than good, in particular, it used to be triggered too easily, leaving
17085 regular end users puzzled in front of cryptic error messages.
17086 @command{configure} could even enter cross-compilation mode only
17087 because the compiler was not functional.  This is mainly because
17088 @command{configure} used to try to detect cross-compilation, instead of
17089 waiting for an explicit flag from the user.
17091 Now, @command{configure} enters cross-compilation mode if and only if
17092 @option{--host} is passed.
17094 That's the short documentation.  To ease the transition between 2.13 and
17095 its successors, a more complicated scheme is implemented.  @emph{Do not
17096 rely on the following}, as it will be removed in the near future.
17098 If you specify @option{--host}, but not @option{--build}, when
17099 @command{configure} performs the first compiler test it will try to run
17100 an executable produced by the compiler.  If the execution fails, it will
17101 enter cross-compilation mode.  This is fragile.  Moreover, by the time
17102 the compiler test is performed, it may be too late to modify the
17103 build-system type: other tests may have already been performed.
17104 Therefore, whenever you specify @option{--host}, be sure to specify
17105 @option{--build} too.
17107 @example
17108 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
17109 @end example
17111 @noindent
17112 will enter cross-compilation mode.  The former interface, which
17113 consisted in setting the compiler to a cross-compiler without informing
17114 @command{configure} is obsolete.  For instance, @command{configure} will
17115 fail if it can't run the code generated by the specified compiler if you
17116 configure as follows:
17118 @example
17119 ./configure CC=m68k-coff-gcc
17120 @end example
17123 @node AC_LIBOBJ vs LIBOBJS
17124 @subsection @code{AC_LIBOBJ} vs.@: @code{LIBOBJS}
17126 Up to Autoconf 2.13, the replacement of functions was triggered via the
17127 variable @code{LIBOBJS}.  Since Autoconf 2.50, the macro
17128 @code{AC_LIBOBJ} should be used instead (@pxref{Generic Functions}).
17129 Starting at Autoconf 2.53, the use of @code{LIBOBJS} is an error.
17131 This change is mandated by the unification of the @acronym{GNU} Build System
17132 components.  In particular, the various fragile techniques used to parse
17133 a @file{configure.ac} are all replaced with the use of traces.  As a
17134 consequence, any action must be traceable, which obsoletes critical
17135 variable assignments.  Fortunately, @code{LIBOBJS} was the only problem,
17136 and it can even be handled gracefully (read, ``without your having to
17137 change something'').
17139 There were two typical uses of @code{LIBOBJS}: asking for a replacement
17140 function, and adjusting @code{LIBOBJS} for Automake and/or Libtool.
17142 @sp 1
17144 As for function replacement, the fix is immediate: use
17145 @code{AC_LIBOBJ}.  For instance:
17147 @example
17148 LIBOBJS="$LIBOBJS fnmatch.o"
17149 LIBOBJS="$LIBOBJS malloc.$ac_objext"
17150 @end example
17152 @noindent
17153 should be replaced with:
17155 @example
17156 AC_LIBOBJ([fnmatch])
17157 AC_LIBOBJ([malloc])
17158 @end example
17160 @sp 1
17162 @ovindex LIBOBJDIR
17163 When used with Automake 1.10 or newer, a suitable value for
17164 @code{LIBOBJDIR} is set so that the @code{LIBOBJS} and @code{LTLIBOBJS}
17165 can be referenced from any @file{Makefile.am}.  Even without Automake,
17166 arranging for @code{LIBOBJDIR} to be set correctly will enable
17167 referencing @code{LIBOBJS} and @code{LTLIBOBJS} in another directory.
17168 The @code{LIBOJBDIR} feature is experimental.
17171 @node AC_FOO_IFELSE vs AC_TRY_FOO
17172 @subsection @code{AC_FOO_IFELSE} vs.@: @code{AC_TRY_FOO}
17174 Since Autoconf 2.50, internal codes uses @code{AC_PREPROC_IFELSE},
17175 @code{AC_COMPILE_IFELSE}, @code{AC_LINK_IFELSE}, and
17176 @code{AC_RUN_IFELSE} on one hand and @code{AC_LANG_SOURCES},
17177 and @code{AC_LANG_PROGRAM} on the other hand instead of the deprecated
17178 @code{AC_TRY_CPP}, @code{AC_TRY_COMPILE}, @code{AC_TRY_LINK}, and
17179 @code{AC_TRY_RUN}.  The motivations where:
17180 @itemize @minus
17181 @item
17182 a more consistent interface: @code{AC_TRY_COMPILE} etc.@: were double
17183 quoting their arguments;
17185 @item
17186 the combinatoric explosion is solved by decomposing on the one hand the
17187 generation of sources, and on the other hand executing the program;
17189 @item
17190 this scheme helps supporting more languages than plain C and C++.
17191 @end itemize
17193 In addition to the change of syntax, the philosophy has changed too:
17194 while emphasis was put on speed at the expense of accuracy, today's
17195 Autoconf promotes accuracy of the testing framework at, ahem@dots{}, the
17196 expense of speed.
17199 As a perfect example of what is @emph{not} to be done, here is how to
17200 find out whether a header file contains a particular declaration, such
17201 as a typedef, a structure, a structure member, or a function.  Use
17202 @code{AC_EGREP_HEADER} instead of running @code{grep} directly on the
17203 header file; on some systems the symbol might be defined in another
17204 header file that the file you are checking @samp{#include}s.
17206 As a (bad) example, here is how you should not check for C preprocessor
17207 symbols, either defined by header files or predefined by the C
17208 preprocessor: using @code{AC_EGREP_CPP}:
17210 @example
17211 @group
17212 AC_EGREP_CPP(yes,
17213 [#ifdef _AIX
17214   yes
17215 #endif
17216 ], is_aix=yes, is_aix=no)
17217 @end group
17218 @end example
17220 The above example, properly written would (i) use
17221 @code{AC_LANG_PROGRAM}, and (ii) run the compiler:
17223 @example
17224 @group
17225 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
17226 [[#if !defined _AIX
17227  error: This isn't AIX!
17228 #endif
17229 ]])],
17230                    [is_aix=yes],
17231                    [is_aix=no])
17232 @end group
17233 @end example
17236 @c ============================= Generating Test Suites with Autotest
17238 @node Using Autotest
17239 @chapter Generating Test Suites with Autotest
17241 @cindex Autotest
17243 @display
17244 @strong{N.B.: This section describes an experimental feature which will
17245 be part of Autoconf in a forthcoming release.  Although we believe
17246 Autotest is stabilizing, this documentation describes an interface which
17247 might change in the future: do not depend upon Autotest without
17248 subscribing to the Autoconf mailing lists.}
17249 @end display
17251 It is paradoxical that portable projects depend on nonportable tools
17252 to run their test suite.  Autoconf by itself is the paragon of this
17253 problem: although it aims at perfectly portability, up to 2.13 its
17254 test suite was using Deja@acronym{GNU}, a rich and complex testing
17255 framework, but which is far from being standard on Posix systems.
17256 Worse yet, it was likely to be missing on the most fragile platforms,
17257 the very platforms that are most likely to torture Autoconf and
17258 exhibit deficiencies.
17260 To circumvent this problem, many package maintainers have developed their
17261 own testing framework, based on simple shell scripts whose sole outputs
17262 are exit status values describing whether the test succeeded.  Most of
17263 these tests share common patterns, and this can result in lots of
17264 duplicated code and tedious maintenance.
17266 Following exactly the same reasoning that yielded to the inception of
17267 Autoconf, Autotest provides a test suite generation framework, based on
17268 M4 macros building a portable shell script.  The suite itself is
17269 equipped with automatic logging and tracing facilities which greatly
17270 diminish the interaction with bug reporters, and simple timing reports.
17272 Autoconf itself has been using Autotest for years, and we do attest that
17273 it has considerably improved the strength of the test suite and the
17274 quality of bug reports.  Other projects are known to use some generation
17275 of Autotest, such as Bison, Free Recode, Free Wdiff, @acronym{GNU} Tar, each of
17276 them with different needs, and this usage has validated Autotest as a general
17277 testing framework.
17279 Nonetheless, compared to Deja@acronym{GNU}, Autotest is inadequate for
17280 interactive tool testing, which is probably its main limitation.
17282 @menu
17283 * Using an Autotest Test Suite::  Autotest and the user
17284 * Writing testsuite.at::        Autotest macros
17285 * testsuite Invocation::        Running @command{testsuite} scripts
17286 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
17287 @end menu
17289 @node Using an Autotest Test Suite
17290 @section Using an Autotest Test Suite
17292 @menu
17293 * testsuite Scripts::           The concepts of Autotest
17294 * Autotest Logs::               Their contents
17295 @end menu
17297 @node testsuite Scripts
17298 @subsection @command{testsuite} Scripts
17300 @cindex @command{testsuite}
17302 Generating testing or validation suites using Autotest is rather easy.
17303 The whole validation suite is held in a file to be processed through
17304 @command{autom4te}, itself using @acronym{GNU} M4 under the scene, to
17305 produce a stand-alone Bourne shell script which then gets distributed.
17306 Neither @command{autom4te} nor @acronym{GNU} M4 are needed at
17307 the installer's end.
17309 @cindex test group
17310 Each test of the validation suite should be part of some test group.  A
17311 @dfn{test group} is a sequence of interwoven tests that ought to be
17312 executed together, usually because one test in the group creates data
17313 files than a later test in the same group needs to read.  Complex test
17314 groups make later debugging more tedious.  It is much better to
17315 keep only a few tests per test group.  Ideally there is only one test
17316 per test group.
17318 For all but the simplest packages, some file such as @file{testsuite.at}
17319 does not fully hold all test sources, as these are often easier to
17320 maintain in separate files.  Each of these separate files holds a single
17321 test group, or a sequence of test groups all addressing some common
17322 functionality in the package.  In such cases, @file{testsuite.at}
17323 merely initializes the validation suite, and sometimes does elementary
17324 health checking, before listing include statements for all other test
17325 files.  The special file @file{package.m4}, containing the
17326 identification of the package, is automatically included if found.
17328 A convenient alternative consists in moving all the global issues
17329 (local Autotest macros, elementary health checking, and @code{AT_INIT}
17330 invocation) into the file @code{local.at}, and making
17331 @file{testsuite.at} be a simple list of @code{m4_include} of sub test
17332 suites.  In such case, generating the whole test suite or pieces of it
17333 is only a matter of choosing the @command{autom4te} command line
17334 arguments.
17336 The validation scripts that Autotest produces are by convention called
17337 @command{testsuite}.  When run, @command{testsuite} executes each test
17338 group in turn, producing only one summary line per test to say if that
17339 particular test succeeded or failed.  At end of all tests, summarizing
17340 counters get printed.  One debugging directory is left for each test
17341 group which failed, if any: such directories are named
17342 @file{testsuite.dir/@var{nn}}, where @var{nn} is the sequence number of
17343 the test group, and they include:
17345 @itemize @bullet
17346 @item a debugging script named @file{run} which reruns the test in
17347 @dfn{debug mode} (@pxref{testsuite Invocation}).  The automatic generation
17348 of debugging scripts has the purpose of easing the chase for bugs.
17350 @item all the files created with @code{AT_DATA}
17352 @item a log of the run, named @file{testsuite.log}
17353 @end itemize
17355 In the ideal situation, none of the tests fail, and consequently no
17356 debugging directory is left behind for validation.
17358 It often happens in practice that individual tests in the validation
17359 suite need to get information coming out of the configuration process.
17360 Some of this information, common for all validation suites, is provided
17361 through the file @file{atconfig}, automatically created by
17362 @code{AC_CONFIG_TESTDIR}.  For configuration informations which your
17363 testing environment specifically needs, you might prepare an optional
17364 file named @file{atlocal.in}, instantiated by @code{AC_CONFIG_FILES}.
17365 The configuration process produces @file{atconfig} and @file{atlocal}
17366 out of these two input files, and these two produced files are
17367 automatically read by the @file{testsuite} script.
17369 Here is a diagram showing the relationship between files.
17371 @noindent
17372 Files used in preparing a software package for distribution:
17374 @example
17375                 [package.m4] -->.
17376                                  \
17377 subfile-1.at ->.  [local.at] ---->+
17378     ...         \                  \
17379 subfile-i.at ---->-- testsuite.at -->-- autom4te* -->testsuite
17380     ...         /
17381 subfile-n.at ->'
17382 @end example
17384 @noindent
17385 Files used in configuring a software package:
17387 @example
17388                                      .--> atconfig
17389                                     /
17390 [atlocal.in] -->  config.status* --<
17391                                     \
17392                                      `--> [atlocal]
17393 @end example
17395 @noindent
17396 Files created during the test suite execution:
17398 @example
17399 atconfig -->.                    .--> testsuite.log
17400              \                  /
17401               >-- testsuite* --<
17402              /                  \
17403 [atlocal] ->'                    `--> [testsuite.dir]
17404 @end example
17407 @node Autotest Logs
17408 @subsection Autotest Logs
17410 When run, the test suite creates a log file named after itself, e.g., a
17411 test suite named @command{testsuite} creates @file{testsuite.log}.  It
17412 contains a lot of information, usually more than maintainers actually
17413 need, but therefore most of the time it contains all that is needed:
17415 @table @asis
17416 @item command line arguments
17417 @c akim s/to consist in/to consist of/
17418 A very bad but unfortunately widespread Posix habit consists of
17419 setting environment variables before the command, such as in
17420 @samp{CC=my-home-grown-cc ./testsuite}.  The test suite will not
17421 know this change, hence (i) it cannot report it to you, and (ii)
17422 it cannot preserve the value of @code{CC} for subsequent runs.
17423 Autoconf faced exactly the same problem, and solved it by asking
17424 users to pass the variable definitions as command line arguments.
17425 Autotest requires this rule, too, but has no means to enforce it; the log
17426 then contains a trace of the variables that were changed by the user.
17428 @item @file{ChangeLog} excerpts
17429 The topmost lines of all the @file{ChangeLog}s found in the source
17430 hierarchy.  This is especially useful when bugs are reported against
17431 development versions of the package, since the version string does not
17432 provide sufficient information to know the exact state of the sources
17433 the user compiled.  Of course, this relies on the use of a
17434 @file{ChangeLog}.
17436 @item build machine
17437 Running a test suite in a cross-compile environment is not an easy task,
17438 since it would mean having the test suite run on a machine @var{build},
17439 while running programs on a machine @var{host}.  It is much simpler to
17440 run both the test suite and the programs on @var{host}, but then, from
17441 the point of view of the test suite, there remains a single environment,
17442 @var{host} = @var{build}.  The log contains relevant information on the
17443 state of the build machine, including some important environment
17444 variables.
17445 @c FIXME: How about having an M4sh macro to say `hey, log the value
17446 @c of `@dots{}'?  This would help both Autoconf and Autotest.
17448 @item tested programs
17449 The absolute file name and answers to @option{--version} of the tested
17450 programs (see @ref{Writing testsuite.at}, @code{AT_TESTED}).
17452 @item configuration log
17453 The contents of @file{config.log}, as created by @command{configure},
17454 are appended.  It contains the configuration flags and a detailed report
17455 on the configuration itself.
17456 @end table
17459 @node Writing testsuite.at
17460 @section Writing @file{testsuite.at}
17462 The @file{testsuite.at} is a Bourne shell script making use of special
17463 Autotest M4 macros.  It often contains a call to @code{AT_INIT} near
17464 its beginning followed by one call to @code{m4_include} per source file
17465 for tests.  Each such included file, or the remainder of
17466 @file{testsuite.at} if include files are not used, contain a sequence of
17467 test groups.  Each test group begins with a call to @code{AT_SETUP},
17468 then an arbitrary number of shell commands or calls to @code{AT_CHECK},
17469 and then completes with a call to @code{AT_CLEANUP}.
17471 @defmac AT_INIT (@ovar{name})
17472 @atindex{INIT}
17473 @c FIXME: Not clear, plus duplication of the information.
17474 Initialize Autotest.  Giving a @var{name} to the test suite is
17475 encouraged if your package includes several test suites.  In any case,
17476 the test suite always displays the package name and version.  It also
17477 inherits the package bug report address.
17478 @end defmac
17480 @defmac AT_COPYRIGHT (@var{copyright-notice})
17481 @atindex{COPYRIGHT}
17482 @cindex Copyright Notice
17483 State that, in addition to the Free Software Foundation's copyright on
17484 the Autotest macros, parts of your test suite are covered by
17485 @var{copyright-notice}.
17487 The @var{copyright-notice} will show up in both the head of
17488 @command{testsuite} and in @samp{testsuite --version}.
17489 @end defmac
17491 @defmac AT_TESTED (@var{executables})
17492 @atindex{TESTED}
17493 Log the file name and answer to @option{--version} of each program in
17494 space-separated list @var{executables}.  Several invocations register
17495 new executables, in other words, don't fear registering one program
17496 several times.
17497 @end defmac
17499 Autotest test suites rely on @env{PATH} to find the tested program.
17500 This avoids the need to generate absolute names of the various tools, and
17501 makes it possible to test installed programs.  Therefore, knowing which
17502 programs are being exercised is crucial to understanding problems in
17503 the test suite itself, or its occasional misuses.  It is a good idea to
17504 also subscribe foreign programs you depend upon, to avoid incompatible
17505 diagnostics.
17507 @sp 1
17509 @defmac AT_SETUP (@var{test-group-name})
17510 @atindex{SETUP}
17511 This macro starts a group of related tests, all to be executed in the
17512 same subshell.  It accepts a single argument, which holds a few words
17513 (no more than about 30 or 40 characters) quickly describing the purpose
17514 of the test group being started.
17515 @end defmac
17517 @defmac AT_KEYWORDS (@var{keywords})
17518 @atindex{KEYWORDS}
17519 Associate the space-separated list of @var{keywords} to the enclosing
17520 test group.  This makes it possible to run ``slices'' of the test suite.
17521 For instance, if some of your test groups exercise some @samp{foo}
17522 feature, then using @samp{AT_KEYWORDS(foo)} lets you run
17523 @samp{./testsuite -k foo} to run exclusively these test groups.  The
17524 @var{title} of the test group is automatically recorded to
17525 @code{AT_KEYWORDS}.
17527 Several invocations within a test group accumulate new keywords.  In
17528 other words, don't fear registering the same keyword several times in a
17529 test group.
17530 @end defmac
17532 @defmac AT_CAPTURE_FILE (@var{file})
17533 @atindex{CAPTURE_FILE}
17534 If the current test group fails, log the contents of @var{file}.
17535 Several identical calls within one test group have no additional effect.
17536 @end defmac
17538 @defmac AT_XFAIL_IF (@var{shell-condition})
17539 @atindex{XFAIL_IF}
17540 Determine whether the test is expected to fail because it is a known
17541 bug (for unsupported features, you should skip the test).
17542 @var{shell-condition} is a shell expression such as a @code{test}
17543 command; you can instantiate this macro many times from within the
17544 same test group, and one of the conditions will be enough to turn
17545 the test into an expected failure.
17546 @end defmac
17548 @defmac AT_CLEANUP
17549 @atindex{CLEANUP}
17550 End the current test group.
17551 @end defmac
17553 @sp 1
17555 @defmac AT_DATA (@var{file}, @var{contents})
17556 @atindex{DATA}
17557 Initialize an input data @var{file} with given @var{contents}.  Of
17558 course, the @var{contents} have to be properly quoted between square
17559 brackets to protect against included commas or spurious M4
17560 expansion.  The contents ought to end with an end of line.
17561 @end defmac
17563 @defmac AT_CHECK (@var{commands}, @dvar{status, @samp{0}}, @dvar{stdout, @samp{}}, @dvar{stderr, @samp{}}, @ovar{run-if-fail}, @ovar{run-if-pass})
17564 @atindex{CHECK}
17565 Execute a test by performing given shell @var{commands}.  These commands
17566 should normally exit with @var{status}, while producing expected
17567 @var{stdout} and @var{stderr} contents.  If @var{commands} exit with
17568 status 77, then the whole test group is skipped.  Otherwise, if this test
17569 fails, run shell commands @var{run-if-fail} or, if this test passes, run shell
17570 commands @var{run-if-pass}.
17572 The @var{commands} @emph{must not} redirect the standard output, nor the
17573 standard error.
17575 If @var{status}, or @var{stdout}, or @var{stderr} is @samp{ignore}, then
17576 the corresponding value is not checked.
17578 The special value @samp{expout} for @var{stdout} means the expected
17579 output of the @var{commands} is the content of the file @file{expout}.
17580 If @var{stdout} is @samp{stdout}, then the standard output of the
17581 @var{commands} is available for further tests in the file @file{stdout}.
17582 Similarly for @var{stderr} with @samp{expout} and @samp{stderr}.
17583 @end defmac
17586 @node testsuite Invocation
17587 @section Running @command{testsuite} Scripts
17588 @cindex @command{testsuite}
17590 Autotest test suites support the following arguments:
17592 @table @option
17593 @item --help
17594 @itemx -h
17595 Display the list of options and exit successfully.
17597 @item --version
17598 @itemx -V
17599 Display the version of the test suite and exit successfully.
17601 @item --clean
17602 @itemx -c
17603 Remove all the files the test suite might have created and exit.  Meant
17604 for @code{clean} Makefile targets.
17606 @item --list
17607 @itemx -l
17608 List all the tests (or only the selection), including their possible
17609 keywords.
17610 @end table
17612 @sp 1
17614 By default all tests are performed (or described with
17615 @option{--list}) in the default environment first silently, then
17616 verbosely, but the environment, set of tests, and verbosity level can be
17617 tuned:
17619 @table @samp
17620 @item @var{variable}=@var{value}
17621 Set the environment @var{variable} to @var{value}.  Use this rather
17622 than @samp{FOO=foo ./testsuite} as debugging scripts would then run in a
17623 different environment.
17625 @cindex @code{AUTOTEST_PATH}
17626 The variable @code{AUTOTEST_PATH} specifies the testing path to prepend
17627 to @env{PATH}.  Relative directory names (not starting with
17628 @samp{/}) are considered to be relative to the top level of the
17629 package being built.  All directories are made absolute, first
17630 starting from the top level @emph{build} tree, then from the
17631 @emph{source} tree.  For instance @samp{./testsuite
17632 AUTOTEST_PATH=tests:bin} for a @file{/src/foo-1.0} source package built
17633 in @file{/tmp/foo} results in @samp{/tmp/foo/tests:/tmp/foo/bin} and
17634 then @samp{/src/foo-1.0/tests:/src/foo-1.0/bin} being prepended to
17635 @env{PATH}.
17637 @item @var{number}
17638 @itemx @var{number}-@var{number}
17639 @itemx @var{number}-
17640 @itemx -@var{number}
17641 Add the corresponding test groups, with obvious semantics, to the
17642 selection.
17644 @item --keywords=@var{keywords}
17645 @itemx -k @var{keywords}
17646 Add to the selection the test groups with title or keywords (arguments
17647 to @code{AT_SETUP} or @code{AT_KEYWORDS}) that match @emph{all} keywords
17648 of the comma separated list @var{keywords}, case-insensitively.  Use
17649 @samp{!} immediately before the keyword to invert the selection for this
17650 keyword.  By default, the keywords match whole words; enclose them in
17651 @samp{.*} to also match parts of words.
17653 For example, running
17655 @example
17656 @kbd{./testsuite -k 'autoupdate,.*FUNC.*'}
17657 @end example
17659 @noindent
17660 will select all tests tagged @samp{autoupdate} @emph{and} with tags
17661 containing @samp{FUNC} (as in @samp{AC_CHECK_FUNC}, @samp{AC_FUNC_FNMATCH},
17662 etc.), while
17664 @example
17665 @kbd{./testsuite -k '!autoupdate' -k '.*FUNC.*'}
17666 @end example
17668 @noindent
17669 will select all tests not tagged @samp{autoupdate} @emph{or} with tags
17670 containing @samp{FUNC}.
17672 @item --errexit
17673 @itemx -e
17674 If any test fails, immediately abort testing.  It implies
17675 @option{--debug}: post test group clean up, and top-level logging
17676 are inhibited.  This option is meant for the full test
17677 suite, it is not really useful for generated debugging scripts.
17679 @item --verbose
17680 @itemx -v
17681 Force more verbosity in the detailed output of what is being done.  This
17682 is the default for debugging scripts.
17684 @item --debug
17685 @itemx -d
17686 Do not remove the files after a test group was performed ---but they are
17687 still removed @emph{before}, therefore using this option is sane when
17688 running several test groups.  Create debugging scripts.  Do not
17689 overwrite the top-level
17690 log (in order to preserve supposedly existing full log file).  This is
17691 the default for debugging scripts, but it can also be useful to debug
17692 the testsuite itself.
17694 @item --trace
17695 @itemx -x
17696 Trigger shell tracing of the test groups.
17697 @end table
17700 @node Making testsuite Scripts
17701 @section Making @command{testsuite} Scripts
17703 For putting Autotest into movement, you need some configuration and
17704 Makefile machinery.  We recommend, at least if your package uses deep or
17705 shallow hierarchies, that you use @file{tests/} as the name of the
17706 directory holding all your tests and their @file{Makefile}.  Here is a
17707 check list of things to do.
17709 @itemize @minus
17711 @item
17712 @cindex @file{package.m4}
17713 Make sure to create the file @file{package.m4}, which defines the
17714 identity of the package.  It must define @code{AT_PACKAGE_STRING}, the
17715 full signature of the package, and @code{AT_PACKAGE_BUGREPORT}, the
17716 address to which bug reports should be sent.  For sake of completeness,
17717 we suggest that you also define @code{AT_PACKAGE_NAME},
17718 @code{AT_PACKAGE_TARNAME}, and @code{AT_PACKAGE_VERSION}.
17719 @xref{Initializing configure}, for a description of these variables.  We
17720 suggest the following Makefile excerpt:
17722 @smallexample
17723 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
17724         @{                                      \
17725           echo '# Signature of the current package.'; \
17726           echo 'm4_define([AT_PACKAGE_NAME],      [@@PACKAGE_NAME@@])'; \
17727           echo 'm4_define([AT_PACKAGE_TARNAME],   [@@PACKAGE_TARNAME@@])'; \
17728           echo 'm4_define([AT_PACKAGE_VERSION],   [@@PACKAGE_VERSION@@])'; \
17729           echo 'm4_define([AT_PACKAGE_STRING],    [@@PACKAGE_STRING@@])'; \
17730           echo 'm4_define([AT_PACKAGE_BUGREPORT], [@@PACKAGE_BUGREPORT@@])'; \
17731         @} >$(srcdir)/package.m4
17732 @end smallexample
17734 @noindent
17735 Be sure to distribute @file{package.m4} and to put it into the source
17736 hierarchy: the test suite ought to be shipped!
17738 @item
17739 Invoke @code{AC_CONFIG_TESTDIR}.
17741 @defmac AC_CONFIG_TESTDIR (@var{directory}, @dvar{test-path, @var{directory}})
17742 @acindex{CONFIG_TESTDIR}
17743 An Autotest test suite is to be configured in @var{directory}.  This
17744 macro requires the instantiation of @file{@var{directory}/atconfig} from
17745 @file{@var{directory}/atconfig.in}, and sets the default
17746 @code{AUTOTEST_PATH} to @var{test-path} (@pxref{testsuite Invocation}).
17747 @end defmac
17749 @item
17750 Still within @file{configure.ac}, as appropriate, ensure that some
17751 @code{AC_CONFIG_FILES} command includes substitution for
17752 @file{tests/atlocal}.
17754 @item
17755 The @file{tests/Makefile.in} should be modified so the validation in
17756 your package is triggered by @samp{make check}.  An example is provided
17757 below.
17758 @end itemize
17760 With Automake, here is a minimal example about how to link @samp{make
17761 check} with a validation suite.
17763 @example
17764 EXTRA_DIST = testsuite.at $(TESTSUITE) atlocal.in
17765 TESTSUITE = $(srcdir)/testsuite
17767 check-local: atconfig atlocal $(TESTSUITE)
17768         $(SHELL) $(TESTSUITE) $(TESTSUITEFLAGS)
17770 installcheck-local: atconfig atlocal $(TESTSUITE)
17771         $(SHELL) $(TESTSUITE) AUTOTEST_PATH="$(bindir)" \
17772           $(TESTSUITEFLAGS)
17774 AUTOTEST = $(AUTOM4TE) --language=autotest
17775 $(TESTSUITE): $(srcdir)/testsuite.at
17776         $(AUTOTEST) -I $(srcdir) -o $@@.tmp $@@.at
17777         mv $@@.tmp $@@
17778 @end example
17780 You might want to list explicitly the dependencies, i.e., the list of
17781 the files @file{testsuite.at} includes.
17783 With strict Autoconf, you might need to add lines inspired from the
17784 following:
17786 @example
17787 subdir = tests
17789 atconfig: $(top_builddir)/config.status
17790         cd $(top_builddir) && \
17791            $(SHELL) ./config.status $(subdir)/$@@
17793 atlocal: $(srcdir)/atlocal.in $(top_builddir)/config.status
17794         cd $(top_builddir) && \
17795            $(SHELL) ./config.status $(subdir)/$@@
17796 @end example
17798 @noindent
17799 and manage to have @file{atconfig.in} and @code{$(EXTRA_DIST)}
17800 distributed.
17802 With all this in place, and if you have not initialized @samp{TESTSUITEFLAGS}
17803 within your Makefile, you can fine-tune test suite execution with this variable,
17804 for example:
17806 @example
17807 make check TESTSUITEFLAGS='-v -d -x 75 -k AC_PROG_CC CFLAGS=-g'
17808 @end example
17812 @c =============================== Frequent Autoconf Questions, with answers
17814 @node FAQ
17815 @chapter Frequent Autoconf Questions, with answers
17817 Several questions about Autoconf come up occasionally.  Here some of them
17818 are addressed.
17820 @menu
17821 * Distributing::                Distributing @command{configure} scripts
17822 * Why GNU m4::                  Why not use the standard M4?
17823 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
17824 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
17825 * Defining Directories::        Passing @code{datadir} to program
17826 * autom4te.cache::              What is it?  Can I remove it?
17827 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
17828 @end menu
17830 @node Distributing
17831 @section Distributing @command{configure} Scripts
17832 @cindex License
17834 @display
17835 What are the restrictions on distributing @command{configure}
17836 scripts that Autoconf generates?  How does that affect my
17837 programs that use them?
17838 @end display
17840 There are no restrictions on how the configuration scripts that Autoconf
17841 produces may be distributed or used.  In Autoconf version 1, they were
17842 covered by the @acronym{GNU} General Public License.  We still encourage
17843 software authors to distribute their work under terms like those of the
17844 @acronym{GPL}, but doing so is not required to use Autoconf.
17846 Of the other files that might be used with @command{configure},
17847 @file{config.h.in} is under whatever copyright you use for your
17848 @file{configure.ac}.  @file{config.sub} and @file{config.guess} have an
17849 exception to the @acronym{GPL} when they are used with an Autoconf-generated
17850 @command{configure} script, which permits you to distribute them under the
17851 same terms as the rest of your package.  @file{install-sh} is from the X
17852 Consortium and is not copyrighted.
17854 @node Why GNU m4
17855 @section Why Require @acronym{GNU} M4?
17857 @display
17858 Why does Autoconf require @acronym{GNU} M4?
17859 @end display
17861 Many M4 implementations have hard-coded limitations on the size and
17862 number of macros that Autoconf exceeds.  They also lack several
17863 builtin macros that it would be difficult to get along without in a
17864 sophisticated application like Autoconf, including:
17866 @example
17867 m4_builtin
17868 m4_indir
17869 m4_bpatsubst
17870 __file__
17871 __line__
17872 @end example
17874 Autoconf requires version 1.4.3 or later of @acronym{GNU} M4.
17876 Since only software maintainers need to use Autoconf, and since @acronym{GNU}
17877 M4 is simple to configure and install, it seems reasonable to require
17878 @acronym{GNU} M4 to be installed also.  Many maintainers of @acronym{GNU} and
17879 other free software already have most of the @acronym{GNU} utilities
17880 installed, since they prefer them.
17882 @node Bootstrapping
17883 @section How Can I Bootstrap?
17884 @cindex Bootstrap
17886 @display
17887 If Autoconf requires @acronym{GNU} M4 and @acronym{GNU} M4 has an Autoconf
17888 @command{configure} script, how do I bootstrap?  It seems like a chicken
17889 and egg problem!
17890 @end display
17892 This is a misunderstanding.  Although @acronym{GNU} M4 does come with a
17893 @command{configure} script produced by Autoconf, Autoconf is not required
17894 in order to run the script and install @acronym{GNU} M4.  Autoconf is only
17895 required if you want to change the M4 @command{configure} script, which few
17896 people have to do (mainly its maintainer).
17898 @node Why Not Imake
17899 @section Why Not Imake?
17900 @cindex Imake
17902 @display
17903 Why not use Imake instead of @command{configure} scripts?
17904 @end display
17906 Several people have written addressing this question, so I include
17907 adaptations of their explanations here.
17909 The following answer is based on one written by Richard Pixley:
17911 @quotation
17912 Autoconf generated scripts frequently work on machines that it has
17913 never been set up to handle before.  That is, it does a good job of
17914 inferring a configuration for a new system.  Imake cannot do this.
17916 Imake uses a common database of host specific data.  For X11, this makes
17917 sense because the distribution is made as a collection of tools, by one
17918 central authority who has control over the database.
17920 @acronym{GNU} tools are not released this way.  Each @acronym{GNU} tool has a
17921 maintainer; these maintainers are scattered across the world.  Using a
17922 common database would be a maintenance nightmare.  Autoconf may appear
17923 to be this kind of database, but in fact it is not.  Instead of listing
17924 host dependencies, it lists program requirements.
17926 If you view the @acronym{GNU} suite as a collection of native tools, then the
17927 problems are similar.  But the @acronym{GNU} development tools can be
17928 configured as cross tools in almost any host+target permutation.  All of
17929 these configurations can be installed concurrently.  They can even be
17930 configured to share host independent files across hosts.  Imake doesn't
17931 address these issues.
17933 Imake templates are a form of standardization.  The @acronym{GNU} coding
17934 standards address the same issues without necessarily imposing the same
17935 restrictions.
17936 @end quotation
17939 Here is some further explanation, written by Per Bothner:
17941 @quotation
17942 One of the advantages of Imake is that it easy to generate large
17943 Makefiles using @code{cpp}'s @samp{#include} and macro mechanisms.
17944 However, @code{cpp} is not programmable: it has limited conditional
17945 facilities, and no looping.  And @code{cpp} cannot inspect its
17946 environment.
17948 All of these problems are solved by using @code{sh} instead of
17949 @code{cpp}.  The shell is fully programmable, has macro substitution,
17950 can execute (or source) other shell scripts, and can inspect its
17951 environment.
17952 @end quotation
17955 Paul Eggert elaborates more:
17957 @quotation
17958 With Autoconf, installers need not assume that Imake itself is already
17959 installed and working well.  This may not seem like much of an advantage
17960 to people who are accustomed to Imake.  But on many hosts Imake is not
17961 installed or the default installation is not working well, and requiring
17962 Imake to install a package hinders the acceptance of that package on
17963 those hosts.  For example, the Imake template and configuration files
17964 might not be installed properly on a host, or the Imake build procedure
17965 might wrongly assume that all source files are in one big directory
17966 tree, or the Imake configuration might assume one compiler whereas the
17967 package or the installer needs to use another, or there might be a
17968 version mismatch between the Imake expected by the package and the Imake
17969 supported by the host.  These problems are much rarer with Autoconf,
17970 where each package comes with its own independent configuration
17971 processor.
17973 Also, Imake often suffers from unexpected interactions between
17974 @command{make} and the installer's C preprocessor.  The fundamental problem
17975 here is that the C preprocessor was designed to preprocess C programs,
17976 not @file{Makefile}s.  This is much less of a problem with Autoconf,
17977 which uses the general-purpose preprocessor M4, and where the
17978 package's author (rather than the installer) does the preprocessing in a
17979 standard way.
17980 @end quotation
17983 Finally, Mark Eichin notes:
17985 @quotation
17986 Imake isn't all that extensible, either.  In order to add new features to
17987 Imake, you need to provide your own project template, and duplicate most
17988 of the features of the existing one.  This means that for a sophisticated
17989 project, using the vendor-provided Imake templates fails to provide any
17990 leverage---since they don't cover anything that your own project needs
17991 (unless it is an X11 program).
17993 On the other side, though:
17995 The one advantage that Imake has over @command{configure}:
17996 @file{Imakefile}s tend to be much shorter (likewise, less redundant)
17997 than @file{Makefile.in}s.  There is a fix to this, however---at least
17998 for the Kerberos V5 tree, we've modified things to call in common
17999 @file{post.in} and @file{pre.in} @file{Makefile} fragments for the
18000 entire tree.  This means that a lot of common things don't have to be
18001 duplicated, even though they normally are in @command{configure} setups.
18002 @end quotation
18005 @node Defining Directories
18006 @section How Do I @code{#define} Installation Directories?
18008 @display
18009 My program needs library files, installed in @code{datadir} and
18010 similar.  If I use
18012 @example
18013 AC_DEFINE_UNQUOTED([DATADIR], [$datadir],
18014   [Define to the read-only architecture-independent
18015    data directory.])
18016 @end example
18018 @noindent
18019 I get
18021 @example
18022 #define DATADIR "$@{prefix@}/share"
18023 @end example
18024 @end display
18026 As already explained, this behavior is on purpose, mandated by the
18027 @acronym{GNU} Coding Standards, see @ref{Installation Directory
18028 Variables}.  There are several means to achieve a similar goal:
18030 @itemize @minus
18031 @item
18032 Do not use @code{AC_DEFINE} but use your @file{Makefile} to pass the
18033 actual value of @code{datadir} via compilation flags, see
18034 @ref{Installation Directory Variables}, for the details.
18036 @item
18037 This solution can be simplified when compiling a program: you may either
18038 extend the @code{CPPFLAGS}:
18040 @example
18041 CPPFLAGS = -DDATADIR=\"$(datadir)\" @@CPPFLAGS@@
18042 @end example
18044 @noindent
18045 or create a dedicated header file:
18047 @example
18048 DISTCLEANFILES = datadir.h
18049 datadir.h: Makefile
18050         echo '#define DATADIR "$(datadir)"' >$@@
18051 @end example
18053 @item
18054 Use @code{AC_DEFINE} but have @command{configure} compute the literal
18055 value of @code{datadir} and others.  Many people have wrapped macros to
18056 automate this task.  For instance, the macro @code{AC_DEFINE_DIR} from
18057 the @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
18058 Archive}.
18060 This solution does not conform to the @acronym{GNU} Coding Standards.
18062 @item
18063 Note that all the previous solutions hard wire the absolute name of
18064 these directories in the executables, which is not a good property.  You
18065 may try to compute the names relative to @code{prefix}, and try to
18066 find @code{prefix} at runtime, this way your package is relocatable.
18067 Some macros are already available to address this issue: see
18068 @code{adl_COMPUTE_RELATIVE_PATHS} and
18069 @code{adl_COMPUTE_STANDARD_RELATIVE_PATHS} on the
18070 @uref{http://autoconf-archive.cryp.to/,
18071 Autoconf Macro Archive}.
18072 @end itemize
18075 @node autom4te.cache
18076 @section What is @file{autom4te.cache}?
18078 @display
18079 What is this directory @file{autom4te.cache}?  Can I safely remove it?
18080 @end display
18082 In the @acronym{GNU} Build System, @file{configure.ac} plays a central
18083 role and is read by many tools: @command{autoconf} to create
18084 @file{configure}, @command{autoheader} to create @file{config.h.in},
18085 @command{automake} to create @file{Makefile.in}, @command{autoscan} to
18086 check the completeness of @file{configure.ac}, @command{autoreconf} to
18087 check the @acronym{GNU} Build System components that are used.  To
18088 ``read @file{configure.ac}'' actually means to compile it with M4,
18089 which can be a very long process for complex @file{configure.ac}.
18091 This is why all these tools, instead of running directly M4, invoke
18092 @command{autom4te} (@pxref{autom4te Invocation}) which, while answering to
18093 a specific demand, stores additional information in
18094 @file{autom4te.cache} for future runs.  For instance, if you run
18095 @command{autoconf}, behind the scenes, @command{autom4te} will also
18096 store information for the other tools, so that when you invoke
18097 @command{autoheader} or @command{automake} etc., re-processing
18098 @file{configure.ac} is not needed.  The speed up is frequently of 30%,
18099 and is increasing with the size of @file{configure.ac}.
18101 But it is and remains being simply a cache: you can safely remove it.
18103 @sp 1
18105 @display
18106 Can I permanently get rid of it?
18107 @end display
18109 The creation of this cache can be disabled from
18110 @file{~/.autom4te.cfg}, see @ref{Customizing autom4te}, for more
18111 details.  You should be aware that disabling the cache slows down the
18112 Autoconf test suite by 40%.  The more @acronym{GNU} Build System
18113 components are used, the more the cache is useful; for instance
18114 running @samp{autoreconf -f} on the Core Utilities is twice slower without
18115 the cache @emph{although @option{--force} implies that the cache is
18116 not fully exploited}, and eight times slower than without
18117 @option{--force}.
18120 @node Present But Cannot Be Compiled
18121 @section Header Present But Cannot Be Compiled
18123 The most important guideline to bear in mind when checking for
18124 features is to mimic as much as possible the intended use.
18125 Unfortunately, old versions of @code{AC_CHECK_HEADER} and
18126 @code{AC_CHECK_HEADERS} failed to follow this idea, and called
18127 the preprocessor, instead of the compiler, to check for headers.  As a
18128 result, incompatibilities between headers went unnoticed during
18129 configuration, and maintainers finally had to deal with this issue
18130 elsewhere.
18132 As of Autoconf 2.56 both checks are performed, and @code{configure}
18133 complains loudly if the compiler and the preprocessor do not agree.
18134 For the time being the result used is that of the preprocessor, to give
18135 maintainers time to adjust their @file{configure.ac}, but in the
18136 future, only the compiler will be considered.
18138 Consider the following example:
18140 @smallexample
18141 $ @kbd{cat number.h}
18142 typedef int number;
18143 $ @kbd{cat pi.h}
18144 const number pi = 3;
18145 $ @kbd{cat configure.ac}
18146 AC_INIT([Example], [1.0], [bug-example@@example.org])
18147 AC_CHECK_HEADERS([pi.h])
18148 $ @kbd{autoconf -Wall}
18149 $ @kbd{./configure}
18150 checking for gcc... gcc
18151 checking for C compiler default output file name... a.out
18152 checking whether the C compiler works... yes
18153 checking whether we are cross compiling... no
18154 checking for suffix of executables...
18155 checking for suffix of object files... o
18156 checking whether we are using the GNU C compiler... yes
18157 checking whether gcc accepts -g... yes
18158 checking for gcc option to accept ISO C89... none needed
18159 checking how to run the C preprocessor... gcc -E
18160 checking for grep that handles long lines and -e... grep
18161 checking for egrep... grep -E
18162 checking for ANSI C header files... yes
18163 checking for sys/types.h... yes
18164 checking for sys/stat.h... yes
18165 checking for stdlib.h... yes
18166 checking for string.h... yes
18167 checking for memory.h... yes
18168 checking for strings.h... yes
18169 checking for inttypes.h... yes
18170 checking for stdint.h... yes
18171 checking for unistd.h... yes
18172 checking pi.h usability... no
18173 checking pi.h presence... yes
18174 configure: WARNING: pi.h: present but cannot be compiled
18175 configure: WARNING: pi.h:     check for missing prerequisite headers?
18176 configure: WARNING: pi.h: see the Autoconf documentation
18177 configure: WARNING: pi.h:     section "Present But Cannot Be Compiled"
18178 configure: WARNING: pi.h: proceeding with the preprocessor's result
18179 configure: WARNING: pi.h: in the future, the compiler will take precedence
18180 configure: WARNING:     ## -------------------------------------- ##
18181 configure: WARNING:     ## Report this to bug-example@@example.org ##
18182 configure: WARNING:     ## -------------------------------------- ##
18183 checking for pi.h... yes
18184 @end smallexample
18186 @noindent
18187 The proper way the handle this case is using the fourth argument
18188 (@pxref{Generic Headers}):
18190 @example
18191 $ @kbd{cat configure.ac}
18192 AC_INIT([Example], [1.0], [bug-example@@example.org])
18193 AC_CHECK_HEADERS([number.h pi.h], [], [],
18194 [[#if HAVE_NUMBER_H
18195 # include <number.h>
18196 #endif
18198 $ @kbd{autoconf -Wall}
18199 $ @kbd{./configure}
18200 checking for gcc... gcc
18201 checking for C compiler default output... a.out
18202 checking whether the C compiler works... yes
18203 checking whether we are cross compiling... no
18204 checking for suffix of executables...
18205 checking for suffix of object files... o
18206 checking whether we are using the GNU C compiler... yes
18207 checking whether gcc accepts -g... yes
18208 checking for gcc option to accept ANSI C... none needed
18209 checking for number.h... yes
18210 checking for pi.h... yes
18211 @end example
18213 See @ref{Particular Headers}, for a list of headers with their
18214 prerequisite.
18216 @c ===================================================== History of Autoconf.
18218 @node History
18219 @chapter History of Autoconf
18220 @cindex History of autoconf
18222 You may be wondering, Why was Autoconf originally written?  How did it
18223 get into its present form?  (Why does it look like gorilla spit?)  If
18224 you're not wondering, then this chapter contains no information useful
18225 to you, and you might as well skip it.  If you @emph{are} wondering,
18226 then let there be light@enddots{}
18228 @menu
18229 * Genesis::                     Prehistory and naming of @command{configure}
18230 * Exodus::                      The plagues of M4 and Perl
18231 * Leviticus::                   The priestly code of portability arrives
18232 * Numbers::                     Growth and contributors
18233 * Deuteronomy::                 Approaching the promises of easy configuration
18234 @end menu
18236 @node Genesis
18237 @section Genesis
18239 In June 1991 I was maintaining many of the @acronym{GNU} utilities for the
18240 Free Software Foundation.  As they were ported to more platforms and
18241 more programs were added, the number of @option{-D} options that users
18242 had to select in the @file{Makefile} (around 20) became burdensome.
18243 Especially for me---I had to test each new release on a bunch of
18244 different systems.  So I wrote a little shell script to guess some of
18245 the correct settings for the fileutils package, and released it as part
18246 of fileutils 2.0.  That @command{configure} script worked well enough that
18247 the next month I adapted it (by hand) to create similar @command{configure}
18248 scripts for several other @acronym{GNU} utilities packages.  Brian Berliner
18249 also adapted one of my scripts for his @acronym{CVS} revision control system.
18251 Later that summer, I learned that Richard Stallman and Richard Pixley
18252 were developing similar scripts to use in the @acronym{GNU} compiler tools;
18253 so I adapted my @command{configure} scripts to support their evolving
18254 interface: using the file name @file{Makefile.in} as the templates;
18255 adding @samp{+srcdir}, the first option (of many); and creating
18256 @file{config.status} files.
18258 @node Exodus
18259 @section Exodus
18261 As I got feedback from users, I incorporated many improvements, using
18262 Emacs to search and replace, cut and paste, similar changes in each of
18263 the scripts.  As I adapted more @acronym{GNU} utilities packages to use
18264 @command{configure} scripts, updating them all by hand became impractical.
18265 Rich Murphey, the maintainer of the @acronym{GNU} graphics utilities, sent me
18266 mail saying that the @command{configure} scripts were great, and asking if
18267 I had a tool for generating them that I could send him.  No, I thought,
18268 but I should!  So I started to work out how to generate them.  And the
18269 journey from the slavery of hand-written @command{configure} scripts to the
18270 abundance and ease of Autoconf began.
18272 Cygnus @command{configure}, which was being developed at around that time,
18273 is table driven; it is meant to deal mainly with a discrete number of
18274 system types with a small number of mainly unguessable features (such as
18275 details of the object file format).  The automatic configuration system
18276 that Brian Fox had developed for Bash takes a similar approach.  For
18277 general use, it seems to me a hopeless cause to try to maintain an
18278 up-to-date database of which features each variant of each operating
18279 system has.  It's easier and more reliable to check for most features on
18280 the fly---especially on hybrid systems that people have hacked on
18281 locally or that have patches from vendors installed.
18283 I considered using an architecture similar to that of Cygnus
18284 @command{configure}, where there is a single @command{configure} script that
18285 reads pieces of @file{configure.in} when run.  But I didn't want to have
18286 to distribute all of the feature tests with every package, so I settled
18287 on having a different @command{configure} made from each
18288 @file{configure.in} by a preprocessor.  That approach also offered more
18289 control and flexibility.
18291 I looked briefly into using the Metaconfig package, by Larry Wall,
18292 Harlan Stenn, and Raphael Manfredi, but I decided not to for several
18293 reasons.  The @command{Configure} scripts it produces are interactive,
18294 which I find quite inconvenient; I didn't like the ways it checked for
18295 some features (such as library functions); I didn't know that it was
18296 still being maintained, and the @command{Configure} scripts I had
18297 seen didn't work on many modern systems (such as System V R4 and NeXT);
18298 it wasn't very flexible in what it could do in response to a feature's
18299 presence or absence; I found it confusing to learn; and it was too big
18300 and complex for my needs (I didn't realize then how much Autoconf would
18301 eventually have to grow).
18303 I considered using Perl to generate my style of @command{configure}
18304 scripts, but decided that M4 was better suited to the job of simple
18305 textual substitutions: it gets in the way less, because output is
18306 implicit.  Plus, everyone already has it.  (Initially I didn't rely on
18307 the @acronym{GNU} extensions to M4.)  Also, some of my friends at the
18308 University of Maryland had recently been putting M4 front ends on
18309 several programs, including @code{tvtwm}, and I was interested in trying
18310 out a new language.
18312 @node Leviticus
18313 @section Leviticus
18315 Since my @command{configure} scripts determine the system's capabilities
18316 automatically, with no interactive user intervention, I decided to call
18317 the program that generates them Autoconfig.  But with a version number
18318 tacked on, that name would be too long for old Unix file systems,
18319 so I shortened it to Autoconf.
18321 In the fall of 1991 I called together a group of fellow questers after
18322 the Holy Grail of portability (er, that is, alpha testers) to give me
18323 feedback as I encapsulated pieces of my handwritten scripts in M4 macros
18324 and continued to add features and improve the techniques used in the
18325 checks.  Prominent among the testers were Fran@,{c}ois Pinard, who came up
18326 with the idea of making an Autoconf shell script to run M4
18327 and check for unresolved macro calls; Richard Pixley, who suggested
18328 running the compiler instead of searching the file system to find
18329 include files and symbols, for more accurate results; Karl Berry, who
18330 got Autoconf to configure @TeX{} and added the macro index to the
18331 documentation; and Ian Lance Taylor, who added support for creating a C
18332 header file as an alternative to putting @option{-D} options in a
18333 @file{Makefile}, so he could use Autoconf for his @acronym{UUCP} package.
18334 The alpha testers cheerfully adjusted their files again and again as the
18335 names and calling conventions of the Autoconf macros changed from
18336 release to release.  They all contributed many specific checks, great
18337 ideas, and bug fixes.
18339 @node Numbers
18340 @section Numbers
18342 In July 1992, after months of alpha testing, I released Autoconf 1.0,
18343 and converted many @acronym{GNU} packages to use it.  I was surprised by how
18344 positive the reaction to it was.  More people started using it than I
18345 could keep track of, including people working on software that wasn't
18346 part of the @acronym{GNU} Project (such as TCL, FSP, and Kerberos V5).
18347 Autoconf continued to improve rapidly, as many people using the
18348 @command{configure} scripts reported problems they encountered.
18350 Autoconf turned out to be a good torture test for M4 implementations.
18351 Unix M4 started to dump core because of the length of the
18352 macros that Autoconf defined, and several bugs showed up in @acronym{GNU}
18353 M4 as well.  Eventually, we realized that we needed to use some
18354 features that only @acronym{GNU} M4 has.  4.3@acronym{BSD} M4, in
18355 particular, has an impoverished set of builtin macros; the System V
18356 version is better, but still doesn't provide everything we need.
18358 More development occurred as people put Autoconf under more stresses
18359 (and to uses I hadn't anticipated).  Karl Berry added checks for X11.
18360 david zuhn contributed C++ support.  Fran@,{c}ois Pinard made it diagnose
18361 invalid arguments.  Jim Blandy bravely coerced it into configuring
18362 @acronym{GNU} Emacs, laying the groundwork for several later improvements.
18363 Roland McGrath got it to configure the @acronym{GNU} C Library, wrote the
18364 @command{autoheader} script to automate the creation of C header file
18365 templates, and added a @option{--verbose} option to @command{configure}.
18366 Noah Friedman added the @option{--autoconf-dir} option and
18367 @code{AC_MACRODIR} environment variable.  (He also coined the term
18368 @dfn{autoconfiscate} to mean ``adapt a software package to use
18369 Autoconf''.)  Roland and Noah improved the quoting protection in
18370 @code{AC_DEFINE} and fixed many bugs, especially when I got sick of
18371 dealing with portability problems from February through June, 1993.
18373 @node Deuteronomy
18374 @section Deuteronomy
18376 A long wish list for major features had accumulated, and the effect of
18377 several years of patching by various people had left some residual
18378 cruft.  In April 1994, while working for Cygnus Support, I began a major
18379 revision of Autoconf.  I added most of the features of the Cygnus
18380 @command{configure} that Autoconf had lacked, largely by adapting the
18381 relevant parts of Cygnus @command{configure} with the help of david zuhn
18382 and Ken Raeburn.  These features include support for using
18383 @file{config.sub}, @file{config.guess}, @option{--host}, and
18384 @option{--target}; making links to files; and running @command{configure}
18385 scripts in subdirectories.  Adding these features enabled Ken to convert
18386 @acronym{GNU} @code{as}, and Rob Savoye to convert Deja@acronym{GNU}, to using
18387 Autoconf.
18389 I added more features in response to other peoples' requests.  Many
18390 people had asked for @command{configure} scripts to share the results of
18391 the checks between runs, because (particularly when configuring a large
18392 source tree, like Cygnus does) they were frustratingly slow.  Mike
18393 Haertel suggested adding site-specific initialization scripts.  People
18394 distributing software that had to unpack on MS-DOS asked for a way to
18395 override the @file{.in} extension on the file names, which produced file
18396 names like @file{config.h.in} containing two dots.  Jim Avera did an
18397 extensive examination of the problems with quoting in @code{AC_DEFINE}
18398 and @code{AC_SUBST}; his insights led to significant improvements.
18399 Richard Stallman asked that compiler output be sent to @file{config.log}
18400 instead of @file{/dev/null}, to help people debug the Emacs
18401 @command{configure} script.
18403 I made some other changes because of my dissatisfaction with the quality
18404 of the program.  I made the messages showing results of the checks less
18405 ambiguous, always printing a result.  I regularized the names of the
18406 macros and cleaned up coding style inconsistencies.  I added some
18407 auxiliary utilities that I had developed to help convert source code
18408 packages to use Autoconf.  With the help of Fran@,{c}ois Pinard, I made
18409 the macros not interrupt each others' messages.  (That feature revealed
18410 some performance bottlenecks in @acronym{GNU} M4, which he hastily
18411 corrected!)  I reorganized the documentation around problems people want
18412 to solve.  And I began a test suite, because experience had shown that
18413 Autoconf has a pronounced tendency to regress when we change it.
18415 Again, several alpha testers gave invaluable feedback, especially
18416 Fran@,{c}ois Pinard, Jim Meyering, Karl Berry, Rob Savoye, Ken Raeburn,
18417 and Mark Eichin.
18419 Finally, version 2.0 was ready.  And there was much rejoicing.  (And I
18420 have free time again.  I think.  Yeah, right.)
18423 @c ========================================================== Appendices
18425 @node Copying This Manual
18426 @appendix Copying This Manual
18427 @cindex License
18429 @menu
18430 * GNU Free Documentation License::  License for copying this manual
18431 @end menu
18433 @include fdl.texi
18435 @node Indices
18436 @appendix Indices
18438 @menu
18439 * Environment Variable Index::  Index of environment variables used
18440 * Output Variable Index::       Index of variables set in output files
18441 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
18442 * Autoconf Macro Index::        Index of Autoconf macros
18443 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
18444 * Autotest Macro Index::        Index of Autotest macros
18445 * Program & Function Index::    Index of those with portability problems
18446 * Concept Index::               General index
18447 @end menu
18449 @node Environment Variable Index
18450 @appendixsec Environment Variable Index
18452 This is an alphabetical list of the environment variables that Autoconf
18453 checks.
18455 @printindex ev
18457 @node Output Variable Index
18458 @appendixsec Output Variable Index
18460 This is an alphabetical list of the variables that Autoconf can
18461 substitute into files that it creates, typically one or more
18462 @file{Makefile}s.  @xref{Setting Output Variables}, for more information
18463 on how this is done.
18465 @printindex ov
18467 @node Preprocessor Symbol Index
18468 @appendixsec Preprocessor Symbol Index
18470 This is an alphabetical list of the C preprocessor symbols that the
18471 Autoconf macros define.  To work with Autoconf, C source code needs to
18472 use these names in @code{#if} directives.
18474 @printindex cv
18476 @node Autoconf Macro Index
18477 @appendixsec Autoconf Macro Index
18479 This is an alphabetical list of the Autoconf macros.
18480 @ifset shortindexflag
18481 To make the list easier to use, the macros are listed without their
18482 preceding @samp{AC_}.
18483 @end ifset
18485 @printindex AC
18487 @node M4 Macro Index
18488 @appendixsec M4 Macro Index
18490 This is an alphabetical list of the M4, M4sugar, and M4sh macros.
18491 @ifset shortindexflag
18492 To make the list easier to use, the macros are listed without their
18493 preceding @samp{m4_} or @samp{AS_}.
18494 @end ifset
18496 @printindex MS
18498 @node Autotest Macro Index
18499 @appendixsec Autotest Macro Index
18501 This is an alphabetical list of the Autotest macros.
18502 @ifset shortindexflag
18503 To make the list easier to use, the macros are listed without their
18504 preceding @samp{AT_}.
18505 @end ifset
18507 @printindex AT
18509 @node Program & Function Index
18510 @appendixsec Program and Function Index
18512 This is an alphabetical list of the programs and functions which
18513 portability is discussed in this document.
18515 @printindex pr
18517 @node Concept Index
18518 @appendixsec Concept Index
18520 This is an alphabetical list of the files, tools, and concepts
18521 introduced in this document.
18523 @printindex cp
18525 @bye
18527 @c  LocalWords:  texinfo setfilename autoconf texi settitle setchapternewpage
18528 @c  LocalWords:  setcontentsaftertitlepage finalout ARG ovar varname dvar acx
18529 @c  LocalWords:  makeinfo dvi defcodeindex ev ov CPP cv Autotest mv defindex fn
18530 @c  LocalWords:  shortindexflag iftex ifset acindex ACindex ifclear ahindex fu
18531 @c  LocalWords:  asindex MSindex atindex ATindex auindex hdrindex prindex FIXME
18532 @c  LocalWords:  msindex alloca fnindex Aaarg indices FSF's dircategory ifnames
18533 @c  LocalWords:  direntry autoscan autoreconf autoheader autoupdate config FDs
18534 @c  LocalWords:  testsuite titlepage Elliston Demaille vskip filll ifnottex hmm
18535 @c  LocalWords:  insertcopying Autoconf's detailmenu Automake Libtool Posix ois
18536 @c  LocalWords:  Systemology Checkpointing Changequote INTERCAL changequote dfn
18537 @c  LocalWords:  Quadrigraphs builtins Shellology acconfig Bugward LIBOBJ Imake
18538 @c  LocalWords:  LIBOBJS IFELSE cindex flushright Pinard Metaconfig uref Simons
18539 @c  LocalWords:  distclean uninstall noindent versioning Tromey dir
18540 @c  LocalWords:  SAMS samp aclocal acsite underquoted emph itemx prepend SUBST
18541 @c  LocalWords:  evindex automake Gettext autopoint gettext symlink libtoolize
18542 @c  LocalWords:  defmac INIT tarname ovindex cvindex BUGREPORT PREREQ asis PROG
18543 @c  LocalWords:  SRCDIR srcdir globbing afterwards cmds foos fooo foooo init cd
18544 @c  LocalWords:  builddir timestamp src Imakefile chmod defvar CFLAGS CPPFLAGS
18545 @c  LocalWords:  CXXFLAGS DEFS DHAVE defvarx FCFLAGS FFLAGS LDFLAGS bindir GCC
18546 @c  LocalWords:  datadir datarootdir docdir dvidir htmldir libdir ifnothtml kbd
18547 @c  LocalWords:  includedir infodir libexecdir localedir localstatedir mandir
18548 @c  LocalWords:  oldincludedir pdfdir PDF psdir PostScript sbindir sysconfdir
18549 @c  LocalWords:  sharedstatedir DDATADIR sed tmp pkgdatadir VPATH conf unistd
18550 @c  LocalWords:  undef endif builtin FUNCS ifndef STACKSEG getb GETB YMP fubar
18551 @c  LocalWords:  PRE dest SUBDIRS subdirs fi struct STDC stdlib stddef INTTYPES
18552 @c  LocalWords:  inttypes STDINT stdint AWK AIX Solaris NeXT env EGREP FGREP yy
18553 @c  LocalWords:  LEXLIB YYTEXT lfl nonportable Automake's LN RANLIB byacc INETD
18554 @c  LocalWords:  inetd prog PROGS progs ranlib lmp lXt lX nsl gethostbyname UX
18555 @c  LocalWords:  NextStep isinf isnan glibc IRIX sunmath lm lsunmath pre sizeof
18556 @c  LocalWords:  ld inline malloc OSF putenv setenv FreeBSD realloc SunOS MinGW
18557 @c  LocalWords:  snprintf vsnprintf sprintf vsprintf sscanf gcc strerror ifdef
18558 @c  LocalWords:  strnlen sysconf PAGESIZE unsetenv va fallback memcpy dst FUNC
18559 @c  LocalWords:  PowerPC GNUC libPW pragma Olibcalls CHOWN chown CLOSEDIR VFORK
18560 @c  LocalWords:  closedir FNMATCH fnmatch vfork FSEEKO LARGEFILE fseeko SVR sc
18561 @c  LocalWords:  largefile GETGROUPS getgroups GETLOADAVG DGUX UMAX NLIST KMEM
18562 @c  LocalWords:  GETLODAVG SETGID getloadavg nlist GETMNTENT irix
18563 @c  LocalWords:  getmntent UnixWare GETPGRP getpgid getpgrp Posix's pid LSTAT
18564 @c  LocalWords:  lstat rpl MEMCMP memcmp OpenStep MBRTOWC mbrtowc MKTIME mktime
18565 @c  LocalWords:  localtime MMAP mmap OBSTACK obstack obstacks ARGTYPES timeval
18566 @c  LocalWords:  SETPGRP setpgrp defmacx Hurd SETVBUF setvbuf STRCOLL strcoll
18567 @c  LocalWords:  STRTOD strtod DECL STRFTIME strftime SCO UTIME utime VPRINTF
18568 @c  LocalWords:  DOPRNT vprintf doprnt sp unfixable LIBSOURCE LIBSOURCES Eggert
18569 @c  LocalWords:  linux netinet ia Tru XFree DIRENT NDIR dirent ndir multitable
18570 @c  LocalWords:  NAMLEN strlen namlen MKDEV SYSMACROS makedev RESOLV resolv DNS
18571 @c  LocalWords:  inet structs NAMESER arpa NETDB netdb UTekV UTS autom GCC's kB
18572 @c  LocalWords:  STDBOOL BOOL stdbool conformant cplusplus bool Bool stdarg tm
18573 @c  LocalWords:  ctype strchr strrchr rindex bcopy memmove memchr WEXITSTATUS
18574 @c  LocalWords:  WIFEXITED TIOCGWINSZ GWINSZ termios preprocess preprocessable
18575 @c  LocalWords:  DECLS strdup calloc BLKSIZE blksize RDEV rdev TZNAME tzname pw
18576 @c  LocalWords:  passwd gecos pwd MBSTATE mbstate wchar RETSIGTYPE hup UID uid
18577 @c  LocalWords:  gid ptrdiff uintmax EXEEXT OBJEXT Ae Onolimit conftest AXP str
18578 @c  LocalWords:  ALIGNOF WERROR Werror cpp HP's WorkShop egcs un fied stdc CXX
18579 @c  LocalWords:  varargs BIGENDIAN Endianness SPARC endianness grep'ed CONST FC
18580 @c  LocalWords:  const STRINGIZE stringizing PARAMS unprotoize protos KCC cxx
18581 @c  LocalWords:  xlC aCC CXXCPP FREEFORM xlf FLIBS FCLIBS ish SRCEXT XTRA LFS
18582 @c  LocalWords:  ISC lcposix MINIX Minix conditionalized inlines hw dD confdefs
18583 @c  LocalWords:  fputs stdout PREPROC ar UFS HFS QNX realtime fstype STATVFS se
18584 @c  LocalWords:  statvfs STATFS statfs func machfile hdr lelf raboof DEFUN GTK
18585 @c  LocalWords:  GTKMM Grmph ified ine defn baz EOF qar Ahhh changecom algol io
18586 @c  LocalWords:  changeword quadrigraphs quadrigraph dnl SGI atoi overquoting
18587 @c  LocalWords:  Aas Wcross sep args namespace undefine bpatsubst popdef dquote
18588 @c  LocalWords:  bregexp Overquote overquotation meisch maisch meische maische
18589 @c  LocalWords:  miscian DIRNAME dirname MKDIR CATFILE XMKMF TRAVOLTA celsius
18590 @c  LocalWords:  EMX emxos Emacsen Korn DYNIX subshell posix Ksh ksh Pdksh Zsh
18591 @c  LocalWords:  pdksh zsh Allbery Lipe Kubota UWS zorglub stderr eval esac lfn
18592 @c  LocalWords:  drivespec Posixy DJGPP doschk prettybird LPT pfew Zsh's yu yaa
18593 @c  LocalWords:  yM uM aM firebird IP subdir misparses ok Unpatched abc bc zA
18594 @c  LocalWords:  CDPATH DUALCASE LINENO prepass Subshells lineno NULLCMD cmp wc
18595 @c  LocalWords:  MAILPATH scanset arg NetBSD Almquist printf expr cp
18596 @c  LocalWords:  Oliva awk Aaaaarg cmd regex xfoo GNV OpenVMS unwriteable te VM
18597 @c  LocalWords:  sparc Proulx SysV nbar nfoo maxdepth acdilrtu TWG mc
18598 @c  LocalWords:  mkdir exe uname OpenBSD Fileutils mktemp umask TMPDIR guid os
18599 @c  LocalWords:  fooXXXXXX Unicos parenthesization utimes hpux hppa unescaped
18600 @c  LocalWords:  pmake DOS's gmake ifoo DESTDIR autoconfiscated pc coff mips gg
18601 @c  LocalWords:  dec ultrix cpu wildcards rpcc rdtsc powerpc behaviour readline
18602 @c  LocalWords:  withval vxworks gless localcache usr LOFF loff CYGWIN Cygwin
18603 @c  LocalWords:  cygwin SIGLIST siglist SYSNDIR SYSDIR ptx lseq rusage elif MSC
18604 @c  LocalWords:  lfoo POUNDBANG lsun NIS getpwnam SYSCALLS RSH INTL lintl aix
18605 @c  LocalWords:  intl lx ldir syslog bsd EPI toolchain netbsd objext de KNR nn
18606 @c  LocalWords:  fication LTLIBOBJS Wdiff TESTDIR atconfig atlocal akim XFAIL
18607 @c  LocalWords:  ChangeLog prepended errexit smallexample TESTSUITEFLAGS GPL er
18608 @c  LocalWords:  installcheck autotest indir Pixley Bothner Eichin Kerberos adl
18609 @c  LocalWords:  DISTCLEANFILES preprocessor's fileutils Stallman Murphey Stenn
18610 @c  LocalWords:  Manfredi Autoconfig TCL FSP david zuhn Blandy MACRODIR Raeburn
18611 @c  LocalWords:  autoconfiscate Savoye Haertel Avera Meyering fdl appendixsec
18612 @c  LocalWords:  printindex american LIBOBJDIR LibdirTest ERLCFLAGS OBJCFLAGS
18613 @c  LocalWords:  VER Gnulib online xyes strcpy TYPEOF typeof OBJC objcc objc ln
18614 @c  LocalWords:  GOBJC OBJCCPP OTP ERLC erl valloc decr dumpdef errprint incr
18615 @c  LocalWords:  esyscmd len maketemp pushdef substr syscmd sysval translit txt
18616 @c  LocalWords:  sinclude foreach myvar tolower toupper uniq BASENAME STDIN
18617 @c  LocalWords:  Dynix descrips basename aname cname macroexpands xno xcheck
18618 @c  LocalWords:  LIBREADLINE lreadline lncurses libreadline
18620 @c Local Variables:
18621 @c fill-column: 72
18622 @c ispell-local-dictionary: "american"
18623 @c End: