Namespace cleanup.
[autoconf.git] / doc / autoconf.texi
blob74295b6554d248bb07fdda64f48e2add4386212c
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, 2007 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-invocation: (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 Make::               Makefile portability pitfalls
266 * Portable C and C++::          C and C++ portability pitfalls
267 * Manual Configuration::        Selecting features that can't be guessed
268 * Site Configuration::          Local defaults for @command{configure}
269 * Running configure Scripts::   How to use the Autoconf output
270 * config.status Invocation::    Recreating a configuration
271 * Obsolete Constructs::         Kept for backward compatibility
272 * Using Autotest::              Creating portable test suites
273 * FAQ::                         Frequent Autoconf Questions, with answers
274 * History::                     History of Autoconf
275 * GNU Free Documentation License::  License for copying this manual
276 * Indices::                     Indices of symbols, concepts, etc.
278 @detailmenu
279  --- The Detailed Node Listing ---
281 The @acronym{GNU} Build System
283 * Automake::                    Escaping makefile hell
284 * Gnulib::                      The @acronym{GNU} portability library
285 * Libtool::                     Building libraries portably
286 * Pointers::                    More info on the @acronym{GNU} build system
288 Making @command{configure} Scripts
290 * Writing Autoconf Input::      What to put in an Autoconf input file
291 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
292 * ifnames Invocation::          Listing the conditionals in source code
293 * autoconf Invocation::         How to create configuration scripts
294 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
296 Writing @file{configure.ac}
298 * Shell Script Compiler::       Autoconf as solution of a problem
299 * Autoconf Language::           Programming in Autoconf
300 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
302 Initialization and Output Files
304 * Initializing configure::      Option processing etc.
305 * Versioning::                  Dealing with Autoconf versions
306 * Notices::                     Copyright, version numbers in @command{configure}
307 * Input::                       Where Autoconf should find files
308 * Output::                      Outputting results from the configuration
309 * Configuration Actions::       Preparing the output based on results
310 * Configuration Files::         Creating output files
311 * Makefile Substitutions::      Using output variables in makefiles
312 * Configuration Headers::       Creating a configuration header file
313 * Configuration Commands::      Running arbitrary instantiation commands
314 * Configuration Links::         Links depending on the configuration
315 * Subdirectories::              Configuring independent packages together
316 * Default Prefix::              Changing the default installation prefix
318 Substitutions in Makefiles
320 * Preset Output Variables::     Output variables that are always set
321 * Installation Directory Variables::  Other preset output variables
322 * Changed Directory Variables:: Warnings about @file{datarootdir}
323 * Build Directories::           Supporting multiple concurrent compiles
324 * Automatic Remaking::          Makefile rules for configuring
326 Configuration Header Files
328 * Header Templates::            Input for the configuration headers
329 * autoheader Invocation::       How to create configuration templates
330 * Autoheader Macros::           How to specify CPP templates
332 Existing Tests
334 * Common Behavior::             Macros' standard schemes
335 * Alternative Programs::        Selecting between alternative programs
336 * Files::                       Checking for the existence of files
337 * Libraries::                   Library archives that might be missing
338 * Library Functions::           C library functions that might be missing
339 * Header Files::                Header files that might be missing
340 * Declarations::                Declarations that may be missing
341 * Structures::                  Structures or members that might be missing
342 * Types::                       Types that might be missing
343 * Compilers and Preprocessors::  Checking for compiling programs
344 * System Services::             Operating system services
345 * Posix Variants::              Special kludges for specific Posix variants
346 * Erlang Libraries::            Checking for the existence of Erlang libraries
348 Common Behavior
350 * Standard Symbols::            Symbols defined by the macros
351 * Default Includes::            Includes used by the generic macros
353 Alternative Programs
355 * Particular Programs::         Special handling to find certain programs
356 * Generic Programs::            How to find other programs
358 Library Functions
360 * Function Portability::        Pitfalls with usual functions
361 * Particular Functions::        Special handling to find certain functions
362 * Generic Functions::           How to find other functions
364 Header Files
366 * Header Portability::          Collected knowledge on common headers
367 * Particular Headers::          Special handling to find certain headers
368 * Generic Headers::             How to find other headers
370 Declarations
372 * Particular Declarations::     Macros to check for certain declarations
373 * Generic Declarations::        How to find other declarations
375 Structures
377 * Particular Structures::       Macros to check for certain structure members
378 * Generic Structures::          How to find other structure members
380 Types
382 * Particular Types::            Special handling to find certain types
383 * Generic Types::               How to find other types
385 Compilers and Preprocessors
387 * Specific Compiler Characteristics::  Some portability issues
388 * Generic Compiler Characteristics::  Language independent tests and features
389 * C Compiler::                  Checking its characteristics
390 * C++ Compiler::                Likewise
391 * Objective C Compiler::        Likewise
392 * Erlang Compiler and Interpreter::  Likewise
393 * Fortran Compiler::            Likewise
395 Writing Tests
397 * Language Choice::             Selecting which language to use for testing
398 * Writing Test Programs::       Forging source files for compilers
399 * Running the Preprocessor::    Detecting preprocessor symbols
400 * Running the Compiler::        Detecting language or header features
401 * Running the Linker::          Detecting library features
402 * Runtime::                     Testing for runtime features
403 * Systemology::                 A zoology of operating systems
404 * Multiple Cases::              Tests for several possible values
406 Writing Test Programs
408 * Guidelines::                  General rules for writing test programs
409 * Test Functions::              Avoiding pitfalls in test programs
410 * Generating Sources::          Source program boilerplate
412 Results of Tests
414 * Defining Symbols::            Defining C preprocessor symbols
415 * Setting Output Variables::    Replacing variables in output files
416 * Special Chars in Variables::  Characters to beware of in variables
417 * Caching Results::             Speeding up subsequent @command{configure} runs
418 * Printing Messages::           Notifying @command{configure} users
420 Caching Results
422 * Cache Variable Names::        Shell variables used in caches
423 * Cache Files::                 Files @command{configure} uses for caching
424 * Cache Checkpointing::         Loading and saving the cache file
426 Programming in M4
428 * M4 Quotation::                Protecting macros from unwanted expansion
429 * Using autom4te::              The Autoconf executables backbone
430 * Programming in M4sugar::      Convenient pure M4 macros
431 * Programming in M4sh::         Common shell Constructs
432 * File Descriptor Macros::      File descriptor macros for input and output
434 M4 Quotation
436 * Active Characters::           Characters that change the behavior of M4
437 * One Macro Call::              Quotation and one macro call
438 * Quoting and Parameters::      M4 vs. shell parameters
439 * Quotation and Nested Macros::  Macros calling macros
440 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
441 * Quadrigraphs::                Another way to escape special characters
442 * Quotation Rule Of Thumb::     One parenthesis, one quote
444 Using @command{autom4te}
446 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
447 * Customizing autom4te::        Customizing the Autoconf package
449 Programming in M4sugar
451 * Redefined M4 Macros::         M4 builtins changed in M4sugar
452 * Conditional constructs::      Conditions in M4
453 * Looping constructs::          Iteration in M4
454 * Evaluation Macros::           More quotation and evaluation control
455 * Text processing Macros::      String manipulation in M4
456 * Forbidden Patterns::          Catching unexpanded macros
458 Writing Autoconf Macros
460 * Macro Definitions::           Basic format of an Autoconf macro
461 * Macro Names::                 What to call your new macros
462 * Reporting Messages::          Notifying @command{autoconf} users
463 * Dependencies Between Macros::  What to do when macros depend on other macros
464 * Obsoleting Macros::           Warning about old ways of doing things
465 * Coding Style::                Writing Autoconf macros @`a la Autoconf
467 Dependencies Between Macros
469 * Prerequisite Macros::         Ensuring required information
470 * Suggested Ordering::          Warning about possible ordering problems
471 * One-Shot Macros::             Ensuring a macro is called only once
473 Portable Shell Programming
475 * Shellology::                  A zoology of shells
476 * Here-Documents::              Quirks and tricks
477 * File Descriptors::            FDs and redirections
478 * File System Conventions::     File names
479 * Shell Pattern Matching::      Pattern matching
480 * Shell Substitutions::         Variable and command expansions
481 * Assignments::                 Varying side effects of assignments
482 * Parentheses::                 Parentheses in shell scripts
483 * Slashes::                     Slashes in shell scripts
484 * Special Shell Variables::     Variables you should not change
485 * Limitations of Builtins::     Portable use of not so portable /bin/sh
486 * Limitations of Usual Tools::  Portable use of portable tools
488 Portable Make Programming
490 * $< in Ordinary Make Rules::   $< in ordinary rules
491 * Failure in Make Rules::       Failing portably in rules
492 * Special Chars in Names::      Special Characters in Macro Names
493 * Backslash-Newline-Newline::   Empty last lines in macro definitions
494 * Backslash-Newline Comments::  Spanning comments across line boundaries
495 * Long Lines in Makefiles::     Line length limitations
496 * Macros and Submakes::         @code{make macro=value} and submakes
497 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
498 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
499 * Comments in Make Rules::      Other problems with Make comments
500 * obj/ and Make::               Don't name a subdirectory @file{obj}
501 * make -k Status::              Exit status of @samp{make -k}
502 * VPATH and Make::              @code{VPATH} woes
503 * Single Suffix Rules::         Single suffix rules and separated dependencies
504 * Timestamps and Make::         Subsecond timestamp resolution
506 @code{VPATH} and Make
508 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
509 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
510 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
511 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
512 * Make Target Lookup::          More details about @code{VPATH} lookup
514 Portable C and C++ Programming
516 * Varieties of Unportability::  How to make your programs unportable
517 * Integer Overflow::            When integers get too large
518 * Null Pointers::               Properties of null pointers
519 * Buffer Overruns::             Subscript errors and the like
520 * Volatile Objects::            @code{volatile} and signals
521 * Floating Point Portability::  Portable floating-point arithmetic
522 * Exiting Portably::            Exiting and the exit status
524 Manual Configuration
526 * Specifying Names::            Specifying the system type
527 * Canonicalizing::              Getting the canonical system type
528 * Using System Type::           What to do with the system type
530 Site Configuration
532 * Help Formatting::             Customizing @samp{configure --help}
533 * External Software::           Working with other optional software
534 * Package Options::             Selecting optional features
535 * Pretty Help Strings::         Formatting help string
536 * Option Checking::             Controlling checking of @command{configure} options
537 * Site Details::                Configuring site details
538 * Transforming Names::          Changing program names when installing
539 * Site Defaults::               Giving @command{configure} local defaults
541 Transforming Program Names When Installing
543 * Transformation Options::      @command{configure} options to transform names
544 * Transformation Examples::     Sample uses of transforming names
545 * Transformation Rules::        Makefile uses of transforming names
547 Running @command{configure} Scripts
549 * Basic Installation::          Instructions for typical cases
550 * Compilers and Options::       Selecting compilers and optimization
551 * Multiple Architectures::      Compiling for multiple architectures at once
552 * Installation Names::          Installing in different directories
553 * Optional Features::           Selecting optional features
554 * System Type::                 Specifying the system type
555 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
556 * Defining Variables::          Specifying the compiler etc.
557 * configure Invocation::        Changing how @command{configure} runs
559 Obsolete Constructs
561 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
562 * acconfig Header::             Additional entries in @file{config.h.in}
563 * autoupdate Invocation::       Automatic update of @file{configure.ac}
564 * Obsolete Macros::             Backward compatibility macros
565 * Autoconf 1::                  Tips for upgrading your files
566 * Autoconf 2.13::               Some fresher tips
568 Upgrading From Version 1
570 * Changed File Names::          Files you might rename
571 * Changed Makefiles::           New things to put in @file{Makefile.in}
572 * Changed Macros::              Macro calls you might replace
573 * Changed Results::             Changes in how to check test results
574 * Changed Macro Writing::       Better ways to write your own macros
576 Upgrading From Version 2.13
578 * Changed Quotation::           Broken code which used to work
579 * New Macros::                  Interaction with foreign macros
580 * Hosts and Cross-Compilation::  Bugward compatibility kludges
581 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
582 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
584 Generating Test Suites with Autotest
586 * Using an Autotest Test Suite::  Autotest and the user
587 * Writing Testsuites::          Autotest macros
588 * testsuite Invocation::        Running @command{testsuite} scripts
589 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
591 Using an Autotest Test Suite
593 * testsuite Scripts::           The concepts of Autotest
594 * Autotest Logs::               Their contents
596 Frequent Autoconf Questions, with answers
598 * Distributing::                Distributing @command{configure} scripts
599 * Why GNU M4::                  Why not use the standard M4?
600 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
601 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
602 * Defining Directories::        Passing @code{datadir} to program
603 * Autom4te Cache::              What is it?  Can I remove it?
604 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
606 History of Autoconf
608 * Genesis::                     Prehistory and naming of @command{configure}
609 * Exodus::                      The plagues of M4 and Perl
610 * Leviticus::                   The priestly code of portability arrives
611 * Numbers::                     Growth and contributors
612 * Deuteronomy::                 Approaching the promises of easy configuration
614 Indices
616 * Environment Variable Index::  Index of environment variables used
617 * Output Variable Index::       Index of variables set in output files
618 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
619 * Autoconf Macro Index::        Index of Autoconf macros
620 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
621 * Autotest Macro Index::        Index of Autotest macros
622 * Program & Function Index::    Index of those with portability problems
623 * Concept Index::               General index
625 @end detailmenu
626 @end menu
628 @c ============================================================= Introduction.
630 @node Introduction
631 @chapter Introduction
632 @cindex Introduction
634 @flushright
635 A physicist, an engineer, and a computer scientist were discussing the
636 nature of God.  ``Surely a Physicist,'' said the physicist, ``because
637 early in the Creation, God made Light; and you know, Maxwell's
638 equations, the dual nature of electromagnetic waves, the relativistic
639 consequences@dots{}'' ``An Engineer!,'' said the engineer, ``because
640 before making Light, God split the Chaos into Land and Water; it takes a
641 hell of an engineer to handle that big amount of mud, and orderly
642 separation of solids from liquids@dots{}'' The computer scientist
643 shouted: ``And the Chaos, where do you think it was coming from, hmm?''
645 ---Anonymous
646 @end flushright
647 @c (via Franc,ois Pinard)
649 Autoconf is a tool for producing shell scripts that automatically
650 configure software source code packages to adapt to many kinds of
651 Posix-like systems.  The configuration scripts produced by Autoconf
652 are independent of Autoconf when they are run, so their users do not
653 need to have Autoconf.
655 The configuration scripts produced by Autoconf require no manual user
656 intervention when run; they do not normally even need an argument
657 specifying the system type.  Instead, they individually test for the
658 presence of each feature that the software package they are for might need.
659 (Before each check, they print a one-line message stating what they are
660 checking for, so the user doesn't get too bored while waiting for the
661 script to finish.)  As a result, they deal well with systems that are
662 hybrids or customized from the more common Posix variants.  There is
663 no need to maintain files that list the features supported by each
664 release of each variant of Posix.
666 For each software package that Autoconf is used with, it creates a
667 configuration script from a template file that lists the system features
668 that the package needs or can use.  After the shell code to recognize
669 and respond to a system feature has been written, Autoconf allows it to
670 be shared by many software packages that can use (or need) that feature.
671 If it later turns out that the shell code needs adjustment for some
672 reason, it needs to be changed in only one place; all of the
673 configuration scripts can be regenerated automatically to take advantage
674 of the updated code.
676 The Metaconfig package is similar in purpose to Autoconf, but the
677 scripts it produces require manual user intervention, which is quite
678 inconvenient when configuring large source trees.  Unlike Metaconfig
679 scripts, Autoconf scripts can support cross-compiling, if some care is
680 taken in writing them.
682 Autoconf does not solve all problems related to making portable
683 software packages---for a more complete solution, it should be used in
684 concert with other @acronym{GNU} build tools like Automake and
685 Libtool.  These other tools take on jobs like the creation of a
686 portable, recursive makefile with all of the standard targets,
687 linking of shared libraries, and so on.  @xref{The GNU Build System},
688 for more information.
690 Autoconf imposes some restrictions on the names of macros used with
691 @code{#if} in C programs (@pxref{Preprocessor Symbol Index}).
693 Autoconf requires @acronym{GNU} M4 version 1.4.5 or later in order to
694 generate the scripts.  It uses features that some versions of M4,
695 including @acronym{GNU} M4 1.3, do not have.  Autoconf works better
696 with @acronym{GNU} M4 version 1.4.8 or later, though this is not
697 required.
699 @xref{Autoconf 1}, for information about upgrading from version 1.
700 @xref{History}, for the story of Autoconf's development.  @xref{FAQ},
701 for answers to some common questions about Autoconf.
703 See the @uref{http://www.gnu.org/software/autoconf/,
704 Autoconf web page} for up-to-date information, details on the mailing
705 lists, pointers to a list of known bugs, etc.
707 Mail suggestions to @email{autoconf@@gnu.org, the Autoconf mailing
708 list}.  Past suggestions are
709 @uref{http://lists.gnu.org/archive/html/autoconf/, archived}.
711 Mail bug reports to @email{bug-autoconf@@gnu.org, the
712 Autoconf Bugs mailing list}.  Past bug reports are
713 @uref{http://lists.gnu.org/archive/html/bug-autoconf/, archived}.
715 If possible, first check that your bug is
716 not already solved in current development versions, and that it has not
717 been reported yet.  Be sure to include all the needed information and a
718 short @file{configure.ac} that demonstrates the problem.
720 Autoconf's development tree is accessible via anonymous @acronym{CVS}; see the
721 @uref{http://savannah.gnu.org/projects/autoconf/, Autoconf
722 Summary} for details.  Patches relative to the
723 current @acronym{CVS} version can be sent for review to the
724 @email{autoconf-patches@@gnu.org, Autoconf Patches mailing list}.
725 Past patches are
726 @uref{http://lists.gnu.org/@/archive/@/html/@/autoconf-patches/, archived}.
728 Because of its mission, the Autoconf package itself
729 includes only a set of often-used
730 macros that have already demonstrated their usefulness.  Nevertheless,
731 if you wish to share your macros, or find existing ones, see the
732 @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
733 Archive}, which is kindly run by @email{simons@@cryp.to,
734 Peter Simons}.
737 @c ================================================= The GNU Build System
739 @node The GNU Build System
740 @chapter The @acronym{GNU} Build System
741 @cindex @acronym{GNU} build system
743 Autoconf solves an important problem---reliable discovery of
744 system-specific build and runtime information---but this is only one
745 piece of the puzzle for the development of portable software.  To this
746 end, the @acronym{GNU} project has developed a suite of integrated
747 utilities to finish the job Autoconf started: the @acronym{GNU} build
748 system, whose most important components are Autoconf, Automake, and
749 Libtool.  In this chapter, we introduce you to those tools, point you
750 to sources of more information, and try to convince you to use the
751 entire @acronym{GNU} build system for your software.
753 @menu
754 * Automake::                    Escaping makefile hell
755 * Gnulib::                      The @acronym{GNU} portability library
756 * Libtool::                     Building libraries portably
757 * Pointers::                    More info on the @acronym{GNU} build system
758 @end menu
760 @node Automake
761 @section Automake
763 The ubiquity of @command{make} means that a makefile is almost the
764 only viable way to distribute automatic build rules for software, but
765 one quickly runs into its numerous limitations.  Its lack of
766 support for automatic dependency tracking, recursive builds in
767 subdirectories, reliable timestamps (e.g., for network file systems), and
768 so on, mean that developers must painfully (and often incorrectly)
769 reinvent the wheel for each project.  Portability is non-trivial, thanks
770 to the quirks of @command{make} on many systems.  On top of all this is the
771 manual labor required to implement the many standard targets that users
772 have come to expect (@code{make install}, @code{make distclean},
773 @code{make uninstall}, etc.).  Since you are, of course, using Autoconf,
774 you also have to insert repetitive code in your @code{Makefile.in} to
775 recognize @code{@@CC@@}, @code{@@CFLAGS@@}, and other substitutions
776 provided by @command{configure}.  Into this mess steps @dfn{Automake}.
777 @cindex Automake
779 Automake allows you to specify your build needs in a @code{Makefile.am}
780 file with a vastly simpler and more powerful syntax than that of a plain
781 makefile, and then generates a portable @code{Makefile.in} for
782 use with Autoconf.  For example, the @code{Makefile.am} to build and
783 install a simple ``Hello world'' program might look like:
785 @example
786 bin_PROGRAMS = hello
787 hello_SOURCES = hello.c
788 @end example
790 @noindent
791 The resulting @code{Makefile.in} (~400 lines) automatically supports all
792 the standard targets, the substitutions provided by Autoconf, automatic
793 dependency tracking, @code{VPATH} building, and so on.  @command{make}
794 builds the @code{hello} program, and @code{make install} installs it
795 in @file{/usr/local/bin} (or whatever prefix was given to
796 @command{configure}, if not @file{/usr/local}).
798 The benefits of Automake increase for larger packages (especially ones
799 with subdirectories), but even for small programs the added convenience
800 and portability can be substantial.  And that's not all@enddots{}
802 @node Gnulib
803 @section Gnulib
805 @acronym{GNU} software has a well-deserved reputation for running on
806 many different types of systems.  While our primary goal is to write
807 software for the @acronym{GNU} system, many users and developers have
808 been introduced to us through the systems that they were already using.
810 @cindex Gnulib
811 Gnulib is a central location for common @acronym{GNU} code, intended to
812 be shared among free software packages.  Its components are typically
813 shared at the source level, rather than being a library that gets built,
814 installed, and linked against.  The idea is to copy files from Gnulib
815 into your own source tree.  There is no distribution tarball; developers
816 should just grab source modules from the repository.  The source files
817 are available online, under various licenses, mostly @acronym{GNU}
818 @acronym{GPL} or @acronym{GNU} @acronym{LGPL}.
820 Gnulib modules typically contain C source code along with Autoconf
821 macros used to configure the source code.  For example, the Gnulib
822 @code{stdbool} module implements a @file{stdbool.h} header that nearly
823 conforms to C99, even on old-fashioned hosts that lack @file{stdbool.h}.
824 This module contains a source file for the replacement header, along
825 with an Autoconf macro that arranges to use the replacement header on
826 old-fashioned systems.
828 @node Libtool
829 @section Libtool
831 Often, one wants to build not only programs, but libraries, so that
832 other programs can benefit from the fruits of your labor.  Ideally, one
833 would like to produce @emph{shared} (dynamically linked) libraries,
834 which can be used by multiple programs without duplication on disk or in
835 memory and can be updated independently of the linked programs.
836 Producing shared libraries portably, however, is the stuff of
837 nightmares---each system has its own incompatible tools, compiler flags,
838 and magic incantations.  Fortunately, @acronym{GNU} provides a solution:
839 @dfn{Libtool}.
840 @cindex Libtool
842 Libtool handles all the requirements of building shared libraries for
843 you, and at this time seems to be the @emph{only} way to do so with any
844 portability.  It also handles many other headaches, such as: the
845 interaction of Make rules with the variable suffixes of
846 shared libraries, linking reliably with shared libraries before they are
847 installed by the superuser, and supplying a consistent versioning system
848 (so that different versions of a library can be installed or upgraded
849 without breaking binary compatibility).  Although Libtool, like
850 Autoconf, can be used without Automake, it is most simply utilized in
851 conjunction with Automake---there, Libtool is used automatically
852 whenever shared libraries are needed, and you need not know its syntax.
854 @node Pointers
855 @section Pointers
857 Developers who are used to the simplicity of @command{make} for small
858 projects on a single system might be daunted at the prospect of
859 learning to use Automake and Autoconf.  As your software is
860 distributed to more and more users, however, you otherwise
861 quickly find yourself putting lots of effort into reinventing the
862 services that the @acronym{GNU} build tools provide, and making the
863 same mistakes that they once made and overcame.  (Besides, since
864 you're already learning Autoconf, Automake is a piece of cake.)
866 There are a number of places that you can go to for more information on
867 the @acronym{GNU} build tools.
869 @itemize @minus
871 @item Web
873 The home pages for
874 @uref{http://www.gnu.org/@/software/@/autoconf/, Autoconf},
875 @uref{http://www.gnu.org/@/software/@/automake/, Automake},
876 @uref{http://www.gnu.org/@/software/@/gnulib/, Gnulib}, and
877 @uref{http://www.gnu.org/@/software/@/libtool/, Libtool}.
879 @item Automake Manual
881 @xref{Top, , Automake, automake, @acronym{GNU} Automake}, for more
882 information on Automake.
884 @item Books
886 The book @cite{@acronym{GNU} Autoconf, Automake and
887 Libtool}@footnote{@cite{@acronym{GNU} Autoconf, Automake and Libtool},
888 by G. V. Vaughan, B. Elliston, T. Tromey, and I. L. Taylor.  SAMS (originally
889 New Riders), 2000, ISBN 1578701902.} describes the complete @acronym{GNU}
890 build environment.  You can also find
891 @uref{http://sources.redhat.com/@/autobook/, the entire book on-line}.
893 @end itemize
895 @c ================================================= Making configure Scripts.
897 @node Making configure Scripts
898 @chapter Making @command{configure} Scripts
899 @cindex @file{aclocal.m4}
900 @cindex @command{configure}
902 The configuration scripts that Autoconf produces are by convention
903 called @command{configure}.  When run, @command{configure} creates several
904 files, replacing configuration parameters in them with appropriate
905 values.  The files that @command{configure} creates are:
907 @itemize @minus
908 @item
909 one or more @file{Makefile} files, usually one in each subdirectory of the
910 package (@pxref{Makefile Substitutions});
912 @item
913 optionally, a C header file, the name of which is configurable,
914 containing @code{#define} directives (@pxref{Configuration Headers});
916 @item
917 a shell script called @file{config.status} that, when run, recreates
918 the files listed above (@pxref{config.status Invocation});
920 @item
921 an optional shell script normally called @file{config.cache}
922 (created when using @samp{configure --config-cache}) that
923 saves the results of running many of the tests (@pxref{Cache Files});
925 @item
926 a file called @file{config.log} containing any messages produced by
927 compilers, to help debugging if @command{configure} makes a mistake.
928 @end itemize
930 @cindex @file{configure.in}
931 @cindex @file{configure.ac}
932 To create a @command{configure} script with Autoconf, you need to write an
933 Autoconf input file @file{configure.ac} (or @file{configure.in}) and run
934 @command{autoconf} on it.  If you write your own feature tests to
935 supplement those that come with Autoconf, you might also write files
936 called @file{aclocal.m4} and @file{acsite.m4}.  If you use a C header
937 file to contain @code{#define} directives, you might also run
938 @command{autoheader}, and you can distribute the generated file
939 @file{config.h.in} with the package.
941 Here is a diagram showing how the files that can be used in
942 configuration are produced.  Programs that are executed are suffixed by
943 @samp{*}.  Optional files are enclosed in square brackets (@samp{[]}).
944 @command{autoconf} and @command{autoheader} also read the installed Autoconf
945 macro files (by reading @file{autoconf.m4}).
947 @noindent
948 Files used in preparing a software package for distribution:
949 @example
950 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
952 @group
953 configure.ac --.
954                |   .------> autoconf* -----> configure
955 [aclocal.m4] --+---+
956                |   `-----> [autoheader*] --> [config.h.in]
957 [acsite.m4] ---'
958 @end group
960 Makefile.in -------------------------------> Makefile.in
961 @end example
963 @noindent
964 Files used in configuring a software package:
965 @example
966 @group
967                        .-------------> [config.cache]
968 configure* ------------+-------------> config.log
969                        |
970 [config.h.in] -.       v            .-> [config.h] -.
971                +--> config.status* -+               +--> make*
972 Makefile.in ---'                    `-> Makefile ---'
973 @end group
974 @end example
976 @menu
977 * Writing Autoconf Input::      What to put in an Autoconf input file
978 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
979 * ifnames Invocation::          Listing the conditionals in source code
980 * autoconf Invocation::         How to create configuration scripts
981 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
982 @end menu
984 @node Writing Autoconf Input
985 @section Writing @file{configure.ac}
987 To produce a @command{configure} script for a software package, create a
988 file called @file{configure.ac} that contains invocations of the
989 Autoconf macros that test the system features your package needs or can
990 use.  Autoconf macros already exist to check for many features; see
991 @ref{Existing Tests}, for their descriptions.  For most other features,
992 you can use Autoconf template macros to produce custom checks; see
993 @ref{Writing Tests}, for information about them.  For especially tricky
994 or specialized features, @file{configure.ac} might need to contain some
995 hand-crafted shell commands; see @ref{Portable Shell}.  The
996 @command{autoscan} program can give you a good start in writing
997 @file{configure.ac} (@pxref{autoscan Invocation}, for more information).
999 Previous versions of Autoconf promoted the name @file{configure.in},
1000 which is somewhat ambiguous (the tool needed to process this file is not
1001 described by its extension), and introduces a slight confusion with
1002 @file{config.h.in} and so on (for which @samp{.in} means ``to be
1003 processed by @command{configure}'').  Using @file{configure.ac} is now
1004 preferred.
1006 @menu
1007 * Shell Script Compiler::       Autoconf as solution of a problem
1008 * Autoconf Language::           Programming in Autoconf
1009 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
1010 @end menu
1012 @node Shell Script Compiler
1013 @subsection A Shell Script Compiler
1015 Just as for any other computer language, in order to properly program
1016 @file{configure.ac} in Autoconf you must understand @emph{what} problem
1017 the language tries to address and @emph{how} it does so.
1019 The problem Autoconf addresses is that the world is a mess.  After all,
1020 you are using Autoconf in order to have your package compile easily on
1021 all sorts of different systems, some of them being extremely hostile.
1022 Autoconf itself bears the price for these differences: @command{configure}
1023 must run on all those systems, and thus @command{configure} must limit itself
1024 to their lowest common denominator of features.
1026 Naturally, you might then think of shell scripts; who needs
1027 @command{autoconf}?  A set of properly written shell functions is enough to
1028 make it easy to write @command{configure} scripts by hand.  Sigh!
1029 Unfortunately, shell functions do not belong to the least common
1030 denominator; therefore, where you would like to define a function and
1031 use it ten times, you would instead need to copy its body ten times.
1033 So, what is really needed is some kind of compiler, @command{autoconf},
1034 that takes an Autoconf program, @file{configure.ac}, and transforms it
1035 into a portable shell script, @command{configure}.
1037 How does @command{autoconf} perform this task?
1039 There are two obvious possibilities: creating a brand new language or
1040 extending an existing one.  The former option is attractive: all
1041 sorts of optimizations could easily be implemented in the compiler and
1042 many rigorous checks could be performed on the Autoconf program
1043 (e.g., rejecting any non-portable construct).  Alternatively, you can
1044 extend an existing language, such as the @code{sh} (Bourne shell)
1045 language.
1047 Autoconf does the latter: it is a layer on top of @code{sh}.  It was
1048 therefore most convenient to implement @command{autoconf} as a macro
1049 expander: a program that repeatedly performs @dfn{macro expansions} on
1050 text input, replacing macro calls with macro bodies and producing a pure
1051 @code{sh} script in the end.  Instead of implementing a dedicated
1052 Autoconf macro expander, it is natural to use an existing
1053 general-purpose macro language, such as M4, and implement the extensions
1054 as a set of M4 macros.
1057 @node Autoconf Language
1058 @subsection The Autoconf Language
1059 @cindex quotation
1061 The Autoconf language differs from many other computer
1062 languages because it treats actual code the same as plain text.  Whereas
1063 in C, for instance, data and instructions have different syntactic
1064 status, in Autoconf their status is rigorously the same.  Therefore, we
1065 need a means to distinguish literal strings from text to be expanded:
1066 quotation.
1068 When calling macros that take arguments, there must not be any white
1069 space between the macro name and the open parenthesis.  Arguments should
1070 be enclosed within the M4 quote characters @samp{[} and @samp{]}, and be
1071 separated by commas.  Any leading blanks or newlines in arguments are ignored,
1072 unless they are quoted.  You should always quote an argument that
1073 might contain a macro name, comma, parenthesis, or a leading blank or
1074 newline.  This rule applies recursively for every macro
1075 call, including macros called from other macros.
1077 For instance:
1079 @example
1080 AC_CHECK_HEADER([stdio.h],
1081                 [AC_DEFINE([HAVE_STDIO_H], [1],
1082                    [Define to 1 if you have <stdio.h>.])],
1083                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1084 @end example
1086 @noindent
1087 is quoted properly.  You may safely simplify its quotation to:
1089 @example
1090 AC_CHECK_HEADER([stdio.h],
1091                 [AC_DEFINE([HAVE_STDIO_H], 1,
1092                    [Define to 1 if you have <stdio.h>.])],
1093                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1094 @end example
1096 @noindent
1097 because @samp{1} cannot contain a macro call.  Here, the argument of
1098 @code{AC_MSG_ERROR} must be quoted; otherwise, its comma would be
1099 interpreted as an argument separator.  Also, the second and third arguments
1100 of @samp{AC_CHECK_HEADER} must be quoted, since they contain
1101 macro calls.  The three arguments @samp{HAVE_STDIO_H}, @samp{stdio.h},
1102 and @samp{Define to 1 if you have <stdio.h>.} do not need quoting, but
1103 if you unwisely defined a macro with a name like @samp{Define} or
1104 @samp{stdio} then they would need quoting.  Cautious Autoconf users
1105 would keep the quotes, but many Autoconf users find such precautions
1106 annoying, and would rewrite the example as follows:
1108 @example
1109 AC_CHECK_HEADER(stdio.h,
1110                 [AC_DEFINE(HAVE_STDIO_H, 1,
1111                    [Define to 1 if you have <stdio.h>.])],
1112                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1113 @end example
1115 @noindent
1116 This is safe, so long as you adopt good naming conventions and do not
1117 define macros with names like @samp{HAVE_STDIO_H}, @samp{stdio}, or
1118 @samp{h}.  Though it is also safe here to omit the quotes around
1119 @samp{Define to 1 if you have <stdio.h>.} this is not recommended, as
1120 message strings are more likely to inadvertently contain commas.
1122 The following example is wrong and dangerous, as it is underquoted:
1124 @example
1125 AC_CHECK_HEADER(stdio.h,
1126                 AC_DEFINE(HAVE_STDIO_H, 1,
1127                    Define to 1 if you have <stdio.h>.),
1128                 AC_MSG_ERROR([Sorry, can't do anything for you]))
1129 @end example
1131 In other cases, you may have to use text that also resembles a macro
1132 call.  You must quote that text even when it is not passed as a macro
1133 argument:
1135 @example
1136 echo "Hard rock was here!  --[AC_DC]"
1137 @end example
1139 @noindent
1140 which results in:
1142 @example
1143 echo "Hard rock was here!  --AC_DC"
1144 @end example
1146 @noindent
1147 When you use the same text in a macro argument, you must therefore have
1148 an extra quotation level (since one is stripped away by the macro
1149 substitution).  In general, then, it is a good idea to @emph{use double
1150 quoting for all literal string arguments}:
1152 @example
1153 AC_MSG_WARN([[AC_DC stinks  --Iron Maiden]])
1154 @end example
1156 You are now able to understand one of the constructs of Autoconf that
1157 has been continually misunderstood@dots{}  The rule of thumb is that
1158 @emph{whenever you expect macro expansion, expect quote expansion};
1159 i.e., expect one level of quotes to be lost.  For instance:
1161 @example
1162 AC_COMPILE_IFELSE([char b[10];], [], [AC_MSG_ERROR([you lose])])
1163 @end example
1165 @noindent
1166 is incorrect: here, the first argument of @code{AC_COMPILE_IFELSE} is
1167 @samp{char b[10];} and is expanded once, which results in
1168 @samp{char b10;}.  (There was an idiom common in Autoconf's past to
1169 address this issue via the M4 @code{changequote} primitive, but do not
1170 use it!)  Let's take a closer look: the author meant the first argument
1171 to be understood as a literal, and therefore it must be quoted twice:
1173 @example
1174 AC_COMPILE_IFELSE([[char b[10];]], [], [AC_MSG_ERROR([you lose])])
1175 @end example
1177 @noindent
1178 Voil@`a, you actually produce @samp{char b[10];} this time!
1180 On the other hand, descriptions (e.g., the last parameter of
1181 @code{AC_DEFINE} or @code{AS_HELP_STRING}) are not literals---they
1182 are subject to line breaking, for example---and should not be double quoted.
1183 Even if these descriptions are short and are not actually broken, double
1184 quoting them yields weird results.
1186 Some macros take optional arguments, which this documentation represents
1187 as @ovar{arg} (not to be confused with the quote characters).  You may
1188 just leave them empty, or use @samp{[]} to make the emptiness of the
1189 argument explicit, or you may simply omit the trailing commas.  The
1190 three lines below are equivalent:
1192 @example
1193 AC_CHECK_HEADERS([stdio.h], [], [], [])
1194 AC_CHECK_HEADERS([stdio.h],,,)
1195 AC_CHECK_HEADERS([stdio.h])
1196 @end example
1198 It is best to put each macro call on its own line in
1199 @file{configure.ac}.  Most of the macros don't add extra newlines; they
1200 rely on the newline after the macro call to terminate the commands.
1201 This approach makes the generated @command{configure} script a little
1202 easier to read by not inserting lots of blank lines.  It is generally
1203 safe to set shell variables on the same line as a macro call, because
1204 the shell allows assignments without intervening newlines.
1206 You can include comments in @file{configure.ac} files by starting them
1207 with the @samp{#}.  For example, it is helpful to begin
1208 @file{configure.ac} files with a line like this:
1210 @example
1211 # Process this file with autoconf to produce a configure script.
1212 @end example
1214 @node Autoconf Input Layout
1215 @subsection Standard @file{configure.ac} Layout
1217 The order in which @file{configure.ac} calls the Autoconf macros is not
1218 important, with a few exceptions.  Every @file{configure.ac} must
1219 contain a call to @code{AC_INIT} before the checks, and a call to
1220 @code{AC_OUTPUT} at the end (@pxref{Output}).  Additionally, some macros
1221 rely on other macros having been called first, because they check
1222 previously set values of some variables to decide what to do.  These
1223 macros are noted in the individual descriptions (@pxref{Existing
1224 Tests}), and they also warn you when @command{configure} is created if they
1225 are called out of order.
1227 To encourage consistency, here is a suggested order for calling the
1228 Autoconf macros.  Generally speaking, the things near the end of this
1229 list are those that could depend on things earlier in it.  For example,
1230 library functions could be affected by types and libraries.
1232 @display
1233 @group
1234 Autoconf requirements
1235 @code{AC_INIT(@var{package}, @var{version}, @var{bug-report-address})}
1236 information on the package
1237 checks for programs
1238 checks for libraries
1239 checks for header files
1240 checks for types
1241 checks for structures
1242 checks for compiler characteristics
1243 checks for library functions
1244 checks for system services
1245 @code{AC_CONFIG_FILES(@r{[}@var{file@dots{}}@r{]})}
1246 @code{AC_OUTPUT}
1247 @end group
1248 @end display
1251 @node autoscan Invocation
1252 @section Using @command{autoscan} to Create @file{configure.ac}
1253 @cindex @command{autoscan}
1255 The @command{autoscan} program can help you create and/or maintain a
1256 @file{configure.ac} file for a software package.  @command{autoscan}
1257 examines source files in the directory tree rooted at a directory given
1258 as a command line argument, or the current directory if none is given.
1259 It searches the source files for common portability problems and creates
1260 a file @file{configure.scan} which is a preliminary @file{configure.ac}
1261 for that package, and checks a possibly existing @file{configure.ac} for
1262 completeness.
1264 When using @command{autoscan} to create a @file{configure.ac}, you
1265 should manually examine @file{configure.scan} before renaming it to
1266 @file{configure.ac}; it probably needs some adjustments.
1267 Occasionally, @command{autoscan} outputs a macro in the wrong order
1268 relative to another macro, so that @command{autoconf} produces a warning;
1269 you need to move such macros manually.  Also, if you want the package to
1270 use a configuration header file, you must add a call to
1271 @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers}).  You might
1272 also have to change or add some @code{#if} directives to your program in
1273 order to make it work with Autoconf (@pxref{ifnames Invocation}, for
1274 information about a program that can help with that job).
1276 When using @command{autoscan} to maintain a @file{configure.ac}, simply
1277 consider adding its suggestions.  The file @file{autoscan.log}
1278 contains detailed information on why a macro is requested.
1280 @command{autoscan} uses several data files (installed along with Autoconf)
1281 to determine which macros to output when it finds particular symbols in
1282 a package's source files.  These data files all have the same format:
1283 each line consists of a symbol, one or more blanks, and the Autoconf macro to
1284 output if that symbol is encountered.  Lines starting with @samp{#} are
1285 comments.
1287 @command{autoscan} accepts the following options:
1289 @table @option
1290 @item --help
1291 @itemx -h
1292 Print a summary of the command line options and exit.
1294 @item --version
1295 @itemx -V
1296 Print the version number of Autoconf and exit.
1298 @item --verbose
1299 @itemx -v
1300 Print the names of the files it examines and the potentially interesting
1301 symbols it finds in them.  This output can be voluminous.
1303 @item --include=@var{dir}
1304 @itemx -I @var{dir}
1305 Append @var{dir} to the include path.  Multiple invocations accumulate.
1307 @item --prepend-include=@var{dir}
1308 @item -B @var{dir}
1309 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1310 @end table
1312 @node ifnames Invocation
1313 @section Using @command{ifnames} to List Conditionals
1314 @cindex @command{ifnames}
1316 @command{ifnames} can help you write @file{configure.ac} for a software
1317 package.  It prints the identifiers that the package already uses in C
1318 preprocessor conditionals.  If a package has already been set up to have
1319 some portability, @command{ifnames} can thus help you figure out what its
1320 @command{configure} needs to check for.  It may help fill in some gaps in a
1321 @file{configure.ac} generated by @command{autoscan} (@pxref{autoscan
1322 Invocation}).
1324 @command{ifnames} scans all of the C source files named on the command line
1325 (or the standard input, if none are given) and writes to the standard
1326 output a sorted list of all the identifiers that appear in those files
1327 in @code{#if}, @code{#elif}, @code{#ifdef}, or @code{#ifndef}
1328 directives.  It prints each identifier on a line, followed by a
1329 space-separated list of the files in which that identifier occurs.
1331 @noindent
1332 @command{ifnames} accepts the following options:
1334 @table @option
1335 @item --help
1336 @itemx -h
1337 Print a summary of the command line options and exit.
1339 @item --version
1340 @itemx -V
1341 Print the version number of Autoconf and exit.
1342 @end table
1344 @node autoconf Invocation
1345 @section Using @command{autoconf} to Create @command{configure}
1346 @cindex @command{autoconf}
1348 To create @command{configure} from @file{configure.ac}, run the
1349 @command{autoconf} program with no arguments.  @command{autoconf} processes
1350 @file{configure.ac} with the M4 macro processor, using the
1351 Autoconf macros.  If you give @command{autoconf} an argument, it reads that
1352 file instead of @file{configure.ac} and writes the configuration script
1353 to the standard output instead of to @command{configure}.  If you give
1354 @command{autoconf} the argument @option{-}, it reads from the standard
1355 input instead of @file{configure.ac} and writes the configuration script
1356 to the standard output.
1358 The Autoconf macros are defined in several files.  Some of the files are
1359 distributed with Autoconf; @command{autoconf} reads them first.  Then it
1360 looks for the optional file @file{acsite.m4} in the directory that
1361 contains the distributed Autoconf macro files, and for the optional file
1362 @file{aclocal.m4} in the current directory.  Those files can contain
1363 your site's or the package's own Autoconf macro definitions
1364 (@pxref{Writing Autoconf Macros}, for more information).  If a macro is
1365 defined in more than one of the files that @command{autoconf} reads, the
1366 last definition it reads overrides the earlier ones.
1368 @command{autoconf} accepts the following options:
1370 @table @option
1371 @item --help
1372 @itemx -h
1373 Print a summary of the command line options and exit.
1375 @item --version
1376 @itemx -V
1377 Print the version number of Autoconf and exit.
1379 @item --verbose
1380 @itemx -v
1381 Report processing steps.
1383 @item --debug
1384 @itemx -d
1385 Don't remove the temporary files.
1387 @item --force
1388 @itemx -f
1389 Remake @file{configure} even if newer than its input files.
1391 @item --include=@var{dir}
1392 @itemx -I @var{dir}
1393 Append @var{dir} to the include path.  Multiple invocations accumulate.
1395 @item --prepend-include=@var{dir}
1396 @item -B @var{dir}
1397 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1399 @item --output=@var{file}
1400 @itemx -o @var{file}
1401 Save output (script or trace) to @var{file}.  The file @option{-} stands
1402 for the standard output.
1404 @item --warnings=@var{category}
1405 @itemx -W @var{category}
1406 @evindex WARNINGS
1407 Report the warnings related to @var{category} (which can actually be a
1408 comma separated list).  @xref{Reporting Messages}, macro
1409 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
1410 values include:
1412 @table @samp
1413 @item all
1414 report all the warnings
1416 @item none
1417 report none
1419 @item error
1420 treats warnings as errors
1422 @item no-@var{category}
1423 disable warnings falling into @var{category}
1424 @end table
1426 Warnings about @samp{syntax} are enabled by default, and the environment
1427 variable @env{WARNINGS}, a comma separated list of categories, is
1428 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1429 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  If
1430 you want to disable the defaults and @env{WARNINGS}, but (for example)
1431 enable the warnings about obsolete constructs, you would use @option{-W
1432 none,obsolete}.
1434 @cindex Back trace
1435 @cindex Macro invocation stack
1436 Because @command{autoconf} uses @command{autom4te} behind the scenes, it
1437 displays a back trace for errors, but not for warnings; if you want
1438 them, just pass @option{-W error}.  @xref{autom4te Invocation}, for some
1439 examples.
1441 @item --trace=@var{macro}[:@var{format}]
1442 @itemx -t @var{macro}[:@var{format}]
1443 Do not create the @command{configure} script, but list the calls to
1444 @var{macro} according to the @var{format}.  Multiple @option{--trace}
1445 arguments can be used to list several macros.  Multiple @option{--trace}
1446 arguments for a single macro are not cumulative; instead, you should
1447 just make @var{format} as long as needed.
1449 The @var{format} is a regular string, with newlines if desired, and
1450 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}; see
1451 @ref{autom4te Invocation}, for details on the @var{format}.
1453 @item --initialization
1454 @itemx -i
1455 By default, @option{--trace} does not trace the initialization of the
1456 Autoconf macros (typically the @code{AC_DEFUN} definitions).  This
1457 results in a noticeable speedup, but can be disabled by this option.
1458 @end table
1461 It is often necessary to check the content of a @file{configure.ac}
1462 file, but parsing it yourself is extremely fragile and error-prone.  It
1463 is suggested that you rely upon @option{--trace} to scan
1464 @file{configure.ac}.  For instance, to find the list of variables that
1465 are substituted, use:
1467 @example
1468 @group
1469 $ @kbd{autoconf -t AC_SUBST}
1470 configure.ac:2:AC_SUBST:ECHO_C
1471 configure.ac:2:AC_SUBST:ECHO_N
1472 configure.ac:2:AC_SUBST:ECHO_T
1473 @i{More traces deleted}
1474 @end group
1475 @end example
1477 @noindent
1478 The example below highlights the difference between @samp{$@@},
1479 @samp{$*}, and @samp{$%}.
1481 @example
1482 @group
1483 $ @kbd{cat configure.ac}
1484 AC_DEFINE(This, is, [an
1485 [example]])
1486 $ @kbd{autoconf -t 'AC_DEFINE:@@: $@@}
1487 *: $*
1488 %: $%'
1489 @@: [This],[is],[an
1490 [example]]
1491 *: This,is,an
1492 [example]
1493 %: This:is:an [example]
1494 @end group
1495 @end example
1497 @noindent
1498 The @var{format} gives you a lot of freedom:
1500 @example
1501 @group
1502 $ @kbd{autoconf -t 'AC_SUBST:$$ac_subst@{"$1"@} = "$f:$l";'}
1503 $ac_subst@{"ECHO_C"@} = "configure.ac:2";
1504 $ac_subst@{"ECHO_N"@} = "configure.ac:2";
1505 $ac_subst@{"ECHO_T"@} = "configure.ac:2";
1506 @i{More traces deleted}
1507 @end group
1508 @end example
1510 @noindent
1511 A long @var{separator} can be used to improve the readability of complex
1512 structures, and to ease their parsing (for instance when no single
1513 character is suitable as a separator):
1515 @example
1516 @group
1517 $ @kbd{autoconf -t 'AM_MISSING_PROG:$@{|:::::|@}*'}
1518 ACLOCAL|:::::|aclocal|:::::|$missing_dir
1519 AUTOCONF|:::::|autoconf|:::::|$missing_dir
1520 AUTOMAKE|:::::|automake|:::::|$missing_dir
1521 @i{More traces deleted}
1522 @end group
1523 @end example
1525 @node autoreconf Invocation
1526 @section Using @command{autoreconf} to Update @command{configure} Scripts
1527 @cindex @command{autoreconf}
1529 Installing the various components of the @acronym{GNU} Build System can be
1530 tedious: running @command{autopoint} for Gettext, @command{automake} for
1531 @file{Makefile.in} etc.@: in each directory.  It may be needed either
1532 because some tools such as @command{automake} have been updated on your
1533 system, or because some of the sources such as @file{configure.ac} have
1534 been updated, or finally, simply in order to install the @acronym{GNU} Build
1535 System in a fresh tree.
1537 @command{autoreconf} runs @command{autoconf}, @command{autoheader},
1538 @command{aclocal}, @command{automake}, @command{libtoolize}, and
1539 @command{autopoint} (when appropriate) repeatedly to update the
1540 @acronym{GNU} Build System in the specified directories and their
1541 subdirectories (@pxref{Subdirectories}).  By default, it only remakes
1542 those files that are older than their sources.
1544 If you install a new version of some tool, you can make
1545 @command{autoreconf} remake @emph{all} of the files by giving it the
1546 @option{--force} option.
1548 @xref{Automatic Remaking}, for Make rules to automatically
1549 remake @command{configure} scripts when their source files change.  That
1550 method handles the timestamps of configuration header templates
1551 properly, but does not pass @option{--autoconf-dir=@var{dir}} or
1552 @option{--localdir=@var{dir}}.
1554 @cindex Gettext
1555 @cindex @command{autopoint}
1556 Gettext supplies the @command{autopoint} command to add translation
1557 infrastructure to a source package.  If you use @command{autopoint},
1558 your @file{configure.ac} should invoke both @code{AM_GNU_GETTEXT} and
1559 @code{AM_GNU_GETTEXT_VERSION(@var{gettext-version})}.  @xref{autopoint
1560 Invocation, , Invoking the @code{autopoint} Program, gettext,
1561 @acronym{GNU} @code{gettext} utilities}, for further details.
1563 @noindent
1564 @command{autoreconf} accepts the following options:
1566 @table @option
1567 @item --help
1568 @itemx -h
1569 Print a summary of the command line options and exit.
1571 @item --version
1572 @itemx -V
1573 Print the version number of Autoconf and exit.
1575 @item --verbose
1576 Print the name of each directory @command{autoreconf} examines and the
1577 commands it runs.  If given two or more times, pass @option{--verbose}
1578 to subordinate tools that support it.
1580 @item --debug
1581 @itemx -d
1582 Don't remove the temporary files.
1584 @item --force
1585 @itemx -f
1586 Remake even @file{configure} scripts and configuration headers that are
1587 newer than their input files (@file{configure.ac} and, if present,
1588 @file{aclocal.m4}).
1590 @item --install
1591 @itemx -i
1592 Install the missing auxiliary files in the package.  By default, files
1593 are copied; this can be changed with @option{--symlink}.
1595 If deemed appropriate, this option triggers calls to
1596 @samp{automake --add-missing},
1597 @samp{libtoolize}, @samp{autopoint}, etc.
1599 @item --no-recursive
1600 Do not rebuild files in subdirectories to configure (see @ref{Subdirectories},
1601 macro @code{AC_CONFIG_SUBDIRS}).
1603 @item --symlink
1604 @itemx -s
1605 When used with @option{--install}, install symbolic links to the missing
1606 auxiliary files instead of copying them.
1608 @item --make
1609 @itemx -m
1610 When the directories were configured, update the configuration by
1611 running @samp{./config.status --recheck && ./config.status}, and then
1612 run @samp{make}.
1614 @item --include=@var{dir}
1615 @itemx -I @var{dir}
1616 Append @var{dir} to the include path.  Multiple invocations accumulate.
1617 Passed on to @command{autoconf} and @command{autoheader} internally.
1619 @item --prepend-include=@var{dir}
1620 @item -B @var{dir}
1621 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1622 Passed on to @command{autoconf} and @command{autoheader} internally.
1624 @item --warnings=@var{category}
1625 @itemx -W @var{category}
1626 @evindex WARNINGS
1627 Report the warnings related to @var{category} (which can actually be a
1628 comma separated list).
1630 @table @samp
1631 @item cross
1632 related to cross compilation issues.
1634 @item obsolete
1635 report the uses of obsolete constructs.
1637 @item portability
1638 portability issues
1640 @item syntax
1641 dubious syntactic constructs.
1643 @item all
1644 report all the warnings
1646 @item none
1647 report none
1649 @item error
1650 treats warnings as errors
1652 @item no-@var{category}
1653 disable warnings falling into @var{category}
1654 @end table
1656 Warnings about @samp{syntax} are enabled by default, and the environment
1657 variable @env{WARNINGS}, a comma separated list of categories, is
1658 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1659 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  If
1660 you want to disable the defaults and @env{WARNINGS}, but (for example)
1661 enable the warnings about obsolete constructs, you would use @option{-W
1662 none,obsolete}.
1663 @end table
1665 If you want @command{autoreconf} to pass flags that are not listed here
1666 on to @command{aclocal}, set @code{ACLOCAL_AMFLAGS} in your @file{Makefile.am}.
1667 Due to a limitation in the Autoconf implementation these flags currently
1668 must be set on a single line in @file{Makefile.am}, without any
1669 backslash-newlines.
1671 @c ========================================= Initialization and Output Files.
1673 @node Setup
1674 @chapter Initialization and Output Files
1676 Autoconf-generated @command{configure} scripts need some information about
1677 how to initialize, such as how to find the package's source files and
1678 about the output files to produce.  The following sections describe the
1679 initialization and the creation of output files.
1681 @menu
1682 * Initializing configure::      Option processing etc.
1683 * Versioning::                  Dealing with Autoconf versions
1684 * Notices::                     Copyright, version numbers in @command{configure}
1685 * Input::                       Where Autoconf should find files
1686 * Output::                      Outputting results from the configuration
1687 * Configuration Actions::       Preparing the output based on results
1688 * Configuration Files::         Creating output files
1689 * Makefile Substitutions::      Using output variables in makefiles
1690 * Configuration Headers::       Creating a configuration header file
1691 * Configuration Commands::      Running arbitrary instantiation commands
1692 * Configuration Links::         Links depending on the configuration
1693 * Subdirectories::              Configuring independent packages together
1694 * Default Prefix::              Changing the default installation prefix
1695 @end menu
1697 @node Initializing configure
1698 @section Initializing @command{configure}
1700 Every @command{configure} script must call @code{AC_INIT} before doing
1701 anything else.  The only other required macro is @code{AC_OUTPUT}
1702 (@pxref{Output}).
1704 @anchor{AC_INIT}
1705 @defmac AC_INIT (@var{package}, @var{version}, @ovar{bug-report}, @
1706   @ovar{tarname})
1707 @acindex{INIT}
1708 Process any command-line arguments and perform various initializations
1709 and verifications.
1711 Set the name of the @var{package} and its @var{version}.  These are
1712 typically used in @option{--version} support, including that of
1713 @command{configure}.  The optional argument @var{bug-report} should be
1714 the email to which users should send bug reports.  The package
1715 @var{tarname} differs from @var{package}: the latter designates the full
1716 package name (e.g., @samp{GNU Autoconf}), while the former is meant for
1717 distribution tar ball names (e.g., @samp{autoconf}).  It defaults to
1718 @var{package} with @samp{GNU } stripped, lower-cased, and all characters
1719 other than alphanumerics and underscores are changed to @samp{-}.
1721 It is preferable that the arguments of @code{AC_INIT} be static, i.e.,
1722 there should not be any shell computation, but they can be computed by
1725 The following M4 macros (e.g., @code{AC_PACKAGE_NAME}), output variables
1726 (e.g., @code{PACKAGE_NAME}), and preprocessor symbols (e.g.,
1727 @code{PACKAGE_NAME}), are defined by @code{AC_INIT}:
1729 @table @asis
1730 @item @code{AC_PACKAGE_NAME}, @code{PACKAGE_NAME}
1731 @acindex{PACKAGE_NAME}
1732 @ovindex PACKAGE_NAME
1733 @cvindex PACKAGE_NAME
1734 Exactly @var{package}.
1736 @item @code{AC_PACKAGE_TARNAME}, @code{PACKAGE_TARNAME}
1737 @acindex{PACKAGE_TARNAME}
1738 @ovindex PACKAGE_TARNAME
1739 @cvindex PACKAGE_TARNAME
1740 Exactly @var{tarname}.
1742 @item @code{AC_PACKAGE_VERSION}, @code{PACKAGE_VERSION}
1743 @acindex{PACKAGE_VERSION}
1744 @ovindex PACKAGE_VERSION
1745 @cvindex PACKAGE_VERSION
1746 Exactly @var{version}.
1748 @item @code{AC_PACKAGE_STRING}, @code{PACKAGE_STRING}
1749 @acindex{PACKAGE_STRING}
1750 @ovindex PACKAGE_STRING
1751 @cvindex PACKAGE_STRING
1752 Exactly @samp{@var{package} @var{version}}.
1754 @item @code{AC_PACKAGE_BUGREPORT}, @code{PACKAGE_BUGREPORT}
1755 @acindex{PACKAGE_BUGREPORT}
1756 @ovindex PACKAGE_BUGREPORT
1757 @cvindex PACKAGE_BUGREPORT
1758 Exactly @var{bug-report}.
1759 @end table
1760 @end defmac
1762 If your @command{configure} script does its own option processing, it
1763 should inspect @samp{$@@} or @samp{$*} immediately after calling
1764 @code{AC_INIT}, because other Autoconf macros liberally use the
1765 @command{set} command to process strings, and this has the side effect
1766 of updating @samp{$@@} and @samp{$*}.  However, we suggest that you use
1767 standard macros like @code{AC_ARG_ENABLE} instead of attempting to
1768 implement your own option processing.  @xref{Site Configuration}.
1770 @node Versioning
1771 @section Dealing with Autoconf versions
1772 @cindex Autoconf version
1773 @cindex version, Autoconf
1775 The following optional macros can be used to help choose the minimum
1776 version of Autoconf that can successfully compile a given
1777 @file{configure.ac}.
1779 @defmac AC_PREREQ (@var{version})
1780 @acindex{PREREQ}
1781 @cindex Version
1782 Ensure that a recent enough version of Autoconf is being used.  If the
1783 version of Autoconf being used to create @command{configure} is
1784 earlier than @var{version}, print an error message to the standard
1785 error output and exit with failure (exit status is 63).  For example:
1787 @example
1788 AC_PREREQ([@value{VERSION}])
1789 @end example
1791 This macro is the only macro that may be used before @code{AC_INIT}, but
1792 for consistency, you are invited not to do so.
1793 @end defmac
1795 @defmac AC_VERSION
1796 @acindex{VERSION}
1797 This macro was introduced in Autoconf 2.62.  It identifies the version
1798 of Autoconf that is currently parsing the input file, in a format
1799 suitable for @code{m4_version_compare} (@pxref{m4_version_compare}); in
1800 other words, for this release of Autoconf, its value is
1801 @samp{@value{VERSION}}.  One potential use of this macro is for writing
1802 conditional fallbacks based on when a feature was added to Autoconf,
1803 rather than using @code{AC_PREREQ} to require the newer version of
1804 Autoconf.  However, remember that the Autoconf philosophy favors feature
1805 checks over version checks.
1806 @end defmac
1808 @node Notices
1809 @section Notices in @command{configure}
1810 @cindex Notices in @command{configure}
1812 The following macros manage version numbers for @command{configure}
1813 scripts.  Using them is optional.
1815 @defmac AC_COPYRIGHT (@var{copyright-notice})
1816 @acindex{COPYRIGHT}
1817 @cindex Copyright Notice
1818 State that, in addition to the Free Software Foundation's copyright on
1819 the Autoconf macros, parts of your @command{configure} are covered by the
1820 @var{copyright-notice}.
1822 The @var{copyright-notice} shows up in both the head of
1823 @command{configure} and in @samp{configure --version}.
1824 @end defmac
1827 @defmac AC_REVISION (@var{revision-info})
1828 @acindex{REVISION}
1829 @cindex Revision
1830 Copy revision stamp @var{revision-info} into the @command{configure}
1831 script, with any dollar signs or double-quotes removed.  This macro lets
1832 you put a revision stamp from @file{configure.ac} into @command{configure}
1833 without @acronym{RCS} or @acronym{CVS} changing it when you check in
1834 @command{configure}.  That way, you can determine easily which revision of
1835 @file{configure.ac} a particular @command{configure} corresponds to.
1837 For example, this line in @file{configure.ac}:
1839 @c The asis prevents RCS from changing the example in the manual.
1840 @example
1841 AC_REVISION([$@asis{Revision: 1.30 }$])
1842 @end example
1844 @noindent
1845 produces this in @command{configure}:
1847 @example
1848 #!/bin/sh
1849 # From configure.ac Revision: 1.30
1850 @end example
1851 @end defmac
1854 @node Input
1855 @section Finding @command{configure} Input
1857 @anchor{AC_CONFIG_SRCDIR}
1858 @defmac AC_CONFIG_SRCDIR (@var{unique-file-in-source-dir})
1859 @acindex{CONFIG_SRCDIR}
1860 @var{unique-file-in-source-dir} is some file that is in the package's
1861 source directory; @command{configure} checks for this file's existence to
1862 make sure that the directory that it is told contains the source code in
1863 fact does.  Occasionally people accidentally specify the wrong directory
1864 with @option{--srcdir}; this is a safety check.  @xref{configure
1865 Invocation}, for more information.
1866 @end defmac
1869 @c FIXME: Remove definitively once --install explained.
1871 @c Small packages may store all their macros in @code{aclocal.m4}.  As the
1872 @c set of macros grows, or for maintenance reasons, a maintainer may prefer
1873 @c to split the macros in several files.  In this case, Autoconf must be
1874 @c told which files to load, and in which order.
1876 @c @defmac AC_INCLUDE (@var{file}@dots{})
1877 @c @acindex{INCLUDE}
1878 @c @c FIXME: There is no longer shell globbing.
1879 @c Read the macro definitions that appear in the listed files.  A list of
1880 @c space-separated file names or shell globbing patterns is expected.  The
1881 @c files are read in the order they're listed.
1883 @c Because the order of definition of macros is important (only the last
1884 @c definition of a macro is used), beware that it is @code{AC_INIT} that
1885 @c loads @file{acsite.m4} and @file{aclocal.m4}.  Note that
1886 @c @code{AC_INCLUDE}ing a file before @code{AC_INIT} or within
1887 @c @file{aclocal.m4} is different from doing so after @code{AC_INIT}: in
1888 @c the latter case, non-macro lines from included files may end up in the
1889 @c @file{configure} script, whereas in the former case, they'd be discarded
1890 @c just like any text that appear before @code{AC_INIT}.
1891 @c @end defmac
1893 Packages that do manual configuration or use the @command{install} program
1894 might need to tell @command{configure} where to find some other shell
1895 scripts by calling @code{AC_CONFIG_AUX_DIR}, though the default places
1896 it looks are correct for most cases.
1898 @defmac AC_CONFIG_AUX_DIR (@var{dir})
1899 @acindex{CONFIG_AUX_DIR}
1900 Use the auxiliary build tools (e.g., @file{install-sh},
1901 @file{config.sub}, @file{config.guess}, Cygnus @command{configure},
1902 Automake and Libtool scripts, etc.)@: that are in directory @var{dir}.
1903 These are auxiliary files used in configuration.  @var{dir} can be
1904 either absolute or relative to @file{@var{srcdir}}.  The default is
1905 @file{@var{srcdir}} or @file{@var{srcdir}/..} or
1906 @file{@var{srcdir}/../..}, whichever is the first that contains
1907 @file{install-sh}.  The other files are not checked for, so that using
1908 @code{AC_PROG_INSTALL} does not automatically require distributing the
1909 other auxiliary files.  It checks for @file{install.sh} also, but that
1910 name is obsolete because some @code{make} have a rule that creates
1911 @file{install} from it if there is no makefile.
1913 The auxiliary directory is commonly named @file{build-aux}.
1914 If you need portability to @acronym{DOS} variants, do not name the
1915 auxiliary directory @file{aux}.  @xref{File System Conventions}.
1916 @end defmac
1918 @defmac AC_REQUIRE_AUX_FILE (@var{file})
1919 @acindex{REQUIRE_AUX_FILE}
1920 Declares that @var{file} is expected in the directory defined above.  In
1921 Autoconf proper, this macro does nothing: its sole purpose is to be
1922 traced by third-party tools to produce a list of expected auxiliary
1923 files.  For instance it is called by macros like @code{AC_PROG_INSTALL}
1924 (@pxref{Particular Programs}) or @code{AC_CANONICAL_BUILD}
1925 (@pxref{Canonicalizing}) to register the auxiliary files they need.
1926 @end defmac
1928 Similarly, packages that use @command{aclocal} should declare where
1929 local macros can be found using @code{AC_CONFIG_MACRO_DIR}.
1931 @defmac AC_CONFIG_MACRO_DIR (@var{dir})
1932 @acindex{CONFIG_MACRO_DIR}
1933 Specify @var{dir} as the location of additional local Autoconf macros.
1934 This macro is intended for use by future versions of commands like
1935 @command{autoreconf} that trace macro calls.  It should be called
1936 directly from @file{configure.ac} so that tools that install macros for
1937 @command{aclocal} can find the macros' declarations.
1938 @end defmac
1941 @node Output
1942 @section Outputting Files
1943 @cindex Outputting files
1945 Every Autoconf script, e.g., @file{configure.ac}, should finish by
1946 calling @code{AC_OUTPUT}.  That is the macro that generates and runs
1947 @file{config.status}, which in turn creates the makefiles and any
1948 other files resulting from configuration.  This is the only required
1949 macro besides @code{AC_INIT} (@pxref{Input}).
1951 @anchor{AC_OUTPUT}
1952 @defmac AC_OUTPUT
1953 @acindex{OUTPUT}
1954 @cindex Instantiation
1955 Generate @file{config.status} and launch it.  Call this macro once, at
1956 the end of @file{configure.ac}.
1958 @file{config.status} performs all the configuration actions: all the
1959 output files (see @ref{Configuration Files}, macro
1960 @code{AC_CONFIG_FILES}), header files (see @ref{Configuration Headers},
1961 macro @code{AC_CONFIG_HEADERS}), commands (see @ref{Configuration
1962 Commands}, macro @code{AC_CONFIG_COMMANDS}), links (see
1963 @ref{Configuration Links}, macro @code{AC_CONFIG_LINKS}), subdirectories
1964 to configure (see @ref{Subdirectories}, macro @code{AC_CONFIG_SUBDIRS})
1965 are honored.
1967 The location of your @code{AC_OUTPUT} invocation is the exact point
1968 where configuration actions are taken: any code afterwards is
1969 executed by @code{configure} once @command{config.status} was run.  If
1970 you want to bind actions to @command{config.status} itself
1971 (independently of whether @command{configure} is being run), see
1972 @ref{Configuration Commands, , Running Arbitrary Configuration
1973 Commands}.
1974 @end defmac
1976 Historically, the usage of @code{AC_OUTPUT} was somewhat different.
1977 @xref{Obsolete Macros}, for a description of the arguments that
1978 @code{AC_OUTPUT} used to support.
1981 If you run @command{make} in subdirectories, you should run it using the
1982 @code{make} variable @code{MAKE}.  Most versions of @command{make} set
1983 @code{MAKE} to the name of the @command{make} program plus any options it
1984 was given.  (But many do not include in it the values of any variables
1985 set on the command line, so those are not passed on automatically.)
1986 Some old versions of @command{make} do not set this variable.  The
1987 following macro allows you to use it even with those versions.
1989 @anchor{AC_PROG_MAKE_SET}
1990 @defmac AC_PROG_MAKE_SET
1991 @acindex{PROG_MAKE_SET}
1992 @ovindex SET_MAKE
1993 If the Make command, @code{$MAKE} if set or else @samp{make}, predefines
1994 @code{$(MAKE)}, define output variable @code{SET_MAKE} to be empty.
1995 Otherwise, define @code{SET_MAKE} to a macro definition that sets
1996 @code{$(MAKE)}, such as @samp{MAKE=make}.  Calls @code{AC_SUBST} for
1997 @code{SET_MAKE}.
1998 @end defmac
2000 If you use this macro, place a line like this in each @file{Makefile.in}
2001 that runs @code{MAKE} on other directories:
2003 @example
2004 @@SET_MAKE@@
2005 @end example
2009 @node Configuration Actions
2010 @section Performing Configuration Actions
2011 @cindex Configuration actions
2013 @file{configure} is designed so that it appears to do everything itself,
2014 but there is actually a hidden slave: @file{config.status}.
2015 @file{configure} is in charge of examining your system, but it is
2016 @file{config.status} that actually takes the proper actions based on the
2017 results of @file{configure}.  The most typical task of
2018 @file{config.status} is to @emph{instantiate} files.
2020 This section describes the common behavior of the four standard
2021 instantiating macros: @code{AC_CONFIG_FILES}, @code{AC_CONFIG_HEADERS},
2022 @code{AC_CONFIG_COMMANDS} and @code{AC_CONFIG_LINKS}.  They all
2023 have this prototype:
2025 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
2026 @c awful.
2027 @example
2028 AC_CONFIG_FOOS(@var{tag}@dots{}, [@var{commands}], [@var{init-cmds}])
2029 @end example
2031 @noindent
2032 where the arguments are:
2034 @table @var
2035 @item tag@dots{}
2036 A blank-or-newline-separated list of tags, which are typically the names of
2037 the files to instantiate.
2039 You are encouraged to use literals as @var{tags}.  In particular, you
2040 should avoid
2042 @example
2043 @dots{} && my_foos="$my_foos fooo"
2044 @dots{} && my_foos="$my_foos foooo"
2045 AC_CONFIG_FOOS([$my_foos])
2046 @end example
2048 @noindent
2049 and use this instead:
2051 @example
2052 @dots{} && AC_CONFIG_FOOS([fooo])
2053 @dots{} && AC_CONFIG_FOOS([foooo])
2054 @end example
2056 The macros @code{AC_CONFIG_FILES} and @code{AC_CONFIG_HEADERS} use
2057 special @var{tag} values: they may have the form @samp{@var{output}} or
2058 @samp{@var{output}:@var{inputs}}.  The file @var{output} is instantiated
2059 from its templates, @var{inputs} (defaulting to @samp{@var{output}.in}).
2061 @samp{AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk)]},
2062 for example, asks for
2063 the creation of the file @file{Makefile} that contains the expansion of the
2064 output variables in the concatenation of @file{boiler/top.mk} and
2065 @file{boiler/bot.mk}.
2067 The special value @samp{-} might be used to denote the standard output
2068 when used in @var{output}, or the standard input when used in the
2069 @var{inputs}.  You most probably don't need to use this in
2070 @file{configure.ac}, but it is convenient when using the command line
2071 interface of @file{./config.status}, see @ref{config.status Invocation},
2072 for more details.
2074 The @var{inputs} may be absolute or relative file names.  In the latter
2075 case they are first looked for in the build tree, and then in the source
2076 tree.
2078 @item commands
2079 Shell commands output literally into @file{config.status}, and
2080 associated with a tag that the user can use to tell @file{config.status}
2081 which the commands to run.  The commands are run each time a @var{tag}
2082 request is given to @file{config.status}, typically each time the file
2083 @file{@var{tag}} is created.
2085 The variables set during the execution of @command{configure} are
2086 @emph{not} available here: you first need to set them via the
2087 @var{init-cmds}.  Nonetheless the following variables are precomputed:
2089 @table @code
2090 @item srcdir
2091 The name of the top source directory, assuming that the working
2092 directory is the top build directory.  This
2093 is what the @command{configure} option @option{--srcdir} sets.
2095 @item ac_top_srcdir
2096 The name of the top source directory, assuming that the working
2097 directory is the current build directory.
2100 @item ac_top_build_prefix
2101 The name of the top build directory, assuming that the working
2102 directory is the current build directory.
2103 It can be empty, or else ends with a slash, so that you may concatenate
2106 @item ac_srcdir
2107 The name of the corresponding source directory, assuming that the
2108 working directory is the current build directory.
2109 @end table
2111 @noindent
2112 The @dfn{current} directory refers to the directory (or
2113 pseudo-directory) containing the input part of @var{tags}.  For
2114 instance, running
2116 @example
2117 AC_CONFIG_COMMANDS([deep/dir/out:in/in.in], [@dots{}], [@dots{}])
2118 @end example
2120 @noindent
2121  with @option{--srcdir=../package} produces the following values:
2123 @example
2124 # Argument of --srcdir
2125 srcdir='../package'
2126 # Reversing deep/dir
2127 ac_top_build_prefix='../../'
2128 # Concatenation of $ac_top_build_prefix and srcdir
2129 ac_top_srcdir='../../../package'
2130 # Concatenation of $ac_top_srcdir and deep/dir
2131 ac_srcdir='../../../package/deep/dir'
2132 @end example
2134 @noindent
2135 independently of @samp{in/in.in}.
2137 @item init-cmds
2138 Shell commands output @emph{unquoted} near the beginning of
2139 @file{config.status}, and executed each time @file{config.status} runs
2140 (regardless of the tag).  Because they are unquoted, for example,
2141 @samp{$var} is output as the value of @code{var}.  @var{init-cmds}
2142 is typically used by @file{configure} to give @file{config.status} some
2143 variables it needs to run the @var{commands}.
2145 You should be extremely cautious in your variable names: all the
2146 @var{init-cmds} share the same name space and may overwrite each other
2147 in unpredictable ways.  Sorry@enddots{}
2148 @end table
2150 All these macros can be called multiple times, with different
2151 @var{tag} values, of course!
2154 @node Configuration Files
2155 @section Creating Configuration Files
2156 @cindex Creating configuration files
2157 @cindex Configuration file creation
2159 Be sure to read the previous section, @ref{Configuration Actions}.
2161 @anchor{AC_CONFIG_FILES}
2162 @defmac AC_CONFIG_FILES (@var{file}@dots{}, @ovar{cmds}, @ovar{init-cmds})
2163 @acindex{CONFIG_FILES}
2164 Make @code{AC_OUTPUT} create each @file{@var{file}} by copying an input
2165 file (by default @file{@var{file}.in}), substituting the output variable
2166 values.
2167 @c Before we used to have this feature, which was later rejected
2168 @c because it complicates the writing of makefiles:
2169 @c If the file would be unchanged, it is left untouched, to preserve
2170 @c timestamp.
2171 This macro is one of the instantiating macros; see @ref{Configuration
2172 Actions}.  @xref{Makefile Substitutions}, for more information on using
2173 output variables.  @xref{Setting Output Variables}, for more information
2174 on creating them.  This macro creates the directory that the file is in
2175 if it doesn't exist.  Usually, makefiles are created this way,
2176 but other files, such as @file{.gdbinit}, can be specified as well.
2178 Typical calls to @code{AC_CONFIG_FILES} look like this:
2180 @example
2181 AC_CONFIG_FILES([Makefile src/Makefile man/Makefile X/Imakefile])
2182 AC_CONFIG_FILES([autoconf], [chmod +x autoconf])
2183 @end example
2185 You can override an input file name by appending to @var{file} a
2186 colon-separated list of input files.  Examples:
2188 @example
2189 AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk]
2190                 [lib/Makefile:boiler/lib.mk])
2191 @end example
2193 @noindent
2194 Doing this allows you to keep your file names acceptable to
2195 @acronym{DOS} variants, or
2196 to prepend and/or append boilerplate to the file.
2197 @end defmac
2201 @node Makefile Substitutions
2202 @section Substitutions in Makefiles
2203 @cindex Substitutions in makefiles
2204 @cindex Makefile substitutions
2206 Each subdirectory in a distribution that contains something to be
2207 compiled or installed should come with a file @file{Makefile.in}, from
2208 which @command{configure} creates a file @file{Makefile} in that directory.
2209 To create @file{Makefile}, @command{configure} performs a simple variable
2210 substitution, replacing occurrences of @samp{@@@var{variable}@@} in
2211 @file{Makefile.in} with the value that @command{configure} has determined
2212 for that variable.  Variables that are substituted into output files in
2213 this way are called @dfn{output variables}.  They are ordinary shell
2214 variables that are set in @command{configure}.  To make @command{configure}
2215 substitute a particular variable into the output files, the macro
2216 @code{AC_SUBST} must be called with that variable name as an argument.
2217 Any occurrences of @samp{@@@var{variable}@@} for other variables are
2218 left unchanged.  @xref{Setting Output Variables}, for more information
2219 on creating output variables with @code{AC_SUBST}.
2221 A software package that uses a @command{configure} script should be
2222 distributed with a file @file{Makefile.in}, but no makefile; that
2223 way, the user has to properly configure the package for the local system
2224 before compiling it.
2226 @xref{Makefile Conventions, , Makefile Conventions, standards, The
2227 @acronym{GNU} Coding Standards}, for more information on what to put in
2228 makefiles.
2230 @menu
2231 * Preset Output Variables::     Output variables that are always set
2232 * Installation Directory Variables::  Other preset output variables
2233 * Changed Directory Variables:: Warnings about @file{datarootdir}
2234 * Build Directories::           Supporting multiple concurrent compiles
2235 * Automatic Remaking::          Makefile rules for configuring
2236 @end menu
2238 @node Preset Output Variables
2239 @subsection Preset Output Variables
2240 @cindex Output variables
2242 Some output variables are preset by the Autoconf macros.  Some of the
2243 Autoconf macros set additional output variables, which are mentioned in
2244 the descriptions for those macros.  @xref{Output Variable Index}, for a
2245 complete list of output variables.  @xref{Installation Directory
2246 Variables}, for the list of the preset ones related to installation
2247 directories.  Below are listed the other preset ones.  They all are
2248 precious variables (@pxref{Setting Output Variables},
2249 @code{AC_ARG_VAR}).
2251 @c Just say no to ASCII sorting!  We're humans, not computers.
2252 @c These variables are listed as they would be in a dictionary:
2253 @c actor
2254 @c Actress
2255 @c actress
2257 @defvar CFLAGS
2258 @ovindex CFLAGS
2259 Debugging and optimization options for the C compiler.  If it is not set
2260 in the environment when @command{configure} runs, the default value is set
2261 when you call @code{AC_PROG_CC} (or empty if you don't).  @command{configure}
2262 uses this variable when compiling or linking programs to test for C features.
2264 If a compiler option affects only the behavior of the preprocessor
2265 (e.g., @option{-D @var{name}}), it should be put into @code{CPPFLAGS}
2266 instead.  If it affects only the linker (e.g., @option{-L
2267 @var{directory}}), it should be put into @code{LDFLAGS} instead.  If it
2268 affects only the compiler proper, @code{CFLAGS} is the natural home for
2269 it.  If an option affects multiple phases of the compiler, though,
2270 matters get tricky.  One approach to put such options directly into
2271 @code{CC}, e.g., @code{CC='gcc -m64'}.  Another is to put them into both
2272 @code{CPPFLAGS} and @code{LDFLAGS}, but not into @code{CFLAGS}.
2274 @end defvar
2276 @defvar configure_input
2277 @ovindex configure_input
2278 A comment saying that the file was generated automatically by
2279 @command{configure} and giving the name of the input file.
2280 @code{AC_OUTPUT} adds a comment line containing this variable to the top
2281 of every makefile it creates.  For other files, you should
2282 reference this variable in a comment at the top of each input file.  For
2283 example, an input shell script should begin like this:
2285 @example
2286 #!/bin/sh
2287 # @@configure_input@@
2288 @end example
2290 @noindent
2291 The presence of that line also reminds people editing the file that it
2292 needs to be processed by @command{configure} in order to be used.
2293 @end defvar
2295 @defvar CPPFLAGS
2296 @ovindex CPPFLAGS
2297 Preprocessor options for the C, C++, and Objective C preprocessors and
2298 compilers.  If
2299 it is not set in the environment when @command{configure} runs, the default
2300 value is empty.  @command{configure} uses this variable when preprocessing
2301 or compiling programs to test for C, C++, and Objective C features.
2303 This variable's contents should contain options like @option{-I},
2304 @option{-D}, and @option{-U} that affect only the behavior of the
2305 preprocessor.  Please see the explanation of @code{CFLAGS} for what you
2306 can do if an option affects other phases of the compiler as well.
2308 Currently, @command{configure} always links as part of a single
2309 invocation of the compiler that also preprocesses and compiles, so it
2310 uses this variable also when linking programs.  However, it is unwise to
2311 depend on this behavior because the @acronym{GNU} coding standards do
2312 not require it and many packages do not use @code{CPPFLAGS} when linking
2313 programs.
2315 @xref{Special Chars in Variables}, for limitations that @code{CPPFLAGS}
2316 might run into.
2317 @end defvar
2319 @defvar CXXFLAGS
2320 @ovindex CXXFLAGS
2321 Debugging and optimization options for the C++ compiler.  It acts like
2322 @code{CFLAGS}, but for C++ instead of C.
2323 @end defvar
2325 @defvar DEFS
2326 @ovindex DEFS
2327 @option{-D} options to pass to the C compiler.  If @code{AC_CONFIG_HEADERS}
2328 is called, @command{configure} replaces @samp{@@DEFS@@} with
2329 @option{-DHAVE_CONFIG_H} instead (@pxref{Configuration Headers}).  This
2330 variable is not defined while @command{configure} is performing its tests,
2331 only when creating the output files.  @xref{Setting Output Variables}, for
2332 how to check the results of previous tests.
2333 @end defvar
2335 @defvar ECHO_C
2336 @defvarx ECHO_N
2337 @defvarx ECHO_T
2338 @ovindex ECHO_C
2339 @ovindex ECHO_N
2340 @ovindex ECHO_T
2341 How does one suppress the trailing newline from @command{echo} for
2342 question-answer message pairs?  These variables provide a way:
2344 @example
2345 echo $ECHO_N "And the winner is... $ECHO_C"
2346 sleep 100000000000
2347 echo "$@{ECHO_T@}dead."
2348 @end example
2350 @noindent
2351 Some old and uncommon @command{echo} implementations offer no means to
2352 achieve this, in which case @code{ECHO_T} is set to tab.  You might not
2353 want to use it.
2354 @end defvar
2356 @defvar ERLCFLAGS
2357 @ovindex ERLCFLAGS
2358 Debugging and optimization options for the Erlang compiler.  If it is not set
2359 in the environment when @command{configure} runs, the default value is empty.
2360 @command{configure} uses this variable when compiling
2361 programs to test for Erlang features.
2362 @end defvar
2364 @defvar FCFLAGS
2365 @ovindex FCFLAGS
2366 Debugging and optimization options for the Fortran compiler.  If it
2367 is not set in the environment when @command{configure} runs, the default
2368 value is set when you call @code{AC_PROG_FC} (or empty if you don't).
2369 @command{configure} uses this variable when compiling or linking
2370 programs to test for Fortran features.
2371 @end defvar
2373 @defvar FFLAGS
2374 @ovindex FFLAGS
2375 Debugging and optimization options for the Fortran 77 compiler.  If it
2376 is not set in the environment when @command{configure} runs, the default
2377 value is set when you call @code{AC_PROG_F77} (or empty if you don't).
2378 @command{configure} uses this variable when compiling or linking
2379 programs to test for Fortran 77 features.
2380 @end defvar
2382 @defvar LDFLAGS
2383 @ovindex LDFLAGS
2384 Options for the linker.  If it is not set
2385 in the environment when @command{configure} runs, the default value is empty.
2386 @command{configure} uses this variable when linking programs to test for
2387 C, C++, Objective C, and Fortran features.
2389 This variable's contents should contain options like @option{-s} and
2390 @option{-L} that affect only the behavior of the linker.  Please see the
2391 explanation of @code{CFLAGS} for what you can do if an option also
2392 affects other phases of the compiler.
2394 Don't use this variable to pass library names
2395 (@option{-l}) to the linker; use @code{LIBS} instead.
2396 @end defvar
2398 @defvar LIBS
2399 @ovindex LIBS
2400 @option{-l} options to pass to the linker.  The default value is empty,
2401 but some Autoconf macros may prepend extra libraries to this variable if
2402 those libraries are found and provide necessary functions, see
2403 @ref{Libraries}.  @command{configure} uses this variable when linking
2404 programs to test for C, C++, and Fortran features.
2405 @end defvar
2407 @defvar OBJCFLAGS
2408 @ovindex OBJCFLAGS
2409 Debugging and optimization options for the Objective C compiler.  It
2410 acts like @code{CFLAGS}, but for Objective C instead of C.
2411 @end defvar
2413 @defvar builddir
2414 @ovindex builddir
2415 Rigorously equal to @samp{.}.  Added for symmetry only.
2416 @end defvar
2418 @defvar abs_builddir
2419 @ovindex abs_builddir
2420 Absolute name of @code{builddir}.
2421 @end defvar
2423 @defvar top_builddir
2424 @ovindex top_builddir
2425 The relative name of the top level of the current build tree.  In the
2426 top-level directory, this is the same as @code{builddir}.
2427 @end defvar
2429 @defvar abs_top_builddir
2430 @ovindex abs_top_builddir
2431 Absolute name of @code{top_builddir}.
2432 @end defvar
2434 @defvar srcdir
2435 @ovindex srcdir
2436 The name of the directory that contains the source code for
2437 that makefile.
2438 @end defvar
2440 @defvar abs_srcdir
2441 @ovindex abs_srcdir
2442 Absolute name of @code{srcdir}.
2443 @end defvar
2445 @defvar top_srcdir
2446 @ovindex top_srcdir
2447 The name of the top-level source code directory for the
2448 package.  In the top-level directory, this is the same as @code{srcdir}.
2449 @end defvar
2451 @defvar abs_top_srcdir
2452 @ovindex abs_top_srcdir
2453 Absolute name of @code{top_srcdir}.
2454 @end defvar
2456 @node Installation Directory Variables
2457 @subsection Installation Directory Variables
2458 @cindex Installation directories
2459 @cindex Directories, installation
2461 The following variables specify the directories for
2462 package installation, see @ref{Directory Variables, , Variables for
2463 Installation Directories, standards, The @acronym{GNU} Coding
2464 Standards}, for more information.  Each variable corresponds to an
2465 argument of @command{configure}; trailing slashes are stripped so that
2466 expressions such as @samp{$@{prefix@}/lib} expand with only one slash
2467 between directory names.  See the end of this section for
2468 details on when and how to use these variables.
2470 @defvar bindir
2471 @ovindex bindir
2472 The directory for installing executables that users run.
2473 @end defvar
2475 @defvar datadir
2476 @ovindex datadir
2477 The directory for installing idiosyncratic read-only
2478 architecture-independent data.
2479 @end defvar
2481 @defvar datarootdir
2482 @ovindex datarootdir
2483 The root of the directory tree for read-only architecture-independent
2484 data files.
2485 @end defvar
2487 @defvar docdir
2488 @ovindex docdir
2489 The directory for installing documentation files (other than Info and
2490 man).
2491 @end defvar
2493 @defvar dvidir
2494 @ovindex dvidir
2495 The directory for installing documentation files in DVI format.
2496 @end defvar
2498 @defvar exec_prefix
2499 @ovindex exec_prefix
2500 The installation prefix for architecture-dependent files.  By default
2501 it's the same as @var{prefix}.  You should avoid installing anything
2502 directly to @var{exec_prefix}.  However, the default value for
2503 directories containing architecture-dependent files should be relative
2504 to @var{exec_prefix}.
2505 @end defvar
2507 @defvar htmldir
2508 @ovindex htmldir
2509 The directory for installing HTML documentation.
2510 @end defvar
2512 @defvar includedir
2513 @ovindex includedir
2514 The directory for installing C header files.
2515 @end defvar
2517 @defvar infodir
2518 @ovindex infodir
2519 The directory for installing documentation in Info format.
2520 @end defvar
2522 @defvar libdir
2523 @ovindex libdir
2524 The directory for installing object code libraries.
2525 @end defvar
2527 @defvar libexecdir
2528 @ovindex libexecdir
2529 The directory for installing executables that other programs run.
2530 @end defvar
2532 @defvar localedir
2533 @ovindex localedir
2534 The directory for installing locale-dependent but
2535 architecture-independent data, such as message catalogs.  This directory
2536 usually has a subdirectory per locale.
2537 @end defvar
2539 @defvar localstatedir
2540 @ovindex localstatedir
2541 The directory for installing modifiable single-machine data.
2542 @end defvar
2544 @defvar mandir
2545 @ovindex mandir
2546 The top-level directory for installing documentation in man format.
2547 @end defvar
2549 @defvar oldincludedir
2550 @ovindex oldincludedir
2551 The directory for installing C header files for non-@acronym{GCC} compilers.
2552 @end defvar
2554 @defvar pdfdir
2555 @ovindex pdfdir
2556 The directory for installing PDF documentation.
2557 @end defvar
2559 @defvar prefix
2560 @ovindex prefix
2561 The common installation prefix for all files.  If @var{exec_prefix}
2562 is defined to a different value, @var{prefix} is used only for
2563 architecture-independent files.
2564 @end defvar
2566 @defvar psdir
2567 @ovindex psdir
2568 The directory for installing PostScript documentation.
2569 @end defvar
2571 @defvar sbindir
2572 @ovindex sbindir
2573 The directory for installing executables that system
2574 administrators run.
2575 @end defvar
2577 @defvar sharedstatedir
2578 @ovindex sharedstatedir
2579 The directory for installing modifiable architecture-independent data.
2580 @end defvar
2582 @defvar sysconfdir
2583 @ovindex sysconfdir
2584 The directory for installing read-only single-machine data.
2585 @end defvar
2588 Most of these variables have values that rely on @code{prefix} or
2589 @code{exec_prefix}.  It is deliberate that the directory output
2590 variables keep them unexpanded: typically @samp{@@datarootdir@@} is
2591 replaced by @samp{$@{prefix@}/share}, not @samp{/usr/local/share}, and
2592 @samp{@@datadir@@} is replaced by @samp{$@{datarootdir@}}.
2594 This behavior is mandated by the @acronym{GNU} coding standards, so that when
2595 the user runs:
2597 @table @samp
2598 @item make
2599 she can still specify a different prefix from the one specified to
2600 @command{configure}, in which case, if needed, the package should hard
2601 code dependencies corresponding to the make-specified prefix.
2603 @item make install
2604 she can specify a different installation location, in which case the
2605 package @emph{must} still depend on the location which was compiled in
2606 (i.e., never recompile when @samp{make install} is run).  This is an
2607 extremely important feature, as many people may decide to install all
2608 the files of a package grouped together, and then install links from
2609 the final locations to there.
2610 @end table
2612 In order to support these features, it is essential that
2613 @code{datarootdir} remains being defined as @samp{$@{prefix@}/share} to
2614 depend upon the current value of @code{prefix}.
2616 A corollary is that you should not use these variables except in
2617 makefiles.  For instance, instead of trying to evaluate @code{datadir}
2618 in @file{configure} and hard-coding it in makefiles using
2619 e.g., @samp{AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])},
2620 you should add
2621 @option{-DDATADIR='$(datadir)'} to your makefile's definition of
2622 @code{CPPFLAGS} (@code{AM_CPPFLAGS} if you are also using Automake).
2624 Similarly, you should not rely on @code{AC_CONFIG_FILES} to replace
2625 @code{datadir} and friends in your shell scripts and other files; instead,
2626 let @command{make} manage their replacement.  For instance Autoconf
2627 ships templates of its shell scripts ending with @samp{.in}, and uses a
2628 makefile snippet similar to the following to build scripts like
2629 @command{autoheader} and @command{autom4te}:
2631 @example
2632 @group
2633 edit = sed \
2634         -e 's|@@datadir[@@]|$(pkgdatadir)|g' \
2635         -e 's|@@prefix[@@]|$(prefix)|g'
2636 @end group
2638 @group
2639 autoheader autom4te: Makefile
2640         rm -f $@@ $@@.tmp
2641         $(edit) '$(srcdir)/$@@.in' >$@@.tmp
2642         chmod +x $@@.tmp
2643         chmod a-w $@@.tmp
2644         mv $@@.tmp $@@
2645 @end group
2647 @group
2648 autoheader: $(srcdir)/autoheader.in
2649 autom4te: $(srcdir)/autom4te.in
2650 @end group
2651 @end example
2653 Some details are noteworthy:
2655 @table @asis
2656 @item @samp{@@datadir[@@]}
2657 The brackets prevent @command{configure} from replacing
2658 @samp{@@datadir@@} in the Sed expression itself.
2659 Brackets are preferable to a backslash here, since
2660 Posix says @samp{\@@} is not portable.
2662 @item @samp{$(pkgdatadir)}
2663 Don't use @samp{@@pkgdatadir@@}!  Use the matching makefile variable
2664 instead.
2666 @item @samp{/}
2667 Don't use @samp{/} in the Sed expressions that replace file names since
2668 most likely the
2669 variables you use, such as @samp{$(pkgdatadir)}, contain @samp{/}.
2670 Use a shell metacharacter instead, such as @samp{|}.
2672 @item special characters
2673 File names, file name components, and the value of @code{VPATH} should
2674 not contain shell metacharacters or white
2675 space.  @xref{Special Chars in Variables}.
2677 @item dependency on @file{Makefile}
2678 Since @code{edit} uses values that depend on the configuration specific
2679 values (@code{prefix}, etc.)@: and not only on @code{VERSION} and so forth,
2680 the output depends on @file{Makefile}, not @file{configure.ac}.
2682 @item @samp{$@@}
2683 The main rule is generic, and uses @samp{$@@} extensively to
2684 avoid the need for multiple copies of the rule.
2686 @item Separated dependencies and single suffix rules
2687 You can't use them!  The above snippet cannot be (portably) rewritten
2690 @example
2691 autoconf autoheader: Makefile
2692 @group
2693 .in:
2694         rm -f $@@ $@@.tmp
2695         $(edit) $< >$@@.tmp
2696         chmod +x $@@.tmp
2697         mv $@@.tmp $@@
2698 @end group
2699 @end example
2701 @xref{Single Suffix Rules}, for details.
2703 @item @samp{$(srcdir)}
2704 Be sure to specify the name of the source directory,
2705 otherwise the package won't support separated builds.
2706 @end table
2708 For the more specific installation of Erlang libraries, the following variables
2709 are defined:
2711 @defvar ERLANG_INSTALL_LIB_DIR
2712 @ovindex ERLANG_INSTALL_LIB_DIR
2713 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
2714 The common parent directory of Erlang library installation directories.
2715 This variable is set by calling the @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR}
2716 macro in @file{configure.ac}.
2717 @end defvar
2719 @defvar ERLANG_INSTALL_LIB_DIR_@var{library}
2720 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
2721 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
2722 The installation directory for Erlang library @var{library}.
2723 This variable is set by calling the
2724 @samp{AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR(@var{library}, @var{version}}
2725 macro in @file{configure.ac}.
2726 @end defvar
2728 @xref{Erlang Libraries}, for details.
2731 @node Changed Directory Variables
2732 @subsection Changed Directory Variables
2733 @cindex @file{datarootdir}
2735 In Autoconf 2.60, the set of directory variables has changed, and the
2736 defaults of some variables have been adjusted
2737 (@pxref{Installation Directory Variables}) to changes in the
2738 @acronym{GNU} Coding Standards.  Notably, @file{datadir}, @file{infodir}, and
2739 @file{mandir} are now expressed in terms of @file{datarootdir}.  If you are
2740 upgrading from an earlier Autoconf version, you may need to adjust your files
2741 to ensure that the directory variables are substituted correctly
2742 (@pxref{Defining Directories}), and that a definition of @file{datarootdir} is
2743 in place.  For example, in a @file{Makefile.in}, adding
2745 @example
2746 datarootdir = @@datarootdir@@
2747 @end example
2749 @noindent
2750 is usually sufficient.  If you use Automake to create @file{Makefile.in},
2751 it will add this for you.
2753 To help with the transition, Autoconf warns about files that seem to use
2754 @code{datarootdir} without defining it.  In some cases, it then expands
2755 the value of @code{$datarootdir} in substitutions of the directory
2756 variables.  The following example shows such a warning:
2758 @example
2759 $ @kbd{cat configure.ac}
2760 AC_INIT
2761 AC_CONFIG_FILES([Makefile])
2762 AC_OUTPUT
2763 $ @kbd{cat Makefile.in}
2764 prefix = @@prefix@@
2765 datadir = @@datadir@@
2766 $ @kbd{autoconf}
2767 $ @kbd{configure}
2768 configure: creating ./config.status
2769 config.status: creating Makefile
2770 config.status: WARNING:
2771                Makefile.in seems to ignore the --datarootdir setting
2772 $ @kbd{cat Makefile}
2773 prefix = /usr/local
2774 datadir = $@{prefix@}/share
2775 @end example
2777 Usually one can easily change the file to accommodate both older and newer
2778 Autoconf releases:
2780 @example
2781 $ @kbd{cat Makefile.in}
2782 prefix = @@prefix@@
2783 datarootdir = @@datarootdir@@
2784 datadir = @@datadir@@
2785 $ @kbd{configure}
2786 configure: creating ./config.status
2787 config.status: creating Makefile
2788 $ @kbd{cat Makefile}
2789 prefix = /usr/local
2790 datarootdir = $@{prefix@}/share
2791 datadir = $@{datarootdir@}
2792 @end example
2794 @acindex{DATAROOTDIR_CHECKED}
2795 In some cases, however, the checks may not be able to detect that a suitable
2796 initialization of @code{datarootdir} is in place, or they may fail to detect
2797 that such an initialization is necessary in the output file.  If, after
2798 auditing your package, there are still spurious @file{configure} warnings about
2799 @code{datarootdir}, you may add the line
2801 @example
2802 AC_DEFUN([AC_DATAROOTDIR_CHECKED])
2803 @end example
2805 @noindent
2806 to your @file{configure.ac} to disable the warnings.  This is an exception
2807 to the usual rule that you should not define a macro whose name begins with
2808 @code{AC_} (@pxref{Macro Names}).
2812 @node Build Directories
2813 @subsection Build Directories
2814 @cindex Build directories
2815 @cindex Directories, build
2817 You can support compiling a software package for several architectures
2818 simultaneously from the same copy of the source code.  The object files
2819 for each architecture are kept in their own directory.
2821 To support doing this, @command{make} uses the @code{VPATH} variable to
2822 find the files that are in the source directory.  @acronym{GNU} Make
2823 can do this.  Most other recent @command{make} programs can do this as
2824 well, though they may have difficulties and it is often simpler to
2825 recommend @acronym{GNU} @command{make} (@pxref{VPATH and Make}).  Older
2826 @command{make} programs do not support @code{VPATH}; when using them, the
2827 source code must be in the same directory as the object files.
2829 To support @code{VPATH}, each @file{Makefile.in} should contain two
2830 lines that look like:
2832 @example
2833 srcdir = @@srcdir@@
2834 VPATH = @@srcdir@@
2835 @end example
2837 Do not set @code{VPATH} to the value of another variable, for example
2838 @samp{VPATH = $(srcdir)}, because some versions of @command{make} do not do
2839 variable substitutions on the value of @code{VPATH}.
2841 @command{configure} substitutes the correct value for @code{srcdir} when
2842 it produces @file{Makefile}.
2844 Do not use the @code{make} variable @code{$<}, which expands to the
2845 file name of the file in the source directory (found with @code{VPATH}),
2846 except in implicit rules.  (An implicit rule is one such as @samp{.c.o},
2847 which tells how to create a @file{.o} file from a @file{.c} file.)  Some
2848 versions of @command{make} do not set @code{$<} in explicit rules; they
2849 expand it to an empty value.
2851 Instead, Make command lines should always refer to source
2852 files by prefixing them with @samp{$(srcdir)/}.  For example:
2854 @example
2855 time.info: time.texinfo
2856         $(MAKEINFO) '$(srcdir)/time.texinfo'
2857 @end example
2859 @node Automatic Remaking
2860 @subsection Automatic Remaking
2861 @cindex Automatic remaking
2862 @cindex Remaking automatically
2864 You can put rules like the following in the top-level @file{Makefile.in}
2865 for a package to automatically update the configuration information when
2866 you change the configuration files.  This example includes all of the
2867 optional files, such as @file{aclocal.m4} and those related to
2868 configuration header files.  Omit from the @file{Makefile.in} rules for
2869 any of these files that your package does not use.
2871 The @samp{$(srcdir)/} prefix is included because of limitations in the
2872 @code{VPATH} mechanism.
2874 The @file{stamp-} files are necessary because the timestamps of
2875 @file{config.h.in} and @file{config.h} are not changed if remaking
2876 them does not change their contents.  This feature avoids unnecessary
2877 recompilation.  You should include the file @file{stamp-h.in} your
2878 package's distribution, so that @command{make} considers
2879 @file{config.h.in} up to date.  Don't use @command{touch}
2880 (@pxref{Limitations of Usual Tools}); instead, use @command{echo} (using
2881 @command{date} would cause needless differences, hence @acronym{CVS}
2882 conflicts, etc.).
2884 @example
2885 @group
2886 $(srcdir)/configure: configure.ac aclocal.m4
2887         cd '$(srcdir)' && autoconf
2889 # autoheader might not change config.h.in, so touch a stamp file.
2890 $(srcdir)/config.h.in: stamp-h.in
2891 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
2892         cd '$(srcdir)' && autoheader
2893         echo timestamp > '$(srcdir)/stamp-h.in'
2895 config.h: stamp-h
2896 stamp-h: config.h.in config.status
2897         ./config.status
2899 Makefile: Makefile.in config.status
2900         ./config.status
2902 config.status: configure
2903         ./config.status --recheck
2904 @end group
2905 @end example
2907 @noindent
2908 (Be careful if you copy these lines directly into your makefile, as you
2909 need to convert the indented lines to start with the tab character.)
2911 In addition, you should use
2913 @example
2914 AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
2915 @end example
2917 @noindent
2918 so @file{config.status} ensures that @file{config.h} is considered up to
2919 date.  @xref{Output}, for more information about @code{AC_OUTPUT}.
2921 @xref{config.status Invocation}, for more examples of handling
2922 configuration-related dependencies.
2924 @node Configuration Headers
2925 @section Configuration Header Files
2926 @cindex Configuration Header
2927 @cindex @file{config.h}
2929 When a package contains more than a few tests that define C preprocessor
2930 symbols, the command lines to pass @option{-D} options to the compiler
2931 can get quite long.  This causes two problems.  One is that the
2932 @command{make} output is hard to visually scan for errors.  More
2933 seriously, the command lines can exceed the length limits of some
2934 operating systems.  As an alternative to passing @option{-D} options to
2935 the compiler, @command{configure} scripts can create a C header file
2936 containing @samp{#define} directives.  The @code{AC_CONFIG_HEADERS}
2937 macro selects this kind of output.  Though it can be called anywhere
2938 between @code{AC_INIT} and @code{AC_OUTPUT}, it is customary to call
2939 it right after @code{AC_INIT}.
2941 The package should @samp{#include} the configuration header file before
2942 any other header files, to prevent inconsistencies in declarations (for
2943 example, if it redefines @code{const}).
2945 To provide for VPATH builds, remember to pass the C compiler a @option{-I.}
2946 option (or @option{-I..}; whichever directory contains @file{config.h}).
2947 Even if you use @samp{#include "config.h"}, the preprocessor searches only
2948 the directory of the currently read file, i.e., the source directory, not
2949 the build directory.
2951 With the appropriate @option{-I} option, you can use
2952 @samp{#include <config.h>}.  Actually, it's a good habit to use it,
2953 because in the rare case when the source directory contains another
2954 @file{config.h}, the build directory should be searched first.
2957 @defmac AC_CONFIG_HEADERS (@var{header} @dots{}, @ovar{cmds}, @ovar{init-cmds})
2958 @acindex{CONFIG_HEADERS}
2959 @cvindex HAVE_CONFIG_H
2960 This macro is one of the instantiating macros; see @ref{Configuration
2961 Actions}.  Make @code{AC_OUTPUT} create the file(s) in the
2962 blank-or-newline-separated list @var{header} containing C preprocessor
2963 @code{#define} statements, and replace @samp{@@DEFS@@} in generated
2964 files with @option{-DHAVE_CONFIG_H} instead of the value of @code{DEFS}.
2965 The usual name for @var{header} is @file{config.h}.
2967 If @var{header} already exists and its contents are identical to what
2968 @code{AC_OUTPUT} would put in it, it is left alone.  Doing this allows
2969 making some changes in the configuration without needlessly causing
2970 object files that depend on the header file to be recompiled.
2972 Usually the input file is named @file{@var{header}.in}; however, you can
2973 override the input file name by appending to @var{header} a
2974 colon-separated list of input files.  For example, you might need to make
2975 the input file name acceptable to @acronym{DOS} variants:
2977 @example
2978 AC_CONFIG_HEADERS([config.h:config.hin])
2979 @end example
2981 @end defmac
2983 @defmac AH_HEADER
2984 @ahindex{HEADER}
2985 This macro is defined as the name of the first declared config header
2986 and undefined if no config headers have been declared up to this point.
2987 A third-party macro may, for example, require use of a config header
2988 without invoking AC_CONFIG_HEADERS twice, like this:
2990 @example
2991 AC_CONFIG_COMMANDS_PRE(
2992         [m4_ifndef([AH_HEADER], [AC_CONFIG_HEADERS([config.h])])])
2993 @end example
2995 @end defmac
2997 @xref{Configuration Actions}, for more details on @var{header}.
2999 @menu
3000 * Header Templates::            Input for the configuration headers
3001 * autoheader Invocation::       How to create configuration templates
3002 * Autoheader Macros::           How to specify CPP templates
3003 @end menu
3005 @node Header Templates
3006 @subsection Configuration Header Templates
3007 @cindex Configuration Header Template
3008 @cindex Header templates
3009 @cindex @file{config.h.in}
3011 Your distribution should contain a template file that looks as you want
3012 the final header file to look, including comments, with @code{#undef}
3013 statements which are used as hooks.  For example, suppose your
3014 @file{configure.ac} makes these calls:
3016 @example
3017 AC_CONFIG_HEADERS([conf.h])
3018 AC_CHECK_HEADERS([unistd.h])
3019 @end example
3021 @noindent
3022 Then you could have code like the following in @file{conf.h.in}.  On
3023 systems that have @file{unistd.h}, @command{configure} defines
3024 @samp{HAVE_UNISTD_H} to 1.  On other systems, the whole line is
3025 commented out (in case the system predefines that symbol).
3027 @example
3028 @group
3029 /* Define as 1 if you have unistd.h.  */
3030 #undef HAVE_UNISTD_H
3031 @end group
3032 @end example
3034 Pay attention that @samp{#undef} is in the first column, and there is
3035 nothing after @samp{HAVE_UNISTD_H}, not even white space.  You can
3036 then decode the configuration header using the preprocessor directives:
3038 @example
3039 @group
3040 #include <conf.h>
3042 #ifdef HAVE_UNISTD_H
3043 # include <unistd.h>
3044 #else
3045 /* We are in trouble.  */
3046 #endif
3047 @end group
3048 @end example
3050 The use of old form templates, with @samp{#define} instead of
3051 @samp{#undef} is strongly discouraged.  Similarly with old templates
3052 with comments on the same line as the @samp{#undef}.  Anyway, putting
3053 comments in preprocessor macros has never been a good idea.
3055 Since it is a tedious task to keep a template header up to date, you may
3056 use @command{autoheader} to generate it, see @ref{autoheader Invocation}.
3059 @node autoheader Invocation
3060 @subsection Using @command{autoheader} to Create @file{config.h.in}
3061 @cindex @command{autoheader}
3063 The @command{autoheader} program can create a template file of C
3064 @samp{#define} statements for @command{configure} to use.
3065 It searches for the first invocation of @code{AC_CONFIG_HEADERS} in
3066 @file{configure} sources to determine the name of the template.
3067 (If the first call of @code{AC_CONFIG_HEADERS} specifies more than one
3068 input file name, @command{autoheader} uses the first one.)
3070 It is recommended that only one input file is used.  If you want to append
3071 a boilerplate code, it is preferable to use
3072 @samp{AH_BOTTOM([#include <conf_post.h>])}.
3073 File @file{conf_post.h} is not processed during the configuration then,
3074 which make things clearer.  Analogically, @code{AH_TOP} can be used to
3075 prepend a boilerplate code.
3077 In order to do its job, @command{autoheader} needs you to document all
3078 of the symbols that you might use.  Typically this is done via an
3079 @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED} call whose first argument
3080 is a literal symbol and whose third argument describes the symbol
3081 (@pxref{Defining Symbols}).  Alternatively, you can use
3082 @code{AH_TEMPLATE} (@pxref{Autoheader Macros}), or you can supply a
3083 suitable input file for a subsequent configuration header file.
3084 Symbols defined by Autoconf's builtin tests are already documented properly;
3085 you need to document only those that you
3086 define yourself.
3088 You might wonder why @command{autoheader} is needed: after all, why
3089 would @command{configure} need to ``patch'' a @file{config.h.in} to
3090 produce a @file{config.h} instead of just creating @file{config.h} from
3091 scratch?  Well, when everything rocks, the answer is just that we are
3092 wasting our time maintaining @command{autoheader}: generating
3093 @file{config.h} directly is all that is needed.  When things go wrong,
3094 however, you'll be thankful for the existence of @command{autoheader}.
3096 The fact that the symbols are documented is important in order to
3097 @emph{check} that @file{config.h} makes sense.  The fact that there is a
3098 well-defined list of symbols that should be defined (or not) is
3099 also important for people who are porting packages to environments where
3100 @command{configure} cannot be run: they just have to @emph{fill in the
3101 blanks}.
3103 But let's come back to the point: the invocation of @command{autoheader}@dots{}
3105 If you give @command{autoheader} an argument, it uses that file instead
3106 of @file{configure.ac} and writes the header file to the standard output
3107 instead of to @file{config.h.in}.  If you give @command{autoheader} an
3108 argument of @option{-}, it reads the standard input instead of
3109 @file{configure.ac} and writes the header file to the standard output.
3111 @command{autoheader} accepts the following options:
3113 @table @option
3114 @item --help
3115 @itemx -h
3116 Print a summary of the command line options and exit.
3118 @item --version
3119 @itemx -V
3120 Print the version number of Autoconf and exit.
3122 @item --verbose
3123 @itemx -v
3124 Report processing steps.
3126 @item --debug
3127 @itemx -d
3128 Don't remove the temporary files.
3130 @item --force
3131 @itemx -f
3132 Remake the template file even if newer than its input files.
3134 @item --include=@var{dir}
3135 @itemx -I @var{dir}
3136 Append @var{dir} to the include path.  Multiple invocations accumulate.
3138 @item --prepend-include=@var{dir}
3139 @item -B @var{dir}
3140 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
3142 @item --warnings=@var{category}
3143 @itemx -W @var{category}
3144 @evindex WARNINGS
3145 Report the warnings related to @var{category} (which can actually be a
3146 comma separated list).  Current categories include:
3148 @table @samp
3149 @item obsolete
3150 report the uses of obsolete constructs
3152 @item all
3153 report all the warnings
3155 @item none
3156 report none
3158 @item error
3159 treats warnings as errors
3161 @item no-@var{category}
3162 disable warnings falling into @var{category}
3163 @end table
3165 @end table
3169 @node Autoheader Macros
3170 @subsection Autoheader Macros
3171 @cindex Autoheader macros
3173 @command{autoheader} scans @file{configure.ac} and figures out which C
3174 preprocessor symbols it might define.  It knows how to generate
3175 templates for symbols defined by @code{AC_CHECK_HEADERS},
3176 @code{AC_CHECK_FUNCS} etc., but if you @code{AC_DEFINE} any additional
3177 symbol, you must define a template for it.  If there are missing
3178 templates, @command{autoheader} fails with an error message.
3180 The template for a @var{symbol} is created
3181 by @command{autoheader} from
3182 the @var{description} argument to an @code{AC_DEFINE};
3183 see @ref{Defining Symbols}.
3185 For special needs, you can use the following macros.
3188 @defmac AH_TEMPLATE (@var{key}, @var{description})
3189 @ahindex{TEMPLATE}
3190 Tell @command{autoheader} to generate a template for @var{key}.  This macro
3191 generates standard templates just like @code{AC_DEFINE} when a
3192 @var{description} is given.
3194 For example:
3196 @example
3197 AH_TEMPLATE([CRAY_STACKSEG_END],
3198             [Define to one of _getb67, GETB67, getb67
3199              for Cray-2 and Cray-YMP systems.  This
3200              function is required for alloca.c support
3201              on those systems.])
3202 @end example
3204 @noindent
3205 generates the following template, with the description properly
3206 justified.
3208 @example
3209 /* Define to one of _getb67, GETB67, getb67 for Cray-2 and
3210    Cray-YMP systems.  This function is required for alloca.c
3211    support on those systems.  */
3212 #undef CRAY_STACKSEG_END
3213 @end example
3214 @end defmac
3217 @defmac AH_VERBATIM (@var{key}, @var{template})
3218 @ahindex{VERBATIM}
3219 Tell @command{autoheader} to include the @var{template} as-is in the header
3220 template file.  This @var{template} is associated with the @var{key},
3221 which is used to sort all the different templates and guarantee their
3222 uniqueness.  It should be a symbol that can be defined via @code{AC_DEFINE}.
3223 @end defmac
3226 @defmac AH_TOP (@var{text})
3227 @ahindex{TOP}
3228 Include @var{text} at the top of the header template file.
3229 @end defmac
3232 @defmac AH_BOTTOM (@var{text})
3233 @ahindex{BOTTOM}
3234 Include @var{text} at the bottom of the header template file.
3235 @end defmac
3238 Please note that @var{text} gets included ``verbatim'' to the template file,
3239 not to the resulting config header, so it can easily get mangled when the
3240 template is processed.  There is rarely a need for something other than
3242 @example
3243 AH_BOTTOM([#include <custom.h>])
3244 @end example
3248 @node Configuration Commands
3249 @section Running Arbitrary Configuration Commands
3250 @cindex Configuration commands
3251 @cindex Commands for configuration
3253 You can execute arbitrary commands before, during, and after
3254 @file{config.status} is run.  The three following macros accumulate the
3255 commands to run when they are called multiple times.
3256 @code{AC_CONFIG_COMMANDS} replaces the obsolete macro
3257 @code{AC_OUTPUT_COMMANDS}; see @ref{Obsolete Macros}, for details.
3259 @anchor{AC_CONFIG_COMMANDS}
3260 @defmac AC_CONFIG_COMMANDS (@var{tag}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3261 @acindex{CONFIG_COMMANDS}
3262 Specify additional shell commands to run at the end of
3263 @file{config.status}, and shell commands to initialize any variables
3264 from @command{configure}.  Associate the commands with @var{tag}.
3265 Since typically the @var{cmds} create a file, @var{tag} should
3266 naturally be the name of that file.  If needed, the directory hosting
3267 @var{tag} is created.  This macro is one of the instantiating macros;
3268 see @ref{Configuration Actions}.
3270 Here is an unrealistic example:
3271 @example
3272 fubar=42
3273 AC_CONFIG_COMMANDS([fubar],
3274                    [echo this is extra $fubar, and so on.],
3275                    [fubar=$fubar])
3276 @end example
3278 Here is a better one:
3279 @example
3280 AC_CONFIG_COMMANDS([timestamp], [date >timestamp])
3281 @end example
3282 @end defmac
3284 The following two macros look similar, but in fact they are not of the same
3285 breed: they are executed directly by @file{configure}, so you cannot use
3286 @file{config.status} to rerun them.
3288 @c Yet it is good to leave them here.  The user sees them together and
3289 @c decides which best fits their needs.
3291 @defmac AC_CONFIG_COMMANDS_PRE (@var{cmds})
3292 @acindex{CONFIG_COMMANDS_PRE}
3293 Execute the @var{cmds} right before creating @file{config.status}.
3295 This macro presents the last opportunity to call @code{AC_SUBST},
3296 @code{AC_DEFINE}, or @code{AC_CONFIG_FOOS} macros.
3297 @end defmac
3299 @defmac AC_CONFIG_COMMANDS_POST (@var{cmds})
3300 @acindex{CONFIG_COMMANDS_POST}
3301 Execute the @var{cmds} right after creating @file{config.status}.
3302 @end defmac
3307 @node Configuration Links
3308 @section Creating Configuration Links
3309 @cindex Configuration links
3310 @cindex Links for configuration
3312 You may find it convenient to create links whose destinations depend upon
3313 results of tests.  One can use @code{AC_CONFIG_COMMANDS} but the
3314 creation of relative symbolic links can be delicate when the package is
3315 built in a directory different from the source directory.
3317 @anchor{AC_CONFIG_LINKS}
3318 @defmac AC_CONFIG_LINKS (@var{dest}:@var{source}@dots{}, @ovar{cmds}, @
3319   @ovar{init-cmds})
3320 @acindex{CONFIG_LINKS}
3321 @cindex Links
3322 Make @code{AC_OUTPUT} link each of the existing files @var{source} to
3323 the corresponding link name @var{dest}.  Makes a symbolic link if
3324 possible, otherwise a hard link if possible, otherwise a copy.  The
3325 @var{dest} and @var{source} names should be relative to the top level
3326 source or build directory.  This macro is one of the instantiating
3327 macros; see @ref{Configuration Actions}.
3329 For example, this call:
3331 @example
3332 AC_CONFIG_LINKS([host.h:config/$machine.h
3333                 object.h:config/$obj_format.h])
3334 @end example
3336 @noindent
3337 creates in the current directory @file{host.h} as a link to
3338 @file{@var{srcdir}/config/$machine.h}, and @file{object.h} as a
3339 link to @file{@var{srcdir}/config/$obj_format.h}.
3341 The tempting value @samp{.} for @var{dest} is invalid: it makes it
3342 impossible for @samp{config.status} to guess the links to establish.
3344 One can then run:
3345 @example
3346 ./config.status host.h object.h
3347 @end example
3348 @noindent
3349 to create the links.
3350 @end defmac
3354 @node Subdirectories
3355 @section Configuring Other Packages in Subdirectories
3356 @cindex Configure subdirectories
3357 @cindex Subdirectory configure
3359 In most situations, calling @code{AC_OUTPUT} is sufficient to produce
3360 makefiles in subdirectories.  However, @command{configure} scripts
3361 that control more than one independent package can use
3362 @code{AC_CONFIG_SUBDIRS} to run @command{configure} scripts for other
3363 packages in subdirectories.
3365 @defmac AC_CONFIG_SUBDIRS (@var{dir} @dots{})
3366 @acindex{CONFIG_SUBDIRS}
3367 @ovindex subdirs
3368 Make @code{AC_OUTPUT} run @command{configure} in each subdirectory
3369 @var{dir} in the given blank-or-newline-separated list.  Each @var{dir} should
3370 be a literal, i.e., please do not use:
3372 @example
3373 if test "$package_foo_enabled" = yes; then
3374   $my_subdirs="$my_subdirs foo"
3376 AC_CONFIG_SUBDIRS([$my_subdirs])
3377 @end example
3379 @noindent
3380 because this prevents @samp{./configure --help=recursive} from
3381 displaying the options of the package @code{foo}.  Instead, you should
3382 write:
3384 @example
3385 if test "$package_foo_enabled" = yes; then
3386   AC_CONFIG_SUBDIRS([foo])
3388 @end example
3390 If a given @var{dir} is not found, an error is reported: if the
3391 subdirectory is optional, write:
3393 @example
3394 if test -d "$srcdir/foo"; then
3395   AC_CONFIG_SUBDIRS([foo])
3397 @end example
3399 @c NB: Yes, below we mean configure.in, not configure.ac.
3400 If a given @var{dir} contains @command{configure.gnu}, it is run instead
3401 of @command{configure}.  This is for packages that might use a
3402 non-Autoconf script @command{Configure}, which can't be called through a
3403 wrapper @command{configure} since it would be the same file on
3404 case-insensitive file systems.  Likewise, if a @var{dir} contains
3405 @file{configure.in} but no @command{configure}, the Cygnus
3406 @command{configure} script found by @code{AC_CONFIG_AUX_DIR} is used.
3408 The subdirectory @command{configure} scripts are given the same command
3409 line options that were given to this @command{configure} script, with minor
3410 changes if needed, which include:
3412 @itemize @minus
3413 @item
3414 adjusting a relative name for the cache file;
3416 @item
3417 adjusting a relative name for the source directory;
3419 @item
3420 propagating the current value of @code{$prefix}, including if it was
3421 defaulted, and if the default values of the top level and of the subdirectory
3422 @file{configure} differ.
3423 @end itemize
3425 This macro also sets the output variable @code{subdirs} to the list of
3426 directories @samp{@var{dir} @dots{}}.  Make rules can use
3427 this variable to determine which subdirectories to recurse into.
3429 This macro may be called multiple times.
3430 @end defmac
3432 @node Default Prefix
3433 @section Default Prefix
3434 @cindex Install prefix
3435 @cindex Prefix for install
3437 By default, @command{configure} sets the prefix for files it installs to
3438 @file{/usr/local}.  The user of @command{configure} can select a different
3439 prefix using the @option{--prefix} and @option{--exec-prefix} options.
3440 There are two ways to change the default: when creating
3441 @command{configure}, and when running it.
3443 Some software packages might want to install in a directory other than
3444 @file{/usr/local} by default.  To accomplish that, use the
3445 @code{AC_PREFIX_DEFAULT} macro.
3447 @defmac AC_PREFIX_DEFAULT (@var{prefix})
3448 @acindex{PREFIX_DEFAULT}
3449 Set the default installation prefix to @var{prefix} instead of
3450 @file{/usr/local}.
3451 @end defmac
3453 It may be convenient for users to have @command{configure} guess the
3454 installation prefix from the location of a related program that they
3455 have already installed.  If you wish to do that, you can call
3456 @code{AC_PREFIX_PROGRAM}.
3458 @anchor{AC_PREFIX_PROGRAM}
3459 @defmac AC_PREFIX_PROGRAM (@var{program})
3460 @acindex{PREFIX_PROGRAM}
3461 If the user did not specify an installation prefix (using the
3462 @option{--prefix} option), guess a value for it by looking for
3463 @var{program} in @env{PATH}, the way the shell does.  If @var{program}
3464 is found, set the prefix to the parent of the directory containing
3465 @var{program}, else default the prefix as described above
3466 (@file{/usr/local} or @code{AC_PREFIX_DEFAULT}).  For example, if
3467 @var{program} is @code{gcc} and the @env{PATH} contains
3468 @file{/usr/local/gnu/bin/gcc}, set the prefix to @file{/usr/local/gnu}.
3469 @end defmac
3473 @c ======================================================== Existing tests
3475 @node Existing Tests
3476 @chapter Existing Tests
3478 These macros test for particular system features that packages might
3479 need or want to use.  If you need to test for a kind of feature that
3480 none of these macros check for, you can probably do it by calling
3481 primitive test macros with appropriate arguments (@pxref{Writing
3482 Tests}).
3484 These tests print messages telling the user which feature they're
3485 checking for, and what they find.  They cache their results for future
3486 @command{configure} runs (@pxref{Caching Results}).
3488 Some of these macros set output variables.  @xref{Makefile
3489 Substitutions}, for how to get their values.  The phrase ``define
3490 @var{name}'' is used below as a shorthand to mean ``define the C
3491 preprocessor symbol @var{name} to the value 1''.  @xref{Defining
3492 Symbols}, for how to get those symbol definitions into your program.
3494 @menu
3495 * Common Behavior::             Macros' standard schemes
3496 * Alternative Programs::        Selecting between alternative programs
3497 * Files::                       Checking for the existence of files
3498 * Libraries::                   Library archives that might be missing
3499 * Library Functions::           C library functions that might be missing
3500 * Header Files::                Header files that might be missing
3501 * Declarations::                Declarations that may be missing
3502 * Structures::                  Structures or members that might be missing
3503 * Types::                       Types that might be missing
3504 * Compilers and Preprocessors::  Checking for compiling programs
3505 * System Services::             Operating system services
3506 * Posix Variants::              Special kludges for specific Posix variants
3507 * Erlang Libraries::            Checking for the existence of Erlang libraries
3508 @end menu
3510 @node Common Behavior
3511 @section Common Behavior
3512 @cindex Common autoconf behavior
3514 Much effort has been expended to make Autoconf easy to learn.  The most
3515 obvious way to reach this goal is simply to enforce standard interfaces
3516 and behaviors, avoiding exceptions as much as possible.  Because of
3517 history and inertia, unfortunately, there are still too many exceptions
3518 in Autoconf; nevertheless, this section describes some of the common
3519 rules.
3521 @menu
3522 * Standard Symbols::            Symbols defined by the macros
3523 * Default Includes::            Includes used by the generic macros
3524 @end menu
3526 @node Standard Symbols
3527 @subsection Standard Symbols
3528 @cindex Standard symbols
3530 All the generic macros that @code{AC_DEFINE} a symbol as a result of
3531 their test transform their @var{argument} values to a standard alphabet.
3532 First, @var{argument} is converted to upper case and any asterisks
3533 (@samp{*}) are each converted to @samp{P}.  Any remaining characters
3534 that are not alphanumeric are converted to underscores.
3536 For instance,
3538 @example
3539 AC_CHECK_TYPES([struct $Expensive*])
3540 @end example
3542 @noindent
3543 defines the symbol @samp{HAVE_STRUCT__EXPENSIVEP} if the check
3544 succeeds.
3547 @node Default Includes
3548 @subsection Default Includes
3549 @cindex Default includes
3550 @cindex Includes, default
3552 Several tests depend upon a set of header files.  Since these headers
3553 are not universally available, tests actually have to provide a set of
3554 protected includes, such as:
3556 @example
3557 @group
3558 #ifdef TIME_WITH_SYS_TIME
3559 # include <sys/time.h>
3560 # include <time.h>
3561 #else
3562 # ifdef HAVE_SYS_TIME_H
3563 #  include <sys/time.h>
3564 # else
3565 #  include <time.h>
3566 # endif
3567 #endif
3568 @end group
3569 @end example
3571 @noindent
3572 Unless you know exactly what you are doing, you should avoid using
3573 unconditional includes, and check the existence of the headers you
3574 include beforehand (@pxref{Header Files}).
3576 Most generic macros use the following macro to provide the default set
3577 of includes:
3579 @defmac AC_INCLUDES_DEFAULT (@ovar{include-directives})
3580 @acindex{INCLUDES_DEFAULT}
3581 Expand to @var{include-directives} if defined, otherwise to:
3583 @example
3584 @group
3585 #include <stdio.h>
3586 #ifdef HAVE_SYS_TYPES_H
3587 # include <sys/types.h>
3588 #endif
3589 #ifdef HAVE_SYS_STAT_H
3590 # include <sys/stat.h>
3591 #endif
3592 #ifdef STDC_HEADERS
3593 # include <stdlib.h>
3594 # include <stddef.h>
3595 #else
3596 # ifdef HAVE_STDLIB_H
3597 #  include <stdlib.h>
3598 # endif
3599 #endif
3600 #ifdef HAVE_STRING_H
3601 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
3602 #  include <memory.h>
3603 # endif
3604 # include <string.h>
3605 #endif
3606 #ifdef HAVE_STRINGS_H
3607 # include <strings.h>
3608 #endif
3609 #ifdef HAVE_INTTYPES_H
3610 # include <inttypes.h>
3611 #endif
3612 #ifdef HAVE_STDINT_H
3613 # include <stdint.h>
3614 #endif
3615 #ifdef HAVE_UNISTD_H
3616 # include <unistd.h>
3617 #endif
3618 @end group
3619 @end example
3621 If the default includes are used, then check for the presence of these
3622 headers and their compatibility, i.e., you don't need to run
3623 @code{AC_HEADER_STDC}, nor check for @file{stdlib.h} etc.
3625 These headers are checked for in the same order as they are included.
3626 For instance, on some systems @file{string.h} and @file{strings.h} both
3627 exist, but conflict.  Then @code{HAVE_STRING_H} is defined, not
3628 @code{HAVE_STRINGS_H}.
3629 @end defmac
3631 @node Alternative Programs
3632 @section Alternative Programs
3633 @cindex Programs, checking
3635 These macros check for the presence or behavior of particular programs.
3636 They are used to choose between several alternative programs and to
3637 decide what to do once one has been chosen.  If there is no macro
3638 specifically defined to check for a program you need, and you don't need
3639 to check for any special properties of it, then you can use one of the
3640 general program-check macros.
3642 @menu
3643 * Particular Programs::         Special handling to find certain programs
3644 * Generic Programs::            How to find other programs
3645 @end menu
3647 @node Particular Programs
3648 @subsection Particular Program Checks
3650 These macros check for particular programs---whether they exist, and
3651 in some cases whether they support certain features.
3653 @defmac AC_PROG_AWK
3654 @acindex{PROG_AWK}
3655 @ovindex AWK
3656 Check for @code{gawk}, @code{mawk}, @code{nawk}, and @code{awk}, in that
3657 order, and set output variable @code{AWK} to the first one that is found.
3658 It tries @code{gawk} first because that is reported to be the
3659 best implementation.
3660 @end defmac
3662 @defmac AC_PROG_GREP
3663 @acindex{PROG_GREP}
3664 @ovindex GREP
3665 Look for the best available @code{grep} or @code{ggrep} that accepts the
3666 longest input lines possible, and that supports multiple @option{-e} options.
3667 Set the output variable @code{GREP} to whatever is chosen.
3668 @xref{Limitations of Usual Tools}, for more information about
3669 portability problems with the @command{grep} command family.
3670 @end defmac
3672 @defmac AC_PROG_EGREP
3673 @acindex{PROG_EGREP}
3674 @ovindex EGREP
3675 Check whether @code{$GREP -E} works, or else look for the best available
3676 @code{egrep} or @code{gegrep} that accepts the longest input lines possible.
3677 Set the output variable @code{EGREP} to whatever is chosen.
3678 @end defmac
3680 @defmac AC_PROG_FGREP
3681 @acindex{PROG_FGREP}
3682 @ovindex FGREP
3683 Check whether @code{$GREP -F} works, or else look for the best available
3684 @code{fgrep} or @code{gfgrep} that accepts the longest input lines possible.
3685 Set the output variable @code{FGREP} to whatever is chosen.
3686 @end defmac
3688 @defmac AC_PROG_INSTALL
3689 @acindex{PROG_INSTALL}
3690 @ovindex INSTALL
3691 @ovindex INSTALL_PROGRAM
3692 @ovindex INSTALL_DATA
3693 @ovindex INSTALL_SCRIPT
3694 Set output variable @code{INSTALL} to the name of a @acronym{BSD}-compatible
3695 @command{install} program, if one is found in the current @env{PATH}.
3696 Otherwise, set @code{INSTALL} to @samp{@var{dir}/install-sh -c},
3697 checking the directories specified to @code{AC_CONFIG_AUX_DIR} (or its
3698 default directories) to determine @var{dir} (@pxref{Output}).  Also set
3699 the variables @code{INSTALL_PROGRAM} and @code{INSTALL_SCRIPT} to
3700 @samp{$@{INSTALL@}} and @code{INSTALL_DATA} to @samp{$@{INSTALL@} -m 644}.
3702 @samp{@@INSTALL@@} is special, as its value may vary for different
3703 configuration files.
3705 This macro screens out various instances of @command{install} known not to
3706 work.  It prefers to find a C program rather than a shell script, for
3707 speed.  Instead of @file{install-sh}, it can also use @file{install.sh},
3708 but that name is obsolete because some @command{make} programs have a rule
3709 that creates @file{install} from it if there is no makefile.
3711 Autoconf comes with a copy of @file{install-sh} that you can use.  If
3712 you use @code{AC_PROG_INSTALL}, you must include either
3713 @file{install-sh} or @file{install.sh} in your distribution; otherwise
3714 @command{configure} produces an error message saying it can't find
3715 them---even if the system you're on has a good @command{install} program.
3716 This check is a safety measure to prevent you from accidentally leaving
3717 that file out, which would prevent your package from installing on
3718 systems that don't have a @acronym{BSD}-compatible @command{install} program.
3720 If you need to use your own installation program because it has features
3721 not found in standard @command{install} programs, there is no reason to use
3722 @code{AC_PROG_INSTALL}; just put the file name of your program into your
3723 @file{Makefile.in} files.
3724 @end defmac
3726 @defmac AC_PROG_MKDIR_P
3727 @acindex{PROG_MKDIR_P}
3728 @ovindex MKDIR_P
3729 Set output variable @code{MKDIR_P} to a program that ensures that for
3730 each argument, a directory named by this argument exists, creating it
3731 and its parent directories if needed, and without race conditions when
3732 two instances of the program attempt to make the same directory at
3733 nearly the same time.
3735 This macro uses the @samp{mkdir -p} command if possible.  Otherwise, it
3736 falls back on invoking @command{install-sh} with the @option{-d} option,
3737 so your package should
3738 contain @file{install-sh} as described under @code{AC_PROG_INSTALL}.
3739 An @file{install-sh} file that predates Autoconf 2.60 or Automake 1.10
3740 is vulnerable to race conditions, so if you want to support parallel
3741 installs from
3742 different packages into the same directory you need to make sure you
3743 have an up-to-date @file{install-sh}.  In particular, be careful about
3744 using @samp{autoreconf -if} if your Automake predates Automake 1.10.
3746 This macro is related to the @code{AS_MKDIR_P} macro (@pxref{Programming
3747 in M4sh}), but it sets an output variable intended for use in other
3748 files, whereas @code{AS_MKDIR_P} is intended for use in scripts like
3749 @command{configure}.  Also, @code{AS_MKDIR_P} does not accept options,
3750 but @code{MKDIR_P} supports the @option{-m} option, e.g., a makefile
3751 might invoke @code{$(MKDIR_P) -m 0 dir} to create an inaccessible
3752 directory, and conversely a makefile should use @code{$(MKDIR_P) --
3753 $(FOO)} if @var{FOO} might yield a value that begins with @samp{-}.
3754 Finally, @code{AS_MKDIR_P} does not check for race condition
3755 vulnerability, whereas @code{AC_PROG_MKDIR_P} does.
3757 @samp{@@MKDIR_P@@} is special, as its value may vary for different
3758 configuration files.
3759 @end defmac
3761 @anchor{AC_PROG_LEX}
3762 @defmac AC_PROG_LEX
3763 @acindex{PROG_LEX}
3764 @ovindex LEX
3765 @ovindex LEXLIB
3766 @cvindex YYTEXT_POINTER
3767 @ovindex LEX_OUTPUT_ROOT
3768 If @code{flex} is found, set output variable @code{LEX} to @samp{flex}
3769 and @code{LEXLIB} to @option{-lfl}, if that library is in a standard
3770 place.  Otherwise set @code{LEX} to @samp{lex} and @code{LEXLIB} to
3771 @option{-ll}.
3773 Define @code{YYTEXT_POINTER} if @code{yytext} defaults to @samp{char *} instead
3774 of to @samp{char []}.  Also set output variable @code{LEX_OUTPUT_ROOT} to
3775 the base of the file name that the lexer generates; usually
3776 @file{lex.yy}, but sometimes something else.  These results vary
3777 according to whether @code{lex} or @code{flex} is being used.
3779 You are encouraged to use Flex in your sources, since it is both more
3780 pleasant to use than plain Lex and the C source it produces is portable.
3781 In order to ensure portability, however, you must either provide a
3782 function @code{yywrap} or, if you don't use it (e.g., your scanner has
3783 no @samp{#include}-like feature), simply include a @samp{%noyywrap}
3784 statement in the scanner's source.  Once this done, the scanner is
3785 portable (unless @emph{you} felt free to use nonportable constructs) and
3786 does not depend on any library.  In this case, and in this case only, it
3787 is suggested that you use this Autoconf snippet:
3789 @example
3790 AC_PROG_LEX
3791 if test "$LEX" != flex; then
3792   LEX="$SHELL $missing_dir/missing flex"
3793   AC_SUBST([LEX_OUTPUT_ROOT], [lex.yy])
3794   AC_SUBST([LEXLIB], [''])
3796 @end example
3798 The shell script @command{missing} can be found in the Automake
3799 distribution.
3801 To ensure backward compatibility, Automake's @code{AM_PROG_LEX} invokes
3802 (indirectly) this macro twice, which causes an annoying but benign
3803 ``@code{AC_PROG_LEX} invoked multiple times'' warning.  Future versions
3804 of Automake will fix this issue; meanwhile, just ignore this message.
3806 As part of running the test, this macro may delete any file in the
3807 configuration directory named @file{lex.yy.c} or @file{lexyy.c}.
3808 @end defmac
3810 @anchor{AC_PROG_LN_S}
3811 @defmac AC_PROG_LN_S
3812 @acindex{PROG_LN_S}
3813 @ovindex LN_S
3814 If @samp{ln -s} works on the current file system (the operating system
3815 and file system support symbolic links), set the output variable
3816 @code{LN_S} to @samp{ln -s}; otherwise, if @samp{ln} works, set
3817 @code{LN_S} to @samp{ln}, and otherwise set it to @samp{cp -p}.
3819 If you make a link in a directory other than the current directory, its
3820 meaning depends on whether @samp{ln} or @samp{ln -s} is used.  To safely
3821 create links using @samp{$(LN_S)}, either find out which form is used
3822 and adjust the arguments, or always invoke @code{ln} in the directory
3823 where the link is to be created.
3825 In other words, it does not work to do:
3826 @example
3827 $(LN_S) foo /x/bar
3828 @end example
3830 Instead, do:
3832 @example
3833 (cd /x && $(LN_S) foo bar)
3834 @end example
3835 @end defmac
3837 @defmac AC_PROG_RANLIB
3838 @acindex{PROG_RANLIB}
3839 @ovindex RANLIB
3840 Set output variable @code{RANLIB} to @samp{ranlib} if @code{ranlib}
3841 is found, and otherwise to @samp{:} (do nothing).
3842 @end defmac
3844 @defmac AC_PROG_SED
3845 @acindex{PROG_SED}
3846 @ovindex SED
3847 Set output variable @code{SED} to a Sed implementation that conforms to
3848 Posix and does not have arbitrary length limits.  Report an error if no
3849 acceptable Sed is found.  @xref{Limitations of Usual Tools}, for more
3850 information about portability problems with Sed.
3851 @end defmac
3853 @defmac AC_PROG_YACC
3854 @acindex{PROG_YACC}
3855 @ovindex YACC
3856 If @code{bison} is found, set output variable @code{YACC} to @samp{bison
3857 -y}.  Otherwise, if @code{byacc} is found, set @code{YACC} to
3858 @samp{byacc}.  Otherwise set @code{YACC} to @samp{yacc}.
3859 @end defmac
3861 @node Generic Programs
3862 @subsection Generic Program and File Checks
3864 These macros are used to find programs not covered by the ``particular''
3865 test macros.  If you need to check the behavior of a program as well as
3866 find out whether it is present, you have to write your own test for it
3867 (@pxref{Writing Tests}).  By default, these macros use the environment
3868 variable @env{PATH}.  If you need to check for a program that might not
3869 be in the user's @env{PATH}, you can pass a modified path to use
3870 instead, like this:
3872 @example
3873 AC_PATH_PROG([INETD], [inetd], [/usr/libexec/inetd],
3874              [$PATH$PATH_SEPARATOR/usr/libexec$PATH_SEPARATOR]dnl
3875 [/usr/sbin$PATH_SEPARATOR/usr/etc$PATH_SEPARATOR/etc])
3876 @end example
3878 You are strongly encouraged to declare the @var{variable} passed to
3879 @code{AC_CHECK_PROG} etc.@: as precious, @xref{Setting Output Variables},
3880 @code{AC_ARG_VAR}, for more details.
3882 @anchor{AC_CHECK_PROG}
3883 @defmac AC_CHECK_PROG (@var{variable}, @var{prog-to-check-for}, @
3884   @var{value-if-found}, @ovar{value-if-not-found}, @dvar{path, $PATH}, @
3885   @ovar{reject})
3886 @acindex{CHECK_PROG}
3887 Check whether program @var{prog-to-check-for} exists in @var{path}.  If
3888 it is found, set @var{variable} to @var{value-if-found}, otherwise to
3889 @var{value-if-not-found}, if given.  Always pass over @var{reject} (an
3890 absolute file name) even if it is the first found in the search path; in
3891 that case, set @var{variable} using the absolute file name of the
3892 @var{prog-to-check-for} found that is not @var{reject}.  If
3893 @var{variable} was already set, do nothing.  Calls @code{AC_SUBST} for
3894 @var{variable}.
3895 @end defmac
3897 @anchor{AC_CHECK_PROGS}
3898 @defmac AC_CHECK_PROGS (@var{variable}, @var{progs-to-check-for}, @
3899   @ovar{value-if-not-found}, @dvar{path, $PATH})
3900 @acindex{CHECK_PROGS}
3901 Check for each program in the blank-separated list
3902 @var{progs-to-check-for} existing in the @var{path}.  If one is found, set
3903 @var{variable} to the name of that program.  Otherwise, continue
3904 checking the next program in the list.  If none of the programs in the
3905 list are found, set @var{variable} to @var{value-if-not-found}; if
3906 @var{value-if-not-found} is not specified, the value of @var{variable}
3907 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3908 @end defmac
3910 @defmac AC_CHECK_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
3911   @ovar{value-if-not-found}, @dvar{path, $PATH})
3912 @acindex{CHECK_TARGET_TOOL}
3913 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3914 with a prefix of the target type as determined by
3915 @code{AC_CANONICAL_TARGET}, followed by a dash (@pxref{Canonicalizing}).
3916 If the tool cannot be found with a prefix, and if the build and target
3917 types are equal, then it is also searched for without a prefix.
3919 As noted in @ref{Specifying Names, , Specifying the system type}, the
3920 target is rarely specified, because most of the time it is the same
3921 as the host: it is the type of system for which any compiler tool in
3922 the package produces code.  What this macro looks for is,
3923 for example, @emph{a tool @r{(assembler, linker, etc.)}@: that the
3924 compiler driver @r{(@command{gcc} for the @acronym{GNU} C Compiler)}
3925 uses to produce objects, archives or executables}.
3926 @end defmac
3928 @defmac AC_CHECK_TOOL (@var{variable}, @var{prog-to-check-for}, @
3929   @ovar{value-if-not-found}, @dvar{path, $PATH})
3930 @acindex{CHECK_TOOL}
3931 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3932 with a prefix of the host type as determined by
3933 @code{AC_CANONICAL_HOST}, followed by a dash (@pxref{Canonicalizing}).
3934 For example, if the user runs @samp{configure --host=i386-gnu}, then
3935 this call:
3936 @example
3937 AC_CHECK_TOOL([RANLIB], [ranlib], [:])
3938 @end example
3939 @noindent
3940 sets @code{RANLIB} to @file{i386-gnu-ranlib} if that program exists in
3941 @var{path}, or otherwise to @samp{ranlib} if that program exists in
3942 @var{path}, or to @samp{:} if neither program exists.
3944 In the future, when cross-compiling this macro will @emph{only}
3945 accept program names that are prefixed with the host type.
3946 For more information, see @ref{Specifying Names, , Specifying the
3947 system type}.
3948 @end defmac
3950 @defmac AC_CHECK_TARGET_TOOLS (@var{variable}, @var{progs-to-check-for}, @
3951   @ovar{value-if-not-found}, @dvar{path, $PATH})
3952 @acindex{CHECK_TARGET_TOOLS}
3953 Like @code{AC_CHECK_TARGET_TOOL}, each of the tools in the list
3954 @var{progs-to-check-for} are checked with a prefix of the target type as
3955 determined by @code{AC_CANONICAL_TARGET}, followed by a dash
3956 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3957 prefix, and if the build and target types are equal, then the first one
3958 without a prefix is used.  If a tool is found, set @var{variable} to
3959 the name of that program.  If none of the tools in the list are found,
3960 set @var{variable} to @var{value-if-not-found}; if @var{value-if-not-found}
3961 is not specified, the value of @var{variable} is not changed.  Calls
3962 @code{AC_SUBST} for @var{variable}.
3963 @end defmac
3965 @defmac AC_CHECK_TOOLS (@var{variable}, @var{progs-to-check-for}, @
3966   @ovar{value-if-not-found}, @dvar{path, $PATH})
3967 @acindex{CHECK_TOOLS}
3968 Like @code{AC_CHECK_TOOL}, each of the tools in the list
3969 @var{progs-to-check-for} are checked with a prefix of the host type as
3970 determined by @code{AC_CANONICAL_HOST}, followed by a dash
3971 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3972 prefix, then the first one without a prefix is used.  If a tool is found,
3973 set @var{variable} to the name of that program.  If none of the tools in
3974 the list are found, set @var{variable} to @var{value-if-not-found}; if
3975 @var{value-if-not-found} is not specified, the value of @var{variable}
3976 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3978 In the future, when cross-compiling this macro will @emph{not}
3979 accept program names that are not prefixed with the host type.
3980 @end defmac
3982 @anchor{AC_PATH_PROG}
3983 @defmac AC_PATH_PROG (@var{variable}, @var{prog-to-check-for}, @
3984   @ovar{value-if-not-found}, @dvar{path, $PATH})
3985 @acindex{PATH_PROG}
3986 Like @code{AC_CHECK_PROG}, but set @var{variable} to the absolute
3987 name of @var{prog-to-check-for} if found.
3988 @end defmac
3990 @anchor{AC_PATH_PROGS}
3991 @defmac AC_PATH_PROGS (@var{variable}, @var{progs-to-check-for}, @
3992   @ovar{value-if-not-found}, @dvar{path, $PATH})
3993 @acindex{PATH_PROGS}
3994 Like @code{AC_CHECK_PROGS}, but if any of @var{progs-to-check-for}
3995 are found, set @var{variable} to the absolute name of the program
3996 found.
3997 @end defmac
3999 @defmac AC_PATH_PROGS_FEATURE_CHECK (@var{variable}, @
4000   @var{progs-to-check-for}, @var{feature-test}, @
4001   @ovar{action-if-not-found}, @dvar{path, $PATH})
4002 @acindex{PATH_PROGS_FEATURE_CHECK}
4003 This macro was introduced in Autoconf 2.62.  If @var{variable} is not
4004 empty, then set the cache variable @code{$ac_cv_path_@var{variable}} to
4005 its value.  Otherwise, check for each program in the blank-separated
4006 list @var{progs-to-check-for} existing in @var{path}.  For each program
4007 found, execute @var{feature-test} with @code{$ac_path_@var{variable}}
4008 set to the absolute name of the candidate program.  If no invocation of
4009 @var{feature-test} sets the shell variable
4010 @code{$ac_cv_path_@var{variable}}, then @var{action-if-not-found} is
4011 executed.  @var{feature-test} will be run even when
4012 @code{ac_cv_path_@var{variable}} is set, to provide the ability to
4013 choose a better candidate found later in @var{path}; to accept the
4014 current setting and bypass all futher checks, @var{feature-test} can
4015 execute @code{ac_path_@var{variable}_found=:}.
4017 Note that this macro has some subtle differences from
4018 @code{AC_CHECK_PROGS}.  It is designed to be run inside
4019 @code{AC_CACHE_VAL}, therefore, it should have no side effects.  In
4020 particular, @var{variable} is not set to the final value of
4021 @code{ac_cv_path_@var{variable}}, nor is @code{AC_SUBST} automatically
4022 run.  Also, on failure, any action can be performed, whereas
4023 @code{AC_CHECK_PROGS} only performs
4024 @code{@var{variable}=@var{value-if-not-found}}.
4026 Here is an example, similar to what Autoconf uses in its own configure
4027 script.  It will search for an implementation of @command{m4} that
4028 supports the @code{indir} builtin, even if it goes by the name
4029 @command{gm4} or is not the first implementation on @env{PATH}.
4031 @example
4032 AC_CACHE_CHECK([for m4 that supports indir], [ac_cv_path_M4],
4033   [AC_PATH_PROGS_FEATURE_CHECK([M4], [m4 gm4],
4034     [[m4out=`echo 'changequote([,])indir([divnum])' | $ac_path_M4`
4035       test "x$m4out" = x0 \
4036       && ac_cv_path_M4=$ac_path_M4 ac_path_M4_found=:]],
4037     [AC_MSG_ERROR([could not find m4 that supports indir])])])
4038 AC_SUBST([M4], [$ac_cv_path_M4])
4039 @end example
4040 @end defmac
4042 @defmac AC_PATH_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
4043   @ovar{value-if-not-found}, @dvar{path, $PATH})
4044 @acindex{PATH_TARGET_TOOL}
4045 Like @code{AC_CHECK_TARGET_TOOL}, but set @var{variable} to the absolute
4046 name of the program if it is found.
4047 @end defmac
4049 @defmac AC_PATH_TOOL (@var{variable}, @var{prog-to-check-for}, @
4050   @ovar{value-if-not-found}, @dvar{path, $PATH})
4051 @acindex{PATH_TOOL}
4052 Like @code{AC_CHECK_TOOL}, but set @var{variable} to the absolute
4053 name of the program if it is found.
4055 In the future, when cross-compiling this macro will @emph{not}
4056 accept program names that are not prefixed with the host type.
4057 @end defmac
4060 @node Files
4061 @section Files
4062 @cindex File, checking
4064 You might also need to check for the existence of files.  Before using
4065 these macros, ask yourself whether a runtime test might not be a better
4066 solution.  Be aware that, like most Autoconf macros, they test a feature
4067 of the host machine, and therefore, they die when cross-compiling.
4069 @defmac AC_CHECK_FILE (@var{file}, @ovar{action-if-found}, @
4070   @ovar{action-if-not-found})
4071 @acindex{CHECK_FILE}
4072 Check whether file @var{file} exists on the native system.  If it is
4073 found, execute @var{action-if-found}, otherwise do
4074 @var{action-if-not-found}, if given.
4075 @end defmac
4077 @defmac AC_CHECK_FILES (@var{files}, @ovar{action-if-found}, @
4078   @ovar{action-if-not-found})
4079 @acindex{CHECK_FILES}
4080 Executes @code{AC_CHECK_FILE} once for each file listed in @var{files}.
4081 Additionally, defines @samp{HAVE_@var{file}} (@pxref{Standard Symbols})
4082 for each file found.
4083 @end defmac
4086 @node Libraries
4087 @section Library Files
4088 @cindex Library, checking
4090 The following macros check for the presence of certain C, C++, or Fortran
4091 library archive files.
4093 @anchor{AC_CHECK_LIB}
4094 @defmac AC_CHECK_LIB (@var{library}, @var{function}, @
4095   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4096 @acindex{CHECK_LIB}
4097 Test whether the library @var{library} is available by trying to link
4098 a test program that calls function @var{function} with the library.
4099 @var{function} should be a function provided by the library.
4100 Use the base
4101 name of the library; e.g., to check for @option{-lmp}, use @samp{mp} as
4102 the @var{library} argument.
4104 @var{action-if-found} is a list of shell commands to run if the link
4105 with the library succeeds; @var{action-if-not-found} is a list of shell
4106 commands to run if the link fails.  If @var{action-if-found} is not
4107 specified, the default action prepends @option{-l@var{library}} to
4108 @code{LIBS} and defines @samp{HAVE_LIB@var{library}} (in all
4109 capitals).  This macro is intended to support building @code{LIBS} in
4110 a right-to-left (least-dependent to most-dependent) fashion such that
4111 library dependencies are satisfied as a natural side effect of
4112 consecutive tests.  Linkers are sensitive to library ordering
4113 so the order in which @code{LIBS} is generated is important to reliable
4114 detection of libraries.
4116 If linking with @var{library} results in unresolved symbols that would
4117 be resolved by linking with additional libraries, give those libraries
4118 as the @var{other-libraries} argument, separated by spaces:
4119 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4120 that @var{library} is present, because linking the test program
4121 always fails with unresolved symbols.  The @var{other-libraries} argument
4122 should be limited to cases where it is desirable to test for one library
4123 in the presence of another that is not already in @code{LIBS}.
4125 @code{AC_CHECK_LIB} requires some care in usage, and should be avoided
4126 in some common cases.  Many standard functions like @code{gethostbyname}
4127 appear in the standard C library on some hosts, and in special libraries
4128 like @code{nsl} on other hosts.  On some hosts the special libraries
4129 contain variant implementations that you may not want to use.  These
4130 days it is normally better to use @code{AC_SEARCH_LIBS([gethostbyname],
4131 [nsl])} instead of @code{AC_CHECK_LIB([nsl], [gethostbyname])}.
4132 @end defmac
4134 @anchor{AC_SEARCH_LIBS}
4135 @defmac AC_SEARCH_LIBS (@var{function}, @var{search-libs}, @
4136   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4137 @acindex{SEARCH_LIBS}
4138 Search for a library defining @var{function} if it's not already
4139 available.  This equates to calling
4140 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])])} first with
4141 no libraries, then for each library listed in @var{search-libs}.
4143 Add @option{-l@var{library}} to @code{LIBS} for the first library found
4144 to contain @var{function}, and run @var{action-if-found}.  If the
4145 function is not found, run @var{action-if-not-found}.
4147 If linking with @var{library} results in unresolved symbols that would
4148 be resolved by linking with additional libraries, give those libraries
4149 as the @var{other-libraries} argument, separated by spaces:
4150 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4151 that @var{function} is present, because linking the test program
4152 always fails with unresolved symbols.
4153 @end defmac
4157 @node Library Functions
4158 @section Library Functions
4160 The following macros check for particular C library functions.
4161 If there is no macro specifically defined to check for a function you need,
4162 and you don't need to check for any special properties of
4163 it, then you can use one of the general function-check macros.
4165 @menu
4166 * Function Portability::        Pitfalls with usual functions
4167 * Particular Functions::        Special handling to find certain functions
4168 * Generic Functions::           How to find other functions
4169 @end menu
4171 @node Function Portability
4172 @subsection Portability of C Functions
4173 @cindex Portability of C functions
4174 @cindex C function portability
4176 Most usual functions can either be missing, or be buggy, or be limited
4177 on some architectures.  This section tries to make an inventory of these
4178 portability issues.  By definition, this list always requires
4179 additions.  Please help us keeping it as complete as possible.
4181 @table @asis
4182 @item @code{exit}
4183 @c @fuindex exit
4184 @prindex @code{exit}
4185 On ancient hosts, @code{exit} returned @code{int}.
4186 This is because @code{exit} predates @code{void}, and there was a long
4187 tradition of it returning @code{int}.
4189 On current hosts, the problem more likely is that @code{exit} is not
4190 declared, due to C++ problems of some sort or another.  For this reason
4191 we suggest that test programs not invoke @code{exit}, but return from
4192 @code{main} instead.
4194 @item @code{free}
4195 @c @fuindex free
4196 @prindex @code{free}
4197 The C standard says a call @code{free (NULL)} does nothing, but
4198 some old systems don't support this (e.g., NextStep).
4200 @item @code{isinf}
4201 @itemx @code{isnan}
4202 @c @fuindex isinf
4203 @c @fuindex isnan
4204 @prindex @code{isinf}
4205 @prindex @code{isnan}
4206 The C99 standard says that @code{isinf} and @code{isnan} are
4207 macros.  On some systems just macros are available
4208 (e.g., @acronym{HP-UX} and Solaris 10), on
4209 some systems both macros and functions (e.g., glibc 2.3.2), and on some
4210 systems only functions (e.g., IRIX 6 and Solaris 9).  In some cases
4211 these functions are declared in nonstandard headers like
4212 @code{<sunmath.h>} and defined in non-default libraries like
4213 @option{-lm} or @option{-lsunmath}.
4215 The C99 @code{isinf} and @code{isnan} macros work correctly with
4216 @code{long double} arguments, but pre-C99 systems that use functions
4217 typically assume @code{double} arguments.  On such a system,
4218 @code{isinf} incorrectly returns true for a finite @code{long double}
4219 argument that is outside the range of @code{double}.
4221 To work around this porting mess, you can use code like the following.
4223 @smallexample
4224 #include <math.h>
4226 #ifndef isnan
4227 # define isnan(x) \
4228     (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
4229      : sizeof (x) == sizeof (double) ? isnan_d (x) \
4230      : isnan_f (x))
4231 static inline int isnan_f  (float       x) @{ return x != x; @}
4232 static inline int isnan_d  (double      x) @{ return x != x; @}
4233 static inline int isnan_ld (long double x) @{ return x != x; @}
4234 #endif
4236 #ifndef isinf
4237 # define isinf(x) \
4238     (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
4239      : sizeof (x) == sizeof (double) ? isinf_d (x) \
4240      : isinf_f (x))
4241 static inline int isinf_f  (float       x) @{ return isnan (x - x); @}
4242 static inline int isinf_d  (double      x) @{ return isnan (x - x); @}
4243 static inline int isinf_ld (long double x) @{ return isnan (x - x); @}
4244 #endif
4245 @end smallexample
4247 Use @code{AC_C_INLINE} (@pxref{C Compiler}) so that this code works on
4248 compilers that lack the @code{inline} keyword.  Some optimizing
4249 compilers mishandle these definitions, but systems with that bug
4250 typically have missing or broken @code{isnan} functions anyway, so it's
4251 probably not worth worrying about.
4253 @item @code{malloc}
4254 @c @fuindex malloc
4255 @prindex @code{malloc}
4256 The C standard says a call @code{malloc (0)} is implementation
4257 dependent.  It can return either @code{NULL} or a new non-null pointer.
4258 The latter is more common (e.g., the @acronym{GNU} C Library) but is by
4259 no means universal.  @code{AC_FUNC_MALLOC}
4260 can be used to insist on non-@code{NULL} (@pxref{Particular Functions}).
4262 @item @code{putenv}
4263 @c @fuindex putenv
4264 @prindex @code{putenv}
4265 Posix prefers @code{setenv} to @code{putenv}; among other things,
4266 @code{putenv} is not required of all Posix implementations, but
4267 @code{setenv} is.
4269 Posix specifies that @code{putenv} puts the given string directly in
4270 @code{environ}, but some systems make a copy of it instead (e.g.,
4271 glibc 2.0, or @acronym{BSD}).  And when a copy is made, @code{unsetenv} might
4272 not free it, causing a memory leak (e.g., Free@acronym{BSD} 4).
4274 On some systems @code{putenv ("FOO")} removes @samp{FOO} from the
4275 environment, but this is not standard usage and it dumps core
4276 on some systems (e.g., AIX).
4278 On MinGW, a call @code{putenv ("FOO=")} removes @samp{FOO} from the
4279 environment, rather than inserting it with an empty value.
4281 @item @code{realloc}
4282 @c @fuindex realloc
4283 @prindex @code{realloc}
4284 The C standard says a call @code{realloc (NULL, size)} is equivalent
4285 to @code{malloc (size)}, but some old systems don't support this (e.g.,
4286 NextStep).
4288 @item @code{signal} handler
4289 @c @fuindex signal
4290 @prindex @code{signal}
4291 Normally @code{signal} takes a handler function with a return type of
4292 @code{void}, but some old systems required @code{int} instead.  Any
4293 actual @code{int} value returned is not used; this is only a
4294 difference in the function prototype demanded.
4296 All systems we know of in current use return @code{void}.  The
4297 @code{int} was to support K&R C, where of course @code{void} is not
4298 available.  @code{AC_TYPE_SIGNAL} (@pxref{Particular Types}) can be
4299 used to establish the correct type in all cases.
4301 @item @code{snprintf}
4302 @c @fuindex snprintf
4303 @prindex @code{snprintf}
4304 @c @fuindex vsnprintf
4305 @prindex @code{vsnprintf}
4306 The C99 standard says that if the output array isn't big enough
4307 and if no other errors occur, @code{snprintf} and @code{vsnprintf}
4308 truncate the output and return the number of bytes that ought to have
4309 been produced.  Some older systems return the truncated length (e.g.,
4310 @acronym{GNU} C Library 2.0.x or @sc{irix} 6.5), some a negative value
4311 (e.g., earlier @acronym{GNU} C Library versions), and some the buffer
4312 length without truncation (e.g., 32-bit Solaris 7).  Also, some buggy
4313 older systems ignore the length and overrun the buffer (e.g., 64-bit
4314 Solaris 7).
4316 @item @code{sprintf}
4317 @c @fuindex sprintf
4318 @prindex @code{sprintf}
4319 @c @fuindex vsprintf
4320 @prindex @code{vsprintf}
4321 The C standard says @code{sprintf} and @code{vsprintf} return the
4322 number of bytes written.  On some ancient systems (SunOS 4 for
4323 instance) they return the buffer pointer instead, but these no
4324 longer need to be worried about.
4326 @item @code{sscanf}
4327 @c @fuindex sscanf
4328 @prindex @code{sscanf}
4329 On various old systems, e.g., @acronym{HP-UX} 9, @code{sscanf} requires
4330 that its
4331 input string be writable (though it doesn't actually change it).  This
4332 can be a problem when using @command{gcc} since it normally puts
4333 constant strings in read-only memory (@pxref{Incompatibilities,
4334 Incompatibilities of @acronym{GCC}, , gcc, Using and
4335 Porting the @acronym{GNU} Compiler Collection}).  Apparently in some cases even
4336 having format strings read-only can be a problem.
4338 @item @code{strerror_r}
4339 @c @fuindex strerror_r
4340 @prindex @code{strerror_r}
4341 Posix specifies that @code{strerror_r} returns an @code{int}, but many
4342 systems (e.g., @acronym{GNU} C Library version 2.2.4) provide a
4343 different version returning a @code{char *}.  @code{AC_FUNC_STRERROR_R}
4344 can detect which is in use (@pxref{Particular Functions}).
4346 @item @code{strnlen}
4347 @c @fuindex strnlen
4348 @prindex @code{strnlen}
4349 @acronym{AIX} 4.3 provides a broken version which produces the
4350 following results:
4352 @example
4353 strnlen ("foobar", 0) = 0
4354 strnlen ("foobar", 1) = 3
4355 strnlen ("foobar", 2) = 2
4356 strnlen ("foobar", 3) = 1
4357 strnlen ("foobar", 4) = 0
4358 strnlen ("foobar", 5) = 6
4359 strnlen ("foobar", 6) = 6
4360 strnlen ("foobar", 7) = 6
4361 strnlen ("foobar", 8) = 6
4362 strnlen ("foobar", 9) = 6
4363 @end example
4365 @item @code{sysconf}
4366 @c @fuindex sysconf
4367 @prindex @code{sysconf}
4368 @code{_SC_PAGESIZE} is standard, but some older systems (e.g., @acronym{HP-UX}
4369 9) have @code{_SC_PAGE_SIZE} instead.  This can be tested with
4370 @code{#ifdef}.
4372 @item @code{unlink}
4373 @c @fuindex unlink
4374 @prindex @code{unlink}
4375 The Posix spec says that @code{unlink} causes the given file to be
4376 removed only after there are no more open file handles for it.  Some
4377 non-Posix hosts have trouble with this requirement, though,
4378 and some @acronym{DOS} variants even corrupt the file system.
4380 @item @code{unsetenv}
4381 @c @fuindex unsetenv
4382 @prindex @code{unsetenv}
4383 On MinGW, @code{unsetenv} is not available, but a variable @samp{FOO}
4384 can be removed with a call @code{putenv ("FOO=")}, as described under
4385 @code{putenv} above.
4387 @item @code{va_copy}
4388 @c @fuindex va_copy
4389 @prindex @code{va_copy}
4390 The C99 standard provides @code{va_copy} for copying
4391 @code{va_list} variables.  It may be available in older environments
4392 too, though possibly as @code{__va_copy} (e.g., @command{gcc} in strict
4393 pre-C99 mode).  These can be tested with @code{#ifdef}.  A fallback to
4394 @code{memcpy (&dst, &src, sizeof (va_list))} gives maximum
4395 portability.
4397 @item @code{va_list}
4398 @c @fuindex va_list
4399 @prindex @code{va_list}
4400 @code{va_list} is not necessarily just a pointer.  It can be a
4401 @code{struct} (e.g., @command{gcc} on Alpha), which means @code{NULL} is
4402 not portable.  Or it can be an array (e.g., @command{gcc} in some
4403 PowerPC configurations), which means as a function parameter it can be
4404 effectively call-by-reference and library routines might modify the
4405 value back in the caller (e.g., @code{vsnprintf} in the @acronym{GNU} C Library
4406 2.1).
4408 @item Signed @code{>>}
4409 Normally the C @code{>>} right shift of a signed type replicates the
4410 high bit, giving a so-called ``arithmetic'' shift.  But care should be
4411 taken since Standard C doesn't require that behavior.  On those
4412 few processors without a native arithmetic shift (for instance Cray
4413 vector systems) zero bits may be shifted in, the same as a shift of an
4414 unsigned type.
4416 @item Integer @code{/}
4417 C divides signed integers by truncating their quotient toward zero,
4418 yielding the same result as Fortran.  However, before C99 the standard
4419 allowed C implementations to take the floor or ceiling of the quotient
4420 in some cases.  Hardly any implementations took advantage of this
4421 freedom, though, and it's probably not worth worrying about this issue
4422 nowadays.
4423 @end table
4426 @node Particular Functions
4427 @subsection Particular Function Checks
4428 @cindex Function, checking
4430 These macros check for particular C functions---whether they exist, and
4431 in some cases how they respond when given certain arguments.
4433 @anchor{AC_FUNC_ALLOCA}
4434 @defmac AC_FUNC_ALLOCA
4435 @acindex{FUNC_ALLOCA}
4436 @cvindex C_ALLOCA
4437 @cvindex HAVE_ALLOCA_H
4438 @ovindex ALLOCA
4439 @c @fuindex alloca
4440 @prindex @code{alloca}
4441 @hdrindex{alloca.h}
4442 Check how to get @code{alloca}.  Tries to get a builtin version by
4443 checking for @file{alloca.h} or the predefined C preprocessor macros
4444 @code{__GNUC__} and @code{_AIX}.  If this macro finds @file{alloca.h},
4445 it defines @code{HAVE_ALLOCA_H}.
4447 If those attempts fail, it looks for the function in the standard C
4448 library.  If any of those methods succeed, it defines
4449 @code{HAVE_ALLOCA}.  Otherwise, it sets the output variable
4450 @code{ALLOCA} to @samp{$@{LIBOBJDIR@}alloca.o} and defines
4451 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
4452 garbage collect).  This variable is separate from @code{LIBOBJS} so
4453 multiple programs can share the value of @code{ALLOCA} without needing
4454 to create an actual library, in case only some of them use the code in
4455 @code{LIBOBJS}.  The @samp{$@{LIBOBJDIR@}} prefix serves the same
4456 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
4458 This macro does not try to get @code{alloca} from the System V R3
4459 @file{libPW} or the System V R4 @file{libucb} because those libraries
4460 contain some incompatible functions that cause trouble.  Some versions
4461 do not even contain @code{alloca} or contain a buggy version.  If you
4462 still want to use their @code{alloca}, use @code{ar} to extract
4463 @file{alloca.o} from them instead of compiling @file{alloca.c}.
4465 Source files that use @code{alloca} should start with a piece of code
4466 like the following, to declare it properly.
4468 @example
4469 @group
4470 #ifdef HAVE_ALLOCA_H
4471 # include <alloca.h>
4472 #elif defined __GNUC__
4473 # define alloca __builtin_alloca
4474 #elif defined _AIX
4475 # define alloca __alloca
4476 #elif defined _MSC_VER
4477 # include <malloc.h>
4478 # define alloca _alloca
4479 #else
4480 # include <stddef.h>
4481 # ifdef  __cplusplus
4482 extern "C"
4483 # endif
4484 void *alloca (size_t);
4485 #endif
4486 @end group
4487 @end example
4488 @end defmac
4490 @defmac AC_FUNC_CHOWN
4491 @acindex{FUNC_CHOWN}
4492 @cvindex HAVE_CHOWN
4493 @c @fuindex chown
4494 @prindex @code{chown}
4495 If the @code{chown} function is available and works (in particular, it
4496 should accept @option{-1} for @code{uid} and @code{gid}), define
4497 @code{HAVE_CHOWN}.
4498 @end defmac
4500 @anchor{AC_FUNC_CLOSEDIR_VOID}
4501 @defmac AC_FUNC_CLOSEDIR_VOID
4502 @acindex{FUNC_CLOSEDIR_VOID}
4503 @cvindex CLOSEDIR_VOID
4504 @c @fuindex closedir
4505 @prindex @code{closedir}
4506 If the @code{closedir} function does not return a meaningful value,
4507 define @code{CLOSEDIR_VOID}.  Otherwise, callers ought to check its
4508 return value for an error indicator.
4510 Currently this test is implemented by running a test program.  When
4511 cross compiling the pessimistic assumption that @code{closedir} does not
4512 return a meaningful value is made.
4514 This macro is obsolescent, as @code{closedir} returns a meaningful value
4515 on current systems.  New programs need not use this macro.
4516 @end defmac
4518 @defmac AC_FUNC_ERROR_AT_LINE
4519 @acindex{FUNC_ERROR_AT_LINE}
4520 @c @fuindex error_at_line
4521 @prindex @code{error_at_line}
4522 If the @code{error_at_line} function is not found, require an
4523 @code{AC_LIBOBJ} replacement of @samp{error}.
4524 @end defmac
4526 @defmac AC_FUNC_FNMATCH
4527 @acindex{FUNC_FNMATCH}
4528 @c @fuindex fnmatch
4529 @prindex @code{fnmatch}
4530 If the @code{fnmatch} function conforms to Posix, define
4531 @code{HAVE_FNMATCH}.  Detect common implementation bugs, for example,
4532 the bugs in Solaris 2.4.
4534 Unlike the other specific
4535 @code{AC_FUNC} macros, @code{AC_FUNC_FNMATCH} does not replace a
4536 broken/missing @code{fnmatch}.  This is for historical reasons.
4537 See @code{AC_REPLACE_FNMATCH} below.
4539 This macro is obsolescent.  New programs should use Gnulib's
4540 @code{fnmatch-posix} module.  @xref{Gnulib}.
4541 @end defmac
4543 @defmac AC_FUNC_FNMATCH_GNU
4544 @acindex{FUNC_FNMATCH_GNU}
4545 @c @fuindex fnmatch
4546 @prindex @code{fnmatch}
4547 Behave like @code{AC_REPLACE_FNMATCH} (@emph{replace}) but also test
4548 whether @code{fnmatch} supports @acronym{GNU} extensions.  Detect common
4549 implementation bugs, for example, the bugs in the @acronym{GNU} C
4550 Library 2.1.
4552 This macro is obsolescent.  New programs should use Gnulib's
4553 @code{fnmatch-gnu} module.  @xref{Gnulib}.
4554 @end defmac
4556 @anchor{AC_FUNC_FORK}
4557 @defmac AC_FUNC_FORK
4558 @acindex{FUNC_FORK}
4559 @cvindex HAVE_VFORK_H
4560 @cvindex HAVE_WORKING_FORK
4561 @cvindex HAVE_WORKING_VFORK
4562 @cvindex vfork
4563 @c @fuindex fork
4564 @prindex @code{fork}
4565 @c @fuindex vfork
4566 @prindex @code{vfork}
4567 @hdrindex{vfork.h}
4568 This macro checks for the @code{fork} and @code{vfork} functions.  If a
4569 working @code{fork} is found, define @code{HAVE_WORKING_FORK}.  This macro
4570 checks whether @code{fork} is just a stub by trying to run it.
4572 If @file{vfork.h} is found, define @code{HAVE_VFORK_H}.  If a working
4573 @code{vfork} is found, define @code{HAVE_WORKING_VFORK}.  Otherwise,
4574 define @code{vfork} to be @code{fork} for backward compatibility with
4575 previous versions of @command{autoconf}.  This macro checks for several known
4576 errors in implementations of @code{vfork} and considers the system to not
4577 have a working @code{vfork} if it detects any of them.  It is not considered
4578 to be an implementation error if a child's invocation of @code{signal}
4579 modifies the parent's signal handler, since child processes rarely change
4580 their signal handlers.
4582 Since this macro defines @code{vfork} only for backward compatibility with
4583 previous versions of @command{autoconf} you're encouraged to define it
4584 yourself in new code:
4585 @example
4586 @group
4587 #ifndef HAVE_WORKING_VFORK
4588 # define vfork fork
4589 #endif
4590 @end group
4591 @end example
4592 @end defmac
4594 @defmac AC_FUNC_FSEEKO
4595 @acindex{FUNC_FSEEKO}
4596 @cvindex _LARGEFILE_SOURCE
4597 @cvindex HAVE_FSEEKO
4598 @c @fuindex fseeko
4599 @prindex @code{fseeko}
4600 If the @code{fseeko} function is available, define @code{HAVE_FSEEKO}.
4601 Define @code{_LARGEFILE_SOURCE} if necessary to make the prototype
4602 visible on some systems (e.g., glibc 2.2).  Otherwise linkage problems
4603 may occur when compiling with @code{AC_SYS_LARGEFILE} on
4604 largefile-sensitive systems where @code{off_t} does not default to a
4605 64bit entity.
4606 @end defmac
4608 @defmac AC_FUNC_GETGROUPS
4609 @acindex{FUNC_GETGROUPS}
4610 @cvindex HAVE_GETGROUPS
4611 @ovindex GETGROUPS_LIBS
4612 @c @fuindex getgroups
4613 @prindex @code{getgroups}
4614 If the @code{getgroups} function is available and works (unlike on
4615 Ultrix 4.3, where @samp{getgroups (0, 0)} always fails), define
4616 @code{HAVE_GETGROUPS}.  Set @code{GETGROUPS_LIBS} to any libraries
4617 needed to get that function.  This macro runs @code{AC_TYPE_GETGROUPS}.
4618 @end defmac
4620 @anchor{AC_FUNC_GETLOADAVG}
4621 @defmac AC_FUNC_GETLOADAVG
4622 @acindex{FUNC_GETLOADAVG}
4623 @cvindex SVR4
4624 @cvindex DGUX
4625 @cvindex UMAX
4626 @cvindex UMAX4_3
4627 @cvindex HAVE_NLIST_H
4628 @cvindex NLIST_NAME_UNION
4629 @cvindex GETLOADAVG_PRIVILEGED
4630 @cvindex NEED_SETGID
4631 @cvindex C_GETLOADAVG
4632 @ovindex LIBOBJS
4633 @ovindex NEED_SETGID
4634 @ovindex KMEM_GROUP
4635 @ovindex GETLOADAVG_LIBS
4636 @c @fuindex getloadavg
4637 @prindex @code{getloadavg}
4638 Check how to get the system load averages.  To perform its tests
4639 properly, this macro needs the file @file{getloadavg.c}; therefore, be
4640 sure to set the @code{AC_LIBOBJ} replacement directory properly (see
4641 @ref{Generic Functions}, @code{AC_CONFIG_LIBOBJ_DIR}).
4643 If the system has the @code{getloadavg} function, define
4644 @code{HAVE_GETLOADAVG}, and set @code{GETLOADAVG_LIBS} to any libraries
4645 necessary to get that function.  Also add @code{GETLOADAVG_LIBS} to
4646 @code{LIBS}.  Otherwise, require an @code{AC_LIBOBJ} replacement for
4647 @samp{getloadavg} with source code in @file{@var{dir}/getloadavg.c}, and
4648 possibly define several other C preprocessor macros and output
4649 variables:
4651 @enumerate
4652 @item
4653 Define @code{C_GETLOADAVG}.
4655 @item
4656 Define @code{SVR4}, @code{DGUX}, @code{UMAX}, or @code{UMAX4_3} if on
4657 those systems.
4659 @item
4660 @hdrindex{nlist.h}
4661 If @file{nlist.h} is found, define @code{HAVE_NLIST_H}.
4663 @item
4664 If @samp{struct nlist} has an @samp{n_un.n_name} member, define
4665 @code{HAVE_STRUCT_NLIST_N_UN_N_NAME}.  The obsolete symbol
4666 @code{NLIST_NAME_UNION} is still defined, but do not depend upon it.
4668 @item
4669 Programs may need to be installed set-group-ID (or set-user-ID) for
4670 @code{getloadavg} to work.  In this case, define
4671 @code{GETLOADAVG_PRIVILEGED}, set the output variable @code{NEED_SETGID}
4672 to @samp{true} (and otherwise to @samp{false}), and set
4673 @code{KMEM_GROUP} to the name of the group that should own the installed
4674 program.
4675 @end enumerate
4677 The @code{AC_FUNC_GETLOADAVG} macro is obsolescent.  New programs should
4678 use Gnulib's @code{getloadavg} module.  @xref{Gnulib}.
4679 @end defmac
4681 @anchor{AC_FUNC_GETMNTENT}
4682 @defmac AC_FUNC_GETMNTENT
4683 @acindex{FUNC_GETMNTENT}
4684 @cvindex HAVE_GETMNTENT
4685 @c @fuindex getmntent
4686 @prindex @code{getmntent}
4687 Check for @code{getmntent} in the standard C library, and then in the
4688 @file{sun}, @file{seq}, and @file{gen} libraries, for @sc{unicos},
4689 @sc{irix} 4, @sc{ptx}, and UnixWare, respectively.  Then, if
4690 @code{getmntent} is available, define @code{HAVE_GETMNTENT}.
4691 @end defmac
4693 @defmac AC_FUNC_GETPGRP
4694 @acindex{FUNC_GETPGRP}
4695 @cvindex GETPGRP_VOID
4696 @c @fuindex getpgid
4697 @c @fuindex getpgrp
4698 @prindex @code{getpgid}
4699 @prindex @code{getpgrp}
4700 Define @code{GETPGRP_VOID} if it is an error to pass 0 to
4701 @code{getpgrp}; this is the Posix behavior.  On older @acronym{BSD}
4702 systems, you must pass 0 to @code{getpgrp}, as it takes an argument and
4703 behaves like Posix's @code{getpgid}.
4705 @example
4706 #ifdef GETPGRP_VOID
4707   pid = getpgrp ();
4708 #else
4709   pid = getpgrp (0);
4710 #endif
4711 @end example
4713 This macro does not check whether
4714 @code{getpgrp} exists at all; if you need to work in that situation,
4715 first call @code{AC_CHECK_FUNC} for @code{getpgrp}.
4717 This macro is obsolescent, as current systems have a @code{getpgrp}
4718 whose signature conforms to Posix.  New programs need not use this macro.
4719 @end defmac
4721 @defmac AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
4722 @acindex{FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK}
4723 @cvindex LSTAT_FOLLOWS_SLASHED_SYMLINK
4724 @c @fuindex lstat
4725 @prindex @code{lstat}
4726 If @file{link} is a symbolic link, then @code{lstat} should treat
4727 @file{link/} the same as @file{link/.}.  However, many older
4728 @code{lstat} implementations incorrectly ignore trailing slashes.
4730 It is safe to assume that if @code{lstat} incorrectly ignores
4731 trailing slashes, then other symbolic-link-aware functions like
4732 @code{unlink} also incorrectly ignore trailing slashes.
4734 If @code{lstat} behaves properly, define
4735 @code{LSTAT_FOLLOWS_SLASHED_SYMLINK}, otherwise require an
4736 @code{AC_LIBOBJ} replacement of @code{lstat}.
4737 @end defmac
4739 @defmac AC_FUNC_MALLOC
4740 @acindex{FUNC_MALLOC}
4741 @cvindex HAVE_MALLOC
4742 @cvindex malloc
4743 @c @fuindex malloc
4744 @prindex @code{malloc}
4745 If the @code{malloc} function is compatible with the @acronym{GNU} C
4746 library @code{malloc} (i.e., @samp{malloc (0)} returns a valid
4747 pointer), define @code{HAVE_MALLOC} to 1.  Otherwise define
4748 @code{HAVE_MALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4749 @samp{malloc}, and define @code{malloc} to @code{rpl_malloc} so that the
4750 native @code{malloc} is not used in the main project.
4752 Typically, the replacement file @file{malloc.c} should look like (note
4753 the @samp{#undef malloc}):
4755 @verbatim
4756 #ifdef HAVE_CONFIG_H
4757 # include <config.h>
4758 #endif
4759 #undef malloc
4761 #include <sys/types.h>
4763 void *malloc ();
4765 /* Allocate an N-byte block of memory from the heap.
4766    If N is zero, allocate a 1-byte block.  */
4768 void *
4769 rpl_malloc (size_t n)
4771   if (n == 0)
4772     n = 1;
4773   return malloc (n);
4775 @end verbatim
4776 @end defmac
4778 @defmac AC_FUNC_MEMCMP
4779 @acindex{FUNC_MEMCMP}
4780 @ovindex LIBOBJS
4781 @c @fuindex memcmp
4782 @prindex @code{memcmp}
4783 If the @code{memcmp} function is not available, or does not work on
4784 8-bit data (like the one on SunOS 4.1.3), or fails when comparing 16
4785 bytes or more and with at least one buffer not starting on a 4-byte
4786 boundary (such as the one on NeXT x86 OpenStep), require an
4787 @code{AC_LIBOBJ} replacement for @samp{memcmp}.
4789 This macro is obsolescent, as current systems have a working
4790 @code{memcmp}.  New programs need not use this macro.
4791 @end defmac
4793 @defmac AC_FUNC_MBRTOWC
4794 @acindex{FUNC_MBRTOWC}
4795 @cvindex HAVE_MBRTOWC
4796 @c @fuindex mbrtowc
4797 @prindex @code{mbrtowc}
4798 Define @code{HAVE_MBRTOWC} to 1 if the function @code{mbrtowc} and the
4799 type @code{mbstate_t} are properly declared.
4800 @end defmac
4802 @defmac AC_FUNC_MKTIME
4803 @acindex{FUNC_MKTIME}
4804 @ovindex LIBOBJS
4805 @c @fuindex mktime
4806 @prindex @code{mktime}
4807 If the @code{mktime} function is not available, or does not work
4808 correctly, require an @code{AC_LIBOBJ} replacement for @samp{mktime}.
4809 For the purposes of this test, @code{mktime} should conform to the
4810 Posix standard and should be the inverse of
4811 @code{localtime}.
4812 @end defmac
4814 @anchor{AC_FUNC_MMAP}
4815 @defmac AC_FUNC_MMAP
4816 @acindex{FUNC_MMAP}
4817 @cvindex HAVE_MMAP
4818 @c @fuindex mmap
4819 @prindex @code{mmap}
4820 If the @code{mmap} function exists and works correctly, define
4821 @code{HAVE_MMAP}.  This checks only private fixed mapping of already-mapped
4822 memory.
4823 @end defmac
4825 @defmac AC_FUNC_OBSTACK
4826 @acindex{FUNC_OBSTACK}
4827 @cvindex HAVE_OBSTACK
4828 @cindex obstack
4829 If the obstacks are found, define @code{HAVE_OBSTACK}, else require an
4830 @code{AC_LIBOBJ} replacement for @samp{obstack}.
4831 @end defmac
4833 @defmac AC_FUNC_REALLOC
4834 @acindex{FUNC_REALLOC}
4835 @cvindex HAVE_REALLOC
4836 @cvindex realloc
4837 @c @fuindex realloc
4838 @prindex @code{realloc}
4839 If the @code{realloc} function is compatible with the @acronym{GNU} C
4840 library @code{realloc} (i.e., @samp{realloc (NULL, 0)} returns a
4841 valid pointer), define @code{HAVE_REALLOC} to 1.  Otherwise define
4842 @code{HAVE_REALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4843 @samp{realloc}, and define @code{realloc} to @code{rpl_realloc} so that
4844 the native @code{realloc} is not used in the main project.  See
4845 @code{AC_FUNC_MALLOC} for details.
4846 @end defmac
4848 @defmac AC_FUNC_SELECT_ARGTYPES
4849 @acindex{FUNC_SELECT_ARGTYPES}
4850 @cvindex SELECT_TYPE_ARG1
4851 @cvindex SELECT_TYPE_ARG234
4852 @cvindex SELECT_TYPE_ARG5
4853 @c @fuindex select
4854 @prindex @code{select}
4855 Determines the correct type to be passed for each of the
4856 @code{select} function's arguments, and defines those types
4857 in @code{SELECT_TYPE_ARG1}, @code{SELECT_TYPE_ARG234}, and
4858 @code{SELECT_TYPE_ARG5} respectively.  @code{SELECT_TYPE_ARG1} defaults
4859 to @samp{int}, @code{SELECT_TYPE_ARG234} defaults to @samp{int *},
4860 and @code{SELECT_TYPE_ARG5} defaults to @samp{struct timeval *}.
4862 This macro is obsolescent, as current systems have a @code{select} whose
4863 signature conforms to Posix.  New programs need not use this macro.
4864 @end defmac
4866 @defmac AC_FUNC_SETPGRP
4867 @acindex{FUNC_SETPGRP}
4868 @cvindex SETPGRP_VOID
4869 @c @fuindex setpgrp
4870 @prindex @code{setpgrp}
4871 If @code{setpgrp} takes no argument (the Posix version), define
4872 @code{SETPGRP_VOID}.  Otherwise, it is the @acronym{BSD} version, which takes
4873 two process IDs as arguments.  This macro does not check whether
4874 @code{setpgrp} exists at all; if you need to work in that situation,
4875 first call @code{AC_CHECK_FUNC} for @code{setpgrp}.
4877 This macro is obsolescent, as current systems have a @code{setpgrp}
4878 whose signature conforms to Posix.  New programs need not use this macro.
4879 @end defmac
4881 @defmac AC_FUNC_STAT
4882 @defmacx AC_FUNC_LSTAT
4883 @acindex{FUNC_STAT}
4884 @acindex{FUNC_LSTAT}
4885 @cvindex HAVE_STAT_EMPTY_STRING_BUG
4886 @cvindex HAVE_LSTAT_EMPTY_STRING_BUG
4887 @c @fuindex stat
4888 @prindex @code{stat}
4889 @c @fuindex lstat
4890 @prindex @code{lstat}
4891 Determine whether @code{stat} or @code{lstat} have the bug that it
4892 succeeds when given the zero-length file name as argument.  The @code{stat}
4893 and @code{lstat} from SunOS 4.1.4 and the Hurd (as of 1998-11-01) do
4894 this.
4896 If it does, then define @code{HAVE_STAT_EMPTY_STRING_BUG} (or
4897 @code{HAVE_LSTAT_EMPTY_STRING_BUG}) and ask for an @code{AC_LIBOBJ}
4898 replacement of it.
4900 These macros are obsolescent, as no current systems have the bug.
4901 New programs need not use these macros.
4902 @end defmac
4904 @anchor{AC_FUNC_STRCOLL}
4905 @defmac AC_FUNC_STRCOLL
4906 @acindex{FUNC_STRCOLL}
4907 @cvindex HAVE_STRCOLL
4908 @c @fuindex strcoll
4909 @prindex @code{strcoll}
4910 If the @code{strcoll} function exists and works correctly, define
4911 @code{HAVE_STRCOLL}.  This does a bit more than
4912 @samp{AC_CHECK_FUNCS(strcoll)}, because some systems have incorrect
4913 definitions of @code{strcoll} that should not be used.
4914 @end defmac
4916 @defmac AC_FUNC_STRERROR_R
4917 @acindex{FUNC_STRERROR_R}
4918 @cvindex HAVE_STRERROR_R
4919 @cvindex HAVE_DECL_STRERROR_R
4920 @cvindex STRERROR_R_CHAR_P
4921 @c @fuindex strerror_r
4922 @prindex @code{strerror_r}
4923 If @code{strerror_r} is available, define @code{HAVE_STRERROR_R}, and if
4924 it is declared, define @code{HAVE_DECL_STRERROR_R}.  If it returns a
4925 @code{char *} message, define @code{STRERROR_R_CHAR_P}; otherwise it
4926 returns an @code{int} error number.  The Thread-Safe Functions option of
4927 Posix requires @code{strerror_r} to return @code{int}, but
4928 many systems (including, for example, version 2.2.4 of the @acronym{GNU} C
4929 Library) return a @code{char *} value that is not necessarily equal to
4930 the buffer argument.
4931 @end defmac
4933 @anchor{AC_FUNC_STRFTIME}
4934 @defmac AC_FUNC_STRFTIME
4935 @acindex{FUNC_STRFTIME}
4936 @cvindex HAVE_STRFTIME
4937 @c @fuindex strftime
4938 @prindex @code{strftime}
4939 Check for @code{strftime} in the @file{intl} library, for SCO Unix.
4940 Then, if @code{strftime} is available, define @code{HAVE_STRFTIME}.
4942 This macro is obsolescent, as no current systems require the @file{intl}
4943 library for @code{strftime}.  New programs need not use this macro.
4944 @end defmac
4946 @defmac AC_FUNC_STRTOD
4947 @acindex{FUNC_STRTOD}
4948 @ovindex POW_LIB
4949 @c @fuindex strtod
4950 @prindex @code{strtod}
4951 If the @code{strtod} function does not exist or doesn't work correctly,
4952 ask for an @code{AC_LIBOBJ} replacement of @samp{strtod}.  In this case,
4953 because @file{strtod.c} is likely to need @samp{pow}, set the output
4954 variable @code{POW_LIB} to the extra library needed.
4955 @end defmac
4957 @defmac AC_FUNC_STRTOLD
4958 @acindex{FUNC_STRTOLD}
4959 @cvindex HAVE_STRTOLD
4960 @prindex @code{strtold}
4961 If the @code{strtold} function exists and conforms to C99, define
4962 @code{HAVE_STRTOLD}.
4963 @end defmac
4965 @defmac AC_FUNC_STRNLEN
4966 @acindex{FUNC_STRNLEN}
4967 @cvindex HAVE_STRNLEN
4968 @c @fuindex strnlen
4969 @prindex @code{strnlen}
4970 If the @code{strnlen} function is not available, or is buggy (like the one
4971 from @acronym{AIX} 4.3), require an @code{AC_LIBOBJ} replacement for it.
4972 @end defmac
4974 @anchor{AC_FUNC_UTIME_NULL}
4975 @defmac AC_FUNC_UTIME_NULL
4976 @acindex{FUNC_UTIME_NULL}
4977 @cvindex HAVE_UTIME_NULL
4978 @c @fuindex utime
4979 @prindex @code{utime}
4980 If @samp{utime (@var{file}, NULL)} sets @var{file}'s timestamp to
4981 the present, define @code{HAVE_UTIME_NULL}.
4983 This macro is obsolescent, as all current systems have a @code{utime}
4984 that behaves this way.  New programs need not use this macro.
4985 @end defmac
4987 @anchor{AC_FUNC_VPRINTF}
4988 @defmac AC_FUNC_VPRINTF
4989 @acindex{FUNC_VPRINTF}
4990 @cvindex HAVE_VPRINTF
4991 @cvindex HAVE_DOPRNT
4992 @c @fuindex vprintf
4993 @prindex @code{vprintf}
4994 If @code{vprintf} is found, define @code{HAVE_VPRINTF}.  Otherwise, if
4995 @code{_doprnt} is found, define @code{HAVE_DOPRNT}.  (If @code{vprintf}
4996 is available, you may assume that @code{vfprintf} and @code{vsprintf}
4997 are also available.)
4999 This macro is obsolescent, as all current systems have @code{vprintf}.
5000 New programs need not use this macro.
5001 @end defmac
5003 @defmac AC_REPLACE_FNMATCH
5004 @acindex{REPLACE_FNMATCH}
5005 @c @fuindex fnmatch
5006 @prindex @code{fnmatch}
5007 @hdrindex{fnmatch.h}
5008 If the @code{fnmatch} function does not conform to Posix (see
5009 @code{AC_FUNC_FNMATCH}), ask for its @code{AC_LIBOBJ} replacement.
5011 The files @file{fnmatch.c}, @file{fnmatch_loop.c}, and @file{fnmatch_.h}
5012 in the @code{AC_LIBOBJ} replacement directory are assumed to contain a
5013 copy of the source code of @acronym{GNU} @code{fnmatch}.  If necessary,
5014 this source code is compiled as an @code{AC_LIBOBJ} replacement, and the
5015 @file{fnmatch_.h} file is linked to @file{fnmatch.h} so that it can be
5016 included in place of the system @code{<fnmatch.h>}.
5018 This macro is obsolescent, as it assumes the use of particular source
5019 files.  New programs should use Gnulib's @code{fnmatch-posix} module,
5020 which provides this macro along with the source files.  @xref{Gnulib}.
5021 @end defmac
5025 @node Generic Functions
5026 @subsection Generic Function Checks
5028 These macros are used to find functions not covered by the ``particular''
5029 test macros.  If the functions might be in libraries other than the
5030 default C library, first call @code{AC_CHECK_LIB} for those libraries.
5031 If you need to check the behavior of a function as well as find out
5032 whether it is present, you have to write your own test for
5033 it (@pxref{Writing Tests}).
5035 @anchor{AC_CHECK_FUNC}
5036 @defmac AC_CHECK_FUNC (@var{function}, @ovar{action-if-found}, @
5037   @ovar{action-if-not-found})
5038 @acindex{CHECK_FUNC}
5039 If C function @var{function} is available, run shell commands
5040 @var{action-if-found}, otherwise @var{action-if-not-found}.  If you just
5041 want to define a symbol if the function is available, consider using
5042 @code{AC_CHECK_FUNCS} instead.  This macro checks for functions with C
5043 linkage even when @code{AC_LANG(C++)} has been called, since C is more
5044 standardized than C++.  (@pxref{Language Choice}, for more information
5045 about selecting the language for checks.)
5046 @end defmac
5048 @anchor{AC_CHECK_FUNCS}
5049 @defmac AC_CHECK_FUNCS (@var{function}@dots{}, @ovar{action-if-found}, @
5050   @ovar{action-if-not-found})
5051 @acindex{CHECK_FUNCS}
5052 @cvindex HAVE_@var{function}
5053 For each @var{function} enumerated in the blank-or-newline-separated argument
5054 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5055 If @var{action-if-found} is given, it is additional shell code to
5056 execute when one of the functions is found.  You can give it a value of
5057 @samp{break} to break out of the loop on the first match.  If
5058 @var{action-if-not-found} is given, it is executed when one of the
5059 functions is not found.
5060 @end defmac
5062 @defmac AC_CHECK_FUNCS_ONCE (@var{function}@dots{})
5063 @acindex{CHECK_FUNCS_ONCE}
5064 @cvindex HAVE_@var{function}
5065 For each @var{function} enumerated in the blank-or-newline-separated argument
5066 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5067 This is a once-only variant of @code{AC_CHECK_FUNCS}.  It generates the
5068 checking code at most once, so that @command{configure} is smaller and
5069 faster; but the checks cannot be conditionalized and are always done once,
5070 early during the @command{configure} run.
5071 @end defmac
5073 @sp 1
5075 Autoconf follows a philosophy that was formed over the years by those
5076 who have struggled for portability: isolate the portability issues in
5077 specific files, and then program as if you were in a Posix
5078 environment.  Some functions may be missing or unfixable, and your
5079 package must be ready to replace them.
5081 Suitable replacements for many such problem functions are available from
5082 Gnulib (@pxref{Gnulib}).
5084 @defmac AC_LIBOBJ (@var{function})
5085 @acindex{LIBOBJ}
5086 @ovindex LIBOBJS
5087 Specify that @samp{@var{function}.c} must be included in the executables
5088 to replace a missing or broken implementation of @var{function}.
5090 Technically, it adds @samp{@var{function}.$ac_objext} to the output
5091 variable @code{LIBOBJS} if it is not already in, and calls
5092 @code{AC_LIBSOURCE} for @samp{@var{function}.c}.  You should not
5093 directly change @code{LIBOBJS}, since this is not traceable.
5094 @end defmac
5096 @defmac AC_LIBSOURCE (@var{file})
5097 @acindex{LIBSOURCE}
5098 Specify that @var{file} might be needed to compile the project.  If you
5099 need to know what files might be needed by a @file{configure.ac}, you
5100 should trace @code{AC_LIBSOURCE}.  @var{file} must be a literal.
5102 This macro is called automatically from @code{AC_LIBOBJ}, but you must
5103 call it explicitly if you pass a shell variable to @code{AC_LIBOBJ}.  In
5104 that case, since shell variables cannot be traced statically, you must
5105 pass to @code{AC_LIBSOURCE} any possible files that the shell variable
5106 might cause @code{AC_LIBOBJ} to need.  For example, if you want to pass
5107 a variable @code{$foo_or_bar} to @code{AC_LIBOBJ} that holds either
5108 @code{"foo"} or @code{"bar"}, you should do:
5110 @example
5111 AC_LIBSOURCE([foo.c])
5112 AC_LIBSOURCE([bar.c])
5113 AC_LIBOBJ([$foo_or_bar])
5114 @end example
5116 @noindent
5117 There is usually a way to avoid this, however, and you are encouraged to
5118 simply call @code{AC_LIBOBJ} with literal arguments.
5120 Note that this macro replaces the obsolete @code{AC_LIBOBJ_DECL}, with
5121 slightly different semantics: the old macro took the function name,
5122 e.g., @code{foo}, as its argument rather than the file name.
5123 @end defmac
5125 @defmac AC_LIBSOURCES (@var{files})
5126 @acindex{LIBSOURCES}
5127 Like @code{AC_LIBSOURCE}, but accepts one or more @var{files} in a
5128 comma-separated M4 list.  Thus, the above example might be rewritten:
5130 @example
5131 AC_LIBSOURCES([foo.c, bar.c])
5132 AC_LIBOBJ([$foo_or_bar])
5133 @end example
5134 @end defmac
5136 @defmac AC_CONFIG_LIBOBJ_DIR (@var{directory})
5137 @acindex{CONFIG_LIBOBJ_DIR}
5138 Specify that @code{AC_LIBOBJ} replacement files are to be found in
5139 @var{directory}, a name relative to the top level of the
5140 source tree.  The replacement directory defaults to @file{.}, the top
5141 level directory, and the most typical value is @file{lib}, corresponding
5142 to @samp{AC_CONFIG_LIBOBJ_DIR([lib])}.
5144 @command{configure} might need to know the replacement directory for the
5145 following reasons: (i) some checks use the replacement files, (ii) some
5146 macros bypass broken system headers by installing links to the
5147 replacement headers (iii) when used in conjunction with Automake,
5148 within each makefile, @var{directory} is used as a relative path
5149 from @code{$(top_srcdir)} to each object named in @code{LIBOBJS} and
5150 @code{LTLIBOBJS}, etc.
5151 @end defmac
5153 @sp 1
5155 It is common to merely check for the existence of a function, and ask
5156 for its @code{AC_LIBOBJ} replacement if missing.  The following macro is
5157 a convenient shorthand.
5159 @defmac AC_REPLACE_FUNCS (@var{function}@dots{})
5160 @acindex{REPLACE_FUNCS}
5161 @cvindex HAVE_@var{function}
5162 @ovindex LIBOBJS
5163 Like @code{AC_CHECK_FUNCS}, but uses @samp{AC_LIBOBJ(@var{function})} as
5164 @var{action-if-not-found}.  You can declare your replacement function by
5165 enclosing the prototype in @samp{#ifndef HAVE_@var{function}}.  If the
5166 system has the function, it probably declares it in a header file you
5167 should be including, so you shouldn't redeclare it lest your declaration
5168 conflict.
5169 @end defmac
5171 @node Header Files
5172 @section Header Files
5173 @cindex Header, checking
5175 The following macros check for the presence of certain C header files.
5176 If there is no macro specifically defined to check for a header file you need,
5177 and you don't need to check for any special properties of
5178 it, then you can use one of the general header-file check macros.
5180 @menu
5181 * Header Portability::          Collected knowledge on common headers
5182 * Particular Headers::          Special handling to find certain headers
5183 * Generic Headers::             How to find other headers
5184 @end menu
5186 @node Header Portability
5187 @subsection Portability of Headers
5188 @cindex Portability of headers
5189 @cindex Header portability
5191 This section tries to collect knowledge about common headers, and the
5192 problems they cause.  By definition, this list always requires
5193 additions.  Please help us keeping it as complete as possible.
5195 @table @asis
5197 @item @file{limits.h}
5198 C99 says that @file{limits.h} defines @code{LLONG_MIN},
5199 @code{LLONG_MAX}, and @code{ULLONG_MAX}, but many almost-C99
5200 environments (e.g., default @acronym{GCC} 4.0.2 + glibc 2.4) do not
5201 define them.
5203 @item @file{inttypes.h} vs.@: @file{stdint.h}
5204 @hdrindex{inttypes.h}
5205 @hdrindex{stdint.h}
5206 The C99 standard says that @file{inttypes.h} includes
5207 @file{stdint.h}, so there's no need to include @file{stdint.h}
5208 separately in a standard environment.  Some implementations have
5209 @file{inttypes.h} but not @file{stdint.h} (e.g., Solaris 7), but we don't
5210 know of any implementation that has @file{stdint.h} but not
5211 @file{inttypes.h}.
5213 @item @file{linux/irda.h}
5214 @hdrindex{linux/irda.h}
5215 It requires @file{linux/types.h} and @file{sys/socket.h}.
5217 @item @file{linux/random.h}
5218 @hdrindex{linux/random.h}
5219 It requires @file{linux/types.h}.
5221 @item @file{net/if.h}
5222 @hdrindex{net/if.h}
5223 On Darwin, this file requires that @file{sys/socket.h} be included
5224 beforehand.  One should run:
5226 @example
5227 AC_CHECK_HEADERS([sys/socket.h])
5228 AC_CHECK_HEADERS([net/if.h], [], [],
5229 [#include <stdio.h>
5230 #ifdef STDC_HEADERS
5231 # include <stdlib.h>
5232 # include <stddef.h>
5233 #else
5234 # ifdef HAVE_STDLIB_H
5235 #  include <stdlib.h>
5236 # endif
5237 #endif
5238 #ifdef HAVE_SYS_SOCKET_H
5239 # include <sys/socket.h>
5240 #endif
5242 @end example
5244 @item @file{netinet/if_ether.h}
5245 @hdrindex{netinet/if_ether.h}
5246 On Darwin, this file requires that @file{stdio.h} and
5247 @file{sys/socket.h} be included beforehand.  One should run:
5249 @example
5250 AC_CHECK_HEADERS([sys/socket.h])
5251 AC_CHECK_HEADERS([netinet/if_ether.h], [], [],
5252 [#include <stdio.h>
5253 #ifdef STDC_HEADERS
5254 # include <stdlib.h>
5255 # include <stddef.h>
5256 #else
5257 # ifdef HAVE_STDLIB_H
5258 #  include <stdlib.h>
5259 # endif
5260 #endif
5261 #ifdef HAVE_SYS_SOCKET_H
5262 # include <sys/socket.h>
5263 #endif
5265 @end example
5267 @item @file{stdint.h}
5268 See above, item @file{inttypes.h} vs.@: @file{stdint.h}.
5270 @item @file{stdlib.h}
5271 @hdrindex{stdlib.h}
5272 On many systems (e.g., Darwin), @file{stdio.h} is a prerequisite.
5274 @item @file{sys/mount.h}
5275 @hdrindex{sys/mount.h}
5276 On Free@acronym{BSD} 4.8 on ia32 and using gcc version 2.95.4,
5277 @file{sys/params.h} is a prerequisite.
5279 @item @file{sys/ptem.h}
5280 @hdrindex{sys/ptem.h}
5281 On Solaris 8, @file{sys/stream.h} is a prerequisite.
5283 @item @file{sys/socket.h}
5284 @hdrindex{sys/socket.h}
5285 On Darwin, @file{stdlib.h} is a prerequisite.
5287 @item @file{sys/ucred.h}
5288 @hdrindex{sys/ucred.h}
5289 On Tru64 5.1, @file{sys/types.h} is a prerequisite.
5291 @item @file{X11/extensions/scrnsaver.h}
5292 @hdrindex{X11/extensions/scrnsaver.h}
5293 Using XFree86, this header requires @file{X11/Xlib.h}, which is probably
5294 so required that you might not even consider looking for it.
5296 @example
5297 AC_CHECK_HEADERS([X11/extensions/scrnsaver.h], [], [],
5298 [[#include <X11/Xlib.h>
5300 @end example
5301 @end table
5304 @node Particular Headers
5305 @subsection Particular Header Checks
5307 These macros check for particular system header files---whether they
5308 exist, and in some cases whether they declare certain symbols.
5310 @defmac AC_HEADER_ASSERT
5311 @acindex{HEADER_ASSERT}
5312 @cvindex NDEBUG
5313 @hdrindex{assert.h}
5314 Check whether to enable assertions in the style of @file{assert.h}.
5315 Assertions are enabled by default, but the user can override this by
5316 invoking @command{configure} with the @option{--disable-assert} option.
5317 @end defmac
5319 @anchor{AC_HEADER_DIRENT}
5320 @defmac AC_HEADER_DIRENT
5321 @acindex{HEADER_DIRENT}
5322 @cvindex HAVE_DIRENT_H
5323 @cvindex HAVE_NDIR_H
5324 @cvindex HAVE_SYS_DIR_H
5325 @cvindex HAVE_SYS_NDIR_H
5326 @hdrindex{dirent.h}
5327 @hdrindex{sys/ndir.h}
5328 @hdrindex{sys/dir.h}
5329 @hdrindex{ndir.h}
5330 Check for the following header files.  For the first one that is
5331 found and defines @samp{DIR}, define the listed C preprocessor macro:
5333 @multitable {@file{sys/ndir.h}} {@code{HAVE_SYS_NDIR_H}}
5334 @item @file{dirent.h}   @tab @code{HAVE_DIRENT_H}
5335 @item @file{sys/ndir.h} @tab @code{HAVE_SYS_NDIR_H}
5336 @item @file{sys/dir.h}  @tab @code{HAVE_SYS_DIR_H}
5337 @item @file{ndir.h}     @tab @code{HAVE_NDIR_H}
5338 @end multitable
5340 The directory-library declarations in your source code should look
5341 something like the following:
5343 @example
5344 @group
5345 #include <sys/types.h>
5346 #ifdef HAVE_DIRENT_H
5347 # include <dirent.h>
5348 # define NAMLEN(dirent) strlen ((dirent)->d_name)
5349 #else
5350 # define dirent direct
5351 # define NAMLEN(dirent) ((dirent)->d_namlen)
5352 # ifdef HAVE_SYS_NDIR_H
5353 #  include <sys/ndir.h>
5354 # endif
5355 # ifdef HAVE_SYS_DIR_H
5356 #  include <sys/dir.h>
5357 # endif
5358 # ifdef HAVE_NDIR_H
5359 #  include <ndir.h>
5360 # endif
5361 #endif
5362 @end group
5363 @end example
5365 Using the above declarations, the program would declare variables to be
5366 of type @code{struct dirent}, not @code{struct direct}, and would access
5367 the length of a directory entry name by passing a pointer to a
5368 @code{struct dirent} to the @code{NAMLEN} macro.
5370 This macro also checks for the SCO Xenix @file{dir} and @file{x} libraries.
5372 This macro is obsolescent, as all current systems with directory
5373 libraries have @code{<dirent.h>}.  New programs need not use this macro.
5375 Also see @code{AC_STRUCT_DIRENT_D_INO} and
5376 @code{AC_STRUCT_DIRENT_D_TYPE} (@pxref{Particular Structures}).
5377 @end defmac
5379 @anchor{AC_HEADER_MAJOR}
5380 @defmac AC_HEADER_MAJOR
5381 @acindex{HEADER_MAJOR}
5382 @cvindex MAJOR_IN_MKDEV
5383 @cvindex MAJOR_IN_SYSMACROS
5384 @hdrindex{sys/mkdev.h}
5385 @hdrindex{sys/sysmacros.h}
5386 If @file{sys/types.h} does not define @code{major}, @code{minor}, and
5387 @code{makedev}, but @file{sys/mkdev.h} does, define
5388 @code{MAJOR_IN_MKDEV}; otherwise, if @file{sys/sysmacros.h} does, define
5389 @code{MAJOR_IN_SYSMACROS}.
5390 @end defmac
5392 @defmac AC_HEADER_RESOLV
5393 @acindex{HEADER_RESOLV}
5394 @cvindex HAVE_RESOLV_H
5395 @hdrindex{resolv.h}
5396 Checks for header @file{resolv.h}, checking for prerequisites first.
5397 To properly use @file{resolv.h}, your code should contain something like
5398 the following:
5400 @verbatim
5401 #ifdef HAVE_SYS_TYPES_H
5402 #  include <sys/types.h>
5403 #endif
5404 #ifdef HAVE_NETINET_IN_H
5405 #  include <netinet/in.h>   /* inet_ functions / structs */
5406 #endif
5407 #ifdef HAVE_ARPA_NAMESER_H
5408 #  include <arpa/nameser.h> /* DNS HEADER struct */
5409 #endif
5410 #ifdef HAVE_NETDB_H
5411 #  include <netdb.h>
5412 #endif
5413 #include <resolv.h>
5414 @end verbatim
5415 @end defmac
5417 @anchor{AC_HEADER_STAT}
5418 @defmac AC_HEADER_STAT
5419 @acindex{HEADER_STAT}
5420 @cvindex STAT_MACROS_BROKEN
5421 @hdrindex{sys/stat.h}
5422 If the macros @code{S_ISDIR}, @code{S_ISREG}, etc.@: defined in
5423 @file{sys/stat.h} do not work properly (returning false positives),
5424 define @code{STAT_MACROS_BROKEN}.  This is the case on Tektronix UTekV,
5425 Amdahl UTS and Motorola System V/88.
5427 This macro is obsolescent, as no current systems have the bug.
5428 New programs need not use this macro.
5429 @end defmac
5431 @defmac AC_HEADER_STDBOOL
5432 @acindex{HEADER_STDBOOL}
5433 @cvindex HAVE_STDBOOL_H
5434 @cvindex HAVE__BOOL
5435 @hdrindex{stdbool.h}
5436 @hdrindex{system.h}
5437 If @file{stdbool.h} exists and conforms to C99, define
5438 @code{HAVE_STDBOOL_H} to 1; if the type @code{_Bool} is defined, define
5439 @code{HAVE__BOOL} to 1.  To fulfill the C99 requirements, your
5440 @file{system.h} could contain the following code:
5442 @verbatim
5443 #ifdef HAVE_STDBOOL_H
5444 # include <stdbool.h>
5445 #else
5446 # ifndef HAVE__BOOL
5447 #  ifdef __cplusplus
5448 typedef bool _Bool;
5449 #  else
5450 #   define _Bool signed char
5451 #  endif
5452 # endif
5453 # define bool _Bool
5454 # define false 0
5455 # define true 1
5456 # define __bool_true_false_are_defined 1
5457 #endif
5458 @end verbatim
5460 Alternatively you can use the @samp{stdbool} package of Gnulib
5461 (@pxref{Gnulib}); it packages the above code into a replacement header
5462 and contains a few other bells and whistles.
5464 @end defmac
5466 @anchor{AC_HEADER_STDC}
5467 @defmac AC_HEADER_STDC
5468 @acindex{HEADER_STDC}
5469 @cvindex STDC_HEADERS
5470 @hdrindex{stdlib.h}
5471 @hdrindex{stdarg.h}
5472 @hdrindex{string.h}
5473 @hdrindex{float.h}
5474 @hdrindex{ctype.h}
5475 Define @code{STDC_HEADERS} if the system has C header files
5476 conforming to @acronym{ANSI} C89 (@acronym{ISO} C90).
5477 Specifically, this macro checks for @file{stdlib.h}, @file{stdarg.h},
5478 @file{string.h}, and @file{float.h}; if the system has those, it
5479 probably has the rest of the C89 header files.  This macro also
5480 checks whether @file{string.h} declares @code{memchr} (and thus
5481 presumably the other @code{mem} functions), whether @file{stdlib.h}
5482 declare @code{free} (and thus presumably @code{malloc} and other related
5483 functions), and whether the @file{ctype.h} macros work on characters
5484 with the high bit set, as the C standard requires.
5486 If you use this macro, your code can refer to @code{STDC_HEADERS} to
5487 determine whether the system has conforming header files (and probably C
5488 library functions).
5490 This macro is obsolescent, as current systems have conforming header
5491 files.  New programs need not use this macro.
5493 @hdrindex{string.h}
5494 @hdrindex{strings.h}
5495 Nowadays @file{string.h} is part of the C standard and declares functions like
5496 @code{strcpy}, and @file{strings.h} is standardized by Posix and declares
5497 @acronym{BSD} functions like @code{bcopy}; but
5498 historically, string functions were a major sticking point in this area.
5499 If you still want to worry about portability to ancient systems without
5500 standard headers, there is so much variation
5501 that it is probably easier to declare the functions you use than to
5502 figure out exactly what the system header files declare.  Some ancient systems
5503 contained a mix of functions from the C standard and from @acronym{BSD};
5504 some were mostly standard but lacked @samp{memmove}; some defined the
5505 @acronym{BSD} functions as macros in @file{string.h} or
5506 @file{strings.h}; some had only the @acronym{BSD} functions but
5507 @file{string.h}; some declared the memory functions in @file{memory.h},
5508 some in @file{string.h}; etc.  It is probably sufficient to check for
5509 one string function and one memory function; if the library had the
5510 standard versions of those then it probably had most of the others.
5511 If you put the following in @file{configure.ac}:
5513 @example
5514 # This example is obsolescent.
5515 # Nowadays you can omit these macro calls.
5516 AC_HEADER_STDC
5517 AC_CHECK_FUNCS([strchr memcpy])
5518 @end example
5520 @noindent
5521 then, in your code, you can use declarations like this:
5523 @example
5524 @group
5525 /* This example is obsolescent.
5526    Nowadays you can just #include <string.h>.  */
5527 #ifdef STDC_HEADERS
5528 # include <string.h>
5529 #else
5530 # ifndef HAVE_STRCHR
5531 #  define strchr index
5532 #  define strrchr rindex
5533 # endif
5534 char *strchr (), *strrchr ();
5535 # ifndef HAVE_MEMCPY
5536 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
5537 #  define memmove(d, s, n) bcopy ((s), (d), (n))
5538 # endif
5539 #endif
5540 @end group
5541 @end example
5543 @noindent
5544 If you use a function like @code{memchr}, @code{memset}, @code{strtok},
5545 or @code{strspn}, which have no @acronym{BSD} equivalent, then macros don't
5546 suffice to port to ancient hosts; you must provide an implementation of
5547 each function.  An easy
5548 way to incorporate your implementations only when needed (since the ones
5549 in system C libraries may be hand optimized) is to, taking @code{memchr}
5550 for example, put it in @file{memchr.c} and use
5551 @samp{AC_REPLACE_FUNCS([memchr])}.
5552 @end defmac
5554 @defmac AC_HEADER_SYS_WAIT
5555 @acindex{HEADER_SYS_WAIT}
5556 @cvindex HAVE_SYS_WAIT_H
5557 @hdrindex{sys/wait.h}
5558 If @file{sys/wait.h} exists and is compatible with Posix, define
5559 @code{HAVE_SYS_WAIT_H}.  Incompatibility can occur if @file{sys/wait.h}
5560 does not exist, or if it uses the old @acronym{BSD} @code{union wait} instead
5561 of @code{int} to store a status value.  If @file{sys/wait.h} is not
5562 Posix compatible, then instead of including it, define the
5563 Posix macros with their usual interpretations.  Here is an
5564 example:
5566 @example
5567 @group
5568 #include <sys/types.h>
5569 #ifdef HAVE_SYS_WAIT_H
5570 # include <sys/wait.h>
5571 #endif
5572 #ifndef WEXITSTATUS
5573 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
5574 #endif
5575 #ifndef WIFEXITED
5576 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
5577 #endif
5578 @end group
5579 @end example
5581 @noindent
5582 This macro is obsolescent, as current systems are compatible with Posix.
5583 New programs need not use this macro.
5584 @end defmac
5586 @cvindex _POSIX_VERSION
5587 @hdrindex{unistd.h}
5588 @code{_POSIX_VERSION} is defined when @file{unistd.h} is included on
5589 Posix systems.  If there is no @file{unistd.h}, it is definitely
5590 not a Posix system.  However, some non-Posix systems do
5591 have @file{unistd.h}.
5593 The way to check whether the system supports Posix is:
5595 @example
5596 @group
5597 #ifdef HAVE_UNISTD_H
5598 # include <sys/types.h>
5599 # include <unistd.h>
5600 #endif
5602 #ifdef _POSIX_VERSION
5603 /* Code for Posix systems.  */
5604 #endif
5605 @end group
5606 @end example
5608 @anchor{AC_HEADER_TIME}
5609 @defmac AC_HEADER_TIME
5610 @acindex{HEADER_TIME}
5611 @cvindex TIME_WITH_SYS_TIME
5612 @hdrindex{time.h}
5613 @hdrindex{sys/time.h}
5614 If a program may include both @file{time.h} and @file{sys/time.h},
5615 define @code{TIME_WITH_SYS_TIME}.  On some ancient systems,
5616 @file{sys/time.h} included @file{time.h}, but @file{time.h} was not
5617 protected against multiple inclusion, so programs could not explicitly
5618 include both files.  This macro is useful in programs that use, for
5619 example, @code{struct timeval} as well as
5620 @code{struct tm}.  It is best used in conjunction with
5621 @code{HAVE_SYS_TIME_H}, which can be checked for using
5622 @code{AC_CHECK_HEADERS([sys/time.h])}.
5624 @example
5625 @group
5626 #ifdef TIME_WITH_SYS_TIME
5627 # include <sys/time.h>
5628 # include <time.h>
5629 #else
5630 # ifdef HAVE_SYS_TIME_H
5631 #  include <sys/time.h>
5632 # else
5633 #  include <time.h>
5634 # endif
5635 #endif
5636 @end group
5637 @end example
5639 @noindent
5640 This macro is obsolescent, as current systems can include both files
5641 when they exist.  New programs need not use this macro.
5642 @end defmac
5645 @defmac AC_HEADER_TIOCGWINSZ
5646 @acindex{HEADER_TIOCGWINSZ}
5647 @cvindex GWINSZ_IN_SYS_IOCTL
5648 @hdrindex{sys/ioctl.h}
5649 @hdrindex{termios.h}
5650 @c FIXME: I need clarifications from Jim.
5651 If the use of @code{TIOCGWINSZ} requires @file{<sys/ioctl.h>}, then
5652 define @code{GWINSZ_IN_SYS_IOCTL}.  Otherwise @code{TIOCGWINSZ} can be
5653 found in @file{<termios.h>}.
5655 Use:
5657 @example
5658 @group
5659 #ifdef HAVE_TERMIOS_H
5660 # include <termios.h>
5661 #endif
5663 #ifdef GWINSZ_IN_SYS_IOCTL
5664 # include <sys/ioctl.h>
5665 #endif
5666 @end group
5667 @end example
5668 @end defmac
5670 @node Generic Headers
5671 @subsection Generic Header Checks
5673 These macros are used to find system header files not covered by the
5674 ``particular'' test macros.  If you need to check the contents of a header
5675 as well as find out whether it is present, you have to write your own
5676 test for it (@pxref{Writing Tests}).
5678 @anchor{AC_CHECK_HEADER}
5679 @defmac AC_CHECK_HEADER (@var{header-file}, @ovar{action-if-found}, @
5680   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5681 @acindex{CHECK_HEADER}
5682 If the system header file @var{header-file} is compilable, execute shell
5683 commands @var{action-if-found}, otherwise execute
5684 @var{action-if-not-found}.  If you just want to define a symbol if the
5685 header file is available, consider using @code{AC_CHECK_HEADERS}
5686 instead.
5688 For compatibility issues with older versions of Autoconf, please read
5689 below.
5690 @end defmac
5692 @anchor{AC_CHECK_HEADERS}
5693 @defmac AC_CHECK_HEADERS (@var{header-file}@dots{}, @
5694   @ovar{action-if-found}, @ovar{action-if-not-found}, @
5695   @dvar{includes, default-includes})
5696 @acindex{CHECK_HEADERS}
5697 @cvindex HAVE_@var{header}
5698 For each given system header file @var{header-file} in the
5699 blank-separated argument list that exists, define
5700 @code{HAVE_@var{header-file}} (in all capitals).  If @var{action-if-found}
5701 is given, it is additional shell code to execute when one of the header
5702 files is found.  You can give it a value of @samp{break} to break out of
5703 the loop on the first match.  If @var{action-if-not-found} is given, it
5704 is executed when one of the header files is not found.
5706 For compatibility issues with older versions of Autoconf, please read
5707 below.
5708 @end defmac
5710 Previous versions of Autoconf merely checked whether the header was
5711 accepted by the preprocessor.  This was changed because the old test was
5712 inappropriate for typical uses.  Headers are typically used to compile,
5713 not merely to preprocess, and the old behavior sometimes accepted
5714 headers that clashed at compile-time.  If you need to check whether a
5715 header is preprocessable, you can use @code{AC_PREPROC_IFELSE}
5716 (@pxref{Running the Preprocessor}).
5718 This scheme, which improves the robustness of the test, also requires
5719 that you make sure that headers that must be included before the
5720 @var{header-file} be part of the @var{includes}, (@pxref{Default
5721 Includes}).  If looking for @file{bar.h}, which requires that
5722 @file{foo.h} be included before if it exists, we suggest the following
5723 scheme:
5725 @verbatim
5726 AC_CHECK_HEADERS([foo.h])
5727 AC_CHECK_HEADERS([bar.h], [], [],
5728 [#ifdef HAVE_FOO_H
5729 # include <foo.h>
5730 # endif
5732 @end verbatim
5734 The following variant generates smaller, faster @command{configure}
5735 files if you do not need the full power of @code{AC_CHECK_HEADERS}.
5737 @defmac AC_CHECK_HEADERS_ONCE (@var{header-file}@dots{})
5738 @acindex{CHECK_HEADERS_ONCE}
5739 @cvindex HAVE_@var{header}
5740 For each given system header file @var{header-file} in the
5741 blank-separated argument list that exists, define
5742 @code{HAVE_@var{header-file}} (in all capitals).
5743 This is a once-only variant of @code{AC_CHECK_HEADERS}.  It generates the
5744 checking code at most once, so that @command{configure} is smaller and
5745 faster; but the checks cannot be conditionalized and are always done once,
5746 early during the @command{configure} run.
5747 @end defmac
5749 @node Declarations
5750 @section Declarations
5751 @cindex Declaration, checking
5753 The following macros check for the declaration of variables and
5754 functions.  If there is no macro specifically defined to check for a
5755 symbol you need, then you can use the general macros (@pxref{Generic
5756 Declarations}) or, for more complex tests, you may use
5757 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5759 @menu
5760 * Particular Declarations::     Macros to check for certain declarations
5761 * Generic Declarations::        How to find other declarations
5762 @end menu
5764 @node Particular Declarations
5765 @subsection Particular Declaration Checks
5767 There are no specific macros for declarations.
5769 @node Generic Declarations
5770 @subsection Generic Declaration Checks
5772 These macros are used to find declarations not covered by the ``particular''
5773 test macros.
5775 @defmac AC_CHECK_DECL (@var{symbol}, @ovar{action-if-found}, @
5776   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5777 @acindex{CHECK_DECL}
5778 If @var{symbol} (a function, variable, or constant) is not declared in
5779 @var{includes} and a declaration is needed, run the shell commands
5780 @var{action-if-not-found}, otherwise @var{action-if-found}.  If no
5781 @var{includes} are specified, the default includes are used
5782 (@pxref{Default Includes}).
5784 This macro actually tests whether @var{symbol} is defined as a macro or
5785 can be used as an r-value, not whether it is really declared, because it
5786 is much safer to avoid
5787 introducing extra declarations when they are not needed.
5788 @end defmac
5790 @anchor{AC_CHECK_DECLS}
5791 @defmac AC_CHECK_DECLS (@var{symbols}, @ovar{action-if-found}, @
5792   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5793 @acindex{CHECK_DECLS}
5794 @cvindex HAVE_DECL_@var{symbol}
5795 For each of the @var{symbols} (@emph{comma}-separated list), define
5796 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5797 @var{symbol} is declared, otherwise to @samp{0}.  If
5798 @var{action-if-not-found} is given, it is additional shell code to
5799 execute when one of the function declarations is needed, otherwise
5800 @var{action-if-found} is executed.
5802 This macro uses an M4 list as first argument:
5803 @example
5804 AC_CHECK_DECLS([strdup])
5805 AC_CHECK_DECLS([strlen])
5806 AC_CHECK_DECLS([malloc, realloc, calloc, free])
5807 @end example
5809 Unlike the other @samp{AC_CHECK_*S} macros, when a @var{symbol} is not
5810 declared, @code{HAVE_DECL_@var{symbol}} is defined to @samp{0} instead
5811 of leaving @code{HAVE_DECL_@var{symbol}} undeclared.  When you are
5812 @emph{sure} that the check was performed, use
5813 @code{HAVE_DECL_@var{symbol}} in @code{#if}:
5815 @example
5816 #if !HAVE_DECL_SYMBOL
5817 extern char *symbol;
5818 #endif
5819 @end example
5821 @noindent
5822 If the test may have not been performed, however, because it is safer
5823 @emph{not} to declare a symbol than to use a declaration that conflicts
5824 with the system's one, you should use:
5826 @example
5827 #if defined HAVE_DECL_MALLOC && !HAVE_DECL_MALLOC
5828 void *malloc (size_t *s);
5829 #endif
5830 @end example
5832 @noindent
5833 You fall into the second category only in extreme situations: either
5834 your files may be used without being configured, or they are used during
5835 the configuration.  In most cases the traditional approach is enough.
5836 @end defmac
5838 @defmac AC_CHECK_DECLS_ONCE (@var{symbols})
5839 @acindex{CHECK_DECLS_ONCE}
5840 @cvindex HAVE_DECL_@var{symbol}
5841 For each of the @var{symbols} (@emph{comma}-separated list), define
5842 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5843 @var{symbol} is declared in the default include files, otherwise to
5844 @samp{0}.  This is a once-only variant of @code{AC_CHECK_DECLS}.  It
5845 generates the checking code at most once, so that @command{configure} is
5846 smaller and faster; but the checks cannot be conditionalized and are
5847 always done once, early during the @command{configure} run.
5848 @end defmac
5851 @node Structures
5852 @section Structures
5853 @cindex Structure, checking
5855 The following macros check for the presence of certain members in C
5856 structures.  If there is no macro specifically defined to check for a
5857 member you need, then you can use the general structure-member macros
5858 (@pxref{Generic Structures}) or, for more complex tests, you may use
5859 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5861 @menu
5862 * Particular Structures::       Macros to check for certain structure members
5863 * Generic Structures::          How to find other structure members
5864 @end menu
5866 @node Particular Structures
5867 @subsection Particular Structure Checks
5869 The following macros check for certain structures or structure members.
5871 @defmac AC_STRUCT_DIRENT_D_INO
5872 @acindex{STRUCT_DIRENT_D_INO}
5873 @cvindex HAVE_STRUCT_DIRENT_D_INO
5874 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5875 Headers}).  Then, if @code{struct dirent} contains a @code{d_ino}
5876 member, define @code{HAVE_STRUCT_DIRENT_D_INO}.
5878 @code{HAVE_STRUCT_DIRENT_D_INO} indicates only the presence of
5879 @code{d_ino}, not whether its contents are always reliable.
5880 Traditionally, a zero @code{d_ino} indicated a deleted directory entry,
5881 though current systems hide this detail from the user and never return
5882 zero @code{d_ino} values.
5883 Many current systems report an incorrect @code{d_ino} for a directory
5884 entry that is a mount point.
5885 @end defmac
5887 @defmac AC_STRUCT_DIRENT_D_TYPE
5888 @acindex{STRUCT_DIRENT_D_TYPE}
5889 @cvindex HAVE_STRUCT_DIRENT_D_TYPE
5890 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5891 Headers}).  Then, if @code{struct dirent} contains a @code{d_type}
5892 member, define @code{HAVE_STRUCT_DIRENT_D_TYPE}.
5893 @end defmac
5895 @anchor{AC_STRUCT_ST_BLOCKS}
5896 @defmac AC_STRUCT_ST_BLOCKS
5897 @acindex{STRUCT_ST_BLOCKS}
5898 @cvindex HAVE_STRUCT_STAT_ST_BLOCKS
5899 @cvindex HAVE_ST_BLOCKS
5900 @ovindex LIBOBJS
5901 If @code{struct stat} contains an @code{st_blocks} member, define
5902 @code{HAVE_STRUCT_STAT_ST_BLOCKS}.  Otherwise, require an
5903 @code{AC_LIBOBJ} replacement of @samp{fileblocks}.  The former name,
5904 @code{HAVE_ST_BLOCKS} is to be avoided, as its support will cease in the
5905 future.
5906 @end defmac
5908 @defmac AC_STRUCT_TM
5909 @acindex{STRUCT_TM}
5910 @cvindex TM_IN_SYS_TIME
5911 @hdrindex{time.h}
5912 @hdrindex{sys/time.h}
5913 If @file{time.h} does not define @code{struct tm}, define
5914 @code{TM_IN_SYS_TIME}, which means that including @file{sys/time.h}
5915 had better define @code{struct tm}.
5917 This macro is obsolescent, as @file{time.h} defines @code{struct tm} in
5918 current systems.  New programs need not use this macro.
5919 @end defmac
5921 @anchor{AC_STRUCT_TIMEZONE}
5922 @defmac AC_STRUCT_TIMEZONE
5923 @acindex{STRUCT_TIMEZONE}
5924 @cvindex HAVE_DECL_TZNAME
5925 @cvindex HAVE_STRUCT_TM_TM_ZONE
5926 @cvindex HAVE_TM_ZONE
5927 @cvindex HAVE_TZNAME
5928 Figure out how to get the current timezone.  If @code{struct tm} has a
5929 @code{tm_zone} member, define @code{HAVE_STRUCT_TM_TM_ZONE} (and the
5930 obsoleted @code{HAVE_TM_ZONE}).  Otherwise, if the external array
5931 @code{tzname} is found, define @code{HAVE_TZNAME}; if it is declared,
5932 define @code{HAVE_DECL_TZNAME}.
5933 @end defmac
5935 @node Generic Structures
5936 @subsection Generic Structure Checks
5938 These macros are used to find structure members not covered by the
5939 ``particular'' test macros.
5941 @defmac AC_CHECK_MEMBER (@var{aggregate}.@var{member}, @
5942   @ovar{action-if-found}, @ovar{action-if-not-found}, @
5943   @dvar{includes, default-includes})
5944 @acindex{CHECK_MEMBER}
5945 Check whether @var{member} is a member of the aggregate @var{aggregate}.
5946 If no @var{includes} are specified, the default includes are used
5947 (@pxref{Default Includes}).
5949 @example
5950 AC_CHECK_MEMBER([struct passwd.pw_gecos], [],
5951                 [AC_MSG_ERROR([We need `passwd.pw_gecos'!])],
5952                 [#include <pwd.h>])
5953 @end example
5955 You can use this macro for submembers:
5957 @example
5958 AC_CHECK_MEMBER(struct top.middle.bot)
5959 @end example
5960 @end defmac
5962 @anchor{AC_CHECK_MEMBERS}
5963 @defmac AC_CHECK_MEMBERS (@var{members}, @ovar{action-if-found}, @
5964   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5965 @acindex{CHECK_MEMBERS}
5966 @cvindex HAVE_@var{aggregate}_@var{member}
5967 Check for the existence of each @samp{@var{aggregate}.@var{member}} of
5968 @var{members} using the previous macro.  When @var{member} belongs to
5969 @var{aggregate}, define @code{HAVE_@var{aggregate}_@var{member}} (in all
5970 capitals, with spaces and dots replaced by underscores).  If
5971 @var{action-if-found} is given, it is executed for each of the found
5972 members.  If @var{action-if-not-found} is given, it is executed for each
5973 of the members that could not be found.
5975 This macro uses M4 lists:
5976 @example
5977 AC_CHECK_MEMBERS([struct stat.st_rdev, struct stat.st_blksize])
5978 @end example
5979 @end defmac
5982 @node Types
5983 @section Types
5984 @cindex Types
5985 @cindex C types
5987 The following macros check for C types, either builtin or typedefs.  If
5988 there is no macro specifically defined to check for a type you need, and
5989 you don't need to check for any special properties of it, then you can
5990 use a general type-check macro.
5992 @menu
5993 * Particular Types::            Special handling to find certain types
5994 * Generic Types::               How to find other types
5995 @end menu
5997 @node Particular Types
5998 @subsection Particular Type Checks
6000 @hdrindex{sys/types.h}
6001 @hdrindex{stdlib.h}
6002 @hdrindex{stdint.h}
6003 @hdrindex{inttypes.h}
6004 These macros check for particular C types in @file{sys/types.h},
6005 @file{stdlib.h}, @file{stdint.h}, @file{inttypes.h} and others, if they
6006 exist.
6008 The Gnulib @code{stdint} module is an alternate way to define many of
6009 these symbols; it is useful if you prefer your code to assume a
6010 C99-or-better environment.  @xref{Gnulib}.
6012 @anchor{AC_TYPE_GETGROUPS}
6013 @defmac AC_TYPE_GETGROUPS
6014 @acindex{TYPE_GETGROUPS}
6015 @cvindex GETGROUPS_T
6016 Define @code{GETGROUPS_T} to be whichever of @code{gid_t} or @code{int}
6017 is the base type of the array argument to @code{getgroups}.
6018 @end defmac
6020 @defmac AC_TYPE_INT8_T
6021 @acindex{TYPE_INT8_T}
6022 @cvindex HAVE_INT8_T
6023 @cvindex int8_t
6024 If @file{stdint.h} or @file{inttypes.h} does not define the type
6025 @code{int8_t}, define @code{int8_t} to a signed
6026 integer type that is exactly 8 bits wide and that uses two's complement
6027 representation, if such a type exists.
6028 If you are worried about porting to hosts that lack such a type, you can
6029 use the results of this macro in C89-or-later code as follows:
6031 @example
6032 #if HAVE_STDINT_H
6033 # include <stdint.h>
6034 #endif
6035 #if defined INT8_MAX || defined int8_t
6036  @emph{code using int8_t}
6037 #else
6038  @emph{complicated alternative using >8-bit 'signed char'}
6039 #endif
6040 @end example
6041 @end defmac
6043 @defmac AC_TYPE_INT16_T
6044 @acindex{TYPE_INT16_T}
6045 @cvindex HAVE_INT16_T
6046 @cvindex int16_t
6047 This is like @code{AC_TYPE_INT8_T}, except for 16-bit integers.
6048 @end defmac
6050 @defmac AC_TYPE_INT32_T
6051 @acindex{TYPE_INT32_T}
6052 @cvindex HAVE_INT32_T
6053 @cvindex int32_t
6054 This is like @code{AC_TYPE_INT8_T}, except for 32-bit integers.
6055 @end defmac
6057 @defmac AC_TYPE_INT64_T
6058 @acindex{TYPE_INT64_T}
6059 @cvindex HAVE_INT64_T
6060 @cvindex int64_t
6061 This is like @code{AC_TYPE_INT8_T}, except for 64-bit integers.
6062 @end defmac
6064 @defmac AC_TYPE_INTMAX_T
6065 @acindex{TYPE_INTMAX_T}
6066 @cvindex HAVE_INTMAX_T
6067 @cvindex intmax_t
6068 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intmax_t},
6069 define @code{HAVE_INTMAX_T}.  Otherwise, define @code{intmax_t} to the
6070 widest signed integer type.
6071 @end defmac
6073 @defmac AC_TYPE_INTPTR_T
6074 @acindex{TYPE_INTPTR_T}
6075 @cvindex HAVE_INTPTR_T
6076 @cvindex intptr_t
6077 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intptr_t},
6078 define @code{HAVE_INTPTR_T}.  Otherwise, define @code{intptr_t} to a
6079 signed integer type wide enough to hold a pointer, if such a type
6080 exists.
6081 @end defmac
6083 @defmac AC_TYPE_LONG_DOUBLE
6084 @acindex{TYPE_LONG_DOUBLE}
6085 @cvindex HAVE_LONG_DOUBLE
6086 If the C compiler supports a working @code{long double} type, define
6087 @code{HAVE_LONG_DOUBLE}.  The @code{long double} type might have the
6088 same range and precision as @code{double}.
6090 This macro is obsolescent, as current C compilers support @code{long
6091 double}.  New programs need not use this macro.
6092 @end defmac
6094 @defmac AC_TYPE_LONG_DOUBLE_WIDER
6095 @acindex{TYPE_LONG_DOUBLE_WIDER}
6096 @cvindex HAVE_LONG_DOUBLE_WIDER
6097 If the C compiler supports a working @code{long double} type with more
6098 range or precision than the @code{double} type, define
6099 @code{HAVE_LONG_DOUBLE_WIDER}.
6100 @end defmac
6102 @defmac AC_TYPE_LONG_LONG_INT
6103 @acindex{TYPE_LONG_LONG_INT}
6104 @cvindex HAVE_LONG_LONG_INT
6105 If the C compiler supports a working @code{long long int} type, define
6106 @code{HAVE_LONG_LONG_INT}.
6107 @end defmac
6109 @defmac AC_TYPE_MBSTATE_T
6110 @acindex{TYPE_MBSTATE_T}
6111 @cvindex mbstate_t
6112 @hdrindex{wchar.h}
6113 Define @code{HAVE_MBSTATE_T} if @code{<wchar.h>} declares the
6114 @code{mbstate_t} type.  Also, define @code{mbstate_t} to be a type if
6115 @code{<wchar.h>} does not declare it.
6116 @end defmac
6118 @anchor{AC_TYPE_MODE_T}
6119 @defmac AC_TYPE_MODE_T
6120 @acindex{TYPE_MODE_T}
6121 @cvindex mode_t
6122 Define @code{mode_t} to a suitable type, if standard headers do not
6123 define it.
6124 @end defmac
6126 @anchor{AC_TYPE_OFF_T}
6127 @defmac AC_TYPE_OFF_T
6128 @acindex{TYPE_OFF_T}
6129 @cvindex off_t
6130 Define @code{off_t} to a suitable type, if standard headers do not
6131 define it.
6132 @end defmac
6134 @anchor{AC_TYPE_PID_T}
6135 @defmac AC_TYPE_PID_T
6136 @acindex{TYPE_PID_T}
6137 @cvindex pid_t
6138 Define @code{pid_t} to a suitable type, if standard headers do not
6139 define it.
6140 @end defmac
6142 @anchor{AC_TYPE_SIGNAL}
6143 @defmac AC_TYPE_SIGNAL
6144 @acindex{TYPE_SIGNAL}
6145 @cvindex RETSIGTYPE
6146 @hdrindex{signal.h}
6147 If @file{signal.h} declares @code{signal} as returning a pointer to a
6148 function returning @code{void}, define @code{RETSIGTYPE} to be
6149 @code{void}; otherwise, define it to be @code{int}.
6151 Define signal handlers as returning type @code{RETSIGTYPE}:
6153 @example
6154 @group
6155 RETSIGTYPE
6156 hup_handler ()
6158 @dots{}
6160 @end group
6161 @end example
6162 @end defmac
6164 @anchor{AC_TYPE_SIZE_T}
6165 @defmac AC_TYPE_SIZE_T
6166 @acindex{TYPE_SIZE_T}
6167 @cvindex size_t
6168 Define @code{size_t} to a suitable type, if standard headers do not
6169 define it.
6170 @end defmac
6172 @defmac AC_TYPE_SSIZE_T
6173 @acindex{TYPE_SSIZE_T}
6174 @cvindex ssize_t
6175 Define @code{ssize_t} to a suitable type, if standard headers do not
6176 define it.
6177 @end defmac
6179 @anchor{AC_TYPE_UID_T}
6180 @defmac AC_TYPE_UID_T
6181 @acindex{TYPE_UID_T}
6182 @cvindex uid_t
6183 @cvindex gid_t
6184 Define @code{uid_t} and @code{gid_t} to suitable types, if standard
6185 headers do not define them.
6186 @end defmac
6188 @defmac AC_TYPE_UINT8_T
6189 @acindex{TYPE_UINT8_T}
6190 @cvindex HAVE_UINT8_T
6191 @cvindex uint8_t
6192 If @file{stdint.h} or @file{inttypes.h} does not define the type
6193 @code{uint8_t}, define @code{uint8_t} to an
6194 unsigned integer type that is exactly 8 bits wide, if such a type
6195 exists.
6196 This is like @code{AC_TYPE_INT8_T}, except for unsigned integers.
6197 @end defmac
6199 @defmac AC_TYPE_UINT16_T
6200 @acindex{TYPE_UINT16_T}
6201 @cvindex HAVE_UINT16_T
6202 @cvindex uint16_t
6203 This is like @code{AC_TYPE_UINT8_T}, except for 16-bit integers.
6204 @end defmac
6206 @defmac AC_TYPE_UINT32_T
6207 @acindex{TYPE_UINT32_T}
6208 @cvindex HAVE_UINT32_T
6209 @cvindex uint32_t
6210 This is like @code{AC_TYPE_UINT8_T}, except for 32-bit integers.
6211 @end defmac
6213 @defmac AC_TYPE_UINT64_T
6214 @acindex{TYPE_UINT64_T}
6215 @cvindex HAVE_UINT64_T
6216 @cvindex uint64_t
6217 This is like @code{AC_TYPE_UINT8_T}, except for 64-bit integers.
6218 @end defmac
6220 @defmac AC_TYPE_UINTMAX_T
6221 @acindex{TYPE_UINTMAX_T}
6222 @cvindex HAVE_UINTMAX_T
6223 @cvindex uintmax_t
6224 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintmax_t},
6225 define @code{HAVE_UINTMAX_T}.  Otherwise, define @code{uintmax_t} to the
6226 widest unsigned integer type.
6227 @end defmac
6229 @defmac AC_TYPE_UINTPTR_T
6230 @acindex{TYPE_UINTPTR_T}
6231 @cvindex HAVE_UINTPTR_T
6232 @cvindex uintptr_t
6233 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintptr_t},
6234 define @code{HAVE_UINTPTR_T}.  Otherwise, define @code{uintptr_t} to an
6235 unsigned integer type wide enough to hold a pointer, if such a type
6236 exists.
6237 @end defmac
6239 @defmac AC_TYPE_UNSIGNED_LONG_LONG_INT
6240 @acindex{TYPE_UNSIGNED_LONG_LONG_INT}
6241 @cvindex HAVE_UNSIGNED_LONG_LONG_INT
6242 If the C compiler supports a working @code{unsigned long long int} type,
6243 define @code{HAVE_UNSIGNED_LONG_LONG_INT}.
6244 @end defmac
6246 @node Generic Types
6247 @subsection Generic Type Checks
6249 These macros are used to check for types not covered by the ``particular''
6250 test macros.
6252 @defmac AC_CHECK_TYPE (@var{type}, @ovar{action-if-found}, @
6253   @ovar{action-if-not-found}, @dvar{includes, default-includes})
6254 @acindex{CHECK_TYPE}
6255 Check whether @var{type} is defined.  It may be a compiler builtin type
6256 or defined by the @var{includes} (@pxref{Default Includes}).
6258 In C, @var{type} must be a type-name, so that the expression @samp{sizeof
6259 (@var{type})} is valid (but @samp{sizeof ((@var{type}))} is not).  The
6260 same test is applied when compiling for C++, which means that in C++
6261 @var{type} should be a type-id and should not be an anonymous
6262 @samp{struct} or @samp{union}.
6263 @end defmac
6266 @defmac AC_CHECK_TYPES (@var{types}, @ovar{action-if-found}, @
6267   @ovar{action-if-not-found}, @dvar{includes, default-includes})
6268 @acindex{CHECK_TYPES}
6269 @cvindex HAVE_@var{type}
6270 For each @var{type} of the @var{types} that is defined, define
6271 @code{HAVE_@var{type}} (in all capitals).  Each @var{type} must follow
6272 the rules of @code{AC_CHECK_TYPE}.  If no @var{includes} are
6273 specified, the default includes are used (@pxref{Default Includes}).  If
6274 @var{action-if-found} is given, it is additional shell code to execute
6275 when one of the types is found.  If @var{action-if-not-found} is given,
6276 it is executed when one of the types is not found.
6278 This macro uses M4 lists:
6279 @example
6280 AC_CHECK_TYPES([ptrdiff_t])
6281 AC_CHECK_TYPES([unsigned long long int, uintmax_t])
6282 @end example
6284 @end defmac
6286 Autoconf, up to 2.13, used to provide to another version of
6287 @code{AC_CHECK_TYPE}, broken by design.  In order to keep backward
6288 compatibility, a simple heuristic, quite safe but not totally, is
6289 implemented.  In case of doubt, read the documentation of the former
6290 @code{AC_CHECK_TYPE}, see @ref{Obsolete Macros}.
6293 @node Compilers and Preprocessors
6294 @section Compilers and Preprocessors
6295 @cindex Compilers
6296 @cindex Preprocessors
6298 @ovindex EXEEXT
6299 All the tests for compilers (@code{AC_PROG_CC}, @code{AC_PROG_CXX},
6300 @code{AC_PROG_F77}) define the output variable @code{EXEEXT} based on
6301 the output of the compiler, typically to the empty string if
6302 Posix and @samp{.exe} if a @acronym{DOS} variant.
6304 @ovindex OBJEXT
6305 They also define the output variable @code{OBJEXT} based on the
6306 output of the compiler, after @file{.c} files have been excluded, typically
6307 to @samp{o} if Posix, @samp{obj} if a @acronym{DOS} variant.
6309 If the compiler being used does not produce executables, the tests fail.  If
6310 the executables can't be run, and cross-compilation is not enabled, they
6311 fail too.  @xref{Manual Configuration}, for more on support for cross
6312 compiling.
6314 @menu
6315 * Specific Compiler Characteristics::  Some portability issues
6316 * Generic Compiler Characteristics::  Language independent tests and features
6317 * C Compiler::                  Checking its characteristics
6318 * C++ Compiler::                Likewise
6319 * Objective C Compiler::        Likewise
6320 * Erlang Compiler and Interpreter::  Likewise
6321 * Fortran Compiler::            Likewise
6322 @end menu
6324 @node Specific Compiler Characteristics
6325 @subsection Specific Compiler Characteristics
6327 Some compilers exhibit different behaviors.
6329 @table @asis
6330 @item Static/Dynamic Expressions
6331 Autoconf relies on a trick to extract one bit of information from the C
6332 compiler: using negative array sizes.  For instance the following
6333 excerpt of a C source demonstrates how to test whether @samp{int} objects are 4
6334 bytes wide:
6336 @example
6337 static int test_array[sizeof (int) == 4 ? 1 : -1];
6338 @end example
6340 @noindent
6341 To our knowledge, there is a single compiler that does not support this
6342 trick: the @acronym{HP} C compilers (the real ones, not only the
6343 ``bundled'') on @acronym{HP-UX} 11.00.
6344 They incorrectly reject the above program with the diagnostic
6345 ``Variable-length arrays cannot have static storage.''
6346 This bug comes from @acronym{HP} compilers' mishandling of @code{sizeof (int)},
6347 not from the @code{? 1 : -1}, and
6348 Autoconf works around this problem by casting @code{sizeof (int)} to
6349 @code{long int} before comparing it.
6350 @end table
6352 @node Generic Compiler Characteristics
6353 @subsection Generic Compiler Characteristics
6355 @anchor{AC_CHECK_SIZEOF}
6356 @defmac AC_CHECK_SIZEOF (@var{type-or-expr}, @ovar{unused}, @
6357   @dvar{includes, default-includes})
6358 @acindex{CHECK_SIZEOF}
6359 @cvindex SIZEOF_@var{type-or-expr}
6360 Define @code{SIZEOF_@var{type-or-expr}} (@pxref{Standard Symbols}) to be
6361 the size in bytes of @var{type-or-expr}, which may be either a type or
6362 an expression returning a value that has a size.  If the expression
6363 @samp{sizeof (@var{type-or-expr})} is invalid, the result is 0.  If no
6364 @var{includes} are specified, the default includes are used
6365 (@pxref{Default Includes}).
6367 This macro now works even when cross-compiling.  The @var{unused}
6368 argument was used when cross-compiling.
6370 For example, the call
6372 @example
6373 AC_CHECK_SIZEOF([int *])
6374 @end example
6376 @noindent
6377 defines @code{SIZEOF_INT_P} to be 8 on DEC Alpha AXP systems.
6378 @end defmac
6380 @defmac AC_CHECK_ALIGNOF (@var{type}, @dvar{includes, default-includes})
6381 @acindex{CHECK_ALIGNOF}
6382 @cvindex ALIGNOF_@var{type}
6383 Define @code{ALIGNOF_@var{type}} (@pxref{Standard Symbols}) to be the
6384 alignment in bytes of @var{type}.  @samp{@var{type} y;} must be valid as
6385 a structure member declaration.  If @samp{type} is unknown, the result
6386 is 0.  If no @var{includes} are specified, the default includes are used
6387 (@pxref{Default Includes}).
6388 @end defmac
6390 @defmac AC_COMPUTE_INT (@var{var}, @var{expression}, @
6391   @dvar{includes, default-includes}, @ovar{action-if-fails})
6392 @acindex{COMPUTE_INT}
6393 Store into the shell variable @var{var} the value of the integer
6394 @var{expression}.  The
6395 value should fit in an initializer in a C variable of type @code{signed
6396 long}.  To support cross compilation (in which case, the macro only works on
6397 hosts that use twos-complement arithmetic), it should be possible to evaluate
6398 the expression at compile-time.  If no @var{includes} are specified, the
6399 default includes are used (@pxref{Default Includes}).
6401 Execute @var{action-if-fails} if the value cannot be determined correctly.
6402 @end defmac
6404 @defmac AC_LANG_WERROR
6405 @acindex{LANG_WERROR}
6406 Normally Autoconf ignores warnings generated by the compiler, linker, and
6407 preprocessor.  If this macro is used, warnings count as fatal
6408 errors for the current language.  This macro is useful when the
6409 results of configuration are used where warnings are unacceptable; for
6410 instance, if parts of a program are built with the @acronym{GCC}
6411 @option{-Werror}
6412 option.  If the whole program is built using @option{-Werror} it is
6413 often simpler to put @option{-Werror} in the compiler flags (@code{CFLAGS},
6414 etc.).
6415 @end defmac
6417 @defmac AC_OPENMP
6418 @acindex{OPENMP}
6419 @cvindex _OPENMP
6420 @ovindex OPENMP_CFLAGS
6421 @ovindex OPENMP_CXXFLAGS
6422 @ovindex OPENMP_FFLAGS
6423 @ovindex OPENMP_FCFLAGS
6424 OpenMP (@url{http://www.openmp.org/}) specifies extensions of C, C++,
6425 and Fortran that simplify optimization of shared memory parallelism,
6426 which is a common problem on multicore CPUs.
6428 If the current language is C, the macro @code{AC_OPENMP} sets the
6429 variable @code{OPENMP_CFLAGS} to the C compiler flags needed for
6430 supporting OpenMP@.  @code{OPENMP_CFLAGS} is set to empty if the
6431 compiler already supports OpenMP, if it has no way to activate OpenMP
6432 support, or if the user rejects OpenMP support by invoking
6433 @samp{configure} with the @samp{--disable-openmp} option.
6435 @code{OPENMP_CFLAGS} needs to be used when compiling programs, when
6436 preprocessing program source, and when linking programs.  Therefore you
6437 need to add @code{$(OPENMP_CFLAGS)} to the @code{CFLAGS} of C programs
6438 that use OpenMP@.  If you preprocess OpenMP-specific C code, you also
6439 need to add @code{$(OPENMP_CFLAGS)} to @code{CPPFLAGS}.  The presence of
6440 OpenMP support is revealed at compile time by the preprocessor macro
6441 @code{_OPENMP}.
6443 Linking a program with @code{OPENMP_CFLAGS} typically adds one more
6444 shared library to the program's dependencies, so its use is recommended
6445 only on programs that actually require OpenMP.
6447 If the current language is C++, @code{AC_OPENMP} sets the variable
6448 @code{OPENMP_CXXFLAGS}, suitably for the C++ compiler.  The same remarks
6449 hold as for C.
6451 If the current language is Fortran 77 or Fortran, @code{AC_OPENMP} sets
6452 the variable @code{OPENMP_FFLAGS} or @code{OPENMP_FCFLAGS},
6453 respectively.  Similar remarks as for C hold, except that
6454 @code{CPPFLAGS} is not used for Fortran, and no preprocessor macro
6455 signals OpenMP support.
6456 @end defmac
6458 @node C Compiler
6459 @subsection C Compiler Characteristics
6461 The following macros provide ways to find and exercise a C Compiler.
6462 There are a few constructs that ought to be avoided, but do not deserve
6463 being checked for, since they can easily be worked around.
6465 @table @asis
6466 @item Don't use lines containing solitary backslashes
6467 They tickle a bug in the @acronym{HP-UX} C compiler (checked on
6468 @acronym{HP-UX} 10.20,
6469 11.00, and 11i).  When given the following source:
6471 @example
6472 #ifdef __STDC__
6474 * A comment with backslash-newlines in it.  %@{ %@} *\
6477 char str[] = "\\
6478 " A string with backslash-newlines in it %@{ %@} \\
6480 char apostrophe = '\\
6484 #endif
6485 @end example
6487 @noindent
6488 the compiler incorrectly fails with the diagnostics ``Non-terminating
6489 comment at end of file'' and ``Missing @samp{#endif} at end of file.''
6490 Removing the lines with solitary backslashes solves the problem.
6492 @item Don't compile several files at once if output matters to you
6493 Some compilers, such as @acronym{HP}'s, report names of files being
6494 compiled when given more than one file operand.  For instance:
6496 @example
6497 $ @kbd{cc a.c b.c}
6498 a.c:
6499 b.c:
6500 @end example
6502 @noindent
6503 This can cause problems if you observe the output of the compiler to
6504 detect failures.  Invoking @samp{cc -c a.c && cc -c b.c && cc -o c a.o
6505 b.o} solves the issue.
6507 @item Don't rely on @code{#error} failing
6508 The @sc{irix} C compiler does not fail when #error is preprocessed; it
6509 simply emits a diagnostic and continues, exiting successfully.  So,
6510 instead of an error directive like @code{#error "Unsupported word size"}
6511 it is more portable to use an invalid directive like @code{#Unsupported
6512 word size} in Autoconf tests.  In ordinary source code, @code{#error} is
6513 OK, since installers with inadequate compilers like @sc{irix} can simply
6514 examine these compilers' diagnostic output.
6516 @item Don't rely on correct @code{#line} support
6517 On Solaris, @command{c89} (at least Sun C 5.3 through 5.8)
6518 diagnoses @code{#line} directives whose line
6519 numbers are greater than 32767.  Nothing in Posix
6520 makes this invalid.  That is why Autoconf stopped issuing
6521 @code{#line} directives.
6522 @end table
6524 @defmac AC_PROG_CC (@ovar{compiler-search-list})
6525 @acindex{PROG_CC}
6526 @ovindex CC
6527 @ovindex CFLAGS
6528 Determine a C compiler to use.  If @code{CC} is not already set in the
6529 environment, check for @code{gcc} and @code{cc}, then for other C
6530 compilers.  Set output variable @code{CC} to the name of the compiler
6531 found.
6533 This macro may, however, be invoked with an optional first argument
6534 which, if specified, must be a blank-separated list of C compilers to
6535 search for.  This just gives the user an opportunity to specify an
6536 alternative search list for the C compiler.  For example, if you didn't
6537 like the default order, then you could invoke @code{AC_PROG_CC} like
6538 this:
6540 @example
6541 AC_PROG_CC([gcc cl cc])
6542 @end example
6544 If the C compiler does not handle function prototypes correctly by
6545 default, try to add an option to output variable @code{CC} to make it
6546 so.  This macro tries various options that select standard-conformance
6547 modes on various systems.
6549 After calling this macro you can check whether the C compiler has been
6550 set to accept @acronym{ANSI} C89 (@acronym{ISO} C90); if not, the shell
6551 variable
6552 @code{ac_cv_prog_cc_c89} is set to @samp{no}.  See also
6553 @code{AC_C_PROTOTYPES} below.
6555 If using the @acronym{GNU} C compiler, set shell variable @code{GCC} to
6556 @samp{yes}.  If output variable @code{CFLAGS} was not already set, set
6557 it to @option{-g -O2} for the @acronym{GNU} C compiler (@option{-O2} on systems
6558 where @acronym{GCC} does not accept @option{-g}), or @option{-g} for
6559 other compilers.
6560 @end defmac
6562 @anchor{AC_PROG_CC_C_O}
6563 @defmac AC_PROG_CC_C_O
6564 @acindex{PROG_CC_C_O}
6565 @cvindex NO_MINUS_C_MINUS_O
6566 If the C compiler does not accept the @option{-c} and @option{-o} options
6567 simultaneously, define @code{NO_MINUS_C_MINUS_O}.  This macro actually
6568 tests both the compiler found by @code{AC_PROG_CC}, and, if different,
6569 the first @code{cc} in the path.  The test fails if one fails.  This
6570 macro was created for @acronym{GNU} Make to choose the default C compilation
6571 rule.
6572 @end defmac
6575 @defmac AC_PROG_CPP
6576 @acindex{PROG_CPP}
6577 @ovindex CPP
6578 Set output variable @code{CPP} to a command that runs the
6579 C preprocessor.  If @samp{$CC -E} doesn't work, @file{/lib/cpp} is used.
6580 It is only portable to run @code{CPP} on files with a @file{.c}
6581 extension.
6583 Some preprocessors don't indicate missing include files by the error
6584 status.  For such preprocessors an internal variable is set that causes
6585 other macros to check the standard error from the preprocessor and
6586 consider the test failed if any warnings have been reported.
6587 For most preprocessors, though, warnings do not cause include-file
6588 tests to fail unless @code{AC_PROG_CPP_WERROR} is also specified.
6589 @end defmac
6591 @defmac AC_PROG_CPP_WERROR
6592 @acindex{PROG_CPP_WERROR}
6593 @ovindex CPP
6594 This acts like @code{AC_PROG_CPP}, except it treats warnings from the
6595 preprocessor as errors even if the preprocessor exit status indicates
6596 success.  This is useful for avoiding headers that generate mandatory
6597 warnings, such as deprecation notices.
6598 @end defmac
6601 The following macros check for C compiler or machine architecture
6602 features.  To check for characteristics not listed here, use
6603 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
6604 @code{AC_RUN_IFELSE} (@pxref{Runtime}).
6606 @defmac AC_PROG_CC_STDC
6607 @acindex{PROG_CC_STDC}
6608 If the C compiler cannot compile @acronym{ISO} Standard C (currently
6609 C99), try to add an option to output variable @code{CC} to make it work.
6610 If the compiler does not support C99, fall back to supporting
6611 @acronym{ANSI} C89 (@acronym{ISO} C90).
6613 After calling this macro you can check whether the C compiler has been
6614 set to accept Standard C; if not, the shell variable
6615 @code{ac_cv_prog_cc_stdc} is set to @samp{no}.
6616 @end defmac
6618 @defmac AC_PROG_CC_C89
6619 @acindex{PROG_CC_C89}
6620 If the C compiler is not in @acronym{ANSI} C89 (@acronym{ISO} C90) mode by
6621 default, try to add an option to output variable @code{CC} to make it
6622 so.  This macro tries various options that select @acronym{ANSI} C89 on
6623 some system or another.  It considers the compiler to be in
6624 @acronym{ANSI} C89 mode if it handles function prototypes correctly.
6626 After calling this macro you can check whether the C compiler has been
6627 set to accept @acronym{ANSI} C89; if not, the shell variable
6628 @code{ac_cv_prog_cc_c89} is set to @samp{no}.
6630 This macro is called automatically by @code{AC_PROG_CC}.
6631 @end defmac
6633 @defmac AC_PROG_CC_C99
6634 @acindex{PROG_CC_C99}
6635 If the C compiler is not in C99 mode by default, try to add an
6636 option to output variable @code{CC} to make it so.  This macro tries
6637 various options that select C99 on some system or another.  It
6638 considers the compiler to be in C99 mode if it handles @code{_Bool},
6639 @code{//} comments, flexible array members, @code{inline}, signed and
6640 unsigned @code{long long int}, mixed code and declarations, named
6641 initialization of structs,
6642 @code{restrict}, @code{va_copy}, varargs macros, variable declarations
6643 in @code{for} loops, and variable length arrays.
6645 After calling this macro you can check whether the C compiler has been
6646 set to accept C99; if not, the shell variable
6647 @code{ac_cv_prog_cc_c99} is set to @samp{no}.
6648 @end defmac
6650 @defmac AC_C_BACKSLASH_A
6651 @acindex{C_BACKSLASH_A}
6652 @cvindex HAVE_C_BACKSLASH_A
6653 Define @samp{HAVE_C_BACKSLASH_A} to 1 if the C compiler understands
6654 @samp{\a}.
6656 This macro is obsolescent, as current C compilers understand @samp{\a}.
6657 New programs need not use this macro.
6658 @end defmac
6660 @anchor{AC_C_BIGENDIAN}
6661 @defmac AC_C_BIGENDIAN (@ovar{action-if-true}, @ovar{action-if-false}, @
6662   @ovar{action-if-unknown}, @ovar{action-if-universal})
6663 @acindex{C_BIGENDIAN}
6664 @cvindex WORDS_BIGENDIAN
6665 @cindex Endianness
6666 If words are stored with the most significant byte first (like Motorola
6667 and SPARC CPUs), execute @var{action-if-true}.  If words are stored with
6668 the least significant byte first (like Intel and VAX CPUs), execute
6669 @var{action-if-false}.
6671 This macro runs a test-case if endianness cannot be determined from the
6672 system header files.  When cross-compiling, the test-case is not run but
6673 grep'ed for some magic values.  @var{action-if-unknown} is executed if
6674 the latter case fails to determine the byte sex of the host system.
6676 In some cases a single run of a compiler can generate code for multiple
6677 architectures.  This can happen, for example, when generating Mac OS X
6678 universal binary files, which work on both PowerPC and Intel
6679 architectures.  In this case, the different variants might be for
6680 different architectures whose endiannesses differ.  If
6681 @command{configure} detects this, it executes @var{action-if-universal}
6682 instead of @var{action-if-unknown}.
6684 The default for @var{action-if-true} is to define
6685 @samp{WORDS_BIGENDIAN}.  The default for @var{action-if-false} is to do
6686 nothing.  The default for @var{action-if-unknown} is to
6687 abort configure and tell the installer how to bypass this test.
6688 And finally, the default for @var{action-if-universal} is to define
6689 @samp{WORDS_BIGENDIAN} or not, depending on the architecture that the
6690 code is being generated for.
6692 If you use this macro without specifying @var{action-if-universal}, you
6693 should also use @code{AC_CONFIG_HEADERS}; otherwise
6694 @samp{WORDS_BIGENDIAN} may be set incorrectly for Mac OS X universal
6695 binary files.
6696 @end defmac
6698 @anchor{AC_C_CONST}
6699 @defmac AC_C_CONST
6700 @acindex{C_CONST}
6701 @cvindex const
6702 If the C compiler does not fully support the @code{const} keyword,
6703 define @code{const} to be empty.  Some C compilers that do
6704 not define @code{__STDC__} do support @code{const}; some compilers that
6705 define @code{__STDC__} do not completely support @code{const}.  Programs
6706 can simply use @code{const} as if every C compiler supported it; for
6707 those that don't, the makefile or configuration header file
6708 defines it as empty.
6710 Occasionally installers use a C++ compiler to compile C code, typically
6711 because they lack a C compiler.  This causes problems with @code{const},
6712 because C and C++ treat @code{const} differently.  For example:
6714 @example
6715 const int foo;
6716 @end example
6718 @noindent
6719 is valid in C but not in C++.  These differences unfortunately cannot be
6720 papered over by defining @code{const} to be empty.
6722 If @command{autoconf} detects this situation, it leaves @code{const} alone,
6723 as this generally yields better results in practice.  However, using a
6724 C++ compiler to compile C code is not recommended or supported, and
6725 installers who run into trouble in this area should get a C compiler
6726 like @acronym{GCC} to compile their C code.
6728 This macro is obsolescent, as current C compilers support @code{const}.
6729 New programs need not use this macro.
6730 @end defmac
6732 @defmac AC_C_RESTRICT
6733 @acindex{C_RESTRICT}
6734 @cvindex restrict
6735 If the C compiler recognizes a variant spelling for the @code{restrict}
6736 keyword (@code{__restrict}, @code{__restrict__}, or @code{_Restrict}),
6737 then define @code{restrict} to that; this is more likely to do the right
6738 thing with compilers that support language variants where plain
6739 @code{restrict} is not a keyword.  Otherwise, if the C compiler
6740 recognizes the @code{restrict} keyword, don't do anything.
6741 Otherwise, define @code{restrict} to be empty.
6742 Thus, programs may simply use @code{restrict} as if every C compiler
6743 supported it; for those that do not, the makefile
6744 or configuration header defines it away.
6746 Although support in C++ for the @code{restrict} keyword is not
6747 required, several C++ compilers do accept the keyword.
6748 This macro works for them, too.
6749 @end defmac
6751 @defmac AC_C_VOLATILE
6752 @acindex{C_VOLATILE}
6753 @cvindex volatile
6754 If the C compiler does not understand the keyword @code{volatile},
6755 define @code{volatile} to be empty.  Programs can simply use
6756 @code{volatile} as if every C compiler supported it; for those that do
6757 not, the makefile or configuration header defines it as
6758 empty.
6760 If the correctness of your program depends on the semantics of
6761 @code{volatile}, simply defining it to be empty does, in a sense, break
6762 your code.  However, given that the compiler does not support
6763 @code{volatile}, you are at its mercy anyway.  At least your
6764 program compiles, when it wouldn't before.
6765 @xref{Volatile Objects}, for more about @code{volatile}.
6767 In general, the @code{volatile} keyword is a standard C feature, so
6768 you might expect that @code{volatile} is available only when
6769 @code{__STDC__} is defined.  However, Ultrix 4.3's native compiler does
6770 support volatile, but does not define @code{__STDC__}.
6772 This macro is obsolescent, as current C compilers support @code{volatile}.
6773 New programs need not use this macro.
6774 @end defmac
6776 @anchor{AC_C_INLINE}
6777 @defmac AC_C_INLINE
6778 @acindex{C_INLINE}
6779 @cvindex inline
6780 If the C compiler supports the keyword @code{inline}, do nothing.
6781 Otherwise define @code{inline} to @code{__inline__} or @code{__inline}
6782 if it accepts one of those, otherwise define @code{inline} to be empty.
6783 @end defmac
6785 @anchor{AC_C_CHAR_UNSIGNED}
6786 @defmac AC_C_CHAR_UNSIGNED
6787 @acindex{C_CHAR_UNSIGNED}
6788 @cvindex __CHAR_UNSIGNED__
6789 If the C type @code{char} is unsigned, define @code{__CHAR_UNSIGNED__},
6790 unless the C compiler predefines it.
6791 @end defmac
6793 @defmac AC_C_STRINGIZE
6794 @acindex{C_STRINGIZE}
6795 @cvindex HAVE_STRINGIZE
6796 If the C preprocessor supports the stringizing operator, define
6797 @code{HAVE_STRINGIZE}.  The stringizing operator is @samp{#} and is
6798 found in macros such as this:
6800 @example
6801 #define x(y) #y
6802 @end example
6804 This macro is obsolescent, as current C compilers support the
6805 stringizing operator.  New programs need not use this macro.
6806 @end defmac
6808 @defmac AC_C_FLEXIBLE_ARRAY_MEMBER
6809 @acindex{C_FLEXIBLE_ARRAY_MEMBER}
6810 @cvindex FLEXIBLE_ARRAY_MEMBER
6811 If the C compiler supports flexible array members, define
6812 @code{FLEXIBLE_ARRAY_MEMBER} to nothing; otherwise define it to 1.
6813 That way, a declaration like this:
6815 @example
6816 struct s
6817   @{
6818     size_t n_vals;
6819     double val[FLEXIBLE_ARRAY_MEMBER];
6820   @};
6821 @end example
6823 @noindent
6824 will let applications use the ``struct hack'' even with compilers that
6825 do not support flexible array members.  To allocate and use such an
6826 object, you can use code like this:
6828 @example
6829 size_t i;
6830 size_t n = compute_value_count ();
6831 struct s *p =
6832    malloc (offsetof (struct s, val)
6833            + n * sizeof (double));
6834 p->n_vals = n;
6835 for (i = 0; i < n; i++)
6836   p->val[i] = compute_value (i);
6837 @end example
6838 @end defmac
6840 @defmac AC_C_VARARRAYS
6841 @acindex{C_VARARRAYS}
6842 @cvindex HAVE_C_VARARRAYS
6843 If the C compiler supports variable-length arrays, define
6844 @code{HAVE_C_VARARRAYS}.  A variable-length array is an array of automatic
6845 storage duration whose length is determined at run time, when the array
6846 is declared.
6847 @end defmac
6849 @defmac AC_C_TYPEOF
6850 @acindex{C_TYPEOF}
6851 @cvindex HAVE_TYPEOF
6852 @cvindex typeof
6853 If the C compiler supports @acronym{GCC}'s @code{typeof} syntax either
6854 directly or
6855 through a different spelling of the keyword (e.g., @code{__typeof__}),
6856 define @code{HAVE_TYPEOF}.  If the support is available only through a
6857 different spelling, define @code{typeof} to that spelling.
6858 @end defmac
6860 @defmac AC_C_PROTOTYPES
6861 @acindex{C_PROTOTYPES}
6862 @cvindex PROTOTYPES
6863 @cvindex __PROTOTYPES
6864 @cvindex PARAMS
6865 If function prototypes are understood by the compiler (as determined by
6866 @code{AC_PROG_CC}), define @code{PROTOTYPES} and @code{__PROTOTYPES}.
6867 Defining @code{__PROTOTYPES} is for the benefit of
6868 header files that cannot use macros that infringe on user name space.
6870 This macro is obsolescent, as current C compilers support prototypes.
6871 New programs need not use this macro.
6872 @end defmac
6874 @anchor{AC_PROG_GCC_TRADITIONAL}
6875 @defmac AC_PROG_GCC_TRADITIONAL
6876 @acindex{PROG_GCC_TRADITIONAL}
6877 @ovindex CC
6878 Add @option{-traditional} to output variable @code{CC} if using the
6879 @acronym{GNU} C compiler and @code{ioctl} does not work properly without
6880 @option{-traditional}.  That usually happens when the fixed header files
6881 have not been installed on an old system.
6883 This macro is obsolescent, since current versions of the @acronym{GNU} C
6884 compiler fix the header files automatically when installed.
6885 @end defmac
6888 @node C++ Compiler
6889 @subsection C++ Compiler Characteristics
6892 @defmac AC_PROG_CXX (@ovar{compiler-search-list})
6893 @acindex{PROG_CXX}
6894 @ovindex CXX
6895 @ovindex CXXFLAGS
6896 Determine a C++ compiler to use.  Check whether the environment variable
6897 @code{CXX} or @code{CCC} (in that order) is set; if so, then set output
6898 variable @code{CXX} to its value.
6900 Otherwise, if the macro is invoked without an argument, then search for
6901 a C++ compiler under the likely names (first @code{g++} and @code{c++}
6902 then other names).  If none of those checks succeed, then as a last
6903 resort set @code{CXX} to @code{g++}.
6905 This macro may, however, be invoked with an optional first argument
6906 which, if specified, must be a blank-separated list of C++ compilers to
6907 search for.  This just gives the user an opportunity to specify an
6908 alternative search list for the C++ compiler.  For example, if you
6909 didn't like the default order, then you could invoke @code{AC_PROG_CXX}
6910 like this:
6912 @example
6913 AC_PROG_CXX([gcc cl KCC CC cxx cc++ xlC aCC c++ g++])
6914 @end example
6916 If using the @acronym{GNU} C++ compiler, set shell variable @code{GXX} to
6917 @samp{yes}.  If output variable @code{CXXFLAGS} was not already set, set
6918 it to @option{-g -O2} for the @acronym{GNU} C++ compiler (@option{-O2} on
6919 systems where G++ does not accept @option{-g}), or @option{-g} for other
6920 compilers.
6921 @end defmac
6923 @defmac AC_PROG_CXXCPP
6924 @acindex{PROG_CXXCPP}
6925 @ovindex CXXCPP
6926 Set output variable @code{CXXCPP} to a command that runs the C++
6927 preprocessor.  If @samp{$CXX -E} doesn't work, @file{/lib/cpp} is used.
6928 It is portable to run @code{CXXCPP} only on files with a @file{.c},
6929 @file{.C}, @file{.cc}, or @file{.cpp} extension.
6931 Some preprocessors don't indicate missing include files by the error
6932 status.  For such preprocessors an internal variable is set that causes
6933 other macros to check the standard error from the preprocessor and
6934 consider the test failed if any warnings have been reported.  However,
6935 it is not known whether such broken preprocessors exist for C++.
6936 @end defmac
6938 @defmac AC_PROG_CXX_C_O
6939 @acindex{PROG_CXX_C_O}
6940 @cvindex CXX_NO_MINUS_C_MINUS_O
6941 Test whether the C++ compiler accepts the options @option{-c} and
6942 @option{-o} simultaneously, and define @code{CXX_NO_MINUS_C_MINUS_O},
6943 if it does not.
6944 @end defmac
6947 @node Objective C Compiler
6948 @subsection Objective C Compiler Characteristics
6951 @defmac AC_PROG_OBJC (@ovar{compiler-search-list})
6952 @acindex{PROG_OBJC}
6953 @ovindex OBJC
6954 @ovindex OBJCFLAGS
6955 Determine an Objective C compiler to use.  If @code{OBJC} is not already
6956 set in the environment, check for Objective C compilers.  Set output
6957 variable @code{OBJC} to the name of the compiler found.
6959 This macro may, however, be invoked with an optional first argument
6960 which, if specified, must be a blank-separated list of Objective C compilers to
6961 search for.  This just gives the user an opportunity to specify an
6962 alternative search list for the Objective C compiler.  For example, if you
6963 didn't like the default order, then you could invoke @code{AC_PROG_OBJC}
6964 like this:
6966 @example
6967 AC_PROG_OBJC([gcc objcc objc])
6968 @end example
6970 If using the @acronym{GNU} Objective C compiler, set shell variable
6971 @code{GOBJC} to @samp{yes}.  If output variable @code{OBJCFLAGS} was not
6972 already set, set it to @option{-g -O2} for the @acronym{GNU} Objective C
6973 compiler (@option{-O2} on systems where @command{gcc} does not accept
6974 @option{-g}), or @option{-g} for other compilers.
6975 @end defmac
6977 @defmac AC_PROG_OBJCPP
6978 @acindex{PROG_OBJCPP}
6979 @ovindex OBJCPP
6980 Set output variable @code{OBJCPP} to a command that runs the Objective C
6981 preprocessor.  If @samp{$OBJC -E} doesn't work, @file{/lib/cpp} is used.
6982 @end defmac
6985 @node Erlang Compiler and Interpreter
6986 @subsection Erlang Compiler and Interpreter Characteristics
6987 @cindex Erlang
6989 Autoconf defines the following macros for determining paths to the essential
6990 Erlang/OTP programs:
6992 @defmac AC_ERLANG_PATH_ERLC (@ovar{value-if-not-found}, @dvar{path, $PATH})
6993 @acindex{ERLANG_PATH_ERLC}
6994 @ovindex ERLC
6995 @ovindex ERLCFLAGS
6996 Determine an Erlang compiler to use.  If @code{ERLC} is not already set in the
6997 environment, check for @command{erlc}.  Set output variable @code{ERLC} to the
6998 complete path of the compiler command found.  In addition, if @code{ERLCFLAGS}
6999 is not set in the environment, set it to an empty value.
7001 The two optional arguments have the same meaning as the two last arguments of
7002 macro @code{AC_PROG_PATH} for looking for the @command{erlc} program.  For
7003 example, to look for @command{erlc} only in the @file{/usr/lib/erlang/bin}
7004 directory:
7006 @example
7007 AC_ERLANG_PATH_ERLC([not found], [/usr/lib/erlang/bin])
7008 @end example
7009 @end defmac
7011 @defmac AC_ERLANG_NEED_ERLC (@dvar{path, $PATH})
7012 @acindex{ERLANG_NEED_ERLC}
7013 A simplified variant of the @code{AC_ERLANG_PATH_ERLC} macro, that prints an
7014 error message and exits the @command{configure} script if the @command{erlc}
7015 program is not found.
7016 @end defmac
7018 @defmac AC_ERLANG_PATH_ERL (@ovar{value-if-not-found}, @dvar{path, $PATH})
7019 @acindex{ERLANG_PATH_ERL}
7020 @ovindex ERL
7021 Determine an Erlang interpreter to use.  If @code{ERL} is not already
7022 set in the
7023 environment, check for @command{erl}.  Set output variable @code{ERL} to the
7024 complete path of the interpreter command found.
7026 The two optional arguments have the same meaning as the two last arguments of
7027 macro @code{AC_PROG_PATH} for looking for the @command{erl} program.  For
7028 example, to look for @command{erl} only in the @file{/usr/lib/erlang/bin}
7029 directory:
7031 @example
7032 AC_ERLANG_PATH_ERL([not found], [/usr/lib/erlang/bin])
7033 @end example
7034 @end defmac
7036 @defmac AC_ERLANG_NEED_ERL (@dvar{path, $PATH})
7037 @acindex{ERLANG_NEED_ERL}
7038 A simplified variant of the @code{AC_ERLANG_PATH_ERL} macro, that prints an
7039 error message and exits the @command{configure} script if the @command{erl}
7040 program is not found.
7041 @end defmac
7044 @node Fortran Compiler
7045 @subsection Fortran Compiler Characteristics
7046 @cindex Fortran
7047 @cindex F77
7049 The Autoconf Fortran support is divided into two categories: legacy
7050 Fortran 77 macros (@code{F77}), and modern Fortran macros (@code{FC}).
7051 The former are intended for traditional Fortran 77 code, and have output
7052 variables like @code{F77}, @code{FFLAGS}, and @code{FLIBS}.  The latter
7053 are for newer programs that can (or must) compile under the newer
7054 Fortran standards, and have output variables like @code{FC},
7055 @code{FCFLAGS}, and @code{FCLIBS}.
7057 Except for two new macros @code{AC_FC_SRCEXT} and
7058 @code{AC_FC_FREEFORM} (see below), the @code{FC} and @code{F77} macros
7059 behave almost identically, and so they are documented together in this
7060 section.
7063 @defmac AC_PROG_F77 (@ovar{compiler-search-list})
7064 @acindex{PROG_F77}
7065 @ovindex F77
7066 @ovindex FFLAGS
7067 Determine a Fortran 77 compiler to use.  If @code{F77} is not already
7068 set in the environment, then check for @code{g77} and @code{f77}, and
7069 then some other names.  Set the output variable @code{F77} to the name
7070 of the compiler found.
7072 This macro may, however, be invoked with an optional first argument
7073 which, if specified, must be a blank-separated list of Fortran 77
7074 compilers to search for.  This just gives the user an opportunity to
7075 specify an alternative search list for the Fortran 77 compiler.  For
7076 example, if you didn't like the default order, then you could invoke
7077 @code{AC_PROG_F77} like this:
7079 @example
7080 AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
7081 @end example
7083 If using @code{g77} (the @acronym{GNU} Fortran 77 compiler), then
7084 set the shell variable @code{G77} to @samp{yes}.
7085 If the output variable @code{FFLAGS} was not already set in the
7086 environment, then set it to @option{-g -02} for @code{g77} (or @option{-O2}
7087 where @code{g77} does not accept @option{-g}).  Otherwise, set
7088 @code{FFLAGS} to @option{-g} for all other Fortran 77 compilers.
7089 @end defmac
7091 @defmac AC_PROG_FC (@ovar{compiler-search-list}, @ovar{dialect})
7092 @acindex{PROG_FC}
7093 @ovindex FC
7094 @ovindex FCFLAGS
7095 Determine a Fortran compiler to use.  If @code{FC} is not already set in
7096 the environment, then @code{dialect} is a hint to indicate what Fortran
7097 dialect to search for; the default is to search for the newest available
7098 dialect.  Set the output variable @code{FC} to the name of the compiler
7099 found.
7101 By default, newer dialects are preferred over older dialects, but if
7102 @code{dialect} is specified then older dialects are preferred starting
7103 with the specified dialect.  @code{dialect} can currently be one of
7104 Fortran 77, Fortran 90, or Fortran 95.  However, this is only a hint of
7105 which compiler @emph{name} to prefer (e.g., @code{f90} or @code{f95}),
7106 and no attempt is made to guarantee that a particular language standard
7107 is actually supported.  Thus, it is preferable that you avoid the
7108 @code{dialect} option, and use AC_PROG_FC only for code compatible with
7109 the latest Fortran standard.
7111 This macro may, alternatively, be invoked with an optional first argument
7112 which, if specified, must be a blank-separated list of Fortran
7113 compilers to search for, just as in @code{AC_PROG_F77}.
7115 If the output variable @code{FCFLAGS} was not already set in the
7116 environment, then set it to @option{-g -02} for @acronym{GNU} @code{g77} (or
7117 @option{-O2} where @code{g77} does not accept @option{-g}).  Otherwise,
7118 set @code{FCFLAGS} to @option{-g} for all other Fortran compilers.
7119 @end defmac
7121 @defmac AC_PROG_F77_C_O
7122 @defmacx AC_PROG_FC_C_O
7123 @acindex{PROG_F77_C_O}
7124 @acindex{PROG_FC_C_O}
7125 @cvindex F77_NO_MINUS_C_MINUS_O
7126 @cvindex FC_NO_MINUS_C_MINUS_O
7127 Test whether the Fortran compiler accepts the options @option{-c} and
7128 @option{-o} simultaneously, and define @code{F77_NO_MINUS_C_MINUS_O} or
7129 @code{FC_NO_MINUS_C_MINUS_O}, respectively, if it does not.
7130 @end defmac
7132 The following macros check for Fortran compiler characteristics.
7133 To check for characteristics not listed here, use
7134 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
7135 @code{AC_RUN_IFELSE} (@pxref{Runtime}), making sure to first set the
7136 current language to Fortran 77 or Fortran via @code{AC_LANG([Fortran 77])}
7137 or @code{AC_LANG(Fortran)} (@pxref{Language Choice}).
7140 @defmac AC_F77_LIBRARY_LDFLAGS
7141 @defmacx AC_FC_LIBRARY_LDFLAGS
7142 @acindex{F77_LIBRARY_LDFLAGS}
7143 @ovindex FLIBS
7144 @acindex{FC_LIBRARY_LDFLAGS}
7145 @ovindex FCLIBS
7146 Determine the linker flags (e.g., @option{-L} and @option{-l}) for the
7147 @dfn{Fortran intrinsic and runtime libraries} that are required to
7148 successfully link a Fortran program or shared library.  The output
7149 variable @code{FLIBS} or @code{FCLIBS} is set to these flags (which
7150 should be included after @code{LIBS} when linking).
7152 This macro is intended to be used in those situations when it is
7153 necessary to mix, e.g., C++ and Fortran source code in a single
7154 program or shared library (@pxref{Mixing Fortran 77 With C and C++, , ,
7155 automake, @acronym{GNU} Automake}).
7157 For example, if object files from a C++ and Fortran compiler must be
7158 linked together, then the C++ compiler/linker must be used for linking
7159 (since special C++-ish things need to happen at link time like calling
7160 global constructors, instantiating templates, enabling exception
7161 support, etc.).
7163 However, the Fortran intrinsic and runtime libraries must be linked in
7164 as well, but the C++ compiler/linker doesn't know by default how to add
7165 these Fortran 77 libraries.  Hence, this macro was created to determine
7166 these Fortran libraries.
7168 The macros @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7169 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} are probably also necessary to
7170 link C/C++ with Fortran; see below.
7171 @end defmac
7173 @defmac AC_F77_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7174 @defmacx AC_FC_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7175 @acindex{F77_DUMMY_MAIN}
7176 @cvindex F77_DUMMY_MAIN
7177 With many compilers, the Fortran libraries detected by
7178 @code{AC_F77_LIBRARY_LDFLAGS} or @code{AC_FC_LIBRARY_LDFLAGS} provide
7179 their own @code{main} entry function that initializes things like
7180 Fortran I/O, and which then calls a user-provided entry function named
7181 (say) @code{MAIN__} to run the user's program.  The
7182 @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7183 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros figure out how to deal with
7184 this interaction.
7186 When using Fortran for purely numerical functions (no I/O, etc.)@: often
7187 one prefers to provide one's own @code{main} and skip the Fortran
7188 library initializations.  In this case, however, one may still need to
7189 provide a dummy @code{MAIN__} routine in order to prevent linking errors
7190 on some systems.  @code{AC_F77_DUMMY_MAIN} or @code{AC_FC_DUMMY_MAIN}
7191 detects whether any such routine is @emph{required} for linking, and
7192 what its name is; the shell variable @code{F77_DUMMY_MAIN} or
7193 @code{FC_DUMMY_MAIN} holds this name, @code{unknown} when no solution
7194 was found, and @code{none} when no such dummy main is needed.
7196 By default, @var{action-if-found} defines @code{F77_DUMMY_MAIN} or
7197 @code{FC_DUMMY_MAIN} to the name of this routine (e.g., @code{MAIN__})
7198 @emph{if} it is required.  @var{action-if-not-found} defaults to
7199 exiting with an error.
7201 In order to link with Fortran routines, the user's C/C++ program should
7202 then include the following code to define the dummy main if it is
7203 needed:
7205 @example
7206 #ifdef F77_DUMMY_MAIN
7207 #  ifdef __cplusplus
7208      extern "C"
7209 #  endif
7210    int F77_DUMMY_MAIN() @{ return 1; @}
7211 #endif
7212 @end example
7214 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7216 Note that this macro is called automatically from @code{AC_F77_WRAPPERS}
7217 or @code{AC_FC_WRAPPERS}; there is generally no need to call it
7218 explicitly unless one wants to change the default actions.
7219 @end defmac
7221 @defmac AC_F77_MAIN
7222 @defmacx AC_FC_MAIN
7223 @acindex{F77_MAIN}
7224 @cvindex F77_MAIN
7225 @acindex{FC_MAIN}
7226 @cvindex FC_MAIN
7227 As discussed above, many Fortran libraries allow you to provide an entry
7228 point called (say) @code{MAIN__} instead of the usual @code{main}, which
7229 is then called by a @code{main} function in the Fortran libraries that
7230 initializes things like Fortran I/O@.  The
7231 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros detect whether it is
7232 @emph{possible} to utilize such an alternate main function, and defines
7233 @code{F77_MAIN} and @code{FC_MAIN} to the name of the function.  (If no
7234 alternate main function name is found, @code{F77_MAIN} and @code{FC_MAIN} are
7235 simply defined to @code{main}.)
7237 Thus, when calling Fortran routines from C that perform things like I/O,
7238 one should use this macro and declare the "main" function like so:
7240 @example
7241 #ifdef __cplusplus
7242   extern "C"
7243 #endif
7244 int F77_MAIN(int argc, char *argv[]);
7245 @end example
7247 (Again, replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7248 @end defmac
7250 @defmac AC_F77_WRAPPERS
7251 @defmacx AC_FC_WRAPPERS
7252 @acindex{F77_WRAPPERS}
7253 @cvindex F77_FUNC
7254 @cvindex F77_FUNC_
7255 @acindex{FC_WRAPPERS}
7256 @cvindex FC_FUNC
7257 @cvindex FC_FUNC_
7258 Defines C macros @code{F77_FUNC (name, NAME)}, @code{FC_FUNC (name, NAME)},
7259 @code{F77_FUNC_(name, NAME)}, and @code{FC_FUNC_(name, NAME)} to properly
7260 mangle the names of C/C++ identifiers, and identifiers with underscores,
7261 respectively, so that they match the name-mangling scheme used by the
7262 Fortran compiler.
7264 Fortran is case-insensitive, and in order to achieve this the Fortran
7265 compiler converts all identifiers into a canonical case and format.  To
7266 call a Fortran subroutine from C or to write a C function that is
7267 callable from Fortran, the C program must explicitly use identifiers in
7268 the format expected by the Fortran compiler.  In order to do this, one
7269 simply wraps all C identifiers in one of the macros provided by
7270 @code{AC_F77_WRAPPERS} or @code{AC_FC_WRAPPERS}.  For example, suppose
7271 you have the following Fortran 77 subroutine:
7273 @example
7274       subroutine foobar (x, y)
7275       double precision x, y
7276       y = 3.14159 * x
7277       return
7278       end
7279 @end example
7281 You would then declare its prototype in C or C++ as:
7283 @example
7284 #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
7285 #ifdef __cplusplus
7286 extern "C"  /* prevent C++ name mangling */
7287 #endif
7288 void FOOBAR_F77(double *x, double *y);
7289 @end example
7291 Note that we pass both the lowercase and uppercase versions of the
7292 function name to @code{F77_FUNC} so that it can select the right one.
7293 Note also that all parameters to Fortran 77 routines are passed as
7294 pointers (@pxref{Mixing Fortran 77 With C and C++, , , automake, @acronym{GNU}
7295 Automake}).
7297 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7299 Although Autoconf tries to be intelligent about detecting the
7300 name-mangling scheme of the Fortran compiler, there may be Fortran
7301 compilers that it doesn't support yet.  In this case, the above code
7302 generates a compile-time error, but some other behavior
7303 (e.g., disabling Fortran-related features) can be induced by checking
7304 whether @code{F77_FUNC} or @code{FC_FUNC} is defined.
7306 Now, to call that routine from a C program, we would do something like:
7308 @example
7310     double x = 2.7183, y;
7311     FOOBAR_F77 (&x, &y);
7313 @end example
7315 If the Fortran identifier contains an underscore (e.g., @code{foo_bar}),
7316 you should use @code{F77_FUNC_} or @code{FC_FUNC_} instead of
7317 @code{F77_FUNC} or @code{FC_FUNC} (with the same arguments).  This is
7318 because some Fortran compilers mangle names differently if they contain
7319 an underscore.
7320 @end defmac
7322 @defmac AC_F77_FUNC (@var{name}, @ovar{shellvar})
7323 @defmacx AC_FC_FUNC (@var{name}, @ovar{shellvar})
7324 @acindex{F77_FUNC}
7325 @acindex{FC_FUNC}
7326 Given an identifier @var{name}, set the shell variable @var{shellvar} to
7327 hold the mangled version @var{name} according to the rules of the
7328 Fortran linker (see also @code{AC_F77_WRAPPERS} or
7329 @code{AC_FC_WRAPPERS}).  @var{shellvar} is optional; if it is not
7330 supplied, the shell variable is simply @var{name}.  The purpose of
7331 this macro is to give the caller a way to access the name-mangling
7332 information other than through the C preprocessor as above, for example,
7333 to call Fortran routines from some language other than C/C++.
7334 @end defmac
7336 @defmac AC_FC_SRCEXT (@var{ext}, @ovar{action-if-success}, @
7337   @ovar{action-if-failure})
7338 @acindex{FC_SRCEXT}
7339 By default, the @code{FC} macros perform their tests using a @file{.f}
7340 extension for source-code files.  Some compilers, however, only enable
7341 newer language features for appropriately named files, e.g., Fortran 90
7342 features only for @file{.f90} files.  On the other hand, some other
7343 compilers expect all source files to end in @file{.f} and require
7344 special flags to support other file name extensions.  The
7345 @code{AC_FC_SRCEXT} macro deals with both of these issues.
7347 The @code{AC_FC_SRCEXT} tries to get the @code{FC} compiler to accept files
7348 ending with the extension .@var{ext} (i.e., @var{ext} does @emph{not}
7349 contain the dot).  If any special compiler flags are needed for this, it
7350 stores them in the output variable @code{FCFLAGS_}@var{ext}.  This
7351 extension and these flags are then used for all subsequent @code{FC} tests
7352 (until @code{AC_FC_SRCEXT} is called again).
7354 For example, you would use @code{AC_FC_SRCEXT(f90)} to employ the
7355 @file{.f90} extension in future tests, and it would set a
7356 @code{FCFLAGS_f90} output variable with any extra flags that are needed
7357 to compile such files.
7359 The @code{FCFLAGS_}@var{ext} can @emph{not} be simply absorbed into
7360 @code{FCFLAGS}, for two reasons based on the limitations of some
7361 compilers.  First, only one @code{FCFLAGS_}@var{ext} can be used at a
7362 time, so files with different extensions must be compiled separately.
7363 Second, @code{FCFLAGS_}@var{ext} must appear @emph{immediately} before
7364 the source-code file name when compiling.  So, continuing the example
7365 above, you might compile a @file{foo.f90} file in your makefile with the
7366 command:
7368 @example
7369 foo.o: foo.f90
7370      $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) '$(srcdir)/foo.f90'
7371 @end example
7373 If @code{AC_FC_SRCEXT} succeeds in compiling files with the @var{ext}
7374 extension, it calls @var{action-if-success} (defaults to nothing).  If
7375 it fails, and cannot find a way to make the @code{FC} compiler accept such
7376 files, it calls @var{action-if-failure} (defaults to exiting with an
7377 error message).
7379 @end defmac
7381 @defmac AC_FC_FREEFORM (@ovar{action-if-success}, @ovar{action-if-failure})
7382 @acindex{FC_FREEFORM}
7384 The @code{AC_FC_FREEFORM} tries to ensure that the Fortran compiler
7385 (@code{$FC}) allows free-format source code (as opposed to the older
7386 fixed-format style from Fortran 77).  If necessary, it may add some
7387 additional flags to @code{FCFLAGS}.
7389 This macro is most important if you are using the default @file{.f}
7390 extension, since many compilers interpret this extension as indicating
7391 fixed-format source unless an additional flag is supplied.  If you
7392 specify a different extension with @code{AC_FC_SRCEXT}, such as
7393 @file{.f90} or @file{.f95}, then @code{AC_FC_FREEFORM} ordinarily
7394 succeeds without modifying @code{FCFLAGS}.
7396 If @code{AC_FC_FREEFORM} succeeds in compiling free-form source, it
7397 calls @var{action-if-success} (defaults to nothing).  If it fails, it
7398 calls @var{action-if-failure} (defaults to exiting with an error
7399 message).
7400 @end defmac
7402 @node System Services
7403 @section System Services
7405 The following macros check for operating system services or capabilities.
7407 @anchor{AC_PATH_X}
7408 @defmac AC_PATH_X
7409 @acindex{PATH_X}
7410 @evindex XMKMF
7411 @cindex X Window System
7412 Try to locate the X Window System include files and libraries.  If the
7413 user gave the command line options @option{--x-includes=@var{dir}} and
7414 @option{--x-libraries=@var{dir}}, use those directories.
7416 If either or both were not given, get the missing values by running
7417 @code{xmkmf} (or an executable pointed to by the @code{XMKMF}
7418 environment variable) on a trivial @file{Imakefile} and examining the
7419 makefile that it produces.  Setting @code{XMKMF} to @samp{false}
7420 disables this method.
7422 If this method fails to find the X Window System, @command{configure}
7423 looks for the files in several directories where they often reside.
7424 If either method is successful, set the shell variables
7425 @code{x_includes} and @code{x_libraries} to their locations, unless they
7426 are in directories the compiler searches by default.
7428 If both methods fail, or the user gave the command line option
7429 @option{--without-x}, set the shell variable @code{no_x} to @samp{yes};
7430 otherwise set it to the empty string.
7431 @end defmac
7433 @anchor{AC_PATH_XTRA}
7434 @defmac AC_PATH_XTRA
7435 @acindex{PATH_XTRA}
7436 @ovindex X_CFLAGS
7437 @ovindex X_LIBS
7438 @ovindex X_EXTRA_LIBS
7439 @ovindex X_PRE_LIBS
7440 @cvindex X_DISPLAY_MISSING
7441 An enhanced version of @code{AC_PATH_X}.  It adds the C compiler flags
7442 that X needs to output variable @code{X_CFLAGS}, and the X linker flags
7443 to @code{X_LIBS}.  Define @code{X_DISPLAY_MISSING} if X is not
7444 available.
7446 This macro also checks for special libraries that some systems need in
7447 order to compile X programs.  It adds any that the system needs to
7448 output variable @code{X_EXTRA_LIBS}.  And it checks for special X11R6
7449 libraries that need to be linked with before @option{-lX11}, and adds
7450 any found to the output variable @code{X_PRE_LIBS}.
7452 @c This is an incomplete kludge.  Make a real way to do it.
7453 @c If you need to check for other X functions or libraries yourself, then
7454 @c after calling this macro, add the contents of @code{X_EXTRA_LIBS} to
7455 @c @code{LIBS} temporarily, like this: (FIXME - add example)
7456 @end defmac
7458 @anchor{AC_SYS_INTERPRETER}
7459 @defmac AC_SYS_INTERPRETER
7460 @acindex{SYS_INTERPRETER}
7461 Check whether the system supports starting scripts with a line of the
7462 form @samp{#!/bin/sh} to select the interpreter to use for the script.
7463 After running this macro, shell code in @file{configure.ac} can check
7464 the shell variable @code{interpval}; it is set to @samp{yes}
7465 if the system supports @samp{#!}, @samp{no} if not.
7466 @end defmac
7468 @defmac AC_SYS_LARGEFILE
7469 @acindex{SYS_LARGEFILE}
7470 @cvindex _FILE_OFFSET_BITS
7471 @cvindex _LARGE_FILES
7472 @ovindex CC
7473 @cindex Large file support
7474 @cindex LFS
7475 Arrange for
7476 @uref{http://www.unix-systems.org/@/version2/@/whatsnew/@/lfs20mar.html,
7477 large-file support}.  On some hosts, one must use special compiler
7478 options to build programs that can access large files.  Append any such
7479 options to the output variable @code{CC}.  Define
7480 @code{_FILE_OFFSET_BITS} and @code{_LARGE_FILES} if necessary.
7482 Large-file support can be disabled by configuring with the
7483 @option{--disable-largefile} option.
7485 If you use this macro, check that your program works even when
7486 @code{off_t} is wider than @code{long int}, since this is common when
7487 large-file support is enabled.  For example, it is not correct to print
7488 an arbitrary @code{off_t} value @code{X} with @code{printf ("%ld",
7489 (long int) X)}.
7491 The LFS introduced the @code{fseeko} and @code{ftello} functions to
7492 replace their C counterparts @code{fseek} and @code{ftell} that do not
7493 use @code{off_t}.  Take care to use @code{AC_FUNC_FSEEKO} to make their
7494 prototypes available when using them and large-file support is
7495 enabled.
7496 @end defmac
7498 @anchor{AC_SYS_LONG_FILE_NAMES}
7499 @defmac AC_SYS_LONG_FILE_NAMES
7500 @acindex{SYS_LONG_FILE_NAMES}
7501 @cvindex HAVE_LONG_FILE_NAMES
7502 If the system supports file names longer than 14 characters, define
7503 @code{HAVE_LONG_FILE_NAMES}.
7504 @end defmac
7506 @defmac AC_SYS_POSIX_TERMIOS
7507 @acindex{SYS_POSIX_TERMIOS}
7508 @cindex Posix termios headers
7509 @cindex termios Posix headers
7510 Check to see if the Posix termios headers and functions are available on the
7511 system.  If so, set the shell variable @code{ac_cv_sys_posix_termios} to
7512 @samp{yes}.  If not, set the variable to @samp{no}.
7513 @end defmac
7515 @node Posix Variants
7516 @section Posix Variants
7518 The following macro makes it possible to use features of Posix that are
7519 extensions to C, as well as platform extensions not defined by Posix.
7521 @anchor{AC_USE_SYSTEM_EXTENSIONS}
7522 @defmac AC_USE_SYSTEM_EXTENSIONS
7523 @acindex{USE_SYSTEM_EXTENSIONS}
7524 @cvindex _ALL_SOURCE
7525 @cvindex _GNU_SOURCE
7526 @cvindex _MINIX
7527 @cvindex _POSIX_1_SOURCE
7528 @cvindex _POSIX_PTHREAD_SEMANTICS
7529 @cvindex _POSIX_SOURCE
7530 @cvindex _TANDEM_SOURCE
7531 @cvindex __EXTENSIONS__
7532 This macro was introduced in Autoconf 2.60.  If possible, enable
7533 extensions to C or Posix on hosts that normally disable the extensions,
7534 typically due to standards-conformance namespace issues.  This should be
7535 called before any macros that run the C compiler.  The following
7536 preprocessor macros are defined where appropriate:
7538 @table @code
7539 @item _GNU_SOURCE
7540 Enable extensions on @acronym{GNU}/Linux.
7541 @item __EXTENSIONS__
7542 Enable general extensions on Solaris.
7543 @item _POSIX_PTHREAD_SEMANTICS
7544 Enable threading extensions on Solaris.
7545 @item _TANDEM_SOURCE
7546 Enable extensions for the @acronym{HP} NonStop platform.
7547 @item _ALL_SOURCE
7548 Enable extensions for @acronym{AIX} 3, and for Interix.
7549 @item _POSIX_SOURCE
7550 Enable Posix functions for Minix.
7551 @item _POSIX_1_SOURCE
7552 Enable additional Posix functions for Minix.
7553 @item _MINIX
7554 Identify Minix platform.  This particular preprocessor macro is
7555 obsolescent, and may be removed in a future release of Autoconf.
7556 @end table
7557 @end defmac
7560 @node Erlang Libraries
7561 @section Erlang Libraries
7562 @cindex Erlang, Library, checking
7564 The following macros check for an installation of Erlang/OTP, and for the
7565 presence of certain Erlang libraries.  All those macros require the
7566 configuration of an Erlang interpreter and an Erlang compiler
7567 (@pxref{Erlang Compiler and Interpreter}).
7569 @defmac AC_ERLANG_SUBST_ROOT_DIR
7570 @acindex{ERLANG_SUBST_ROOT_DIR}
7571 @ovindex ERLANG_ROOT_DIR
7573 Set the output variable @code{ERLANG_ROOT_DIR} to the path to the base
7574 directory in which Erlang/OTP is installed (as returned by Erlang's
7575 @code{code:root_dir/0} function).  The result of this test is cached if
7576 caching is enabled when running @command{configure}.
7577 @end defmac
7579 @defmac AC_ERLANG_SUBST_LIB_DIR
7580 @acindex{ERLANG_SUBST_LIB_DIR}
7581 @ovindex ERLANG_LIB_DIR
7583 Set the output variable @code{ERLANG_LIB_DIR} to the path of the library
7584 directory of Erlang/OTP (as returned by Erlang's
7585 @code{code:lib_dir/0} function), which subdirectories each contain an installed
7586 Erlang/OTP library.  The result of this test is cached if caching is enabled
7587 when running @command{configure}.
7588 @end defmac
7590 @defmac AC_ERLANG_CHECK_LIB (@var{library}, @ovar{action-if-found}, @
7591   @ovar{action-if-not-found})
7592 @acindex{ERLANG_CHECK_LIB}
7593 @ovindex ERLANG_LIB_DIR_@var{library}
7594 @ovindex ERLANG_LIB_VER_@var{library}
7596 Test whether the Erlang/OTP library @var{library} is installed by
7597 calling Erlang's @code{code:lib_dir/1} function.  The result of this
7598 test is cached if caching is enabled when running @command{configure}.
7599 @var{action-if-found} is a list of shell commands to run if the library
7600 is installed; @var{action-if-not-found} is a list of shell commands to
7601 run if it is not.  Additionally, if the library is installed, the output
7602 variable @samp{ERLANG_LIB_DIR_@var{library}} is set to the path to the
7603 library installation directory, and the output variable
7604 @samp{ERLANG_LIB_VER_@var{library}} is set to the version number that is
7605 part of the subdirectory name, if it is in the standard form
7606 (@code{@var{library}-@var{version}}).  If the directory name does not
7607 have a version part, @samp{ERLANG_LIB_VER_@var{library}} is set to the
7608 empty string.  If the library is not installed,
7609 @samp{ERLANG_LIB_DIR_@var{library}} and
7610 @samp{ERLANG_LIB_VER_@var{library}} are set to @code{"not found"}.  For
7611 example, to check if library @code{stdlib} is installed:
7613 @example
7614 AC_ERLANG_CHECK_LIB([stdlib],
7615   [echo "stdlib version \"$ERLANG_LIB_VER_stdlib\""
7616    echo "is installed in \"$ERLANG_LIB_DIR_stdlib\""],
7617   [AC_MSG_ERROR([stdlib was not found!])])
7618 @end example
7619 @end defmac
7621 In addition to the above macros, which test installed Erlang libraries, the
7622 following macros determine the paths to the directories into which newly built
7623 Erlang libraries are to be installed:
7625 @defmac AC_ERLANG_SUBST_INSTALL_LIB_DIR
7626 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
7627 @ovindex ERLANG_INSTALL_LIB_DIR
7629 Set the @code{ERLANG_INSTALL_LIB_DIR} output variable to the directory into
7630 which every built Erlang library should be installed in a separate
7631 subdirectory.
7632 If this variable is not set in the environment when @command{configure} runs,
7633 its default value is @code{$ERLANG_LIB_DIR}, which value is set by the
7634 @code{AC_ERLANG_SUBST_LIB_DIR} macro.
7635 @end defmac
7637 @defmac AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR (@var{library}, @var{version})
7638 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
7639 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
7641 Set the @samp{ERLANG_INSTALL_LIB_DIR_@var{library}} output variable to the
7642 directory into which the built Erlang library @var{library} version
7643 @var{version} should be installed.  If this variable is not set in the
7644 environment when @command{configure} runs, its default value is
7645 @samp{$ERLANG_INSTALL_LIB_DIR/@var{library}-@var{version}}, the value of the
7646 @code{ERLANG_INSTALL_LIB_DIR} variable being set by the
7647 @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR} macro.
7648 @end defmac
7654 @c ========================================================= Writing Tests
7656 @node Writing Tests
7657 @chapter Writing Tests
7659 If the existing feature tests don't do something you need, you have to
7660 write new ones.  These macros are the building blocks.  They provide
7661 ways for other macros to check whether various kinds of features are
7662 available and report the results.
7664 This chapter contains some suggestions and some of the reasons why the
7665 existing tests are written the way they are.  You can also learn a lot
7666 about how to write Autoconf tests by looking at the existing ones.  If
7667 something goes wrong in one or more of the Autoconf tests, this
7668 information can help you understand the assumptions behind them, which
7669 might help you figure out how to best solve the problem.
7671 These macros check the output of the compiler system of the current
7672 language (@pxref{Language Choice}).  They do not cache the results of
7673 their tests for future use (@pxref{Caching Results}), because they don't
7674 know enough about the information they are checking for to generate a
7675 cache variable name.  They also do not print any messages, for the same
7676 reason.  The checks for particular kinds of features call these macros
7677 and do cache their results and print messages about what they're
7678 checking for.
7680 When you write a feature test that could be applicable to more than one
7681 software package, the best thing to do is encapsulate it in a new macro.
7682 @xref{Writing Autoconf Macros}, for how to do that.
7684 @menu
7685 * Language Choice::             Selecting which language to use for testing
7686 * Writing Test Programs::       Forging source files for compilers
7687 * Running the Preprocessor::    Detecting preprocessor symbols
7688 * Running the Compiler::        Detecting language or header features
7689 * Running the Linker::          Detecting library features
7690 * Runtime::                     Testing for runtime features
7691 * Systemology::                 A zoology of operating systems
7692 * Multiple Cases::              Tests for several possible values
7693 @end menu
7695 @node Language Choice
7696 @section Language Choice
7697 @cindex Language
7699 Autoconf-generated @command{configure} scripts check for the C compiler and
7700 its features by default.  Packages that use other programming languages
7701 (maybe more than one, e.g., C and C++) need to test features of the
7702 compilers for the respective languages.  The following macros determine
7703 which programming language is used in the subsequent tests in
7704 @file{configure.ac}.
7706 @anchor{AC_LANG}
7707 @defmac AC_LANG (@var{language})
7708 Do compilation tests using the compiler, preprocessor, and file
7709 extensions for the specified @var{language}.
7711 Supported languages are:
7713 @table @samp
7714 @item C
7715 Do compilation tests using @code{CC} and @code{CPP} and use extension
7716 @file{.c} for test programs.  Use compilation flags: @code{CPPFLAGS} with
7717 @code{CPP}, and both @code{CPPFLAGS} and @code{CFLAGS} with @code{CC}.
7719 @item C++
7720 Do compilation tests using @code{CXX} and @code{CXXCPP} and use
7721 extension @file{.C} for test programs.  Use compilation flags:
7722 @code{CPPFLAGS} with @code{CXXCPP}, and both @code{CPPFLAGS} and
7723 @code{CXXFLAGS} with @code{CXX}.
7725 @item Fortran 77
7726 Do compilation tests using @code{F77} and use extension @file{.f} for
7727 test programs.  Use compilation flags: @code{FFLAGS}.
7729 @item Fortran
7730 Do compilation tests using @code{FC} and use extension @file{.f} (or
7731 whatever has been set by @code{AC_FC_SRCEXT}) for test programs.  Use
7732 compilation flags: @code{FCFLAGS}.
7734 @item Erlang
7735 @ovindex ERLC
7736 @ovindex ERL
7737 @ovindex ERLCFLAGS
7738 Compile and execute tests using @code{ERLC} and @code{ERL} and use extension
7739 @file{.erl} for test Erlang modules.  Use compilation flags: @code{ERLCFLAGS}.
7741 @item Objective C
7742 Do compilation tests using @code{OBJC} and @code{OBJCPP} and use
7743 extension @file{.m} for test programs.  Use compilation flags:
7744 @code{CPPFLAGS} with @code{OBJCPP}, and both @code{CPPFLAGS} and
7745 @code{OBJCFLAGS} with @code{OBJC}.
7746 @end table
7747 @end defmac
7749 @anchor{AC_LANG_PUSH}
7750 @defmac AC_LANG_PUSH (@var{language})
7751 @acindex{LANG_PUSH}
7752 Remember the current language (as set by @code{AC_LANG}) on a stack, and
7753 then select the @var{language}.  Use this macro and @code{AC_LANG_POP}
7754 in macros that need to temporarily switch to a particular language.
7755 @end defmac
7757 @defmac AC_LANG_POP (@ovar{language})
7758 @acindex{LANG_POP}
7759 Select the language that is saved on the top of the stack, as set by
7760 @code{AC_LANG_PUSH}, and remove it from the stack.
7762 If given, @var{language} specifies the language we just @emph{quit}.  It
7763 is a good idea to specify it when it's known (which should be the
7764 case@dots{}), since Autoconf detects inconsistencies.
7766 @example
7767 AC_LANG_PUSH([Fortran 77])
7768 # Perform some tests on Fortran 77.
7769 # @dots{}
7770 AC_LANG_POP([Fortran 77])
7771 @end example
7772 @end defmac
7774 @defmac AC_LANG_ASSERT (@var{language})
7775 @acindex{LANG_ASSERT} Check statically that the current language is
7776 @var{language}.  You should use this in your language specific macros
7777 to avoid that they be called with an inappropriate language.
7779 This macro runs only at @command{autoconf} time, and incurs no cost at
7780 @command{configure} time.  Sadly enough and because Autoconf is a two
7781 layer language @footnote{Because M4 is not aware of Sh code,
7782 especially conditionals, some optimizations that look nice statically
7783 may produce incorrect results at runtime.}, the macros
7784 @code{AC_LANG_PUSH} and @code{AC_LANG_POP} cannot be ``optimizing'',
7785 therefore as much as possible you ought to avoid using them to wrap
7786 your code, rather, require from the user to run the macro with a
7787 correct current language, and check it with @code{AC_LANG_ASSERT}.
7788 And anyway, that may help the user understand she is running a Fortran
7789 macro while expecting a result about her Fortran 77 compiler@dots{}
7790 @end defmac
7793 @defmac AC_REQUIRE_CPP
7794 @acindex{REQUIRE_CPP}
7795 Ensure that whichever preprocessor would currently be used for tests has
7796 been found.  Calls @code{AC_REQUIRE} (@pxref{Prerequisite Macros}) with an
7797 argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
7798 depending on which language is current.
7799 @end defmac
7802 @node Writing Test Programs
7803 @section Writing Test Programs
7805 Autoconf tests follow a common scheme: feed some program with some
7806 input, and most of the time, feed a compiler with some source file.
7807 This section is dedicated to these source samples.
7809 @menu
7810 * Guidelines::                  General rules for writing test programs
7811 * Test Functions::              Avoiding pitfalls in test programs
7812 * Generating Sources::          Source program boilerplate
7813 @end menu
7815 @node Guidelines
7816 @subsection Guidelines for Test Programs
7818 The most important rule to follow when writing testing samples is:
7820 @center @emph{Look for realism.}
7822 This motto means that testing samples must be written with the same
7823 strictness as real programs are written.  In particular, you should
7824 avoid ``shortcuts'' and simplifications.
7826 Don't just play with the preprocessor if you want to prepare a
7827 compilation.  For instance, using @command{cpp} to check whether a header is
7828 functional might let your @command{configure} accept a header which
7829 causes some @emph{compiler} error.  Do not hesitate to check a header with
7830 other headers included before, especially required headers.
7832 Make sure the symbols you use are properly defined, i.e., refrain for
7833 simply declaring a function yourself instead of including the proper
7834 header.
7836 Test programs should not write to standard output.  They
7837 should exit with status 0 if the test succeeds, and with status 1
7838 otherwise, so that success
7839 can be distinguished easily from a core dump or other failure;
7840 segmentation violations and other failures produce a nonzero exit
7841 status.  Unless you arrange for @code{exit} to be declared, test
7842 programs should @code{return}, not @code{exit}, from @code{main},
7843 because on many systems @code{exit} is not declared by default.
7845 Test programs can use @code{#if} or @code{#ifdef} to check the values of
7846 preprocessor macros defined by tests that have already run.  For
7847 example, if you call @code{AC_HEADER_STDBOOL}, then later on in
7848 @file{configure.ac} you can have a test program that includes
7849 @file{stdbool.h} conditionally:
7851 @example
7852 @group
7853 #ifdef HAVE_STDBOOL_H
7854 # include <stdbool.h>
7855 #endif
7856 @end group
7857 @end example
7859 Both @code{#if HAVE_STDBOOL_H} and @code{#ifdef HAVE_STDBOOL_H} will
7860 work with any standard C compiler.  Some developers prefer @code{#if}
7861 because it is easier to read, while others prefer @code{#ifdef} because
7862 it avoids diagnostics with picky compilers like @acronym{GCC} with the
7863 @option{-Wundef} option.
7865 If a test program needs to use or create a data file, give it a name
7866 that starts with @file{conftest}, such as @file{conftest.data}.  The
7867 @command{configure} script cleans up by running @samp{rm -f -r conftest*}
7868 after running test programs and if the script is interrupted.
7870 @node Test Functions
7871 @subsection Test Functions
7873 These days it's safe to assume support for function prototypes
7874 (introduced in C89).
7876 Functions that test programs declare should also be conditionalized for
7877 C++, which requires @samp{extern "C"} prototypes.  Make sure to not
7878 include any header files containing clashing prototypes.
7880 @example
7881 #ifdef __cplusplus
7882 extern "C"
7883 #endif
7884 void *valloc (size_t);
7885 @end example
7887 If a test program calls a function with invalid parameters (just to see
7888 whether it exists), organize the program to ensure that it never invokes
7889 that function.  You can do this by calling it in another function that is
7890 never invoked.  You can't do it by putting it after a call to
7891 @code{exit}, because @acronym{GCC} version 2 knows that @code{exit}
7892 never returns
7893 and optimizes out any code that follows it in the same block.
7895 If you include any header files, be sure to call the functions
7896 relevant to them with the correct number of arguments, even if they are
7897 just 0, to avoid compilation errors due to prototypes.  @acronym{GCC}
7898 version 2
7899 has internal prototypes for several functions that it automatically
7900 inlines; for example, @code{memcpy}.  To avoid errors when checking for
7901 them, either pass them the correct number of arguments or redeclare them
7902 with a different return type (such as @code{char}).
7905 @node Generating Sources
7906 @subsection Generating Sources
7908 Autoconf provides a set of macros that can be used to generate test
7909 source files.  They are written to be language generic, i.e., they
7910 actually depend on the current language (@pxref{Language Choice}) to
7911 ``format'' the output properly.
7914 @defmac AC_LANG_CONFTEST (@var{source})
7915 @acindex{LANG_CONFTEST}
7916 Save the @var{source} text in the current test source file:
7917 @file{conftest.@var{extension}} where the @var{extension} depends on the
7918 current language.
7920 Note that the @var{source} is evaluated exactly once, like regular
7921 Autoconf macro arguments, and therefore (i) you may pass a macro
7922 invocation, (ii) if not, be sure to double quote if needed.
7923 @end defmac
7925 @defmac AC_LANG_SOURCE (@var{source})
7926 @acindex{LANG_SOURCE}
7927 Expands into the @var{source}, with the definition of
7928 all the @code{AC_DEFINE} performed so far.
7929 @end defmac
7931 For instance executing (observe the double quotation!):
7933 @example
7934 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7935 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7936   [Greetings string.])
7937 AC_LANG(C)
7938 AC_LANG_CONFTEST(
7939    [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
7940 gcc -E -dD -o - conftest.c
7941 @end example
7943 @noindent
7944 results in:
7946 @example
7947 @dots{}
7948 # 1 "conftest.c"
7950 #define PACKAGE_NAME "Hello"
7951 #define PACKAGE_TARNAME "hello"
7952 #define PACKAGE_VERSION "1.0"
7953 #define PACKAGE_STRING "Hello 1.0"
7954 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7955 #define HELLO_WORLD "Hello, World\n"
7957 const char hw[] = "Hello, World\n";
7958 @end example
7960 When the test language is Fortran or Erlang, the @code{AC_DEFINE} definitions
7961 are not automatically translated into constants in the source code by this
7962 macro.
7964 @defmac AC_LANG_PROGRAM (@var{prologue}, @var{body})
7965 @acindex{LANG_PROGRAM}
7966 Expands into a source file which consists of the @var{prologue}, and
7967 then @var{body} as body of the main function (e.g., @code{main} in
7968 C).  Since it uses @code{AC_LANG_SOURCE}, the features of the latter are
7969 available.
7970 @end defmac
7972 For instance:
7974 @example
7975 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7976 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7977   [Greetings string.])
7978 AC_LANG_CONFTEST(
7979 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
7980                  [[fputs (hw, stdout);]])])
7981 gcc -E -dD -o - conftest.c
7982 @end example
7984 @noindent
7985 results in:
7987 @example
7988 @dots{}
7989 # 1 "conftest.c"
7991 #define PACKAGE_NAME "Hello"
7992 #define PACKAGE_TARNAME "hello"
7993 #define PACKAGE_VERSION "1.0"
7994 #define PACKAGE_STRING "Hello 1.0"
7995 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7996 #define HELLO_WORLD "Hello, World\n"
7998 const char hw[] = "Hello, World\n";
8000 main ()
8002 fputs (hw, stdout);
8003   ;
8004   return 0;
8006 @end example
8008 In Erlang tests, the created source file is that of an Erlang module called
8009 @code{conftest} (@file{conftest.erl}).  This module defines and exports
8010 at least
8011 one @code{start/0} function, which is called to perform the test.  The
8012 @var{prologue} is optional code that is inserted between the module header and
8013 the @code{start/0} function definition.  @var{body} is the body of the
8014 @code{start/0} function without the final period (@pxref{Runtime}, about
8015 constraints on this function's behavior).
8017 For instance:
8019 @example
8020 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8021 AC_LANG(Erlang)
8022 AC_LANG_CONFTEST(
8023 [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
8024                  [[io:format("~s~n", [?HELLO_WORLD])]])])
8025 cat conftest.erl
8026 @end example
8028 @noindent
8029 results in:
8031 @example
8032 -module(conftest).
8033 -export([start/0]).
8034 -define(HELLO_WORLD, "Hello, world!").
8035 start() ->
8036 io:format("~s~n", [?HELLO_WORLD])
8038 @end example
8040 @defmac AC_LANG_CALL (@var{prologue}, @var{function})
8041 @acindex{LANG_CALL}
8042 Expands into a source file which consists of the @var{prologue}, and
8043 then a call to the @var{function} as body of the main function (e.g.,
8044 @code{main} in C).  Since it uses @code{AC_LANG_PROGRAM}, the feature
8045 of the latter are available.
8047 This function will probably be replaced in the future by a version
8048 which would enable specifying the arguments.  The use of this macro is
8049 not encouraged, as it violates strongly the typing system.
8051 This macro cannot be used for Erlang tests.
8052 @end defmac
8054 @defmac AC_LANG_FUNC_LINK_TRY (@var{function})
8055 @acindex{LANG_FUNC_LINK_TRY}
8056 Expands into a source file which uses the @var{function} in the body of
8057 the main function (e.g., @code{main} in C).  Since it uses
8058 @code{AC_LANG_PROGRAM}, the features of the latter are available.
8060 As @code{AC_LANG_CALL}, this macro is documented only for completeness.
8061 It is considered to be severely broken, and in the future will be
8062 removed in favor of actual function calls (with properly typed
8063 arguments).
8065 This macro cannot be used for Erlang tests.
8066 @end defmac
8068 @node Running the Preprocessor
8069 @section Running the Preprocessor
8071 Sometimes one might need to run the preprocessor on some source file.
8072 @emph{Usually it is a bad idea}, as you typically need to @emph{compile}
8073 your project, not merely run the preprocessor on it; therefore you
8074 certainly want to run the compiler, not the preprocessor.  Resist the
8075 temptation of following the easiest path.
8077 Nevertheless, if you need to run the preprocessor, then use
8078 @code{AC_PREPROC_IFELSE}.
8080 The macros described in this section cannot be used for tests in Erlang or
8081 Fortran, since those languages require no preprocessor.
8083 @anchor{AC_PREPROC_IFELSE}
8084 @defmac AC_PREPROC_IFELSE (@var{input}, @ovar{action-if-true}, @
8085   @ovar{action-if-false})
8086 @acindex{PREPROC_IFELSE}
8087 Run the preprocessor of the current language (@pxref{Language Choice})
8088 on the @var{input}, run the shell commands @var{action-if-true} on
8089 success, @var{action-if-false} otherwise.  The @var{input} can be made
8090 by @code{AC_LANG_PROGRAM} and friends.
8092 This macro uses @code{CPPFLAGS}, but not @code{CFLAGS}, because
8093 @option{-g}, @option{-O}, etc.@: are not valid options to many C
8094 preprocessors.
8096 It is customary to report unexpected failures with
8097 @code{AC_MSG_FAILURE}.
8098 @end defmac
8100 For instance:
8102 @example
8103 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8104 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
8105   [Greetings string.])
8106 AC_PREPROC_IFELSE(
8107    [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
8108                     [[fputs (hw, stdout);]])],
8109    [AC_MSG_RESULT([OK])],
8110    [AC_MSG_FAILURE([unexpected preprocessor failure])])
8111 @end example
8113 @noindent
8114 results in:
8116 @example
8117 checking for gcc... gcc
8118 checking for C compiler default output file name... a.out
8119 checking whether the C compiler works... yes
8120 checking whether we are cross compiling... no
8121 checking for suffix of executables...
8122 checking for suffix of object files... o
8123 checking whether we are using the GNU C compiler... yes
8124 checking whether gcc accepts -g... yes
8125 checking for gcc option to accept ISO C89... none needed
8126 checking how to run the C preprocessor... gcc -E
8128 @end example
8130 @sp 1
8132 The macro @code{AC_TRY_CPP} (@pxref{Obsolete Macros}) used to play the
8133 role of @code{AC_PREPROC_IFELSE}, but double quotes its argument, making
8134 it impossible to use it to elaborate sources.  You are encouraged to
8135 get rid of your old use of the macro @code{AC_TRY_CPP} in favor of
8136 @code{AC_PREPROC_IFELSE}, but, in the first place, are you sure you need
8137 to run the @emph{preprocessor} and not the compiler?
8139 @anchor{AC_EGREP_HEADER}
8140 @defmac AC_EGREP_HEADER (@var{pattern}, @var{header-file}, @
8141   @var{action-if-found}, @ovar{action-if-not-found})
8142 @acindex{EGREP_HEADER}
8143 If the output of running the preprocessor on the system header file
8144 @var{header-file} matches the extended regular expression
8145 @var{pattern}, execute shell commands @var{action-if-found}, otherwise
8146 execute @var{action-if-not-found}.
8147 @end defmac
8149 @anchor{AC_EGREP_CPP}
8150 @defmac AC_EGREP_CPP (@var{pattern}, @var{program}, @
8151   @ovar{action-if-found}, @ovar{action-if-not-found})
8152 @acindex{EGREP_CPP}
8153 @var{program} is the text of a C or C++ program, on which shell
8154 variable, back quote, and backslash substitutions are performed.  If the
8155 output of running the preprocessor on @var{program} matches the
8156 extended regular expression @var{pattern}, execute shell commands
8157 @var{action-if-found}, otherwise execute @var{action-if-not-found}.
8158 @end defmac
8162 @node Running the Compiler
8163 @section Running the Compiler
8165 To check for a syntax feature of the current language's (@pxref{Language
8166 Choice}) compiler, such as whether it recognizes a certain keyword, or
8167 simply to try some library feature, use @code{AC_COMPILE_IFELSE} to try
8168 to compile a small program that uses that feature.
8170 @defmac AC_COMPILE_IFELSE (@var{input}, @ovar{action-if-true}, @
8171   @ovar{action-if-false})
8172 @acindex{COMPILE_IFELSE}
8173 Run the compiler and compilation flags of the current language
8174 (@pxref{Language Choice}) on the @var{input}, run the shell commands
8175 @var{action-if-true} on success, @var{action-if-false} otherwise.  The
8176 @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
8178 It is customary to report unexpected failures with
8179 @code{AC_MSG_FAILURE}.  This macro does not try to link; use
8180 @code{AC_LINK_IFELSE} if you need to do that (@pxref{Running the
8181 Linker}).
8182 @end defmac
8184 @ovindex ERL
8185 For tests in Erlang, the @var{input} must be the source code of a module named
8186 @code{conftest}.  @code{AC_COMPILE_IFELSE} generates a @file{conftest.beam}
8187 file that can be interpreted by the Erlang virtual machine (@code{ERL}).  It is
8188 recommended to use @code{AC_LANG_PROGRAM} to specify the test program,
8189 to ensure that the Erlang module has the right name.
8191 @node Running the Linker
8192 @section Running the Linker
8194 To check for a library, a function, or a global variable, Autoconf
8195 @command{configure} scripts try to compile and link a small program that
8196 uses it.  This is unlike Metaconfig, which by default uses @code{nm} or
8197 @code{ar} on the C library to try to figure out which functions are
8198 available.  Trying to link with the function is usually a more reliable
8199 approach because it avoids dealing with the variations in the options
8200 and output formats of @code{nm} and @code{ar} and in the location of the
8201 standard libraries.  It also allows configuring for cross-compilation or
8202 checking a function's runtime behavior if needed.  On the other hand,
8203 it can be slower than scanning the libraries once, but accuracy is more
8204 important than speed.
8206 @code{AC_LINK_IFELSE} is used to compile test programs to test for
8207 functions and global variables.  It is also used by @code{AC_CHECK_LIB}
8208 to check for libraries (@pxref{Libraries}), by adding the library being
8209 checked for to @code{LIBS} temporarily and trying to link a small
8210 program.
8212 @anchor{AC_LINK_IFELSE}
8213 @defmac AC_LINK_IFELSE (@var{input}, @ovar{action-if-true}, @
8214   @ovar{action-if-false})
8215 @acindex{LINK_IFELSE}
8216 Run the compiler (and compilation flags) and the linker of the current
8217 language (@pxref{Language Choice}) on the @var{input}, run the shell
8218 commands @var{action-if-true} on success, @var{action-if-false}
8219 otherwise.  The @var{input} can be made by @code{AC_LANG_PROGRAM} and
8220 friends.
8222 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
8223 current compilation flags.
8225 It is customary to report unexpected failures with
8226 @code{AC_MSG_FAILURE}.  This macro does not try to execute the program;
8227 use @code{AC_RUN_IFELSE} if you need to do that (@pxref{Runtime}).
8228 @end defmac
8230 The @code{AC_LINK_IFELSE} macro cannot be used for Erlang tests, since Erlang
8231 programs are interpreted and do not require linking.
8235 @node Runtime
8236 @section Checking Runtime Behavior
8238 Sometimes you need to find out how a system performs at runtime, such
8239 as whether a given function has a certain capability or bug.  If you
8240 can, make such checks when your program runs instead of when it is
8241 configured.  You can check for things like the machine's endianness when
8242 your program initializes itself.
8244 If you really need to test for a runtime behavior while configuring,
8245 you can write a test program to determine the result, and compile and
8246 run it using @code{AC_RUN_IFELSE}.  Avoid running test programs if
8247 possible, because this prevents people from configuring your package for
8248 cross-compiling.
8250 @anchor{AC_RUN_IFELSE}
8251 @defmac AC_RUN_IFELSE (@var{input}, @ovar{action-if-true}, @
8252   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
8253 @acindex{RUN_IFELSE}
8254 If @var{program} compiles and links successfully and returns an exit
8255 status of 0 when executed, run shell commands @var{action-if-true}.
8256 Otherwise, run shell commands @var{action-if-false}.
8258 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
8259 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
8260 compilation flags of the current language (@pxref{Language Choice}).
8262 If the compiler being used does not produce executables that run on the
8263 system where @command{configure} is being run, then the test program is
8264 not run.  If the optional shell commands @var{action-if-cross-compiling}
8265 are given, they are run instead.  Otherwise, @command{configure} prints
8266 an error message and exits.
8268 In the @var{action-if-false} section, the failing exit status is
8269 available in the shell variable @samp{$?}.  This exit status might be
8270 that of a failed compilation, or it might be that of a failed program
8271 execution.
8273 It is customary to report unexpected failures with
8274 @code{AC_MSG_FAILURE}.
8275 @end defmac
8277 Try to provide a pessimistic default value to use when cross-compiling
8278 makes runtime tests impossible.  You do this by passing the optional
8279 last argument to @code{AC_RUN_IFELSE}.  @command{autoconf} prints a
8280 warning message when creating @command{configure} each time it
8281 encounters a call to @code{AC_RUN_IFELSE} with no
8282 @var{action-if-cross-compiling} argument given.  You may ignore the
8283 warning, though users cannot configure your package for
8284 cross-compiling.  A few of the macros distributed with Autoconf produce
8285 this warning message.
8287 To configure for cross-compiling you can also choose a value for those
8288 parameters based on the canonical system name (@pxref{Manual
8289 Configuration}).  Alternatively, set up a test results cache file with
8290 the correct values for the host system (@pxref{Caching Results}).
8292 @ovindex cross_compiling
8293 To provide a default for calls of @code{AC_RUN_IFELSE} that are embedded
8294 in other macros, including a few of the ones that come with Autoconf,
8295 you can test whether the shell variable @code{cross_compiling} is set to
8296 @samp{yes}, and then use an alternate method to get the results instead
8297 of calling the macros.
8299 A C or C++ runtime test should be portable.
8300 @xref{Portable C and C++}.
8302 Erlang tests must exit themselves the Erlang VM by calling the @code{halt/1}
8303 function: the given status code is used to determine the success of the test
8304 (status is @code{0}) or its failure (status is different than @code{0}), as
8305 explained above.  It must be noted that data output through the standard output
8306 (e.g., using @code{io:format/2}) may be truncated when halting the VM.
8307 Therefore, if a test must output configuration information, it is recommended
8308 to create and to output data into the temporary file named @file{conftest.out},
8309 using the functions of module @code{file}.  The @code{conftest.out} file is
8310 automatically deleted by the @code{AC_RUN_IFELSE} macro.  For instance, a
8311 simplified implementation of Autoconf's @code{AC_ERLANG_SUBST_LIB_DIR}
8312 macro is:
8314 @example
8315 AC_INIT([LibdirTest], [1.0], [bug-libdirtest@@example.org])
8316 AC_ERLANG_NEED_ERL
8317 AC_LANG(Erlang)
8318 AC_RUN_IFELSE(
8319   [AC_LANG_PROGRAM([], [dnl
8320     file:write_file("conftest.out", code:lib_dir()),
8321     halt(0)])],
8322   [echo "code:lib_dir() returned: `cat conftest.out`"],
8323   [AC_MSG_FAILURE([test Erlang program execution failed])])
8324 @end example
8327 @node Systemology
8328 @section Systemology
8329 @cindex Systemology
8331 This section aims at presenting some systems and pointers to
8332 documentation.  It may help you addressing particular problems reported
8333 by users.
8335 @uref{http://www.opengroup.org/susv3, Posix-conforming systems} are
8336 derived from the @uref{http://www.bell-labs.com/history/unix/, Unix
8337 operating system}.
8339 The @uref{http://bhami.com/rosetta.html, Rosetta Stone for Unix}
8340 contains a table correlating the features of various Posix-conforming
8341 systems.  @uref{http://www.levenez.com/unix/, Unix History} is a
8342 simplified diagram of how many Unix systems were derived from each
8343 other.
8345 @uref{http://heirloom.sourceforge.net/, The Heirloom Project}
8346 provides some variants of traditional implementations of Unix utilities.
8348 @table @asis
8349 @item Darwin
8350 @cindex Darwin
8351 Darwin is also known as Mac OS X@.  Beware that the file system @emph{can} be
8352 case-preserving, but case insensitive.  This can cause nasty problems,
8353 since for instance the installation attempt for a package having an
8354 @file{INSTALL} file can result in @samp{make install} report that
8355 nothing was to be done!
8357 That's all dependent on whether the file system is a UFS (case
8358 sensitive) or HFS+ (case preserving).  By default Apple wants you to
8359 install the OS on HFS+.  Unfortunately, there are some pieces of
8360 software which really need to be built on UFS@.  We may want to rebuild
8361 Darwin to have both UFS and HFS+ available (and put the /local/build
8362 tree on the UFS).
8364 @item @acronym{QNX} 4.25
8365 @cindex @acronym{QNX} 4.25
8366 @c FIXME: Please, if you feel like writing something more precise,
8367 @c it'd be great.  In particular, I can't understand the difference with
8368 @c QNX Neutrino.
8369 @acronym{QNX} is a realtime operating system running on Intel architecture
8370 meant to be scalable from the small embedded systems to the hundred
8371 processor super-computer.  It claims to be Posix certified.  More
8372 information is available on the
8373 @uref{http://www.qnx.com/, @acronym{QNX} home page}.
8375 @item Tru64
8376 @cindex Tru64
8377 @uref{http://h30097.www3.hp.com/@/docs/,
8378 Documentation of several versions of Tru64} is available in different
8379 formats.
8381 @item Unix version 7
8382 @cindex Unix version 7
8383 @cindex V7
8384 Officially this was called the ``Seventh Edition'' of ``the @sc{unix}
8385 time-sharing system'' but we use the more-common name ``Unix version 7''.
8386 Documentation is available in the
8387 @uref{http://plan9.bell-labs.com/@/7thEdMan/, Unix Seventh Edition Manual}.
8388 Previous versions of Unix are called ``Unix version 6'', etc., but
8389 they were not as widely used.
8390 @end table
8393 @node Multiple Cases
8394 @section Multiple Cases
8396 Some operations are accomplished in several possible ways, depending on
8397 the OS variant.  Checking for them essentially requires a ``case
8398 statement''.  Autoconf does not directly provide one; however, it is
8399 easy to simulate by using a shell variable to keep track of whether a
8400 way to perform the operation has been found yet.
8402 Here is an example that uses the shell variable @code{fstype} to keep
8403 track of whether the remaining cases need to be checked.
8405 @example
8406 @group
8407 AC_MSG_CHECKING([how to get file system type])
8408 fstype=no
8409 # The order of these tests is important.
8410 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statvfs.h>
8411 #include <sys/fstyp.h>]])],
8412                   [AC_DEFINE([FSTYPE_STATVFS], [1],
8413                      [Define if statvfs exists.])
8414                    fstype=SVR4])
8415 if test $fstype = no; then
8416   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8417 #include <sys/fstyp.h>]])],
8418                   [AC_DEFINE([FSTYPE_USG_STATFS], [1],
8419                      [Define if USG statfs.])
8420                    fstype=SVR3])
8422 if test $fstype = no; then
8423   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8424 #include <sys/vmount.h>]])]),
8425                   [AC_DEFINE([FSTYPE_AIX_STATFS], [1],
8426                      [Define if AIX statfs.])
8427                    fstype=AIX])
8429 # (more cases omitted here)
8430 AC_MSG_RESULT([$fstype])
8431 @end group
8432 @end example
8434 @c ====================================================== Results of Tests.
8436 @node Results
8437 @chapter Results of Tests
8439 Once @command{configure} has determined whether a feature exists, what can
8440 it do to record that information?  There are four sorts of things it can
8441 do: define a C preprocessor symbol, set a variable in the output files,
8442 save the result in a cache file for future @command{configure} runs, and
8443 print a message letting the user know the result of the test.
8445 @menu
8446 * Defining Symbols::            Defining C preprocessor symbols
8447 * Setting Output Variables::    Replacing variables in output files
8448 * Special Chars in Variables::  Characters to beware of in variables
8449 * Caching Results::             Speeding up subsequent @command{configure} runs
8450 * Printing Messages::           Notifying @command{configure} users
8451 @end menu
8453 @node Defining Symbols
8454 @section Defining C Preprocessor Symbols
8456 A common action to take in response to a feature test is to define a C
8457 preprocessor symbol indicating the results of the test.  That is done by
8458 calling @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}.
8460 By default, @code{AC_OUTPUT} places the symbols defined by these macros
8461 into the output variable @code{DEFS}, which contains an option
8462 @option{-D@var{symbol}=@var{value}} for each symbol defined.  Unlike in
8463 Autoconf version 1, there is no variable @code{DEFS} defined while
8464 @command{configure} is running.  To check whether Autoconf macros have
8465 already defined a certain C preprocessor symbol, test the value of the
8466 appropriate cache variable, as in this example:
8468 @example
8469 AC_CHECK_FUNC([vprintf], [AC_DEFINE([HAVE_VPRINTF], [1],
8470                           [Define if vprintf exists.])])
8471 if test "$ac_cv_func_vprintf" != yes; then
8472   AC_CHECK_FUNC([_doprnt], [AC_DEFINE([HAVE_DOPRNT], [1],
8473                             [Define if _doprnt exists.])])
8475 @end example
8477 If @code{AC_CONFIG_HEADERS} has been called, then instead of creating
8478 @code{DEFS}, @code{AC_OUTPUT} creates a header file by substituting the
8479 correct values into @code{#define} statements in a template file.
8480 @xref{Configuration Headers}, for more information about this kind of
8481 output.
8483 @defmac AC_DEFINE (@var{variable}, @var{value}, @ovar{description})
8484 @defmacx AC_DEFINE (@var{variable})
8485 @cvindex @var{variable}
8486 @acindex{DEFINE}
8487 Define @var{variable} to @var{value} (verbatim), by defining a C
8488 preprocessor macro for @var{variable}.  @var{variable} should be a C
8489 identifier, optionally suffixed by a parenthesized argument list to
8490 define a C preprocessor macro with arguments.  The macro argument list,
8491 if present, should be a comma-separated list of C identifiers, possibly
8492 terminated by an ellipsis @samp{...} if C99 syntax is employed.
8493 @var{variable} should not contain comments, white space, trigraphs,
8494 backslash-newlines, universal character names, or non-@acronym{ASCII}
8495 characters.
8497 @var{value} may contain backslash-escaped newlines, which will be
8498 preserved if you use @code{AC_CONFIG_HEADERS} but flattened if passed
8499 via @code{@@DEFS@@} (with no effect on the compilation, since the
8500 preprocessor sees only one line in the first place).  @var{value} should
8501 not contain raw newlines.  If you are not using
8502 @code{AC_CONFIG_HEADERS}, @var{value} should not contain any @samp{#}
8503 characters, as @command{make} tends to eat them.  To use a shell
8504 variable, use @code{AC_DEFINE_UNQUOTED} instead.
8506 @var{description} is only useful if you are using
8507 @code{AC_CONFIG_HEADERS}.  In this case, @var{description} is put into
8508 the generated @file{config.h.in} as the comment before the macro define.
8509 The following example defines the C preprocessor variable
8510 @code{EQUATION} to be the string constant @samp{"$a > $b"}:
8512 @example
8513 AC_DEFINE([EQUATION], ["$a > $b"],
8514   [Equation string.])
8515 @end example
8517 If neither @var{value} nor @var{description} are given, then
8518 @var{value} defaults to 1 instead of to the empty string.  This is for
8519 backwards compatibility with older versions of Autoconf, but this usage
8520 is obsolescent and may be withdrawn in future versions of Autoconf.
8522 If the @var{variable} is a literal string, it is passed to
8523 @code{m4_pattern_allow} (@pxref{Forbidden Patterns}).
8525 If multiple @code{AC_DEFINE} statements are executed for the same
8526 @var{variable} name (not counting any parenthesized argument list),
8527 the last one wins.
8528 @end defmac
8530 @defmac AC_DEFINE_UNQUOTED (@var{variable}, @var{value}, @ovar{description})
8531 @defmacx AC_DEFINE_UNQUOTED (@var{variable})
8532 @acindex{DEFINE_UNQUOTED}
8533 @cvindex @var{variable}
8534 Like @code{AC_DEFINE}, but three shell expansions are
8535 performed---once---on @var{variable} and @var{value}: variable expansion
8536 (@samp{$}), command substitution (@samp{`}), and backslash escaping
8537 (@samp{\}).  Single and double quote characters in the value have no
8538 special meaning.  Use this macro instead of @code{AC_DEFINE} when
8539 @var{variable} or @var{value} is a shell variable.  Examples:
8541 @example
8542 AC_DEFINE_UNQUOTED([config_machfile], ["$machfile"],
8543   [Configuration machine file.])
8544 AC_DEFINE_UNQUOTED([GETGROUPS_T], [$ac_cv_type_getgroups],
8545   [getgroups return type.])
8546 AC_DEFINE_UNQUOTED([$ac_tr_hdr], [1],
8547   [Translated header name.])
8548 @end example
8549 @end defmac
8551 Due to a syntactical bizarreness of the Bourne shell, do not use
8552 semicolons to separate @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}
8553 calls from other macro calls or shell code; that can cause syntax errors
8554 in the resulting @command{configure} script.  Use either blanks or
8555 newlines.  That is, do this:
8557 @example
8558 AC_CHECK_HEADER([elf.h],
8559   [AC_DEFINE([SVR4], [1], [System V Release 4]) LIBS="-lelf $LIBS"])
8560 @end example
8562 @noindent
8563 or this:
8565 @example
8566 AC_CHECK_HEADER([elf.h],
8567   [AC_DEFINE([SVR4], [1], [System V Release 4])
8568    LIBS="-lelf $LIBS"])
8569 @end example
8571 @noindent
8572 instead of this:
8574 @example
8575 AC_CHECK_HEADER([elf.h],
8576   [AC_DEFINE([SVR4], [1], [System V Release 4]); LIBS="-lelf $LIBS"])
8577 @end example
8579 @node Setting Output Variables
8580 @section Setting Output Variables
8581 @cindex Output variables
8583 Another way to record the results of tests is to set @dfn{output
8584 variables}, which are shell variables whose values are substituted into
8585 files that @command{configure} outputs.  The two macros below create new
8586 output variables.  @xref{Preset Output Variables}, for a list of output
8587 variables that are always available.
8589 @defmac AC_SUBST (@var{variable}, @ovar{value})
8590 @acindex{SUBST}
8591 Create an output variable from a shell variable.  Make @code{AC_OUTPUT}
8592 substitute the variable @var{variable} into output files (typically one
8593 or more makefiles).  This means that @code{AC_OUTPUT}
8594 replaces instances of @samp{@@@var{variable}@@} in input files with the
8595 value that the shell variable @var{variable} has when @code{AC_OUTPUT}
8596 is called.  The value can contain any non-@code{NUL} character, including
8597 newline.
8598 Variable occurrences should not overlap: e.g., an input file should
8599 not contain @samp{@@@var{var1}@@@var{var2}@@} if @var{var1} and @var{var2}
8600 are variable names.
8601 The substituted value is not rescanned for more output variables;
8602 occurrences of @samp{@@@var{variable}@@} in the value are inserted
8603 literally into the output file.  (The algorithm uses the special marker
8604 @code{|#_!!_#|} internally, so neither the substituted value nor the
8605 output file may contain @code{|#_!!_#|}.)
8607 If @var{value} is given, in addition assign it to @var{variable}.
8609 The string @var{variable} is passed to @code{m4_pattern_allow}
8610 (@pxref{Forbidden Patterns}).
8611 @end defmac
8613 @defmac AC_SUBST_FILE (@var{variable})
8614 @acindex{SUBST_FILE}
8615 Another way to create an output variable from a shell variable.  Make
8616 @code{AC_OUTPUT} insert (without substitutions) the contents of the file
8617 named by shell variable @var{variable} into output files.  This means
8618 that @code{AC_OUTPUT} replaces instances of
8619 @samp{@@@var{variable}@@} in output files (such as @file{Makefile.in})
8620 with the contents of the file that the shell variable @var{variable}
8621 names when @code{AC_OUTPUT} is called.  Set the variable to
8622 @file{/dev/null} for cases that do not have a file to insert.
8623 This substitution occurs only when the @samp{@@@var{variable}@@} is on a
8624 line by itself, optionally surrounded by spaces and tabs.  The
8625 substitution replaces the whole line, including the spaces, tabs, and
8626 the terminating newline.
8628 This macro is useful for inserting makefile fragments containing
8629 special dependencies or other @code{make} directives for particular host
8630 or target types into makefiles.  For example, @file{configure.ac}
8631 could contain:
8633 @example
8634 AC_SUBST_FILE([host_frag])
8635 host_frag=$srcdir/conf/sun4.mh
8636 @end example
8638 @noindent
8639 and then a @file{Makefile.in} could contain:
8641 @example
8642 @@host_frag@@
8643 @end example
8645 The string @var{variable} is passed to @code{m4_pattern_allow}
8646 (@pxref{Forbidden Patterns}).
8647 @end defmac
8649 @cindex Precious Variable
8650 @cindex Variable, Precious
8651 Running @command{configure} in varying environments can be extremely
8652 dangerous.  If for instance the user runs @samp{CC=bizarre-cc
8653 ./configure}, then the cache, @file{config.h}, and many other output
8654 files depend upon @command{bizarre-cc} being the C compiler.  If
8655 for some reason the user runs @command{./configure} again, or if it is
8656 run via @samp{./config.status --recheck}, (@xref{Automatic Remaking},
8657 and @pxref{config.status Invocation}), then the configuration can be
8658 inconsistent, composed of results depending upon two different
8659 compilers.
8661 Environment variables that affect this situation, such as @samp{CC}
8662 above, are called @dfn{precious variables}, and can be declared as such
8663 by @code{AC_ARG_VAR}.
8665 @defmac AC_ARG_VAR (@var{variable}, @var{description})
8666 @acindex{ARG_VAR}
8667 Declare @var{variable} is a precious variable, and include its
8668 @var{description} in the variable section of @samp{./configure --help}.
8670 Being precious means that
8671 @itemize @minus
8672 @item
8673 @var{variable} is substituted via @code{AC_SUBST}.
8675 @item
8676 The value of @var{variable} when @command{configure} was launched is
8677 saved in the cache, including if it was not specified on the command
8678 line but via the environment.  Indeed, while @command{configure} can
8679 notice the definition of @code{CC} in @samp{./configure CC=bizarre-cc},
8680 it is impossible to notice it in @samp{CC=bizarre-cc ./configure},
8681 which, unfortunately, is what most users do.
8683 We emphasize that it is the @emph{initial} value of @var{variable} which
8684 is saved, not that found during the execution of @command{configure}.
8685 Indeed, specifying @samp{./configure FOO=foo} and letting
8686 @samp{./configure} guess that @code{FOO} is @code{foo} can be two
8687 different things.
8689 @item
8690 @var{variable} is checked for consistency between two
8691 @command{configure} runs.  For instance:
8693 @example
8694 $ @kbd{./configure --silent --config-cache}
8695 $ @kbd{CC=cc ./configure --silent --config-cache}
8696 configure: error: `CC' was not set in the previous run
8697 configure: error: changes in the environment can compromise \
8698 the build
8699 configure: error: run `make distclean' and/or \
8700 `rm config.cache' and start over
8701 @end example
8703 @noindent
8704 and similarly if the variable is unset, or if its content is changed.
8707 @item
8708 @var{variable} is kept during automatic reconfiguration
8709 (@pxref{config.status Invocation}) as if it had been passed as a command
8710 line argument, including when no cache is used:
8712 @example
8713 $ @kbd{CC=/usr/bin/cc ./configure var=raboof --silent}
8714 $ @kbd{./config.status --recheck}
8715 running CONFIG_SHELL=/bin/sh /bin/sh ./configure var=raboof \
8716   CC=/usr/bin/cc  --no-create --no-recursion
8717 @end example
8718 @end itemize
8719 @end defmac
8721 @node Special Chars in Variables
8722 @section Special Characters in Output Variables
8723 @cindex Output variables, special characters in
8725 Many output variables are intended to be evaluated both by
8726 @command{make} and by the shell.  Some characters are expanded
8727 differently in these two contexts, so to avoid confusion these
8728 variables' values should not contain any of the following characters:
8730 @example
8731 " # $ & ' ( ) * ; < > ? [ \ ^ ` |
8732 @end example
8734 Also, these variables' values should neither contain newlines, nor start
8735 with @samp{~}, nor contain white space or @samp{:} immediately followed
8736 by @samp{~}.  The values can contain nonempty sequences of white space
8737 characters like tabs and spaces, but each such sequence might
8738 arbitrarily be replaced by a single space during substitution.
8740 These restrictions apply both to the values that @command{configure}
8741 computes, and to the values set directly by the user.  For example, the
8742 following invocations of @command{configure} are problematic, since they
8743 attempt to use special characters within @code{CPPFLAGS} and white space
8744 within @code{$(srcdir)}:
8746 @example
8747 CPPFLAGS='-DOUCH="&\"#$*?"' '../My Source/ouch-1.0/configure'
8749 '../My Source/ouch-1.0/configure' CPPFLAGS='-DOUCH="&\"#$*?"'
8750 @end example
8752 @node Caching Results
8753 @section Caching Results
8754 @cindex Cache
8756 To avoid checking for the same features repeatedly in various
8757 @command{configure} scripts (or in repeated runs of one script),
8758 @command{configure} can optionally save the results of many checks in a
8759 @dfn{cache file} (@pxref{Cache Files}).  If a @command{configure} script
8760 runs with caching enabled and finds a cache file, it reads the results
8761 of previous runs from the cache and avoids rerunning those checks.  As a
8762 result, @command{configure} can then run much faster than if it had to
8763 perform all of the checks every time.
8765 @defmac AC_CACHE_VAL (@var{cache-id}, @var{commands-to-set-it})
8766 @acindex{CACHE_VAL}
8767 Ensure that the results of the check identified by @var{cache-id} are
8768 available.  If the results of the check were in the cache file that was
8769 read, and @command{configure} was not given the @option{--quiet} or
8770 @option{--silent} option, print a message saying that the result was
8771 cached; otherwise, run the shell commands @var{commands-to-set-it}.  If
8772 the shell commands are run to determine the value, the value is
8773 saved in the cache file just before @command{configure} creates its output
8774 files.  @xref{Cache Variable Names}, for how to choose the name of the
8775 @var{cache-id} variable.
8777 The @var{commands-to-set-it} @emph{must have no side effects} except for
8778 setting the variable @var{cache-id}, see below.
8779 @end defmac
8781 @defmac AC_CACHE_CHECK (@var{message}, @var{cache-id}, @
8782   @var{commands-to-set-it})
8783 @acindex{CACHE_CHECK}
8784 A wrapper for @code{AC_CACHE_VAL} that takes care of printing the
8785 messages.  This macro provides a convenient shorthand for the most
8786 common way to use these macros.  It calls @code{AC_MSG_CHECKING} for
8787 @var{message}, then @code{AC_CACHE_VAL} with the @var{cache-id} and
8788 @var{commands} arguments, and @code{AC_MSG_RESULT} with @var{cache-id}.
8790 The @var{commands-to-set-it} @emph{must have no side effects} except for
8791 setting the variable @var{cache-id}, see below.
8792 @end defmac
8794 It is common to find buggy macros using @code{AC_CACHE_VAL} or
8795 @code{AC_CACHE_CHECK}, because people are tempted to call
8796 @code{AC_DEFINE} in the @var{commands-to-set-it}.  Instead, the code that
8797 @emph{follows} the call to @code{AC_CACHE_VAL} should call
8798 @code{AC_DEFINE}, by examining the value of the cache variable.  For
8799 instance, the following macro is broken:
8801 @example
8802 @group
8803 AC_DEFUN([AC_SHELL_TRUE],
8804 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
8805                 [my_cv_shell_true_works=no
8806                  (true) 2>/dev/null && my_cv_shell_true_works=yes
8807                  if test "$my_cv_shell_true_works" = yes; then
8808                    AC_DEFINE([TRUE_WORKS], [1],
8809                              [Define if `true(1)' works properly.])
8810                  fi])
8812 @end group
8813 @end example
8815 @noindent
8816 This fails if the cache is enabled: the second time this macro is run,
8817 @code{TRUE_WORKS} @emph{will not be defined}.  The proper implementation
8820 @example
8821 @group
8822 AC_DEFUN([AC_SHELL_TRUE],
8823 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
8824                 [my_cv_shell_true_works=no
8825                  (true) 2>/dev/null && my_cv_shell_true_works=yes])
8826  if test "$my_cv_shell_true_works" = yes; then
8827    AC_DEFINE([TRUE_WORKS], [1],
8828              [Define if `true(1)' works properly.])
8829  fi
8831 @end group
8832 @end example
8834 Also, @var{commands-to-set-it} should not print any messages, for
8835 example with @code{AC_MSG_CHECKING}; do that before calling
8836 @code{AC_CACHE_VAL}, so the messages are printed regardless of whether
8837 the results of the check are retrieved from the cache or determined by
8838 running the shell commands.
8840 @menu
8841 * Cache Variable Names::        Shell variables used in caches
8842 * Cache Files::                 Files @command{configure} uses for caching
8843 * Cache Checkpointing::         Loading and saving the cache file
8844 @end menu
8846 @node Cache Variable Names
8847 @subsection Cache Variable Names
8848 @cindex Cache variable
8850 The names of cache variables should have the following format:
8852 @example
8853 @var{package-prefix}_cv_@var{value-type}_@var{specific-value}_@ovar{additional-options}
8854 @end example
8856 @noindent
8857 for example, @samp{ac_cv_header_stat_broken} or
8858 @samp{ac_cv_prog_gcc_traditional}.  The parts of the variable name are:
8860 @table @asis
8861 @item @var{package-prefix}
8862 An abbreviation for your package or organization; the same prefix you
8863 begin local Autoconf macros with, except lowercase by convention.
8864 For cache values used by the distributed Autoconf macros, this value is
8865 @samp{ac}.
8867 @item @code{_cv_}
8868 Indicates that this shell variable is a cache value.  This string
8869 @emph{must} be present in the variable name, including the leading
8870 underscore.
8872 @item @var{value-type}
8873 A convention for classifying cache values, to produce a rational naming
8874 system.  The values used in Autoconf are listed in @ref{Macro Names}.
8876 @item @var{specific-value}
8877 Which member of the class of cache values this test applies to.
8878 For example, which function (@samp{alloca}), program (@samp{gcc}), or
8879 output variable (@samp{INSTALL}).
8881 @item @var{additional-options}
8882 Any particular behavior of the specific member that this test applies to.
8883 For example, @samp{broken} or @samp{set}.  This part of the name may
8884 be omitted if it does not apply.
8885 @end table
8887 The values assigned to cache variables may not contain newlines.
8888 Usually, their values are Boolean (@samp{yes} or @samp{no}) or the
8889 names of files or functions; so this is not an important restriction.
8891 @node Cache Files
8892 @subsection Cache Files
8894 A cache file is a shell script that caches the results of configure
8895 tests run on one system so they can be shared between configure scripts
8896 and configure runs.  It is not useful on other systems.  If its contents
8897 are invalid for some reason, the user may delete or edit it.
8899 By default, @command{configure} uses no cache file,
8900 to avoid problems caused by accidental
8901 use of stale cache files.
8903 To enable caching, @command{configure} accepts @option{--config-cache} (or
8904 @option{-C}) to cache results in the file @file{config.cache}.
8905 Alternatively, @option{--cache-file=@var{file}} specifies that
8906 @var{file} be the cache file.  The cache file is created if it does not
8907 exist already.  When @command{configure} calls @command{configure} scripts in
8908 subdirectories, it uses the @option{--cache-file} argument so that they
8909 share the same cache.  @xref{Subdirectories}, for information on
8910 configuring subdirectories with the @code{AC_CONFIG_SUBDIRS} macro.
8912 @file{config.status} only pays attention to the cache file if it is
8913 given the @option{--recheck} option, which makes it rerun
8914 @command{configure}.
8916 It is wrong to try to distribute cache files for particular system types.
8917 There is too much room for error in doing that, and too much
8918 administrative overhead in maintaining them.  For any features that
8919 can't be guessed automatically, use the standard method of the canonical
8920 system type and linking files (@pxref{Manual Configuration}).
8922 The site initialization script can specify a site-wide cache file to
8923 use, instead of the usual per-program cache.  In this case, the cache
8924 file gradually accumulates information whenever someone runs a new
8925 @command{configure} script.  (Running @command{configure} merges the new cache
8926 results with the existing cache file.)  This may cause problems,
8927 however, if the system configuration (e.g., the installed libraries or
8928 compilers) changes and the stale cache file is not deleted.
8930 @node Cache Checkpointing
8931 @subsection Cache Checkpointing
8933 If your configure script, or a macro called from @file{configure.ac}, happens
8934 to abort the configure process, it may be useful to checkpoint the cache
8935 a few times at key points using @code{AC_CACHE_SAVE}.  Doing so
8936 reduces the amount of time it takes to rerun the configure script with
8937 (hopefully) the error that caused the previous abort corrected.
8939 @c FIXME: Do we really want to document this guy?
8940 @defmac AC_CACHE_LOAD
8941 @acindex{CACHE_LOAD}
8942 Loads values from existing cache file, or creates a new cache file if a
8943 cache file is not found.  Called automatically from @code{AC_INIT}.
8944 @end defmac
8946 @defmac AC_CACHE_SAVE
8947 @acindex{CACHE_SAVE}
8948 Flushes all cached values to the cache file.  Called automatically from
8949 @code{AC_OUTPUT}, but it can be quite useful to call
8950 @code{AC_CACHE_SAVE} at key points in @file{configure.ac}.
8951 @end defmac
8953 For instance:
8955 @example
8956 @r{ @dots{} AC_INIT, etc. @dots{}}
8957 @group
8958 # Checks for programs.
8959 AC_PROG_CC
8960 AC_PROG_AWK
8961 @r{ @dots{} more program checks @dots{}}
8962 AC_CACHE_SAVE
8963 @end group
8965 @group
8966 # Checks for libraries.
8967 AC_CHECK_LIB([nsl], [gethostbyname])
8968 AC_CHECK_LIB([socket], [connect])
8969 @r{ @dots{} more lib checks @dots{}}
8970 AC_CACHE_SAVE
8971 @end group
8973 @group
8974 # Might abort@dots{}
8975 AM_PATH_GTK([1.0.2], [], [AC_MSG_ERROR([GTK not in path])])
8976 AM_PATH_GTKMM([0.9.5], [], [AC_MSG_ERROR([GTK not in path])])
8977 @end group
8978 @r{ @dots{} AC_OUTPUT, etc. @dots{}}
8979 @end example
8981 @node Printing Messages
8982 @section Printing Messages
8983 @cindex Messages, from @command{configure}
8985 @command{configure} scripts need to give users running them several kinds
8986 of information.  The following macros print messages in ways appropriate
8987 for each kind.  The arguments to all of them get enclosed in shell
8988 double quotes, so the shell performs variable and back-quote
8989 substitution on them.
8991 These macros are all wrappers around the @command{echo} shell command.
8992 They direct output to the appropriate file descriptor (@pxref{File
8993 Descriptor Macros}).
8994 @command{configure} scripts should rarely need to run @command{echo} directly
8995 to print messages for the user.  Using these macros makes it easy to
8996 change how and when each kind of message is printed; such changes need
8997 only be made to the macro definitions and all the callers change
8998 automatically.
9000 To diagnose static issues, i.e., when @command{autoconf} is run, see
9001 @ref{Reporting Messages}.
9003 @defmac AC_MSG_CHECKING (@var{feature-description})
9004 @acindex{MSG_CHECKING}
9005 Notify the user that @command{configure} is checking for a particular
9006 feature.  This macro prints a message that starts with @samp{checking }
9007 and ends with @samp{...} and no newline.  It must be followed by a call
9008 to @code{AC_MSG_RESULT} to print the result of the check and the
9009 newline.  The @var{feature-description} should be something like
9010 @samp{whether the Fortran compiler accepts C++ comments} or @samp{for
9011 c89}.
9013 This macro prints nothing if @command{configure} is run with the
9014 @option{--quiet} or @option{--silent} option.
9015 @end defmac
9017 @anchor{AC_MSG_RESULT}
9018 @defmac AC_MSG_RESULT (@var{result-description})
9019 @acindex{MSG_RESULT}
9020 Notify the user of the results of a check.  @var{result-description} is
9021 almost always the value of the cache variable for the check, typically
9022 @samp{yes}, @samp{no}, or a file name.  This macro should follow a call
9023 to @code{AC_MSG_CHECKING}, and the @var{result-description} should be
9024 the completion of the message printed by the call to
9025 @code{AC_MSG_CHECKING}.
9027 This macro prints nothing if @command{configure} is run with the
9028 @option{--quiet} or @option{--silent} option.
9029 @end defmac
9031 @anchor{AC_MSG_NOTICE}
9032 @defmac AC_MSG_NOTICE (@var{message})
9033 @acindex{MSG_NOTICE}
9034 Deliver the @var{message} to the user.  It is useful mainly to print a
9035 general description of the overall purpose of a group of feature checks,
9036 e.g.,
9038 @example
9039 AC_MSG_NOTICE([checking if stack overflow is detectable])
9040 @end example
9042 This macro prints nothing if @command{configure} is run with the
9043 @option{--quiet} or @option{--silent} option.
9044 @end defmac
9046 @anchor{AC_MSG_ERROR}
9047 @defmac AC_MSG_ERROR (@var{error-description}, @ovar{exit-status})
9048 @acindex{MSG_ERROR}
9049 Notify the user of an error that prevents @command{configure} from
9050 completing.  This macro prints an error message to the standard error
9051 output and exits @command{configure} with @var{exit-status} (1 by default).
9052 @var{error-description} should be something like @samp{invalid value
9053 $HOME for \$HOME}.
9055 The @var{error-description} should start with a lower-case letter, and
9056 ``cannot'' is preferred to ``can't''.
9057 @end defmac
9059 @defmac AC_MSG_FAILURE (@var{error-description}, @ovar{exit-status})
9060 @acindex{MSG_FAILURE}
9061 This @code{AC_MSG_ERROR} wrapper notifies the user of an error that
9062 prevents @command{configure} from completing @emph{and} that additional
9063 details are provided in @file{config.log}.  This is typically used when
9064 abnormal results are found during a compilation.
9065 @end defmac
9067 @anchor{AC_MSG_WARN}
9068 @defmac AC_MSG_WARN (@var{problem-description})
9069 @acindex{MSG_WARN}
9070 Notify the @command{configure} user of a possible problem.  This macro
9071 prints the message to the standard error output; @command{configure}
9072 continues running afterward, so macros that call @code{AC_MSG_WARN} should
9073 provide a default (back-up) behavior for the situations they warn about.
9074 @var{problem-description} should be something like @samp{ln -s seems to
9075 make hard links}.
9076 @end defmac
9080 @c ====================================================== Programming in M4.
9082 @node Programming in M4
9083 @chapter Programming in M4
9084 @cindex M4
9086 Autoconf is written on top of two layers: @dfn{M4sugar}, which provides
9087 convenient macros for pure M4 programming, and @dfn{M4sh}, which
9088 provides macros dedicated to shell script generation.
9090 As of this version of Autoconf, these two layers are still experimental,
9091 and their interface might change in the future.  As a matter of fact,
9092 @emph{anything that is not documented must not be used}.
9094 @menu
9095 * M4 Quotation::                Protecting macros from unwanted expansion
9096 * Using autom4te::              The Autoconf executables backbone
9097 * Programming in M4sugar::      Convenient pure M4 macros
9098 * Programming in M4sh::         Common shell Constructs
9099 * File Descriptor Macros::      File descriptor macros for input and output
9100 @end menu
9102 @node M4 Quotation
9103 @section M4 Quotation
9104 @cindex M4 quotation
9105 @cindex quotation
9107 The most common problem with existing macros is an improper quotation.
9108 This section, which users of Autoconf can skip, but which macro writers
9109 @emph{must} read, first justifies the quotation scheme that was chosen
9110 for Autoconf and then ends with a rule of thumb.  Understanding the
9111 former helps one to follow the latter.
9113 @menu
9114 * Active Characters::           Characters that change the behavior of M4
9115 * One Macro Call::              Quotation and one macro call
9116 * Quoting and Parameters::      M4 vs. shell parameters
9117 * Quotation and Nested Macros::  Macros calling macros
9118 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
9119 * Quadrigraphs::                Another way to escape special characters
9120 * Quotation Rule Of Thumb::     One parenthesis, one quote
9121 @end menu
9123 @node Active Characters
9124 @subsection Active Characters
9126 To fully understand where proper quotation is important, you first need
9127 to know what the special characters are in Autoconf: @samp{#} introduces
9128 a comment inside which no macro expansion is performed, @samp{,}
9129 separates arguments, @samp{[} and @samp{]} are the quotes themselves,
9130 @samp{(} and @samp{)} (which M4 tries to match by pairs), and finally
9131 @samp{$} inside a macro definition.
9133 In order to understand the delicate case of macro calls, we first have
9134 to present some obvious failures.  Below they are ``obvious-ified'',
9135 but when you find them in real life, they are usually in disguise.
9137 Comments, introduced by a hash and running up to the newline, are opaque
9138 tokens to the top level: active characters are turned off, and there is
9139 no macro expansion:
9141 @example
9142 # define([def], ine)
9143 @result{}# define([def], ine)
9144 @end example
9146 Each time there can be a macro expansion, there is a quotation
9147 expansion, i.e., one level of quotes is stripped:
9149 @example
9150 int tab[10];
9151 @result{}int tab10;
9152 [int tab[10];]
9153 @result{}int tab[10];
9154 @end example
9156 Without this in mind, the reader might try hopelessly to use her macro
9157 @code{array}:
9159 @example
9160 define([array], [int tab[10];])
9161 array
9162 @result{}int tab10;
9163 [array]
9164 @result{}array
9165 @end example
9167 @noindent
9168 How can you correctly output the intended results@footnote{Using
9169 @code{defn}.}?
9172 @node One Macro Call
9173 @subsection One Macro Call
9175 Let's proceed on the interaction between active characters and macros
9176 with this small macro, which just returns its first argument:
9178 @example
9179 define([car], [$1])
9180 @end example
9182 @noindent
9183 The two pairs of quotes above are not part of the arguments of
9184 @code{define}; rather, they are understood by the top level when it
9185 tries to find the arguments of @code{define}.  Therefore, assuming
9186 @code{car} is not already defined, it is equivalent to write:
9188 @example
9189 define(car, $1)
9190 @end example
9192 @noindent
9193 But, while it is acceptable for a @file{configure.ac} to avoid unnecessary
9194 quotes, it is bad practice for Autoconf macros which must both be more
9195 robust and also advocate perfect style.
9197 At the top level, there are only two possibilities: either you
9198 quote or you don't:
9200 @example
9201 car(foo, bar, baz)
9202 @result{}foo
9203 [car(foo, bar, baz)]
9204 @result{}car(foo, bar, baz)
9205 @end example
9207 Let's pay attention to the special characters:
9209 @example
9210 car(#)
9211 @error{}EOF in argument list
9212 @end example
9214 The closing parenthesis is hidden in the comment; with a hypothetical
9215 quoting, the top level understood it this way:
9217 @example
9218 car([#)]
9219 @end example
9221 @noindent
9222 Proper quotation, of course, fixes the problem:
9224 @example
9225 car([#])
9226 @result{}#
9227 @end example
9229 Here are more examples:
9231 @example
9232 car(foo, bar)
9233 @result{}foo
9234 car([foo, bar])
9235 @result{}foo, bar
9236 car((foo, bar))
9237 @result{}(foo, bar)
9238 car([(foo], [bar)])
9239 @result{}(foo
9240 define([a], [b])
9241 @result{}
9242 car(a)
9243 @result{}b
9244 car([a])
9245 @result{}b
9246 car([[a]])
9247 @result{}a
9248 car([[[a]]])
9249 @result{}[a]
9250 @end example
9252 @node Quoting and Parameters
9253 @subsection
9255 When M4 encounters @samp{$} within a macro definition, followed
9256 immediately by a character it recognizes (@samp{0}@dots{}@samp{9},
9257 @samp{#}, @samp{@@}, or @samp{*}), it will perform M4 parameter
9258 expansion.  This happens regardless of how many layers of quotes the
9259 parameter expansion is nested within, or even if it occurs in text that
9260 will be rescanned as a comment.
9262 @example
9263 define([none], [$1])
9264 @result{}
9265 define([one], [[$1]])
9266 @result{}
9267 define([two], [[[$1]]])
9268 @result{}
9269 define([comment], [# $1])
9270 @result{}
9271 define([active], [ACTIVE])
9272 @result{}
9273 none([active])
9274 @result{}ACTIVE
9275 one([active])
9276 @result{}active
9277 two([active])
9278 @result{}[active]
9279 comment([active])
9280 @result{}# active
9281 @end example
9283 On the other hand, since autoconf generates shell code, you often want
9284 to output shell variable expansion, rather than performing M4 parameter
9285 expansion.  To do this, you must use M4 quoting to separate the @samp{$}
9286 from the next character in the definition of your macro.  If the macro
9287 definition occurs in single-quoted text, then insert another level of
9288 quoting; if the usage is already inside a double-quoted string, then
9289 split it into concatenated strings.
9291 @example
9292 define([single], [a single-quoted $[]1 definition])
9293 @result{}
9294 define([double], [[a double-quoted $][1 definition]])
9295 @result{}
9296 single
9297 @result{}a single-quoted $1 definition
9298 double
9299 @result{}a double-quoted $1 definition
9300 @end example
9302 Posix states that M4 implementations are free to provide implementation
9303 extensions when @samp{$@{} is encountered in a macro definition.
9304 Autoconf reserves the longer sequence @samp{$@{@{} for use with planned
9305 extensions that will be available in the future @acronym{GNU} M4 2.0,
9306 but guarantees that all other instances of @samp{$@{} will be output
9307 literally.  Therefore, this idiom can also be used to output shell code
9308 parameter references:
9310 @example
9311 define([first], [$@{1@}])first
9312 @result{}$@{1@}
9313 @end example
9315 Posix also states that @samp{$11} should expand to the first parameter
9316 concatenated with a literal @samp{1}, although some versions of
9317 @acronym{GNU} M4 expand the eleventh parameter instead.  For
9318 portability, you should only use single-digit M4 parameter expansion.
9320 With this in mind, we can explore the cases where macros invoke
9321 macros@enddots{}
9323 @node Quotation and Nested Macros
9324 @subsection Quotation and Nested Macros
9326 The examples below use the following macros:
9328 @example
9329 define([car], [$1])
9330 define([active], [ACT, IVE])
9331 define([array], [int tab[10]])
9332 @end example
9334 Each additional embedded macro call introduces other possible
9335 interesting quotations:
9337 @example
9338 car(active)
9339 @result{}ACT
9340 car([active])
9341 @result{}ACT, IVE
9342 car([[active]])
9343 @result{}active
9344 @end example
9346 In the first case, the top level looks for the arguments of @code{car},
9347 and finds @samp{active}.  Because M4 evaluates its arguments
9348 before applying the macro, @samp{active} is expanded, which results in:
9350 @example
9351 car(ACT, IVE)
9352 @result{}ACT
9353 @end example
9355 @noindent
9356 In the second case, the top level gives @samp{active} as first and only
9357 argument of @code{car}, which results in:
9359 @example
9360 active
9361 @result{}ACT, IVE
9362 @end example
9364 @noindent
9365 i.e., the argument is evaluated @emph{after} the macro that invokes it.
9366 In the third case, @code{car} receives @samp{[active]}, which results in:
9368 @example
9369 [active]
9370 @result{}active
9371 @end example
9373 @noindent
9374 exactly as we already saw above.
9376 The example above, applied to a more realistic example, gives:
9378 @example
9379 car(int tab[10];)
9380 @result{}int tab10;
9381 car([int tab[10];])
9382 @result{}int tab10;
9383 car([[int tab[10];]])
9384 @result{}int tab[10];
9385 @end example
9387 @noindent
9388 Huh?  The first case is easily understood, but why is the second wrong,
9389 and the third right?  To understand that, you must know that after
9390 M4 expands a macro, the resulting text is immediately subjected
9391 to macro expansion and quote removal.  This means that the quote removal
9392 occurs twice---first before the argument is passed to the @code{car}
9393 macro, and second after the @code{car} macro expands to the first
9394 argument.
9396 As the author of the Autoconf macro @code{car}, you then consider it to
9397 be incorrect that your users have to double-quote the arguments of
9398 @code{car}, so you ``fix'' your macro.  Let's call it @code{qar} for
9399 quoted car:
9401 @example
9402 define([qar], [[$1]])
9403 @end example
9405 @noindent
9406 and check that @code{qar} is properly fixed:
9408 @example
9409 qar([int tab[10];])
9410 @result{}int tab[10];
9411 @end example
9413 @noindent
9414 Ahhh!  That's much better.
9416 But note what you've done: now that the result of @code{qar} is always
9417 a literal string, the only time a user can use nested macros is if she
9418 relies on an @emph{unquoted} macro call:
9420 @example
9421 qar(active)
9422 @result{}ACT
9423 qar([active])
9424 @result{}active
9425 @end example
9427 @noindent
9428 leaving no way for her to reproduce what she used to do with @code{car}:
9430 @example
9431 car([active])
9432 @result{}ACT, IVE
9433 @end example
9435 @noindent
9436 Worse yet: she wants to use a macro that produces a set of @code{cpp}
9437 macros:
9439 @example
9440 define([my_includes], [#include <stdio.h>])
9441 car([my_includes])
9442 @result{}#include <stdio.h>
9443 qar(my_includes)
9444 @error{}EOF in argument list
9445 @end example
9447 This macro, @code{qar}, because it double quotes its arguments, forces
9448 its users to leave their macro calls unquoted, which is dangerous.
9449 Commas and other active symbols are interpreted by M4 before
9450 they are given to the macro, often not in the way the users expect.
9451 Also, because @code{qar} behaves differently from the other macros,
9452 it's an exception that should be avoided in Autoconf.
9454 @node Changequote is Evil
9455 @subsection @code{changequote} is Evil
9456 @cindex @code{changequote}
9458 The temptation is often high to bypass proper quotation, in particular
9459 when it's late at night.  Then, many experienced Autoconf hackers
9460 finally surrender to the dark side of the force and use the ultimate
9461 weapon: @code{changequote}.
9463 The M4 builtin @code{changequote} belongs to a set of primitives that
9464 allow one to adjust the syntax of the language to adjust it to one's
9465 needs.  For instance, by default M4 uses @samp{`} and @samp{'} as
9466 quotes, but in the context of shell programming (and actually of most
9467 programming languages), that's about the worst choice one can make:
9468 because of strings and back-quoted expressions in shell code (such as
9469 @samp{'this'} and @samp{`that`}), and because of literal characters in usual
9470 programming languages (as in @samp{'0'}), there are many unbalanced
9471 @samp{`} and @samp{'}.  Proper M4 quotation then becomes a nightmare, if
9472 not impossible.  In order to make M4 useful in such a context, its
9473 designers have equipped it with @code{changequote}, which makes it
9474 possible to choose another pair of quotes.  M4sugar, M4sh, Autoconf, and
9475 Autotest all have chosen to use @samp{[} and @samp{]}.  Not especially
9476 because they are unlikely characters, but @emph{because they are
9477 characters unlikely to be unbalanced}.
9479 There are other magic primitives, such as @code{changecom} to specify
9480 what syntactic forms are comments (it is common to see
9481 @samp{changecom(<!--, -->)} when M4 is used to produce HTML pages),
9482 @code{changeword} and @code{changesyntax} to change other syntactic
9483 details (such as the character to denote the @var{n}th argument, @samp{$} by
9484 default, the parenthesis around arguments, etc.).
9486 These primitives are really meant to make M4 more useful for specific
9487 domains: they should be considered like command line options:
9488 @option{--quotes}, @option{--comments}, @option{--words}, and
9489 @option{--syntax}.  Nevertheless, they are implemented as M4 builtins, as
9490 it makes M4 libraries self contained (no need for additional options).
9492 There lies the problem@enddots{}
9494 @sp 1
9496 The problem is that it is then tempting to use them in the middle of an
9497 M4 script, as opposed to its initialization.  This, if not carefully
9498 thought out, can lead to disastrous effects: @emph{you are changing the
9499 language in the middle of the execution}.  Changing and restoring the
9500 syntax is often not enough: if you happened to invoke macros in between,
9501 these macros are lost, as the current syntax is probably not
9502 the one they were implemented with.
9504 @c FIXME: I've been looking for a short, real case example, but I
9505 @c lost them all :(
9508 @node Quadrigraphs
9509 @subsection Quadrigraphs
9510 @cindex quadrigraphs
9511 @cindex @samp{@@S|@@}
9512 @cindex @samp{@@&t@@}
9513 @c Info cannot handle `:' in index entries.
9514 @c @cindex @samp{@@<:@@}
9515 @c @cindex @samp{@@:>@@}
9516 @c @cindex @samp{@@%:@@}
9518 When writing an Autoconf macro you may occasionally need to generate
9519 special characters that are difficult to express with the standard
9520 Autoconf quoting rules.  For example, you may need to output the regular
9521 expression @samp{[^[]}, which matches any character other than @samp{[}.
9522 This expression contains unbalanced brackets so it cannot be put easily
9523 into an M4 macro.
9525 You can work around this problem by using one of the following
9526 @dfn{quadrigraphs}:
9528 @table @samp
9529 @item @@<:@@
9530 @samp{[}
9531 @item @@:>@@
9532 @samp{]}
9533 @item @@S|@@
9534 @samp{$}
9535 @item @@%:@@
9536 @samp{#}
9537 @item @@&t@@
9538 Expands to nothing.
9539 @end table
9541 Quadrigraphs are replaced at a late stage of the translation process,
9542 after @command{m4} is run, so they do not get in the way of M4 quoting.
9543 For example, the string @samp{^@@<:@@}, independently of its quotation,
9544 appears as @samp{^[} in the output.
9546 The empty quadrigraph can be used:
9548 @itemize @minus
9549 @item to mark trailing spaces explicitly
9551 Trailing spaces are smashed by @command{autom4te}.  This is a feature.
9553 @item to produce other quadrigraphs
9555 For instance @samp{@@<@@&t@@:@@} produces @samp{@@<:@@}.
9557 @item to escape @emph{occurrences} of forbidden patterns
9559 For instance you might want to mention @code{AC_FOO} in a comment, while
9560 still being sure that @command{autom4te} still catches unexpanded
9561 @samp{AC_*}.  Then write @samp{AC@@&t@@_FOO}.
9562 @end itemize
9564 The name @samp{@@&t@@} was suggested by Paul Eggert:
9566 @quotation
9567 I should give some credit to the @samp{@@&t@@} pun.  The @samp{&} is my
9568 own invention, but the @samp{t} came from the source code of the
9569 @sc{algol68c} compiler, written by Steve Bourne (of Bourne shell fame),
9570 and which used @samp{mt} to denote the empty string.  In C, it would
9571 have looked like something like:
9573 @example
9574 char const mt[] = "";
9575 @end example
9577 @noindent
9578 but of course the source code was written in Algol 68.
9580 I don't know where he got @samp{mt} from: it could have been his own
9581 invention, and I suppose it could have been a common pun around the
9582 Cambridge University computer lab at the time.
9583 @end quotation
9585 @node Quotation Rule Of Thumb
9586 @subsection Quotation Rule Of Thumb
9588 To conclude, the quotation rule of thumb is:
9590 @center @emph{One pair of quotes per pair of parentheses.}
9592 Never over-quote, never under-quote, in particular in the definition of
9593 macros.  In the few places where the macros need to use brackets
9594 (usually in C program text or regular expressions), properly quote
9595 @emph{the arguments}!
9597 It is common to read Autoconf programs with snippets like:
9599 @example
9600 AC_TRY_LINK(
9601 changequote(<<, >>)dnl
9602 <<#include <time.h>
9603 #ifndef tzname /* For SGI.  */
9604 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9605 #endif>>,
9606 changequote([, ])dnl
9607 [atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
9608 @end example
9610 @noindent
9611 which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
9612 double quoting, so you just need:
9614 @example
9615 AC_TRY_LINK(
9616 [#include <time.h>
9617 #ifndef tzname /* For SGI.  */
9618 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9619 #endif],
9620             [atoi (*tzname);],
9621             [ac_cv_var_tzname=yes],
9622             [ac_cv_var_tzname=no])
9623 @end example
9625 @noindent
9626 The M4-fluent reader might note that these two examples are rigorously
9627 equivalent, since M4 swallows both the @samp{changequote(<<, >>)}
9628 and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
9629 quotes are not part of the arguments!
9631 Simplified, the example above is just doing this:
9633 @example
9634 changequote(<<, >>)dnl
9635 <<[]>>
9636 changequote([, ])dnl
9637 @end example
9639 @noindent
9640 instead of simply:
9642 @example
9643 [[]]
9644 @end example
9646 With macros that do not double quote their arguments (which is the
9647 rule), double-quote the (risky) literals:
9649 @example
9650 AC_LINK_IFELSE([AC_LANG_PROGRAM(
9651 [[#include <time.h>
9652 #ifndef tzname /* For SGI.  */
9653 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9654 #endif]],
9655                                 [atoi (*tzname);])],
9656                [ac_cv_var_tzname=yes],
9657                [ac_cv_var_tzname=no])
9658 @end example
9660 Please note that the macro @code{AC_TRY_LINK} is obsolete, so you really
9661 should be using @code{AC_LINK_IFELSE} instead.
9663 @xref{Quadrigraphs}, for what to do if you run into a hopeless case
9664 where quoting does not suffice.
9666 When you create a @command{configure} script using newly written macros,
9667 examine it carefully to check whether you need to add more quotes in
9668 your macros.  If one or more words have disappeared in the M4
9669 output, you need more quotes.  When in doubt, quote.
9671 However, it's also possible to put on too many layers of quotes.  If
9672 this happens, the resulting @command{configure} script may contain
9673 unexpanded macros.  The @command{autoconf} program checks for this problem
9674 by looking for the string @samp{AC_} in @file{configure}.  However, this
9675 heuristic does not work in general: for example, it does not catch
9676 overquoting in @code{AC_DEFINE} descriptions.
9679 @c ---------------------------------------- Using autom4te
9681 @node Using autom4te
9682 @section Using @command{autom4te}
9684 The Autoconf suite, including M4sugar, M4sh, and Autotest, in addition
9685 to Autoconf per se, heavily rely on M4.  All these different uses
9686 revealed common needs factored into a layer over M4:
9687 @command{autom4te}@footnote{
9689 Yet another great name from Lars J. Aas.
9693 @command{autom4te} is a preprocessor that is like @command{m4}.
9694 It supports M4 extensions designed for use in tools like Autoconf.
9696 @menu
9697 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
9698 * Customizing autom4te::        Customizing the Autoconf package
9699 @end menu
9701 @node autom4te Invocation
9702 @subsection Invoking @command{autom4te}
9704 The command line arguments are modeled after M4's:
9706 @example
9707 autom4te @var{options} @var{files}
9708 @end example
9710 @noindent
9711 @evindex M4
9712 where the @var{files} are directly passed to @command{m4}.  By default,
9713 @acronym{GNU} M4 is found during configuration, but the environment
9714 variable
9715 @env{M4} can be set to tell @command{autom4te} where to look.  In addition
9716 to the regular expansion, it handles the replacement of the quadrigraphs
9717 (@pxref{Quadrigraphs}), and of @samp{__oline__}, the current line in the
9718 output.  It supports an extended syntax for the @var{files}:
9720 @table @file
9721 @item @var{file}.m4f
9722 This file is an M4 frozen file.  Note that @emph{all the previous files
9723 are ignored}.  See the option @option{--melt} for the rationale.
9725 @item @var{file}?
9726 If found in the library path, the @var{file} is included for expansion,
9727 otherwise it is ignored instead of triggering a failure.
9728 @end table
9730 @sp 1
9732 Of course, it supports the Autoconf common subset of options:
9734 @table @option
9735 @item --help
9736 @itemx -h
9737 Print a summary of the command line options and exit.
9739 @item --version
9740 @itemx -V
9741 Print the version number of Autoconf and exit.
9743 @item --verbose
9744 @itemx -v
9745 Report processing steps.
9747 @item --debug
9748 @itemx -d
9749 Don't remove the temporary files and be even more verbose.
9751 @item --include=@var{dir}
9752 @itemx -I @var{dir}
9753 Also look for input files in @var{dir}.  Multiple invocations
9754 accumulate.
9756 @item --output=@var{file}
9757 @itemx -o @var{file}
9758 Save output (script or trace) to @var{file}.  The file @option{-} stands
9759 for the standard output.
9760 @end table
9762 @sp 1
9764 As an extension of @command{m4}, it includes the following options:
9766 @table @option
9767 @item --warnings=@var{category}
9768 @itemx -W @var{category}
9769 @evindex WARNINGS
9770 @c FIXME: Point to the M4sugar macros, not Autoconf's.
9771 Report the warnings related to @var{category} (which can actually be a
9772 comma separated list).  @xref{Reporting Messages}, macro
9773 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
9774 values include:
9776 @table @samp
9777 @item all
9778 report all the warnings
9780 @item none
9781 report none
9783 @item error
9784 treats warnings as errors
9786 @item no-@var{category}
9787 disable warnings falling into @var{category}
9788 @end table
9790 Warnings about @samp{syntax} are enabled by default, and the environment
9791 variable @env{WARNINGS}, a comma separated list of categories, is
9792 honored.  @samp{autom4te -W @var{category}} actually
9793 behaves as if you had run:
9795 @example
9796 autom4te --warnings=syntax,$WARNINGS,@var{category}
9797 @end example
9799 @noindent
9800 For example, if you want to disable defaults and @env{WARNINGS}
9801 of @command{autom4te}, but enable the warnings about obsolete
9802 constructs, you would use @option{-W none,obsolete}.
9804 @cindex Back trace
9805 @cindex Macro invocation stack
9806 @command{autom4te} displays a back trace for errors, but not for
9807 warnings; if you want them, just pass @option{-W error}.
9809 @item --melt
9810 @itemx -M
9811 Do not use frozen files.  Any argument @code{@var{file}.m4f} is
9812 replaced by @code{@var{file}.m4}.  This helps tracing the macros which
9813 are executed only when the files are frozen, typically
9814 @code{m4_define}.  For instance, running:
9816 @example
9817 autom4te --melt 1.m4 2.m4f 3.m4 4.m4f input.m4
9818 @end example
9820 @noindent
9821 is roughly equivalent to running:
9823 @example
9824 m4 1.m4 2.m4 3.m4 4.m4 input.m4
9825 @end example
9827 @noindent
9828 while
9830 @example
9831 autom4te 1.m4 2.m4f 3.m4 4.m4f input.m4
9832 @end example
9834 @noindent
9835 is equivalent to:
9837 @example
9838 m4 --reload-state=4.m4f input.m4
9839 @end example
9841 @item --freeze
9842 @itemx -f
9843 Produce a frozen state file.  @command{autom4te} freezing is stricter
9844 than M4's: it must produce no warnings, and no output other than empty
9845 lines (a line with white space is @emph{not} empty) and comments
9846 (starting with @samp{#}).  Unlike @command{m4}'s similarly-named option,
9847 this option takes no argument:
9849 @example
9850 autom4te 1.m4 2.m4 3.m4 --freeze --output=3.m4f
9851 @end example
9853 @noindent
9854 corresponds to
9856 @example
9857 m4 1.m4 2.m4 3.m4 --freeze-state=3.m4f
9858 @end example
9860 @item --mode=@var{octal-mode}
9861 @itemx -m @var{octal-mode}
9862 Set the mode of the non-traces output to @var{octal-mode}; by default
9863 @samp{0666}.
9864 @end table
9866 @sp 1
9868 @cindex @file{autom4te.cache}
9869 As another additional feature over @command{m4}, @command{autom4te}
9870 caches its results.  @acronym{GNU} M4 is able to produce a regular
9871 output and traces at the same time.  Traces are heavily used in the
9872 @acronym{GNU} Build System: @command{autoheader} uses them to build
9873 @file{config.h.in}, @command{autoreconf} to determine what
9874 @acronym{GNU} Build System components are used, @command{automake} to
9875 ``parse'' @file{configure.ac} etc.  To avoid recomputation,
9876 traces are cached while performing regular expansion,
9877 and conversely.  This cache is (actually, the caches are) stored in
9878 the directory @file{autom4te.cache}.  @emph{It can safely be removed}
9879 at any moment (especially if for some reason @command{autom4te}
9880 considers it is trashed).
9882 @table @option
9883 @item --cache=@var{directory}
9884 @itemx -C @var{directory}
9885 Specify the name of the directory where the result should be cached.
9886 Passing an empty value disables caching.  Be sure to pass a relative
9887 file name, as for the time being, global caches are not supported.
9889 @item --no-cache
9890 Don't cache the results.
9892 @item --force
9893 @itemx -f
9894 If a cache is used, consider it obsolete (but update it anyway).
9895 @end table
9897 @sp 1
9899 Because traces are so important to the @acronym{GNU} Build System,
9900 @command{autom4te} provides high level tracing features as compared to
9901 M4, and helps exploiting the cache:
9903 @table @option
9904 @item --trace=@var{macro}[:@var{format}]
9905 @itemx -t @var{macro}[:@var{format}]
9906 Trace the invocations of @var{macro} according to the @var{format}.
9907 Multiple @option{--trace} arguments can be used to list several macros.
9908 Multiple @option{--trace} arguments for a single macro are not
9909 cumulative; instead, you should just make @var{format} as long as
9910 needed.
9912 The @var{format} is a regular string, with newlines if desired, and
9913 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}.  It can
9914 use the following special escapes:
9916 @table @samp
9917 @item $$
9918 The character @samp{$}.
9920 @item $f
9921 The file name from which @var{macro} is called.
9923 @item $l
9924 The line number from which @var{macro} is called.
9926 @item $d
9927 The depth of the @var{macro} call.  This is an M4 technical detail that
9928 you probably don't want to know about.
9930 @item $n
9931 The name of the @var{macro}.
9933 @item $@var{num}
9934 The @var{num}th argument of the call to @var{macro}.
9936 @item $@@
9937 @itemx $@var{sep}@@
9938 @itemx $@{@var{separator}@}@@
9939 All the arguments passed to @var{macro}, separated by the character
9940 @var{sep} or the string @var{separator} (@samp{,} by default).  Each
9941 argument is quoted, i.e., enclosed in a pair of square brackets.
9943 @item $*
9944 @itemx $@var{sep}*
9945 @itemx $@{@var{separator}@}*
9946 As above, but the arguments are not quoted.
9948 @item $%
9949 @itemx $@var{sep}%
9950 @itemx $@{@var{separator}@}%
9951 As above, but the arguments are not quoted, all new line characters in
9952 the arguments are smashed, and the default separator is @samp{:}.
9954 The escape @samp{$%} produces single-line trace outputs (unless you put
9955 newlines in the @samp{separator}), while @samp{$@@} and @samp{$*} do
9956 not.
9957 @end table
9959 @xref{autoconf Invocation}, for examples of trace uses.
9961 @item --preselect=@var{macro}
9962 @itemx -p @var{macro}
9963 Cache the traces of @var{macro}, but do not enable traces.  This is
9964 especially important to save CPU cycles in the future.  For instance,
9965 when invoked, @command{autoconf} preselects all the macros that
9966 @command{autoheader}, @command{automake}, @command{autoreconf}, etc.,
9967 trace, so that running @command{m4} is not needed to trace them: the
9968 cache suffices.  This results in a huge speed-up.
9969 @end table
9971 @sp 1
9973 @cindex Autom4te Library
9974 Finally, @command{autom4te} introduces the concept of @dfn{Autom4te
9975 libraries}.  They consists in a powerful yet extremely simple feature:
9976 sets of combined command line arguments:
9978 @table @option
9979 @item --language=@var{language}
9980 @itemx -l @var{language}
9981 Use the @var{language} Autom4te library.  Current languages include:
9983 @table @code
9984 @item M4sugar
9985 create M4sugar output.
9987 @item M4sh
9988 create M4sh executable shell scripts.
9990 @item Autotest
9991 create Autotest executable test suites.
9993 @item Autoconf-without-aclocal-m4
9994 create Autoconf executable configure scripts without
9995 reading @file{aclocal.m4}.
9997 @item Autoconf
9998 create Autoconf executable configure scripts.  This language inherits
9999 all the characteristics of @code{Autoconf-without-aclocal-m4} and
10000 additionally reads @file{aclocal.m4}.
10001 @end table
10003 @item --prepend-include=@var{dir}
10004 @item -B @var{dir}
10005 Prepend directory @var{dir} to the search path.  This is used to include
10006 the language-specific files before any third-party macros.
10008 @end table
10010 @cindex @file{autom4te.cfg}
10011 As an example, if Autoconf is installed in its default location,
10012 @file{/usr/local}, the command @samp{autom4te -l m4sugar foo.m4} is
10013 strictly equivalent to the command:
10015 @example
10016 autom4te --prepend-include /usr/local/share/autoconf \
10017   m4sugar/m4sugar.m4f --warnings syntax foo.m4
10018 @end example
10020 @noindent
10021 Recursive expansion applies here: the command @samp{autom4te -l m4sh foo.m4}
10022 is the same as @samp{autom4te --language M4sugar m4sugar/m4sh.m4f
10023 foo.m4}, i.e.:
10025 @example
10026 autom4te --prepend-include /usr/local/share/autoconf \
10027   m4sugar/m4sugar.m4f m4sugar/m4sh.m4f --mode 777 foo.m4
10028 @end example
10030 @noindent
10031 The definition of the languages is stored in @file{autom4te.cfg}.
10033 @node Customizing autom4te
10034 @subsection Customizing @command{autom4te}
10036 One can customize @command{autom4te} via @file{~/.autom4te.cfg} (i.e.,
10037 as found in the user home directory), and @file{./.autom4te.cfg} (i.e.,
10038 as found in the directory from which @command{autom4te} is run).  The
10039 order is first reading @file{autom4te.cfg}, then @file{~/.autom4te.cfg},
10040 then @file{./.autom4te.cfg}, and finally the command line arguments.
10042 In these text files, comments are introduced with @code{#}, and empty
10043 lines are ignored.  Customization is performed on a per-language basis,
10044 wrapped in between a @samp{begin-language: "@var{language}"},
10045 @samp{end-language: "@var{language}"} pair.
10047 Customizing a language stands for appending options (@pxref{autom4te
10048 Invocation}) to the current definition of the language.  Options, and
10049 more generally arguments, are introduced by @samp{args:
10050 @var{arguments}}.  You may use the traditional shell syntax to quote the
10051 @var{arguments}.
10053 As an example, to disable Autoconf caches (@file{autom4te.cache})
10054 globally, include the following lines in @file{~/.autom4te.cfg}:
10056 @verbatim
10057 ## ------------------ ##
10058 ## User Preferences.  ##
10059 ## ------------------ ##
10061 begin-language: "Autoconf-without-aclocal-m4"
10062 args: --no-cache
10063 end-language: "Autoconf-without-aclocal-m4"
10064 @end verbatim
10067 @node Programming in M4sugar
10068 @section Programming in M4sugar
10070 @cindex M4sugar
10071 M4 by itself provides only a small, but sufficient, set of all-purpose
10072 macros.  M4sugar introduces additional generic macros.  Its name was
10073 coined by Lars J. Aas: ``Readability And Greater Understanding Stands 4
10074 M4sugar''.
10076 M4sugar reserves the macro namespace @samp{^_m4_} for internal use, and
10077 the macro namespace @samp{^m4_} for M4sugar macros.  You should not
10078 define your own macros into these namespaces.
10080 @menu
10081 * Redefined M4 Macros::         M4 builtins changed in M4sugar
10082 * Conditional constructs::      Conditions in M4
10083 * Looping constructs::          Iteration in M4
10084 * Evaluation Macros::           More quotation and evaluation control
10085 * Text processing Macros::      String manipulation in M4
10086 * Forbidden Patterns::          Catching unexpanded macros
10087 @end menu
10089 @node Redefined M4 Macros
10090 @subsection Redefined M4 Macros
10092 @msindex{builtin}
10093 @msindex{changecom}
10094 @msindex{changequote}
10095 @msindex{debugfile}
10096 @msindex{debugmode}
10097 @msindex{decr}
10098 @msindex{define}
10099 @msindex{divnum}
10100 @msindex{dumpdef}
10101 @msindex{errprint}
10102 @msindex{esyscmd}
10103 @msindex{eval}
10104 @msindex{format}
10105 @msindex{ifdef}
10106 @msindex{incr}
10107 @msindex{index}
10108 @msindex{indir}
10109 @msindex{len}
10110 @msindex{pushdef}
10111 @msindex{shift}
10112 @msindex{substr}
10113 @msindex{syscmd}
10114 @msindex{sysval}
10115 @msindex{traceoff}
10116 @msindex{traceon}
10117 @msindex{translit}
10118 @msindex{undefine}
10119 With a few exceptions, all the M4 native macros are moved in the
10120 @samp{m4_} pseudo-namespace, e.g., M4sugar renames @code{define} as
10121 @code{m4_define} etc.
10123 Some M4 macros are redefined, and are slightly incompatible with their
10124 native equivalent.
10126 @defmac dnl
10127 @msindex{dnl}
10128 This macro kept its original name: no @code{m4_dnl} is defined.
10129 @end defmac
10131 @defmac m4_defn (@var{macro})
10132 @msindex{defn}
10133 Unlike the M4 builtin, this macro fails if @var{macro} is not
10134 defined.  See @code{m4_undefine}.
10135 @end defmac
10137 @c FIXME: Need to document m4_divert, m4_undivert, m4_divert_push,
10138 @c m4_divert_pop, m4_divert_text, m4_divert_once
10140 @defmac m4_exit (@var{exit-status})
10141 @msindex{exit}
10142 This macro corresponds to @code{m4exit}.
10143 @end defmac
10145 @defmac m4_if (@var{comment})
10146 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @ovar{not-equal})
10147 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @dots{})
10148 @msindex{if}
10149 This macro corresponds to @code{ifelse}.  @var{string-1} and
10150 @var{string-2} are compared literally, so usually one of the two
10151 arguments is passed unquoted.  @xref{Conditional constructs}, for more
10152 conditional idioms.
10153 @end defmac
10155 @defmac m4_include (@var{file})
10156 @defmacx m4_sinclude (@var{file})
10157 @msindex{include}
10158 @msindex{sinclude}
10159 Like the M4 builtins, but warn against multiple inclusions of @var{file}.
10160 @end defmac
10162 @defmac m4_mkstemp (@var{template})
10163 @defmacx m4_maketemp (@var{template})
10164 @msindex{maketemp}
10165 @msindex{mkstemp}
10166 Posix requires @code{maketemp} to replace the trailing @samp{X}
10167 characters in @var{template} with the process id, without regards to the
10168 existence of a file by that name, but this a security hole.  When this
10169 was pointed out to the Posix folks, they agreed to invent a new macro
10170 @code{mkstemp} that always creates a uniquely named file, but not all
10171 versions of @acronym{GNU} M4 support the new macro.  In M4sugar,
10172 @code{m4_maketemp} and @code{m4_mkstemp} are synonyms for each other,
10173 and both have the secure semantics regardless of which macro the
10174 underlying M4 provides.
10175 @end defmac
10177 @defmac m4_bpatsubst (@var{string}, @var{regexp}, @ovar{replacement})
10178 @msindex{bpatsubst}
10179 This macro corresponds to @code{patsubst}.  The name @code{m4_patsubst}
10180 is kept for future versions of M4sugar, once @acronym{GNU} M4 2.0 is
10181 released and supports extended regular expression syntax.
10182 @end defmac
10184 @defmac m4_popdef (@var{macro})
10185 @msindex{popdef}
10186 Unlike the M4 builtin, this macro fails if @var{macro} is not
10187 defined.  See @code{m4_undefine}.
10188 @end defmac
10190 @defmac m4_bregexp (@var{string}, @var{regexp}, @ovar{replacement})
10191 @msindex{bregexp}
10192 This macro corresponds to @code{regexp}.  The name @code{m4_regexp}
10193 is kept for future versions of M4sugar, once @acronym{GNU} M4 2.0 is
10194 released and supports extended regular expression syntax.
10195 @end defmac
10197 @defmac m4_undefine (@var{macro})
10198 @msindex{undefine}
10199 Unlike the M4 builtin, this macro fails if @var{macro} is not
10200 defined.  Use
10202 @example
10203 m4_ifdef([@var{macro}], [m4_undefine([@var{macro}])])
10204 @end example
10206 @noindent
10207 to recover the behavior of the builtin.
10208 @end defmac
10210 @defmac m4_wrap (@var{text})
10211 @msindex{wrap}
10212 This macro corresponds to @code{m4wrap}.
10214 Posix requires arguments of multiple @code{m4wrap} calls to be
10215 reprocessed at @acronym{EOF} in the same order as the original calls.
10216 @acronym{GNU} M4 versions through 1.4.x, however, reprocess them in
10217 reverse order.  Your code should not depend on the order.
10219 Also, Posix requires @code{m4wrap} to ignore its second and succeeding
10220 arguments, but @acronym{GNU} M4 versions through 1.4.x concatenate the
10221 arguments with intervening spaces.  Your code should not pass more than
10222 one argument.
10224 You are encouraged to end @var{text} with @samp{[]}, to avoid unexpected
10225 token pasting between consecutive invocations of @code{m4_wrap}, as in:
10227 @example
10228 m4_define([foo], [bar])
10229 m4_define([foofoo], [OUCH])
10230 m4_wrap([foo])
10231 m4_wrap([foo])
10232 @result{}OUCH
10233 @end example
10234 @end defmac
10237 @node Conditional constructs
10238 @subsection Conditional constructs
10240 The following macros provide additional conditional contructs, as
10241 convenience wrappers around @code{m4_if}.
10243 @defmac m4_bmatch (@var{string}, @var{regex-1}, @var{value-1}, @dots{}, @
10244   @ovar{default})
10245 @msindex{bmatch}
10246 The string @var{string} is repeatedly compared against a series of
10247 @var{regex} arguments; if a match is found, the expansion is the
10248 corresponding @var{value}, otherwise, the macro moves on to the next
10249 @var{regex}.  If no @var{regex} match, then the result is the optional
10250 @var{default}, or nothing.
10251 @end defmac
10253 @defmac m4_bpatsubsts (@var{string}, @var{regex-1}, @var{subst-1}, @dots{})
10254 @msindex{bpatsubsts}
10255 The string @var{string} is altered by @var{regex-1} and @var{subst-1},
10256 as if by:
10257 @example
10258 m4_bpatsubst([[@var{string}]], [@var{regex}], [@var{subst}])
10259 @end example
10261 @noindent
10262 The result of the substitution is then passed through the next set of
10263 @var{regex} and @var{subst}, and so forth.  An empty @var{subst} implies
10264 deletion of any matched portions in the current string.  Note that this
10265 macro over-quotes @var{string}; this behavior is intentional, so that
10266 the result of each step of the recursion remains as a quoted string.
10267 However, it means that anchors (@samp{^} and @samp{$} in the @var{regex}
10268 will line up with the extra quotations, and not the characters of the
10269 original string.
10270 @end defmac
10272 @defmac m4_case (@var{string}, @var{value-1}, @var{if-value-1}, @dots{}, @
10273   @ovar{default})
10274 @msindex{case}
10275 Test @var{string} against multiple @var{value} possibilities, resulting
10276 in the first @var{if-value} for a match, or in the optional
10277 @var{default}.  This is shorthand for:
10278 @example
10279 m4_if([@var{string}], [@var{value-1}], [@var{if-value-1}],
10280       [@var{string}], [@var{value-2}], [@var{if-value-2}], @dots{},
10281       [@var{default}])
10282 @end example
10283 @end defmac
10285 @defmac m4_cond (@var{test-1}, @var{value-1}, @var{if-value-1}, @
10286   @var{test-2}, @var{value-2}, @var{if-value-2}, @dots{}, @ovar{default})
10287 @msindex{cond}
10288 Similar to @code{m4_if}, except that each @var{test} is expanded only
10289 when it is encountered.  This is useful for short-circuiting expensive
10290 tests; while @code{m4_if} requires all its strings to be expanded up
10291 front before doing comparisons, @code{m4_cond} only expands a @var{test}
10292 when all earlier tests have failed.
10294 For an example, these two sequences give the same result, but in the
10295 case where @samp{$1} does not contain a backslash, the @code{m4_cond}
10296 version only expands @code{m4_index} once, instead of five times, for
10297 faster computation if this is a common case for @samp{$1}.  Notice that
10298 every third argument is unquoted for @code{m4_if}, and quoted for
10299 @code{m4_cond}:
10301 @example
10302 m4_if(m4_index([$1], [\]), [-1], [$2],
10303       m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
10304       m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
10305       m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
10306       m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
10307       [$2])
10308 m4_cond([m4_index([$1], [\])], [-1], [$2],
10309         [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
10310         [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
10311         [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
10312         [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
10313         [$2])
10314 @end example
10315 @end defmac
10317 @defmac m4_default (@var{expr-1}, @var{expr-2})
10318 @msindex{default}
10319 If @var{expr-1} is not empty, use it.  Otherwise, expand to
10320 @var{expr-2}.  Useful for providing a fixed default if the expression
10321 that results in @var{expr-1} would otherwise be empty.
10322 @end defmac
10324 @defmac m4_ifndef (@var{macro}, @var{if-not-defined}, @ovar{if-defined})
10325 @msindex{ifndef}
10326 This is shorthand for:
10327 @example
10328 m4_ifdef([@var{macro}], [@var{if-defined}], [@var{if-not-defined}])
10329 @end example
10330 @end defmac
10332 @defmac m4_ifset (@var{macro}, @ovar{if-true}, @ovar{if-false})
10333 @msindex{ifset}
10334 If @var{macro} is undefined, or is defined as the empty string, expand
10335 to @var{if-false}.  Otherwise, expands to @var{if-true}.  Similar to:
10336 @example
10337 m4_ifval(m4_defn([@var{macro}]), [@var{if-true}], [@var{if-false}])
10338 @end example
10339 @noindent
10340 except that it is not an error if @var{macro} is undefined.
10341 @end defmac
10343 @defmac m4_ifval (@var{cond}, @ovar{if-true}, @ovar{if-false})
10344 @msindex{ifval}
10345 Expands to @var{if-true} if @var{cond} is not empty, otherwise to
10346 @var{if-false}.  This is shorthand for:
10347 @example
10348 m4_if([@var{cond}], [], [@var{if-true}], [@var{if-false}])
10349 @end example
10350 @end defmac
10352 @defmac m4_ifvaln (@var{cond}, @ovar{if-true}, @ovar{if-false})
10353 @msindex{ifvaln}
10354 Similar to @code{m4_ifval}, except guarantee that a newline is present
10355 after any non-empty expansion.
10356 @end defmac
10358 @defmac m4_n (@var{text})
10359 @msindex{n}
10360 Expand to @var{text}, and add a newline if @var{text} is not empty.
10361 @end defmac
10364 @node Looping constructs
10365 @subsection Looping constructs
10367 The following macros implement loops in M4.
10369 @defmac m4_for (@var{var}, @var{first}, @var{last}, @ovar{step}, @
10370   @var{expression})
10371 @msindex{for}
10372 Loop over the numeric values between @var{first} and @var{last}
10373 including bounds by increments of @var{step}.  For each iteration,
10374 expand @var{expression} with the numeric value assigned to @var{var}.
10375 If @var{step} is omitted, it defaults to @samp{1} or @samp{-1} depending
10376 on the order of the limits.  If given, @var{step} has to match this
10377 order.
10378 @end defmac
10380 @defmac m4_foreach (@var{var}, @var{list}, @var{expression})
10381 @msindex{foreach}
10382 Loop over the comma-separated M4 list @var{list}, assigning each value
10383 to @var{var}, and expand @var{expression}.  The following example
10384 outputs two lines:
10386 @example
10387 m4_foreach([myvar], [[foo], [bar, baz]],
10388            [echo myvar
10391 @end example
10392 @end defmac
10394 @anchor{m4_foreach_w}
10395 @defmac m4_foreach_w (@var{var}, @var{list}, @var{expression})
10396 @msindex{foreach_w}
10397 Loop over the white-space-separated list @var{list}, assigning each value
10398 to @var{var}, and expand @var{expression}.
10400 The deprecated macro @code{AC_FOREACH} is an alias of
10401 @code{m4_foreach_w}.
10402 @end defmac
10404 The following macros are useful in implementing recursive algorithms.
10406 @defmac m4_do (@dots{})
10407 @msindex{do}
10408 This macro loops over its arguments and expands each one in sequence.
10409 Its main use is for readability; it allows the use of indentation and
10410 fewer @code{dnl} to result in the same expansion.
10411 @end defmac
10413 @defmac m4_shiftn (@var{count}, @dots{})
10414 @defmacx m4_shift2 (@dots{})
10415 @defmacx m4_shift3 (@dots{})
10416 @msindex{shift2}
10417 @msindex{shift3}
10418 @msindex{shiftn}
10419 @code{m4_shiftn} performs @var{count} iterations of @code{m4_shift},
10420 along with validation that enough arguments were passed in to match the
10421 shift count.  @code{m4_shift2} and @code{m4_shift3} are specializations
10422 of @code{m4_shiftn} that are more efficient for two and three shifts,
10423 respectively.
10424 @end defmac
10427 @node Evaluation Macros
10428 @subsection Evaluation Macros
10430 The following macros give some control over the order of the evaluation
10431 by adding or removing levels of quotes.  They are meant for hard-core M4
10432 programmers.
10434 @defmac m4_dquote (@var{arg1}, @dots{})
10435 @msindex{dquote}
10436 Return the arguments as a quoted list of quoted arguments.
10437 @end defmac
10439 @defmac m4_quote (@var{arg1}, @dots{})
10440 @msindex{quote}
10441 Return the arguments as a single entity, i.e., wrap them into a pair of
10442 quotes.
10443 @end defmac
10445 The following example aims at emphasizing the difference between (i), not
10446 using these macros, (ii), using @code{m4_quote}, and (iii), using
10447 @code{m4_dquote}.
10449 @example
10450 $ @kbd{cat example.m4}
10451 # Overquote, so that quotes are visible.
10452 m4_define([show], [$[]1 = [$1], $[]@@ = [$@@]])
10453 m4_define([mkargs], [1, 2, 3])
10454 m4_define([arg1], [[$1]])
10455 m4_divert(0)dnl
10456 show(a, b)
10457 show(m4_quote(a, b))
10458 show(m4_dquote(a, b))
10459 arg1(mkargs)
10460 arg1([mkargs])
10461 arg1(m4_defn([mkargs]))
10462 arg1(m4_quote(mkargs))
10463 arg1(m4_dquote(mkargs))
10464 $ @kbd{autom4te -l m4sugar example.m4}
10465 $1 = a, $@@ = [a],[b]
10466 $1 = a,b, $@@ = [a,b]
10467 $1 = [a],[b], $@@ = [[a],[b]]
10469 mkargs
10470 1, 2, 3
10471 1,2,3
10472 [1],[2],[3]
10473 @end example
10477 @node Text processing Macros
10478 @subsection Text processing Macros
10480 The following macros may be used to manipulate strings in M4.
10481 They are not intended for casual use.
10483 @defmac m4_re_escape (@var{string})
10484 @msindex{re_escape}
10485 Backslash-escape all characters in @var{string} that are active in
10486 regexps.
10487 @end defmac
10489 @defmac m4_tolower (@var{string})
10490 @defmacx m4_toupper (@var{string})
10491 @msindex{tolower}
10492 @msindex{toupper}
10493 Return @var{string} with letters converted to upper or lower case,
10494 respectively.
10495 @end defmac
10497 @defmac m4_split (@var{string}, @ovar{regexp})
10498 @msindex{split}
10499 Split @var{string} into an M4 list of elements quoted by @samp{[} and
10500 @samp{]}, while keeping white space at the beginning and at the end.
10501 If @var{regexp} is given, use it instead of @samp{[\t ]+} for splitting.
10502 If @var{string} is empty, the result is an empty list.
10503 @end defmac
10505 @defmac m4_normalize (@var{string})
10506 @msindex{normalize}
10507 Remove leading and trailing spaces and tabs, sequences of
10508 backslash-then-newline, and replace multiple spaces and tabs with a
10509 single space.
10510 @end defmac
10512 @defmac m4_append (@var{macro-name}, @var{string}, @ovar{separator})
10513 @defmacx m4_append_uniq (@var{macro-name}, @var{string}, @ovar{separator})
10514 @msindex{append}
10515 @msindex{append_uniq}
10516 Redefine @var{macro-name} to its former contents with @var{separator}
10517 and @var{string} added at the end.  If @var{macro-name} was undefined
10518 before (but not if it was defined but empty), then no @var{separator} is
10519 added.  @code{m4_append} can be used to grow strings, and
10520 @code{m4_append_uniq} to grow strings without duplicating substrings.
10521 @end defmac
10523 @anchor{m4_version_compare}
10524 @defmac m4_version_compare (@var{version-1}, @var{version-2})
10525 @msindex{version_compare}
10526 Introduced in autoconf 2.53.  Compare the version strings
10527 @var{version-1} and @var{version-2}, and expand to @samp{-1} if
10528 @var{version-1} is smaller, @samp{0} if they are the same, or @samp{1}
10529 @var{version-2} is smaller.  Version strings must be a list of elements
10530 separated by @samp{.}, where each element is a number along with an
10531 optional lower case letter.  The comparison stops at the leftmost
10532 element that contains a difference, although a 0 element compares equal
10533 to a missing element.
10535 @example
10536 m4_version_compare([1.1], [2.0])
10537 @result{}-1
10538 m4_version_compare([2.0b], [2.0a])
10539 @result{}1
10540 m4_version_compare([1.1.1], [1.1.1a])
10541 @result{}-1
10542 m4_version_compare([1.2], [1.1.1a])
10543 @result{}1
10544 m4_version_compare([1.0], [1])
10545 @result{}0
10546 @end example
10547 @end defmac
10550 @node Forbidden Patterns
10551 @subsection Forbidden Patterns
10552 @cindex Forbidden patterns
10553 @cindex Patterns, forbidden
10555 M4sugar provides a means to define suspicious patterns, patterns
10556 describing tokens which should not be found in the output.  For
10557 instance, if an Autoconf @file{configure} script includes tokens such as
10558 @samp{AC_DEFINE}, or @samp{dnl}, then most probably something went
10559 wrong (typically a macro was not evaluated because of overquotation).
10561 M4sugar forbids all the tokens matching @samp{^_?m4_} and @samp{^dnl$}.
10562 Additional layers, such as M4sh and Autoconf, add additional forbidden
10563 patterns to the list.
10565 @defmac m4_pattern_forbid (@var{pattern})
10566 @msindex{pattern_forbid}
10567 Declare that no token matching @var{pattern} must be found in the output.
10568 Comments are not checked; this can be a problem if, for instance, you
10569 have some macro left unexpanded after an @samp{#include}.  No consensus
10570 is currently found in the Autoconf community, as some people consider it
10571 should be valid to name macros in comments (which doesn't make sense to
10572 the authors of this documentation: input, such as macros, should be
10573 documented by @samp{dnl} comments; reserving @samp{#}-comments to
10574 document the output).
10575 @end defmac
10577 Of course, you might encounter exceptions to these generic rules, for
10578 instance you might have to refer to @samp{$m4_flags}.
10580 @defmac m4_pattern_allow (@var{pattern})
10581 @msindex{pattern_allow}
10582 Any token matching @var{pattern} is allowed, including if it matches an
10583 @code{m4_pattern_forbid} pattern.
10584 @end defmac
10586 @node Programming in M4sh
10587 @section Programming in M4sh
10589 @c FIXME: Eventually will become a chapter, as it is not related to
10590 @c programming in M4 per se.
10592 M4sh, pronounced ``mash'', is aiming at producing portable Bourne shell
10593 scripts.  This name was coined by Lars J. Aas, who notes that,
10594 according to the Webster's Revised Unabridged Dictionary (1913):
10596 @quotation
10597 Mash \Mash\, n.  [Akin to G. meisch, maisch, meische, maische, mash,
10598 wash, and prob.@: to AS. miscian to mix.  See ``Mix''.]
10600 @enumerate 1
10601 @item
10602 A mass of mixed ingredients reduced to a soft pulpy state by beating or
10603 pressure@enddots{}
10605 @item
10606 A mixture of meal or bran and water fed to animals.
10608 @item
10609 A mess; trouble.  [Obs.] --Beau.@: & Fl.
10610 @end enumerate
10611 @end quotation
10614 For the time being, it is not mature enough to be widely used.
10616 M4sh reserves the M4 macro namespace @samp{^_AS_} for internal use, and
10617 the namespace @samp{^AS_} for M4sh macros.  It also reserves the shell
10618 and environment variable namespace @samp{^as_}, and the here-doc
10619 delimiter namespace @samp{^_AS[A-Z]} in the output file.  You should not
10620 define your own macros or output shell code that conflicts with these
10621 namespaces.
10623 M4sh provides portable alternatives for some common shell constructs
10624 that unfortunately are not portable in practice.
10626 @c Deprecated, to be replaced by a better API
10627 @ignore
10628 @defmac AS_BASENAME (@var{file-name})
10629 @asindex{BASENAME}
10630 Output the non-directory portion of @var{file-name}.  For example,
10631 if @code{$file} is @samp{/one/two/three}, the command
10632 @code{base=`AS_BASENAME(["$file"])`} sets @code{base} to @samp{three}.
10633 @end defmac
10634 @end ignore
10636 @defmac AS_BOURNE_COMPATIBLE
10637 @asindex{BOURNE_COMPATIBLE}
10638 Set up the shell to be more compatible with the Bourne shell as
10639 standardized by Posix, if possible.  This may involve setting
10640 environment variables, or setting options, or similar
10641 implementation-specific actions.
10642 @end defmac
10644 @defmac AS_CASE (@var{word}, @ovar{pattern1}, @ovar{if-matched1}, @
10645   @dots{}, @ovar{default})
10646 @asindex{CASE}
10647 Expand into a shell @samp{case} statement, where @var{word} is matched
10648 against one or more patterns.  @var{if-matched} is run if the
10649 corresponding pattern matched @var{word}, else @var{default} is run.
10650 @end defmac
10652 @defmac AS_DIRNAME (@var{file-name})
10653 @asindex{DIRNAME}
10654 Output the directory portion of @var{file-name}.  For example,
10655 if @code{$file} is @samp{/one/two/three}, the command
10656 @code{dir=`AS_DIRNAME(["$file"])`} sets @code{dir} to @samp{/one/two}.
10657 @end defmac
10659 @defmac AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{}, @ovar{run-if-false})
10660 @asindex{IF}
10661 Run shell code @var{test1}.  If @var{test1} exits with a zero status then
10662 run shell code @var{run-if-true1}, else examine further tests.  If no test
10663 exits with a zero status, run shell code @var{run-if-false}, with
10664 simplifications if either @var{run-if-true1} or @var{run-if-false1}
10665 is empty.  For example,
10667 @example
10668 AS_IF([test "$foo" = yes], [HANDLE_FOO([yes])],
10669       [test "$foo" != no], [HANDLE_FOO([maybe])],
10670       [echo foo not specified])
10671 @end example
10673 @noindent
10674 ensures any required macros of @code{HANDLE_FOO}
10675 are expanded before the first test.
10676 @end defmac
10678 @defmac AS_MKDIR_P (@var{file-name})
10679 @asindex{MKDIR_P}
10680 Make the directory @var{file-name}, including intervening directories
10681 as necessary.  This is equivalent to @samp{mkdir -p @var{file-name}},
10682 except that it is portable to older versions of @command{mkdir} that
10683 lack support for the @option{-p} option.  Also, @code{AS_MKDIR_P}
10684 succeeds if @var{file-name} is a symbolic link to an existing directory,
10685 even though Posix is unclear whether @samp{mkdir -p} should
10686 succeed in that case.  If creation of @var{file-name} fails, exit the
10687 script.
10689 Also see the @code{AC_PROG_MKDIR_P} macro (@pxref{Particular Programs}).
10690 @end defmac
10692 @defmac AS_SHELL_SANITIZE
10693 @asindex{SHELL_SANITIZE}
10694 Initialize the shell suitably for @code{configure} scripts.  This has
10695 the effect of @code{AS_BOURNE_COMPATIBLE}, and sets some other
10696 environment variables for predictable results from configuration tests.
10697 For example, it sets @env{LC_ALL} to change to the default C locale.
10698 @xref{Special Shell Variables}.
10699 @end defmac
10701 @defmac AS_TR_CPP (@var{expression})
10702 @asindex{TR_CPP}
10703 Transform @var{expression} into a valid right-hand side for a C @code{#define}.
10704 For example:
10706 @example
10707 # This outputs "#define HAVE_CHAR_P 1".
10708 type="char *"
10709 echo "#define AS_TR_CPP([HAVE_$type]) 1"
10710 @end example
10711 @end defmac
10713 @defmac AS_TR_SH (@var{expression})
10714 @asindex{TR_SH}
10715 Transform @var{expression} into a valid shell variable name.  For example:
10717 @example
10718 # This outputs "Have it!".
10719 header="sys/some file.h"
10720 AS_TR_SH([HAVE_$header])=yes
10721 if test "$HAVE_sys_some_file_h" = yes; then echo "Have it!"; fi
10722 @end example
10723 @end defmac
10725 @defmac AS_SET_CATFILE (@var{var}, @var{dir}, @var{file})
10726 @asindex{SET_CATFILE}
10727 Set the shell variable @var{var} to @var{dir}/@var{file}, but
10728 optimizing the common cases (@var{dir} or @var{file} is @samp{.},
10729 @var{file} is absolute, etc.).
10730 @end defmac
10733 @node File Descriptor Macros
10734 @section File Descriptor Macros
10735 @cindex input
10736 @cindex standard input
10737 @cindex file descriptors
10738 @cindex descriptors
10739 @cindex low-level output
10740 @cindex output, low-level
10742 The following macros define file descriptors used to output messages
10743 (or input values) from @file{configure} scripts.
10744 For example:
10746 @example
10747 echo "$wombats found" >&AS_MESSAGE_LOG_FD
10748 echo 'Enter desired kangaroo count:' >&AS_MESSAGE_FD
10749 read kangaroos <&AS_ORIGINAL_STDIN_FD`
10750 @end example
10752 @noindent
10753 However doing so is seldom needed, because Autoconf provides higher
10754 level macros as described below.
10756 @defmac AS_MESSAGE_FD
10757 @asindex{MESSAGE_FD}
10758 The file descriptor for @samp{checking for...}  messages and results.
10759 Normally this directs messages to the standard output, however when
10760 @command{configure} is run with the @option{-q} option, messages sent to
10761 @code{AS_MESSAGE_FD} are discarded.
10763 If you want to display some messages, consider using one of the printing
10764 macros (@pxref{Printing Messages}) instead.  Copies of messages output
10765 via these macros are also recorded in @file{config.log}.
10766 @end defmac
10768 @defmac AS_MESSAGE_LOG_FD
10769 @asindex{MESSAGE_LOG_FD}
10771 The file descriptor for messages logged to @file{config.log}.  Macros
10772 that run tools, like @code{AC_COMPILE_IFELSE} (@pxref{Running the
10773 Compiler}), redirect all output to this descriptor.  You may want to do
10774 so if you develop such a low-level macro.
10775 @end defmac
10777 @defmac AS_ORIGINAL_STDIN_FD
10778 @asindex{ORIGINAL_STDIN_FD}
10779 The file descriptor for the original standard input.
10781 When @command{configure} runs, it may accidentally execute an
10782 interactive command that has the same name as the non-interactive meant
10783 to be used or checked.  If the standard input was the terminal, such
10784 interactive programs would cause @command{configure} to stop, pending
10785 some user input.  Therefore @command{configure} redirects its standard
10786 input from @file{/dev/null} during its initialization.  This is not
10787 normally a problem, since @command{configure} normally does not need
10788 user input.
10790 In the extreme case where your @file{configure} script really needs to
10791 obtain some values from the original standard input, you can read them
10792 explicitly from @code{AS_ORIGINAL_STDIN_FD}.
10793 @end defmac
10796 @c =================================================== Writing Autoconf Macros.
10798 @node Writing Autoconf Macros
10799 @chapter Writing Autoconf Macros
10801 When you write a feature test that could be applicable to more than one
10802 software package, the best thing to do is encapsulate it in a new macro.
10803 Here are some instructions and guidelines for writing Autoconf macros.
10805 @menu
10806 * Macro Definitions::           Basic format of an Autoconf macro
10807 * Macro Names::                 What to call your new macros
10808 * Reporting Messages::          Notifying @command{autoconf} users
10809 * Dependencies Between Macros::  What to do when macros depend on other macros
10810 * Obsoleting Macros::           Warning about old ways of doing things
10811 * Coding Style::                Writing Autoconf macros @`a la Autoconf
10812 @end menu
10814 @node Macro Definitions
10815 @section Macro Definitions
10817 @acindex{DEFUN}
10818 Autoconf macros are defined using the @code{AC_DEFUN} macro, which is
10819 similar to the M4 builtin @code{m4_define} macro.  In addition to
10820 defining a macro, @code{AC_DEFUN} adds to it some code that is used to
10821 constrain the order in which macros are called (@pxref{Prerequisite
10822 Macros}).
10824 An Autoconf macro definition looks like this:
10826 @example
10827 AC_DEFUN(@var{macro-name}, @var{macro-body})
10828 @end example
10830 You can refer to any arguments passed to the macro as @samp{$1},
10831 @samp{$2}, etc.  @xref{Definitions, , How to define new macros, m4.info,
10832 @acronym{GNU} M4}, for more complete information on writing M4 macros.
10834 Be sure to properly quote both the @var{macro-body} @emph{and} the
10835 @var{macro-name} to avoid any problems if the macro happens to have
10836 been previously defined.
10838 Each macro should have a header comment that gives its prototype, and a
10839 brief description.  When arguments have default values, display them in
10840 the prototype.  For example:
10842 @example
10843 # AC_MSG_ERROR(ERROR, [EXIT-STATUS = 1])
10844 # --------------------------------------
10845 m4_define([AC_MSG_ERROR],
10846   [@{ AS_MESSAGE([error: $1], [2])
10847      exit m4_default([$2], [1]); @}])
10848 @end example
10850 Comments about the macro should be left in the header comment.  Most
10851 other comments make their way into @file{configure}, so just keep
10852 using @samp{#} to introduce comments.
10854 @cindex @code{dnl}
10855 If you have some special comments about pure M4 code, comments
10856 that make no sense in @file{configure} and in the header comment, then
10857 use the builtin @code{dnl}: it causes M4 to discard the text
10858 through the next newline.
10860 Keep in mind that @code{dnl} is rarely needed to introduce comments;
10861 @code{dnl} is more useful to get rid of the newlines following macros
10862 that produce no output, such as @code{AC_REQUIRE}.
10865 @node Macro Names
10866 @section Macro Names
10868 All of the public Autoconf macros have all-uppercase names in the
10869 namespace @samp{^AC_} to prevent them from accidentally conflicting with
10870 other text; Autoconf also reserves the namespace @samp{^_AC_} for
10871 internal macros.  All shell variables that they use for internal
10872 purposes have mostly-lowercase names starting with @samp{ac_}.  Autoconf
10873 also uses here-doc delimiters in the namespace @samp{^_AC[A-Z]}.  During
10874 @command{configure}, files produced by Autoconf make heavy use of the
10875 file system namespace @samp{^conf}.
10877 Since Autoconf is built on top of M4sugar (@pxref{Programming in
10878 M4sugar}) and M4sh (@pxref{Programming in M4sh}), you must also be aware
10879 of those namespaces (@samp{^_?\(m4\|AS\)_}).  And since
10880 @file{configure.ac} is also designed to be scanned by Autoheader,
10881 Autoscan, Autoupdate, and Automake, you should be aware of the
10882 @samp{^_?A[HNUM]_} namespaces.  In general, you @emph{should not use}
10883 the namespace of a package that does not own the macro or shell code you
10884 are writing.
10886 To ensure that your macros don't conflict with present or future
10887 Autoconf macros, you should prefix your own macro names and any shell
10888 variables they use with some other sequence.  Possibilities include your
10889 initials, or an abbreviation for the name of your organization or
10890 software package.  Historically, people have not always followed the
10891 rule of using a namespace appropriate for their package, and this has
10892 made it difficult for determining the origin of a macro (and where to
10893 report bugs about that macro), as well as difficult for the true
10894 namespace owner to add new macros without interference from pre-existing
10895 uses of third-party macros.  Perhaps the best example of this confusion
10896 is the @code{AM_GNU_GETTEXT} macro, which belongs, not to Automake, but
10897 to Gettext.
10899 Most of the Autoconf macros' names follow a structured naming convention
10900 that indicates the kind of feature check by the name.  The macro names
10901 consist of several words, separated by underscores, going from most
10902 general to most specific.  The names of their cache variables use the
10903 same convention (@pxref{Cache Variable Names}, for more information on
10904 them).
10906 The first word of the name after the namepace initials (such as
10907 @samp{AC_}) usually tells the category
10908 of the feature being tested.  Here are the categories used in Autoconf for
10909 specific test macros, the kind of macro that you are more likely to
10910 write.  They are also used for cache variables, in all-lowercase.  Use
10911 them where applicable; where they're not, invent your own categories.
10913 @table @code
10914 @item C
10915 C language builtin features.
10916 @item DECL
10917 Declarations of C variables in header files.
10918 @item FUNC
10919 Functions in libraries.
10920 @item GROUP
10921 Posix group owners of files.
10922 @item HEADER
10923 Header files.
10924 @item LIB
10925 C libraries.
10926 @item PROG
10927 The base names of programs.
10928 @item MEMBER
10929 Members of aggregates.
10930 @item SYS
10931 Operating system features.
10932 @item TYPE
10933 C builtin or declared types.
10934 @item VAR
10935 C variables in libraries.
10936 @end table
10938 After the category comes the name of the particular feature being
10939 tested.  Any further words in the macro name indicate particular aspects
10940 of the feature.  For example, @code{AC_PROG_CC_STDC} checks whether the
10941 C compiler supports @acronym{ISO} Standard C.
10943 An internal macro should have a name that starts with an underscore;
10944 Autoconf internals should therefore start with @samp{_AC_}.
10945 Additionally, a macro that is an internal subroutine of another macro
10946 should have a name that starts with an underscore and the name of that
10947 other macro, followed by one or more words saying what the internal
10948 macro does.  For example, @code{AC_PATH_X} has internal macros
10949 @code{_AC_PATH_X_XMKMF} and @code{_AC_PATH_X_DIRECT}.
10951 @node Reporting Messages
10952 @section Reporting Messages
10953 @cindex Messages, from @command{autoconf}
10955 When macros statically diagnose abnormal situations, benign or fatal,
10956 they should report them using these macros.  For dynamic issues, i.e.,
10957 when @command{configure} is run, see @ref{Printing Messages}.
10959 @defmac AC_DIAGNOSE (@var{category}, @var{message})
10960 @acindex{DIAGNOSE}
10961 Report @var{message} as a warning (or as an error if requested by the
10962 user) if warnings of the @var{category} are turned on.  You are
10963 encouraged to use standard categories, which currently include:
10965 @table @samp
10966 @item all
10967 messages that don't fall into one of the following categories.  Use of an
10968 empty @var{category} is equivalent.
10970 @item cross
10971 related to cross compilation issues.
10973 @item obsolete
10974 use of an obsolete construct.
10976 @item syntax
10977 dubious syntactic constructs, incorrectly ordered macro calls.
10978 @end table
10979 @end defmac
10981 @defmac AC_WARNING (@var{message})
10982 @acindex{WARNING}
10983 Equivalent to @samp{AC_DIAGNOSE([syntax], @var{message})}, but you are
10984 strongly encouraged to use a finer grained category.
10985 @end defmac
10987 @defmac AC_FATAL (@var{message})
10988 @acindex{FATAL}
10989 Report a severe error @var{message}, and have @command{autoconf} die.
10990 @end defmac
10992 When the user runs @samp{autoconf -W error}, warnings from
10993 @code{AC_DIAGNOSE} and @code{AC_WARNING} are reported as error, see
10994 @ref{autoconf Invocation}.
10996 @node Dependencies Between Macros
10997 @section Dependencies Between Macros
10998 @cindex Dependencies between macros
11000 Some Autoconf macros depend on other macros having been called first in
11001 order to work correctly.  Autoconf provides a way to ensure that certain
11002 macros are called if needed and a way to warn the user if macros are
11003 called in an order that might cause incorrect operation.
11005 @menu
11006 * Prerequisite Macros::         Ensuring required information
11007 * Suggested Ordering::          Warning about possible ordering problems
11008 * One-Shot Macros::             Ensuring a macro is called only once
11009 @end menu
11011 @node Prerequisite Macros
11012 @subsection Prerequisite Macros
11013 @cindex Prerequisite macros
11014 @cindex Macros, prerequisites
11016 A macro that you write might need to use values that have previously
11017 been computed by other macros.  For example, @code{AC_DECL_YYTEXT}
11018 examines the output of @code{flex} or @code{lex}, so it depends on
11019 @code{AC_PROG_LEX} having been called first to set the shell variable
11020 @code{LEX}.
11022 Rather than forcing the user of the macros to keep track of the
11023 dependencies between them, you can use the @code{AC_REQUIRE} macro to do
11024 it automatically.  @code{AC_REQUIRE} can ensure that a macro is only
11025 called if it is needed, and only called once.
11027 @defmac AC_REQUIRE (@var{macro-name})
11028 @acindex{REQUIRE}
11029 If the M4 macro @var{macro-name} has not already been called, call it
11030 (without any arguments).  Make sure to quote @var{macro-name} with
11031 square brackets.  @var{macro-name} must have been defined using
11032 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
11033 that it has been called.
11035 @code{AC_REQUIRE} must be used inside a macro defined by @code{AC_DEFUN}; it
11036 must not be called from the top level.
11037 @end defmac
11039 @code{AC_REQUIRE} is often misunderstood.  It really implements
11040 dependencies between macros in the sense that if one macro depends upon
11041 another, the latter is expanded @emph{before} the body of the
11042 former.  To be more precise, the required macro is expanded before
11043 the outermost defined macro in the current expansion stack.
11044 In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
11045 @code{FOO}.  For instance, this definition of macros:
11047 @example
11048 @group
11049 AC_DEFUN([TRAVOLTA],
11050 [test "$body_temperature_in_celsius" -gt "38" &&
11051   dance_floor=occupied])
11052 AC_DEFUN([NEWTON_JOHN],
11053 [test "$hair_style" = "curly" &&
11054   dance_floor=occupied])
11055 @end group
11057 @group
11058 AC_DEFUN([RESERVE_DANCE_FLOOR],
11059 [if date | grep '^Sat.*pm' >/dev/null 2>&1; then
11060   AC_REQUIRE([TRAVOLTA])
11061   AC_REQUIRE([NEWTON_JOHN])
11062 fi])
11063 @end group
11064 @end example
11066 @noindent
11067 with this @file{configure.ac}
11069 @example
11070 AC_INIT([Dance Manager], [1.0], [bug-dance@@example.org])
11071 RESERVE_DANCE_FLOOR
11072 if test "$dance_floor" = occupied; then
11073   AC_MSG_ERROR([cannot pick up here, let's move])
11075 @end example
11077 @noindent
11078 does not leave you with a better chance to meet a kindred soul at
11079 other times than Saturday night since it expands into:
11081 @example
11082 @group
11083 test "$body_temperature_in_Celsius" -gt "38" &&
11084   dance_floor=occupied
11085 test "$hair_style" = "curly" &&
11086   dance_floor=occupied
11088 if date | grep '^Sat.*pm' >/dev/null 2>&1; then
11092 @end group
11093 @end example
11095 This behavior was chosen on purpose: (i) it prevents messages in
11096 required macros from interrupting the messages in the requiring macros;
11097 (ii) it avoids bad surprises when shell conditionals are used, as in:
11099 @example
11100 @group
11101 if @dots{}; then
11102   AC_REQUIRE([SOME_CHECK])
11104 @dots{}
11105 SOME_CHECK
11106 @end group
11107 @end example
11109 The helper macros @code{AS_IF} and @code{AS_CASE} may be used to
11110 enforce expansion of required macros outside of shell conditional
11111 constructs.  You are furthermore encouraged to put all @code{AC_REQUIRE} calls
11112 at the beginning of a macro.  You can use @code{dnl} to avoid the empty
11113 lines they leave.
11115 @node Suggested Ordering
11116 @subsection Suggested Ordering
11117 @cindex Macros, ordering
11118 @cindex Ordering macros
11120 Some macros should be run before another macro if both are called, but
11121 neither @emph{requires} that the other be called.  For example, a macro
11122 that changes the behavior of the C compiler should be called before any
11123 macros that run the C compiler.  Many of these dependencies are noted in
11124 the documentation.
11126 Autoconf provides the @code{AC_BEFORE} macro to warn users when macros
11127 with this kind of dependency appear out of order in a
11128 @file{configure.ac} file.  The warning occurs when creating
11129 @command{configure} from @file{configure.ac}, not when running
11130 @command{configure}.
11132 For example, @code{AC_PROG_CPP} checks whether the C compiler
11133 can run the C preprocessor when given the @option{-E} option.  It should
11134 therefore be called after any macros that change which C compiler is
11135 being used, such as @code{AC_PROG_CC}.  So @code{AC_PROG_CC} contains:
11137 @example
11138 AC_BEFORE([$0], [AC_PROG_CPP])dnl
11139 @end example
11141 @noindent
11142 This warns the user if a call to @code{AC_PROG_CPP} has already occurred
11143 when @code{AC_PROG_CC} is called.
11145 @defmac AC_BEFORE (@var{this-macro-name}, @var{called-macro-name})
11146 @acindex{BEFORE}
11147 Make M4 print a warning message to the standard error output if
11148 @var{called-macro-name} has already been called.  @var{this-macro-name}
11149 should be the name of the macro that is calling @code{AC_BEFORE}.  The
11150 macro @var{called-macro-name} must have been defined using
11151 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
11152 that it has been called.
11153 @end defmac
11155 @node One-Shot Macros
11156 @subsection One-Shot Macros
11157 @cindex One-shot macros
11158 @cindex Macros, called once
11160 Some macros should be called only once, either because calling them
11161 multiple time is unsafe, or because it is bad style.  For instance
11162 Autoconf ensures that @code{AC_CANONICAL_BUILD} and cousins
11163 (@pxref{Canonicalizing}) are evaluated only once, because it makes no
11164 sense to run these expensive checks more than once.  Such one-shot
11165 macros can be defined using @code{AC_DEFUN_ONCE}.
11167 @defmac AC_DEFUN_ONCE (@var{macro-name}, @var{macro-body})
11168 @acindex{DEFUN_ONCE}
11170 Declare macro @var{macro-name} like @code{AC_DEFUN} would (@pxref{Macro
11171 Definitions}), and emit a warning any time the macro is called more than
11172 once.
11173 @end defmac
11175 Obviously it is not sensible to evaluate a macro defined by
11176 @code{AC_DEFUN_ONCE} in a macro defined by @code{AC_DEFUN}.
11177 Most of the time you want to use @code{AC_REQUIRE} (@pxref{Prerequisite
11178 Macros}).
11180 @node Obsoleting Macros
11181 @section Obsoleting Macros
11182 @cindex Obsoleting macros
11183 @cindex Macros, obsoleting
11185 Configuration and portability technology has evolved over the years.
11186 Often better ways of solving a particular problem are developed, or
11187 ad-hoc approaches are systematized.  This process has occurred in many
11188 parts of Autoconf.  One result is that some of the macros are now
11189 considered @dfn{obsolete}; they still work, but are no longer considered
11190 the best thing to do, hence they should be replaced with more modern
11191 macros.  Ideally, @command{autoupdate} should replace the old macro calls
11192 with their modern implementation.
11194 Autoconf provides a simple means to obsolete a macro.
11196 @anchor{AU_DEFUN}
11197 @defmac AU_DEFUN (@var{old-macro}, @var{implementation}, @ovar{message})
11198 @auindex{DEFUN}
11199 Define @var{old-macro} as @var{implementation}.  The only difference
11200 with @code{AC_DEFUN} is that the user is warned that
11201 @var{old-macro} is now obsolete.
11203 If she then uses @command{autoupdate}, the call to @var{old-macro} is
11204 replaced by the modern @var{implementation}.  @var{message} should
11205 include information on what to do after running @command{autoupdate};
11206 @command{autoupdate} prints it as a warning, and includes it
11207 in the updated @file{configure.ac} file.
11209 The details of this macro are hairy: if @command{autoconf} encounters an
11210 @code{AU_DEFUN}ed macro, all macros inside its second argument are expanded
11211 as usual.  However, when @command{autoupdate} is run, only M4 and M4sugar
11212 macros are expanded here, while all other macros are disabled and
11213 appear literally in the updated @file{configure.ac}.
11214 @end defmac
11216 @defmac AU_ALIAS (@var{old-name}, @var{new-name})
11217 @auindex{ALIAS}
11218 Used if the @var{old-name} is to be replaced by a call to @var{new-macro}
11219 with the same parameters.  This happens for example if the macro was renamed.
11220 @end defmac
11222 @node Coding Style
11223 @section Coding Style
11224 @cindex Coding style
11226 The Autoconf macros follow a strict coding style.  You are encouraged to
11227 follow this style, especially if you intend to distribute your macro,
11228 either by contributing it to Autoconf itself, or via other means.
11230 The first requirement is to pay great attention to the quotation.  For
11231 more details, see @ref{Autoconf Language}, and @ref{M4 Quotation}.
11233 Do not try to invent new interfaces.  It is likely that there is a macro
11234 in Autoconf that resembles the macro you are defining: try to stick to
11235 this existing interface (order of arguments, default values, etc.).  We
11236 @emph{are} conscious that some of these interfaces are not perfect;
11237 nevertheless, when harmless, homogeneity should be preferred over
11238 creativity.
11240 Be careful about clashes both between M4 symbols and between shell
11241 variables.
11243 If you stick to the suggested M4 naming scheme (@pxref{Macro Names}),
11244 you are unlikely to generate conflicts.  Nevertheless, when you need to
11245 set a special value, @emph{avoid using a regular macro name}; rather,
11246 use an ``impossible'' name.  For instance, up to version 2.13, the macro
11247 @code{AC_SUBST} used to remember what @var{symbol} macros were already defined
11248 by setting @code{AC_SUBST_@var{symbol}}, which is a regular macro name.
11249 But since there is a macro named @code{AC_SUBST_FILE}, it was just
11250 impossible to @samp{AC_SUBST(FILE)}!  In this case,
11251 @code{AC_SUBST(@var{symbol})} or @code{_AC_SUBST(@var{symbol})} should
11252 have been used (yes, with the parentheses).
11253 @c or better yet, high-level macros such as @code{m4_expand_once}
11255 No Autoconf macro should ever enter the user-variable name space; i.e.,
11256 except for the variables that are the actual result of running the
11257 macro, all shell variables should start with @code{ac_}.  In
11258 addition, small macros or any macro that is likely to be embedded in
11259 other macros should be careful not to use obvious names.
11261 @cindex @code{dnl}
11262 Do not use @code{dnl} to introduce comments: most of the comments you
11263 are likely to write are either header comments which are not output
11264 anyway, or comments that should make their way into @file{configure}.
11265 There are exceptional cases where you do want to comment special M4
11266 constructs, in which case @code{dnl} is right, but keep in mind that it
11267 is unlikely.
11269 M4 ignores the leading blanks and newlines before each argument.
11270 Use this feature to
11271 indent in such a way that arguments are (more or less) aligned with the
11272 opening parenthesis of the macro being called.  For instance, instead of
11274 @example
11275 AC_CACHE_CHECK(for EMX OS/2 environment,
11276 ac_cv_emxos2,
11277 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [return __EMX__;])],
11278 [ac_cv_emxos2=yes], [ac_cv_emxos2=no])])
11279 @end example
11281 @noindent
11282 write
11284 @example
11285 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
11286 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
11287                    [ac_cv_emxos2=yes],
11288                    [ac_cv_emxos2=no])])
11289 @end example
11291 @noindent
11292 or even
11294 @example
11295 AC_CACHE_CHECK([for EMX OS/2 environment],
11296                [ac_cv_emxos2],
11297                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
11298                                                    [return __EMX__;])],
11299                                   [ac_cv_emxos2=yes],
11300                                   [ac_cv_emxos2=no])])
11301 @end example
11303 When using @code{AC_RUN_IFELSE} or any macro that cannot work when
11304 cross-compiling, provide a pessimistic value (typically @samp{no}).
11306 Feel free to use various tricks to prevent auxiliary tools, such as
11307 syntax-highlighting editors, from behaving improperly.  For instance,
11308 instead of:
11310 @example
11311 m4_bpatsubst([$1], [$"])
11312 @end example
11314 @noindent
11317 @example
11318 m4_bpatsubst([$1], [$""])
11319 @end example
11321 @noindent
11322 so that Emacsen do not open an endless ``string'' at the first quote.
11323 For the same reasons, avoid:
11325 @example
11326 test $[#] != 0
11327 @end example
11329 @noindent
11330 and use:
11332 @example
11333 test $[@@%:@@] != 0
11334 @end example
11336 @noindent
11337 Otherwise, the closing bracket would be hidden inside a @samp{#}-comment,
11338 breaking the bracket-matching highlighting from Emacsen.  Note the
11339 preferred style to escape from M4: @samp{$[1]}, @samp{$[@@]}, etc.  Do
11340 not escape when it is unnecessary.  Common examples of useless quotation
11341 are @samp{[$]$1} (write @samp{$$1}), @samp{[$]var} (use @samp{$var}),
11342 etc.  If you add portability issues to the picture, you'll prefer
11343 @samp{$@{1+"$[@@]"@}} to @samp{"[$]@@"}, and you'll prefer do something
11344 better than hacking Autoconf @code{:-)}.
11346 When using @command{sed}, don't use @option{-e} except for indenting
11347 purposes.  With the @code{s} and @code{y} commands, the preferred
11348 separator is @samp{/} unless @samp{/} itself might appear in the pattern
11349 or replacement, in which case you should use @samp{|}, or optionally
11350 @samp{,} if you know the pattern and replacement cannot contain a file
11351 name.  If none of these characters will do, choose a printable character
11352 that cannot appear in the pattern or replacement.  Characters from the
11353 set @samp{"#$&'()*;<=>?`|~} are good choices if the pattern or
11354 replacement might contain a file name, since they have special meaning
11355 to the shell and are less likely to occur in file names.
11357 @xref{Macro Definitions}, for details on how to define a macro.  If a
11358 macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
11359 of an @code{AC_REQUIRE} directive, and macros required by other macros
11360 inside arguments do not need to be expanded before this macro, then
11361 use @code{m4_define}.  In case of doubt, use @code{AC_DEFUN}.
11362 All the @code{AC_REQUIRE} statements should be at the beginning of the
11363 macro, and each statement should be followed by @code{dnl}.
11365 You should not rely on the number of arguments: instead of checking
11366 whether an argument is missing, test that it is not empty.  It provides
11367 both a simpler and a more predictable interface to the user, and saves
11368 room for further arguments.
11370 Unless the macro is short, try to leave the closing @samp{])} at the
11371 beginning of a line, followed by a comment that repeats the name of the
11372 macro being defined.  This introduces an additional newline in
11373 @command{configure}; normally, that is not a problem, but if you want to
11374 remove it you can use @samp{[]dnl} on the last line.  You can similarly
11375 use @samp{[]dnl} after a macro call to remove its newline.  @samp{[]dnl}
11376 is recommended instead of @samp{dnl} to ensure that M4 does not
11377 interpret the @samp{dnl} as being attached to the preceding text or
11378 macro output.  For example, instead of:
11380 @example
11381 AC_DEFUN([AC_PATH_X],
11382 [AC_MSG_CHECKING([for X])
11383 AC_REQUIRE_CPP()
11384 @r{# @dots{}omitted@dots{}}
11385   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
11386 fi])
11387 @end example
11389 @noindent
11390 you would write:
11392 @example
11393 AC_DEFUN([AC_PATH_X],
11394 [AC_REQUIRE_CPP()[]dnl
11395 AC_MSG_CHECKING([for X])
11396 @r{# @dots{}omitted@dots{}}
11397   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
11398 fi[]dnl
11399 ])# AC_PATH_X
11400 @end example
11402 If the macro is long, try to split it into logical chunks.  Typically,
11403 macros that check for a bug in a function and prepare its
11404 @code{AC_LIBOBJ} replacement should have an auxiliary macro to perform
11405 this setup.  Do not hesitate to introduce auxiliary macros to factor
11406 your code.
11408 In order to highlight the recommended coding style, here is a macro
11409 written the old way:
11411 @example
11412 dnl Check for EMX on OS/2.
11413 dnl _AC_EMXOS2
11414 AC_DEFUN(_AC_EMXOS2,
11415 [AC_CACHE_CHECK(for EMX OS/2 environment, ac_cv_emxos2,
11416 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, return __EMX__;)],
11417 ac_cv_emxos2=yes, ac_cv_emxos2=no)])
11418 test "$ac_cv_emxos2" = yes && EMXOS2=yes])
11419 @end example
11421 @noindent
11422 and the new way:
11424 @example
11425 # _AC_EMXOS2
11426 # ----------
11427 # Check for EMX on OS/2.
11428 m4_define([_AC_EMXOS2],
11429 [AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
11430 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
11431                    [ac_cv_emxos2=yes],
11432                    [ac_cv_emxos2=no])])
11433 test "$ac_cv_emxos2" = yes && EMXOS2=yes[]dnl
11434 ])# _AC_EMXOS2
11435 @end example
11440 @c ============================================= Portable Shell Programming
11442 @node Portable Shell
11443 @chapter Portable Shell Programming
11444 @cindex Portable shell programming
11446 When writing your own checks, there are some shell-script programming
11447 techniques you should avoid in order to make your code portable.  The
11448 Bourne shell and upward-compatible shells like the Korn shell and Bash
11449 have evolved over the years, but to prevent trouble, do not take
11450 advantage of features that were added after Unix version 7, circa
11451 1977 (@pxref{Systemology}).
11453 You should not use aliases, negated character classes, or other features
11454 that are not found in all Bourne-compatible shells; restrict yourself
11455 to the lowest common denominator.  Even @code{unset} is not supported
11456 by all shells!
11458 Shell functions are considered portable nowadays, though Autoconf
11459 still does not use them (Autotest does).  However, inside a shell
11460 function you should not be using @code{$?} to check the return code
11461 of a subshell invocation; in general, since the caller of a shell
11462 function might look at the function's return code, make sure that the
11463 last statement of a shell function does not invoke a subshell.
11464 Using subshells triggers bugs in zsh 4.x; while Autoconf tries
11465 to find a shell that does not exhibit the bug, zsh might be the
11466 only shell present on the user's machine.
11468 Some ancient systems have quite
11469 small limits on the length of the @samp{#!} line; for instance, 32
11470 bytes (not including the newline) on SunOS 4.
11471 A few ancient 4.2@acronym{BSD} based systems (such as Dynix circa 1984)
11472 required a single space between the @samp{#!} and the @samp{/}.
11473 However, these ancient systems are no longer of practical concern.
11475 The set of external programs you should run in a @command{configure} script
11476 is fairly small.  @xref{Utilities in Makefiles, , Utilities in
11477 Makefiles, standards, @acronym{GNU} Coding Standards}, for the list.  This
11478 restriction allows users to start out with a fairly small set of
11479 programs and build the rest, avoiding too many interdependencies between
11480 packages.
11482 Some of these external utilities have a portable subset of features; see
11483 @ref{Limitations of Usual Tools}.
11485 There are other sources of documentation about shells.  The
11486 specification for the Posix
11487 @uref{http://www.opengroup.org/@/susv3/@/utilities/@/xcu_chap02.html, Shell
11488 Command Language}, though more generous than the restrictive shell
11489 subset described above, is fairly portable nowadays.  Also please see
11490 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/, the Shell FAQs}.
11492 @menu
11493 * Shellology::                  A zoology of shells
11494 * Here-Documents::              Quirks and tricks
11495 * File Descriptors::            FDs and redirections
11496 * File System Conventions::     File names
11497 * Shell Pattern Matching::      Pattern matching
11498 * Shell Substitutions::         Variable and command expansions
11499 * Assignments::                 Varying side effects of assignments
11500 * Parentheses::                 Parentheses in shell scripts
11501 * Slashes::                     Slashes in shell scripts
11502 * Special Shell Variables::     Variables you should not change
11503 * Limitations of Builtins::     Portable use of not so portable /bin/sh
11504 * Limitations of Usual Tools::  Portable use of portable tools
11505 @end menu
11507 @node Shellology
11508 @section Shellology
11509 @cindex Shellology
11511 There are several families of shells, most prominently the Bourne family
11512 and the C shell family which are deeply incompatible.  If you want to
11513 write portable shell scripts, avoid members of the C shell family.  The
11514 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/@/shell-differences/, the
11515 Shell difference FAQ} includes a small history of Posix shells, and a
11516 comparison between several of them.
11518 Below we describe some of the members of the Bourne shell family.
11520 @table @asis
11521 @item Ash
11522 @cindex Ash
11523 Ash is often used on @acronym{GNU}/Linux and @acronym{BSD}
11524 systems as a light-weight Bourne-compatible shell.  Ash 0.2 has some
11525 bugs that are fixed in the 0.3.x series, but portable shell scripts
11526 should work around them, since version 0.2 is still shipped with many
11527 @acronym{GNU}/Linux distributions.
11529 To be compatible with Ash 0.2:
11531 @itemize @minus
11532 @item
11533 don't use @samp{$?} after expanding empty or unset variables,
11534 or at the start of an @command{eval}:
11536 @example
11537 foo=
11538 false
11539 $foo
11540 echo "Do not use it: $?"
11541 false
11542 eval 'echo "Do not use it: $?"'
11543 @end example
11545 @item
11546 don't use command substitution within variable expansion:
11548 @example
11549 cat $@{FOO=`bar`@}
11550 @end example
11552 @item
11553 beware that single builtin substitutions are not performed by a
11554 subshell, hence their effect applies to the current shell!  @xref{Shell
11555 Substitutions}, item ``Command Substitution''.
11556 @end itemize
11558 @item Bash
11559 @cindex Bash
11560 To detect whether you are running Bash, test whether
11561 @code{BASH_VERSION} is set.  To require
11562 Posix compatibility, run @samp{set -o posix}.  @xref{Bash POSIX
11563 Mode, , Bash Posix Mode, bash, The @acronym{GNU} Bash Reference
11564 Manual}, for details.
11566 @item Bash 2.05 and later
11567 @cindex Bash 2.05 and later
11568 Versions 2.05 and later of Bash use a different format for the
11569 output of the @command{set} builtin, designed to make evaluating its
11570 output easier.  However, this output is not compatible with earlier
11571 versions of Bash (or with many other shells, probably).  So if
11572 you use Bash 2.05 or higher to execute @command{configure},
11573 you'll need to use Bash 2.05 for all other build tasks as well.
11575 @item Ksh
11576 @cindex Ksh
11577 @cindex Korn shell
11578 @prindex @samp{ksh}
11579 @prindex @samp{ksh88}
11580 @prindex @samp{ksh93}
11581 The Korn shell is compatible with the Bourne family and it mostly
11582 conforms to Posix.  It has two major variants commonly
11583 called @samp{ksh88} and @samp{ksh93}, named after the years of initial
11584 release.  It is usually called @command{ksh}, but is called @command{sh}
11585 on some hosts if you set your path appropriately.
11587 Solaris systems have three variants:
11588 @prindex @command{/usr/bin/ksh} on Solaris
11589 @command{/usr/bin/ksh} is @samp{ksh88}; it is
11590 standard on Solaris 2.0 and later.
11591 @prindex @command{/usr/xpg4/bin/sh} on Solaris
11592 @command{/usr/xpg4/bin/sh} is a Posix-compliant variant of
11593 @samp{ksh88}; it is standard on Solaris 9 and later.
11594 @prindex @command{/usr/dt/bin/dtksh} on Solaris
11595 @command{/usr/dt/bin/dtksh} is @samp{ksh93}.
11596 Variants that are not standard may be parts of optional
11597 packages.  There is no extra charge for these packages, but they are
11598 not part of a minimal OS install and therefore some installations may
11599 not have it.
11601 Starting with Tru64 Version 4.0, the Korn shell @command{/usr/bin/ksh}
11602 is also available as @command{/usr/bin/posix/sh}.  If the environment
11603 variable @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
11604 the standard shell conform to Posix.
11606 @item Pdksh
11607 @prindex @samp{pdksh}
11608 A public-domain clone of the Korn shell called @command{pdksh} is widely
11609 available: it has most of the @samp{ksh88} features along with a few of
11610 its own.  It usually sets @code{KSH_VERSION}, except if invoked as
11611 @command{/bin/sh} on Open@acronym{BSD}, and similarly to Bash you can require
11612 Posix compatibility by running @samp{set -o posix}.  Unfortunately, with
11613 @command{pdksh} 5.2.14 (the latest stable version as of January 2007)
11614 Posix mode is buggy and causes @command{pdksh} to depart from Posix in
11615 at least one respect:
11617 @example
11618 $ @kbd{echo "`echo \"hello\"`"}
11619 hello
11620 $ @kbd{set -o posix}
11621 $ @kbd{echo "`echo \"hello\"`"}
11622 "hello"
11623 @end example
11625 The last line of output contains spurious quotes.  This is yet another
11626 reason why portable shell code should not contain
11627 @code{"`@dots{}\"@dots{}\"@dots{}`"} constructs (@pxref{Shell
11628 Substitutions}).
11630 @item Zsh
11631 @cindex Zsh
11632 To detect whether you are running @command{zsh}, test whether
11633 @code{ZSH_VERSION} is set.  By default @command{zsh} is @emph{not}
11634 compatible with the Bourne shell: you must execute @samp{emulate sh},
11635 and for @command{zsh} versions before 3.1.6-dev-18 you must also
11636 set @code{NULLCMD} to @samp{:}.  @xref{Compatibility, , Compatibility,
11637 zsh, The Z Shell Manual}, for details.
11639 The default Mac OS X @command{sh} was originally Zsh; it was changed to
11640 Bash in Mac OS X 10.2.
11641 @end table
11643 The following discussion between Russ Allbery and Robert Lipe is worth
11644 reading:
11646 @noindent
11647 Russ Allbery:
11649 @quotation
11650 The @acronym{GNU} assumption that @command{/bin/sh} is the one and only shell
11651 leads to a permanent deadlock.  Vendors don't want to break users'
11652 existing shell scripts, and there are some corner cases in the Bourne
11653 shell that are not completely compatible with a Posix shell.  Thus,
11654 vendors who have taken this route will @emph{never} (OK@dots{}``never say
11655 never'') replace the Bourne shell (as @command{/bin/sh}) with a
11656 Posix shell.
11657 @end quotation
11659 @noindent
11660 Robert Lipe:
11662 @quotation
11663 This is exactly the problem.  While most (at least most System V's) do
11664 have a Bourne shell that accepts shell functions most vendor
11665 @command{/bin/sh} programs are not the Posix shell.
11667 So while most modern systems do have a shell @emph{somewhere} that meets the
11668 Posix standard, the challenge is to find it.
11669 @end quotation
11671 @node Here-Documents
11672 @section Here-Documents
11673 @cindex Here-documents
11674 @cindex Shell here-documents
11676 Don't rely on @samp{\} being preserved just because it has no special
11677 meaning together with the next symbol.  In the native @command{sh}
11678 on Open@acronym{BSD} 2.7 @samp{\"} expands to @samp{"} in here-documents with
11679 unquoted delimiter.  As a general rule, if @samp{\\} expands to @samp{\}
11680 use @samp{\\} to get @samp{\}.
11682 With Open@acronym{BSD} 2.7's @command{sh}
11684 @example
11685 @group
11686 $ @kbd{cat <<EOF
11687 > \" \\
11688 > EOF}
11689 " \
11690 @end group
11691 @end example
11693 @noindent
11694 and with Bash:
11696 @example
11697 @group
11698 bash-2.04$ @kbd{cat <<EOF
11699 > \" \\
11700 > EOF}
11701 \" \
11702 @end group
11703 @end example
11705 Some shells mishandle large here-documents: for example,
11706 Solaris 10 @command{dtksh} and the UnixWare 7.1.1 Posix shell, which are
11707 derived from Korn shell version M-12/28/93d, mishandle braced variable
11708 expansion that crosses a 1024- or 4096-byte buffer boundary
11709 within a here-document.  Only the part of the variable name after the boundary
11710 is used.  For example, @code{$@{variable@}} could be replaced by the expansion
11711 of @code{$@{ble@}}.  If the end of the variable name is aligned with the block
11712 boundary, the shell reports an error, as if you used @code{$@{@}}.
11713 Instead of @code{$@{variable-default@}}, the shell may expand
11714 @code{$@{riable-default@}}, or even @code{$@{fault@}}.  This bug can often
11715 be worked around by omitting the braces: @code{$variable}.  The bug was
11716 fixed in
11717 @samp{ksh93g} (1998-04-30) but as of 2006 many operating systems were
11718 still shipping older versions with the bug.
11720 Many shells (including the Bourne shell) implement here-documents
11721 inefficiently.  In particular, some shells can be extremely inefficient when
11722 a single statement contains many here-documents.  For instance if your
11723 @file{configure.ac} includes something like:
11725 @example
11726 @group
11727 if <cross_compiling>; then
11728   assume this and that
11729 else
11730   check this
11731   check that
11732   check something else
11733   @dots{}
11734   on and on forever
11735   @dots{}
11737 @end group
11738 @end example
11740 A shell parses the whole @code{if}/@code{fi} construct, creating
11741 temporary files for each here-document in it.  Some shells create links
11742 for such here-documents on every @code{fork}, so that the clean-up code
11743 they had installed correctly removes them.  It is creating the links
11744 that can take the shell forever.
11746 Moving the tests out of the @code{if}/@code{fi}, or creating multiple
11747 @code{if}/@code{fi} constructs, would improve the performance
11748 significantly.  Anyway, this kind of construct is not exactly the
11749 typical use of Autoconf.  In fact, it's even not recommended, because M4
11750 macros can't look into shell conditionals, so we may fail to expand a
11751 macro when it was expanded before in a conditional path, and the
11752 condition turned out to be false at runtime, and we end up not
11753 executing the macro at all.
11755 @node File Descriptors
11756 @section File Descriptors
11757 @cindex Descriptors
11758 @cindex File descriptors
11759 @cindex Shell file descriptors
11761 Most shells, if not all (including Bash, Zsh, Ash), output traces on
11762 stderr, even for subshells.  This might result in undesirable content
11763 if you meant to capture the standard-error output of the inner command:
11765 @example
11766 $ @kbd{ash -x -c '(eval "echo foo >&2") 2>stderr'}
11767 $ @kbd{cat stderr}
11768 + eval echo foo >&2
11769 + echo foo
11771 $ @kbd{bash -x -c '(eval "echo foo >&2") 2>stderr'}
11772 $ @kbd{cat stderr}
11773 + eval 'echo foo >&2'
11774 ++ echo foo
11776 $ @kbd{zsh -x -c '(eval "echo foo >&2") 2>stderr'}
11777 @i{# Traces on startup files deleted here.}
11778 $ @kbd{cat stderr}
11779 +zsh:1> eval echo foo >&2
11780 +zsh:1> echo foo
11782 @end example
11784 @noindent
11785 One workaround is to grep out uninteresting lines, hoping not to remove
11786 good ones.
11788 If you intend to redirect both standard error and standard output,
11789 redirect standard output first.  This works better with @acronym{HP-UX},
11790 since its shell mishandles tracing if standard error is redirected
11791 first:
11793 @example
11794 $ @kbd{sh -x -c ': 2>err >out'}
11795 + :
11796 + 2> err $ @kbd{cat err}
11797 1> out
11798 @end example
11800 Don't try to redirect the standard error of a command substitution.  It
11801 must be done @emph{inside} the command substitution.  When running
11802 @samp{: `cd /zorglub` 2>/dev/null} expect the error message to
11803 escape, while @samp{: `cd /zorglub 2>/dev/null`} works properly.
11805 It is worth noting that Zsh (but not Ash nor Bash) makes it possible
11806 in assignments though: @samp{foo=`cd /zorglub` 2>/dev/null}.
11808 When catering to old systems, don't redirect the same file descriptor
11809 several times, as you are doomed to failure under Ultrix.
11811 @example
11812 ULTRIX V4.4 (Rev. 69) System #31: Thu Aug 10 19:42:23 GMT 1995
11813 UWS V4.4 (Rev. 11)
11814 $ @kbd{eval 'echo matter >fullness' >void}
11815 illegal io
11816 $ @kbd{eval '(echo matter >fullness)' >void}
11817 illegal io
11818 $ @kbd{(eval '(echo matter >fullness)') >void}
11819 Ambiguous output redirect.
11820 @end example
11822 @noindent
11823 In each case the expected result is of course @file{fullness} containing
11824 @samp{matter} and @file{void} being empty.  However, this bug is
11825 probably not of practical concern to modern platforms.
11827 Don't rely on file descriptors 0, 1, and 2 remaining closed in a
11828 subsidiary program.  If any of these descriptors is closed, the
11829 operating system may open an unspecified file for the descriptor in the
11830 new process image.  Posix says this may be done only if the subsidiary
11831 program is set-user-ID or set-group-ID, but @acronym{HP-UX} 11.23 does
11832 it even for ordinary programs.
11834 Don't rely on open file descriptors being open in child processes.  In
11835 @command{ksh}, file descriptors above 2 which are opened using
11836 @samp{exec @var{n}>file} are closed by a subsequent @samp{exec} (such as
11837 that involved in the fork-and-exec which runs a program or script).
11838 Thus, using @command{sh}, we have:
11840 @example
11841 $ @kbd{cat ./descrips}
11842 #!/bin/sh -
11843 echo hello >&5
11844 $ @kbd{exec 5>t}
11845 $ @kbd{./descrips}
11846 $ @kbd{cat t}
11847 hello
11849 @end example
11851 @noindent
11852 But using ksh:
11854 @example
11855 $ @kbd{exec 5>t}
11856 $ @kbd{./descrips}
11857 hello
11858 $ @kbd{cat t}
11860 @end example
11862 @noindent
11863 Within the process which runs the @samp{descrips} script, file
11864 descriptor 5 is closed.
11866 @acronym{DOS} variants cannot rename or remove open files, such as in
11867 @samp{mv foo bar >foo} or @samp{rm foo >foo}, even though this is
11868 perfectly portable among Posix hosts.
11870 A few ancient systems reserved some file descriptors.  By convention,
11871 file descriptor 3 was opened to @file{/dev/tty} when you logged into
11872 Eighth Edition (1985) through Tenth Edition Unix (1989).  File
11873 descriptor 4 had a special use on the Stardent/Kubota Titan (circa
11874 1990), though we don't now remember what it was.  Both these systems are
11875 obsolete, so it's now safe to treat file descriptors 3 and 4 like any
11876 other file descriptors.
11878 @node File System Conventions
11879 @section File System Conventions
11880 @cindex File system conventions
11882 Autoconf uses shell-script processing extensively, so the file names
11883 that it processes should not contain characters that are special to the
11884 shell.  Special characters include space, tab, newline, @sc{nul}, and
11885 the following:
11887 @example
11888 " # $ & ' ( ) * ; < = > ? [ \ ` |
11889 @end example
11891 Also, file names should not begin with @samp{~} or @samp{-}, and should
11892 contain neither @samp{-} immediately after @samp{/} nor @samp{~}
11893 immediately after @samp{:}.  On Posix-like platforms, directory names
11894 should not contain @samp{:}, as this runs afoul of @samp{:} used as the
11895 path separator.
11897 These restrictions apply not only to the files that you distribute, but
11898 also to the absolute file names of your source, build, and destination
11899 directories.
11901 On some Posix-like platforms, @samp{!} and @samp{^} are special too, so
11902 they should be avoided.
11904 Posix lets implementations treat leading @file{//} specially, but
11905 requires leading @file{///} and beyond to be equivalent to @file{/}.
11906 Most Unix variants treat @file{//} like @file{/}.  However, some treat
11907 @file{//} as a ``super-root'' that can provide access to files that are
11908 not otherwise reachable from @file{/}.  The super-root tradition began
11909 with Apollo Domain/OS, which died out long ago, but unfortunately Cygwin
11910 has revived it.
11912 While @command{autoconf} and friends are usually run on some Posix
11913 variety, they can be used on other systems, most notably @acronym{DOS}
11914 variants.  This impacts several assumptions regarding file names.
11916 @noindent
11917 For example, the following code:
11919 @example
11920 case $foo_dir in
11921   /*) # Absolute
11922      ;;
11923   *)
11924      foo_dir=$dots$foo_dir ;;
11925 esac
11926 @end example
11928 @noindent
11929 fails to properly detect absolute file names on those systems, because
11930 they can use a drivespec, and usually use a backslash as directory
11931 separator.  If you want to be portable to @acronym{DOS} variants (at the
11932 price of rejecting valid but oddball Posix file names like @file{a:\b}),
11933 you can check for absolute file names like this:
11935 @cindex absolute file names, detect
11936 @example
11937 case $foo_dir in
11938   [\\/]* | ?:[\\/]* ) # Absolute
11939      ;;
11940   *)
11941      foo_dir=$dots$foo_dir ;;
11942 esac
11943 @end example
11945 @noindent
11946 Make sure you quote the brackets if appropriate and keep the backslash as
11947 first character (@pxref{Limitations of Builtins}).
11949 Also, because the colon is used as part of a drivespec, these systems don't
11950 use it as path separator.  When creating or accessing paths, you can use the
11951 @code{PATH_SEPARATOR} output variable instead.  @command{configure} sets this
11952 to the appropriate value for the build system (@samp{:} or @samp{;}) when it
11953 starts up.
11955 File names need extra care as well.  While @acronym{DOS} variants
11956 that are Posixy enough to run @command{autoconf} (such as @acronym{DJGPP})
11957 are usually able to handle long file names properly, there are still
11958 limitations that can seriously break packages.  Several of these issues
11959 can be easily detected by the
11960 @uref{ftp://ftp.gnu.org/gnu/non-gnu/doschk/doschk-1.1.tar.gz, doschk}
11961 package.
11963 A short overview follows; problems are marked with @sc{sfn}/@sc{lfn} to
11964 indicate where they apply: @sc{sfn} means the issues are only relevant to
11965 plain @acronym{DOS}, not to @acronym{DOS} under Microsoft Windows
11966 variants, while @sc{lfn} identifies problems that exist even under
11967 Microsoft Windows variants.
11969 @table @asis
11970 @item No multiple dots (@sc{sfn})
11971 @acronym{DOS} cannot handle multiple dots in file names.  This is an especially
11972 important thing to remember when building a portable configure script,
11973 as @command{autoconf} uses a .in suffix for template files.
11975 This is perfectly OK on Posix variants:
11977 @example
11978 AC_CONFIG_HEADERS([config.h])
11979 AC_CONFIG_FILES([source.c foo.bar])
11980 AC_OUTPUT
11981 @end example
11983 @noindent
11984 but it causes problems on @acronym{DOS}, as it requires @samp{config.h.in},
11985 @samp{source.c.in} and @samp{foo.bar.in}.  To make your package more portable
11986 to @acronym{DOS}-based environments, you should use this instead:
11988 @example
11989 AC_CONFIG_HEADERS([config.h:config.hin])
11990 AC_CONFIG_FILES([source.c:source.cin foo.bar:foobar.in])
11991 AC_OUTPUT
11992 @end example
11994 @item No leading dot (@sc{sfn})
11995 @acronym{DOS} cannot handle file names that start with a dot.  This is usually
11996 not important for @command{autoconf}.
11998 @item Case insensitivity (@sc{lfn})
11999 @acronym{DOS} is case insensitive, so you cannot, for example, have both a
12000 file called @samp{INSTALL} and a directory called @samp{install}.  This
12001 also affects @command{make}; if there's a file called @samp{INSTALL} in
12002 the directory, @samp{make install} does nothing (unless the
12003 @samp{install} target is marked as PHONY).
12005 @item The 8+3 limit (@sc{sfn})
12006 Because the @acronym{DOS} file system only stores the first 8 characters of
12007 the file name and the first 3 of the extension, those must be unique.
12008 That means that @file{foobar-part1.c}, @file{foobar-part2.c} and
12009 @file{foobar-prettybird.c} all resolve to the same file name
12010 (@file{FOOBAR-P.C}).  The same goes for @file{foo.bar} and
12011 @file{foo.bartender}.
12013 The 8+3 limit is not usually a problem under Microsoft Windows, as it
12014 uses numeric
12015 tails in the short version of file names to make them unique.  However, a
12016 registry setting can turn this behavior off.  While this makes it
12017 possible to share file trees containing long file names between @sc{sfn}
12018 and @sc{lfn} environments, it also means the above problem applies there
12019 as well.
12021 @item Invalid characters (@sc{lfn})
12022 Some characters are invalid in @acronym{DOS} file names, and should therefore
12023 be avoided.  In a @sc{lfn} environment, these are @samp{/}, @samp{\},
12024 @samp{?}, @samp{*}, @samp{:}, @samp{<}, @samp{>}, @samp{|} and @samp{"}.
12025 In a @sc{sfn} environment, other characters are also invalid.  These
12026 include @samp{+}, @samp{,}, @samp{[} and @samp{]}.
12028 @item Invalid names (@sc{lfn})
12029 Some @acronym{DOS} file names are reserved, and cause problems if you
12030 try to use files with those names.  These names include @file{CON},
12031 @file{AUX}, @file{COM1}, @file{COM2}, @file{COM3}, @file{COM4},
12032 @file{LPT1}, @file{LPT2}, @file{LPT3}, @file{NUL}, and @file{PRN}.
12033 File names are case insensitive, so even names like
12034 @file{aux/config.guess} are disallowed.
12036 @end table
12038 @node Shell Pattern Matching
12039 @section Shell Pattern Matching
12040 @cindex Shell pattern matching
12042 Nowadays portable patterns can use negated character classes like
12043 @samp{[!-aeiou]}.  The older syntax @samp{[^-aeiou]} is supported by
12044 some shells but not others; hence portable scripts should never use
12045 @samp{^} as the first character of a bracket pattern.
12047 Outside the C locale, patterns like @samp{[a-z]} are problematic since
12048 they may match characters that are not lower-case letters.
12050 @node Shell Substitutions
12051 @section Shell Substitutions
12052 @cindex Shell substitutions
12054 Contrary to a persistent urban legend, the Bourne shell does not
12055 systematically split variables and back-quoted expressions, in particular
12056 on the right-hand side of assignments and in the argument of @code{case}.
12057 For instance, the following code:
12059 @example
12060 case "$given_srcdir" in
12061 .)  top_srcdir="`echo "$dots" | sed 's|/$||'`" ;;
12062 *)  top_srcdir="$dots$given_srcdir" ;;
12063 esac
12064 @end example
12066 @noindent
12067 is more readable when written as:
12069 @example
12070 case $given_srcdir in
12071 .)  top_srcdir=`echo "$dots" | sed 's|/$||'` ;;
12072 *)  top_srcdir=$dots$given_srcdir ;;
12073 esac
12074 @end example
12076 @noindent
12077 and in fact it is even @emph{more} portable: in the first case of the
12078 first attempt, the computation of @code{top_srcdir} is not portable,
12079 since not all shells properly understand @code{"`@dots{}"@dots{}"@dots{}`"}.
12080 Worse yet, not all shells understand @code{"`@dots{}\"@dots{}\"@dots{}`"}
12081 the same way.  There is just no portable way to use double-quoted
12082 strings inside double-quoted back-quoted expressions (pfew!).
12084 @table @code
12085 @item $@@
12086 @cindex @samp{"$@@"}
12087 One of the most famous shell-portability issues is related to
12088 @samp{"$@@"}.  When there are no positional arguments, Posix says
12089 that @samp{"$@@"} is supposed to be equivalent to nothing, but the
12090 original Unix version 7 Bourne shell treated it as equivalent to
12091 @samp{""} instead, and this behavior survives in later implementations
12092 like Digital Unix 5.0.
12094 The traditional way to work around this portability problem is to use
12095 @samp{$@{1+"$@@"@}}.  Unfortunately this method does not work with
12096 Zsh (3.x and 4.x), which is used on Mac OS X@.  When emulating
12097 the Bourne shell, Zsh performs word splitting on @samp{$@{1+"$@@"@}}:
12099 @example
12100 zsh $ @kbd{emulate sh}
12101 zsh $ @kbd{for i in "$@@"; do echo $i; done}
12102 Hello World
12104 zsh $ @kbd{for i in $@{1+"$@@"@}; do echo $i; done}
12105 Hello
12106 World
12108 @end example
12110 @noindent
12111 Zsh handles plain @samp{"$@@"} properly, but we can't use plain
12112 @samp{"$@@"} because of the portability problems mentioned above.
12113 One workaround relies on Zsh's ``global aliases'' to convert
12114 @samp{$@{1+"$@@"@}} into @samp{"$@@"} by itself:
12116 @example
12117 test "$@{ZSH_VERSION+set@}" = set && alias -g '$@{1+"$@@"@}'='"$@@"'
12118 @end example
12120 Zsh only recognizes this alias when a shell word matches it exactly;
12121 @samp{"foo"$@{1+"$@@"@}} remains subject to word splitting.  Since this
12122 case always yields at least one shell word, use plain @samp{"$@@"}.
12124 A more conservative workaround is to avoid @samp{"$@@"} if it is
12125 possible that there may be no positional arguments.  For example,
12126 instead of:
12128 @example
12129 cat conftest.c "$@@"
12130 @end example
12132 you can use this instead:
12134 @example
12135 case $# in
12136 0) cat conftest.c;;
12137 *) cat conftest.c "$@@";;
12138 esac
12139 @end example
12141 Autoconf macros often use the @command{set} command to update
12142 @samp{$@@}, so if you are writing shell code intended for
12143 @command{configure} you should not assume that the value of @samp{$@@}
12144 persists for any length of time.
12147 @item $@{10@}
12148 @cindex positional parameters
12149 The 10th, 11th, @dots{} positional parameters can be accessed only after
12150 a @code{shift}.  The 7th Edition shell reported an error if given
12151 @code{$@{10@}}, and
12152 Solaris 10 @command{/bin/sh} still acts that way:
12154 @example
12155 $ @kbd{set 1 2 3 4 5 6 7 8 9 10}
12156 $ @kbd{echo $@{10@}}
12157 bad substitution
12158 @end example
12160 @item $@{@var{var}:-@var{value}@}
12161 @c Info cannot handle `:' in index entries.
12162 @c @cindex $@{@var{var}:-@var{value}@}
12163 Old @acronym{BSD} shells, including the Ultrix @code{sh}, don't accept the
12164 colon for any shell substitution, and complain and die.
12165 Similarly for $@{@var{var}:=@var{value}@}, $@{@var{var}:?@var{value}@}, etc.
12167 @item $@{@var{var}=@var{literal}@}
12168 @cindex $@{@var{var}=@var{literal}@}
12169 Be sure to quote:
12171 @example
12172 : $@{var='Some words'@}
12173 @end example
12175 @noindent
12176 otherwise some shells, such as on Digital Unix V 5.0, die because
12177 of a ``bad substitution''.
12179 @sp 1
12181 Solaris @command{/bin/sh} has a frightening bug in its interpretation
12182 of this.  Imagine you need set a variable to a string containing
12183 @samp{@}}.  This @samp{@}} character confuses Solaris @command{/bin/sh}
12184 when the affected variable was already set.  This bug can be exercised
12185 by running:
12187 @example
12188 $ @kbd{unset foo}
12189 $ @kbd{foo=$@{foo='@}'@}}
12190 $ @kbd{echo $foo}
12192 $ @kbd{foo=$@{foo='@}'   # no error; this hints to what the bug is}
12193 $ @kbd{echo $foo}
12195 $ @kbd{foo=$@{foo='@}'@}}
12196 $ @kbd{echo $foo}
12197 @}@}
12198  ^ ugh!
12199 @end example
12201 It seems that @samp{@}} is interpreted as matching @samp{$@{}, even
12202 though it is enclosed in single quotes.  The problem doesn't happen
12203 using double quotes.
12205 @item $@{@var{var}=@var{expanded-value}@}
12206 @cindex $@{@var{var}=@var{expanded-value}@}
12207 On Ultrix,
12208 running
12210 @example
12211 default="yu,yaa"
12212 : $@{var="$default"@}
12213 @end example
12215 @noindent
12216 sets @var{var} to @samp{M-yM-uM-,M-yM-aM-a}, i.e., the 8th bit of
12217 each char is set.  You don't observe the phenomenon using a simple
12218 @samp{echo $var} since apparently the shell resets the 8th bit when it
12219 expands $var.  Here are two means to make this shell confess its sins:
12221 @example
12222 $ @kbd{cat -v <<EOF
12223 $var
12224 EOF}
12225 @end example
12227 @noindent
12230 @example
12231 $ @kbd{set | grep '^var=' | cat -v}
12232 @end example
12234 One classic incarnation of this bug is:
12236 @example
12237 default="a b c"
12238 : $@{list="$default"@}
12239 for c in $list; do
12240   echo $c
12241 done
12242 @end example
12244 @noindent
12245 You'll get @samp{a b c} on a single line.  Why?  Because there are no
12246 spaces in @samp{$list}: there are @samp{M- }, i.e., spaces with the 8th
12247 bit set, hence no IFS splitting is performed!!!
12249 One piece of good news is that Ultrix works fine with @samp{:
12250 $@{list=$default@}}; i.e., if you @emph{don't} quote.  The bad news is
12251 then that @acronym{QNX} 4.25 then sets @var{list} to the @emph{last} item of
12252 @var{default}!
12254 The portable way out consists in using a double assignment, to switch
12255 the 8th bit twice on Ultrix:
12257 @example
12258 list=$@{list="$default"@}
12259 @end example
12261 @noindent
12262 @dots{}but beware of the @samp{@}} bug from Solaris (see above).  For safety,
12263 use:
12265 @example
12266 test "$@{var+set@}" = set || var=@var{@{value@}}
12267 @end example
12269 @item $@{#@var{var}@}
12270 @itemx $@{@var{var}%@var{word}@}
12271 @itemx $@{@var{var}%%@var{word}@}
12272 @itemx $@{@var{var}#@var{word}@}
12273 @itemx $@{@var{var}##@var{word}@}
12274 @cindex $@{#@var{var}@}
12275 @cindex $@{@var{var}%@var{word}@}
12276 @cindex $@{@var{var}%%@var{word}@}
12277 @cindex $@{@var{var}#@var{word}@}
12278 @cindex $@{@var{var}##@var{word}@}
12279 Posix requires support for these usages, but they do not work with many
12280 traditional shells, e.g., Solaris 10 @command{/bin/sh}.
12282 Also, @command{pdksh} 5.2.14 mishandles some @var{word} forms.  For
12283 example if @samp{$1} is @samp{a/b} and @samp{$2} is @samp{a}, then
12284 @samp{$@{1#$2@}} should yield @samp{/b}, but with @command{pdksh} it
12285 yields the empty string.
12288 @item `@var{commands}`
12289 @cindex `@var{commands}`
12290 @cindex Command Substitution
12291 Posix requires shells to trim all trailing newlines from command
12292 output before substituting it, so assignments like
12293 @samp{dir=`echo "$file" | tr a A`} do not work as expected if
12294 @samp{$file} ends in a newline.
12296 While in general it makes no sense, do not substitute a single builtin
12297 with side effects, because Ash 0.2, trying to optimize, does not fork a
12298 subshell to perform the command.
12300 For instance, if you wanted to check that @command{cd} is silent, do not
12301 use @samp{test -z "`cd /`"} because the following can happen:
12303 @example
12304 $ @kbd{pwd}
12305 /tmp
12306 $ @kbd{test -z "`cd /`" && pwd}
12308 @end example
12310 @noindent
12311 The result of @samp{foo=`exit 1`} is left as an exercise to the reader.
12313 The MSYS shell leaves a stray byte in the expansion of a double-quoted
12314 command substitution of a native program, if the end of the substitution
12315 is not aligned with the end of the double quote.  This may be worked
12316 around by inserting another pair of quotes:
12318 @example
12319 $ @kbd{echo "`printf 'foo\r\n'` bar" > broken}
12320 $ @kbd{echo "`printf 'foo\r\n'`"" bar" | cmp - broken}
12321 - broken differ: char 4, line 1
12322 @end example
12325 @item $(@var{commands})
12326 @cindex $(@var{commands})
12327 This construct is meant to replace @samp{`@var{commands}`},
12328 and it has most of the problems listed under @code{`@var{commands}`}.
12330 This construct can be
12331 nested while this is impossible to do portably with back quotes.
12332 Unfortunately it is not yet universally supported.  Most notably, even recent
12333 releases of Solaris don't support it:
12335 @example
12336 $ @kbd{showrev -c /bin/sh | grep version}
12337 Command version: SunOS 5.10 Generic 121005-03 Oct 2006
12338 $ @kbd{echo $(echo blah)}
12339 syntax error: `(' unexpected
12340 @end example
12342 @noindent
12343 nor does @sc{irix} 6.5's Bourne shell:
12344 @example
12345 $ @kbd{uname -a}
12346 IRIX firebird-image 6.5 07151432 IP22
12347 $ @kbd{echo $(echo blah)}
12348 $(echo blah)
12349 @end example
12351 If you do use @samp{$(@var{commands})}, make sure that the commands
12352 do not start with a parenthesis, as that would cause confusion with
12353 a different notation @samp{$((@var{expression}))} that in modern
12354 shells is an arithmetic expression not a command.  To avoid the
12355 confusion, insert a space between the two opening parentheses.
12357 Avoid @var{commands} that contain unbalanced parentheses in
12358 here-documents, comments, or case statement patterns, as many shells
12359 mishandle them.  For example, Bash 3.1, @samp{ksh88}, @command{pdksh}
12360 5.2.14, and Zsh 4.2.6 all mishandle the following valid command:
12362 @example
12363 echo $(case x in x) echo hello;; esac)
12364 @end example
12366 @item ^
12367 @cindex ^ quoting
12368 Always quote @samp{^}, otherwise traditional shells such as
12369 @command{/bin/sh} on Solaris 10 treat this like @samp{|}.
12371 @end table
12374 @node Assignments
12375 @section Assignments
12376 @cindex Shell assignments
12378 When setting several variables in a row, be aware that the order of the
12379 evaluation is undefined.  For instance @samp{foo=1 foo=2; echo $foo}
12380 gives @samp{1} with Solaris @command{/bin/sh}, but @samp{2} with Bash.
12381 You must use
12382 @samp{;} to enforce the order: @samp{foo=1; foo=2; echo $foo}.
12384 Don't rely on the following to find @file{subdir/program}:
12386 @example
12387 PATH=subdir$PATH_SEPARATOR$PATH program
12388 @end example
12390 @noindent
12391 as this does not work with Zsh 3.0.6.  Use something like this
12392 instead:
12394 @example
12395 (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)
12396 @end example
12398 Don't rely on the exit status of an assignment: Ash 0.2 does not change
12399 the status and propagates that of the last statement:
12401 @example
12402 $ @kbd{false || foo=bar; echo $?}
12404 $ @kbd{false || foo=`:`; echo $?}
12406 @end example
12408 @noindent
12409 and to make things even worse, @acronym{QNX} 4.25 just sets the exit status
12410 to 0 in any case:
12412 @example
12413 $ @kbd{foo=`exit 1`; echo $?}
12415 @end example
12417 To assign default values, follow this algorithm:
12419 @enumerate
12420 @item
12421 If the default value is a literal and does not contain any closing
12422 brace, use:
12424 @example
12425 : $@{var='my literal'@}
12426 @end example
12428 @item
12429 If the default value contains no closing brace, has to be expanded, and
12430 the variable being initialized is not intended to be IFS-split
12431 (i.e., it's not a list), then use:
12433 @example
12434 : $@{var="$default"@}
12435 @end example
12437 @item
12438 If the default value contains no closing brace, has to be expanded, and
12439 the variable being initialized is intended to be IFS-split (i.e., it's a list),
12440 then use:
12442 @example
12443 var=$@{var="$default"@}
12444 @end example
12446 @item
12447 If the default value contains a closing brace, then use:
12449 @example
12450 test "$@{var+set@}" = set || var="has a '@}'"
12451 @end example
12452 @end enumerate
12454 In most cases @samp{var=$@{var="$default"@}} is fine, but in case of
12455 doubt, just use the last form.  @xref{Shell Substitutions}, items
12456 @samp{$@{@var{var}:-@var{value}@}} and @samp{$@{@var{var}=@var{value}@}}
12457 for the rationale.
12459 @node Parentheses
12460 @section Parentheses in Shell Scripts
12461 @cindex Shell parentheses
12463 Beware of two opening parentheses in a row, as many shell
12464 implementations treat them specially.  Posix requires that the command
12465 @samp{((cat))} must behave like @samp{(cat)}, but many shells, including
12466 Bash and the Korn shell, treat @samp{((cat))} as an arithmetic
12467 expression equivalent to @samp{let "cat"}, and may or may not report an
12468 error when they detect that @samp{cat} is not a number.  As another
12469 example, @samp{pdksh} 5.2.14 misparses the following code:
12471 @example
12472 if ((true) || false); then
12473   echo ok
12475 @end example
12477 @noindent
12478 To work around this problem, insert a space between the two opening
12479 parentheses.  There is a similar problem and workaround with
12480 @samp{$((}; see @ref{Shell Substitutions}.
12482 @node Slashes
12483 @section Slashes in Shell Scripts
12484 @cindex Shell slashes
12486 Unpatched Tru64 5.1 @command{sh} omits the last slash of command-line
12487 arguments that contain two trailing slashes:
12489 @example
12490 $ @kbd{echo / // /// //// .// //.}
12491 / / // /// ./ //.
12492 $ @kbd{x=//}
12493 $ @kbd{eval "echo \$x"}
12495 $ @kbd{set -x}
12496 $ @kbd{echo abc | tr -t ab //}
12497 + echo abc
12498 + tr -t ab /
12500 @end example
12502 Unpatched Tru64 4.0 @command{sh} adds a slash after @samp{"$var"} if the
12503 variable is empty and the second double-quote is followed by a word that
12504 begins and ends with slash:
12506 @example
12507 $ @kbd{sh -xc 'p=; echo "$p"/ouch/'}
12509 + echo //ouch/
12510 //ouch/
12511 @end example
12513 However, our understanding is that patches are available, so perhaps
12514 it's not worth worrying about working around these horrendous bugs.
12516 @node Special Shell Variables
12517 @section Special Shell Variables
12518 @cindex Shell variables
12519 @cindex Special shell variables
12521 Some shell variables should not be used, since they can have a deep
12522 influence on the behavior of the shell.  In order to recover a sane
12523 behavior from the shell, some variables should be unset, but
12524 @command{unset} is not portable (@pxref{Limitations of Builtins}) and a
12525 fallback value is needed.
12527 As a general rule, shell variable names containing a lower-case letter
12528 are safe; you can define and use these variables without worrying about
12529 their effect on the underlying system, and without worrying about
12530 whether the shell changes them unexpectedly.  (The exception is the
12531 shell variable @code{status}, as described below.)
12533 Here is a list of names that are known to cause trouble.  This list is
12534 not exhaustive, but you should be safe if you avoid the name
12535 @code{status} and names containing only upper-case letters and
12536 underscores.
12538 @c Alphabetical order, case insensitive, `A' before `a'.
12539 @table @code
12540 @item _
12541 Many shells reserve @samp{$_} for various purposes, e.g., the name of
12542 the last command executed.
12544 @item BIN_SH
12545 @evindex BIN_SH
12546 In Tru64, if @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
12547 the standard shell conform to Posix.
12549 @item CDPATH
12550 @evindex CDPATH
12551 When this variable is set it specifies a list of directories to search
12552 when invoking @code{cd} with a relative file name that did not start
12553 with @samp{./} or @samp{../}.  Posix
12554 1003.1-2001 says that if a nonempty directory name from @env{CDPATH}
12555 is used successfully, @code{cd} prints the resulting absolute
12556 file name.  Unfortunately this output can break idioms like
12557 @samp{abs=`cd src && pwd`} because @code{abs} receives the name twice.
12558 Also, many shells do not conform to this part of Posix; for
12559 example, @command{zsh} prints the result only if a directory name
12560 other than @file{.} was chosen from @env{CDPATH}.
12562 In practice the shells that have this problem also support
12563 @command{unset}, so you can work around the problem as follows:
12565 @example
12566 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
12567 @end example
12569 You can also avoid output by ensuring that your directory name is
12570 absolute or anchored at @samp{./}, as in @samp{abs=`cd ./src && pwd`}.
12572 Autoconf-generated scripts automatically unset @env{CDPATH} if
12573 possible, so you need not worry about this problem in those scripts.
12575 @item DUALCASE
12576 @evindex DUALCASE
12577 In the MKS shell, case statements and file name generation are
12578 case-insensitive unless @env{DUALCASE} is nonzero.
12579 Autoconf-generated scripts export this variable when they start up.
12581 @item ENV
12582 @itemx MAIL
12583 @itemx MAILPATH
12584 @itemx PS1
12585 @itemx PS2
12586 @itemx PS4
12587 @evindex ENV
12588 @evindex MAIL
12589 @evindex MAILPATH
12590 @evindex PS1
12591 @evindex PS2
12592 @evindex PS4
12593 These variables should not matter for shell scripts, since they are
12594 supposed to affect only interactive shells.  However, at least one
12595 shell (the pre-3.0 @sc{uwin} Korn shell) gets confused about
12596 whether it is interactive, which means that (for example) a @env{PS1}
12597 with a side effect can unexpectedly modify @samp{$?}.  To work around
12598 this bug, Autoconf-generated scripts do something like this:
12600 @example
12601 (unset ENV) >/dev/null 2>&1 && unset ENV MAIL MAILPATH
12602 PS1='$ '
12603 PS2='> '
12604 PS4='+ '
12605 @end example
12607 @item FPATH
12608 The Korn shell uses @env{FPATH} to find shell functions, so avoid
12609 @env{FPATH} in portable scripts.  @env{FPATH} is consulted after
12610 @env{PATH}, but you still need to be wary of tests that use @env{PATH}
12611 to find whether a command exists, since they might report the wrong
12612 result if @env{FPATH} is also set.
12614 @item IFS
12615 @evindex IFS
12616 Long ago, shell scripts inherited @env{IFS} from the environment,
12617 but this caused many problems so modern shells ignore any environment
12618 settings for @env{IFS}.
12620 Don't set the first character of @code{IFS} to backslash.  Indeed,
12621 Bourne shells use the first character (backslash) when joining the
12622 components in @samp{"$@@"} and some shells then reinterpret (!)@: the
12623 backslash escapes, so you can end up with backspace and other strange
12624 characters.
12626 The proper value for @code{IFS} (in regular code, not when performing
12627 splits) is @samp{@key{SPC}@key{TAB}@key{RET}}.  The first character is
12628 especially important, as it is used to join the arguments in @samp{$*};
12629 however, note that traditional shells, but also bash-2.04, fail to adhere
12630 to this and join with a space anyway.
12632 @item LANG
12633 @itemx LC_ALL
12634 @itemx LC_COLLATE
12635 @itemx LC_CTYPE
12636 @itemx LC_MESSAGES
12637 @itemx LC_MONETARY
12638 @itemx LC_NUMERIC
12639 @itemx LC_TIME
12640 @evindex LANG
12641 @evindex LC_ALL
12642 @evindex LC_COLLATE
12643 @evindex LC_CTYPE
12644 @evindex LC_MESSAGES
12645 @evindex LC_MONETARY
12646 @evindex LC_NUMERIC
12647 @evindex LC_TIME
12649 Autoconf-generated scripts normally set all these variables to
12650 @samp{C} because so much configuration code assumes the C locale and
12651 Posix requires that locale environment variables be set to
12652 @samp{C} if the C locale is desired.  However, some older, nonstandard
12653 systems (notably @acronym{SCO}) break if locale environment variables
12654 are set to @samp{C}, so when running on these systems
12655 Autoconf-generated scripts unset the variables instead.
12657 @item LANGUAGE
12658 @evindex LANGUAGE
12660 @env{LANGUAGE} is not specified by Posix, but it is a @acronym{GNU}
12661 extension that overrides @env{LC_ALL} in some cases, so
12662 Autoconf-generated scripts set it too.
12664 @item LC_ADDRESS
12665 @itemx LC_IDENTIFICATION
12666 @itemx LC_MEASUREMENT
12667 @itemx LC_NAME
12668 @itemx LC_PAPER
12669 @itemx LC_TELEPHONE
12670 @evindex LC_ADDRESS
12671 @evindex LC_IDENTIFICATION
12672 @evindex LC_MEASUREMENT
12673 @evindex LC_NAME
12674 @evindex LC_PAPER
12675 @evindex LC_TELEPHONE
12677 These locale environment variables are @acronym{GNU} extensions.  They
12678 are treated like their Posix brethren (@env{LC_COLLATE},
12679 etc.)@: as described above.
12681 @item LINENO
12682 Most modern shells provide the current line number in @code{LINENO}.
12683 Its value is the line number of the beginning of the current command.
12684 Autoconf attempts to execute @command{configure} with a shell that
12685 supports @code{LINENO}.
12686 If no such shell is available, it attempts to implement @code{LINENO}
12687 with a Sed prepass that replaces each instance of the string
12688 @code{$LINENO} (not followed by an alphanumeric character) with the
12689 line's number.
12691 You should not rely on @code{LINENO} within @command{eval}, as the
12692 behavior differs in practice.  Also, the possibility of the Sed
12693 prepass means that you should not rely on @code{$LINENO} when quoted,
12694 when in here-documents, or when in long commands that cross line
12695 boundaries.  Subshells should be OK, though.  In the following
12696 example, lines 1, 6, and 9 are portable, but the other instances of
12697 @code{LINENO} are not:
12699 @example
12700 @group
12701 $ @kbd{cat lineno}
12702 echo 1. $LINENO
12703 cat <<EOF
12704 3. $LINENO
12705 4. $LINENO
12707 ( echo 6. $LINENO )
12708 eval 'echo 7. $LINENO'
12709 echo 8. '$LINENO'
12710 echo 9. $LINENO '
12711 10.' $LINENO
12712 @end group
12713 @group
12714 $ @kbd{bash-2.05 lineno}
12715 1. 1
12716 3. 2
12717 4. 2
12718 6. 6
12719 7. 1
12720 8. $LINENO
12721 9. 9
12722 10. 9
12723 @end group
12724 @group
12725 $ @kbd{zsh-3.0.6 lineno}
12726 1. 1
12727 3. 2
12728 4. 2
12729 6. 6
12730 7. 7
12731 8. $LINENO
12732 9. 9
12733 10. 9
12734 @end group
12735 @group
12736 $ @kbd{pdksh-5.2.14 lineno}
12737 1. 1
12738 3. 2
12739 4. 2
12740 6. 6
12741 7. 0
12742 8. $LINENO
12743 9. 9
12744 10. 9
12745 @end group
12746 @group
12747 $ @kbd{sed '=' <lineno |}
12748 > @kbd{  sed '}
12749 > @kbd{    N}
12750 > @kbd{    s,$,-,}
12751 > @kbd{    t loop}
12752 > @kbd{    :loop}
12753 > @kbd{    s,^\([0-9]*\)\(.*\)[$]LINENO\([^a-zA-Z0-9_]\),\1\2\1\3,}
12754 > @kbd{    t loop}
12755 > @kbd{    s,-$,,}
12756 > @kbd{    s,^[0-9]*\n,,}
12757 > @kbd{  ' |}
12758 > @kbd{  sh}
12759 1. 1
12760 3. 3
12761 4. 4
12762 6. 6
12763 7. 7
12764 8. 8
12765 9. 9
12766 10. 10
12767 @end group
12768 @end example
12770 @item NULLCMD
12771 @evindex NULLCMD
12772 When executing the command @samp{>foo}, @command{zsh} executes
12773 @samp{$NULLCMD >foo} unless it is operating in Bourne shell
12774 compatibility mode and the @command{zsh} version is newer
12775 than 3.1.6-dev-18.  If you are using an older @command{zsh}
12776 and forget to set @env{NULLCMD},
12777 your script might be suspended waiting for data on its standard input.
12779 @item PATH_SEPARATOR
12780 @evindex PATH_SEPARATOR
12781 On @acronym{DJGPP} systems, the @env{PATH_SEPARATOR} environment
12782 variable can be set to either @samp{:} or @samp{;} to control the path
12783 separator Bash uses to set up certain environment variables (such as
12784 @env{PATH}).  You can set this variable to @samp{;} if you want
12785 @command{configure} to use @samp{;} as a separator; this might be useful
12786 if you plan to use non-Posix shells to execute files.  @xref{File System
12787 Conventions}, for more information about @code{PATH_SEPARATOR}.
12789 @item PWD
12790 @evindex PWD
12791 Posix 1003.1-2001 requires that @command{cd} and
12792 @command{pwd} must update the @env{PWD} environment variable to point
12793 to the logical name of the current directory, but traditional shells
12794 do not support this.  This can cause confusion if one shell instance
12795 maintains @env{PWD} but a subsidiary and different shell does not know
12796 about @env{PWD} and executes @command{cd}; in this case @env{PWD}
12797 points to the wrong directory.  Use @samp{`pwd`} rather than
12798 @samp{$PWD}.
12800 @item RANDOM
12801 Many shells provide @code{RANDOM}, a variable that returns a different
12802 integer each time it is used.  Most of the time, its value does not
12803 change when it is not used, but on @sc{irix} 6.5 the value changes all
12804 the time.  This can be observed by using @command{set}.  It is common
12805 practice to use @code{$RANDOM} as part of a file name, but code
12806 shouldn't rely on @code{$RANDOM} expanding to a nonempty string.
12808 @item status
12809 This variable is an alias to @samp{$?} for @code{zsh} (at least 3.1.6),
12810 hence read-only.  Do not use it.
12811 @end table
12813 @node Limitations of Builtins
12814 @section Limitations of Shell Builtins
12815 @cindex Shell builtins
12816 @cindex Limitations of shell builtins
12818 No, no, we are serious: some shells do have limitations!  :)
12820 You should always keep in mind that any builtin or command may support
12821 options, and therefore differ in behavior with arguments
12822 starting with a dash.  For instance, the innocent @samp{echo "$word"}
12823 can give unexpected results when @code{word} starts with a dash.  It is
12824 often possible to avoid this problem using @samp{echo "x$word"}, taking
12825 the @samp{x} into account later in the pipe.
12827 @table @asis
12828 @item @command{.}
12829 @prindex @command{.}
12830 Use @command{.} only with regular files (use @samp{test -f}).  Bash
12831 2.03, for instance, chokes on @samp{. /dev/null}.  Also, remember that
12832 @command{.} uses @env{PATH} if its argument contains no slashes, so if
12833 you want to use @command{.} on a file @file{foo} in the current
12834 directory, you must use @samp{. ./foo}.
12836 @item @command{!}
12837 @prindex @command{!}
12838 The Unix version 7 shell did not support
12839 negating the exit status of commands with @command{!}, and this feature
12840 is still absent from some shells (e.g., Solaris @command{/bin/sh}).
12841 Shell code like this:
12843 @example
12844 if ! cmp file1 file2 >/dev/null 2>&1; then
12845   echo files differ or trouble
12847 @end example
12849 is therefore not portable in practice.  Typically it is easy to rewrite
12850 such code, e.g.:
12852 @example
12853 cmp file1 file2 >/dev/null 2>&1 ||
12854   echo files differ or trouble
12855 @end example
12857 More generally, one can always rewrite @samp{! @var{command}} as:
12859 @example
12860 if @var{command}; then (exit 1); else :; fi
12861 @end example
12863 @item @command{break}
12864 @c ------------------
12865 @prindex @command{break}
12866 The use of @samp{break 2} etc.@: is safe.
12869 @item @command{case}
12870 @c -----------------
12871 @prindex @command{case}
12872 You don't need to quote the argument; no splitting is performed.
12874 You don't need the final @samp{;;}, but you should use it.
12876 Posix requires support for @code{case} patterns with opening
12877 parentheses like this:
12879 @example
12880 case $file_name in
12881 (*.c) echo "C source code";;
12882 esac
12883 @end example
12885 @noindent
12886 but the @code{(} in this example is not portable to many Bourne
12887 shell implementations.  It can be omitted safely.
12889 Zsh handles pattern fragments derived from parameter expansions or
12890 command substitutions as though quoted:
12892 @example
12893 $ pat=\?; case aa in ?$pat) echo match;; esac
12894 $ pat=\?; case a? in ?$pat) echo match;; esac
12895 match
12896 @end example
12898 @noindent
12899 Because of a bug in its @code{fnmatch}, Bash fails to properly
12900 handle backslashes in character classes:
12902 @example
12903 bash-2.02$ @kbd{case /tmp in [/\\]*) echo OK;; esac}
12904 bash-2.02$
12905 @end example
12907 @noindent
12908 This is extremely unfortunate, since you are likely to use this code to
12909 handle Posix or @sc{ms-dos} absolute file names.  To work around this
12910 bug, always put the backslash first:
12912 @example
12913 bash-2.02$ @kbd{case '\TMP' in [\\/]*) echo OK;; esac}
12915 bash-2.02$ @kbd{case /tmp in [\\/]*) echo OK;; esac}
12917 @end example
12919 Many Bourne shells cannot handle closing brackets in character classes
12920 correctly.
12922 Some shells also have problems with backslash escaping in case you do not want
12923 to match the backslash: both a backslash and the escaped character match this
12924 pattern.  To work around this, specify the character class in a variable, so
12925 that quote removal does not apply afterwards, and the special characters don't
12926 have to be backslash-escaped:
12928 @example
12929 $ @kbd{case '\' in [\<]) echo OK;; esac}
12931 $ @kbd{scanset='[<]'; case '\' in $scanset) echo OK;; esac}
12933 @end example
12935 Even with this, Solaris @command{ksh} matches a backslash if the set
12936 contains any
12937 of the characters @samp{|}, @samp{&}, @samp{(}, or @samp{)}.
12939 Conversely, Tru64 @command{ksh} (circa 2003) erroneously always matches
12940 a closing parenthesis if not specified in a character class:
12942 @example
12943 $ @kbd{case foo in *\)*) echo fail ;; esac}
12944 fail
12945 $ @kbd{case foo in *')'*) echo fail ;; esac}
12946 fail
12947 @end example
12949 Some shells, such as Ash 0.3.8, are confused by an empty
12950 @code{case}/@code{esac}:
12952 @example
12953 ash-0.3.8 $ @kbd{case foo in esac;}
12954 @error{}Syntax error: ";" unexpected (expecting ")")
12955 @end example
12957 Many shells still do not support parenthesized cases, which is a pity
12958 for those of us using tools that rely on balanced parentheses.  For
12959 instance, Solaris @command{/bin/sh}:
12961 @example
12962 $ @kbd{case foo in (foo) echo foo;; esac}
12963 @error{}syntax error: `(' unexpected
12964 @end example
12967 @item @command{cd}
12968 @c ---------------
12969 @prindex @command{cd}
12970 Posix 1003.1-2001 requires that @command{cd} must support
12971 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
12972 with @option{-L} being the default.  However, traditional shells do
12973 not support these options, and their @command{cd} command has the
12974 @option{-P} behavior.
12976 Portable scripts should assume neither option is supported, and should
12977 assume neither behavior is the default.  This can be a bit tricky,
12978 since the Posix default behavior means that, for example,
12979 @samp{ls ..} and @samp{cd ..} may refer to different directories if
12980 the current logical directory is a symbolic link.  It is safe to use
12981 @command{cd @var{dir}} if @var{dir} contains no @file{..} components.
12982 Also, Autoconf-generated scripts check for this problem when computing
12983 variables like @code{ac_top_srcdir} (@pxref{Configuration Actions}),
12984 so it is safe to @command{cd} to these variables.
12986 See @xref{Special Shell Variables}, for portability problems involving
12987 @command{cd} and the @env{CDPATH} environment variable.
12988 Also please see the discussion of the @command{pwd} command.
12991 @item @command{echo}
12992 @c -----------------
12993 @prindex @command{echo}
12994 The simple @command{echo} is probably the most surprising source of
12995 portability troubles.  It is not possible to use @samp{echo} portably
12996 unless both options and escape sequences are omitted.  New applications
12997 which are not aiming at portability should use @samp{printf} instead of
12998 @samp{echo}.
13000 Don't expect any option.  @xref{Preset Output Variables}, @code{ECHO_N}
13001 etc.@: for a means to simulate @option{-n}.
13003 Do not use backslashes in the arguments, as there is no consensus on
13004 their handling.  For @samp{echo '\n' | wc -l}, the @command{sh} of
13005 Solaris outputs 2, but Bash and Zsh (in @command{sh} emulation mode) output 1.
13006 The problem is truly @command{echo}: all the shells
13007 understand @samp{'\n'} as the string composed of a backslash and an
13008 @samp{n}.
13010 Because of these problems, do not pass a string containing arbitrary
13011 characters to @command{echo}.  For example, @samp{echo "$foo"} is safe
13012 if you know that @var{foo}'s value cannot contain backslashes and cannot
13013 start with @samp{-}, but otherwise you should use a here-document like
13014 this:
13016 @example
13017 cat <<EOF
13018 $foo
13020 @end example
13023 @item @command{eval}
13024 @c -----------------
13025 @prindex @command{eval}
13026 The @command{eval} command is useful in limited circumstances, e.g.,
13027 using commands like @samp{eval table_$key=\$value} and @samp{eval
13028 value=table_$key} to simulate a hash table when the key is known to be
13029 alphanumeric.  However, @command{eval} is tricky to use on arbitrary
13030 arguments, even when it is implemented correctly.
13032 It is obviously unwise to use @samp{eval $cmd} if the string value of
13033 @samp{cmd} was derived from an untrustworthy source.  But even if the
13034 string value is valid, @samp{eval $cmd} might not work as intended,
13035 since it causes field splitting and file name expansion to occur twice,
13036 once for the @command{eval} and once for the command itself.  It is
13037 therefore safer to use @samp{eval "$cmd"}.  For example, if @var{cmd}
13038 has the value @samp{cat test?.c}, @samp{eval $cmd} might expand to the
13039 equivalent of @samp{cat test;.c} if there happens to be a file named
13040 @file{test;.c} in the current directory; and this in turn
13041 mistakenly attempts to invoke @command{cat} on the file @file{test} and
13042 then execute the command @command{.c}.  To avoid this problem, use
13043 @samp{eval "$cmd"} rather than @samp{eval $cmd}.
13045 However, suppose that you want to output the text of the evaluated
13046 command just before executing it.  Assuming the previous example,
13047 @samp{echo "Executing: $cmd"} outputs @samp{Executing: cat test?.c}, but
13048 this output doesn't show the user that @samp{test;.c} is the actual name
13049 of the copied file.  Conversely, @samp{eval "echo Executing: $cmd"}
13050 works on this example, but it fails with @samp{cmd='cat foo >bar'},
13051 since it mistakenly replaces the contents of @file{bar} by the
13052 string @samp{cat foo}.  No simple, general, and portable solution to
13053 this problem is known.
13055 You should also be wary of common bugs in @command{eval} implementations.
13056 In some shell implementations (e.g., older @command{ash}, Open@acronym{BSD} 3.8
13057 @command{sh}, @command{pdksh} v5.2.14 99/07/13.2, and @command{zsh}
13058 4.2.5), the arguments of @samp{eval} are evaluated in a context where
13059 @samp{$?} is 0, so they exhibit behavior like this:
13061 @example
13062 $ @kbd{false; eval 'echo $?'}
13064 @end example
13066 The correct behavior here is to output a nonzero value,
13067 but portable scripts should not rely on this.
13069 You should not rely on @code{LINENO} within @command{eval}.
13070 @xref{Special Shell Variables}.
13072 @item @command{exit}
13073 @c -----------------
13074 @prindex @command{exit}
13075 The default value of @command{exit} is supposed to be @code{$?};
13076 unfortunately, some shells, such as the @acronym{DJGPP} port of Bash 2.04, just
13077 perform @samp{exit 0}.
13079 @example
13080 bash-2.04$ @kbd{foo=`exit 1` || echo fail}
13081 fail
13082 bash-2.04$ @kbd{foo=`(exit 1)` || echo fail}
13083 fail
13084 bash-2.04$ @kbd{foo=`(exit 1); exit` || echo fail}
13085 bash-2.04$
13086 @end example
13088 Using @samp{exit $?} restores the expected behavior.
13090 Some shell scripts, such as those generated by @command{autoconf}, use a
13091 trap to clean up before exiting.  If the last shell command exited with
13092 nonzero status, the trap also exits with nonzero status so that the
13093 invoker can tell that an error occurred.
13095 Unfortunately, in some shells, such as Solaris @command{/bin/sh}, an exit
13096 trap ignores the @code{exit} command's argument.  In these shells, a trap
13097 cannot determine whether it was invoked by plain @code{exit} or by
13098 @code{exit 1}.  Instead of calling @code{exit} directly, use the
13099 @code{AC_MSG_ERROR} macro that has a workaround for this problem.
13102 @item @command{export}
13103 @c -------------------
13104 @prindex @command{export}
13105 The builtin @command{export} dubs a shell variable @dfn{environment
13106 variable}.  Each update of exported variables corresponds to an update
13107 of the environment variables.  Conversely, each environment variable
13108 received by the shell when it is launched should be imported as a shell
13109 variable marked as exported.
13111 Alas, many shells, such as Solaris @command{/bin/sh},
13112 @sc{irix} 6.3, @sc{irix} 5.2,
13113 @acronym{AIX} 4.1.5, and Digital Unix 4.0, forget to
13114 @command{export} the environment variables they receive.  As a result,
13115 two variables coexist: the environment variable and the shell
13116 variable.  The following code demonstrates this failure:
13118 @example
13119 #!/bin/sh
13120 echo $FOO
13121 FOO=bar
13122 echo $FOO
13123 exec /bin/sh $0
13124 @end example
13126 @noindent
13127 when run with @samp{FOO=foo} in the environment, these shells print
13128 alternately @samp{foo} and @samp{bar}, although they should print only
13129 @samp{foo} and then a sequence of @samp{bar}s.
13131 Therefore you should @command{export} again each environment variable
13132 that you update.
13135 @item @command{false}
13136 @c ------------------
13137 @prindex @command{false}
13138 Don't expect @command{false} to exit with status 1: in native
13139 Solaris @file{/bin/false} exits with status 255.
13142 @item @command{for}
13143 @c ----------------
13144 @prindex @command{for}
13145 To loop over positional arguments, use:
13147 @example
13148 for arg
13150   echo "$arg"
13151 done
13152 @end example
13154 @noindent
13155 You may @emph{not} leave the @code{do} on the same line as @code{for},
13156 since some shells improperly grok:
13158 @example
13159 for arg; do
13160   echo "$arg"
13161 done
13162 @end example
13164 If you want to explicitly refer to the positional arguments, given the
13165 @samp{$@@} bug (@pxref{Shell Substitutions}), use:
13167 @example
13168 for arg in $@{1+"$@@"@}; do
13169   echo "$arg"
13170 done
13171 @end example
13173 @noindent
13174 But keep in mind that Zsh, even in Bourne shell emulation mode, performs
13175 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
13176 item @samp{$@@}, for more.
13179 @item @command{if}
13180 @c ---------------
13181 @prindex @command{if}
13182 Using @samp{!} is not portable.  Instead of:
13184 @example
13185 if ! cmp -s file file.new; then
13186   mv file.new file
13188 @end example
13190 @noindent
13191 use:
13193 @example
13194 if cmp -s file file.new; then :; else
13195   mv file.new file
13197 @end example
13199 There are shells that do not reset the exit status from an @command{if}:
13201 @example
13202 $ @kbd{if (exit 42); then true; fi; echo $?}
13204 @end example
13206 @noindent
13207 whereas a proper shell should have printed @samp{0}.  This is especially
13208 bad in makefiles since it produces false failures.  This is why properly
13209 written makefiles, such as Automake's, have such hairy constructs:
13211 @example
13212 if test -f "$file"; then
13213   install "$file" "$dest"
13214 else
13215   :
13217 @end example
13220 @item @command{printf}
13221 @c ------------------
13222 @prindex @command{printf}
13223 A format string starting with a @samp{-} can cause problems.
13224 Bash interprets it as an option and
13225 gives an error.  And @samp{--} to mark the end of options is not good
13226 in the Net@acronym{BSD} Almquist shell (e.g., 0.4.6) which takes that
13227 literally as the format string.  Putting the @samp{-} in a @samp{%c}
13228 or @samp{%s} is probably easiest:
13230 @example
13231 printf %s -foo
13232 @end example
13234 Bash 2.03 mishandles an escape sequence that happens to evaluate to @samp{%}:
13236 @example
13237 $ @kbd{printf '\045'}
13238 bash: printf: `%': missing format character
13239 @end example
13241 Large outputs may cause trouble.  On Solaris 2.5.1 through 10, for
13242 example, @file{/usr/bin/printf} is buggy, so when using
13243 @command{/bin/sh} the command @samp{printf %010000x 123} normally dumps
13244 core.
13247 @item @command{read}
13248 @c ------------------
13249 @prindex @command{read}
13250 Not all shells support @option{-r} (Solaris @command{/bin/sh} for example).
13253 @item @command{pwd}
13254 @c ----------------
13255 @prindex @command{pwd}
13256 With modern shells, plain @command{pwd} outputs a ``logical''
13257 directory name, some of whose components may be symbolic links.  These
13258 directory names are in contrast to ``physical'' directory names, whose
13259 components are all directories.
13261 Posix 1003.1-2001 requires that @command{pwd} must support
13262 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
13263 with @option{-L} being the default.  However, traditional shells do
13264 not support these options, and their @command{pwd} command has the
13265 @option{-P} behavior.
13267 Portable scripts should assume neither option is supported, and should
13268 assume neither behavior is the default.  Also, on many hosts
13269 @samp{/bin/pwd} is equivalent to @samp{pwd -P}, but Posix
13270 does not require this behavior and portable scripts should not rely on
13273 Typically it's best to use plain @command{pwd}.  On modern hosts this
13274 outputs logical directory names, which have the following advantages:
13276 @itemize @bullet
13277 @item
13278 Logical names are what the user specified.
13279 @item
13280 Physical names may not be portable from one installation
13281 host to another due to network file system gymnastics.
13282 @item
13283 On modern hosts @samp{pwd -P} may fail due to lack of permissions to
13284 some parent directory, but plain @command{pwd} cannot fail for this
13285 reason.
13286 @end itemize
13288 Also please see the discussion of the @command{cd} command.
13291 @item @command{set}
13292 @c ----------------
13293 @prindex @command{set}
13294 With the Free@acronym{BSD} 6.0 shell, the @command{set} command (without
13295 any options) does not sort its output.
13297 The @command{set} builtin faces the usual problem with arguments
13298 starting with a
13299 dash.  Modern shells such as Bash or Zsh understand @option{--} to specify
13300 the end of the options (any argument after @option{--} is a parameter,
13301 even @samp{-x} for instance), but many traditional shells (e.g., Solaris
13302 10 @command{/bin/sh}) simply stop option
13303 processing as soon as a non-option argument is found.  Therefore, use
13304 @samp{dummy} or simply @samp{x} to end the option processing, and use
13305 @command{shift} to pop it out:
13307 @example
13308 set x $my_list; shift
13309 @end example
13311 Avoid @samp{set -}, e.g., @samp{set - $my_list}.  Posix no
13312 longer requires support for this command, and in traditional shells
13313 @samp{set - $my_list} resets the @option{-v} and @option{-x} options, which
13314 makes scripts harder to debug.
13316 Some nonstandard shells do not recognize more than one option
13317 (e.g., @samp{set -e -x} assigns @samp{-x} to the command line).  It is
13318 better to combine them:
13320 @example
13321 set -ex
13322 @end example
13324 The @acronym{BSD} shell has had several problems with the @option{-e}
13325 option, partly because @acronym{BSD} @command{make} traditionally used
13326 @option{-e} even though this was incompatible with Posix
13327 (@pxref{Failure in Make Rules}).  Older versions of the @acronym{BSD}
13328 shell (circa 1990) mishandled @samp{&&}, @samp{||}, @samp{if}, and
13329 @samp{case} when @option{-e} was in effect, causing the shell to exit
13330 unexpectedly in some cases.  This was particularly a problem with
13331 makefiles, and led to circumlocutions like @samp{sh -c 'test -f file ||
13332 touch file'}, where the seemingly-unnecessary @samp{sh -c '@dots{}'}
13333 wrapper works around the bug.
13335 Even relatively-recent versions of the @acronym{BSD} shell (e.g.,
13336 Open@acronym{BSD} 3.4) wrongly exit with @option{-e} if a command within
13337 @samp{&&} fails inside a compound statement.  For example:
13339 @example
13340 #! /bin/sh
13341 set -e
13342 foo=''
13343 test -n "$foo" && exit 1
13344 echo one
13345 if :; then
13346   test -n "$foo" && exit 1
13348 echo two
13349 @end example
13351 @noindent
13352 does not print @samp{two}.  One workaround is to use @samp{if test -n
13353 "$foo"; then exit 1; fi} rather than @samp{test -n "$foo" && exit 1}.
13354 Another possibility is to warn @acronym{BSD} users not to use @samp{sh -e}.
13357 @item @command{shift}
13358 @c ------------------
13359 @prindex @command{shift}
13360 Not only is @command{shift}ing a bad idea when there is nothing left to
13361 shift, but in addition it is not portable: the shell of @acronym{MIPS
13362 RISC/OS} 4.52 refuses to do it.
13364 Don't use @samp{shift 2} etc.; it was not in the 7th Edition Bourne shell,
13365 and it is also absent in many pre-Posix shells.
13368 @item @command{source}
13369 @c -------------------
13370 @prindex @command{source}
13371 This command is not portable, as Posix does not require it; use
13372 @command{.} instead.
13375 @item @command{test}
13376 @c -----------------
13377 @prindex @command{test}
13378 The @code{test} program is the way to perform many file and string
13379 tests.  It is often invoked by the alternate name @samp{[}, but using
13380 that name in Autoconf code is asking for trouble since it is an M4 quote
13381 character.
13383 The @option{-a}, @option{-o}, @samp{(}, and @samp{)} operands are not
13384 portable and should be avoided.  Thus, portable uses of @command{test}
13385 should never have more than four arguments, and scripts should use shell
13386 constructs like @samp{&&} and @samp{||} instead.  If you combine
13387 @samp{&&} and @samp{||} in the same statement, keep in mind that they
13388 have equal precedence, so it is often better to parenthesize even when
13389 this is redundant.  For example:
13391 @smallexample
13392 # Not portable:
13393 test "X$a" = "X$b" -a \
13394   '(' "X$c" != "X$d" -o "X$e" = "X$f" ')'
13396 # Portable:
13397 test "X$a" = "X$b" &&
13398   @{ test "X$c" != "X$d" || test "X$e" = "X$f"; @}
13399 @end smallexample
13401 @command{test} does not process options like most other commands do; for
13402 example, it does not recognize the @option{--} argument as marking the
13403 end of options.
13405 It is safe to use @samp{!} as a @command{test} operator.  For example,
13406 @samp{if test ! -d foo; @dots{}} is portable even though @samp{if ! test
13407 -d foo; @dots{}} is not.
13410 @item @command{test} (files)
13411 @c -------------------------
13412 To enable @command{configure} scripts to support cross-compilation, they
13413 shouldn't do anything that tests features of the build system instead of
13414 the host system.  But occasionally you may find it necessary to check
13415 whether some arbitrary file exists.  To do so, use @samp{test -f} or
13416 @samp{test -r}.  Do not use @samp{test -x}, because 4.3@acronym{BSD} does not
13417 have it.  Do not use @samp{test -e} either, because Solaris @command{/bin/sh}
13418 lacks it.  To test for symbolic links on systems that have them, use
13419 @samp{test -h} rather than @samp{test -L}; either form conforms to
13420 Posix 1003.1-2001, but older shells like Solaris 8
13421 @code{/bin/sh} support only @option{-h}.
13423 @item @command{test} (strings)
13424 @c ---------------------------
13425 Posix says that @samp{test "@var{string}"} succeeds if @var{string} is
13426 not null, but this usage is not portable to traditional platforms like
13427 Solaris 10 @command{/bin/sh}, which mishandle strings like @samp{!} and
13428 @samp{-n}.
13430 Posix also says that @samp{test ! "@var{string}"},
13431 @samp{test -n "@var{string}"} and
13432 @samp{test -z "@var{string}"} work with any string, but many
13433 shells (such as Solaris, @acronym{AIX} 3.2, @sc{unicos} 10.0.0.6,
13434 Digital Unix 4, etc.)@: get confused if
13435 @var{string} looks like an operator:
13437 @example
13438 $ @kbd{test -n =}
13439 test: argument expected
13440 $ @kbd{test ! -n}
13441 test: argument expected
13442 @end example
13444 Similarly, Posix says that both @samp{test "@var{string1}" = "@var{string2"}}
13445 and @samp{test "@var{string1}" != "@var{string2"}} work for any pairs of
13446 strings, but in practice this is not true for troublesome strings that
13447 look like operators or parentheses, or that begin with @samp{-}.
13449 It is best to protect such strings with a leading @samp{X}, e.g.,
13450 @samp{test "X@var{string}" != X} rather than @samp{test -n
13451 "@var{string}"} or @samp{test ! "@var{string}"}.
13453 It is common to find variations of the following idiom:
13455 @example
13456 test -n "`echo $ac_feature | sed 's/[-a-zA-Z0-9_]//g'`" &&
13457   @var{action}
13458 @end example
13460 @noindent
13461 to take an action when a token matches a given pattern.  Such constructs
13462 should be avoided by using:
13464 @example
13465 case $ac_feature in
13466   *[!-a-zA-Z0-9_]*) @var{action};;
13467 esac
13468 @end example
13470 If the pattern is a complicated regular expression that cannot be
13471 expressed as a shell pattern, use something like this instead:
13473 @example
13474 expr "X$ac_feature" : 'X.*[^-a-zA-Z0-9_]' >/dev/null &&
13475   @var{action}
13476 @end example
13478 @samp{expr "X@var{foo}" : "X@var{bar}"} is more robust than @samp{echo
13479 "X@var{foo}" | grep "^X@var{bar}"}, because it avoids problems when
13480 @samp{@var{foo}} contains backslashes.
13483 @item @command{trap}
13484 @c -----------------
13485 @prindex @command{trap}
13486 It is safe to trap at least the signals 1, 2, 13, and 15.  You can also
13487 trap 0, i.e., have the @command{trap} run when the script ends (either via an
13488 explicit @command{exit}, or the end of the script).  The trap for 0 should be
13489 installed outside of a shell function, or @acronym{AIX} 5.3 @command{/bin/sh}
13490 will invoke the trap at the end of this function.
13492 Posix says that @samp{trap - 1 2 13 15} resets the traps for the
13493 specified signals to their default values, but many common shells (e.g.,
13494 Solaris @command{/bin/sh}) misinterpret this and attempt to execute a
13495 ``command'' named @command{-} when the specified conditions arise.
13496 There is no portable workaround, except for @samp{trap - 0}, for which
13497 @samp{trap '' 0} is a portable substitute.
13499 Although Posix is not absolutely clear on this point, it is widely
13500 admitted that when entering the trap @samp{$?} should be set to the exit
13501 status of the last command run before the trap.  The ambiguity can be
13502 summarized as: ``when the trap is launched by an @command{exit}, what is
13503 the @emph{last} command run: that before @command{exit}, or
13504 @command{exit} itself?''
13506 Bash considers @command{exit} to be the last command, while Zsh and
13507 Solaris @command{/bin/sh} consider that when the trap is run it is
13508 @emph{still} in the @command{exit}, hence it is the previous exit status
13509 that the trap receives:
13511 @example
13512 $ @kbd{cat trap.sh}
13513 trap 'echo $?' 0
13514 (exit 42); exit 0
13515 $ @kbd{zsh trap.sh}
13517 $ @kbd{bash trap.sh}
13519 @end example
13521 The portable solution is then simple: when you want to @samp{exit 42},
13522 run @samp{(exit 42); exit 42}, the first @command{exit} being used to
13523 set the exit status to 42 for Zsh, and the second to trigger the trap
13524 and pass 42 as exit status for Bash.
13526 The shell in Free@acronym{BSD} 4.0 has the following bug: @samp{$?} is
13527 reset to 0 by empty lines if the code is inside @command{trap}.
13529 @example
13530 $ @kbd{trap 'false}
13532 echo $?' 0
13533 $ @kbd{exit}
13535 @end example
13537 @noindent
13538 Fortunately, this bug only affects @command{trap}.
13540 @item @command{true}
13541 @c -----------------
13542 @prindex @command{true}
13543 @c Info cannot handle `:' in index entries.
13544 @c @prindex @command{:}
13545 Don't worry: as far as we know @command{true} is portable.
13546 Nevertheless, it's not always a builtin (e.g., Bash 1.x), and the
13547 portable shell community tends to prefer using @command{:}.  This has a
13548 funny side effect: when asked whether @command{false} is more portable
13549 than @command{true} Alexandre Oliva answered:
13551 @quotation
13552 In a sense, yes, because if it doesn't exist, the shell will produce an
13553 exit status of failure, which is correct for @command{false}, but not
13554 for @command{true}.
13555 @end quotation
13558 @item @command{unset}
13559 @c ------------------
13560 @prindex @command{unset}
13561 In some nonconforming shells (e.g., Bash 2.05a), @code{unset FOO} fails
13562 when @code{FOO} is not set.  Also, Bash 2.01 mishandles @code{unset
13563 MAIL} in some cases and dumps core.
13565 A few ancient shells lack @command{unset} entirely.  Nevertheless, because
13566 it is extremely useful to disable embarrassing variables such as
13567 @code{PS1}, you can test for its existence and use
13568 it @emph{provided} you give a neutralizing value when @command{unset} is
13569 not supported:
13571 @smallexample
13572 # "|| exit" suppresses any "Segmentation fault" message.
13573 if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
13574   unset=unset
13575 else
13576   unset=false
13578 $unset PS1 || PS1='$ '
13579 @end smallexample
13581 @noindent
13582 @xref{Special Shell Variables}, for some neutralizing values.  Also, see
13583 @ref{Limitations of Builtins}, documentation of @command{export}, for
13584 the case of environment variables.
13585 @end table
13587 @node Limitations of Usual Tools
13588 @section Limitations of Usual Tools
13589 @cindex Limitations of usual tools
13591 The small set of tools you can expect to find on any machine can still
13592 include some limitations you should be aware of.
13594 @table @asis
13595 @item Awk
13596 @c ------
13597 @prindex Awk
13598 Don't leave white space before the opening parenthesis in a user function call.
13599 Posix does not allow this and @acronym{GNU} Awk rejects it:
13601 @example
13602 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
13603         BEGIN @{ die () @}'}
13604 gawk: cmd. line:2:         BEGIN @{ die () @}
13605 gawk: cmd. line:2:                      ^ parse error
13606 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
13607         BEGIN @{ die() @}'}
13608 Aaaaarg!
13609 @end example
13611 Posix says that if a program contains only @samp{BEGIN} actions, and
13612 contains no instances of @code{getline}, then the program merely
13613 executes the actions without reading input.  However, traditional Awk
13614 implementations (such as Solaris 10 @command{awk}) read and discard
13615 input in this case.  Portable scripts can redirect input from
13616 @file{/dev/null} to work around the problem.  For example:
13618 @example
13619 awk 'BEGIN @{print "hello world"@}' </dev/null
13620 @end example
13622 Posix says that in an @samp{END} action, @samp{$NF} (and presumably,
13623 @samp{$1}) retain their value from the last record read, if no
13624 intervening @samp{getline} occurred.  However, some implementations
13625 (such as Solaris 10 @samp{/usr/bin/awk}, @samp{nawk}, or Darwin
13626 @samp{awk}) reset these variables.  A workaround is to use an
13627 intermediate variable prior to the @samp{END} block.  For example:
13629 @example
13630 $ @kbd{cat end.awk}
13631 @{ tmp = $1 @}
13632 END @{ print "a", $1, $NF, "b", tmp @}
13633 $ @kbd{echo 1 | awk -f end.awk}
13634 a   b 1
13635 $ @kbd{echo 1 | gawk -f end.awk}
13636 a 1 1 b 1
13637 @end example
13639 If you want your program to be deterministic, don't depend on @code{for}
13640 on arrays:
13642 @example
13643 $ @kbd{cat for.awk}
13644 END @{
13645   arr["foo"] = 1
13646   arr["bar"] = 1
13647   for (i in arr)
13648     print i
13650 $ @kbd{gawk -f for.awk </dev/null}
13653 $ @kbd{nawk -f for.awk </dev/null}
13656 @end example
13658 Some Awk implementations, such as @acronym{HP-UX} 11.0's native one,
13659 mishandle anchors:
13661 @example
13662 $ @kbd{echo xfoo | $AWK '/foo|^bar/ @{ print @}'}
13663 $ @kbd{echo bar | $AWK '/foo|^bar/ @{ print @}'}
13665 $ @kbd{echo xfoo | $AWK '/^bar|foo/ @{ print @}'}
13666 xfoo
13667 $ @kbd{echo bar | $AWK '/^bar|foo/ @{ print @}'}
13669 @end example
13671 @noindent
13672 Either do not depend on such patterns (i.e., use @samp{/^(.*foo|bar)/},
13673 or use a simple test to reject such implementations.
13675 @acronym{AIX} version 5.2 has an arbitrary limit of 399 on the
13676 length of regular expressions and literal strings in an Awk program.
13678 Traditional Awk implementations derived from Unix version 7, such as
13679 Solaris @command{/bin/awk}, have many limitations and do not
13680 conform to Posix.  Nowadays @code{AC_PROG_AWK} (@pxref{Particular
13681 Programs}) finds you an Awk that doesn't have these problems, but if
13682 for some reason you prefer not to use @code{AC_PROG_AWK} you may need to
13683 address them.
13685 Traditional Awk does not support multidimensional arrays or user-defined
13686 functions.
13688 Traditional Awk does not support the @option{-v} option.  You can use
13689 assignments after the program instead, e.g., @command{$AWK '@{print v
13690 $1@}' v=x}; however, don't forget that such assignments are not
13691 evaluated until they are encountered (e.g., after any @code{BEGIN}
13692 action).
13694 Traditional Awk does not support the keywords @code{delete} or @code{do}.
13696 Traditional Awk does not support the expressions
13697 @code{@var{a}?@var{b}:@var{c}}, @code{!@var{a}}, @code{@var{a}^@var{b}},
13698 or @code{@var{a}^=@var{b}}.
13700 Traditional Awk does not support the predefined @code{CONVFMT} variable.
13702 Traditional Awk supports only the predefined functions @code{exp}, @code{index},
13703 @code{int}, @code{length}, @code{log}, @code{split}, @code{sprintf},
13704 @code{sqrt}, and @code{substr}.
13706 Traditional Awk @code{getline} is not at all compatible with Posix;
13707 avoid it.
13709 Traditional Awk has @code{for (i in a) @dots{}} but no other uses of the
13710 @code{in} keyword.  For example, it lacks @code{if (i in a) @dots{}}.
13712 In code portable to both traditional and modern Awk, @code{FS} must be a
13713 string containing just one ordinary character, and similarly for the
13714 field-separator argument to @code{split}.
13716 Traditional Awk has a limit of 99
13717 fields in a record.  You may be able to circumvent this problem by using
13718 @code{split}.
13720 Traditional Awk has a limit of at most 99 bytes in a number formatted by
13721 @code{OFMT}; for example, @code{OFMT="%.300e"; print 0.1;} typically
13722 dumps core.
13724 The original version of Awk had a limit of at most 99 bytes per
13725 @code{split} field, 99 bytes per @code{substr} substring, and 99 bytes
13726 per run of non-special characters in a @code{printf} format, but these
13727 bugs have been fixed on all practical hosts that we know of.
13729 @item @command{basename}
13730 @c ---------------------
13731 @prindex @command{basename}
13732 Not all hosts have a working @command{basename}.
13733 You can use @command{expr} instead.
13735 @c AS_BASENAME is to be replaced by a better API.
13736 @ignore
13737 Not all hosts have a working @command{basename}, and you should instead
13738 use @code{AS_BASENAME} (@pxref{Programming in M4sh}), followed by
13739 @command{expr} if you need to strip a suffix.  For example:
13741 @example
13742 a=`basename "$aname"`       # This is not portable.
13743 a=`AS_BASENAME(["$aname"])` # This is more portable.
13745 # This is not portable.
13746 c=`basename "$cname" .c`
13748 # This is more portable.
13749 c=`AS_BASENAME(["$cname"])`
13750 case $c in
13751 ?*.c) c=`expr "X$c" : 'X\(.*\)\.c'`;;
13752 esac
13753 @end example
13754 @end ignore
13757 @item @command{cat}
13758 @c ----------------
13759 @prindex @command{cat}
13760 Don't rely on any option.
13763 @item @command{cc}
13764 @c ---------------
13765 @prindex @command{cc}
13766 The command @samp{cc -c foo.c} traditionally produces an object file
13767 named @file{foo.o}.  Most compilers allow @option{-c} to be combined
13768 with @option{-o} to specify a different object file name, but
13769 Posix does not require this combination and a few compilers
13770 lack support for it.  @xref{C Compiler}, for how @acronym{GNU} Make
13771 tests for this feature with @code{AC_PROG_CC_C_O}.
13773 When a compilation such as @samp{cc -o foo foo.c} fails, some compilers
13774 (such as @sc{cds} on Reliant Unix) leave a @file{foo.o}.
13776 @acronym{HP-UX} @command{cc} doesn't accept @file{.S} files to preprocess and
13777 assemble.  @samp{cc -c foo.S} appears to succeed, but in fact does
13778 nothing.
13780 The default executable, produced by @samp{cc foo.c}, can be
13782 @itemize
13783 @item @file{a.out} --- usual Posix convention.
13784 @item @file{b.out} --- i960 compilers (including @command{gcc}).
13785 @item @file{a.exe} --- @acronym{DJGPP} port of @command{gcc}.
13786 @item @file{a_out.exe} --- GNV @command{cc} wrapper for DEC C on OpenVMS.
13787 @item @file{foo.exe} --- various MS-DOS compilers.
13788 @end itemize
13790 The C compiler's traditional name is @command{cc}, but other names like
13791 @command{gcc} are common.  Posix 1003.1-2001 specifies the
13792 name @command{c99}, but older Posix editions specified
13793 @command{c89} and anyway these standard names are rarely used in
13794 practice.  Typically the C compiler is invoked from makefiles that use
13795 @samp{$(CC)}, so the value of the @samp{CC} make variable selects the
13796 compiler name.
13799 @item @command{chmod}
13800 @c ------------------
13801 @prindex @command{chmod}
13802 Avoid usages like @samp{chmod -w file}; use @samp{chmod a-w file}
13803 instead, for two reasons.  First, plain @option{-w} does not necessarily
13804 make the file unwritable, since it does not affect mode bits that
13805 correspond to bits in the file mode creation mask.  Second,
13806 Posix says that the @option{-w} might be interpreted as an
13807 implementation-specific option, not as a mode; Posix suggests
13808 using @samp{chmod -- -w file} to avoid this confusion, but unfortunately
13809 @samp{--} does not work on some older hosts.
13812 @item @command{cmp}
13813 @c ----------------
13814 @prindex @command{cmp}
13815 @command{cmp} performs a raw data comparison of two files, while
13816 @command{diff} compares two text files.  Therefore, if you might compare
13817 DOS files, even if only checking whether two files are different, use
13818 @command{diff} to avoid spurious differences due to differences of
13819 newline encoding.
13822 @item @command{cp}
13823 @c ---------------
13824 @prindex @command{cp}
13825 Avoid the @option{-r} option, since Posix 1003.1-2004 marks it as
13826 obsolescent and its behavior on special files is implementation-defined.
13827 Use @option{-R} instead.  On @acronym{GNU} hosts the two options
13828 are equivalent, but on Solaris hosts (for example) @command{cp -r}
13829 reads from pipes instead of replicating them.
13831 Some @command{cp} implementations (e.g., @acronym{BSD/OS} 4.2) do not allow
13832 trailing slashes at the end of nonexistent destination directories.  To
13833 avoid this problem, omit the trailing slashes.  For example, use
13834 @samp{cp -R source /tmp/newdir} rather than @samp{cp -R source
13835 /tmp/newdir/} if @file{/tmp/newdir} does not exist.
13837 @c This is thanks to Ian.
13838 The ancient SunOS 4 @command{cp} does not support @option{-f}, although
13839 its @command{mv} does.
13841 @cindex timestamp resolution
13842 Traditionally, file timestamps had 1-second resolution, and @samp{cp
13843 -p} copied the timestamps exactly.  However, many modern file systems
13844 have timestamps with 1-nanosecond resolution.  Unfortunately, @samp{cp
13845 -p} implementations truncate timestamps when copying files, so this
13846 can result in the destination file appearing to be older than the
13847 source.  The exact amount of truncation depends on the resolution of
13848 the system calls that @command{cp} uses; traditionally this was
13849 @code{utime}, which has 1-second resolution, but some newer
13850 @command{cp} implementations use @code{utimes}, which has
13851 1-microsecond resolution.  These newer implementations include @acronym{GNU}
13852 Core Utilities 5.0.91 or later, and Solaris 8 (sparc) patch 109933-02 or
13853 later.  Unfortunately as of January 2006 there is still no system
13854 call to set timestamps to the full nanosecond resolution.
13856 Bob Proulx notes that @samp{cp -p} always @emph{tries} to copy
13857 ownerships.  But whether it actually does copy ownerships or not is a
13858 system dependent policy decision implemented by the kernel.  If the
13859 kernel allows it then it happens.  If the kernel does not allow it then
13860 it does not happen.  It is not something @command{cp} itself has control
13861 over.
13863 In Unix System V any user can chown files to any other user, and System
13864 V also has a non-sticky @file{/tmp}.  That probably derives from the
13865 heritage of System V in a business environment without hostile users.
13866 @acronym{BSD} changed this
13867 to be a more secure model where only root can @command{chown} files and
13868 a sticky @file{/tmp} is used.  That undoubtedly derives from the heritage
13869 of @acronym{BSD} in a campus environment.
13871 @acronym{GNU}/Linux and Solaris by default follow @acronym{BSD}, but
13872 can be configured to allow a System V style @command{chown}.  On the
13873 other hand, @acronym{HP-UX} follows System V, but can
13874 be configured to use the modern security model and disallow
13875 @command{chown}.  Since it is an administrator-configurable parameter
13876 you can't use the name of the kernel as an indicator of the behavior.
13880 @item @command{date}
13881 @c -----------------
13882 @prindex @command{date}
13883 Some versions of @command{date} do not recognize special @samp{%} directives,
13884 and unfortunately, instead of complaining, they just pass them through,
13885 and exit with success:
13887 @example
13888 $ @kbd{uname -a}
13889 OSF1 medusa.sis.pasteur.fr V5.1 732 alpha
13890 $ @kbd{date "+%s"}
13892 @end example
13895 @item @command{diff}
13896 @c -----------------
13897 @prindex @command{diff}
13898 Option @option{-u} is nonportable.
13900 Some implementations, such as Tru64's, fail when comparing to
13901 @file{/dev/null}.  Use an empty file instead.
13904 @item @command{dirname}
13905 @c --------------------
13906 @prindex @command{dirname}
13907 Not all hosts have a working @command{dirname}, and you should instead
13908 use @code{AS_DIRNAME} (@pxref{Programming in M4sh}).  For example:
13910 @example
13911 dir=`dirname "$file"`       # This is not portable.
13912 dir=`AS_DIRNAME(["$file"])` # This is more portable.
13913 @end example
13916 @item @command{egrep}
13917 @c ------------------
13918 @prindex @command{egrep}
13919 Posix 1003.1-2001 no longer requires @command{egrep},
13920 but many hosts do not yet support the Posix
13921 replacement @code{grep -E}.  Also, some traditional implementations do
13922 not work on long input lines.  To work around these problems, invoke
13923 @code{AC_PROG_EGREP} and then use @code{$EGREP}.
13925 Portable extended regular expressions should use @samp{\} only to escape
13926 characters in the string @samp{$()*+.?[\^@{|}.  For example, @samp{\@}}
13927 is not portable, even though it typically matches @samp{@}}.
13929 The empty alternative is not portable.  Use @samp{?} instead.  For
13930 instance with Digital Unix v5.0:
13932 @example
13933 > printf "foo\n|foo\n" | $EGREP '^(|foo|bar)$'
13934 |foo
13935 > printf "bar\nbar|\n" | $EGREP '^(foo|bar|)$'
13936 bar|
13937 > printf "foo\nfoo|\n|bar\nbar\n" | $EGREP '^(foo||bar)$'
13939 |bar
13940 @end example
13942 @command{$EGREP} also suffers the limitations of @command{grep}.
13944 @item @command{expr}
13945 @c -----------------
13946 @prindex @command{expr}
13947 No @command{expr} keyword starts with @samp{X}, so use @samp{expr
13948 X"@var{word}" : 'X@var{regex}'} to keep @command{expr} from
13949 misinterpreting @var{word}.
13951 Don't use @code{length}, @code{substr}, @code{match} and @code{index}.
13953 @item @command{expr} (@samp{|})
13954 @prindex @command{expr} (@samp{|})
13955 You can use @samp{|}.  Although Posix does require that @samp{expr
13956 ''} return the empty string, it does not specify the result when you
13957 @samp{|} together the empty string (or zero) with the empty string.  For
13958 example:
13960 @example
13961 expr '' \| ''
13962 @end example
13964 Posix 1003.2-1992 returns the empty string
13965 for this case, but traditional Unix returns @samp{0} (Solaris is
13966 one such example).  In Posix 1003.1-2001, the specification was
13967 changed to match traditional Unix's behavior (which is
13968 bizarre, but it's too late to fix this).  Please note that the same
13969 problem does arise when the empty string results from a computation,
13970 as in:
13972 @example
13973 expr bar : foo \| foo : bar
13974 @end example
13976 @noindent
13977 Avoid this portability problem by avoiding the empty string.
13980 @item @command{expr} (@samp{:})
13981 @c ----------------------------
13982 @prindex @command{expr}
13983 Portable @command{expr} regular expressions should use @samp{\} to
13984 escape only characters in the string @samp{$()*.0123456789[\^n@{@}}.
13985 For example, alternation, @samp{\|}, is common but Posix does not
13986 require its support, so it should be avoided in portable scripts.
13987 Similarly, @samp{\+} and @samp{\?} should be avoided.
13989 Portable @command{expr} regular expressions should not begin with
13990 @samp{^}.  Patterns are automatically anchored so leading @samp{^} is
13991 not needed anyway.
13993 The Posix standard is ambiguous as to whether
13994 @samp{expr 'a' : '\(b\)'} outputs @samp{0} or the empty string.
13995 In practice, it outputs the empty string on most platforms, but portable
13996 scripts should not assume this.  For instance, the @acronym{QNX} 4.25 native
13997 @command{expr} returns @samp{0}.
13999 One might think that a way to get a uniform behavior would be to use
14000 the empty string as a default value:
14002 @example
14003 expr a : '\(b\)' \| ''
14004 @end example
14006 @noindent
14007 Unfortunately this behaves exactly as the original expression; see the
14008 @command{expr} (@samp{|}) entry for more information.
14010 Some ancient @command{expr} implementations (e.g., SunOS 4 @command{expr} and
14011 Solaris 8 @command{/usr/ucb/expr}) have a silly length limit that causes
14012 @command{expr} to fail if the matched substring is longer than 120
14013 bytes.  In this case, you might want to fall back on @samp{echo|sed} if
14014 @command{expr} fails.  Nowadays this is of practical importance only for
14015 the rare installer who mistakenly puts @file{/usr/ucb} before
14016 @file{/usr/bin} in @env{PATH}.
14018 On Mac OS X 10.4, @command{expr} mishandles the pattern @samp{[^-]} in
14019 some cases.  For example, the command
14020 @example
14021 expr Xpowerpc-apple-darwin8.1.0 : 'X[^-]*-[^-]*-\(.*\)'
14022 @end example
14024 @noindent
14025 outputs @samp{apple-darwin8.1.0} rather than the correct @samp{darwin8.1.0}.
14026 This particular case can be worked around by substituting @samp{[^--]}
14027 for @samp{[^-]}.
14029 Don't leave, there is some more!
14031 The @acronym{QNX} 4.25 @command{expr}, in addition of preferring @samp{0} to
14032 the empty string, has a funny behavior in its exit status: it's always 1
14033 when parentheses are used!
14035 @example
14036 $ @kbd{val=`expr 'a' : 'a'`; echo "$?: $val"}
14037 0: 1
14038 $ @kbd{val=`expr 'a' : 'b'`; echo "$?: $val"}
14039 1: 0
14041 $ @kbd{val=`expr 'a' : '\(a\)'`; echo "?: $val"}
14042 1: a
14043 $ @kbd{val=`expr 'a' : '\(b\)'`; echo "?: $val"}
14044 1: 0
14045 @end example
14047 @noindent
14048 In practice this can be a big problem if you are ready to catch failures
14049 of @command{expr} programs with some other method (such as using
14050 @command{sed}), since you may get twice the result.  For instance
14052 @example
14053 $ @kbd{expr 'a' : '\(a\)' || echo 'a' | sed 's/^\(a\)$/\1/'}
14054 @end example
14056 @noindent
14057 outputs @samp{a} on most hosts, but @samp{aa} on @acronym{QNX} 4.25.  A
14058 simple workaround consists of testing @command{expr} and using a variable
14059 set to @command{expr} or to @command{false} according to the result.
14061 Tru64 @command{expr} incorrectly treats the result as a number, if it
14062 can be interpreted that way:
14064 @example
14065 $ @kbd{expr 00001 : '.*\(...\)'}
14067 @end example
14070 @item @command{fgrep}
14071 @c ------------------
14072 @prindex @command{fgrep}
14073 Posix 1003.1-2001 no longer requires @command{fgrep},
14074 but many hosts do not yet support the Posix
14075 replacement @code{grep -F}.  Also, some traditional implementations do
14076 not work on long input lines.  To work around these problems, invoke
14077 @code{AC_PROG_FGREP} and then use @code{$FGREP}.
14080 @item @command{find}
14081 @c -----------------
14082 @prindex @command{find}
14083 The option @option{-maxdepth} seems to be @acronym{GNU} specific.
14084 Tru64 v5.1, Net@acronym{BSD} 1.5 and Solaris @command{find}
14085 commands do not understand it.
14087 The replacement of @samp{@{@}} is guaranteed only if the argument is
14088 exactly @emph{@{@}}, not if it's only a part of an argument.  For
14089 instance on DU, and @acronym{HP-UX} 10.20 and @acronym{HP-UX} 11:
14091 @example
14092 $ @kbd{touch foo}
14093 $ @kbd{find . -name foo -exec echo "@{@}-@{@}" \;}
14094 @{@}-@{@}
14095 @end example
14097 @noindent
14098 while @acronym{GNU} @command{find} reports @samp{./foo-./foo}.
14101 @item @command{grep}
14102 @c -----------------
14103 @prindex @command{grep}
14104 Portable scripts can rely on the @command{grep} options @option{-c},
14105 @option{-l}, @option{-n}, and @option{-v}, but should avoid other
14106 options.  For example, don't use @option{-w}, as Posix does not require
14107 it and Irix 6.5.16m's @command{grep} does not support it.  Also,
14108 portable scripts should not combine @option{-c} with @option{-l},
14109 as Posix does not allow this.
14111 Some of the options required by Posix are not portable in practice.
14112 Don't use @samp{grep -q} to suppress output, because many @command{grep}
14113 implementations (e.g., Solaris) do not support @option{-q}.
14114 Don't use @samp{grep -s} to suppress output either, because Posix
14115 says @option{-s} does not suppress output, only some error messages;
14116 also, the @option{-s} option of traditional @command{grep} behaved
14117 like @option{-q} does in most modern implementations.  Instead,
14118 redirect the standard output and standard error (in case the file
14119 doesn't exist) of @code{grep} to @file{/dev/null}.  Check the exit
14120 status of @code{grep} to determine whether it found a match.
14122 Some traditional @command{grep} implementations do not work on long
14123 input lines.  On AIX the default @code{grep} silently truncates long
14124 lines on the input before matching.
14126 Also, many implementations do not support multiple regexps
14127 with @option{-e}: they either reject @option{-e} entirely (e.g., Solaris)
14128 or honor only the last pattern (e.g., @acronym{IRIX} 6.5 and NeXT).  To
14129 work around these problems, invoke @code{AC_PROG_GREP} and then use
14130 @code{$GREP}.
14132 Another possible workaround for the multiple @option{-e} problem is to
14133 separate the patterns by newlines, for example:
14135 @example
14136 grep 'foo
14137 bar' in.txt
14138 @end example
14140 @noindent
14141 except that this fails with traditional @command{grep}
14142 implementations and with Open@acronym{BSD} 3.8 @command{grep}.
14144 Traditional @command{grep} implementations (e.g., Solaris) do not
14145 support the @option{-E} or @option{-F} options.  To work around these
14146 problems, invoke @code{AC_PROG_EGREP} and then use @code{$EGREP}, and
14147 similarly for @code{AC_PROG_FGREP} and @code{$FGREP}.  Even if you are
14148 willing to require support for Posix @command{grep}, your script should
14149 not use both @option{-E} and @option{-F}, since Posix does not allow
14150 this combination.
14152 Portable @command{grep} regular expressions should use @samp{\} only to
14153 escape characters in the string @samp{$()*.0123456789[\^@{@}}.  For example,
14154 alternation, @samp{\|}, is common but Posix does not require its
14155 support in basic regular expressions, so it should be avoided in
14156 portable scripts.  Solaris and HP-UX @command{grep} do not support it.
14157 Similarly, the following escape sequences should also be avoided:
14158 @samp{\<}, @samp{\>}, @samp{\+}, @samp{\?}, @samp{\`}, @samp{\'},
14159 @samp{\B}, @samp{\b}, @samp{\S}, @samp{\s}, @samp{\W}, and @samp{\w}.
14162 @item @command{join}
14163 @c -----------------
14164 @prindex @command{join}
14165 Solaris 8 @command{join} has bugs when the second operand is standard
14166 input, and when standard input is a pipe.  For example, the following
14167 shell script causes Solaris 8 @command{join} to loop forever:
14169 @example
14170 cat >file <<'EOF'
14171 1 x
14172 2 y
14174 cat file | join file -
14175 @end example
14177 Use @samp{join - file} instead.
14180 @item @command{ln}
14181 @c ---------------
14182 @prindex @command{ln}
14183 @cindex Symbolic links
14184 Don't rely on @command{ln} having a @option{-f} option.  Symbolic links
14185 are not available on old systems; use @samp{$(LN_S)} as a portable substitute.
14187 For versions of the @acronym{DJGPP} before 2.04,
14188 @command{ln} emulates symbolic links
14189 to executables by generating a stub that in turn calls the real
14190 program.  This feature also works with nonexistent files like in the
14191 Posix spec.  So @samp{ln -s file link} generates @file{link.exe},
14192 which attempts to call @file{file.exe} if run.  But this feature only
14193 works for executables, so @samp{cp -p} is used instead for these
14194 systems.  @acronym{DJGPP} versions 2.04 and later have full support
14195 for symbolic links.
14198 @item @command{ls}
14199 @c ---------------
14200 @prindex @command{ls}
14201 @cindex Listing directories
14202 The portable options are @option{-acdilrtu}.  Current practice is for
14203 @option{-l} to output both owner and group, even though ancient versions
14204 of @command{ls} omitted the group.
14206 On ancient hosts, @samp{ls foo} sent the diagnostic @samp{foo not found}
14207 to standard output if @file{foo} did not exist.  Hence a shell command
14208 like @samp{sources=`ls *.c 2>/dev/null`} did not always work, since it
14209 was equivalent to @samp{sources='*.c not found'} in the absence of
14210 @samp{.c} files.  This is no longer a practical problem, since current
14211 @command{ls} implementations send diagnostics to standard error.
14213 @item @command{mkdir}
14214 @c ------------------
14215 @prindex @command{mkdir}
14216 @cindex Making directories
14217 No @command{mkdir} option is portable to older systems.  Instead of
14218 @samp{mkdir -p @var{file-name}}, you should use
14219 @code{AS_MKDIR_P(@var{file-name})} (@pxref{Programming in M4sh})
14220 or @code{AC_PROG_MKDIR_P} (@pxref{Particular Programs}).
14222 Combining the @option{-m} and @option{-p} options, as in @samp{mkdir -m
14223 go-w -p @var{dir}}, often leads to trouble.  Free@acronym{BSD}
14224 @command{mkdir} incorrectly attempts to change the permissions of
14225 @var{dir} even if it already exists.  @acronym{HP-UX} 11.23 and
14226 @acronym{IRIX} 6.5 @command{mkdir} often assign the wrong permissions to
14227 any newly-created parents of @var{dir}.
14229 Posix does not clearly specify whether @samp{mkdir -p foo}
14230 should succeed when @file{foo} is a symbolic link to an already-existing
14231 directory.  The @acronym{GNU} Core Utilities 5.1.0 @command{mkdir}
14232 succeeds, but Solaris @command{mkdir} fails.
14234 Traditional @code{mkdir -p} implementations suffer from race conditions.
14235 For example, if you invoke @code{mkdir -p a/b} and @code{mkdir -p a/c}
14236 at the same time, both processes might detect that @file{a} is missing,
14237 one might create @file{a}, then the other might try to create @file{a}
14238 and fail with a @code{File exists} diagnostic.  The @acronym{GNU} Core
14239 Utilities (@samp{fileutils} version 4.1), Free@acronym{BSD} 5.0,
14240 Net@acronym{BSD} 2.0.2, and Open@acronym{BSD} 2.4 are known to be
14241 race-free when two processes invoke @code{mkdir -p} simultaneously, but
14242 earlier versions are vulnerable.  Solaris @command{mkdir} is still
14243 vulnerable as of Solaris 10, and other traditional Unix systems are
14244 probably vulnerable too.  This possible race is harmful in parallel
14245 builds when several Make rules call @code{mkdir -p} to
14246 construct directories.  You may use
14247 @code{install-sh -d} as a safe replacement, provided this script is
14248 recent enough; the copy shipped with Autoconf 2.60 and Automake 1.10 is
14249 OK, but copies from older versions are vulnerable.
14252 @item @command{mktemp}
14253 @c -------------------
14254 @prindex @command{mktemp}
14255 @cindex Creating temporary files
14256 Shell scripts can use temporary files safely with @command{mktemp}, but
14257 it does not exist on all systems.  A portable way to create a safe
14258 temporary file name is to create a temporary directory with mode 700 and
14259 use a file inside this directory.  Both methods prevent attackers from
14260 gaining control, though @command{mktemp} is far less likely to fail
14261 gratuitously under attack.
14263 Here is sample code to create a new temporary directory safely:
14265 @example
14266 # Create a temporary directory $tmp in $TMPDIR (default /tmp).
14267 # Use mktemp if possible; otherwise fall back on mkdir,
14268 # with $RANDOM to make collisions less likely.
14269 : $@{TMPDIR=/tmp@}
14271   tmp=`
14272     (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
14273   ` &&
14274   test -n "$tmp" && test -d "$tmp"
14275 @} || @{
14276   tmp=$TMPDIR/foo$$-$RANDOM
14277   (umask 077 && mkdir "$tmp")
14278 @} || exit $?
14279 @end example
14282 @item @command{mv}
14283 @c ---------------
14284 @prindex @command{mv}
14285 @cindex Moving open files
14286 The only portable options are @option{-f} and @option{-i}.
14288 Moving individual files between file systems is portable (it was in Unix
14289 version 6),
14290 but it is not always atomic: when doing @samp{mv new existing}, there's
14291 a critical section where neither the old nor the new version of
14292 @file{existing} actually exists.
14294 On some systems moving files from @file{/tmp} can sometimes cause
14295 undesirable (but perfectly valid) warnings, even if you created these
14296 files.  This is because @file{/tmp} belongs to a group that ordinary
14297 users are not members of, and files created in @file{/tmp} inherit
14298 the group of @file{/tmp}.  When the file is copied, @command{mv} issues
14299 a diagnostic without failing:
14301 @smallexample
14302 $ @kbd{touch /tmp/foo}
14303 $ @kbd{mv /tmp/foo .}
14304 @error{}mv: ./foo: set owner/group (was: 100/0): Operation not permitted
14305 $ @kbd{echo $?}
14307 $ @kbd{ls foo}
14309 @end smallexample
14311 @noindent
14312 This annoying behavior conforms to Posix, unfortunately.
14314 Moving directories across mount points is not portable, use @command{cp}
14315 and @command{rm}.
14317 @acronym{DOS} variants cannot rename or remove open files, and do not
14318 support commands like @samp{mv foo bar >foo}, even though this is
14319 perfectly portable among Posix hosts.
14322 @item @command{od}
14323 @c ---------------
14324 @prindex @command{od}
14326 In Mac OS X 10.3, @command{od} does not support the
14327 standard Posix options @option{-A}, @option{-j}, @option{-N}, or
14328 @option{-t}, or the @acronym{XSI} option @option{-s}.  The only
14329 supported Posix option is @option{-v}, and the only supported
14330 @acronym{XSI} options are those in @option{-bcdox}.  The @acronym{BSD}
14331 @command{hexdump} program can be used instead.
14333 This problem no longer exists in Mac OS X 10.4.3.
14336 @item @command{rm}
14337 @c ---------------
14338 @prindex @command{rm}
14339 The @option{-f} and @option{-r} options are portable.
14341 It is not portable to invoke @command{rm} without operands.  For
14342 example, on many systems @samp{rm -f -r} (with no other arguments)
14343 silently succeeds without doing anything, but it fails with a diagnostic
14344 on Net@acronym{BSD} 2.0.2.
14346 A file might not be removed even if its parent directory is writable
14347 and searchable.  Many Posix hosts cannot remove a mount point, a named
14348 stream, a working directory, or a last link to a file that is being
14349 executed.
14351 @acronym{DOS} variants cannot rename or remove open files, and do not
14352 support commands like @samp{rm foo >foo}, even though this is
14353 perfectly portable among Posix hosts.
14356 @item @command{sed}
14357 @c ----------------
14358 @prindex @command{sed}
14359 Patterns should not include the separator (unless escaped), even as part
14360 of a character class.  In conformance with Posix, the Cray
14361 @command{sed} rejects @samp{s/[^/]*$//}: use @samp{s,[^/]*$,,}.
14363 Avoid empty patterns within parentheses (i.e., @samp{\(\)}).  Posix does
14364 not require support for empty patterns, and Unicos 9 @command{sed} rejects
14365 them.
14367 Unicos 9 @command{sed} loops endlessly on patterns like @samp{.*\n.*}.
14369 Sed scripts should not use branch labels longer than 7 characters and
14370 should not contain comments.  @acronym{HP-UX} sed has a limit of 99 commands
14371 (not counting @samp{:} commands) and
14372 48 labels, which can not be circumvented by using more than one script
14373 file.  It can execute up to 19 reads with the @samp{r} command per cycle.
14374 Solaris @command{/usr/ucb/sed} rejects usages that exceed an limit of
14375 about 6000 bytes for the internal representation of commands.
14377 Avoid redundant @samp{;}, as some @command{sed} implementations, such as
14378 Net@acronym{BSD} 1.4.2's, incorrectly try to interpret the second
14379 @samp{;} as a command:
14381 @example
14382 $ @kbd{echo a | sed 's/x/x/;;s/x/x/'}
14383 sed: 1: "s/x/x/;;s/x/x/": invalid command code ;
14384 @end example
14386 Input should not have unreasonably long lines, since some @command{sed}
14387 implementations have an input buffer limited to 4000 bytes.
14389 Portable @command{sed} regular expressions should use @samp{\} only to escape
14390 characters in the string @samp{$()*.0123456789[\^n@{@}}.  For example,
14391 alternation, @samp{\|}, is common but Posix does not require its
14392 support, so it should be avoided in portable scripts.  Solaris
14393 @command{sed} does not support alternation; e.g., @samp{sed '/a\|b/d'}
14394 deletes only lines that contain the literal string @samp{a|b}.
14395 Similarly, @samp{\+} and @samp{\?} should be avoided.
14397 Anchors (@samp{^} and @samp{$}) inside groups are not portable.
14399 Nested parentheses in patterns (e.g., @samp{\(\(a*\)b*)\)}) are
14400 quite portable to current hosts, but was not supported by some ancient
14401 @command{sed} implementations like SVR3.
14403 Some @command{sed} implementations, e.g., Solaris,
14404 restrict the special role of the asterisk to one-character regular expressions.
14405 This may lead to unexpected behavior:
14407 @example
14408 $ @kbd{echo '1*23*4' | /usr/bin/sed 's/\(.\)*/x/g'}
14409 x2x4
14410 $ @kbd{echo '1*23*4' | /usr/xpg4/bin/sed 's/\(.\)*/x/g'}
14412 @end example
14414 The @option{-e} option is mostly portable.
14415 However, its argument
14416 cannot start with @samp{a}, @samp{c}, or @samp{i},
14417 as this runs afoul of a Tru64 5.1 bug.
14418 Also, its argument cannot be empty, as this fails on @acronym{AIX} 5.3.
14419 Some people prefer to use @samp{-e}:
14421 @example
14422 sed -e '@var{command-1}' \
14423     -e '@var{command-2}'
14424 @end example
14426 @noindent
14427 as opposed to the equivalent:
14429 @example
14430 sed '
14431   @var{command-1}
14432   @var{command-2}
14434 @end example
14436 @noindent
14437 The following usage is sometimes equivalent:
14439 @example
14440 sed '@var{command-1};@var{command-2}'
14441 @end example
14443 but Posix says that this use of a semicolon has undefined effect if
14444 @var{command-1}'s verb is @samp{@{}, @samp{a}, @samp{b}, @samp{c},
14445 @samp{i}, @samp{r}, @samp{t}, @samp{w}, @samp{:}, or @samp{#}, so you
14446 should use semicolon only with simple scripts that do not use these
14447 verbs.
14449 Commands inside @{ @} brackets are further restricted.  Posix says that
14450 they cannot be preceded by addresses, @samp{!}, or @samp{;}, and that
14451 each command must be followed immediately by a newline, without any
14452 intervening blanks or semicolons.  The closing bracket must be alone on
14453 a line, other than white space preceding or following it.
14455 Contrary to yet another urban legend, you may portably use @samp{&} in
14456 the replacement part of the @code{s} command to mean ``what was
14457 matched''.  All descendants of Unix version 7 @command{sed}
14458 (at least; we
14459 don't have first hand experience with older @command{sed} implementations) have
14460 supported it.
14462 Posix requires that you must not have any white space between
14463 @samp{!} and the following command.  It is OK to have blanks between
14464 the address and the @samp{!}.  For instance, on Solaris:
14466 @example
14467 $ @kbd{echo "foo" | sed -n '/bar/ ! p'}
14468 @error{}Unrecognized command: /bar/ ! p
14469 $ @kbd{echo "foo" | sed -n '/bar/! p'}
14470 @error{}Unrecognized command: /bar/! p
14471 $ @kbd{echo "foo" | sed -n '/bar/ !p'}
14473 @end example
14475 Posix also says that you should not combine @samp{!} and @samp{;}.  If
14476 you use @samp{!}, it is best to put it on a command that is delimited by
14477 newlines rather than @samp{;}.
14479 Also note that Posix requires that the @samp{b}, @samp{t}, @samp{r}, and
14480 @samp{w} commands be followed by exactly one space before their argument.
14481 On the other hand, no white space is allowed between @samp{:} and the
14482 subsequent label name.
14484 If a sed script is specified on the command line and ends in an
14485 @samp{a}, @samp{c}, or @samp{i} command, the last line of inserted text
14486 should be followed by a newline.  Otherwise some @command{sed}
14487 implementations (e.g., Open@acronym{BSD} 3.9) do not append a newline to the
14488 inserted text.
14490 Many @command{sed} implementations (e.g., MacOS X 10.4,
14491 Open@acronym{BSD} 3.9, Solaris 10
14492 @command{/usr/ucb/sed}) strip leading white space from the text of
14493 @samp{a}, @samp{c}, and @samp{i} commands.  Prepend a backslash to
14494 work around this incompatibility with Posix:
14496 @example
14497 $ @kbd{echo flushleft | sed 'a\}
14498 > @kbd{   indented}
14499 > @kbd{'}
14500 flushleft
14501 indented
14502 $ @kbd{echo foo | sed 'a\}
14503 > @kbd{\   indented}
14504 > @kbd{'}
14505 flushleft
14506    indented
14507 @end example
14510 @item @command{sed} (@samp{t})
14511 @c ---------------------------
14512 @prindex @command{sed} (@samp{t})
14513 Some old systems have @command{sed} that ``forget'' to reset their
14514 @samp{t} flag when starting a new cycle.  For instance on @acronym{MIPS
14515 RISC/OS}, and on @sc{irix} 5.3, if you run the following @command{sed}
14516 script (the line numbers are not actual part of the texts):
14518 @example
14519 s/keep me/kept/g  # a
14520 t end             # b
14521 s/.*/deleted/g    # c
14522 :end              # d
14523 @end example
14525 @noindent
14528 @example
14529 delete me         # 1
14530 delete me         # 2
14531 keep me           # 3
14532 delete me         # 4
14533 @end example
14535 @noindent
14536 you get
14538 @example
14539 deleted
14540 delete me
14541 kept
14542 deleted
14543 @end example
14545 @noindent
14546 instead of
14548 @example
14549 deleted
14550 deleted
14551 kept
14552 deleted
14553 @end example
14555 Why?  When processing line 1, (c) matches, therefore sets the @samp{t}
14556 flag, and the output is produced.  When processing
14557 line 2, the @samp{t} flag is still set (this is the bug).  Command (a)
14558 fails to match, but @command{sed} is not supposed to clear the @samp{t}
14559 flag when a substitution fails.  Command (b) sees that the flag is set,
14560 therefore it clears it, and jumps to (d), hence you get @samp{delete me}
14561 instead of @samp{deleted}.  When processing line (3), @samp{t} is clear,
14562 (a) matches, so the flag is set, hence (b) clears the flags and jumps.
14563 Finally, since the flag is clear, line 4 is processed properly.
14565 There are two things one should remember about @samp{t} in @command{sed}.
14566 Firstly, always remember that @samp{t} jumps if @emph{some} substitution
14567 succeeded, not only the immediately preceding substitution.  Therefore,
14568 always use a fake @samp{t clear} followed by a @samp{:clear} on the next
14569 line, to reset the @samp{t} flag where needed.
14571 Secondly, you cannot rely on @command{sed} to clear the flag at each new
14572 cycle.
14574 One portable implementation of the script above is:
14576 @example
14577 t clear
14578 :clear
14579 s/keep me/kept/g
14580 t end
14581 s/.*/deleted/g
14582 :end
14583 @end example
14585 @item @command{touch}
14586 @c ------------------
14587 @prindex @command{touch}
14588 @cindex timestamp resolution
14589 If you specify the desired timestamp (e.g., with the @option{-r}
14590 option), @command{touch} typically uses the @code{utime} or
14591 @code{utimes} system call, which can result in the same kind of
14592 timestamp truncation problems that @samp{cp -p} has.
14594 On ancient @acronym{BSD} systems, @command{touch} or any command that
14595 results in an empty file does not update the timestamps, so use a
14596 command like @command{echo} as a workaround.
14597 Also,
14598 @acronym{GNU} @command{touch} 3.16r (and presumably all before that)
14599 fails to work on SunOS 4.1.3 when the empty file is on an
14600 @acronym{NFS}-mounted 4.2 volume.
14601 However, these problems are no longer of practical concern.
14603 @end table
14606 @node Portable Make
14607 @chapter Portable Make Programming
14608 @prindex @command{make}
14609 @cindex Limitations of @command{make}
14611 Writing portable makefiles is an art.  Since a makefile's commands are
14612 executed by the shell, you must consider the shell portability issues
14613 already mentioned.  However, other issues are specific to @command{make}
14614 itself.
14616 @menu
14617 * $< in Ordinary Make Rules::   $< in ordinary rules
14618 * Failure in Make Rules::       Failing portably in rules
14619 * Special Chars in Names::      Special Characters in Macro Names
14620 * Backslash-Newline-Newline::   Empty last lines in macro definitions
14621 * Backslash-Newline Comments::  Spanning comments across line boundaries
14622 * Long Lines in Makefiles::     Line length limitations
14623 * Macros and Submakes::         @code{make macro=value} and submakes
14624 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
14625 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
14626 * Comments in Make Rules::      Other problems with Make comments
14627 * obj/ and Make::               Don't name a subdirectory @file{obj}
14628 * make -k Status::              Exit status of @samp{make -k}
14629 * VPATH and Make::              @code{VPATH} woes
14630 * Single Suffix Rules::         Single suffix rules and separated dependencies
14631 * Timestamps and Make::         Subsecond timestamp resolution
14632 @end menu
14634 @node $< in Ordinary Make Rules
14635 @section @code{$<} in Ordinary Make Rules
14637 Posix says that the @samp{$<} construct in makefiles can be
14638 used only in inference rules and in the @samp{.DEFAULT} rule; its
14639 meaning in ordinary rules is unspecified.  Solaris @command{make}
14640 for instance replaces it with the empty string.  Open@acronym{BSD} (3.0 and
14641 later) @command{make} diagnoses these uses and errors out.
14643 @node Failure in Make Rules
14644 @section Failure in Make Rules
14646 Since 1992 Posix has required that @command{make} must invoke
14647 each command with the equivalent of a @samp{sh -c} subshell.  However,
14648 many @command{make} implementations, including @acronym{BSD} make through 2004,
14649 use @samp{sh -e -c} instead, and the @option{-e} option causes the
14650 subshell to exit immediately if a subsidiary simple-command fails.  For
14651 example, the command @samp{touch T; rm -f U} always attempts to
14652 remove @file{U} with Posix make, but incompatible
14653 @command{make} implementations skip the @command{rm} if the
14654 @command{touch} fails.  One way to work around this is to reword the
14655 affected simple-commands so that they always succeed, e.g., @samp{touch
14656 T || :; rm -f U}.
14657 However, even this approach can run into common bugs in @acronym{BSD}
14658 implementations of the @option{-e} option of @command{sh} and
14659 @command{set} (@pxref{Limitations of Builtins}), so if you are worried
14660 about porting to buggy @acronym{BSD} shells it may be simpler to migrate
14661 complicated @command{make} actions into separate scripts.
14663 @node Special Chars in Names
14664 @section Special Characters in Make Macro Names
14666 Posix limits macro names to nonempty strings containing only
14667 @acronym{ASCII} letters and digits, @samp{.}, and @samp{_}.  Many
14668 @command{make} implementations allow a wider variety of characters, but
14669 portable makefiles should avoid them.  It is portable to start a name
14670 with a special character, e.g., @samp{$(.FOO)}.
14672 Some ancient @command{make} implementations don't support leading
14673 underscores in macro names.  An example is @acronym{NEWS-OS} 4.2R.
14675 @example
14676 $ @kbd{cat Makefile}
14677 _am_include = #
14678 _am_quote =
14679 all:; @@echo this is test
14680 $ @kbd{make}
14681 Make: Must be a separator on rules line 2.  Stop.
14682 $ @kbd{cat Makefile2}
14683 am_include = #
14684 am_quote =
14685 all:; @@echo this is test
14686 $ @kbd{make -f Makefile2}
14687 this is test
14688 @end example
14690 @noindent
14691 However, this problem is no longer of practical concern.
14693 @node Backslash-Newline-Newline
14694 @section Backslash-Newline-Newline in Make Macro Values
14696 @c  This has been seen on ia64 hpux 11.20, and on one hppa hpux 10.20,
14697 @c  but another hppa hpux 10.20 didn't have it.  Bob Proulx
14698 @c  <bob@proulx.com> thinks it was in hpux 8.0 too.
14699 On some versions of @acronym{HP-UX}, @command{make} reads multiple newlines
14700 following a backslash, continuing to the next non-empty line.  For
14701 example,
14703 @example
14704 FOO = one \
14706 BAR = two
14708 test:
14709         : FOO is "$(FOO)"
14710         : BAR is "$(BAR)"
14711 @end example
14713 @noindent
14714 shows @code{FOO} equal to @code{one BAR = two}.  Other implementations
14715 sensibly let a backslash continue only to the immediately following
14716 line.
14718 @node Backslash-Newline Comments
14719 @section Backslash-Newline in Make Comments
14721 According to Posix, Make comments start with @code{#}
14722 and continue until an unescaped newline is reached.
14724 @example
14725 $ @kbd{cat Makefile}
14726 # A = foo \
14727       bar \
14728       baz
14730 all:
14731         @@echo ok
14732 $ @kbd{make}   # GNU make
14734 @end example
14736 @noindent
14737 However this is not always the case.  Some implementations
14738 discard everything from @code{#} through the end of the line, ignoring any
14739 trailing backslash.
14741 @example
14742 $ @kbd{pmake}  # BSD make
14743 "Makefile", line 3: Need an operator
14744 Fatal errors encountered -- cannot continue
14745 @end example
14747 @noindent
14748 Therefore, if you want to comment out a multi-line definition, prefix each
14749 line with @code{#}, not only the first.
14751 @example
14752 # A = foo \
14753 #     bar \
14754 #     baz
14755 @end example
14757 @node Long Lines in Makefiles
14758 @section Long Lines in Makefiles
14760 Tru64 5.1's @command{make} has been reported to crash when given a
14761 makefile with lines longer than around 20 kB.  Earlier versions are
14762 reported to exit with @code{Line too long} diagnostics.
14764 @node Macros and Submakes
14765 @section @code{make macro=value} and Submakes
14767 A command-line variable definition such as @code{foo=bar} overrides any
14768 definition of @code{foo} in a makefile.  Some @command{make}
14769 implementations (such as @acronym{GNU} @command{make}) propagate this
14770 override to subsidiary invocations of @command{make}.  Some other
14771 implementations do not pass the substitution along to submakes.
14773 @example
14774 $ @kbd{cat Makefile}
14775 foo = foo
14776 one:
14777         @@echo $(foo)
14778         $(MAKE) two
14779 two:
14780         @@echo $(foo)
14781 $ @kbd{make foo=bar}            # GNU make 3.79.1
14783 make two
14784 make[1]: Entering directory `/home/adl'
14786 make[1]: Leaving directory `/home/adl'
14787 $ @kbd{pmake foo=bar}           # BSD make
14789 pmake two
14791 @end example
14793 You have a few possibilities if you do want the @code{foo=bar} override
14794 to propagate to submakes.  One is to use the @option{-e}
14795 option, which causes all environment variables to have precedence over
14796 the makefile macro definitions, and declare foo as an environment
14797 variable:
14799 @example
14800 $ @kbd{env foo=bar make -e}
14801 @end example
14803 The @option{-e} option is propagated to submakes automatically,
14804 and since the environment is inherited between @command{make}
14805 invocations, the @code{foo} macro is overridden in
14806 submakes as expected.
14808 This syntax (@code{foo=bar make -e}) is portable only when used
14809 outside of a makefile, for instance from a script or from the
14810 command line.  When run inside a @command{make} rule, @acronym{GNU}
14811 @command{make} 3.80 and prior versions forget to propagate the
14812 @option{-e} option to submakes.
14814 Moreover, using @option{-e} could have unexpected side effects if your
14815 environment contains some other macros usually defined by the
14816 makefile.  (See also the note about @code{make -e} and @code{SHELL}
14817 below.)
14819 Another way to propagate overrides to submakes is to do it
14820 manually, from your makefile:
14822 @example
14823 foo = foo
14824 one:
14825         @@echo $(foo)
14826         $(MAKE) foo=$(foo) two
14827 two:
14828         @@echo $(foo)
14829 @end example
14831 You need to foresee all macros that a user might want to override if
14832 you do that.
14834 @node The Make Macro MAKEFLAGS
14835 @section The Make Macro MAKEFLAGS
14836 @cindex @code{MAKEFLAGS} and @command{make}
14837 @cindex @command{make} and @code{MAKEFLAGS}
14839 Posix requires @command{make} to use @code{MAKEFLAGS} to affect the
14840 current and recursive invocations of make, but allows implementations
14841 several formats for the variable.  It is tricky to parse
14842 @code{$MAKEFLAGS} to determine whether @option{-s} for silent execution
14843 or @option{-k} for continued execution are in effect.  For example, you
14844 cannot assume that the first space-separated word in @code{$MAKEFLAGS}
14845 contains single-letter options, since in the Cygwin version of
14846 @acronym{GNU} @command{make} it is either @option{--unix} or
14847 @option{--win32} with the second word containing single-letter options.
14849 @example
14850 $ @kbd{cat Makefile}
14851 all:
14852         @@echo MAKEFLAGS = $(MAKEFLAGS)
14853 $ @kbd{make}
14854 MAKEFLAGS = --unix
14855 $ @kbd{make -k}
14856 MAKEFLAGS = --unix -k
14857 @end example
14859 @node The Make Macro SHELL
14860 @section The Make Macro @code{SHELL}
14861 @cindex @code{SHELL} and @command{make}
14862 @cindex @command{make} and @code{SHELL}
14864 Posix-compliant @command{make} internally uses the @code{$(SHELL)}
14865 macro to spawn shell processes and execute Make rules.  This
14866 is a builtin macro supplied by @command{make}, but it can be modified
14867 by a makefile or by a command-line argument.
14869 Not all @command{make} implementations define this @code{SHELL} macro.
14870 Tru64
14871 @command{make} is an example; this implementation always uses
14872 @code{/bin/sh}.  So it's a good idea to always define @code{SHELL} in
14873 your makefiles.  If you use Autoconf, do
14875 @example
14876 SHELL = @@SHELL@@
14877 @end example
14879 Do not force @code{SHELL = /bin/sh} because that is not correct
14880 everywhere.  For instance @acronym{DJGPP} lacks @code{/bin/sh}, and when
14881 its @acronym{GNU} @code{make} port sees such a setting it enters a special
14882 emulation mode where features like pipes and redirections are emulated
14883 on top of DOS's @command{command.com}.  Unfortunately this emulation is
14884 incomplete; for instance it does not handle command substitutions.
14885 On @acronym{DJGPP} @code{SHELL} should point to Bash.
14887 Posix-compliant @command{make} should never acquire the value of
14888 $(SHELL) from the environment, even when @code{make -e} is used
14889 (otherwise, think about what would happen to your rules if
14890 @code{SHELL=/bin/tcsh}).
14892 However not all @command{make} implementations have this exception.
14893 For instance it's not surprising that Tru64 @command{make} doesn't
14894 protect @code{SHELL}, since it doesn't use it.
14896 @example
14897 $ @kbd{cat Makefile}
14898 SHELL = /bin/sh
14899 FOO = foo
14900 all:
14901         @@echo $(SHELL)
14902         @@echo $(FOO)
14903 $ @kbd{env SHELL=/bin/tcsh FOO=bar make -e}   # Tru64 Make
14904 /bin/tcsh
14906 $ @kbd{env SHELL=/bin/tcsh FOO=bar gmake -e}  # GNU make
14907 /bin/sh
14909 @end example
14911 @node Comments in Make Rules
14912 @section Comments in Make Rules
14913 @cindex Comments in @file{Makefile} rules
14914 @cindex @file{Makefile} rules and comments
14916 Never put comments in a rule.
14918 Some @command{make} treat anything starting with a tab as a command for
14919 the current rule, even if the tab is immediately followed by a @code{#}.
14920 The @command{make} from Tru64 Unix V5.1 is one of them.  The following
14921 makefile runs @code{# foo} through the shell.
14923 @example
14924 all:
14925         # foo
14926 @end example
14928 @node obj/ and Make
14929 @section The @file{obj/} Subdirectory and Make
14930 @cindex @file{obj/}, subdirectory
14931 @cindex @acronym{BSD} @command{make} and @file{obj/}
14933 Never name one of your subdirectories @file{obj/} if you don't like
14934 surprises.
14936 If an @file{obj/} directory exists, @acronym{BSD} @command{make} enters it
14937 before reading the makefile.  Hence the makefile in the
14938 current directory is not read.
14940 @example
14941 $ @kbd{cat Makefile}
14942 all:
14943         echo Hello
14944 $ @kbd{cat obj/Makefile}
14945 all:
14946         echo World
14947 $ @kbd{make}      # GNU make
14948 echo Hello
14949 Hello
14950 $ @kbd{pmake}     # BSD make
14951 echo World
14952 World
14953 @end example
14955 @node make -k Status
14956 @section Exit Status of @code{make -k}
14957 @cindex @code{make -k}
14959 Do not rely on the exit status of @code{make -k}.  Some implementations
14960 reflect whether they encountered an error in their exit status; other
14961 implementations always succeed.
14963 @example
14964 $ @kbd{cat Makefile}
14965 all:
14966         false
14967 $ @kbd{make -k; echo exit status: $?}    # GNU make
14968 false
14969 make: *** [all] Error 1
14970 exit status: 2
14971 $ @kbd{pmake -k; echo exit status: $?}   # BSD make
14972 false
14973 *** Error code 1 (continuing)
14974 exit status: 0
14975 @end example
14977 @node VPATH and Make
14978 @section @code{VPATH} and Make
14979 @cindex @code{VPATH}
14981 Posix does not specify the semantics of @code{VPATH}.  Typically,
14982 @command{make} supports @code{VPATH}, but its implementation is not
14983 consistent.
14985 Autoconf and Automake support makefiles whose usages of @code{VPATH} are
14986 portable to recent-enough popular implementations of @command{make}, but
14987 to keep the resulting makefiles portable, a package's makefile
14988 prototypes must take the following issues into account.  These issues
14989 are complicated and are often poorly understood, and installers who use
14990 @code{VPATH} should expect to find many bugs in this area.  If you use
14991 @code{VPATH}, the simplest way to avoid these portability bugs is to
14992 stick with @acronym{GNU} @command{make}, since it is the most
14993 commonly-used @command{make} among Autoconf users.
14995 Here are some known issues with some @code{VPATH}
14996 implementations.
14998 @menu
14999 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
15000 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
15001 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
15002 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
15003 * Make Target Lookup::          More details about @code{VPATH} lookup
15004 @end menu
15006 @node VPATH and Double-colon
15007 @subsection @code{VPATH} and Double-colon Rules
15008 @cindex @code{VPATH} and double-colon rules
15009 @cindex double-colon rules and @code{VPATH}
15011 With ancient versions of Sun @command{make},
15012 any assignment to @code{VPATH} causes @command{make} to execute only
15013 the first set of double-colon rules.
15014 However, this problem is no longer of practical concern.
15016 @node $< in Explicit Rules
15017 @subsection @code{$<} Not Supported in Explicit Rules
15018 @cindex explicit rules, @code{$<}, and @code{VPATH}
15019 @cindex @code{$<}, explicit rules, and @code{VPATH}
15020 @cindex @code{VPATH}, explicit rules, and @code{$<}
15022 Using @code{$<} in explicit rules is not portable.
15023 The prerequisite file must be named explicitly in the rule.  If you want
15024 to find the prerequisite via a @code{VPATH} search, you have to code the
15025 whole thing manually.  @xref{Build Directories}.
15027 @node Automatic Rule Rewriting
15028 @subsection Automatic Rule Rewriting
15029 @cindex @code{VPATH} and automatic rule rewriting
15030 @cindex automatic rule rewriting and @code{VPATH}
15032 Some @command{make} implementations, such as Solaris and Tru64,
15033 search for prerequisites in @code{VPATH} and
15034 then rewrite each occurrence as a plain word in the rule.
15035 For instance:
15037 @example
15038 # This isn't portable to GNU make.
15039 VPATH = ../pkg/src
15040 f.c: if.c
15041         cp if.c f.c
15042 @end example
15044 @noindent
15045 executes @code{cp ../pkg/src/if.c f.c} if @file{if.c} is
15046 found in @file{../pkg/src}.
15048 However, this rule leads to real problems in practice.  For example, if
15049 the source directory contains an ordinary file named @file{test} that is
15050 used in a dependency, Solaris @command{make} rewrites commands like
15051 @samp{if test -r foo; @dots{}} to @samp{if ../pkg/src/test -r foo;
15052 @dots{}}, which is typically undesirable.  To avoid this problem,
15053 portable makefiles should never mention a source file whose name is that
15054 of a shell keyword like @file{until} or a shell command like
15055 @command{cat} or @command{gcc} or @command{test}.
15057 Because of these problems @acronym{GNU} @command{make} and many other
15058 @command{make} implementations do not rewrite commands, so portable
15059 makefiles should
15060 search @code{VPATH} manually.  It is tempting to write this:
15062 @smallexample
15063 # This isn't portable to Solaris make.
15064 VPATH = ../pkg/src
15065 f.c: if.c
15066         cp `test -f if.c || echo $(VPATH)/`if.c f.c
15067 @end smallexample
15069 @noindent
15070 However, the ``prerequisite rewriting'' still applies here.  So if
15071 @file{if.c} is in @file{../pkg/src}, Solaris and Tru64 @command{make}
15072 execute
15074 @smallexample
15075 cp `test -f ../pkg/src/if.c || echo ../pkg/src/`if.c f.c
15076 @end smallexample
15078 @noindent
15079 which reduces to
15081 @example
15082 cp if.c f.c
15083 @end example
15085 @noindent
15086 and thus fails.  Oops.
15088 A simple workaround, and good practice anyway, is to use @samp{$?} and
15089 @samp{$@@} when possible:
15091 @smallexample
15092 VPATH = ../pkg/src
15093 f.c: if.c
15094         cp $? $@@
15095 @end smallexample
15097 @noindent
15098 but this does not generalize well to commands with multiple
15099 prerequisites.  A more general workaround is to rewrite the rule so that
15100 the prerequisite @file{if.c} never appears as a plain word.  For
15101 example, these three rules would be safe, assuming @file{if.c} is in
15102 @file{../pkg/src} and the other files are in the working directory:
15104 @smallexample
15105 VPATH = ../pkg/src
15106 f.c: if.c f1.c
15107         cat `test -f ./if.c || echo $(VPATH)/`if.c f1.c >$@@
15108 g.c: if.c g1.c
15109         cat `test -f 'if.c' || echo $(VPATH)/`if.c g1.c >$@@
15110 h.c: if.c h1.c
15111         cat `test -f "if.c" || echo $(VPATH)/`if.c h1.c >$@@
15112 @end smallexample
15114 Things get worse when your prerequisites are in a macro.
15116 @example
15117 VPATH = ../pkg/src
15118 HEADERS = f.h g.h h.h
15119 install-HEADERS: $(HEADERS)
15120         for i in $(HEADERS); do \
15121           $(INSTALL) -m 644 \
15122             `test -f $$i || echo $(VPATH)/`$$i \
15123             $(DESTDIR)$(includedir)/$$i; \
15124         done
15125 @end example
15127 The above @code{install-HEADERS} rule is not Solaris-proof because @code{for
15128 i in $(HEADERS);} is expanded to @code{for i in f.h g.h h.h;}
15129 where @code{f.h} and @code{g.h} are plain words and are hence
15130 subject to @code{VPATH} adjustments.
15132 If the three files are in @file{../pkg/src}, the rule is run as:
15134 @example
15135 for i in ../pkg/src/f.h ../pkg/src/g.h h.h; do \
15136   install -m 644 \
15137      `test -f $i || echo ../pkg/src/`$i \
15138      /usr/local/include/$i; \
15139 done
15140 @end example
15142 where the two first @command{install} calls fail.  For instance,
15143 consider the @code{f.h} installation:
15145 @example
15146 install -m 644 \
15147   `test -f ../pkg/src/f.h || \
15148     echo ../pkg/src/ \
15149   `../pkg/src/f.h \
15150   /usr/local/include/../pkg/src/f.h;
15151 @end example
15153 @noindent
15154 It reduces to:
15156 @example
15157 install -m 644 \
15158   ../pkg/src/f.h \
15159   /usr/local/include/../pkg/src/f.h;
15160 @end example
15162 Note that the manual @code{VPATH} search did not cause any problems here;
15163 however this command installs @file{f.h} in an incorrect directory.
15165 Trying to quote @code{$(HEADERS)} in some way, as we did for
15166 @code{foo.c} a few makefiles ago, does not help:
15168 @example
15169 install-HEADERS: $(HEADERS)
15170         headers='$(HEADERS)'; \
15171         for i in $$headers; do \
15172           $(INSTALL) -m 644 \
15173             `test -f $$i || echo $(VPATH)/`$$i \
15174             $(DESTDIR)$(includedir)/$$i; \
15175         done
15176 @end example
15178 Now, @code{headers='$(HEADERS)'} macro-expands to:
15180 @example
15181 headers='f.h g.h h.h'
15182 @end example
15184 @noindent
15185 but @code{g.h} is still a plain word.  (As an aside, the idiom
15186 @code{headers='$(HEADERS)'; for i in $$headers;} is a good
15187 idea if @code{$(HEADERS)} can be empty, because some shells diagnose a
15188 syntax error on @code{for i in;}.)
15190 One workaround is to strip this unwanted @file{../pkg/src/} prefix manually:
15192 @example
15193 VPATH = ../pkg/src
15194 HEADERS = f.h g.h h.h
15195 install-HEADERS: $(HEADERS)
15196         headers='$(HEADERS)'; \
15197         for i in $$headers; do \
15198           i=`expr "$$i" : '$(VPATH)/\(.*\)'`;
15199           $(INSTALL) -m 644 \
15200             `test -f $$i || echo $(VPATH)/`$$i \
15201             $(DESTDIR)$(includedir)/$$i; \
15202         done
15203 @end example
15205 Automake does something similar.  However the above hack works only if
15206 the files listed in @code{HEADERS} are in the current directory or a
15207 subdirectory; they should not be in an enclosing directory.  If we had
15208 @code{HEADERS = ../f.h}, the above fragment would fail in a VPATH
15209 build with Tru64 @command{make}.  The reason is that not only does
15210 Tru64 @command{make} rewrite dependencies, but it also simplifies
15211 them.  Hence @code{../f.h} becomes @code{../pkg/f.h} instead of
15212 @code{../pkg/src/../f.h}.  This obviously defeats any attempt to strip
15213 a leading @file{../pkg/src/} component.
15215 The following example makes the behavior of Tru64 @command{make}
15216 more apparent.
15218 @example
15219 $ @kbd{cat Makefile}
15220 VPATH = sub
15221 all: ../foo
15222         echo ../foo
15223 $ @kbd{ls}
15224 Makefile foo
15225 $ @kbd{make}
15226 echo foo
15228 @end example
15230 @noindent
15231 Dependency @file{../foo} was found in @file{sub/../foo}, but Tru64
15232 @command{make} simplified it as @file{foo}.  (Note that the @file{sub/}
15233 directory does not even exist, this just means that the simplification
15234 occurred before the file was checked for.)
15236 For the record here is how SunOS 4 @command{make} behaves on this
15237 example.
15239 @smallexample
15240 $ @kbd{make}
15241 make: Fatal error: Don't know how to make target `../foo'
15242 $ @kbd{mkdir sub}
15243 $ @kbd{make}
15244 echo sub/../foo
15245 sub/../foo
15246 @end smallexample
15249 @node Tru64 Directory Magic
15250 @subsection Tru64 @command{make} Creates Prerequisite Directories Magically
15251 @cindex @code{VPATH} and prerequisite directories
15252 @cindex prerequisite directories and @code{VPATH}
15254 When a prerequisite is a subdirectory of @code{VPATH}, Tru64
15255 @command{make} creates it in the current directory.
15257 @example
15258 $ @kbd{mkdir -p foo/bar build}
15259 $ @kbd{cd build}
15260 $ @kbd{cat >Makefile <<END
15261 VPATH = ..
15262 all: foo/bar
15263 END}
15264 $ @kbd{make}
15265 mkdir foo
15266 mkdir foo/bar
15267 @end example
15269 This can yield unexpected results if a rule uses a manual @code{VPATH}
15270 search as presented before.
15272 @example
15273 VPATH = ..
15274 all : foo/bar
15275         command `test -d foo/bar || echo ../`foo/bar
15276 @end example
15278 The above @command{command} is run on the empty @file{foo/bar}
15279 directory that was created in the current directory.
15281 @node Make Target Lookup
15282 @subsection Make Target Lookup
15283 @cindex @code{VPATH}, resolving target pathnames
15285 @acronym{GNU} @command{make} uses a complex algorithm to decide when it
15286 should use files found via a @code{VPATH} search.  @xref{Search
15287 Algorithm, , How Directory Searches are Performed, make, The @acronym{GNU} Make
15288 Manual}.
15290 If a target needs to be rebuilt, @acronym{GNU} @command{make} discards the
15291 file name found during the @code{VPATH} search for this target, and
15292 builds the file locally using the file name given in the makefile.
15293 If a target does not need to be rebuilt, @acronym{GNU} @command{make} uses the
15294 file name found during the @code{VPATH} search.
15296 Other @command{make} implementations, like Net@acronym{BSD} @command{make}, are
15297 easier to describe: the file name found during the @code{VPATH} search
15298 is used whether the target needs to be rebuilt or not.  Therefore
15299 new files are created locally, but existing files are updated at their
15300 @code{VPATH} location.
15302 Open@acronym{BSD} and Free@acronym{BSD} @command{make}, however,
15303 never perform a
15304 @code{VPATH} search for a dependency that has an explicit rule.
15305 This is extremely annoying.
15307 When attempting a @code{VPATH} build for an autoconfiscated package
15308 (e.g., @code{mkdir build && cd build && ../configure}), this means
15309 @acronym{GNU}
15310 @command{make} builds everything locally in the @file{build}
15311 directory, while @acronym{BSD} @command{make} builds new files locally and
15312 updates existing files in the source directory.
15314 @example
15315 $ @kbd{cat Makefile}
15316 VPATH = ..
15317 all: foo.x bar.x
15318 foo.x bar.x: newer.x
15319         @@echo Building $@@
15320 $ @kbd{touch ../bar.x}
15321 $ @kbd{touch ../newer.x}
15322 $ @kbd{make}        # GNU make
15323 Building foo.x
15324 Building bar.x
15325 $ @kbd{pmake}       # NetBSD make
15326 Building foo.x
15327 Building ../bar.x
15328 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
15329 Building foo.x
15330 Building bar.x
15331 $ @kbd{tmake}       # Tru64 make
15332 Building foo.x
15333 Building bar.x
15334 $ @kbd{touch ../bar.x}
15335 $ @kbd{make}        # GNU make
15336 Building foo.x
15337 $ @kbd{pmake}       # NetBSD make
15338 Building foo.x
15339 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
15340 Building foo.x
15341 Building bar.x
15342 $ @kbd{tmake}       # Tru64 make
15343 Building foo.x
15344 Building bar.x
15345 @end example
15347 Note how Net@acronym{BSD} @command{make} updates @file{../bar.x} in its
15348 VPATH location, and how Free@acronym{BSD}, Open@acronym{BSD}, and Tru64
15349 @command{make} always
15350 update @file{bar.x}, even when @file{../bar.x} is up to date.
15352 Another point worth mentioning is that once @acronym{GNU} @command{make} has
15353 decided to ignore a @code{VPATH} file name (e.g., it ignored
15354 @file{../bar.x} in the above example) it continues to ignore it when
15355 the target occurs as a prerequisite of another rule.
15357 The following example shows that @acronym{GNU} @command{make} does not look up
15358 @file{bar.x} in @code{VPATH} before performing the @code{.x.y} rule,
15359 because it ignored the @code{VPATH} result of @file{bar.x} while running
15360 the @code{bar.x: newer.x} rule.
15362 @example
15363 $ @kbd{cat Makefile}
15364 VPATH = ..
15365 all: bar.y
15366 bar.x: newer.x
15367         @@echo Building $@@
15368 .SUFFIXES: .x .y
15369 .x.y:
15370         cp $< $@@
15371 $ @kbd{touch ../bar.x}
15372 $ @kbd{touch ../newer.x}
15373 $ @kbd{make}        # GNU make
15374 Building bar.x
15375 cp bar.x bar.y
15376 cp: cannot stat `bar.x': No such file or directory
15377 make: *** [bar.y] Error 1
15378 $ @kbd{pmake}       # NetBSD make
15379 Building ../bar.x
15380 cp ../bar.x bar.y
15381 $ @kbd{rm bar.y}
15382 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
15383 echo Building bar.x
15384 cp bar.x bar.y
15385 cp: cannot stat `bar.x': No such file or directory
15386 *** Error code 1
15387 $ @kbd{tmake}       # Tru64 make
15388 Building bar.x
15389 cp: bar.x: No such file or directory
15390 *** Exit 1
15391 @end example
15393 Note that if you drop away the command from the @code{bar.x: newer.x}
15394 rule, @acronym{GNU} @command{make} magically starts to work: it
15395 knows that @code{bar.x} hasn't been updated, therefore it doesn't
15396 discard the result from @code{VPATH} (@file{../bar.x}) in succeeding
15397 uses.  Tru64 also works, but Free@acronym{BSD} and Open@acronym{BSD}
15398 still don't.
15400 @example
15401 $ @kbd{cat Makefile}
15402 VPATH = ..
15403 all: bar.y
15404 bar.x: newer.x
15405 .SUFFIXES: .x .y
15406 .x.y:
15407         cp $< $@@
15408 $ @kbd{touch ../bar.x}
15409 $ @kbd{touch ../newer.x}
15410 $ @kbd{make}        # GNU make
15411 cp ../bar.x bar.y
15412 $ @kbd{rm bar.y}
15413 $ @kbd{pmake}       # NetBSD make
15414 cp ../bar.x bar.y
15415 $ @kbd{rm bar.y}
15416 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
15417 cp bar.x bar.y
15418 cp: cannot stat `bar.x': No such file or directory
15419 *** Error code 1
15420 $ @kbd{tmake}       # Tru64 make
15421 cp ../bar.x bar.y
15422 @end example
15424 It seems the sole solution that would please every @command{make}
15425 implementation is to never rely on @code{VPATH} searches for targets.
15426 In other words, @code{VPATH} should be reserved to unbuilt sources.
15429 @node Single Suffix Rules
15430 @section Single Suffix Rules and Separated Dependencies
15431 @cindex Single Suffix Inference Rule
15432 @cindex Rule, Single Suffix Inference
15433 A @dfn{Single Suffix Rule} is basically a usual suffix (inference) rule
15434 (@samp{.from.to:}), but which @emph{destination} suffix is empty
15435 (@samp{.from:}).
15437 @cindex Separated Dependencies
15438 @dfn{Separated dependencies} simply refers to listing the prerequisite
15439 of a target, without defining a rule.  Usually one can list on the one
15440 hand side, the rules, and on the other hand side, the dependencies.
15442 Solaris @command{make} does not support separated dependencies for
15443 targets defined by single suffix rules:
15445 @example
15446 $ @kbd{cat Makefile}
15447 .SUFFIXES: .in
15448 foo: foo.in
15449 .in:
15450         cp $< $@@
15451 $ @kbd{touch foo.in}
15452 $ @kbd{make}
15453 $ @kbd{ls}
15454 Makefile  foo.in
15455 @end example
15457 @noindent
15458 while @acronym{GNU} Make does:
15460 @example
15461 $ @kbd{gmake}
15462 cp foo.in foo
15463 $ @kbd{ls}
15464 Makefile  foo       foo.in
15465 @end example
15467 Note it works without the @samp{foo: foo.in} dependency.
15469 @example
15470 $ @kbd{cat Makefile}
15471 .SUFFIXES: .in
15472 .in:
15473         cp $< $@@
15474 $ @kbd{make foo}
15475 cp foo.in foo
15476 @end example
15478 @noindent
15479 and it works with double suffix inference rules:
15481 @example
15482 $ @kbd{cat Makefile}
15483 foo.out: foo.in
15484 .SUFFIXES: .in .out
15485 .in.out:
15486         cp $< $@@
15487 $ @kbd{make}
15488 cp foo.in foo.out
15489 @end example
15491 As a result, in such a case, you have to write target rules.
15493 @node Timestamps and Make
15494 @section Timestamp Resolution and Make
15495 @cindex timestamp resolution
15496 Traditionally, file timestamps had 1-second resolution, and
15497 @command{make} used those timestamps to determine whether one file was
15498 newer than the other.  However, many modern file systems have
15499 timestamps with 1-nanosecond resolution.  Some @command{make}
15500 implementations look at the entire timestamp; others ignore the
15501 fractional part, which can lead to incorrect results.  Normally this
15502 is not a problem, but in some extreme cases you may need to use tricks
15503 like @samp{sleep 1} to work around timestamp truncation bugs.
15505 Commands like @samp{cp -p} and @samp{touch -r} typically do not copy
15506 file timestamps to their full resolutions (@pxref{Limitations of Usual
15507 Tools}).  Hence you should be wary of rules like this:
15509 @example
15510 dest: src
15511         cp -p src dest
15512 @end example
15514 as @file{dest} often appears to be older than @file{src} after the
15515 timestamp is truncated, and this can cause @command{make} to do
15516 needless rework the next time it is invoked.  To work around this
15517 problem, you can use a timestamp file, e.g.:
15519 @example
15520 dest-stamp: src
15521         cp -p src dest
15522         date >dest-stamp
15523 @end example
15528 @c ======================================== Portable C and C++ Programming
15530 @node Portable C and C++
15531 @chapter Portable C and C++ Programming
15532 @cindex Portable C and C++ programming
15534 C and C++ programs often use low-level features of the underlying
15535 system, and therefore are often more difficult to make portable to other
15536 platforms.
15538 Several standards have been developed to help make your programs more
15539 portable.  If you write programs with these standards in mind, you can
15540 have greater confidence that your programs work on a wide variety
15541 of systems.  @xref{Standards, , Language Standards Supported by
15542 @acronym{GCC}, gcc, Using the @acronym{GNU} Compiler Collection
15543 (@acronym{GCC})}, for a list of C-related
15544 standards.  Many programs also assume the
15545 @uref{http://www.opengroup.org/susv3, Posix standard}.
15547 Some old code is written to be portable to K&R C, which predates any C
15548 standard.  K&R C compilers are no longer of practical interest, though,
15549 and the rest of section assumes at least C89, the first C standard.
15551 Program portability is a huge topic, and this section can only briefly
15552 introduce common pitfalls.  @xref{System Portability, , Portability
15553 between System Types, standards, @acronym{GNU} Coding Standards}, for
15554 more information.
15556 @menu
15557 * Varieties of Unportability::  How to make your programs unportable
15558 * Integer Overflow::            When integers get too large
15559 * Null Pointers::               Properties of null pointers
15560 * Buffer Overruns::             Subscript errors and the like
15561 * Volatile Objects::            @code{volatile} and signals
15562 * Floating Point Portability::  Portable floating-point arithmetic
15563 * Exiting Portably::            Exiting and the exit status
15564 @end menu
15566 @node Varieties of Unportability
15567 @section Varieties of Unportability
15568 @cindex portability
15570 Autoconf tests and ordinary programs often need to test what is allowed
15571 on a system, and therefore they may need to deliberately exceed the
15572 boundaries of what the standards allow, if only to see whether an
15573 optional feature is present.  When you write such a program, you should
15574 keep in mind the difference between constraints, unspecified behavior,
15575 and undefined behavior.
15577 In C, a @dfn{constraint} is a rule that the compiler must enforce.  An
15578 example constraint is that C programs must not declare a bit-field with
15579 negative width.  Tests can therefore reliably assume that programs with
15580 negative-width bit-fields are rejected by a compiler that conforms
15581 to the standard.
15583 @dfn{Unspecified behavior} is valid behavior, where the standard allows
15584 multiple possibilities.  For example, the order of evaluation of
15585 function arguments is unspecified.  Some unspecified behavior is
15586 @dfn{implementation-defined}, i.e., documented by the implementation,
15587 but since Autoconf tests cannot read the documentation they cannot
15588 distinguish between implementation-defined and other unspecified
15589 behavior.  It is common for Autoconf tests to probe implementations to
15590 determine otherwise-unspecified behavior.
15592 @dfn{Undefined behavior} is invalid behavior, where the standard allows
15593 the implementation to do anything it pleases.  For example,
15594 dereferencing a null pointer leads to undefined behavior.  If possible,
15595 test programs should avoid undefined behavior, since a program with
15596 undefined behavior might succeed on a test that should fail.
15598 The above rules apply to programs that are intended to conform to the
15599 standard.  However, strictly-conforming programs are quite rare, since
15600 the standards are so limiting.  A major goal of Autoconf is to support
15601 programs that use implementation features not described by the standard,
15602 and it is fairly common for test programs to violate the above rules, if
15603 the programs work well enough in practice.
15605 @node Integer Overflow
15606 @section Integer Overflow
15607 @cindex integer overflow
15608 @cindex overflow, signed integer
15609 @cindex signed integer overflow
15610 @cindex wraparound arithmetic
15612 In practice many portable C programs assume that signed integer overflow wraps
15613 around reliably using two's complement arithmetic.  Yet the C standard
15614 says that program behavior is undefined on overflow, and in a few cases
15615 C programs do not work on some modern implementations because their
15616 overflows do not wrap around as their authors expected.  Conversely, in
15617 signed integer remainder, the C standard requires overflow
15618 behavior that is commonly not implemented.
15620 @menu
15621 * Integer Overflow Basics::      Why integer overflow is a problem
15622 * Signed Overflow Examples::     Examples of code assuming wraparound
15623 * Optimization and Wraparound::  Optimizations that break uses of wraparound
15624 * Signed Overflow Advice::       Practical advice for signed overflow issues
15625 * Signed Integer Division::      @code{INT_MIN / -1} and @code{INT_MIN % -1}
15626 @end menu
15628 @node Integer Overflow Basics
15629 @subsection Basics of Integer Overflow
15630 @cindex integer overflow
15631 @cindex overflow, signed integer
15632 @cindex signed integer overflow
15633 @cindex wraparound arithmetic
15635 In languages like C, unsigned integer overflow reliably wraps around;
15636 e.g., @code{UINT_MAX + 1} yields zero.
15637 This is guaranteed by the C standard and is
15638 portable in practice, unless you specify aggressive,
15639 nonstandard optimization options
15640 suitable only for special applications.
15642 In contrast, the C standard says that signed integer overflow leads to
15643 undefined behavior where a program can do anything, including dumping
15644 core or overrunning a buffer.  The misbehavior can even precede the
15645 overflow.  Such an overflow can occur during addition, subtraction,
15646 multiplication, division, and left shift.
15648 Despite this requirement of the standard, many C programs and Autoconf
15649 tests assume that signed integer overflow silently wraps around modulo a
15650 power of two, using two's complement arithmetic, so long as you cast the
15651 resulting value to a signed integer type or store it into a signed
15652 integer variable.  If you use conservative optimization flags, such
15653 programs are generally portable to the vast majority of modern
15654 platforms, with a few exceptions discussed later.
15656 For historical reasons the C standard also allows implementations with
15657 ones' complement or signed magnitude arithmetic, but it is safe to
15658 assume two's complement nowadays.
15660 Also, overflow can occur when converting an out-of-range value to a
15661 signed integer type.  Here a standard implementation must define what
15662 happens, but this might include raising an exception.  In practice all
15663 known implementations support silent wraparound in this case, so you need
15664 not worry about other possibilities.
15666 @node Signed Overflow Examples
15667 @subsection Examples of Code Assuming Wraparound Overflow
15668 @cindex integer overflow
15669 @cindex overflow, signed integer
15670 @cindex signed integer overflow
15671 @cindex wraparound arithmetic
15673 There has long been a tension between what the C standard requires for
15674 signed integer overflow, and what C programs commonly assume.  The
15675 standard allows aggressive optimizations based on assumptions that
15676 overflow never occurs, but many practical C programs rely on overflow
15677 wrapping around.  These programs do not conform to the standard, but
15678 they commonly work in practice because compiler writers are
15679 understandably reluctant to implement optimizations that would break
15680 many programs, unless perhaps a user specifies aggressive optimization.
15682 The C Standard says that if a program has signed integer overflow its
15683 behavior is undefined, and the undefined behavior can even precede the
15684 overflow.  To take an extreme example:
15686 @c Inspired by Robert Dewar's example in
15687 @c <http://gcc.gnu.org/ml/gcc/2007-01/msg00038.html> (2007-01-01).
15688 @example
15689 if (password == expected_password)
15690   allow_superuser_privileges ();
15691 else if (counter++ == INT_MAX)
15692   abort ();
15693 else
15694   printf ("%d password mismatches\n", counter);
15695 @end example
15697 @noindent
15698 If the @code{int} variable @code{counter} equals @code{INT_MAX},
15699 @code{counter++} must overflow and the behavior is undefined, so the C
15700 standard allows the compiler to optimize away the test against
15701 @code{INT_MAX} and the @code{abort} call.
15702 Worse, if an earlier bug in the program lets the compiler deduce that
15703 @code{counter == INT_MAX} or that @code{counter} previously overflowed,
15704 the C standard allows the compiler to optimize away the password test
15705 and generate code that allows superuser privileges unconditionally.
15707 Despite this requirement by the standard, it has long been common for C
15708 code to assume wraparound arithmetic after signed overflow, and all
15709 known practical C implementations support some C idioms that assume
15710 wraparound signed arithmetic, even if the idioms do not conform
15711 strictly to the standard.  If your code looks like the following
15712 examples it will almost surely work with real-world compilers.
15714 Here is an example derived from the 7th Edition Unix implementation of
15715 @code{atoi} (1979-01-10):
15717 @example
15718 char *p;
15719 int f, n;
15720 @dots{}
15721 while (*p >= '0' && *p <= '9')
15722   n = n * 10 + *p++ - '0';
15723 return (f ? -n : n);
15724 @end example
15726 @noindent
15727 Even if the input string is in range, on most modern machines this has
15728 signed overflow when computing the most negative integer (the @code{-n}
15729 overflows) or a value near an extreme integer (the first @code{+}
15730 overflows).
15732 Here is another example, derived from the 7th Edition implementation of
15733 @code{rand} (1979-01-10).  Here the programmer expects both
15734 multiplication and addition to wrap on overflow:
15736 @example
15737 static long int randx = 1;
15738 @dots{}
15739 randx = randx * 1103515245 + 12345;
15740 return (randx >> 16) & 077777;
15741 @end example
15743 In the following example, derived from the @acronym{GNU} C Library 2.5
15744 implementation of @code{mktime} (2006-09-09), the code assumes
15745 wraparound arithmetic in @code{+} to detect signed overflow:
15747 @example
15748 time_t t, t1, t2;
15749 int sec_requested, sec_adjustment;
15750 @dots{}
15751 t1 = t + sec_requested;
15752 t2 = t1 + sec_adjustment;
15753 if (((t1 < t) != (sec_requested < 0))
15754     | ((t2 < t1) != (sec_adjustment < 0)))
15755   return -1;
15756 @end example
15758 If your code looks like these examples, it is probably safe even though
15759 it does not strictly conform to the C standard.  This might lead one to
15760 believe that one can generally assume wraparound on overflow, but that
15761 is not always true, as can be seen in the next section.
15763 @node Optimization and Wraparound
15764 @subsection Optimizations That Break Wraparound Arithmetic
15765 @cindex loop induction
15767 Compilers sometimes generate code that is incompatible with wraparound
15768 integer arithmetic.  A simple example is an algebraic simplification: a
15769 compiler might translate @code{(i * 2000) / 1000} to @code{i * 2}
15770 because it assumes that @code{i * 2000} does not overflow.  The
15771 translation is not equivalent to the original when overflow occurs:
15772 e.g., in the typical case of 32-bit signed two's complement wraparound
15773 @code{int}, if @code{i} has type @code{int} and value @code{1073742},
15774 the original expression returns @minus{}2147483 but the optimized
15775 version returns the mathematically correct value 2147484.
15777 More subtly, loop induction optimizations often exploit the undefined
15778 behavior of signed overflow.  Consider the following contrived function
15779 @code{sumc}:
15781 @example
15783 sumc (int lo, int hi)
15785   int sum = 0;
15786   int i;
15787   for (i = lo; i <= hi; i++)
15788     sum ^= i * 53;
15789   return sum;
15791 @end example
15793 @noindent
15794 To avoid multiplying by 53 each time through the loop, an optimizing
15795 compiler might internally transform @code{sumc} to the equivalent of the
15796 following:
15798 @example
15800 transformed_sumc (int lo, int hi)
15802   int sum = 0;
15803   int hic = hi * 53;
15804   int ic;
15805   for (ic = lo * 53; ic <= hic; ic += 53)
15806     sum ^= ic;
15807   return sum;
15809 @end example
15811 @noindent
15812 This transformation is allowed by the C standard, but it is invalid for
15813 wraparound arithmetic when @code{INT_MAX / 53 < hi}, because then the
15814 overflow in computing expressions like @code{hi * 53} can cause the
15815 expression @code{i <= hi} to yield a different value from the
15816 transformed expression @code{ic <= hic}.
15818 For this reason, compilers that use loop induction and similar
15819 techniques often do not support reliable wraparound arithmetic when a
15820 loop induction variable like @code{ic} is involved.  Since loop
15821 induction variables are generated by the compiler, and are not visible
15822 in the source code, it is not always trivial to say whether the problem
15823 affects your code.
15825 Hardly any code actually depends on wraparound arithmetic in cases like
15826 these, so in practice these loop induction optimizations are almost
15827 always useful.  However, edge cases in this area can cause problems.
15828 For example:
15830 @example
15831 int j;
15832 for (j = 1; 0 < j; j *= 2)
15833   test (j);
15834 @end example
15836 @noindent
15837 Here, the loop attempts to iterate through all powers of 2 that
15838 @code{int} can represent, but the C standard allows a compiler to
15839 optimize away the comparison and generate an infinite loop,
15840 under the argument that behavior is undefined on overflow.  As of this
15841 writing this optimization is not done by any production version of
15842 @acronym{GCC} with @option{-O2}, but it might be performed by other
15843 compilers, or by more aggressive @acronym{GCC} optimization options,
15844 and the @acronym{GCC} developers have not decided whether it will
15845 continue to work with @acronym{GCC} and @option{-O2}.
15847 @node Signed Overflow Advice
15848 @subsection Practical Advice for Signed Overflow Issues
15849 @cindex integer overflow
15850 @cindex overflow, signed integer
15851 @cindex signed integer overflow
15852 @cindex wraparound arithmetic
15854 Ideally the safest approach is to avoid signed integer overflow
15855 entirely.  For example, instead of multiplying two signed integers, you
15856 can convert them to unsigned integers, multiply the unsigned values,
15857 then test whether the result is in signed range.
15859 Rewriting code in this way will be inconvenient, though, particularly if
15860 the signed values might be negative.  Also, it may hurt
15861 performance.  Using unsigned arithmetic to check for overflow is
15862 particularly painful to do portably and efficiently when dealing with an
15863 integer type like @code{uid_t} whose width and signedness vary from
15864 platform to platform.
15866 Furthermore, many C applications pervasively assume wraparound behavior
15867 and typically it is not easy to find and remove all these assumptions.
15868 Hence it is often useful to maintain nonstandard code that assumes
15869 wraparound on overflow, instead of rewriting the code.  The rest of this
15870 section attempts to give practical advice for this situation.
15872 If your code wants to detect signed integer overflow in @code{sum = a +
15873 b}, it is generally safe to use an expression like @code{(sum < a) != (b
15874 < 0)}.
15876 If your code uses a signed loop index, make sure that the index cannot
15877 overflow, along with all signed expressions derived from the index.
15878 Here is a contrived example of problematic code with two instances of
15879 overflow.
15881 @example
15882 for (i = INT_MAX - 10; i <= INT_MAX; i++)
15883   if (i + 1 < 0)
15884     @{
15885       report_overflow ();
15886       break;
15887     @}
15888 @end example
15890 @noindent
15891 Because of the two overflows, a compiler might optimize away or
15892 transform the two comparisons in a way that is incompatible with the
15893 wraparound assumption.
15895 If your code uses an expression like @code{(i * 2000) / 1000} and you
15896 actually want the multiplication to wrap around on overflow, use
15897 unsigned arithmetic
15898 to do it, e.g., @code{((int) (i * 2000u)) / 1000}.
15900 If your code assumes wraparound behavior and you want to insulate it
15901 against any @acronym{GCC} optimizations that would fail to support that
15902 behavior, you should use @acronym{GCC}'s @option{-fwrapv} option, which
15903 causes signed overflow to wrap around reliably (except for division and
15904 remainder, as discussed in the next section).
15906 If you need to port to platforms where signed integer overflow does not
15907 reliably wrap around (e.g., due to hardware overflow checking, or to
15908 highly aggressive optimizations), you should consider debugging with
15909 @acronym{GCC}'s @option{-ftrapv} option, which causes signed overflow to
15910 raise an exception.
15912 @node Signed Integer Division
15913 @subsection Signed Integer Division and Integer Overflow
15914 @cindex division, integer
15916 Overflow in signed
15917 integer division is not always harmless: for example, on CPUs of the
15918 i386 family, dividing @code{INT_MIN} by @code{-1} yields a SIGFPE signal
15919 which by default terminates the program.  Worse, taking the remainder
15920 of these two values typically yields the same signal on these CPUs,
15921 even though the C standard requires @code{INT_MIN % -1} to yield zero
15922 because the expression does not overflow.
15924 @node Null Pointers
15925 @section Properties of Null Pointers
15926 @cindex null pointers
15928 Most modern hosts reliably fail when you attempt to dereference a null
15929 pointer.
15931 On almost all modern hosts, null pointers use an all-bits-zero internal
15932 representation, so you can reliably use @code{memset} with 0 to set all
15933 the pointers in an array to null values.
15935 If @code{p} is a null pointer to an object type, the C expression
15936 @code{p + 0} always evaluates to @code{p} on modern hosts, even though
15937 the standard says that it has undefined behavior.
15939 @node Buffer Overruns
15940 @section Buffer Overruns and Subscript Errors
15941 @cindex buffer overruns
15943 Buffer overruns and subscript errors are the most common dangerous
15944 errors in C programs.  They result in undefined behavior because storing
15945 outside an array typically modifies storage that is used by some other
15946 object, and most modern systems lack runtime checks to catch these
15947 errors.  Programs should not rely on buffer overruns being caught.
15949 There is one exception to the usual rule that a portable program cannot
15950 address outside an array.  In C, it is valid to compute the address just
15951 past an object, e.g., @code{&a[N]} where @code{a} has @code{N} elements,
15952 so long as you do not dereference the resulting pointer.  But it is not
15953 valid to compute the address just before an object, e.g., @code{&a[-1]};
15954 nor is it valid to compute two past the end, e.g., @code{&a[N+1]}.  On
15955 most platforms @code{&a[-1] < &a[0] && &a[N] < &a[N+1]}, but this is not
15956 reliable in general, and it is usually easy enough to avoid the
15957 potential portability problem, e.g., by allocating an extra unused array
15958 element at the start or end.
15960 @uref{http://valgrind.org/, Valgrind} can catch many overruns.
15961 @acronym{GCC}
15962 users might also consider using the @option{-fmudflap} option to catch
15963 overruns.
15965 Buffer overruns are usually caused by off-by-one errors, but there are
15966 more subtle ways to get them.
15968 Using @code{int} values to index into an array or compute array sizes
15969 causes problems on typical 64-bit hosts where an array index might
15970 be @math{2^31} or larger.  Index values of type @code{size_t} avoid this
15971 problem, but cannot be negative.  Index values of type @code{ptrdiff_t}
15972 are signed, and are wide enough in practice.
15974 If you add or multiply two numbers to calculate an array size, e.g.,
15975 @code{malloc (x * sizeof y + z)}, havoc ensues if the addition or
15976 multiplication overflows.
15978 Many implementations of the @code{alloca} function silently misbehave
15979 and can generate buffer overflows if given sizes that are too large.
15980 The size limits are implementation dependent, but are at least 4000
15981 bytes on all platforms that we know about.
15983 The standard functions @code{asctime}, @code{asctime_r}, @code{ctime},
15984 @code{ctime_r}, and @code{gets} are prone to buffer overflows, and
15985 portable code should not use them unless the inputs are known to be
15986 within certain limits.  The time-related functions can overflow their
15987 buffers if given timestamps out of range (e.g., a year less than -999
15988 or greater than 9999).  Time-related buffer overflows cannot happen with
15989 recent-enough versions of the @acronym{GNU} C library, but are possible
15990 with other
15991 implementations.  The @code{gets} function is the worst, since it almost
15992 invariably overflows its buffer when presented with an input line larger
15993 than the buffer.
15995 @node Volatile Objects
15996 @section Volatile Objects
15997 @cindex volatile objects
15999 The keyword @code{volatile} is often misunderstood in portable code.
16000 Its use inhibits some memory-access optimizations, but programmers often
16001 wish that it had a different meaning than it actually does.
16003 @code{volatile} was designed for code that accesses special objects like
16004 memory-mapped device registers whose contents spontaneously change.
16005 Such code is inherently low-level, and it is difficult to specify
16006 portably what @code{volatile} means in these cases.  The C standard
16007 says, ``What constitutes an access to an object that has
16008 volatile-qualified type is implementation-defined,'' so in theory each
16009 implementation is supposed to fill in the gap by documenting what
16010 @code{volatile} means for that implementation.  In practice, though,
16011 this documentation is usually absent or incomplete.
16013 One area of confusion is the distinction between objects defined with
16014 volatile types, and volatile lvalues.  From the C standard's point of
16015 view, an object defined with a volatile type has externally visible
16016 behavior.  You can think of such objects as having little oscilloscope
16017 probes attached to them, so that the user can observe some properties of
16018 accesses to them, just as the user can observe data written to output
16019 files.  However, the standard does not make it clear whether users can
16020 observe accesses by volatile lvalues to ordinary objects.  For example:
16022 @example
16023 /* Declare and access a volatile object.
16024    Accesses to X are "visible" to users.  */
16025 static int volatile x;
16026 x = 1;
16028 /* Access two ordinary objects via a volatile lvalue.
16029    It's not clear whether accesses to *P are "visible".  */
16030 int y;
16031 int *z = malloc (sizeof (int));
16032 int volatile *p;
16033 p = &y;
16034 *p = 1;
16035 p = z;
16036 *p = 1;
16037 @end example
16039 Programmers often wish that @code{volatile} meant ``Perform the memory
16040 access here and now, without merging several memory accesses, without
16041 changing the memory word size, and without reordering.''  But the C
16042 standard does not require this.  For objects defined with a volatile
16043 type, accesses must be done before the next sequence point; but
16044 otherwise merging, reordering, and word-size change is allowed.  Worse,
16045 it is not clear from the standard whether volatile lvalues provide more
16046 guarantees in general than nonvolatile lvalues, if the underlying
16047 objects are ordinary.
16049 Even when accessing objects defined with a volatile type,
16050 the C standard allows only
16051 extremely limited signal handlers: the behavior is undefined if a signal
16052 handler reads any nonlocal object, or writes to any nonlocal object
16053 whose type is not @code{sig_atomic_t volatile}, or calls any standard
16054 library function other than @code{abort}, @code{signal}, and (if C99)
16055 @code{_Exit}.  Hence C compilers need not worry about a signal handler
16056 disturbing ordinary computation, unless the computation accesses a
16057 @code{sig_atomic_t volatile} lvalue that is not a local variable.
16058 (There is an obscure exception for accesses via a pointer to a volatile
16059 character, since it may point into part of a @code{sig_atomic_t
16060 volatile} object.)  Posix
16061 adds to the list of library functions callable from a portable signal
16062 handler, but otherwise is like the C standard in this area.
16064 Some C implementations allow memory-access optimizations within each
16065 translation unit, such that actual behavior agrees with the behavior
16066 required by the standard only when calling a function in some other
16067 translation unit, and a signal handler acts like it was called from a
16068 different translation unit.  The C standard hints that in these
16069 implementations, objects referred to by signal handlers ``would require
16070 explicit specification of @code{volatile} storage, as well as other
16071 implementation-defined restrictions.''  But unfortunately even for this
16072 special case these other restrictions are often not documented well.
16073 @xref{Volatiles, , When is a Volatile Object Accessed?, gcc, Using the
16074 @acronym{GNU} Compiler Collection (@acronym{GCC})}, for some
16075 restrictions imposed by @acronym{GCC}.  @xref{Defining Handlers, ,
16076 Defining Signal Handlers, libc, The @acronym{GNU} C Library}, for some
16077 restrictions imposed by the @acronym{GNU} C library.  Restrictions
16078 differ on other platforms.
16080 If possible, it is best to use a signal handler that fits within the
16081 limits imposed by the C and Posix standards.
16083 If this is not practical, you can try the following rules of thumb.  A
16084 signal handler should access only volatile lvalues, preferably lvalues
16085 that refer to objects defined with a volatile type, and should not
16086 assume that the accessed objects have an internally consistent state
16087 if they are larger than a machine word.  Furthermore, installers
16088 should employ compilers and compiler options that are commonly used
16089 for building operating system kernels, because kernels often need more
16090 from @code{volatile} than the C Standard requires, and installers who
16091 compile an application in a similar environment can sometimes benefit
16092 from the extra constraints imposed by kernels on compilers.
16093 Admittedly we are handwaving somewhat here, as there are few
16094 guarantees in this area; the rules of thumb may help to fix some bugs
16095 but there is a good chance that they will not fix them all.
16097 For @code{volatile}, C++ has the same problems that C does.
16098 Multithreaded applications have even more problems with @code{volatile},
16099 but they are beyond the scope of this section.
16101 The bottom line is that using @code{volatile} typically hurts
16102 performance but should not hurt correctness.  In some cases its use
16103 does help correctness, but these cases are often so poorly understood
16104 that all too often adding @code{volatile} to a data structure merely
16105 alleviates some symptoms of a bug while not fixing the bug in general.
16107 @node Floating Point Portability
16108 @section Floating Point Portability
16109 @cindex floating point
16111 Almost all modern systems use IEEE-754 floating point, and it is safe to
16112 assume IEEE-754 in most portable code these days.  For more information,
16113 please see David Goldberg's classic paper
16114 @uref{http://www.validlab.com/goldberg/paper.pdf, What Every Computer
16115 Scientist Should Know About Floating-Point Arithmetic}.
16117 @node Exiting Portably
16118 @section Exiting Portably
16119 @cindex exiting portably
16121 A C or C++ program can exit with status @var{N} by returning
16122 @var{N} from the @code{main} function.  Portable programs are supposed
16123 to exit either with status 0 or @code{EXIT_SUCCESS} to succeed, or with
16124 status @code{EXIT_FAILURE} to fail, but in practice it is portable to
16125 fail by exiting with status 1, and test programs that assume Posix can
16126 fail by exiting with status values from 1 through 255.  Programs on
16127 SunOS 2.0 (1985) through 3.5.2 (1988) incorrectly exited with zero
16128 status when @code{main} returned nonzero, but ancient systems like these
16129 are no longer of practical concern.
16131 A program can also exit with status @var{N} by passing @var{N} to the
16132 @code{exit} function, and a program can fail by calling the @code{abort}
16133 function.  If a program is specialized to just some platforms, it can fail
16134 by calling functions specific to those platforms, e.g., @code{_exit}
16135 (Posix) and @code{_Exit} (C99).  However, like other functions, an exit
16136 function should be declared, typically by including a header.  For
16137 example, if a C program calls @code{exit}, it should include @file{stdlib.h}
16138 either directly or via the default includes (@pxref{Default Includes}).
16140 A program can fail due to undefined behavior such as dereferencing a null
16141 pointer, but this is not recommended as undefined behavior allows an
16142 implementation to do whatever it pleases and this includes exiting
16143 successfully.
16146 @c ================================================== Manual Configuration
16148 @node Manual Configuration
16149 @chapter Manual Configuration
16151 A few kinds of features can't be guessed automatically by running test
16152 programs.  For example, the details of the object-file format, or
16153 special options that need to be passed to the compiler or linker.  You
16154 can check for such features using ad-hoc means, such as having
16155 @command{configure} check the output of the @code{uname} program, or
16156 looking for libraries that are unique to particular systems.  However,
16157 Autoconf provides a uniform method for handling unguessable features.
16159 @menu
16160 * Specifying Names::            Specifying the system type
16161 * Canonicalizing::              Getting the canonical system type
16162 * Using System Type::           What to do with the system type
16163 @end menu
16165 @node Specifying Names
16166 @section Specifying the System Type
16167 @cindex System type
16169 Autoconf-generated
16170 @command{configure} scripts can make decisions based on a canonical name
16171 for the system type, which has the form:
16172 @samp{@var{cpu}-@var{vendor}-@var{os}}, where @var{os} can be
16173 @samp{@var{system}} or @samp{@var{kernel}-@var{system}}
16175 @command{configure} can usually guess the canonical name for the type of
16176 system it's running on.  To do so it runs a script called
16177 @command{config.guess}, which infers the name using the @code{uname}
16178 command or symbols predefined by the C preprocessor.
16180 Alternately, the user can specify the system type with command line
16181 arguments to @command{configure}.  Doing so is necessary when
16182 cross-compiling.  In the most complex case of cross-compiling, three
16183 system types are involved.  The options to specify them are:
16185 @table @option
16186 @item --build=@var{build-type}
16187 the type of system on which the package is being configured and
16188 compiled.  It defaults to the result of running @command{config.guess}.
16190 @item --host=@var{host-type}
16191 the type of system on which the package runs.  By default it is the
16192 same as the build machine.  Specifying it enables the cross-compilation
16193 mode.
16195 @item --target=@var{target-type}
16196 the type of system for which any compiler tools in the package
16197 produce code (rarely needed).  By default, it is the same as host.
16198 @end table
16200 If you mean to override the result of @command{config.guess}, use
16201 @option{--build}, not @option{--host}, since the latter enables
16202 cross-compilation.  For historical reasons,
16203 whenever you specify @option{--host},
16204 be sure to specify @option{--build} too; this will be fixed in the
16205 future.  So, to enter cross-compilation mode, use a command like this
16207 @example
16208 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
16209 @end example
16211 @noindent
16212 Note that if you do not specify @option{--host}, @command{configure}
16213 fails if it can't run the code generated by the specified compiler.  For
16214 example, configuring as follows fails:
16216 @example
16217 ./configure CC=m68k-coff-gcc
16218 @end example
16220 In the future, when cross-compiling Autoconf will @emph{not}
16221 accept tools (compilers, linkers, assemblers) whose name is not
16222 prefixed with the host type.  The only case when this may be
16223 useful is when you really are not cross-compiling, but only
16224 building for a least-common-denominator architecture: an example
16225 is building for @code{i386-pc-linux-gnu} while running on an
16226 @code{i686-pc-linux-gnu} architecture.  In this case, some particular
16227 pairs might be similar enough to let you get away with the system
16228 compilers, but in general the compiler might make bogus assumptions
16229 on the host: if you know what you are doing, please create symbolic
16230 links from the host compiler to the build compiler.
16232 @cindex @command{config.sub}
16233 @command{configure} recognizes short aliases for many system types; for
16234 example, @samp{decstation} can be used instead of
16235 @samp{mips-dec-ultrix4.2}.  @command{configure} runs a script called
16236 @command{config.sub} to canonicalize system type aliases.
16238 This section deliberately omits the description of the obsolete
16239 interface; see @ref{Hosts and Cross-Compilation}.
16242 @node Canonicalizing
16243 @section Getting the Canonical System Type
16244 @cindex System type
16245 @cindex Canonical system type
16247 The following macros make the system type available to @command{configure}
16248 scripts.
16250 @ovindex build_alias
16251 @ovindex host_alias
16252 @ovindex target_alias
16254 The variables @samp{build_alias}, @samp{host_alias}, and
16255 @samp{target_alias} are always exactly the arguments of @option{--build},
16256 @option{--host}, and @option{--target}; in particular, they are left empty
16257 if the user did not use them, even if the corresponding
16258 @code{AC_CANONICAL} macro was run.  Any configure script may use these
16259 variables anywhere.  These are the variables that should be used when in
16260 interaction with the user.
16262 If you need to recognize some special environments based on their system
16263 type, run the following macros to get canonical system names.  These
16264 variables are not set before the macro call.
16266 If you use these macros, you must distribute @command{config.guess} and
16267 @command{config.sub} along with your source code.  @xref{Output}, for
16268 information about the @code{AC_CONFIG_AUX_DIR} macro which you can use
16269 to control in which directory @command{configure} looks for those scripts.
16272 @defmac AC_CANONICAL_BUILD
16273 @acindex{CANONICAL_BUILD}
16274 @ovindex build
16275 @ovindex build_cpu
16276 @ovindex build_vendor
16277 @ovindex build_os
16278 Compute the canonical build-system type variable, @code{build}, and its
16279 three individual parts @code{build_cpu}, @code{build_vendor}, and
16280 @code{build_os}.
16282 If @option{--build} was specified, then @code{build} is the
16283 canonicalization of @code{build_alias} by @command{config.sub},
16284 otherwise it is determined by the shell script @command{config.guess}.
16285 @end defmac
16287 @defmac AC_CANONICAL_HOST
16288 @acindex{CANONICAL_HOST}
16289 @ovindex host
16290 @ovindex host_cpu
16291 @ovindex host_vendor
16292 @ovindex host_os
16293 Compute the canonical host-system type variable, @code{host}, and its
16294 three individual parts @code{host_cpu}, @code{host_vendor}, and
16295 @code{host_os}.
16297 If @option{--host} was specified, then @code{host} is the
16298 canonicalization of @code{host_alias} by @command{config.sub},
16299 otherwise it defaults to @code{build}.
16300 @end defmac
16302 @defmac AC_CANONICAL_TARGET
16303 @acindex{CANONICAL_TARGET}
16304 @ovindex target
16305 @ovindex target_cpu
16306 @ovindex target_vendor
16307 @ovindex target_os
16308 Compute the canonical target-system type variable, @code{target}, and its
16309 three individual parts @code{target_cpu}, @code{target_vendor}, and
16310 @code{target_os}.
16312 If @option{--target} was specified, then @code{target} is the
16313 canonicalization of @code{target_alias} by @command{config.sub},
16314 otherwise it defaults to @code{host}.
16315 @end defmac
16317 Note that there can be artifacts due to the backward compatibility
16318 code.  See @xref{Hosts and Cross-Compilation}, for more.
16320 @node Using System Type
16321 @section Using the System Type
16323 In @file{configure.ac} the system type is generally used by one or more
16324 @code{case} statements to select system-specifics.  Shell wildcards can
16325 be used to match a group of system types.
16327 For example, an extra assembler code object file could be chosen, giving
16328 access to a CPU cycle counter register.  @code{$(CYCLE_OBJ)} in the
16329 following would be used in a makefile to add the object to a
16330 program or library.
16332 @example
16333 case $host in
16334   alpha*-*-*) CYCLE_OBJ=rpcc.o ;;
16335   i?86-*-*)   CYCLE_OBJ=rdtsc.o ;;
16336   *)          CYCLE_OBJ= ;;
16337 esac
16338 AC_SUBST([CYCLE_OBJ])
16339 @end example
16341 @code{AC_CONFIG_LINKS} (@pxref{Configuration Links}) is another good way
16342 to select variant source files, for example optimized code for some
16343 CPUs.  The configured CPU type doesn't always indicate exact CPU types,
16344 so some runtime capability checks may be necessary too.
16346 @example
16347 case $host in
16348   alpha*-*-*)   AC_CONFIG_LINKS([dither.c:alpha/dither.c]) ;;
16349   powerpc*-*-*) AC_CONFIG_LINKS([dither.c:powerpc/dither.c]) ;;
16350   *-*-*)        AC_CONFIG_LINKS([dither.c:generic/dither.c]) ;;
16351 esac
16352 @end example
16354 The host system type can also be used to find cross-compilation tools
16355 with @code{AC_CHECK_TOOL} (@pxref{Generic Programs}).
16357 The above examples all show @samp{$host}, since this is where the code
16358 is going to run.  Only rarely is it necessary to test @samp{$build}
16359 (which is where the build is being done).
16361 Whenever you're tempted to use @samp{$host} it's worth considering
16362 whether some sort of probe would be better.  New system types come along
16363 periodically or previously missing features are added.  Well-written
16364 probes can adapt themselves to such things, but hard-coded lists of
16365 names can't.  Here are some guidelines,
16367 @itemize @bullet
16368 @item
16369 Availability of libraries and library functions should always be checked
16370 by probing.
16371 @item
16372 Variant behavior of system calls is best identified with runtime tests
16373 if possible, but bug workarounds or obscure difficulties might have to
16374 be driven from @samp{$host}.
16375 @item
16376 Assembler code is inevitably highly CPU-specific and is best selected
16377 according to @samp{$host_cpu}.
16378 @item
16379 Assembler variations like underscore prefix on globals or ELF versus
16380 COFF type directives are however best determined by probing, perhaps
16381 even examining the compiler output.
16382 @end itemize
16384 @samp{$target} is for use by a package creating a compiler or similar.
16385 For ordinary packages it's meaningless and should not be used.  It
16386 indicates what the created compiler should generate code for, if it can
16387 cross-compile.  @samp{$target} generally selects various hard-coded CPU
16388 and system conventions, since usually the compiler or tools under
16389 construction themselves determine how the target works.
16392 @c ===================================================== Site Configuration.
16394 @node Site Configuration
16395 @chapter Site Configuration
16397 @command{configure} scripts support several kinds of local configuration
16398 decisions.  There are ways for users to specify where external software
16399 packages are, include or exclude optional features, install programs
16400 under modified names, and set default values for @command{configure}
16401 options.
16403 @menu
16404 * Help Formatting::             Customizing @samp{configure --help}
16405 * External Software::           Working with other optional software
16406 * Package Options::             Selecting optional features
16407 * Pretty Help Strings::         Formatting help string
16408 * Option Checking::             Controlling checking of @command{configure} options
16409 * Site Details::                Configuring site details
16410 * Transforming Names::          Changing program names when installing
16411 * Site Defaults::               Giving @command{configure} local defaults
16412 @end menu
16414 @node Help Formatting
16415 @section Controlling Help Output
16417 Users consult @samp{configure --help} to learn of configuration
16418 decisions specific to your package.  By default, @command{configure}
16419 breaks this output into sections for each type of option; within each
16420 section, help strings appear in the order @file{configure.ac} defines
16421 them:
16423 @example
16424 Optional Features:
16425   @dots{}
16426   --enable-bar            include bar
16428 Optional Packages:
16429   @dots{}
16430   --with-foo              use foo
16431 @end example
16433 @defmac AC_PRESERVE_HELP_ORDER
16434 @acindex{PRESERVE_HELP_ORDER}
16436 Request an alternate @option{--help} format, in which options of all
16437 types appear together, in the order defined.  Call this macro before any
16438 @code{AC_ARG_ENABLE} or @code{AC_ARG_WITH}.
16440 @example
16441 Optional Features and Packages:
16442   @dots{}
16443   --enable-bar            include bar
16444   --with-foo              use foo
16445 @end example
16447 @end defmac
16449 @node External Software
16450 @section Working With External Software
16451 @cindex External software
16453 Some packages require, or can optionally use, other software packages
16454 that are already installed.  The user can give @command{configure}
16455 command line options to specify which such external software to use.
16456 The options have one of these forms:
16458 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
16459 @c awful.
16460 @example
16461 --with-@var{package}[=@var{arg}]
16462 --without-@var{package}
16463 @end example
16465 For example, @option{--with-gnu-ld} means work with the @acronym{GNU} linker
16466 instead of some other linker.  @option{--with-x} means work with The X
16467 Window System.
16469 The user can give an argument by following the package name with
16470 @samp{=} and the argument.  Giving an argument of @samp{no} is for
16471 packages that are used by default; it says to @emph{not} use the
16472 package.  An argument that is neither @samp{yes} nor @samp{no} could
16473 include a name or number of a version of the other package, to specify
16474 more precisely which other package this program is supposed to work
16475 with.  If no argument is given, it defaults to @samp{yes}.
16476 @option{--without-@var{package}} is equivalent to
16477 @option{--with-@var{package}=no}.
16479 Normally @command{configure} scripts complain about
16480 @option{--with-@var{package}} options that they do not support.
16481 @xref{Option Checking}, for details, and for how to override the
16482 defaults.
16484 For each external software package that may be used, @file{configure.ac}
16485 should call @code{AC_ARG_WITH} to detect whether the @command{configure}
16486 user asked to use it.  Whether each package is used or not by default,
16487 and which arguments are valid, is up to you.
16489 @anchor{AC_ARG_WITH}
16490 @defmac AC_ARG_WITH (@var{package}, @var{help-string}, @
16491   @ovar{action-if-given}, @ovar{action-if-not-given})
16492 @acindex{ARG_WITH}
16493 If the user gave @command{configure} the option @option{--with-@var{package}}
16494 or @option{--without-@var{package}}, run shell commands
16495 @var{action-if-given}.  If neither option was given, run shell commands
16496 @var{action-if-not-given}.  The name @var{package} indicates another
16497 software package that this program should work with.  It should consist
16498 only of alphanumeric characters, dashes, and dots.
16500 The option's argument is available to the shell commands
16501 @var{action-if-given} in the shell variable @code{withval}, which is
16502 actually just the value of the shell variable named
16503 @code{with_@var{package}}, with any non-alphanumeric characters in
16504 @var{package} changed into @samp{_}.  You may use that variable instead,
16505 if you wish.
16507 The argument @var{help-string} is a description of the option that
16508 looks like this:
16509 @example
16510   --with-readline         support fancy command line editing
16511 @end example
16513 @noindent
16514 @var{help-string} may be more than one line long, if more detail is
16515 needed.  Just make sure the columns line up in @samp{configure
16516 --help}.  Avoid tabs in the help string.  You'll need to enclose the
16517 help string in @samp{[} and @samp{]} in order to produce the leading
16518 blanks.
16520 You should format your @var{help-string} with the macro
16521 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
16523 The following example shows how to use the @code{AC_ARG_WITH} macro in
16524 a common situation.  You want to let the user decide whether to enable
16525 support for an external library (e.g., the readline library); if the user
16526 specified neither @option{--with-readline} nor @option{--without-readline},
16527 you want to enable support for readline only if the library is available
16528 on the system.
16530 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
16531 @example
16532 AC_ARG_WITH([readline],
16533   [AS_HELP_STRING([--with-readline],
16534     [support fancy command line editing @@<:@@default=check@@:>@@])],
16535   [],
16536   [with_readline=check])
16538 LIBREADLINE=
16539 AS_IF([test "x$with_readline" != xno],
16540   [AC_CHECK_LIB([readline], [main],
16541     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
16542      AC_DEFINE([HAVE_LIBREADLINE], [1],
16543                [Define if you have libreadline])
16544     ],
16545     [if test "x$with_readline" != xcheck; then
16546        AC_MSG_FAILURE(
16547          [--with-readline was given, but test for readline failed])
16548      fi
16549     ], -lncurses)])
16550 @end example
16552 The next example shows how to use @code{AC_ARG_WITH} to give the user the
16553 possibility to enable support for the readline library, in case it is still
16554 experimental and not well tested, and is therefore disabled by default.
16556 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
16557 @example
16558 AC_ARG_WITH([readline],
16559   [AS_HELP_STRING([--with-readline],
16560     [enable experimental support for readline])],
16561   [],
16562   [with_readline=no])
16564 LIBREADLINE=
16565 AS_IF([test "x$with_readline" != xno],
16566   [AC_CHECK_LIB([readline], [main],
16567     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
16568      AC_DEFINE([HAVE_LIBREADLINE], [1],
16569                [Define if you have libreadline])
16570     ],
16571     [AC_MSG_FAILURE(
16572        [--with-readline was given, but test for readline failed])],
16573     [-lncurses])])
16574 @end example
16576 The last example shows how to use @code{AC_ARG_WITH} to give the user the
16577 possibility to disable support for the readline library, given that it is
16578 an important feature and that it should be enabled by default.
16580 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
16581 @example
16582 AC_ARG_WITH([readline],
16583   [AS_HELP_STRING([--without-readline],
16584     [disable support for readline])],
16585   [],
16586   [with_readline=yes])
16588 LIBREADLINE=
16589 AS_IF([test "x$with_readline" != xno],
16590   [AC_CHECK_LIB([readline], [main],
16591     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
16592      AC_DEFINE([HAVE_LIBREADLINE], [1],
16593                [Define if you have libreadline])
16594     ],
16595     [AC_MSG_FAILURE(
16596        [readline test failed (--without-readline to disable)])],
16597     [-lncurses])])
16598 @end example
16600 These three examples can be easily adapted to the case where
16601 @code{AC_ARG_ENABLE} should be preferred to @code{AC_ARG_WITH} (see
16602 @ref{Package Options}).
16603 @end defmac
16605 @node Package Options
16606 @section Choosing Package Options
16607 @cindex Package options
16608 @cindex Options, package
16610 If a software package has optional compile-time features, the user can
16611 give @command{configure} command line options to specify whether to
16612 compile them.  The options have one of these forms:
16614 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
16615 @c awful.
16616 @example
16617 --enable-@var{feature}[=@var{arg}]
16618 --disable-@var{feature}
16619 @end example
16621 These options allow users to choose which optional features to build and
16622 install.  @option{--enable-@var{feature}} options should never make a
16623 feature behave differently or cause one feature to replace another.
16624 They should only cause parts of the program to be built rather than left
16625 out.
16627 The user can give an argument by following the feature name with
16628 @samp{=} and the argument.  Giving an argument of @samp{no} requests
16629 that the feature @emph{not} be made available.  A feature with an
16630 argument looks like @option{--enable-debug=stabs}.  If no argument is
16631 given, it defaults to @samp{yes}.  @option{--disable-@var{feature}} is
16632 equivalent to @option{--enable-@var{feature}=no}.
16634 Normally @command{configure} scripts complain about
16635 @option{--enable-@var{package}} options that they do not support.
16636 @xref{Option Checking}, for details, and for how to override the
16637 defaults.
16639 For each optional feature, @file{configure.ac} should call
16640 @code{AC_ARG_ENABLE} to detect whether the @command{configure} user asked
16641 to include it.  Whether each feature is included or not by default, and
16642 which arguments are valid, is up to you.
16644 @anchor{AC_ARG_ENABLE}
16645 @defmac AC_ARG_ENABLE (@var{feature}, @var{help-string}, @
16646   @ovar{action-if-given}, @ovar{action-if-not-given})
16647 @acindex{ARG_ENABLE}
16648 If the user gave @command{configure} the option
16649 @option{--enable-@var{feature}} or @option{--disable-@var{feature}}, run
16650 shell commands @var{action-if-given}.  If neither option was given, run
16651 shell commands @var{action-if-not-given}.  The name @var{feature}
16652 indicates an optional user-level facility.  It should consist only of
16653 alphanumeric characters, dashes, and dots.
16655 The option's argument is available to the shell commands
16656 @var{action-if-given} in the shell variable @code{enableval}, which is
16657 actually just the value of the shell variable named
16658 @code{enable_@var{feature}}, with any non-alphanumeric characters in
16659 @var{feature} changed into @samp{_}.  You may use that variable instead,
16660 if you wish.  The @var{help-string} argument is like that of
16661 @code{AC_ARG_WITH} (@pxref{External Software}).
16663 You should format your @var{help-string} with the macro
16664 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
16666 See the examples suggested with the definition of @code{AC_ARG_WITH}
16667 (@pxref{External Software}) to get an idea of possible applications of
16668 @code{AC_ARG_ENABLE}.
16669 @end defmac
16671 @node Pretty Help Strings
16672 @section Making Your Help Strings Look Pretty
16673 @cindex Help strings
16675 Properly formatting the @samp{help strings} which are used in
16676 @code{AC_ARG_WITH} (@pxref{External Software}) and @code{AC_ARG_ENABLE}
16677 (@pxref{Package Options}) can be challenging.  Specifically, you want
16678 your own @samp{help strings} to line up in the appropriate columns of
16679 @samp{configure --help} just like the standard Autoconf @samp{help
16680 strings} do.  This is the purpose of the @code{AS_HELP_STRING} macro.
16682 @anchor{AS_HELP_STRING}
16683 @defmac AS_HELP_STRING (@var{left-hand-side}, @var{right-hand-side})
16684 @asindex{HELP_STRING}
16686 Expands into an help string that looks pretty when the user executes
16687 @samp{configure --help}.  It is typically used in @code{AC_ARG_WITH}
16688 (@pxref{External Software}) or @code{AC_ARG_ENABLE} (@pxref{Package
16689 Options}).  The following example makes this clearer.
16691 @example
16692 AC_ARG_WITH([foo],
16693   [AS_HELP_STRING([--with-foo],
16694      [use foo (default is no)])],
16695   [use_foo=$withval],
16696   [use_foo=no])
16697 @end example
16699 The second argument of @code{AS_HELP_STRING} is
16700 not a literal, and should not be double quoted.
16701 @xref{Autoconf Language}, for a more detailed explanation.
16702 Then the last few lines of @samp{configure --help} appear like
16703 this:
16705 @example
16706 --enable and --with options recognized:
16707   --with-foo              use foo (default is no)
16708 @end example
16710 The @code{AS_HELP_STRING} macro is particularly helpful when the
16711 @var{left-hand-side} and/or @var{right-hand-side} are composed of macro
16712 arguments, as shown in the following example.
16714 @example
16715 AC_DEFUN([MY_ARG_WITH],
16716   [AC_ARG_WITH([$1],
16717      [AS_HELP_STRING([--with-$1], [use $1 (default is $2)])],
16718      [use_[]$1=$withval],
16719      [use_[]$1=$2])])
16720 @end example
16721 @end defmac
16724 @node Option Checking
16725 @section Controlling Checking of @command{configure} Options
16726 @cindex Options, Package
16728 The @command{configure} script checks its command-line options against a
16729 list of known options, like @option{--help} or @option{--config-cache}.
16730 An unknown option ordinarily indicates a mistake by the user and
16731 @command{configure} halts with an error.  However, by default unknown
16732 @option{--with-@var{package}} and @option{--enable-@var{feature}}
16733 options elicit only a warning, to support configuring entire source
16734 trees.
16736 Source trees often contain multiple packages with a top-level
16737 @command{configure} script that uses the @code{AC_CONFIG_SUBDIRS} macro
16738 (@pxref{Subdirectories}).  Because the packages generally support
16739 different @option{--with-@var{package}} and
16740 @option{--enable-@var{feature}} options, the @acronym{GNU} Coding
16741 Standards say they must accept unrecognized options without halting.
16742 Even a warning message is undesirable here, so @code{AC_CONFIG_SUBDIRS}
16743 automatically disables the warnings.
16745 This default behavior may be modified in two ways.  First, the installer
16746 can invoke @command{configure --disable-option-checking} to disable
16747 these warnings, or invoke @command{configure --enable-option-checking=fatal}
16748 options to turn them into fatal errors, respectively.  Second, the
16749 maintainer can use @code{AC_DISABLE_OPTION_CHECKING}.
16751 @defmac AC_DISABLE_OPTION_CHECKING
16752 @acindex{DISABLE_OPTION_CHECKING}
16754 By default, disable warnings related to any unrecognized
16755 @option{--with-@var{package}} or @option{--enable-@var{feature}}
16756 options.  This is implied by @code{AC_CONFIG_SUBDIRS}.
16758 The installer can override this behavior by passing
16759 @option{--enable-option-checking} (enable warnings) or
16760 @option{--enable-option-checking=fatal} (enable errors) to
16761 @command{configure}.
16762 @end defmac
16765 @node Site Details
16766 @section Configuring Site Details
16767 @cindex Site details
16769 Some software packages require complex site-specific information.  Some
16770 examples are host names to use for certain services, company names, and
16771 email addresses to contact.  Since some configuration scripts generated
16772 by Metaconfig ask for such information interactively, people sometimes
16773 wonder how to get that information in Autoconf-generated configuration
16774 scripts, which aren't interactive.
16776 Such site configuration information should be put in a file that is
16777 edited @emph{only by users}, not by programs.  The location of the file
16778 can either be based on the @code{prefix} variable, or be a standard
16779 location such as the user's home directory.  It could even be specified
16780 by an environment variable.  The programs should examine that file at
16781 runtime, rather than at compile time.  Runtime configuration is more
16782 convenient for users and makes the configuration process simpler than
16783 getting the information while configuring.  @xref{Directory Variables, ,
16784 Variables for Installation Directories, standards, @acronym{GNU} Coding
16785 Standards}, for more information on where to put data files.
16787 @node Transforming Names
16788 @section Transforming Program Names When Installing
16789 @cindex Transforming program names
16790 @cindex Program names, transforming
16792 Autoconf supports changing the names of programs when installing them.
16793 In order to use these transformations, @file{configure.ac} must call the
16794 macro @code{AC_ARG_PROGRAM}.
16796 @defmac AC_ARG_PROGRAM
16797 @acindex{ARG_PROGRAM}
16798 @ovindex program_transform_name
16799 Place in output variable @code{program_transform_name} a sequence of
16800 @code{sed} commands for changing the names of installed programs.
16802 If any of the options described below are given to @command{configure},
16803 program names are transformed accordingly.  Otherwise, if
16804 @code{AC_CANONICAL_TARGET} has been called and a @option{--target} value
16805 is given, the target type followed by a dash is used as a prefix.
16806 Otherwise, no program name transformation is done.
16807 @end defmac
16809 @menu
16810 * Transformation Options::      @command{configure} options to transform names
16811 * Transformation Examples::     Sample uses of transforming names
16812 * Transformation Rules::        Makefile uses of transforming names
16813 @end menu
16815 @node Transformation Options
16816 @subsection Transformation Options
16818 You can specify name transformations by giving @command{configure} these
16819 command line options:
16821 @table @option
16822 @item --program-prefix=@var{prefix}
16823 prepend @var{prefix} to the names;
16825 @item --program-suffix=@var{suffix}
16826 append @var{suffix} to the names;
16828 @item --program-transform-name=@var{expression}
16829 perform @code{sed} substitution @var{expression} on the names.
16830 @end table
16832 @node Transformation Examples
16833 @subsection Transformation Examples
16835 These transformations are useful with programs that can be part of a
16836 cross-compilation development environment.  For example, a
16837 cross-assembler running on a Sun 4 configured with
16838 @option{--target=i960-vxworks} is normally installed as
16839 @file{i960-vxworks-as}, rather than @file{as}, which could be confused
16840 with a native Sun 4 assembler.
16842 You can force a program name to begin with @file{g}, if you don't want
16843 @acronym{GNU} programs installed on your system to shadow other programs with
16844 the same name.  For example, if you configure @acronym{GNU} @code{diff} with
16845 @option{--program-prefix=g}, then when you run @samp{make install} it is
16846 installed as @file{/usr/local/bin/gdiff}.
16848 As a more sophisticated example, you could use
16850 @example
16851 --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/'
16852 @end example
16853 @noindent
16855 to prepend @samp{g} to most of the program names in a source tree,
16856 excepting those like @code{gdb} that already have one and those like
16857 @code{less} and @code{lesskey} that aren't @acronym{GNU} programs.  (That is
16858 assuming that you have a source tree containing those programs that is
16859 set up to use this feature.)
16861 One way to install multiple versions of some programs simultaneously is
16862 to append a version number to the name of one or both.  For example, if
16863 you want to keep Autoconf version 1 around for awhile, you can configure
16864 Autoconf version 2 using @option{--program-suffix=2} to install the
16865 programs as @file{/usr/local/bin/autoconf2},
16866 @file{/usr/local/bin/autoheader2}, etc.  Nevertheless, pay attention
16867 that only the binaries are renamed, therefore you'd have problems with
16868 the library files which might overlap.
16870 @node Transformation Rules
16871 @subsection Transformation Rules
16873 Here is how to use the variable @code{program_transform_name} in a
16874 @file{Makefile.in}:
16876 @example
16877 PROGRAMS = cp ls rm
16878 transform = @@program_transform_name@@
16879 install:
16880         for p in $(PROGRAMS); do \
16881           $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p | \
16882                                               sed '$(transform)'`; \
16883         done
16885 uninstall:
16886         for p in $(PROGRAMS); do \
16887           rm -f $(DESTDIR)$(bindir)/`echo $$p | sed '$(transform)'`; \
16888         done
16889 @end example
16891 It is guaranteed that @code{program_transform_name} is never empty, and
16892 that there are no useless separators.  Therefore you may safely embed
16893 @code{program_transform_name} within a sed program using @samp{;}:
16895 @example
16896 transform = @@program_transform_name@@
16897 transform_exe = s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/
16898 @end example
16900 Whether to do the transformations on documentation files (Texinfo or
16901 @code{man}) is a tricky question; there seems to be no perfect answer,
16902 due to the several reasons for name transforming.  Documentation is not
16903 usually particular to a specific architecture, and Texinfo files do not
16904 conflict with system documentation.  But they might conflict with
16905 earlier versions of the same files, and @code{man} pages sometimes do
16906 conflict with system documentation.  As a compromise, it is probably
16907 best to do name transformations on @code{man} pages but not on Texinfo
16908 manuals.
16910 @node Site Defaults
16911 @section Setting Site Defaults
16912 @cindex Site defaults
16914 Autoconf-generated @command{configure} scripts allow your site to provide
16915 default values for some configuration values.  You do this by creating
16916 site- and system-wide initialization files.
16918 @evindex CONFIG_SITE
16919 If the environment variable @code{CONFIG_SITE} is set, @command{configure}
16920 uses its value as the name of a shell script to read.  Otherwise, it
16921 reads the shell script @file{@var{prefix}/share/config.site} if it exists,
16922 then @file{@var{prefix}/etc/config.site} if it exists.  Thus,
16923 settings in machine-specific files override those in machine-independent
16924 ones in case of conflict.
16926 Site files can be arbitrary shell scripts, but only certain kinds of
16927 code are really appropriate to be in them.  Because @command{configure}
16928 reads any cache file after it has read any site files, a site file can
16929 define a default cache file to be shared between all Autoconf-generated
16930 @command{configure} scripts run on that system (@pxref{Cache Files}).  If
16931 you set a default cache file in a site file, it is a good idea to also
16932 set the output variable @code{CC} in that site file, because the cache
16933 file is only valid for a particular compiler, but many systems have
16934 several available.
16936 You can examine or override the value set by a command line option to
16937 @command{configure} in a site file; options set shell variables that have
16938 the same names as the options, with any dashes turned into underscores.
16939 The exceptions are that @option{--without-} and @option{--disable-} options
16940 are like giving the corresponding @option{--with-} or @option{--enable-}
16941 option and the value @samp{no}.  Thus, @option{--cache-file=localcache}
16942 sets the variable @code{cache_file} to the value @samp{localcache};
16943 @option{--enable-warnings=no} or @option{--disable-warnings} sets the variable
16944 @code{enable_warnings} to the value @samp{no}; @option{--prefix=/usr} sets the
16945 variable @code{prefix} to the value @samp{/usr}; etc.
16947 Site files are also good places to set default values for other output
16948 variables, such as @code{CFLAGS}, if you need to give them non-default
16949 values: anything you would normally do, repetitively, on the command
16950 line.  If you use non-default values for @var{prefix} or
16951 @var{exec_prefix} (wherever you locate the site file), you can set them
16952 in the site file if you specify it with the @code{CONFIG_SITE}
16953 environment variable.
16955 You can set some cache values in the site file itself.  Doing this is
16956 useful if you are cross-compiling, where it is impossible to check features
16957 that require running a test program.  You could ``prime the cache'' by
16958 setting those values correctly for that system in
16959 @file{@var{prefix}/etc/config.site}.  To find out the names of the cache
16960 variables you need to set, look for shell variables with @samp{_cv_} in
16961 their names in the affected @command{configure} scripts, or in the Autoconf
16962 M4 source code for those macros.
16964 The cache file is careful to not override any variables set in the site
16965 files.  Similarly, you should not override command-line options in the
16966 site files.  Your code should check that variables such as @code{prefix}
16967 and @code{cache_file} have their default values (as set near the top of
16968 @command{configure}) before changing them.
16970 Here is a sample file @file{/usr/share/local/gnu/share/config.site}.  The
16971 command @samp{configure --prefix=/usr/share/local/gnu} would read this
16972 file (if @code{CONFIG_SITE} is not set to a different file).
16974 @example
16975 # config.site for configure
16977 # Change some defaults.
16978 test "$prefix" = NONE && prefix=/usr/share/local/gnu
16979 test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu
16980 test "$sharedstatedir" = '$prefix/com' && sharedstatedir=/var
16981 test "$localstatedir" = '$prefix/var' && localstatedir=/var
16983 # Give Autoconf 2.x generated configure scripts a shared default
16984 # cache file for feature test results, architecture-specific.
16985 if test "$cache_file" = /dev/null; then
16986   cache_file="$prefix/var/config.cache"
16987   # A cache file is only valid for one C compiler.
16988   CC=gcc
16990 @end example
16993 @c ============================================== Running configure Scripts.
16995 @node Running configure Scripts
16996 @chapter Running @command{configure} Scripts
16997 @cindex @command{configure}
16999 Below are instructions on how to configure a package that uses a
17000 @command{configure} script, suitable for inclusion as an @file{INSTALL}
17001 file in the package.  A plain-text version of @file{INSTALL} which you
17002 may use comes with Autoconf.
17004 @menu
17005 * Basic Installation::          Instructions for typical cases
17006 * Compilers and Options::       Selecting compilers and optimization
17007 * Multiple Architectures::      Compiling for multiple architectures at once
17008 * Installation Names::          Installing in different directories
17009 * Optional Features::           Selecting optional features
17010 * System Type::                 Specifying the system type
17011 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
17012 * Defining Variables::          Specifying the compiler etc.
17013 * configure Invocation::        Changing how @command{configure} runs
17014 @end menu
17016 @set autoconf
17017 @include install.texi
17020 @c ============================================== config.status Invocation
17022 @node config.status Invocation
17023 @chapter config.status Invocation
17024 @cindex @command{config.status}
17026 The @command{configure} script creates a file named @file{config.status},
17027 which actually configures, @dfn{instantiates}, the template files.  It
17028 also records the configuration options that were specified when the
17029 package was last configured in case reconfiguring is needed.
17031 Synopsis:
17032 @example
17033 ./config.status @var{option}@dots{} [@var{file}@dots{}]
17034 @end example
17036 It configures the @var{files}; if none are specified, all the templates
17037 are instantiated.  The files must be specified without their
17038 dependencies, as in
17040 @example
17041 ./config.status foobar
17042 @end example
17044 @noindent
17047 @example
17048 ./config.status foobar:foo.in:bar.in
17049 @end example
17051 The supported options are:
17053 @table @option
17054 @item --help
17055 @itemx -h
17056 Print a summary of the command line options, the list of the template
17057 files, and exit.
17059 @item --version
17060 @itemx -V
17061 Print the version number of Autoconf and the configuration settings,
17062 and exit.
17064 @item --silent
17065 @itemx --quiet
17066 @itemx -q
17067 Do not print progress messages.
17069 @item --debug
17070 @itemx -d
17071 Don't remove the temporary files.
17073 @item --file=@var{file}[:@var{template}]
17074 Require that @var{file} be instantiated as if
17075 @samp{AC_CONFIG_FILES(@var{file}:@var{template})} was used.  Both
17076 @var{file} and @var{template} may be @samp{-} in which case the standard
17077 output and/or standard input, respectively, is used.  If a
17078 @var{template} file name is relative, it is first looked for in the build
17079 tree, and then in the source tree.  @xref{Configuration Actions}, for
17080 more details.
17082 This option and the following ones provide one way for separately
17083 distributed packages to share the values computed by @command{configure}.
17084 Doing so can be useful if some of the packages need a superset of the
17085 features that one of them, perhaps a common library, does.  These
17086 options allow a @file{config.status} file to create files other than the
17087 ones that its @file{configure.ac} specifies, so it can be used for a
17088 different package.
17090 @item --header=@var{file}[:@var{template}]
17091 Same as @option{--file} above, but with @samp{AC_CONFIG_HEADERS}.
17093 @item --recheck
17094 Ask @file{config.status} to update itself and exit (no instantiation).
17095 This option is useful if you change @command{configure}, so that the
17096 results of some tests might be different from the previous run.  The
17097 @option{--recheck} option reruns @command{configure} with the same arguments
17098 you used before, plus the @option{--no-create} option, which prevents
17099 @command{configure} from running @file{config.status} and creating
17100 @file{Makefile} and other files, and the @option{--no-recursion} option,
17101 which prevents @command{configure} from running other @command{configure}
17102 scripts in subdirectories.  (This is so other Make rules can
17103 run @file{config.status} when it changes; @pxref{Automatic Remaking},
17104 for an example).
17105 @end table
17107 @file{config.status} checks several optional environment variables that
17108 can alter its behavior:
17110 @defvar CONFIG_SHELL
17111 @evindex CONFIG_SHELL
17112 The shell with which to run @command{configure} for the @option{--recheck}
17113 option.  It must be Bourne-compatible.  The default is a shell that
17114 supports @code{LINENO} if available, and @file{/bin/sh} otherwise.
17115 Invoking @command{configure} by hand bypasses this setting, so you may
17116 need to use a command like @samp{CONFIG_SHELL=/bin/bash /bin/bash ./configure}
17117 to insure that the same shell is used everywhere.  The absolute name of the
17118 shell should be passed.
17119 @end defvar
17121 @defvar CONFIG_STATUS
17122 @evindex CONFIG_STATUS
17123 The file name to use for the shell script that records the
17124 configuration.  The default is @file{./config.status}.  This variable is
17125 useful when one package uses parts of another and the @command{configure}
17126 scripts shouldn't be merged because they are maintained separately.
17127 @end defvar
17129 You can use @file{./config.status} in your makefiles.  For example, in
17130 the dependencies given above (@pxref{Automatic Remaking}),
17131 @file{config.status} is run twice when @file{configure.ac} has changed.
17132 If that bothers you, you can make each run only regenerate the files for
17133 that rule:
17134 @example
17135 @group
17136 config.h: stamp-h
17137 stamp-h: config.h.in config.status
17138         ./config.status config.h
17139         echo > stamp-h
17141 Makefile: Makefile.in config.status
17142         ./config.status Makefile
17143 @end group
17144 @end example
17146 The calling convention of @file{config.status} has changed; see
17147 @ref{Obsolete config.status Use}, for details.
17150 @c =================================================== Obsolete Constructs
17152 @node Obsolete Constructs
17153 @chapter Obsolete Constructs
17154 @cindex Obsolete constructs
17156 Autoconf changes, and throughout the years some constructs have been
17157 obsoleted.  Most of the changes involve the macros, but in some cases
17158 the tools themselves, or even some concepts, are now considered
17159 obsolete.
17161 You may completely skip this chapter if you are new to Autoconf.  Its
17162 intention is mainly to help maintainers updating their packages by
17163 understanding how to move to more modern constructs.
17165 @menu
17166 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
17167 * acconfig Header::             Additional entries in @file{config.h.in}
17168 * autoupdate Invocation::       Automatic update of @file{configure.ac}
17169 * Obsolete Macros::             Backward compatibility macros
17170 * Autoconf 1::                  Tips for upgrading your files
17171 * Autoconf 2.13::               Some fresher tips
17172 @end menu
17174 @node Obsolete config.status Use
17175 @section Obsolete @file{config.status} Invocation
17177 @file{config.status} now supports arguments to specify the files to
17178 instantiate; see @ref{config.status Invocation}, for more details.
17179 Before, environment variables had to be used.
17181 @defvar CONFIG_COMMANDS
17182 @evindex CONFIG_COMMANDS
17183 The tags of the commands to execute.  The default is the arguments given
17184 to @code{AC_OUTPUT} and @code{AC_CONFIG_COMMANDS} in
17185 @file{configure.ac}.
17186 @end defvar
17188 @defvar CONFIG_FILES
17189 @evindex CONFIG_FILES
17190 The files in which to perform @samp{@@@var{variable}@@} substitutions.
17191 The default is the arguments given to @code{AC_OUTPUT} and
17192 @code{AC_CONFIG_FILES} in @file{configure.ac}.
17193 @end defvar
17195 @defvar CONFIG_HEADERS
17196 @evindex CONFIG_HEADERS
17197 The files in which to substitute C @code{#define} statements.  The
17198 default is the arguments given to @code{AC_CONFIG_HEADERS}; if that
17199 macro was not called, @file{config.status} ignores this variable.
17200 @end defvar
17202 @defvar CONFIG_LINKS
17203 @evindex CONFIG_LINKS
17204 The symbolic links to establish.  The default is the arguments given to
17205 @code{AC_CONFIG_LINKS}; if that macro was not called,
17206 @file{config.status} ignores this variable.
17207 @end defvar
17209 In @ref{config.status Invocation}, using this old interface, the example
17210 would be:
17212 @example
17213 @group
17214 config.h: stamp-h
17215 stamp-h: config.h.in config.status
17216         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_FILES= \
17217           CONFIG_HEADERS=config.h ./config.status
17218         echo > stamp-h
17220 Makefile: Makefile.in config.status
17221         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_HEADERS= \
17222           CONFIG_FILES=Makefile ./config.status
17223 @end group
17224 @end example
17226 @noindent
17227 (If @file{configure.ac} does not call @code{AC_CONFIG_HEADERS}, there is
17228 no need to set @code{CONFIG_HEADERS} in the @code{make} rules.  Equally
17229 for @code{CONFIG_COMMANDS}, etc.)
17232 @node acconfig Header
17233 @section @file{acconfig.h}
17235 @cindex @file{acconfig.h}
17236 @cindex @file{config.h.top}
17237 @cindex @file{config.h.bot}
17239 In order to produce @file{config.h.in}, @command{autoheader} needs to
17240 build or to find templates for each symbol.  Modern releases of Autoconf
17241 use @code{AH_VERBATIM} and @code{AH_TEMPLATE} (@pxref{Autoheader
17242 Macros}), but in older releases a file, @file{acconfig.h}, contained the
17243 list of needed templates.  @command{autoheader} copied comments and
17244 @code{#define} and @code{#undef} statements from @file{acconfig.h} in
17245 the current directory, if present.  This file used to be mandatory if
17246 you @code{AC_DEFINE} any additional symbols.
17248 Modern releases of Autoconf also provide @code{AH_TOP} and
17249 @code{AH_BOTTOM} if you need to prepend/append some information to
17250 @file{config.h.in}.  Ancient versions of Autoconf had a similar feature:
17251 if @file{./acconfig.h} contains the string @samp{@@TOP@@},
17252 @command{autoheader} copies the lines before the line containing
17253 @samp{@@TOP@@} into the top of the file that it generates.  Similarly,
17254 if @file{./acconfig.h} contains the string @samp{@@BOTTOM@@},
17255 @command{autoheader} copies the lines after that line to the end of the
17256 file it generates.  Either or both of those strings may be omitted.  An
17257 even older alternate way to produce the same effect in ancient versions
17258 of Autoconf is to create the files @file{@var{file}.top} (typically
17259 @file{config.h.top}) and/or @file{@var{file}.bot} in the current
17260 directory.  If they exist, @command{autoheader} copies them to the
17261 beginning and end, respectively, of its output.
17263 In former versions of Autoconf, the files used in preparing a software
17264 package for distribution were:
17265 @example
17266 @group
17267 configure.ac --.   .------> autoconf* -----> configure
17268                +---+
17269 [aclocal.m4] --+   `---.
17270 [acsite.m4] ---'       |
17271                        +--> [autoheader*] -> [config.h.in]
17272 [acconfig.h] ----.     |
17273                  +-----'
17274 [config.h.top] --+
17275 [config.h.bot] --'
17276 @end group
17277 @end example
17279 Using only the @code{AH_} macros, @file{configure.ac} should be
17280 self-contained, and should not depend upon @file{acconfig.h} etc.
17283 @node autoupdate Invocation
17284 @section Using @command{autoupdate} to Modernize @file{configure.ac}
17285 @cindex @command{autoupdate}
17287 The @command{autoupdate} program updates a @file{configure.ac} file that
17288 calls Autoconf macros by their old names to use the current macro names.
17289 In version 2 of Autoconf, most of the macros were renamed to use a more
17290 uniform and descriptive naming scheme.  @xref{Macro Names}, for a
17291 description of the new scheme.  Although the old names still work
17292 (@pxref{Obsolete Macros}, for a list of the old macros and the corresponding
17293 new names), you can make your @file{configure.ac} files more readable
17294 and make it easier to use the current Autoconf documentation if you
17295 update them to use the new macro names.
17297 @evindex SIMPLE_BACKUP_SUFFIX
17298 If given no arguments, @command{autoupdate} updates @file{configure.ac},
17299 backing up the original version with the suffix @file{~} (or the value
17300 of the environment variable @code{SIMPLE_BACKUP_SUFFIX}, if that is
17301 set).  If you give @command{autoupdate} an argument, it reads that file
17302 instead of @file{configure.ac} and writes the updated file to the
17303 standard output.
17305 @noindent
17306 @command{autoupdate} accepts the following options:
17308 @table @option
17309 @item --help
17310 @itemx -h
17311 Print a summary of the command line options and exit.
17313 @item --version
17314 @itemx -V
17315 Print the version number of Autoconf and exit.
17317 @item --verbose
17318 @itemx -v
17319 Report processing steps.
17321 @item --debug
17322 @itemx -d
17323 Don't remove the temporary files.
17325 @item --force
17326 @itemx -f
17327 Force the update even if the file has not changed.  Disregard the cache.
17329 @item --include=@var{dir}
17330 @itemx -I @var{dir}
17331 Also look for input files in @var{dir}.  Multiple invocations accumulate.
17332 Directories are browsed from last to first.
17333 @end table
17335 @node Obsolete Macros
17336 @section Obsolete Macros
17338 Several macros are obsoleted in Autoconf, for various reasons (typically
17339 they failed to quote properly, couldn't be extended for more recent
17340 issues, etc.).  They are still supported, but deprecated: their use
17341 should be avoided.
17343 During the jump from Autoconf version 1 to version 2, most of the
17344 macros were renamed to use a more uniform and descriptive naming scheme,
17345 but their signature did not change.  @xref{Macro Names}, for a
17346 description of the new naming scheme.  Below, if there is just the mapping
17347 from old names to new names for these macros, the reader is invited to
17348 refer to the definition of the new macro for the signature and the
17349 description.
17351 @defmac AC_AIX
17352 @acindex{AIX}
17353 @cvindex _ALL_SOURCE
17354 This macro is a platform-specific subset of
17355 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
17356 @end defmac
17358 @defmac AC_ALLOCA
17359 @acindex{ALLOCA}
17360 Replaced by @code{AC_FUNC_ALLOCA} (@pxref{AC_FUNC_ALLOCA}).
17361 @end defmac
17363 @defmac AC_ARG_ARRAY
17364 @acindex{ARG_ARRAY}
17365 Removed because of limited usefulness.
17366 @end defmac
17368 @defmac AC_C_CROSS
17369 @acindex{C_CROSS}
17370 This macro is obsolete; it does nothing.
17371 @end defmac
17373 @defmac AC_C_LONG_DOUBLE
17374 @acindex{C_LONG_DOUBLE}
17375 @cvindex HAVE_LONG_DOUBLE
17376 If the C compiler supports a working @code{long double} type with more
17377 range or precision than the @code{double} type, define
17378 @code{HAVE_LONG_DOUBLE}.
17380 You should use @code{AC_TYPE_LONG_DOUBLE} or
17381 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
17382 @end defmac
17384 @defmac AC_CANONICAL_SYSTEM
17385 @acindex{CANONICAL_SYSTEM}
17386 Determine the system type and set output variables to the names of the
17387 canonical system types.  @xref{Canonicalizing}, for details about the
17388 variables this macro sets.
17390 The user is encouraged to use either @code{AC_CANONICAL_BUILD}, or
17391 @code{AC_CANONICAL_HOST}, or @code{AC_CANONICAL_TARGET}, depending on
17392 the needs.  Using @code{AC_CANONICAL_TARGET} is enough to run the two
17393 other macros (@pxref{Canonicalizing}).
17394 @end defmac
17396 @defmac AC_CHAR_UNSIGNED
17397 @acindex{CHAR_UNSIGNED}
17398 Replaced by @code{AC_C_CHAR_UNSIGNED} (@pxref{AC_C_CHAR_UNSIGNED}).
17399 @end defmac
17401 @defmac AC_CHECK_TYPE (@var{type}, @var{default})
17402 @acindex{CHECK_TYPE}
17403 Autoconf, up to 2.13, used to provide this version of
17404 @code{AC_CHECK_TYPE}, deprecated because of its flaws.  First, although
17405 it is a member of the @code{CHECK} clan, it does
17406 more than just checking.  Secondly, missing types are defined
17407 using @code{#define}, not @code{typedef}, and this can lead to
17408 problems in the case of pointer types.
17410 This use of @code{AC_CHECK_TYPE} is obsolete and discouraged; see
17411 @ref{Generic Types}, for the description of the current macro.
17413 If the type @var{type} is not defined, define it to be the C (or C++)
17414 builtin type @var{default}, e.g., @samp{short int} or @samp{unsigned int}.
17416 This macro is equivalent to:
17418 @example
17419 AC_CHECK_TYPE([@var{type}], [],
17420   [AC_DEFINE_UNQUOTED([@var{type}], [@var{default}],
17421      [Define to `@var{default}'
17422       if <sys/types.h> does not define.])])
17423 @end example
17425 In order to keep backward compatibility, the two versions of
17426 @code{AC_CHECK_TYPE} are implemented, selected using these heuristics:
17428 @enumerate
17429 @item
17430 If there are three or four arguments, the modern version is used.
17432 @item
17433 If the second argument appears to be a C or C++ type, then the
17434 obsolete version is used.  This happens if the argument is a C or C++
17435 @emph{builtin} type or a C identifier ending in @samp{_t}, optionally
17436 followed by one of @samp{[(* } and then by a string of zero or more
17437 characters taken from the set @samp{[]()* _a-zA-Z0-9}.
17439 @item
17440 If the second argument is spelled with the alphabet of valid C and C++
17441 types, the user is warned and the modern version is used.
17443 @item
17444 Otherwise, the modern version is used.
17445 @end enumerate
17447 @noindent
17448 You are encouraged either to use a valid builtin type, or to use the
17449 equivalent modern code (see above), or better yet, to use
17450 @code{AC_CHECK_TYPES} together with
17452 @example
17453 #ifndef HAVE_LOFF_T
17454 typedef loff_t off_t;
17455 #endif
17456 @end example
17457 @end defmac
17458 @c end of AC_CHECK_TYPE
17460 @defmac AC_CHECKING (@var{feature-description})
17461 @acindex{CHECKING}
17462 Same as
17464 @example
17465 AC_MSG_NOTICE([checking @var{feature-description}@dots{}]
17466 @end example
17468 @noindent
17469 @xref{AC_MSG_NOTICE}.
17470 @end defmac
17472 @defmac AC_COMPILE_CHECK (@var{echo-text}, @var{includes}, @
17473   @var{function-body}, @var{action-if-true}, @ovar{action-if-false})
17474 @acindex{COMPILE_CHECK}
17475 This is an obsolete version of @code{AC_TRY_COMPILE} itself replaced by
17476 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}), with the
17477 addition that it prints @samp{checking for @var{echo-text}} to the
17478 standard output first, if @var{echo-text} is non-empty.  Use
17479 @code{AC_MSG_CHECKING} and @code{AC_MSG_RESULT} instead to print
17480 messages (@pxref{Printing Messages}).
17481 @end defmac
17483 @defmac AC_CONST
17484 @acindex{CONST}
17485 Replaced by @code{AC_C_CONST} (@pxref{AC_C_CONST}).
17486 @end defmac
17488 @defmac AC_CROSS_CHECK
17489 @acindex{CROSS_CHECK}
17490 Same as @code{AC_C_CROSS}, which is obsolete too, and does nothing
17491 @code{:-)}.
17492 @end defmac
17494 @defmac AC_CYGWIN
17495 @acindex{CYGWIN}
17496 @evindex CYGWIN
17497 Check for the Cygwin environment in which case the shell variable
17498 @code{CYGWIN} is set to @samp{yes}.  Don't use this macro, the dignified
17499 means to check the nature of the host is using @code{AC_CANONICAL_HOST}
17500 (@pxref{Canonicalizing}).  As a matter of fact this macro is defined as:
17502 @example
17503 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
17504 case $host_os in
17505   *cygwin* ) CYGWIN=yes;;
17506          * ) CYGWIN=no;;
17507 esac
17508 @end example
17510 Beware that the variable @env{CYGWIN} has a special meaning when
17511 running Cygwin, and should not be changed.  That's yet another reason
17512 not to use this macro.
17513 @end defmac
17515 @defmac AC_DECL_SYS_SIGLIST
17516 @acindex{DECL_SYS_SIGLIST}
17517 @cvindex SYS_SIGLIST_DECLARED
17518 Same as:
17520 @example
17521 AC_CHECK_DECLS([sys_siglist], [], [],
17522 [#include <signal.h>
17523 /* NetBSD declares sys_siglist in unistd.h.  */
17524 #ifdef HAVE_UNISTD_H
17525 # include <unistd.h>
17526 #endif
17528 @end example
17530 @noindent
17531 @xref{AC_CHECK_DECLS}.
17532 @end defmac
17534 @defmac AC_DECL_YYTEXT
17535 @acindex{DECL_YYTEXT}
17536 Does nothing, now integrated in @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
17537 @end defmac
17539 @defmac AC_DIR_HEADER
17540 @acindex{DIR_HEADER}
17541 @cvindex DIRENT
17542 @cvindex SYSNDIR
17543 @cvindex SYSDIR
17544 @cvindex NDIR
17545 Like calling @code{AC_FUNC_CLOSEDIR_VOID}
17546 (@pxref{AC_FUNC_CLOSEDIR_VOID}) and @code{AC_HEADER_DIRENT}
17547 (@pxref{AC_HEADER_DIRENT}),
17548 but defines a different set of C preprocessor macros to indicate which
17549 header file is found:
17551 @multitable {@file{sys/ndir.h}} {Old Symbol} {@code{HAVE_SYS_NDIR_H}}
17552 @item Header            @tab Old Symbol     @tab New Symbol
17553 @item @file{dirent.h}   @tab @code{DIRENT}  @tab @code{HAVE_DIRENT_H}
17554 @item @file{sys/ndir.h} @tab @code{SYSNDIR} @tab @code{HAVE_SYS_NDIR_H}
17555 @item @file{sys/dir.h}  @tab @code{SYSDIR}  @tab @code{HAVE_SYS_DIR_H}
17556 @item @file{ndir.h}     @tab @code{NDIR}    @tab @code{HAVE_NDIR_H}
17557 @end multitable
17558 @end defmac
17560 @defmac AC_DYNIX_SEQ
17561 @acindex{DYNIX_SEQ}
17562 If on DYNIX/ptx, add @option{-lseq} to output variable
17563 @code{LIBS}.  This macro used to be defined as
17565 @example
17566 AC_CHECK_LIB([seq], [getmntent], [LIBS="-lseq $LIBS"])
17567 @end example
17569 @noindent
17570 now it is just @code{AC_FUNC_GETMNTENT} (@pxref{AC_FUNC_GETMNTENT}).
17571 @end defmac
17573 @defmac AC_EXEEXT
17574 @acindex{EXEEXT}
17575 @ovindex EXEEXT
17576 Defined the output variable @code{EXEEXT} based on the output of the
17577 compiler, which is now done automatically.  Typically set to empty
17578 string if Posix and @samp{.exe} if a @acronym{DOS} variant.
17579 @end defmac
17581 @defmac AC_EMXOS2
17582 @acindex{EMXOS2}
17583 Similar to @code{AC_CYGWIN} but checks for the EMX environment on OS/2
17584 and sets @code{EMXOS2}.  Don't use this macro, the dignified means to
17585 check the nature of the host is using @code{AC_CANONICAL_HOST}
17586 (@pxref{Canonicalizing}).
17587 @end defmac
17589 @defmac AC_ENABLE (@var{feature}, @var{action-if-given}, @
17590   @ovar{action-if-not-given})
17591 @acindex{ENABLE}
17592 This is an obsolete version of @code{AC_ARG_ENABLE} that does not
17593 support providing a help string (@pxref{AC_ARG_ENABLE}).
17594 @end defmac
17596 @defmac AC_ERROR
17597 @acindex{ERROR}
17598 Replaced by @code{AC_MSG_ERROR} (@pxref{AC_MSG_ERROR}).
17599 @end defmac
17601 @defmac AC_FIND_X
17602 @acindex{FIND_X}
17603 Replaced by @code{AC_PATH_X} (@pxref{AC_PATH_X}).
17604 @end defmac
17606 @defmac AC_FIND_XTRA
17607 @acindex{FIND_XTRA}
17608 Replaced by @code{AC_PATH_XTRA} (@pxref{AC_PATH_XTRA}).
17609 @end defmac
17611 @defmac AC_FOREACH
17612 @acindex{FOREACH}
17613 Replaced by @code{m4_foreach_w} (@pxref{m4_foreach_w}).
17614 @end defmac
17616 @defmac AC_FUNC_CHECK
17617 @acindex{FUNC_CHECK}
17618 Replaced by @code{AC_CHECK_FUNC} (@pxref{AC_CHECK_FUNC}).
17619 @end defmac
17621 @anchor{AC_FUNC_SETVBUF_REVERSED}
17622 @defmac AC_FUNC_SETVBUF_REVERSED
17623 @acindex{FUNC_SETVBUF_REVERSED}
17624 @cvindex SETVBUF_REVERSED
17625 @c @fuindex setvbuf
17626 @prindex @code{setvbuf}
17627 Do nothing.  Formerly, this macro checked whether @code{setvbuf} takes
17628 the buffering type as its second argument and the buffer pointer as the
17629 third, instead of the other way around, and defined
17630 @code{SETVBUF_REVERSED}.  However, the last systems to have the problem
17631 were those based on SVR2, which became obsolete in 1987, and the macro
17632 is no longer needed.
17633 @end defmac
17635 @defmac AC_FUNC_WAIT3
17636 @acindex{FUNC_WAIT3}
17637 @cvindex HAVE_WAIT3
17638 If @code{wait3} is found and fills in the contents of its third argument
17639 (a @samp{struct rusage *}), which @acronym{HP-UX} does not do, define
17640 @code{HAVE_WAIT3}.
17642 These days portable programs should use @code{waitpid}, not
17643 @code{wait3}, as @code{wait3} has been removed from Posix.
17644 @end defmac
17646 @defmac AC_GCC_TRADITIONAL
17647 @acindex{GCC_TRADITIONAL}
17648 Replaced by @code{AC_PROG_GCC_TRADITIONAL} (@pxref{AC_PROG_GCC_TRADITIONAL}).
17649 @end defmac
17651 @defmac AC_GETGROUPS_T
17652 @acindex{GETGROUPS_T}
17653 Replaced by @code{AC_TYPE_GETGROUPS} (@pxref{AC_TYPE_GETGROUPS}).
17654 @end defmac
17656 @defmac AC_GETLOADAVG
17657 @acindex{GETLOADAVG}
17658 Replaced by @code{AC_FUNC_GETLOADAVG} (@pxref{AC_FUNC_GETLOADAVG}).
17659 @end defmac
17661 @defmac AC_GNU_SOURCE
17662 @acindex{GNU_SOURCE}
17663 @cvindex _GNU_SOURCE
17664 This macro is a platform-specific subset of
17665 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
17666 @end defmac
17668 @defmac AC_HAVE_FUNCS
17669 @acindex{HAVE_FUNCS}
17670 Replaced by @code{AC_CHECK_FUNCS} (@pxref{AC_CHECK_FUNCS}).
17671 @end defmac
17673 @defmac AC_HAVE_HEADERS
17674 @acindex{HAVE_HEADERS}
17675 Replaced by @code{AC_CHECK_HEADERS} (@pxref{AC_CHECK_HEADERS}).
17676 @end defmac
17678 @defmac AC_HAVE_LIBRARY (@var{library}, @ovar{action-if-found}, @
17679   @ovar{action-if-not-found}, @ovar{other-libraries})
17680 @acindex{HAVE_LIBRARY}
17681 This macro is equivalent to calling @code{AC_CHECK_LIB} with a
17682 @var{function} argument of @code{main}.  In addition, @var{library} can
17683 be written as any of @samp{foo}, @option{-lfoo}, or @samp{libfoo.a}.  In
17684 all of those cases, the compiler is passed @option{-lfoo}.  However,
17685 @var{library} cannot be a shell variable; it must be a literal name.
17686 @xref{AC_CHECK_LIB}.
17687 @end defmac
17689 @defmac AC_HAVE_POUNDBANG
17690 @acindex{HAVE_POUNDBANG}
17691 Replaced by @code{AC_SYS_INTERPRETER} (@pxref{AC_SYS_INTERPRETER}).
17692 @end defmac
17694 @defmac AC_HEADER_CHECK
17695 @acindex{HEADER_CHECK}
17696 Replaced by @code{AC_CHECK_HEADER} (@pxref{AC_CHECK_HEADER}).
17697 @end defmac
17699 @defmac AC_HEADER_EGREP
17700 @acindex{HEADER_EGREP}
17701 Replaced by @code{AC_EGREP_HEADER} (@pxref{AC_EGREP_HEADER}).
17702 @end defmac
17704 @defmac AC_HELP_STRING
17705 @acindex{HELP_STRING}
17706 Replaced by @code{AS_HELP_STRING} (@pxref{AS_HELP_STRING}).
17707 @end defmac
17709 @defmac AC_INIT (@var{unique-file-in-source-dir})
17710 @acindex{INIT}
17711 Formerly @code{AC_INIT} used to have a single argument, and was
17712 equivalent to:
17714 @example
17715 AC_INIT
17716 AC_CONFIG_SRCDIR(@var{unique-file-in-source-dir})
17717 @end example
17718 See @ref{AC_INIT} and @ref{AC_CONFIG_SRCDIR}.
17719 @end defmac
17721 @defmac AC_INLINE
17722 @acindex{INLINE}
17723 Replaced by @code{AC_C_INLINE} (@pxref{AC_C_INLINE}).
17724 @end defmac
17726 @defmac AC_INT_16_BITS
17727 @acindex{INT_16_BITS}
17728 @cvindex INT_16_BITS
17729 If the C type @code{int} is 16 bits wide, define @code{INT_16_BITS}.
17730 Use @samp{AC_CHECK_SIZEOF(int)} instead (@pxref{AC_CHECK_SIZEOF}).
17731 @end defmac
17733 @defmac AC_IRIX_SUN
17734 @acindex{IRIX_SUN}
17735 If on @sc{irix} (Silicon Graphics Unix), add @option{-lsun} to output
17736 @code{LIBS}.  If you were using it to get @code{getmntent}, use
17737 @code{AC_FUNC_GETMNTENT} instead.  If you used it for the NIS versions
17738 of the password and group functions, use @samp{AC_CHECK_LIB(sun,
17739 getpwnam)}.  Up to Autoconf 2.13, it used to be
17741 @example
17742 AC_CHECK_LIB([sun], [getmntent], [LIBS="-lsun $LIBS"])
17743 @end example
17745 @noindent
17746 now it is defined as
17748 @example
17749 AC_FUNC_GETMNTENT
17750 AC_CHECK_LIB([sun], [getpwnam])
17751 @end example
17753 @noindent
17754 See @ref{AC_FUNC_GETMNTENT} and @ref{AC_CHECK_LIB}.
17755 @end defmac
17757 @defmac AC_ISC_POSIX
17758 @acindex{ISC_POSIX}
17759 @ovindex LIBS
17760 This macro adds @option{-lcposix} to output variable @code{LIBS} if
17761 necessary for Posix facilities.  Sun dropped support for the obsolete
17762 @sc{interactive} Systems Corporation Unix on 2006-07-23.  New programs
17763 need not use this macro.  It is implemented as
17764 @code{AC_SEARCH_LIBS([strerror], [cposix])} (@pxref{AC_SEARCH_LIBS}).
17765 @end defmac
17767 @defmac AC_LANG_C
17768 @acindex{LANG_C}
17769 Same as @samp{AC_LANG([C])} (@pxref{AC_LANG}).
17770 @end defmac
17772 @defmac AC_LANG_CPLUSPLUS
17773 @acindex{LANG_CPLUSPLUS}
17774 Same as @samp{AC_LANG([C++])} (@pxref{AC_LANG}).
17775 @end defmac
17777 @defmac AC_LANG_FORTRAN77
17778 @acindex{LANG_FORTRAN77}
17779 Same as @samp{AC_LANG([Fortran 77])} (@pxref{AC_LANG}).
17780 @end defmac
17782 @defmac AC_LANG_RESTORE
17783 @acindex{LANG_RESTORE}
17784 Select the @var{language} that is saved on the top of the stack, as set
17785 by @code{AC_LANG_SAVE}, remove it from the stack, and call
17786 @code{AC_LANG(@var{language})}.  @xref{Language Choice}, for the
17787 preferred way to change languages.
17788 @end defmac
17790 @defmac AC_LANG_SAVE
17791 @acindex{LANG_SAVE}
17792 Remember the current language (as set by @code{AC_LANG}) on a stack.
17793 The current language does not change.  @code{AC_LANG_PUSH} is preferred
17794 (@pxref{AC_LANG_PUSH}).
17795 @end defmac
17797 @defmac AC_LINK_FILES (@var{source}@dots{}, @var{dest}@dots{})
17798 @acindex{LINK_FILES}
17799 This is an obsolete version of @code{AC_CONFIG_LINKS}
17800 (@pxref{AC_CONFIG_LINKS}.  An updated version of:
17802 @example
17803 AC_LINK_FILES(config/$machine.h config/$obj_format.h,
17804               host.h            object.h)
17805 @end example
17807 @noindent
17810 @example
17811 AC_CONFIG_LINKS([host.h:config/$machine.h
17812                 object.h:config/$obj_format.h])
17813 @end example
17814 @end defmac
17816 @defmac AC_LN_S
17817 @acindex{LN_S}
17818 Replaced by @code{AC_PROG_LN_S} (@pxref{AC_PROG_LN_S}).
17819 @end defmac
17821 @defmac AC_LONG_64_BITS
17822 @acindex{LONG_64_BITS}
17823 @cvindex LONG_64_BITS
17824 Define @code{LONG_64_BITS} if the C type @code{long int} is 64 bits wide.
17825 Use the generic macro @samp{AC_CHECK_SIZEOF([long int])} instead
17826 (@pxref{AC_CHECK_SIZEOF}).
17827 @end defmac
17829 @defmac AC_LONG_DOUBLE
17830 @acindex{LONG_DOUBLE}
17831 If the C compiler supports a working @code{long double} type with more
17832 range or precision than the @code{double} type, define
17833 @code{HAVE_LONG_DOUBLE}.
17835 You should use @code{AC_TYPE_LONG_DOUBLE} or
17836 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
17837 @end defmac
17839 @defmac AC_LONG_FILE_NAMES
17840 @acindex{LONG_FILE_NAMES}
17841 Replaced by
17842 @example
17843 AC_SYS_LONG_FILE_NAMES
17844 @end example
17845 @noindent
17846 @xref{AC_SYS_LONG_FILE_NAMES}.
17847 @end defmac
17849 @defmac AC_MAJOR_HEADER
17850 @acindex{MAJOR_HEADER}
17851 Replaced by @code{AC_HEADER_MAJOR} (@pxref{AC_HEADER_MAJOR}).
17852 @end defmac
17854 @defmac AC_MEMORY_H
17855 @acindex{MEMORY_H}
17856 @cvindex NEED_MEMORY_H
17857 Used to define @code{NEED_MEMORY_H} if the @code{mem} functions were
17858 defined in @file{memory.h}.  Today it is equivalent to
17859 @samp{AC_CHECK_HEADERS([memory.h])} (@pxref{AC_CHECK_HEADERS}).  Adjust
17860 your code to depend upon
17861 @code{HAVE_MEMORY_H}, not @code{NEED_MEMORY_H}; see @ref{Standard
17862 Symbols}.
17863 @end defmac
17865 @defmac AC_MINGW32
17866 @acindex{MINGW32}
17867 Similar to @code{AC_CYGWIN} but checks for the MinGW compiler
17868 environment and sets @code{MINGW32}.  Don't use this macro, the
17869 dignified means to check the nature of the host is using
17870 @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
17871 @end defmac
17873 @defmac AC_MINIX
17874 @acindex{MINIX}
17875 @cvindex _MINIX
17876 @cvindex _POSIX_SOURCE
17877 @cvindex _POSIX_1_SOURCE
17878 This macro is a platform-specific subset of
17879 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
17880 @end defmac
17882 @defmac AC_MINUS_C_MINUS_O
17883 @acindex{MINUS_C_MINUS_O}
17884 Replaced by @code{AC_PROG_CC_C_O} (@pxref{AC_PROG_CC_C_O}).
17885 @end defmac
17887 @defmac AC_MMAP
17888 @acindex{MMAP}
17889 Replaced by @code{AC_FUNC_MMAP} (@pxref{AC_FUNC_MMAP}).
17890 @end defmac
17892 @defmac AC_MODE_T
17893 @acindex{MODE_T}
17894 Replaced by @code{AC_TYPE_MODE_T} (@pxref{AC_TYPE_MODE_T}).
17895 @end defmac
17897 @defmac AC_OBJEXT
17898 @acindex{OBJEXT}
17899 @ovindex OBJEXT
17900 Defined the output variable @code{OBJEXT} based on the output of the
17901 compiler, after .c files have been excluded.  Typically set to @samp{o}
17902 if Posix, @samp{obj} if a @acronym{DOS} variant.
17903 Now the compiler checking macros handle
17904 this automatically.
17905 @end defmac
17907 @defmac AC_OBSOLETE (@var{this-macro-name}, @ovar{suggestion})
17908 @acindex{OBSOLETE}
17909 Make M4 print a message to the standard error output warning that
17910 @var{this-macro-name} is obsolete, and giving the file and line number
17911 where it was called.  @var{this-macro-name} should be the name of the
17912 macro that is calling @code{AC_OBSOLETE}.  If @var{suggestion} is given,
17913 it is printed at the end of the warning message; for example, it can be
17914 a suggestion for what to use instead of @var{this-macro-name}.
17916 For instance
17918 @example
17919 AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl
17920 @end example
17922 @noindent
17923 You are encouraged to use @code{AU_DEFUN} instead, since it gives better
17924 services to the user (@pxref{AU_DEFUN}).
17925 @end defmac
17927 @defmac AC_OFF_T
17928 @acindex{OFF_T}
17929 Replaced by @code{AC_TYPE_OFF_T} (@pxref{AC_TYPE_OFF_T}).
17930 @end defmac
17932 @defmac AC_OUTPUT (@ovar{file}@dots{}, @ovar{extra-cmds}, @ovar{init-cmds})
17933 @acindex{OUTPUT}
17934 The use of @code{AC_OUTPUT} with arguments is deprecated.  This obsoleted
17935 interface is equivalent to:
17937 @example
17938 @group
17939 AC_CONFIG_FILES(@var{file}@dots{})
17940 AC_CONFIG_COMMANDS([default],
17941                    @var{extra-cmds}, @var{init-cmds})
17942 AC_OUTPUT
17943 @end group
17944 @end example
17946 @noindent
17947 See @ref{AC_CONFIG_FILES}, @ref{AC_CONFIG_COMMANDS}, and @ref{AC_OUTPUT}.
17948 @end defmac
17950 @defmac AC_OUTPUT_COMMANDS (@var{extra-cmds}, @ovar{init-cmds})
17951 @acindex{OUTPUT_COMMANDS}
17952 Specify additional shell commands to run at the end of
17953 @file{config.status}, and shell commands to initialize any variables
17954 from @command{configure}.  This macro may be called multiple times.  It is
17955 obsolete, replaced by @code{AC_CONFIG_COMMANDS} (@pxref{AC_CONFIG_COMMANDS}).
17957 Here is an unrealistic example:
17959 @example
17960 fubar=27
17961 AC_OUTPUT_COMMANDS([echo this is extra $fubar, and so on.],
17962                    [fubar=$fubar])
17963 AC_OUTPUT_COMMANDS([echo this is another, extra, bit],
17964                    [echo init bit])
17965 @end example
17967 Aside from the fact that @code{AC_CONFIG_COMMANDS} requires an
17968 additional key, an important difference is that
17969 @code{AC_OUTPUT_COMMANDS} is quoting its arguments twice, unlike
17970 @code{AC_CONFIG_COMMANDS}.  This means that @code{AC_CONFIG_COMMANDS}
17971 can safely be given macro calls as arguments:
17973 @example
17974 AC_CONFIG_COMMANDS(foo, [my_FOO()])
17975 @end example
17977 @noindent
17978 Conversely, where one level of quoting was enough for literal strings
17979 with @code{AC_OUTPUT_COMMANDS}, you need two with
17980 @code{AC_CONFIG_COMMANDS}.  The following lines are equivalent:
17982 @example
17983 @group
17984 AC_OUTPUT_COMMANDS([echo "Square brackets: []"])
17985 AC_CONFIG_COMMANDS([default], [[echo "Square brackets: []"]])
17986 @end group
17987 @end example
17988 @end defmac
17990 @defmac AC_PID_T
17991 @acindex{PID_T}
17992 Replaced by @code{AC_TYPE_PID_T} (@pxref{AC_TYPE_PID_T}).
17993 @end defmac
17995 @defmac AC_PREFIX
17996 @acindex{PREFIX}
17997 Replaced by @code{AC_PREFIX_PROGRAM} (@pxref{AC_PREFIX_PROGRAM}).
17998 @end defmac
18000 @defmac AC_PROGRAMS_CHECK
18001 @acindex{PROGRAMS_CHECK}
18002 Replaced by @code{AC_CHECK_PROGS} (@pxref{AC_CHECK_PROGS}).
18003 @end defmac
18005 @defmac AC_PROGRAMS_PATH
18006 @acindex{PROGRAMS_PATH}
18007 Replaced by @code{AC_PATH_PROGS} (@pxref{AC_PATH_PROGS}).
18008 @end defmac
18010 @defmac AC_PROGRAM_CHECK
18011 @acindex{PROGRAM_CHECK}
18012 Replaced by @code{AC_CHECK_PROG} (@pxref{AC_CHECK_PROG}).
18013 @end defmac
18015 @defmac AC_PROGRAM_EGREP
18016 @acindex{PROGRAM_EGREP}
18017 Replaced by @code{AC_EGREP_CPP} (@pxref{AC_EGREP_CPP}).
18018 @end defmac
18020 @defmac AC_PROGRAM_PATH
18021 @acindex{PROGRAM_PATH}
18022 Replaced by @code{AC_PATH_PROG} (@pxref{AC_PATH_PROG}).
18023 @end defmac
18025 @defmac AC_REMOTE_TAPE
18026 @acindex{REMOTE_TAPE}
18027 Removed because of limited usefulness.
18028 @end defmac
18030 @defmac AC_RESTARTABLE_SYSCALLS
18031 @acindex{RESTARTABLE_SYSCALLS}
18032 This macro was renamed @code{AC_SYS_RESTARTABLE_SYSCALLS}.  However,
18033 these days portable programs should use @code{sigaction} with
18034 @code{SA_RESTART} if they want restartable system calls.  They should
18035 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
18036 system call is restartable is a dynamic issue, not a configuration-time
18037 issue.
18038 @end defmac
18040 @defmac AC_RETSIGTYPE
18041 @acindex{RETSIGTYPE}
18042 Replaced by @code{AC_TYPE_SIGNAL} (@pxref{AC_TYPE_SIGNAL}).
18043 @end defmac
18045 @defmac AC_RSH
18046 @acindex{RSH}
18047 Removed because of limited usefulness.
18048 @end defmac
18050 @defmac AC_SCO_INTL
18051 @acindex{SCO_INTL}
18052 @ovindex LIBS
18053 If on SCO Unix, add @option{-lintl} to output variable @code{LIBS}.  This
18054 macro used to do this:
18056 @example
18057 AC_CHECK_LIB([intl], [strftime], [LIBS="-lintl $LIBS"])
18058 @end example
18060 @noindent
18061 Now it just calls @code{AC_FUNC_STRFTIME} instead (@pxref{AC_FUNC_STRFTIME}).
18062 @end defmac
18064 @defmac AC_SETVBUF_REVERSED
18065 @acindex{SETVBUF_REVERSED}
18066 Replaced by
18067 @example
18068 AC_FUNC_SETVBUF_REVERSED
18069 @end example
18070 @noindent
18071 @xref{AC_FUNC_SETVBUF_REVERSED}.
18072 @end defmac
18074 @defmac AC_SET_MAKE
18075 @acindex{SET_MAKE}
18076 Replaced by @code{AC_PROG_MAKE_SET} (@pxref{AC_PROG_MAKE_SET}).
18077 @end defmac
18079 @defmac AC_SIZEOF_TYPE
18080 @acindex{SIZEOF_TYPE}
18081 Replaced by @code{AC_CHECK_SIZEOF} (@pxref{AC_CHECK_SIZEOF}).
18082 @end defmac
18084 @defmac AC_SIZE_T
18085 @acindex{SIZE_T}
18086 Replaced by @code{AC_TYPE_SIZE_T} (@pxref{AC_TYPE_SIZE_T}).
18087 @end defmac
18089 @defmac AC_STAT_MACROS_BROKEN
18090 @acindex{STAT_MACROS_BROKEN}
18091 Replaced by @code{AC_HEADER_STAT} (@pxref{AC_HEADER_STAT}).
18092 @end defmac
18094 @defmac AC_STDC_HEADERS
18095 @acindex{STDC_HEADERS}
18096 Replaced by @code{AC_HEADER_STDC} (@pxref{AC_HEADER_STDC}).
18097 @end defmac
18099 @defmac AC_STRCOLL
18100 @acindex{STRCOLL}
18101 Replaced by @code{AC_FUNC_STRCOLL} (@pxref{AC_FUNC_STRCOLL}).
18102 @end defmac
18104 @defmac AC_STRUCT_ST_BLKSIZE
18105 @acindex{STRUCT_ST_BLKSIZE}
18106 @cvindex HAVE_STRUCT_STAT_ST_BLKSIZE
18107 @cvindex HAVE_ST_BLKSIZE
18108 If @code{struct stat} contains an @code{st_blksize} member, define
18109 @code{HAVE_STRUCT_STAT_ST_BLKSIZE}.  The former name,
18110 @code{HAVE_ST_BLKSIZE} is to be avoided, as its support will cease in
18111 the future.  This macro is obsoleted, and should be replaced by
18113 @example
18114 AC_CHECK_MEMBERS([struct stat.st_blksize])
18115 @end example
18116 @noindent
18117 @xref{AC_CHECK_MEMBERS}.
18118 @end defmac
18120 @defmac AC_STRUCT_ST_RDEV
18121 @acindex{STRUCT_ST_RDEV}
18122 @cvindex HAVE_ST_RDEV
18123 @cvindex HAVE_STRUCT_STAT_ST_RDEV
18124 If @code{struct stat} contains an @code{st_rdev} member, define
18125 @code{HAVE_STRUCT_STAT_ST_RDEV}.  The former name for this macro,
18126 @code{HAVE_ST_RDEV}, is to be avoided as it will cease to be supported
18127 in the future.  Actually, even the new macro is obsolete and should be
18128 replaced by:
18129 @example
18130 AC_CHECK_MEMBERS([struct stat.st_rdev])
18131 @end example
18132 @noindent
18133 @xref{AC_CHECK_MEMBERS}.
18134 @end defmac
18136 @defmac AC_ST_BLKSIZE
18137 @acindex{ST_BLKSIZE}
18138 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
18139 @end defmac
18141 @defmac AC_ST_BLOCKS
18142 @acindex{ST_BLOCKS}
18143 Replaced by @code{AC_STRUCT_ST_BLOCKS} (@pxref{AC_STRUCT_ST_BLOCKS}).
18144 @end defmac
18146 @defmac AC_ST_RDEV
18147 @acindex{ST_RDEV}
18148 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
18149 @end defmac
18151 @defmac AC_SYS_RESTARTABLE_SYSCALLS
18152 @acindex{SYS_RESTARTABLE_SYSCALLS}
18153 @cvindex HAVE_RESTARTABLE_SYSCALLS
18154 If the system automatically restarts a system call that is interrupted
18155 by a signal, define @code{HAVE_RESTARTABLE_SYSCALLS}.  This macro does
18156 not check whether system calls are restarted in general---it checks whether a
18157 signal handler installed with @code{signal} (but not @code{sigaction})
18158 causes system calls to be restarted.  It does not check whether system calls
18159 can be restarted when interrupted by signals that have no handler.
18161 These days portable programs should use @code{sigaction} with
18162 @code{SA_RESTART} if they want restartable system calls.  They should
18163 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
18164 system call is restartable is a dynamic issue, not a configuration-time
18165 issue.
18166 @end defmac
18168 @defmac AC_SYS_SIGLIST_DECLARED
18169 @acindex{SYS_SIGLIST_DECLARED}
18170 This macro was renamed @code{AC_DECL_SYS_SIGLIST}.  However, even that
18171 name is obsolete, as the same functionality is now acheived via
18172 @code{AC_CHECK_DECLS} (@pxref{AC_CHECK_DECLS}).
18173 @end defmac
18175 @defmac AC_TEST_CPP
18176 @acindex{TEST_CPP}
18177 This macro was renamed @code{AC_TRY_CPP}, which in turn was replaced by
18178 @code{AC_PREPROC_IFELSE} (@pxref{AC_PREPROC_IFELSE}).
18179 @end defmac
18181 @defmac AC_TEST_PROGRAM
18182 @acindex{TEST_PROGRAM}
18183 This macro was renamed @code{AC_TRY_RUN}, which in turn was replaced by
18184 @code{AC_RUN_IFELSE} (@pxref{AC_RUN_IFELSE}).
18185 @end defmac
18187 @defmac AC_TIMEZONE
18188 @acindex{TIMEZONE}
18189 Replaced by @code{AC_STRUCT_TIMEZONE} (@pxref{AC_STRUCT_TIMEZONE}).
18190 @end defmac
18192 @defmac AC_TIME_WITH_SYS_TIME
18193 @acindex{TIME_WITH_SYS_TIME}
18194 Replaced by @code{AC_HEADER_TIME} (@pxref{AC_HEADER_TIME}).
18195 @end defmac
18197 @defmac AC_TRY_COMPILE (@var{includes}, @var{function-body}, @
18198   @ovar{action-if-true}, @ovar{action-if-false})
18199 @acindex{TRY_COMPILE}
18200 Same as:
18202 @example
18203 AC_COMPILE_IFELSE(
18204   [AC_LANG_PROGRAM([[@var{includes}]],
18205      [[@var{function-body}]])],
18206   [@var{action-if-true}],
18207   [@var{action-if-false}])
18208 @end example
18210 @noindent
18211 @xref{Running the Compiler}.
18213 This macro double quotes both @var{includes} and @var{function-body}.
18215 For C and C++, @var{includes} is any @code{#include} statements needed
18216 by the code in @var{function-body} (@var{includes} is ignored if
18217 the currently selected language is Fortran or Fortran 77).  The compiler
18218 and compilation flags are determined by the current language
18219 (@pxref{Language Choice}).
18220 @end defmac
18222 @defmac AC_TRY_CPP (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
18223 @acindex{TRY_CPP}
18224 Same as:
18226 @example
18227 AC_PREPROC_IFELSE(
18228   [AC_LANG_SOURCE([[@var{input}]])],
18229   [@var{action-if-true}],
18230   [@var{action-if-false}])
18231 @end example
18233 @noindent
18234 @xref{Running the Preprocessor}.
18236 This macro double quotes the @var{input}.
18237 @end defmac
18239 @defmac AC_TRY_LINK (@var{includes}, @var{function-body}, @
18240   @ovar{action-if-true}, @ovar{action-if-false})
18241 @acindex{TRY_LINK}
18242 Same as:
18244 @example
18245 AC_LINK_IFELSE(
18246   [AC_LANG_PROGRAM([[@var{includes}]],
18247      [[@var{function-body}]])],
18248   [@var{action-if-true}],
18249   [@var{action-if-false}])
18250 @end example
18252 @noindent
18253 @xref{Running the Compiler}.
18255 This macro double quotes both @var{includes} and @var{function-body}.
18257 Depending on the current language (@pxref{Language Choice}), create a
18258 test program to see whether a function whose body consists of
18259 @var{function-body} can be compiled and linked.  If the file compiles
18260 and links successfully, run shell commands @var{action-if-found},
18261 otherwise run @var{action-if-not-found}.
18263 This macro double quotes both @var{includes} and @var{function-body}.
18265 For C and C++, @var{includes} is any @code{#include} statements needed
18266 by the code in @var{function-body} (@var{includes} is ignored if
18267 the currently selected language is Fortran or Fortran 77).  The compiler
18268 and compilation flags are determined by the current language
18269 (@pxref{Language Choice}), and in addition @code{LDFLAGS} and
18270 @code{LIBS} are used for linking.
18271 @end defmac
18273 @defmac AC_TRY_LINK_FUNC (@var{function}, @ovar{action-if-found}, @
18274   @ovar{action-if-not-found})
18275 @acindex{TRY_LINK_FUNC}
18276 This macro is equivalent to
18277 @example
18278 AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])],
18279   [@var{action-if-found}], [@var{action-if-not-found}])
18280 @end example
18281 @noindent
18282 @xref{AC_LINK_IFELSE}.
18283 @end defmac
18285 @defmac AC_TRY_RUN (@var{program}, @ovar{action-if-true}, @
18286   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
18287 @acindex{TRY_RUN}
18288 Same as:
18290 @example
18291 AC_RUN_IFELSE(
18292   [AC_LANG_SOURCE([[@var{program}]])],
18293   [@var{action-if-true}],
18294   [@var{action-if-false}],
18295   [@var{action-if-cross-compiling}])
18296 @end example
18298 @noindent
18299 @xref{Runtime}.
18300 @end defmac
18302 @defmac AC_UID_T
18303 @acindex{UID_T}
18304 Replaced by @code{AC_TYPE_UID_T} (@pxref{AC_TYPE_UID_T}).
18305 @end defmac
18307 @defmac AC_UNISTD_H
18308 @acindex{UNISTD_H}
18309 Same as @samp{AC_CHECK_HEADERS([unistd.h])} (@pxref{AC_CHECK_HEADERS}).
18310 @end defmac
18312 @defmac AC_USG
18313 @acindex{USG}
18314 @cvindex USG
18315 Define @code{USG} if the @acronym{BSD} string functions are defined in
18316 @file{strings.h}.  You should no longer depend upon @code{USG}, but on
18317 @code{HAVE_STRING_H}; see @ref{Standard Symbols}.
18318 @end defmac
18320 @defmac AC_UTIME_NULL
18321 @acindex{UTIME_NULL}
18322 Replaced by @code{AC_FUNC_UTIME_NULL} (@pxref{AC_FUNC_UTIME_NULL}).
18323 @end defmac
18325 @defmac AC_VALIDATE_CACHED_SYSTEM_TUPLE (@ovar{cmd})
18326 @acindex{VALIDATE_CACHED_SYSTEM_TUPLE}
18327 If the cache file is inconsistent with the current host, target and
18328 build system types, it used to execute @var{cmd} or print a default
18329 error message.  This is now handled by default.
18330 @end defmac
18332 @defmac AC_VERBOSE (@var{result-description})
18333 @acindex{VERBOSE}
18334 Replaced by @code{AC_MSG_RESULT} (@pxref{AC_MSG_RESULT}).
18335 @end defmac
18337 @defmac AC_VFORK
18338 @acindex{VFORK}
18339 Replaced by @code{AC_FUNC_FORK} (@pxref{AC_FUNC_FORK}).
18340 @end defmac
18342 @defmac AC_VPRINTF
18343 @acindex{VPRINTF}
18344 Replaced by @code{AC_FUNC_VPRINTF} (@pxref{AC_FUNC_VPRINTF}).
18345 @end defmac
18347 @defmac AC_WAIT3
18348 @acindex{WAIT3}
18349 This macro was renamed @code{AC_FUNC_WAIT3}.  However, these days
18350 portable programs should use @code{waitpid}, not @code{wait3}, as
18351 @code{wait3} has been removed from Posix.
18352 @end defmac
18354 @defmac AC_WARN
18355 @acindex{WARN}
18356 Replaced by @code{AC_MSG_WARN} (@pxref{AC_MSG_WARN}).
18357 @end defmac
18359 @defmac AC_WITH (@var{package}, @var{action-if-given}, @
18360   @ovar{action-if-not-given})
18361 @acindex{WITH}
18362 This is an obsolete version of @code{AC_ARG_WITH} that does not
18363 support providing a help string (@pxref{AC_ARG_WITH}).
18364 @end defmac
18366 @defmac AC_WORDS_BIGENDIAN
18367 @acindex{WORDS_BIGENDIAN}
18368 Replaced by @code{AC_C_BIGENDIAN} (@pxref{AC_C_BIGENDIAN}).
18369 @end defmac
18371 @defmac AC_XENIX_DIR
18372 @acindex{XENIX_DIR}
18373 @ovindex LIBS
18374 This macro used to add @option{-lx} to output variable @code{LIBS} if on
18375 Xenix.  Also, if @file{dirent.h} is being checked for, added
18376 @option{-ldir} to @code{LIBS}.  Now it is merely an alias of
18377 @code{AC_HEADER_DIRENT} instead, plus some code to detect whether
18378 running @sc{xenix} on which you should not depend:
18380 @example
18381 AC_MSG_CHECKING([for Xenix])
18382 AC_EGREP_CPP([yes],
18383 [#if defined M_XENIX && !defined M_UNIX
18384   yes
18385 #endif],
18386              [AC_MSG_RESULT([yes]); XENIX=yes],
18387              [AC_MSG_RESULT([no]); XENIX=])
18388 @end example
18389 @noindent
18390 Don't use this macro, the dignified means to check the nature of the
18391 host is using @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
18392 @end defmac
18394 @defmac AC_YYTEXT_POINTER
18395 @acindex{YYTEXT_POINTER}
18396 This macro was renamed @code{AC_DECL_YYTEXT}, which in turn was
18397 integrated into @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
18398 @end defmac
18400 @node Autoconf 1
18401 @section Upgrading From Version 1
18402 @cindex Upgrading autoconf
18403 @cindex Autoconf upgrading
18405 Autoconf version 2 is mostly backward compatible with version 1.
18406 However, it introduces better ways to do some things, and doesn't
18407 support some of the ugly things in version 1.  So, depending on how
18408 sophisticated your @file{configure.ac} files are, you might have to do
18409 some manual work in order to upgrade to version 2.  This chapter points
18410 out some problems to watch for when upgrading.  Also, perhaps your
18411 @command{configure} scripts could benefit from some of the new features in
18412 version 2; the changes are summarized in the file @file{NEWS} in the
18413 Autoconf distribution.
18415 @menu
18416 * Changed File Names::          Files you might rename
18417 * Changed Makefiles::           New things to put in @file{Makefile.in}
18418 * Changed Macros::              Macro calls you might replace
18419 * Changed Results::             Changes in how to check test results
18420 * Changed Macro Writing::       Better ways to write your own macros
18421 @end menu
18423 @node Changed File Names
18424 @subsection Changed File Names
18426 If you have an @file{aclocal.m4} installed with Autoconf (as opposed to
18427 in a particular package's source directory), you must rename it to
18428 @file{acsite.m4}.  @xref{autoconf Invocation}.
18430 If you distribute @file{install.sh} with your package, rename it to
18431 @file{install-sh} so @code{make} builtin rules don't inadvertently
18432 create a file called @file{install} from it.  @code{AC_PROG_INSTALL}
18433 looks for the script under both names, but it is best to use the new name.
18435 If you were using @file{config.h.top}, @file{config.h.bot}, or
18436 @file{acconfig.h}, you still can, but you have less clutter if you
18437 use the @code{AH_} macros.  @xref{Autoheader Macros}.
18439 @node Changed Makefiles
18440 @subsection Changed Makefiles
18442 Add @samp{@@CFLAGS@@}, @samp{@@CPPFLAGS@@}, and @samp{@@LDFLAGS@@} in
18443 your @file{Makefile.in} files, so they can take advantage of the values
18444 of those variables in the environment when @command{configure} is run.
18445 Doing this isn't necessary, but it's a convenience for users.
18447 Also add @samp{@@configure_input@@} in a comment to each input file for
18448 @code{AC_OUTPUT}, so that the output files contain a comment saying
18449 they were produced by @command{configure}.  Automatically selecting the
18450 right comment syntax for all the kinds of files that people call
18451 @code{AC_OUTPUT} on became too much work.
18453 Add @file{config.log} and @file{config.cache} to the list of files you
18454 remove in @code{distclean} targets.
18456 If you have the following in @file{Makefile.in}:
18458 @example
18459 prefix = /usr/local
18460 exec_prefix = $(prefix)
18461 @end example
18463 @noindent
18464 you must change it to:
18466 @example
18467 prefix = @@prefix@@
18468 exec_prefix = @@exec_prefix@@
18469 @end example
18471 @noindent
18472 The old behavior of replacing those variables without @samp{@@}
18473 characters around them has been removed.
18475 @node Changed Macros
18476 @subsection Changed Macros
18478 Many of the macros were renamed in Autoconf version 2.  You can still
18479 use the old names, but the new ones are clearer, and it's easier to find
18480 the documentation for them.  @xref{Obsolete Macros}, for a table showing the
18481 new names for the old macros.  Use the @command{autoupdate} program to
18482 convert your @file{configure.ac} to using the new macro names.
18483 @xref{autoupdate Invocation}.
18485 Some macros have been superseded by similar ones that do the job better,
18486 but are not call-compatible.  If you get warnings about calling obsolete
18487 macros while running @command{autoconf}, you may safely ignore them, but
18488 your @command{configure} script generally works better if you follow
18489 the advice that is printed about what to replace the obsolete macros with.  In
18490 particular, the mechanism for reporting the results of tests has
18491 changed.  If you were using @command{echo} or @code{AC_VERBOSE} (perhaps
18492 via @code{AC_COMPILE_CHECK}), your @command{configure} script's output
18493 looks better if you switch to @code{AC_MSG_CHECKING} and
18494 @code{AC_MSG_RESULT}.  @xref{Printing Messages}.  Those macros work best
18495 in conjunction with cache variables.  @xref{Caching Results}.
18499 @node Changed Results
18500 @subsection Changed Results
18502 If you were checking the results of previous tests by examining the
18503 shell variable @code{DEFS}, you need to switch to checking the values of
18504 the cache variables for those tests.  @code{DEFS} no longer exists while
18505 @command{configure} is running; it is only created when generating output
18506 files.  This difference from version 1 is because properly quoting the
18507 contents of that variable turned out to be too cumbersome and
18508 inefficient to do every time @code{AC_DEFINE} is called.  @xref{Cache
18509 Variable Names}.
18511 For example, here is a @file{configure.ac} fragment written for Autoconf
18512 version 1:
18514 @example
18515 AC_HAVE_FUNCS(syslog)
18516 case "$DEFS" in
18517 *-DHAVE_SYSLOG*) ;;
18518 *) # syslog is not in the default libraries.  See if it's in some other.
18519   saved_LIBS="$LIBS"
18520   for lib in bsd socket inet; do
18521     AC_CHECKING(for syslog in -l$lib)
18522     LIBS="-l$lib $saved_LIBS"
18523     AC_HAVE_FUNCS(syslog)
18524     case "$DEFS" in
18525     *-DHAVE_SYSLOG*) break ;;
18526     *) ;;
18527     esac
18528     LIBS="$saved_LIBS"
18529   done ;;
18530 esac
18531 @end example
18533 Here is a way to write it for version 2:
18535 @example
18536 AC_CHECK_FUNCS([syslog])
18537 if test $ac_cv_func_syslog = no; then
18538   # syslog is not in the default libraries.  See if it's in some other.
18539   for lib in bsd socket inet; do
18540     AC_CHECK_LIB([$lib], [syslog], [AC_DEFINE([HAVE_SYSLOG])
18541       LIBS="-l$lib $LIBS"; break])
18542   done
18544 @end example
18546 If you were working around bugs in @code{AC_DEFINE_UNQUOTED} by adding
18547 backslashes before quotes, you need to remove them.  It now works
18548 predictably, and does not treat quotes (except back quotes) specially.
18549 @xref{Setting Output Variables}.
18551 All of the Boolean shell variables set by Autoconf macros now use
18552 @samp{yes} for the true value.  Most of them use @samp{no} for false,
18553 though for backward compatibility some use the empty string instead.  If
18554 you were relying on a shell variable being set to something like 1 or
18555 @samp{t} for true, you need to change your tests.
18557 @node Changed Macro Writing
18558 @subsection Changed Macro Writing
18560 When defining your own macros, you should now use @code{AC_DEFUN}
18561 instead of @code{define}.  @code{AC_DEFUN} automatically calls
18562 @code{AC_PROVIDE} and ensures that macros called via @code{AC_REQUIRE}
18563 do not interrupt other macros, to prevent nested @samp{checking@dots{}}
18564 messages on the screen.  There's no actual harm in continuing to use the
18565 older way, but it's less convenient and attractive.  @xref{Macro
18566 Definitions}.
18568 You probably looked at the macros that came with Autoconf as a guide for
18569 how to do things.  It would be a good idea to take a look at the new
18570 versions of them, as the style is somewhat improved and they take
18571 advantage of some new features.
18573 If you were doing tricky things with undocumented Autoconf internals
18574 (macros, variables, diversions), check whether you need to change
18575 anything to account for changes that have been made.  Perhaps you can
18576 even use an officially supported technique in version 2 instead of
18577 kludging.  Or perhaps not.
18579 To speed up your locally written feature tests, add caching to them.
18580 See whether any of your tests are of general enough usefulness to
18581 encapsulate them into macros that you can share.
18584 @node Autoconf 2.13
18585 @section Upgrading From Version 2.13
18586 @cindex Upgrading autoconf
18587 @cindex Autoconf upgrading
18589 The introduction of the previous section (@pxref{Autoconf 1}) perfectly
18590 suits this section@enddots{}
18592 @quotation
18593 Autoconf version 2.50 is mostly backward compatible with version 2.13.
18594 However, it introduces better ways to do some things, and doesn't
18595 support some of the ugly things in version 2.13.  So, depending on how
18596 sophisticated your @file{configure.ac} files are, you might have to do
18597 some manual work in order to upgrade to version 2.50.  This chapter
18598 points out some problems to watch for when upgrading.  Also, perhaps
18599 your @command{configure} scripts could benefit from some of the new
18600 features in version 2.50; the changes are summarized in the file
18601 @file{NEWS} in the Autoconf distribution.
18602 @end quotation
18604 @menu
18605 * Changed Quotation::           Broken code which used to work
18606 * New Macros::                  Interaction with foreign macros
18607 * Hosts and Cross-Compilation::  Bugward compatibility kludges
18608 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
18609 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
18610 @end menu
18612 @node Changed Quotation
18613 @subsection Changed Quotation
18615 The most important changes are invisible to you: the implementation of
18616 most macros have completely changed.  This allowed more factorization of
18617 the code, better error messages, a higher uniformity of the user's
18618 interface etc.  Unfortunately, as a side effect, some construct which
18619 used to (miraculously) work might break starting with Autoconf 2.50.
18620 The most common culprit is bad quotation.
18622 For instance, in the following example, the message is not properly
18623 quoted:
18625 @example
18626 AC_INIT
18627 AC_CHECK_HEADERS(foo.h, ,
18628   AC_MSG_ERROR(cannot find foo.h, bailing out))
18629 AC_OUTPUT
18630 @end example
18632 @noindent
18633 Autoconf 2.13 simply ignores it:
18635 @example
18636 $ @kbd{autoconf-2.13; ./configure --silent}
18637 creating cache ./config.cache
18638 configure: error: cannot find foo.h
18640 @end example
18642 @noindent
18643 while Autoconf 2.50 produces a broken @file{configure}:
18645 @example
18646 $ @kbd{autoconf-2.50; ./configure --silent}
18647 configure: error: cannot find foo.h
18648 ./configure: exit: bad non-numeric arg `bailing'
18649 ./configure: exit: bad non-numeric arg `bailing'
18651 @end example
18653 The message needs to be quoted, and the @code{AC_MSG_ERROR} invocation
18654 too!
18656 @example
18657 AC_INIT([Example], [1.0], [bug-example@@example.org])
18658 AC_CHECK_HEADERS([foo.h], [],
18659   [AC_MSG_ERROR([cannot find foo.h, bailing out])])
18660 AC_OUTPUT
18661 @end example
18663 Many many (and many more) Autoconf macros were lacking proper quotation,
18664 including no less than@dots{} @code{AC_DEFUN} itself!
18666 @example
18667 $ @kbd{cat configure.in}
18668 AC_DEFUN([AC_PROG_INSTALL],
18669 [# My own much better version
18671 AC_INIT
18672 AC_PROG_INSTALL
18673 AC_OUTPUT
18674 $ @kbd{autoconf-2.13}
18675 autoconf: Undefined macros:
18676 ***BUG in Autoconf--please report*** AC_FD_MSG
18677 ***BUG in Autoconf--please report*** AC_EPI
18678 configure.in:1:AC_DEFUN([AC_PROG_INSTALL],
18679 configure.in:5:AC_PROG_INSTALL
18680 $ @kbd{autoconf-2.50}
18682 @end example
18685 @node New Macros
18686 @subsection New Macros
18688 @cindex undefined macro
18689 @cindex @code{_m4_divert_diversion}
18691 While Autoconf was relatively dormant in the late 1990s, Automake
18692 provided Autoconf-like macros for a while.  Starting with Autoconf 2.50
18693 in 2001, Autoconf provided
18694 versions of these macros, integrated in the @code{AC_} namespace,
18695 instead of @code{AM_}.  But in order to ease the upgrading via
18696 @command{autoupdate}, bindings to such @code{AM_} macros are provided.
18698 Unfortunately older versions of Automake (e.g., Automake 1.4)
18699 did not quote the names of these macros.
18700 Therefore, when @command{m4} finds something like
18701 @samp{AC_DEFUN(AM_TYPE_PTRDIFF_T, @dots{})} in @file{aclocal.m4},
18702 @code{AM_TYPE_PTRDIFF_T} is
18703 expanded, replaced with its Autoconf definition.
18705 Fortunately Autoconf catches pre-@code{AC_INIT} expansions, and
18706 complains, in its own words:
18708 @example
18709 $ @kbd{cat configure.ac}
18710 AC_INIT([Example], [1.0], [bug-example@@example.org])
18711 AM_TYPE_PTRDIFF_T
18712 $ @kbd{aclocal-1.4}
18713 $ @kbd{autoconf}
18714 aclocal.m4:17: error: m4_defn: undefined macro: _m4_divert_diversion
18715 aclocal.m4:17: the top level
18716 autom4te: m4 failed with exit status: 1
18718 @end example
18720 Modern versions of Automake no longer define most of these
18721 macros, and properly quote the names of the remaining macros.
18722 If you must use an old Automake, do not depend upon macros from Automake
18723 as it is simply not its job
18724 to provide macros (but the one it requires itself):
18726 @example
18727 $ @kbd{cat configure.ac}
18728 AC_INIT([Example], [1.0], [bug-example@@example.org])
18729 AM_TYPE_PTRDIFF_T
18730 $ @kbd{rm aclocal.m4}
18731 $ @kbd{autoupdate}
18732 autoupdate: `configure.ac' is updated
18733 $ @kbd{cat configure.ac}
18734 AC_INIT([Example], [1.0], [bug-example@@example.org])
18735 AC_CHECK_TYPES([ptrdiff_t])
18736 $ @kbd{aclocal-1.4}
18737 $ @kbd{autoconf}
18739 @end example
18742 @node Hosts and Cross-Compilation
18743 @subsection Hosts and Cross-Compilation
18744 @cindex Cross compilation
18746 Based on the experience of compiler writers, and after long public
18747 debates, many aspects of the cross-compilation chain have changed:
18749 @itemize @minus
18750 @item
18751 the relationship between the build, host, and target architecture types,
18753 @item
18754 the command line interface for specifying them to @command{configure},
18756 @item
18757 the variables defined in @command{configure},
18759 @item
18760 the enabling of cross-compilation mode.
18761 @end itemize
18763 @sp 1
18765 The relationship between build, host, and target have been cleaned up:
18766 the chain of default is now simply: target defaults to host, host to
18767 build, and build to the result of @command{config.guess}.  Nevertheless,
18768 in order to ease the transition from 2.13 to 2.50, the following
18769 transition scheme is implemented.  @emph{Do not rely on it}, as it will
18770 be completely disabled in a couple of releases (we cannot keep it, as it
18771 proves to cause more problems than it cures).
18773 They all default to the result of running @command{config.guess}, unless
18774 you specify either @option{--build} or @option{--host}.  In this case,
18775 the default becomes the system type you specified.  If you specify both,
18776 and they're different, @command{configure} enters cross compilation
18777 mode, so it doesn't run any tests that require execution.
18779 Hint: if you mean to override the result of @command{config.guess},
18780 prefer @option{--build} over @option{--host}.  In the future,
18781 @option{--host} will not override the name of the build system type.
18782 Whenever you specify @option{--host}, be sure to specify @option{--build}
18783 too.
18785 @sp 1
18787 For backward compatibility, @command{configure} accepts a system
18788 type as an option by itself.  Such an option overrides the
18789 defaults for build, host, and target system types.  The following
18790 configure statement configures a cross toolchain that runs on
18791 Net@acronym{BSD}/alpha but generates code for @acronym{GNU} Hurd/sparc,
18792 which is also the build platform.
18794 @example
18795 ./configure --host=alpha-netbsd sparc-gnu
18796 @end example
18798 @sp 1
18800 In Autoconf 2.13 and before, the variables @code{build}, @code{host},
18801 and @code{target} had a different semantics before and after the
18802 invocation of @code{AC_CANONICAL_BUILD} etc.  Now, the argument of
18803 @option{--build} is strictly copied into @code{build_alias}, and is left
18804 empty otherwise.  After the @code{AC_CANONICAL_BUILD}, @code{build} is
18805 set to the canonicalized build type.  To ease the transition, before,
18806 its contents is the same as that of @code{build_alias}.  Do @emph{not}
18807 rely on this broken feature.
18809 For consistency with the backward compatibility scheme exposed above,
18810 when @option{--host} is specified but @option{--build} isn't, the build
18811 system is assumed to be the same as @option{--host}, and
18812 @samp{build_alias} is set to that value.  Eventually, this
18813 historically incorrect behavior will go away.
18815 @sp 1
18817 The former scheme to enable cross-compilation proved to cause more harm
18818 than good, in particular, it used to be triggered too easily, leaving
18819 regular end users puzzled in front of cryptic error messages.
18820 @command{configure} could even enter cross-compilation mode only
18821 because the compiler was not functional.  This is mainly because
18822 @command{configure} used to try to detect cross-compilation, instead of
18823 waiting for an explicit flag from the user.
18825 Now, @command{configure} enters cross-compilation mode if and only if
18826 @option{--host} is passed.
18828 That's the short documentation.  To ease the transition between 2.13 and
18829 its successors, a more complicated scheme is implemented.  @emph{Do not
18830 rely on the following}, as it will be removed in the near future.
18832 If you specify @option{--host}, but not @option{--build}, when
18833 @command{configure} performs the first compiler test it tries to run
18834 an executable produced by the compiler.  If the execution fails, it
18835 enters cross-compilation mode.  This is fragile.  Moreover, by the time
18836 the compiler test is performed, it may be too late to modify the
18837 build-system type: other tests may have already been performed.
18838 Therefore, whenever you specify @option{--host}, be sure to specify
18839 @option{--build} too.
18841 @example
18842 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
18843 @end example
18845 @noindent
18846 enters cross-compilation mode.  The former interface, which
18847 consisted in setting the compiler to a cross-compiler without informing
18848 @command{configure} is obsolete.  For instance, @command{configure}
18849 fails if it can't run the code generated by the specified compiler if you
18850 configure as follows:
18852 @example
18853 ./configure CC=m68k-coff-gcc
18854 @end example
18857 @node AC_LIBOBJ vs LIBOBJS
18858 @subsection @code{AC_LIBOBJ} vs.@: @code{LIBOBJS}
18860 Up to Autoconf 2.13, the replacement of functions was triggered via the
18861 variable @code{LIBOBJS}.  Since Autoconf 2.50, the macro
18862 @code{AC_LIBOBJ} should be used instead (@pxref{Generic Functions}).
18863 Starting at Autoconf 2.53, the use of @code{LIBOBJS} is an error.
18865 This change is mandated by the unification of the @acronym{GNU} Build System
18866 components.  In particular, the various fragile techniques used to parse
18867 a @file{configure.ac} are all replaced with the use of traces.  As a
18868 consequence, any action must be traceable, which obsoletes critical
18869 variable assignments.  Fortunately, @code{LIBOBJS} was the only problem,
18870 and it can even be handled gracefully (read, ``without your having to
18871 change something'').
18873 There were two typical uses of @code{LIBOBJS}: asking for a replacement
18874 function, and adjusting @code{LIBOBJS} for Automake and/or Libtool.
18876 @sp 1
18878 As for function replacement, the fix is immediate: use
18879 @code{AC_LIBOBJ}.  For instance:
18881 @example
18882 LIBOBJS="$LIBOBJS fnmatch.o"
18883 LIBOBJS="$LIBOBJS malloc.$ac_objext"
18884 @end example
18886 @noindent
18887 should be replaced with:
18889 @example
18890 AC_LIBOBJ([fnmatch])
18891 AC_LIBOBJ([malloc])
18892 @end example
18894 @sp 1
18896 @ovindex LIBOBJDIR
18897 When used with Automake 1.10 or newer, a suitable value for
18898 @code{LIBOBJDIR} is set so that the @code{LIBOBJS} and @code{LTLIBOBJS}
18899 can be referenced from any @file{Makefile.am}.  Even without Automake,
18900 arranging for @code{LIBOBJDIR} to be set correctly enables
18901 referencing @code{LIBOBJS} and @code{LTLIBOBJS} in another directory.
18902 The @code{LIBOBJDIR} feature is experimental.
18905 @node AC_FOO_IFELSE vs AC_TRY_FOO
18906 @subsection @code{AC_FOO_IFELSE} vs.@: @code{AC_TRY_FOO}
18908 Since Autoconf 2.50, internal codes uses @code{AC_PREPROC_IFELSE},
18909 @code{AC_COMPILE_IFELSE}, @code{AC_LINK_IFELSE}, and
18910 @code{AC_RUN_IFELSE} on one hand and @code{AC_LANG_SOURCES},
18911 and @code{AC_LANG_PROGRAM} on the other hand instead of the deprecated
18912 @code{AC_TRY_CPP}, @code{AC_TRY_COMPILE}, @code{AC_TRY_LINK}, and
18913 @code{AC_TRY_RUN}.  The motivations where:
18914 @itemize @minus
18915 @item
18916 a more consistent interface: @code{AC_TRY_COMPILE} etc.@: were double
18917 quoting their arguments;
18919 @item
18920 the combinatoric explosion is solved by decomposing on the one hand the
18921 generation of sources, and on the other hand executing the program;
18923 @item
18924 this scheme helps supporting more languages than plain C and C++.
18925 @end itemize
18927 In addition to the change of syntax, the philosophy has changed too:
18928 while emphasis was put on speed at the expense of accuracy, today's
18929 Autoconf promotes accuracy of the testing framework at, ahem@dots{}, the
18930 expense of speed.
18933 As a perfect example of what is @emph{not} to be done, here is how to
18934 find out whether a header file contains a particular declaration, such
18935 as a typedef, a structure, a structure member, or a function.  Use
18936 @code{AC_EGREP_HEADER} instead of running @code{grep} directly on the
18937 header file; on some systems the symbol might be defined in another
18938 header file that the file you are checking includes.
18940 As a (bad) example, here is how you should not check for C preprocessor
18941 symbols, either defined by header files or predefined by the C
18942 preprocessor: using @code{AC_EGREP_CPP}:
18944 @example
18945 @group
18946 AC_EGREP_CPP(yes,
18947 [#ifdef _AIX
18948   yes
18949 #endif
18950 ], is_aix=yes, is_aix=no)
18951 @end group
18952 @end example
18954 The above example, properly written would (i) use
18955 @code{AC_LANG_PROGRAM}, and (ii) run the compiler:
18957 @example
18958 @group
18959 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
18960 [[#ifndef _AIX
18961  error: This isn't AIX!
18962 #endif
18963 ]])],
18964                    [is_aix=yes],
18965                    [is_aix=no])
18966 @end group
18967 @end example
18970 @c ============================= Generating Test Suites with Autotest
18972 @node Using Autotest
18973 @chapter Generating Test Suites with Autotest
18975 @cindex Autotest
18977 @display
18978 @strong{N.B.: This section describes an experimental feature which will
18979 be part of Autoconf in a forthcoming release.  Although we believe
18980 Autotest is stabilizing, this documentation describes an interface which
18981 might change in the future: do not depend upon Autotest without
18982 subscribing to the Autoconf mailing lists.}
18983 @end display
18985 It is paradoxical that portable projects depend on nonportable tools
18986 to run their test suite.  Autoconf by itself is the paragon of this
18987 problem: although it aims at perfectly portability, up to 2.13 its
18988 test suite was using Deja@acronym{GNU}, a rich and complex testing
18989 framework, but which is far from being standard on Posix systems.
18990 Worse yet, it was likely to be missing on the most fragile platforms,
18991 the very platforms that are most likely to torture Autoconf and
18992 exhibit deficiencies.
18994 To circumvent this problem, many package maintainers have developed their
18995 own testing framework, based on simple shell scripts whose sole outputs
18996 are exit status values describing whether the test succeeded.  Most of
18997 these tests share common patterns, and this can result in lots of
18998 duplicated code and tedious maintenance.
19000 Following exactly the same reasoning that yielded to the inception of
19001 Autoconf, Autotest provides a test suite generation framework, based on
19002 M4 macros building a portable shell script.  The suite itself is
19003 equipped with automatic logging and tracing facilities which greatly
19004 diminish the interaction with bug reporters, and simple timing reports.
19006 Autoconf itself has been using Autotest for years, and we do attest that
19007 it has considerably improved the strength of the test suite and the
19008 quality of bug reports.  Other projects are known to use some generation
19009 of Autotest, such as Bison, Free Recode, Free Wdiff, @acronym{GNU} Tar, each of
19010 them with different needs, and this usage has validated Autotest as a general
19011 testing framework.
19013 Nonetheless, compared to Deja@acronym{GNU}, Autotest is inadequate for
19014 interactive tool testing, which is probably its main limitation.
19016 @menu
19017 * Using an Autotest Test Suite::  Autotest and the user
19018 * Writing Testsuites::          Autotest macros
19019 * testsuite Invocation::        Running @command{testsuite} scripts
19020 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
19021 @end menu
19023 @node Using an Autotest Test Suite
19024 @section Using an Autotest Test Suite
19026 @menu
19027 * testsuite Scripts::           The concepts of Autotest
19028 * Autotest Logs::               Their contents
19029 @end menu
19031 @node testsuite Scripts
19032 @subsection @command{testsuite} Scripts
19034 @cindex @command{testsuite}
19036 Generating testing or validation suites using Autotest is rather easy.
19037 The whole validation suite is held in a file to be processed through
19038 @command{autom4te}, itself using @acronym{GNU} M4 under the scene, to
19039 produce a stand-alone Bourne shell script which then gets distributed.
19040 Neither @command{autom4te} nor @acronym{GNU} M4 are needed at
19041 the installer's end.
19043 @cindex test group
19044 Each test of the validation suite should be part of some test group.  A
19045 @dfn{test group} is a sequence of interwoven tests that ought to be
19046 executed together, usually because one test in the group creates data
19047 files than a later test in the same group needs to read.  Complex test
19048 groups make later debugging more tedious.  It is much better to
19049 keep only a few tests per test group.  Ideally there is only one test
19050 per test group.
19052 For all but the simplest packages, some file such as @file{testsuite.at}
19053 does not fully hold all test sources, as these are often easier to
19054 maintain in separate files.  Each of these separate files holds a single
19055 test group, or a sequence of test groups all addressing some common
19056 functionality in the package.  In such cases, @file{testsuite.at}
19057 merely initializes the validation suite, and sometimes does elementary
19058 health checking, before listing include statements for all other test
19059 files.  The special file @file{package.m4}, containing the
19060 identification of the package, is automatically included if found.
19062 A convenient alternative consists in moving all the global issues
19063 (local Autotest macros, elementary health checking, and @code{AT_INIT}
19064 invocation) into the file @code{local.at}, and making
19065 @file{testsuite.at} be a simple list of @code{m4_include} of sub test
19066 suites.  In such case, generating the whole test suite or pieces of it
19067 is only a matter of choosing the @command{autom4te} command line
19068 arguments.
19070 The validation scripts that Autotest produces are by convention called
19071 @command{testsuite}.  When run, @command{testsuite} executes each test
19072 group in turn, producing only one summary line per test to say if that
19073 particular test succeeded or failed.  At end of all tests, summarizing
19074 counters get printed.  One debugging directory is left for each test
19075 group which failed, if any: such directories are named
19076 @file{testsuite.dir/@var{nn}}, where @var{nn} is the sequence number of
19077 the test group, and they include:
19079 @itemize @bullet
19080 @item a debugging script named @file{run} which reruns the test in
19081 @dfn{debug mode} (@pxref{testsuite Invocation}).  The automatic generation
19082 of debugging scripts has the purpose of easing the chase for bugs.
19084 @item all the files created with @code{AT_DATA}
19086 @item a log of the run, named @file{testsuite.log}
19087 @end itemize
19089 In the ideal situation, none of the tests fail, and consequently no
19090 debugging directory is left behind for validation.
19092 It often happens in practice that individual tests in the validation
19093 suite need to get information coming out of the configuration process.
19094 Some of this information, common for all validation suites, is provided
19095 through the file @file{atconfig}, automatically created by
19096 @code{AC_CONFIG_TESTDIR}.  For configuration informations which your
19097 testing environment specifically needs, you might prepare an optional
19098 file named @file{atlocal.in}, instantiated by @code{AC_CONFIG_FILES}.
19099 The configuration process produces @file{atconfig} and @file{atlocal}
19100 out of these two input files, and these two produced files are
19101 automatically read by the @file{testsuite} script.
19103 Here is a diagram showing the relationship between files.
19105 @noindent
19106 Files used in preparing a software package for distribution:
19108 @example
19109                 [package.m4] -->.
19110                                  \
19111 subfile-1.at ->.  [local.at] ---->+
19112     ...         \                  \
19113 subfile-i.at ---->-- testsuite.at -->-- autom4te* -->testsuite
19114     ...         /
19115 subfile-n.at ->'
19116 @end example
19118 @noindent
19119 Files used in configuring a software package:
19121 @example
19122                                      .--> atconfig
19123                                     /
19124 [atlocal.in] -->  config.status* --<
19125                                     \
19126                                      `--> [atlocal]
19127 @end example
19129 @noindent
19130 Files created during the test suite execution:
19132 @example
19133 atconfig -->.                    .--> testsuite.log
19134              \                  /
19135               >-- testsuite* --<
19136              /                  \
19137 [atlocal] ->'                    `--> [testsuite.dir]
19138 @end example
19141 @node Autotest Logs
19142 @subsection Autotest Logs
19144 When run, the test suite creates a log file named after itself, e.g., a
19145 test suite named @command{testsuite} creates @file{testsuite.log}.  It
19146 contains a lot of information, usually more than maintainers actually
19147 need, but therefore most of the time it contains all that is needed:
19149 @table @asis
19150 @item command line arguments
19151 @c akim s/to consist in/to consist of/
19152 A bad but unfortunately widespread habit consists of
19153 setting environment variables before the command, such as in
19154 @samp{CC=my-home-grown-cc ./testsuite}.  The test suite does not
19155 know this change, hence (i) it cannot report it to you, and (ii)
19156 it cannot preserve the value of @code{CC} for subsequent runs.
19157 Autoconf faced exactly the same problem, and solved it by asking
19158 users to pass the variable definitions as command line arguments.
19159 Autotest requires this rule, too, but has no means to enforce it; the log
19160 then contains a trace of the variables that were changed by the user.
19162 @item @file{ChangeLog} excerpts
19163 The topmost lines of all the @file{ChangeLog} files found in the source
19164 hierarchy.  This is especially useful when bugs are reported against
19165 development versions of the package, since the version string does not
19166 provide sufficient information to know the exact state of the sources
19167 the user compiled.  Of course, this relies on the use of a
19168 @file{ChangeLog}.
19170 @item build machine
19171 Running a test suite in a cross-compile environment is not an easy task,
19172 since it would mean having the test suite run on a machine @var{build},
19173 while running programs on a machine @var{host}.  It is much simpler to
19174 run both the test suite and the programs on @var{host}, but then, from
19175 the point of view of the test suite, there remains a single environment,
19176 @var{host} = @var{build}.  The log contains relevant information on the
19177 state of the build machine, including some important environment
19178 variables.
19179 @c FIXME: How about having an M4sh macro to say `hey, log the value
19180 @c of `@dots{}'?  This would help both Autoconf and Autotest.
19182 @item tested programs
19183 The absolute file name and answers to @option{--version} of the tested
19184 programs (see @ref{Writing Testsuites}, @code{AT_TESTED}).
19186 @item configuration log
19187 The contents of @file{config.log}, as created by @command{configure},
19188 are appended.  It contains the configuration flags and a detailed report
19189 on the configuration itself.
19190 @end table
19193 @node Writing Testsuites
19194 @section Writing @file{testsuite.at}
19196 The @file{testsuite.at} is a Bourne shell script making use of special
19197 Autotest M4 macros.  It often contains a call to @code{AT_INIT} near
19198 its beginning followed by one call to @code{m4_include} per source file
19199 for tests.  Each such included file, or the remainder of
19200 @file{testsuite.at} if include files are not used, contain a sequence of
19201 test groups.  Each test group begins with a call to @code{AT_SETUP},
19202 then an arbitrary number of shell commands or calls to @code{AT_CHECK},
19203 and then completes with a call to @code{AT_CLEANUP}.
19205 @defmac AT_INIT (@ovar{name})
19206 @atindex{INIT}
19207 @c FIXME: Not clear, plus duplication of the information.
19208 Initialize Autotest.  Giving a @var{name} to the test suite is
19209 encouraged if your package includes several test suites.  In any case,
19210 the test suite always displays the package name and version.  It also
19211 inherits the package bug report address.
19212 @end defmac
19214 @defmac AT_COPYRIGHT (@var{copyright-notice})
19215 @atindex{COPYRIGHT}
19216 @cindex Copyright Notice
19217 State that, in addition to the Free Software Foundation's copyright on
19218 the Autotest macros, parts of your test suite are covered by
19219 @var{copyright-notice}.
19221 The @var{copyright-notice} shows up in both the head of
19222 @command{testsuite} and in @samp{testsuite --version}.
19223 @end defmac
19225 @defmac AT_TESTED (@var{executables})
19226 @atindex{TESTED}
19227 Log the file name and answer to @option{--version} of each program in
19228 space-separated list @var{executables}.  Several invocations register
19229 new executables, in other words, don't fear registering one program
19230 several times.
19231 @end defmac
19233 Autotest test suites rely on @env{PATH} to find the tested program.
19234 This avoids the need to generate absolute names of the various tools, and
19235 makes it possible to test installed programs.  Therefore, knowing which
19236 programs are being exercised is crucial to understanding problems in
19237 the test suite itself, or its occasional misuses.  It is a good idea to
19238 also subscribe foreign programs you depend upon, to avoid incompatible
19239 diagnostics.
19241 @sp 1
19243 @defmac AT_SETUP (@var{test-group-name})
19244 @atindex{SETUP}
19245 This macro starts a group of related tests, all to be executed in the
19246 same subshell.  It accepts a single argument, which holds a few words
19247 (no more than about 30 or 40 characters) quickly describing the purpose
19248 of the test group being started.
19249 @end defmac
19251 @defmac AT_KEYWORDS (@var{keywords})
19252 @atindex{KEYWORDS}
19253 Associate the space-separated list of @var{keywords} to the enclosing
19254 test group.  This makes it possible to run ``slices'' of the test suite.
19255 For instance, if some of your test groups exercise some @samp{foo}
19256 feature, then using @samp{AT_KEYWORDS(foo)} lets you run
19257 @samp{./testsuite -k foo} to run exclusively these test groups.  The
19258 @var{title} of the test group is automatically recorded to
19259 @code{AT_KEYWORDS}.
19261 Several invocations within a test group accumulate new keywords.  In
19262 other words, don't fear registering the same keyword several times in a
19263 test group.
19264 @end defmac
19266 @defmac AT_CAPTURE_FILE (@var{file})
19267 @atindex{CAPTURE_FILE}
19268 If the current test group fails, log the contents of @var{file}.
19269 Several identical calls within one test group have no additional effect.
19270 @end defmac
19272 @defmac AT_XFAIL_IF (@var{shell-condition})
19273 @atindex{XFAIL_IF}
19274 Determine whether the test is expected to fail because it is a known
19275 bug (for unsupported features, you should skip the test).
19276 @var{shell-condition} is a shell expression such as a @code{test}
19277 command; you can instantiate this macro many times from within the
19278 same test group, and one of the conditions is enough to turn
19279 the test into an expected failure.
19280 @end defmac
19282 @defmac AT_CLEANUP
19283 @atindex{CLEANUP}
19284 End the current test group.
19285 @end defmac
19287 @sp 1
19289 @defmac AT_DATA (@var{file}, @var{contents})
19290 @atindex{DATA}
19291 Initialize an input data @var{file} with given @var{contents}.  Of
19292 course, the @var{contents} have to be properly quoted between square
19293 brackets to protect against included commas or spurious M4
19294 expansion.  The contents ought to end with an end of line.
19295 @end defmac
19297 @defmac AT_CHECK (@var{commands}, @dvar{status, 0}, @dvar{stdout, }, @
19298   @dvar{stderr, }, @ovar{run-if-fail}, @ovar{run-if-pass})
19299 @atindex{CHECK}
19300 Execute a test by performing given shell @var{commands}.  These commands
19301 should normally exit with @var{status}, while producing expected
19302 @var{stdout} and @var{stderr} contents.  If @var{commands} exit with
19303 status 77, then the whole test group is skipped.  Otherwise, if this test
19304 fails, run shell commands @var{run-if-fail} or, if this test passes, run shell
19305 commands @var{run-if-pass}.
19307 @c Previously, we had this:
19308 @c  The @var{commands} @emph{must not} redirect the standard output, nor the
19309 @c  standard error.
19310 @c to prevent trigerring the double redirect bug on Ultrix, see
19311 @c `File Descriptors'.  This was too restricting, and Ultrix is pretty
19312 @c much dead, so we dropped the limitation; the obvious workaround on
19313 @c Ultrix is to use a working shell there.
19315 If @var{status}, or @var{stdout}, or @var{stderr} is @samp{ignore}, then
19316 the corresponding value is not checked.
19318 The special value @samp{expout} for @var{stdout} means the expected
19319 output of the @var{commands} is the content of the file @file{expout}.
19320 If @var{stdout} is @samp{stdout}, then the standard output of the
19321 @var{commands} is available for further tests in the file @file{stdout}.
19322 Similarly for @var{stderr} with @samp{experr} and @samp{stderr}.
19323 @end defmac
19326 @node testsuite Invocation
19327 @section Running @command{testsuite} Scripts
19328 @cindex @command{testsuite}
19330 Autotest test suites support the following arguments:
19332 @table @option
19333 @item --help
19334 @itemx -h
19335 Display the list of options and exit successfully.
19337 @item --version
19338 @itemx -V
19339 Display the version of the test suite and exit successfully.
19341 @item --clean
19342 @itemx -c
19343 Remove all the files the test suite might have created and exit.  Meant
19344 for @code{clean} Make targets.
19346 @item --list
19347 @itemx -l
19348 List all the tests (or only the selection), including their possible
19349 keywords.
19350 @end table
19352 @sp 1
19354 By default all tests are performed (or described with
19355 @option{--list}) in the default environment first silently, then
19356 verbosely, but the environment, set of tests, and verbosity level can be
19357 tuned:
19359 @table @samp
19360 @item @var{variable}=@var{value}
19361 Set the environment @var{variable} to @var{value}.  Use this rather
19362 than @samp{FOO=foo ./testsuite} as debugging scripts would then run in a
19363 different environment.
19365 @cindex @code{AUTOTEST_PATH}
19366 The variable @code{AUTOTEST_PATH} specifies the testing path to prepend
19367 to @env{PATH}.  Relative directory names (not starting with
19368 @samp{/}) are considered to be relative to the top level of the
19369 package being built.  All directories are made absolute, first
19370 starting from the top level @emph{build} tree, then from the
19371 @emph{source} tree.  For instance @samp{./testsuite
19372 AUTOTEST_PATH=tests:bin} for a @file{/src/foo-1.0} source package built
19373 in @file{/tmp/foo} results in @samp{/tmp/foo/tests:/tmp/foo/bin} and
19374 then @samp{/src/foo-1.0/tests:/src/foo-1.0/bin} being prepended to
19375 @env{PATH}.
19377 @item @var{number}
19378 @itemx @var{number}-@var{number}
19379 @itemx @var{number}-
19380 @itemx -@var{number}
19381 Add the corresponding test groups, with obvious semantics, to the
19382 selection.
19384 @item --keywords=@var{keywords}
19385 @itemx -k @var{keywords}
19386 Add to the selection the test groups with title or keywords (arguments
19387 to @code{AT_SETUP} or @code{AT_KEYWORDS}) that match @emph{all} keywords
19388 of the comma separated list @var{keywords}, case-insensitively.  Use
19389 @samp{!} immediately before the keyword to invert the selection for this
19390 keyword.  By default, the keywords match whole words; enclose them in
19391 @samp{.*} to also match parts of words.
19393 For example, running
19395 @example
19396 @kbd{./testsuite -k 'autoupdate,.*FUNC.*'}
19397 @end example
19399 @noindent
19400 selects all tests tagged @samp{autoupdate} @emph{and} with tags
19401 containing @samp{FUNC} (as in @samp{AC_CHECK_FUNC}, @samp{AC_FUNC_ALLOCA},
19402 etc.), while
19404 @example
19405 @kbd{./testsuite -k '!autoupdate' -k '.*FUNC.*'}
19406 @end example
19408 @noindent
19409 selects all tests not tagged @samp{autoupdate} @emph{or} with tags
19410 containing @samp{FUNC}.
19412 @item --errexit
19413 @itemx -e
19414 If any test fails, immediately abort testing.  It implies
19415 @option{--debug}: post test group clean up, and top-level logging
19416 are inhibited.  This option is meant for the full test
19417 suite, it is not really useful for generated debugging scripts.
19419 @item --verbose
19420 @itemx -v
19421 Force more verbosity in the detailed output of what is being done.  This
19422 is the default for debugging scripts.
19424 @item --debug
19425 @itemx -d
19426 Do not remove the files after a test group was performed ---but they are
19427 still removed @emph{before}, therefore using this option is sane when
19428 running several test groups.  Create debugging scripts.  Do not
19429 overwrite the top-level
19430 log (in order to preserve supposedly existing full log file).  This is
19431 the default for debugging scripts, but it can also be useful to debug
19432 the testsuite itself.
19434 @item --trace
19435 @itemx -x
19436 Trigger shell tracing of the test groups.
19437 @end table
19440 @node Making testsuite Scripts
19441 @section Making @command{testsuite} Scripts
19443 For putting Autotest into movement, you need some configuration and
19444 makefile machinery.  We recommend, at least if your package uses deep or
19445 shallow hierarchies, that you use @file{tests/} as the name of the
19446 directory holding all your tests and their makefile.  Here is a
19447 check list of things to do.
19449 @itemize @minus
19451 @item
19452 @cindex @file{package.m4}
19453 Make sure to create the file @file{package.m4}, which defines the
19454 identity of the package.  It must define @code{AT_PACKAGE_STRING}, the
19455 full signature of the package, and @code{AT_PACKAGE_BUGREPORT}, the
19456 address to which bug reports should be sent.  For sake of completeness,
19457 we suggest that you also define @code{AT_PACKAGE_NAME},
19458 @code{AT_PACKAGE_TARNAME}, and @code{AT_PACKAGE_VERSION}.
19459 @xref{Initializing configure}, for a description of these variables.  We
19460 suggest the following makefile excerpt:
19462 @smallexample
19463 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
19464         @{                                      \
19465           echo '# Signature of the current package.'; \
19466           echo 'm4_define([AT_PACKAGE_NAME],      [@@PACKAGE_NAME@@])'; \
19467           echo 'm4_define([AT_PACKAGE_TARNAME],   [@@PACKAGE_TARNAME@@])'; \
19468           echo 'm4_define([AT_PACKAGE_VERSION],   [@@PACKAGE_VERSION@@])'; \
19469           echo 'm4_define([AT_PACKAGE_STRING],    [@@PACKAGE_STRING@@])'; \
19470           echo 'm4_define([AT_PACKAGE_BUGREPORT], [@@PACKAGE_BUGREPORT@@])'; \
19471         @} >'$(srcdir)/package.m4'
19472 @end smallexample
19474 @noindent
19475 Be sure to distribute @file{package.m4} and to put it into the source
19476 hierarchy: the test suite ought to be shipped!
19478 @item
19479 Invoke @code{AC_CONFIG_TESTDIR}.
19481 @defmac AC_CONFIG_TESTDIR (@var{directory}, @dvar{test-path, directory})
19482 @acindex{CONFIG_TESTDIR}
19483 An Autotest test suite is to be configured in @var{directory}.  This
19484 macro requires the instantiation of @file{@var{directory}/atconfig} from
19485 @file{@var{directory}/atconfig.in}, and sets the default
19486 @code{AUTOTEST_PATH} to @var{test-path} (@pxref{testsuite Invocation}).
19487 @end defmac
19489 @item
19490 Still within @file{configure.ac}, as appropriate, ensure that some
19491 @code{AC_CONFIG_FILES} command includes substitution for
19492 @file{tests/atlocal}.
19494 @item
19495 The @file{tests/Makefile.in} should be modified so the validation in
19496 your package is triggered by @samp{make check}.  An example is provided
19497 below.
19498 @end itemize
19500 With Automake, here is a minimal example about how to link @samp{make
19501 check} with a validation suite.
19503 @example
19504 EXTRA_DIST = testsuite.at $(TESTSUITE) atlocal.in
19505 TESTSUITE = $(srcdir)/testsuite
19507 check-local: atconfig atlocal $(TESTSUITE)
19508         $(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS)
19510 installcheck-local: atconfig atlocal $(TESTSUITE)
19511         $(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \
19512           $(TESTSUITEFLAGS)
19514 clean-local:
19515         test ! -f '$(TESTSUITE)' || \
19516          $(SHELL) '$(TESTSUITE)' --clean
19518 AUTOTEST = $(AUTOM4TE) --language=autotest
19519 $(TESTSUITE): $(srcdir)/testsuite.at
19520         $(AUTOTEST) -I '$(srcdir)' -o $@@.tmp $@@.at
19521         mv $@@.tmp $@@
19522 @end example
19524 You might want to list explicitly the dependencies, i.e., the list of
19525 the files @file{testsuite.at} includes.
19527 With strict Autoconf, you might need to add lines inspired from the
19528 following:
19530 @example
19531 subdir = tests
19533 atconfig: $(top_builddir)/config.status
19534         cd $(top_builddir) && \
19535            $(SHELL) ./config.status $(subdir)/$@@
19537 atlocal: $(srcdir)/atlocal.in $(top_builddir)/config.status
19538         cd $(top_builddir) && \
19539            $(SHELL) ./config.status $(subdir)/$@@
19540 @end example
19542 @noindent
19543 and manage to have @file{atconfig.in} and @code{$(EXTRA_DIST)}
19544 distributed.
19546 With all this in place, and if you have not initialized @samp{TESTSUITEFLAGS}
19547 within your makefile, you can fine-tune test suite execution with this
19548 variable, for example:
19550 @example
19551 make check TESTSUITEFLAGS='-v -d -x 75 -k AC_PROG_CC CFLAGS=-g'
19552 @end example
19556 @c =============================== Frequent Autoconf Questions, with answers
19558 @node FAQ
19559 @chapter Frequent Autoconf Questions, with answers
19561 Several questions about Autoconf come up occasionally.  Here some of them
19562 are addressed.
19564 @menu
19565 * Distributing::                Distributing @command{configure} scripts
19566 * Why GNU M4::                  Why not use the standard M4?
19567 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
19568 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
19569 * Defining Directories::        Passing @code{datadir} to program
19570 * Autom4te Cache::              What is it?  Can I remove it?
19571 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
19572 @end menu
19574 @node Distributing
19575 @section Distributing @command{configure} Scripts
19576 @cindex License
19578 @display
19579 What are the restrictions on distributing @command{configure}
19580 scripts that Autoconf generates?  How does that affect my
19581 programs that use them?
19582 @end display
19584 There are no restrictions on how the configuration scripts that Autoconf
19585 produces may be distributed or used.  In Autoconf version 1, they were
19586 covered by the @acronym{GNU} General Public License.  We still encourage
19587 software authors to distribute their work under terms like those of the
19588 @acronym{GPL}, but doing so is not required to use Autoconf.
19590 Of the other files that might be used with @command{configure},
19591 @file{config.h.in} is under whatever copyright you use for your
19592 @file{configure.ac}.  @file{config.sub} and @file{config.guess} have an
19593 exception to the @acronym{GPL} when they are used with an Autoconf-generated
19594 @command{configure} script, which permits you to distribute them under the
19595 same terms as the rest of your package.  @file{install-sh} is from the X
19596 Consortium and is not copyrighted.
19598 @node Why GNU M4
19599 @section Why Require @acronym{GNU} M4?
19601 @display
19602 Why does Autoconf require @acronym{GNU} M4?
19603 @end display
19605 Many M4 implementations have hard-coded limitations on the size and
19606 number of macros that Autoconf exceeds.  They also lack several
19607 builtin macros that it would be difficult to get along without in a
19608 sophisticated application like Autoconf, including:
19610 @example
19611 m4_builtin
19612 m4_indir
19613 m4_bpatsubst
19614 __file__
19615 __line__
19616 @end example
19618 Autoconf requires version 1.4.5 or later of @acronym{GNU} M4.
19620 Since only software maintainers need to use Autoconf, and since @acronym{GNU}
19621 M4 is simple to configure and install, it seems reasonable to require
19622 @acronym{GNU} M4 to be installed also.  Many maintainers of @acronym{GNU} and
19623 other free software already have most of the @acronym{GNU} utilities
19624 installed, since they prefer them.
19626 @node Bootstrapping
19627 @section How Can I Bootstrap?
19628 @cindex Bootstrap
19630 @display
19631 If Autoconf requires @acronym{GNU} M4 and @acronym{GNU} M4 has an Autoconf
19632 @command{configure} script, how do I bootstrap?  It seems like a chicken
19633 and egg problem!
19634 @end display
19636 This is a misunderstanding.  Although @acronym{GNU} M4 does come with a
19637 @command{configure} script produced by Autoconf, Autoconf is not required
19638 in order to run the script and install @acronym{GNU} M4.  Autoconf is only
19639 required if you want to change the M4 @command{configure} script, which few
19640 people have to do (mainly its maintainer).
19642 @node Why Not Imake
19643 @section Why Not Imake?
19644 @cindex Imake
19646 @display
19647 Why not use Imake instead of @command{configure} scripts?
19648 @end display
19650 Several people have written addressing this question, so I include
19651 adaptations of their explanations here.
19653 The following answer is based on one written by Richard Pixley:
19655 @quotation
19656 Autoconf generated scripts frequently work on machines that it has
19657 never been set up to handle before.  That is, it does a good job of
19658 inferring a configuration for a new system.  Imake cannot do this.
19660 Imake uses a common database of host specific data.  For X11, this makes
19661 sense because the distribution is made as a collection of tools, by one
19662 central authority who has control over the database.
19664 @acronym{GNU} tools are not released this way.  Each @acronym{GNU} tool has a
19665 maintainer; these maintainers are scattered across the world.  Using a
19666 common database would be a maintenance nightmare.  Autoconf may appear
19667 to be this kind of database, but in fact it is not.  Instead of listing
19668 host dependencies, it lists program requirements.
19670 If you view the @acronym{GNU} suite as a collection of native tools, then the
19671 problems are similar.  But the @acronym{GNU} development tools can be
19672 configured as cross tools in almost any host+target permutation.  All of
19673 these configurations can be installed concurrently.  They can even be
19674 configured to share host independent files across hosts.  Imake doesn't
19675 address these issues.
19677 Imake templates are a form of standardization.  The @acronym{GNU} coding
19678 standards address the same issues without necessarily imposing the same
19679 restrictions.
19680 @end quotation
19683 Here is some further explanation, written by Per Bothner:
19685 @quotation
19686 One of the advantages of Imake is that it easy to generate large
19687 makefiles using the @samp{#include} and macro mechanisms of @command{cpp}.
19688 However, @code{cpp} is not programmable: it has limited conditional
19689 facilities, and no looping.  And @code{cpp} cannot inspect its
19690 environment.
19692 All of these problems are solved by using @code{sh} instead of
19693 @code{cpp}.  The shell is fully programmable, has macro substitution,
19694 can execute (or source) other shell scripts, and can inspect its
19695 environment.
19696 @end quotation
19699 Paul Eggert elaborates more:
19701 @quotation
19702 With Autoconf, installers need not assume that Imake itself is already
19703 installed and working well.  This may not seem like much of an advantage
19704 to people who are accustomed to Imake.  But on many hosts Imake is not
19705 installed or the default installation is not working well, and requiring
19706 Imake to install a package hinders the acceptance of that package on
19707 those hosts.  For example, the Imake template and configuration files
19708 might not be installed properly on a host, or the Imake build procedure
19709 might wrongly assume that all source files are in one big directory
19710 tree, or the Imake configuration might assume one compiler whereas the
19711 package or the installer needs to use another, or there might be a
19712 version mismatch between the Imake expected by the package and the Imake
19713 supported by the host.  These problems are much rarer with Autoconf,
19714 where each package comes with its own independent configuration
19715 processor.
19717 Also, Imake often suffers from unexpected interactions between
19718 @command{make} and the installer's C preprocessor.  The fundamental problem
19719 here is that the C preprocessor was designed to preprocess C programs,
19720 not makefiles.  This is much less of a problem with Autoconf,
19721 which uses the general-purpose preprocessor M4, and where the
19722 package's author (rather than the installer) does the preprocessing in a
19723 standard way.
19724 @end quotation
19727 Finally, Mark Eichin notes:
19729 @quotation
19730 Imake isn't all that extensible, either.  In order to add new features to
19731 Imake, you need to provide your own project template, and duplicate most
19732 of the features of the existing one.  This means that for a sophisticated
19733 project, using the vendor-provided Imake templates fails to provide any
19734 leverage---since they don't cover anything that your own project needs
19735 (unless it is an X11 program).
19737 On the other side, though:
19739 The one advantage that Imake has over @command{configure}:
19740 @file{Imakefile} files tend to be much shorter (likewise, less redundant)
19741 than @file{Makefile.in} files.  There is a fix to this, however---at least
19742 for the Kerberos V5 tree, we've modified things to call in common
19743 @file{post.in} and @file{pre.in} makefile fragments for the
19744 entire tree.  This means that a lot of common things don't have to be
19745 duplicated, even though they normally are in @command{configure} setups.
19746 @end quotation
19749 @node Defining Directories
19750 @section How Do I @code{#define} Installation Directories?
19752 @display
19753 My program needs library files, installed in @code{datadir} and
19754 similar.  If I use
19756 @example
19757 AC_DEFINE_UNQUOTED([DATADIR], [$datadir],
19758   [Define to the read-only architecture-independent
19759    data directory.])
19760 @end example
19762 @noindent
19763 I get
19765 @example
19766 #define DATADIR "$@{prefix@}/share"
19767 @end example
19768 @end display
19770 As already explained, this behavior is on purpose, mandated by the
19771 @acronym{GNU} Coding Standards, see @ref{Installation Directory
19772 Variables}.  There are several means to achieve a similar goal:
19774 @itemize @minus
19775 @item
19776 Do not use @code{AC_DEFINE} but use your makefile to pass the
19777 actual value of @code{datadir} via compilation flags.
19778 @xref{Installation Directory Variables}, for the details.
19780 @item
19781 This solution can be simplified when compiling a program: you may either
19782 extend the @code{CPPFLAGS}:
19784 @example
19785 CPPFLAGS = -DDATADIR='"$(datadir)"' @@CPPFLAGS@@
19786 @end example
19788 @noindent
19789 If you are using Automake, you should use @code{AM_CPPFLAGS} instead:
19791 @example
19792 AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
19793 @end example
19795 @noindent
19796 Alternatively, create a dedicated header file:
19798 @example
19799 DISTCLEANFILES = myprog-paths.h
19800 myprog-paths.h: Makefile
19801         echo '#define DATADIR "$(datadir)"' >$@@
19802 @end example
19804 @item
19805 Use @code{AC_DEFINE} but have @command{configure} compute the literal
19806 value of @code{datadir} and others.  Many people have wrapped macros to
19807 automate this task.  For instance, the macro @code{AC_DEFINE_DIR} from
19808 the @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
19809 Archive}.
19811 This solution does not conform to the @acronym{GNU} Coding Standards.
19813 @item
19814 Note that all the previous solutions hard wire the absolute name of
19815 these directories in the executables, which is not a good property.  You
19816 may try to compute the names relative to @code{prefix}, and try to
19817 find @code{prefix} at runtime, this way your package is relocatable.
19818 @end itemize
19821 @node Autom4te Cache
19822 @section What is @file{autom4te.cache}?
19824 @display
19825 What is this directory @file{autom4te.cache}?  Can I safely remove it?
19826 @end display
19828 In the @acronym{GNU} Build System, @file{configure.ac} plays a central
19829 role and is read by many tools: @command{autoconf} to create
19830 @file{configure}, @command{autoheader} to create @file{config.h.in},
19831 @command{automake} to create @file{Makefile.in}, @command{autoscan} to
19832 check the completeness of @file{configure.ac}, @command{autoreconf} to
19833 check the @acronym{GNU} Build System components that are used.  To
19834 ``read @file{configure.ac}'' actually means to compile it with M4,
19835 which can be a long process for complex @file{configure.ac}.
19837 This is why all these tools, instead of running directly M4, invoke
19838 @command{autom4te} (@pxref{autom4te Invocation}) which, while answering to
19839 a specific demand, stores additional information in
19840 @file{autom4te.cache} for future runs.  For instance, if you run
19841 @command{autoconf}, behind the scenes, @command{autom4te} also
19842 stores information for the other tools, so that when you invoke
19843 @command{autoheader} or @command{automake} etc., reprocessing
19844 @file{configure.ac} is not needed.  The speed up is frequently of 30%,
19845 and is increasing with the size of @file{configure.ac}.
19847 But it is and remains being simply a cache: you can safely remove it.
19849 @sp 1
19851 @display
19852 Can I permanently get rid of it?
19853 @end display
19855 The creation of this cache can be disabled from
19856 @file{~/.autom4te.cfg}, see @ref{Customizing autom4te}, for more
19857 details.  You should be aware that disabling the cache slows down the
19858 Autoconf test suite by 40%.  The more @acronym{GNU} Build System
19859 components are used, the more the cache is useful; for instance
19860 running @samp{autoreconf -f} on the Core Utilities is twice slower without
19861 the cache @emph{although @option{--force} implies that the cache is
19862 not fully exploited}, and eight times slower than without
19863 @option{--force}.
19866 @node Present But Cannot Be Compiled
19867 @section Header Present But Cannot Be Compiled
19869 The most important guideline to bear in mind when checking for
19870 features is to mimic as much as possible the intended use.
19871 Unfortunately, old versions of @code{AC_CHECK_HEADER} and
19872 @code{AC_CHECK_HEADERS} failed to follow this idea, and called
19873 the preprocessor, instead of the compiler, to check for headers.  As a
19874 result, incompatibilities between headers went unnoticed during
19875 configuration, and maintainers finally had to deal with this issue
19876 elsewhere.
19878 As of Autoconf 2.56 both checks are performed, and @code{configure}
19879 complains loudly if the compiler and the preprocessor do not agree.
19880 For the time being the result used is that of the preprocessor, to give
19881 maintainers time to adjust their @file{configure.ac}, but in the
19882 future, only the compiler will be considered.
19884 Consider the following example:
19886 @smallexample
19887 $ @kbd{cat number.h}
19888 typedef int number;
19889 $ @kbd{cat pi.h}
19890 const number pi = 3;
19891 $ @kbd{cat configure.ac}
19892 AC_INIT([Example], [1.0], [bug-example@@example.org])
19893 AC_CHECK_HEADERS([pi.h])
19894 $ @kbd{autoconf -Wall}
19895 $ @kbd{./configure}
19896 checking for gcc... gcc
19897 checking for C compiler default output file name... a.out
19898 checking whether the C compiler works... yes
19899 checking whether we are cross compiling... no
19900 checking for suffix of executables...
19901 checking for suffix of object files... o
19902 checking whether we are using the GNU C compiler... yes
19903 checking whether gcc accepts -g... yes
19904 checking for gcc option to accept ISO C89... none needed
19905 checking how to run the C preprocessor... gcc -E
19906 checking for grep that handles long lines and -e... grep
19907 checking for egrep... grep -E
19908 checking for ANSI C header files... yes
19909 checking for sys/types.h... yes
19910 checking for sys/stat.h... yes
19911 checking for stdlib.h... yes
19912 checking for string.h... yes
19913 checking for memory.h... yes
19914 checking for strings.h... yes
19915 checking for inttypes.h... yes
19916 checking for stdint.h... yes
19917 checking for unistd.h... yes
19918 checking pi.h usability... no
19919 checking pi.h presence... yes
19920 configure: WARNING: pi.h: present but cannot be compiled
19921 configure: WARNING: pi.h:     check for missing prerequisite headers?
19922 configure: WARNING: pi.h: see the Autoconf documentation
19923 configure: WARNING: pi.h:     section "Present But Cannot Be Compiled"
19924 configure: WARNING: pi.h: proceeding with the preprocessor's result
19925 configure: WARNING: pi.h: in the future, the compiler will take precedence
19926 configure: WARNING:     ## -------------------------------------- ##
19927 configure: WARNING:     ## Report this to bug-example@@example.org ##
19928 configure: WARNING:     ## -------------------------------------- ##
19929 checking for pi.h... yes
19930 @end smallexample
19932 @noindent
19933 The proper way the handle this case is using the fourth argument
19934 (@pxref{Generic Headers}):
19936 @example
19937 $ @kbd{cat configure.ac}
19938 AC_INIT([Example], [1.0], [bug-example@@example.org])
19939 AC_CHECK_HEADERS([number.h pi.h], [], [],
19940 [[#ifdef HAVE_NUMBER_H
19941 # include <number.h>
19942 #endif
19944 $ @kbd{autoconf -Wall}
19945 $ @kbd{./configure}
19946 checking for gcc... gcc
19947 checking for C compiler default output... a.out
19948 checking whether the C compiler works... yes
19949 checking whether we are cross compiling... no
19950 checking for suffix of executables...
19951 checking for suffix of object files... o
19952 checking whether we are using the GNU C compiler... yes
19953 checking whether gcc accepts -g... yes
19954 checking for gcc option to accept ANSI C... none needed
19955 checking for number.h... yes
19956 checking for pi.h... yes
19957 @end example
19959 See @ref{Particular Headers}, for a list of headers with their
19960 prerequisite.
19962 @c ===================================================== History of Autoconf.
19964 @node History
19965 @chapter History of Autoconf
19966 @cindex History of autoconf
19968 You may be wondering, Why was Autoconf originally written?  How did it
19969 get into its present form?  (Why does it look like gorilla spit?)  If
19970 you're not wondering, then this chapter contains no information useful
19971 to you, and you might as well skip it.  If you @emph{are} wondering,
19972 then let there be light@enddots{}
19974 @menu
19975 * Genesis::                     Prehistory and naming of @command{configure}
19976 * Exodus::                      The plagues of M4 and Perl
19977 * Leviticus::                   The priestly code of portability arrives
19978 * Numbers::                     Growth and contributors
19979 * Deuteronomy::                 Approaching the promises of easy configuration
19980 @end menu
19982 @node Genesis
19983 @section Genesis
19985 In June 1991 I was maintaining many of the @acronym{GNU} utilities for the
19986 Free Software Foundation.  As they were ported to more platforms and
19987 more programs were added, the number of @option{-D} options that users
19988 had to select in the makefile (around 20) became burdensome.
19989 Especially for me---I had to test each new release on a bunch of
19990 different systems.  So I wrote a little shell script to guess some of
19991 the correct settings for the fileutils package, and released it as part
19992 of fileutils 2.0.  That @command{configure} script worked well enough that
19993 the next month I adapted it (by hand) to create similar @command{configure}
19994 scripts for several other @acronym{GNU} utilities packages.  Brian Berliner
19995 also adapted one of my scripts for his @acronym{CVS} revision control system.
19997 Later that summer, I learned that Richard Stallman and Richard Pixley
19998 were developing similar scripts to use in the @acronym{GNU} compiler tools;
19999 so I adapted my @command{configure} scripts to support their evolving
20000 interface: using the file name @file{Makefile.in} as the templates;
20001 adding @samp{+srcdir}, the first option (of many); and creating
20002 @file{config.status} files.
20004 @node Exodus
20005 @section Exodus
20007 As I got feedback from users, I incorporated many improvements, using
20008 Emacs to search and replace, cut and paste, similar changes in each of
20009 the scripts.  As I adapted more @acronym{GNU} utilities packages to use
20010 @command{configure} scripts, updating them all by hand became impractical.
20011 Rich Murphey, the maintainer of the @acronym{GNU} graphics utilities, sent me
20012 mail saying that the @command{configure} scripts were great, and asking if
20013 I had a tool for generating them that I could send him.  No, I thought,
20014 but I should!  So I started to work out how to generate them.  And the
20015 journey from the slavery of hand-written @command{configure} scripts to the
20016 abundance and ease of Autoconf began.
20018 Cygnus @command{configure}, which was being developed at around that time,
20019 is table driven; it is meant to deal mainly with a discrete number of
20020 system types with a small number of mainly unguessable features (such as
20021 details of the object file format).  The automatic configuration system
20022 that Brian Fox had developed for Bash takes a similar approach.  For
20023 general use, it seems to me a hopeless cause to try to maintain an
20024 up-to-date database of which features each variant of each operating
20025 system has.  It's easier and more reliable to check for most features on
20026 the fly---especially on hybrid systems that people have hacked on
20027 locally or that have patches from vendors installed.
20029 I considered using an architecture similar to that of Cygnus
20030 @command{configure}, where there is a single @command{configure} script that
20031 reads pieces of @file{configure.in} when run.  But I didn't want to have
20032 to distribute all of the feature tests with every package, so I settled
20033 on having a different @command{configure} made from each
20034 @file{configure.in} by a preprocessor.  That approach also offered more
20035 control and flexibility.
20037 I looked briefly into using the Metaconfig package, by Larry Wall,
20038 Harlan Stenn, and Raphael Manfredi, but I decided not to for several
20039 reasons.  The @command{Configure} scripts it produces are interactive,
20040 which I find quite inconvenient; I didn't like the ways it checked for
20041 some features (such as library functions); I didn't know that it was
20042 still being maintained, and the @command{Configure} scripts I had
20043 seen didn't work on many modern systems (such as System V R4 and NeXT);
20044 it wasn't flexible in what it could do in response to a feature's
20045 presence or absence; I found it confusing to learn; and it was too big
20046 and complex for my needs (I didn't realize then how much Autoconf would
20047 eventually have to grow).
20049 I considered using Perl to generate my style of @command{configure}
20050 scripts, but decided that M4 was better suited to the job of simple
20051 textual substitutions: it gets in the way less, because output is
20052 implicit.  Plus, everyone already has it.  (Initially I didn't rely on
20053 the @acronym{GNU} extensions to M4.)  Also, some of my friends at the
20054 University of Maryland had recently been putting M4 front ends on
20055 several programs, including @code{tvtwm}, and I was interested in trying
20056 out a new language.
20058 @node Leviticus
20059 @section Leviticus
20061 Since my @command{configure} scripts determine the system's capabilities
20062 automatically, with no interactive user intervention, I decided to call
20063 the program that generates them Autoconfig.  But with a version number
20064 tacked on, that name would be too long for old Unix file systems,
20065 so I shortened it to Autoconf.
20067 In the fall of 1991 I called together a group of fellow questers after
20068 the Holy Grail of portability (er, that is, alpha testers) to give me
20069 feedback as I encapsulated pieces of my handwritten scripts in M4 macros
20070 and continued to add features and improve the techniques used in the
20071 checks.  Prominent among the testers were Fran@,{c}ois Pinard, who came up
20072 with the idea of making an Autoconf shell script to run M4
20073 and check for unresolved macro calls; Richard Pixley, who suggested
20074 running the compiler instead of searching the file system to find
20075 include files and symbols, for more accurate results; Karl Berry, who
20076 got Autoconf to configure @TeX{} and added the macro index to the
20077 documentation; and Ian Lance Taylor, who added support for creating a C
20078 header file as an alternative to putting @option{-D} options in a
20079 makefile, so he could use Autoconf for his @acronym{UUCP} package.
20080 The alpha testers cheerfully adjusted their files again and again as the
20081 names and calling conventions of the Autoconf macros changed from
20082 release to release.  They all contributed many specific checks, great
20083 ideas, and bug fixes.
20085 @node Numbers
20086 @section Numbers
20088 In July 1992, after months of alpha testing, I released Autoconf 1.0,
20089 and converted many @acronym{GNU} packages to use it.  I was surprised by how
20090 positive the reaction to it was.  More people started using it than I
20091 could keep track of, including people working on software that wasn't
20092 part of the @acronym{GNU} Project (such as TCL, FSP, and Kerberos V5).
20093 Autoconf continued to improve rapidly, as many people using the
20094 @command{configure} scripts reported problems they encountered.
20096 Autoconf turned out to be a good torture test for M4 implementations.
20097 Unix M4 started to dump core because of the length of the
20098 macros that Autoconf defined, and several bugs showed up in @acronym{GNU}
20099 M4 as well.  Eventually, we realized that we needed to use some
20100 features that only @acronym{GNU} M4 has.  4.3@acronym{BSD} M4, in
20101 particular, has an impoverished set of builtin macros; the System V
20102 version is better, but still doesn't provide everything we need.
20104 More development occurred as people put Autoconf under more stresses
20105 (and to uses I hadn't anticipated).  Karl Berry added checks for X11.
20106 david zuhn contributed C++ support.  Fran@,{c}ois Pinard made it diagnose
20107 invalid arguments.  Jim Blandy bravely coerced it into configuring
20108 @acronym{GNU} Emacs, laying the groundwork for several later improvements.
20109 Roland McGrath got it to configure the @acronym{GNU} C Library, wrote the
20110 @command{autoheader} script to automate the creation of C header file
20111 templates, and added a @option{--verbose} option to @command{configure}.
20112 Noah Friedman added the @option{--autoconf-dir} option and
20113 @code{AC_MACRODIR} environment variable.  (He also coined the term
20114 @dfn{autoconfiscate} to mean ``adapt a software package to use
20115 Autoconf''.)  Roland and Noah improved the quoting protection in
20116 @code{AC_DEFINE} and fixed many bugs, especially when I got sick of
20117 dealing with portability problems from February through June, 1993.
20119 @node Deuteronomy
20120 @section Deuteronomy
20122 A long wish list for major features had accumulated, and the effect of
20123 several years of patching by various people had left some residual
20124 cruft.  In April 1994, while working for Cygnus Support, I began a major
20125 revision of Autoconf.  I added most of the features of the Cygnus
20126 @command{configure} that Autoconf had lacked, largely by adapting the
20127 relevant parts of Cygnus @command{configure} with the help of david zuhn
20128 and Ken Raeburn.  These features include support for using
20129 @file{config.sub}, @file{config.guess}, @option{--host}, and
20130 @option{--target}; making links to files; and running @command{configure}
20131 scripts in subdirectories.  Adding these features enabled Ken to convert
20132 @acronym{GNU} @code{as}, and Rob Savoye to convert Deja@acronym{GNU}, to using
20133 Autoconf.
20135 I added more features in response to other peoples' requests.  Many
20136 people had asked for @command{configure} scripts to share the results of
20137 the checks between runs, because (particularly when configuring a large
20138 source tree, like Cygnus does) they were frustratingly slow.  Mike
20139 Haertel suggested adding site-specific initialization scripts.  People
20140 distributing software that had to unpack on MS-DOS asked for a way to
20141 override the @file{.in} extension on the file names, which produced file
20142 names like @file{config.h.in} containing two dots.  Jim Avera did an
20143 extensive examination of the problems with quoting in @code{AC_DEFINE}
20144 and @code{AC_SUBST}; his insights led to significant improvements.
20145 Richard Stallman asked that compiler output be sent to @file{config.log}
20146 instead of @file{/dev/null}, to help people debug the Emacs
20147 @command{configure} script.
20149 I made some other changes because of my dissatisfaction with the quality
20150 of the program.  I made the messages showing results of the checks less
20151 ambiguous, always printing a result.  I regularized the names of the
20152 macros and cleaned up coding style inconsistencies.  I added some
20153 auxiliary utilities that I had developed to help convert source code
20154 packages to use Autoconf.  With the help of Fran@,{c}ois Pinard, I made
20155 the macros not interrupt each others' messages.  (That feature revealed
20156 some performance bottlenecks in @acronym{GNU} M4, which he hastily
20157 corrected!)  I reorganized the documentation around problems people want
20158 to solve.  And I began a test suite, because experience had shown that
20159 Autoconf has a pronounced tendency to regress when we change it.
20161 Again, several alpha testers gave invaluable feedback, especially
20162 Fran@,{c}ois Pinard, Jim Meyering, Karl Berry, Rob Savoye, Ken Raeburn,
20163 and Mark Eichin.
20165 Finally, version 2.0 was ready.  And there was much rejoicing.  (And I
20166 have free time again.  I think.  Yeah, right.)
20169 @c ========================================================== Appendices
20172 @node GNU Free Documentation License
20173 @appendix GNU Free Documentation License
20175 @include fdl.texi
20177 @node Indices
20178 @appendix Indices
20180 @menu
20181 * Environment Variable Index::  Index of environment variables used
20182 * Output Variable Index::       Index of variables set in output files
20183 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
20184 * Autoconf Macro Index::        Index of Autoconf macros
20185 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
20186 * Autotest Macro Index::        Index of Autotest macros
20187 * Program & Function Index::    Index of those with portability problems
20188 * Concept Index::               General index
20189 @end menu
20191 @node Environment Variable Index
20192 @appendixsec Environment Variable Index
20194 This is an alphabetical list of the environment variables that Autoconf
20195 checks.
20197 @printindex ev
20199 @node Output Variable Index
20200 @appendixsec Output Variable Index
20202 This is an alphabetical list of the variables that Autoconf can
20203 substitute into files that it creates, typically one or more
20204 makefiles.  @xref{Setting Output Variables}, for more information
20205 on how this is done.
20207 @printindex ov
20209 @node Preprocessor Symbol Index
20210 @appendixsec Preprocessor Symbol Index
20212 This is an alphabetical list of the C preprocessor symbols that the
20213 Autoconf macros define.  To work with Autoconf, C source code needs to
20214 use these names in @code{#if} or @code{#ifdef} directives.
20216 @printindex cv
20218 @node Autoconf Macro Index
20219 @appendixsec Autoconf Macro Index
20221 This is an alphabetical list of the Autoconf macros.
20222 @ifset shortindexflag
20223 To make the list easier to use, the macros are listed without their
20224 preceding @samp{AC_}.
20225 @end ifset
20227 @printindex AC
20229 @node M4 Macro Index
20230 @appendixsec M4 Macro Index
20232 This is an alphabetical list of the M4, M4sugar, and M4sh macros.
20233 @ifset shortindexflag
20234 To make the list easier to use, the macros are listed without their
20235 preceding @samp{m4_} or @samp{AS_}.
20236 @end ifset
20238 @printindex MS
20240 @node Autotest Macro Index
20241 @appendixsec Autotest Macro Index
20243 This is an alphabetical list of the Autotest macros.
20244 @ifset shortindexflag
20245 To make the list easier to use, the macros are listed without their
20246 preceding @samp{AT_}.
20247 @end ifset
20249 @printindex AT
20251 @node Program & Function Index
20252 @appendixsec Program and Function Index
20254 This is an alphabetical list of the programs and functions whose
20255 portability is discussed in this document.
20257 @printindex pr
20259 @node Concept Index
20260 @appendixsec Concept Index
20262 This is an alphabetical list of the files, tools, and concepts
20263 introduced in this document.
20265 @printindex cp
20267 @bye
20269 @c  LocalWords:  texinfo setfilename autoconf texi settitle setchapternewpage
20270 @c  LocalWords:  setcontentsaftertitlepage finalout ARG ovar varname dvar acx
20271 @c  LocalWords:  makeinfo dvi defcodeindex ev ov CPP cv Autotest mv defindex fn
20272 @c  LocalWords:  shortindexflag iftex ifset acindex ACindex ifclear ahindex fu
20273 @c  LocalWords:  asindex MSindex atindex ATindex auindex hdrindex prindex FIXME
20274 @c  LocalWords:  msindex alloca fnindex Aaarg indices FSF's dircategory ifnames
20275 @c  LocalWords:  direntry autoscan autoreconf autoheader autoupdate config FDs
20276 @c  LocalWords:  testsuite titlepage Elliston Demaille vskip filll ifnottex hmm
20277 @c  LocalWords:  insertcopying Autoconf's detailmenu Automake Libtool Posix ois
20278 @c  LocalWords:  Systemology Checkpointing Changequote INTERCAL changequote dfn
20279 @c  LocalWords:  Quadrigraphs builtins Shellology acconfig Bugward LIBOBJ Imake
20280 @c  LocalWords:  LIBOBJS IFELSE cindex flushright Pinard Metaconfig uref Simons
20281 @c  LocalWords:  distclean uninstall noindent versioning Tromey dir
20282 @c  LocalWords:  SAMS samp aclocal acsite underquoted emph itemx prepend SUBST
20283 @c  LocalWords:  evindex automake Gettext autopoint gettext symlink libtoolize
20284 @c  LocalWords:  defmac INIT tarname ovindex cvindex BUGREPORT PREREQ asis PROG
20285 @c  LocalWords:  SRCDIR srcdir globbing afterwards cmds foos fooo foooo init cd
20286 @c  LocalWords:  builddir timestamp src Imakefile chmod defvar CFLAGS CPPFLAGS
20287 @c  LocalWords:  CXXFLAGS DEFS DHAVE defvarx FCFLAGS FFLAGS LDFLAGS bindir GCC
20288 @c  LocalWords:  datadir datarootdir docdir dvidir htmldir libdir ifnothtml kbd
20289 @c  LocalWords:  includedir infodir libexecdir localedir localstatedir mandir
20290 @c  LocalWords:  oldincludedir pdfdir PDF psdir PostScript sbindir sysconfdir
20291 @c  LocalWords:  sharedstatedir DDATADIR sed tmp pkgdatadir VPATH conf unistd
20292 @c  LocalWords:  undef endif builtin FUNCS ifndef STACKSEG getb GETB YMP fubar
20293 @c  LocalWords:  PRE dest SUBDIRS subdirs fi struct STDC stdlib stddef INTTYPES
20294 @c  LocalWords:  inttypes STDINT stdint AWK AIX Solaris NeXT env EGREP FGREP yy
20295 @c  LocalWords:  LEXLIB YYTEXT lfl nonportable Automake's LN RANLIB byacc INETD
20296 @c  LocalWords:  inetd prog PROGS progs ranlib lmp lXt lX nsl gethostbyname UX
20297 @c  LocalWords:  NextStep isinf isnan glibc IRIX sunmath lm lsunmath pre sizeof
20298 @c  LocalWords:  ld inline malloc putenv setenv FreeBSD realloc SunOS MinGW
20299 @c  LocalWords:  snprintf vsnprintf sprintf vsprintf sscanf gcc strerror ifdef
20300 @c  LocalWords:  strnlen sysconf PAGESIZE unsetenv va fallback memcpy dst FUNC
20301 @c  LocalWords:  PowerPC GNUC libPW pragma Olibcalls CHOWN chown CLOSEDIR VFORK
20302 @c  LocalWords:  closedir FNMATCH fnmatch vfork FSEEKO LARGEFILE fseeko SVR sc
20303 @c  LocalWords:  largefile GETGROUPS getgroups GETLOADAVG DGUX UMAX NLIST KMEM
20304 @c  LocalWords:  SETGID getloadavg nlist GETMNTENT irix
20305 @c  LocalWords:  getmntent UnixWare GETPGRP getpgid getpgrp Posix's pid LSTAT
20306 @c  LocalWords:  lstat rpl MEMCMP memcmp OpenStep MBRTOWC mbrtowc MKTIME mktime
20307 @c  LocalWords:  localtime MMAP mmap OBSTACK obstack obstacks ARGTYPES timeval
20308 @c  LocalWords:  SETPGRP setpgrp defmacx Hurd SETVBUF setvbuf STRCOLL strcoll
20309 @c  LocalWords:  STRTOD strtod DECL STRFTIME strftime SCO UTIME utime VPRINTF
20310 @c  LocalWords:  DOPRNT vprintf doprnt sp unfixable LIBSOURCE LIBSOURCES Eggert
20311 @c  LocalWords:  linux netinet ia Tru XFree DIRENT NDIR dirent ndir multitable
20312 @c  LocalWords:  NAMLEN strlen namlen MKDEV SYSMACROS makedev RESOLV resolv DNS
20313 @c  LocalWords:  inet structs NAMESER arpa NETDB netdb UTekV UTS GCC's kB
20314 @c  LocalWords:  STDBOOL BOOL stdbool conformant cplusplus bool Bool stdarg tm
20315 @c  LocalWords:  ctype strchr strrchr rindex bcopy memmove memchr WEXITSTATUS
20316 @c  LocalWords:  WIFEXITED TIOCGWINSZ GWINSZ termios preprocess preprocessable
20317 @c  LocalWords:  DECLS strdup calloc BLKSIZE blksize RDEV rdev TZNAME tzname pw
20318 @c  LocalWords:  passwd gecos pwd MBSTATE mbstate wchar RETSIGTYPE hup UID uid
20319 @c  LocalWords:  gid ptrdiff uintmax EXEEXT OBJEXT Ae conftest AXP str
20320 @c  LocalWords:  ALIGNOF WERROR Werror cpp HP's WorkShop egcs un fied stdc CXX
20321 @c  LocalWords:  varargs BIGENDIAN Endianness SPARC endianness grep'ed CONST FC
20322 @c  LocalWords:  const STRINGIZE stringizing PARAMS unprotoize protos KCC cxx
20323 @c  LocalWords:  xlC aCC CXXCPP FREEFORM xlf FLIBS FCLIBS ish SRCEXT XTRA LFS
20324 @c  LocalWords:  ISC lcposix MINIX Minix conditionalized inlines hw dD confdefs
20325 @c  LocalWords:  fputs stdout PREPROC ar UFS HFS QNX realtime fstype STATVFS se
20326 @c  LocalWords:  statvfs STATFS statfs func machfile hdr lelf raboof DEFUN GTK
20327 @c  LocalWords:  GTKMM Grmph ified ine defn baz EOF qar Ahhh changecom algol io
20328 @c  LocalWords:  changeword quadrigraphs quadrigraph dnl SGI atoi overquoting
20329 @c  LocalWords:  Aas Wcross sep args namespace undefine bpatsubst popdef dquote
20330 @c  LocalWords:  bregexp Overquote overquotation meisch maisch meische maische
20331 @c  LocalWords:  miscian DIRNAME dirname MKDIR CATFILE XMKMF TRAVOLTA celsius
20332 @c  LocalWords:  EMX emxos Emacsen Korn DYNIX subshell posix Ksh ksh Pdksh Zsh
20333 @c  LocalWords:  pdksh zsh Allbery Lipe Kubota UWS zorglub stderr eval esac lfn
20334 @c  LocalWords:  drivespec Posixy DJGPP doschk prettybird LPT pfew Zsh's yu yaa
20335 @c  LocalWords:  yM uM aM firebird IP subdir misparses ok Unpatched abc bc zA
20336 @c  LocalWords:  CDPATH DUALCASE LINENO prepass Subshells lineno NULLCMD cmp wc
20337 @c  LocalWords:  MAILPATH scanset arg NetBSD Almquist printf expr cp
20338 @c  LocalWords:  Oliva awk Aaaaarg cmd regex xfoo GNV OpenVMS VM
20339 @c  LocalWords:  sparc Proulx nbar nfoo maxdepth acdilrtu TWG mc
20340 @c  LocalWords:  mkdir exe uname OpenBSD Fileutils mktemp umask TMPDIR guid os
20341 @c  LocalWords:  fooXXXXXX Unicos utimes hpux hppa unescaped
20342 @c  LocalWords:  pmake DOS's gmake ifoo DESTDIR autoconfiscated pc coff mips gg
20343 @c  LocalWords:  dec ultrix cpu wildcards rpcc rdtsc powerpc readline
20344 @c  LocalWords:  withval vxworks gless localcache usr LOFF loff CYGWIN Cygwin
20345 @c  LocalWords:  cygwin SIGLIST siglist SYSNDIR SYSDIR ptx lseq rusage elif MSC
20346 @c  LocalWords:  lfoo POUNDBANG lsun NIS getpwnam SYSCALLS RSH INTL lintl aix
20347 @c  LocalWords:  intl lx ldir syslog bsd EPI toolchain netbsd objext de KNR nn
20348 @c  LocalWords:  fication LTLIBOBJS Wdiff TESTDIR atconfig atlocal akim XFAIL
20349 @c  LocalWords:  ChangeLog prepended errexit smallexample TESTSUITEFLAGS GPL er
20350 @c  LocalWords:  installcheck autotest indir Pixley Bothner Eichin Kerberos adl
20351 @c  LocalWords:  DISTCLEANFILES preprocessor's fileutils Stallman Murphey Stenn
20352 @c  LocalWords:  Manfredi Autoconfig TCL FSP david zuhn Blandy MACRODIR Raeburn
20353 @c  LocalWords:  autoconfiscate Savoye Haertel Avera Meyering fdl appendixsec
20354 @c  LocalWords:  printindex american LIBOBJDIR LibdirTest ERLCFLAGS OBJCFLAGS
20355 @c  LocalWords:  VER Gnulib online xyes strcpy TYPEOF typeof OBJC objcc objc ln
20356 @c  LocalWords:  GOBJC OTP ERLC erl valloc decr dumpdef errprint incr
20357 @c  LocalWords:  esyscmd len maketemp pushdef substr syscmd sysval translit txt
20358 @c  LocalWords:  sinclude foreach myvar tolower toupper uniq BASENAME STDIN
20359 @c  LocalWords:  Dynix descrips basename aname cname macroexpands xno xcheck
20360 @c  LocalWords:  LIBREADLINE lreadline lncurses libreadline
20362 @c Local Variables:
20363 @c fill-column: 72
20364 @c ispell-local-dictionary: "american"
20365 @c indent-tabs-mode: nil
20366 @c whitespace-check-buffer-indent: nil
20367 @c End: