* doc/autoconf.texi (Volatile Objects): New section.
[autoconf.git] / doc / autoconf.texi
blobf64b8b1fa9ad35021e7321fadb4310c108bc6125
1 \input texinfo @c -*-texinfo-*-
2 @comment ========================================================
3 @comment %**start of header
4 @setfilename autoconf.info
5 @include version.texi
6 @settitle Autoconf
7 @setchapternewpage odd
8 @ifnothtml
9 @setcontentsaftertitlepage
10 @end ifnothtml
11 @finalout
13 @c @ovar(ARG, DEFAULT)
14 @c -------------------
15 @c The ARG is an optional argument.  To be used for macro arguments in
16 @c their documentation (@defmac).
17 @macro ovar{varname}
18 @r{[}@var{\varname\}@r{]}
19 @end macro
21 @c @dvar(ARG, DEFAULT)
22 @c -------------------
23 @c The ARG is an optional argument, defaulting to DEFAULT.  To be used
24 @c for macro arguments in their documentation (@defmac).
25 @macro dvar{varname, default}
26 @r{[}@var{\varname\} = @samp{\default\}@r{]}
27 @end macro
29 @c Handling the indexes with Texinfo yields several different problems.
31 @c Because we want to drop out the AC_ part of the macro names in the
32 @c printed manual, but not in the other outputs, we need a layer above
33 @c the usual @acindex{} etc.  That's why we first define indexes such as
34 @c acx meant to become the macro @acindex.  First of all, using ``ac_''
35 @c does not work with makeinfo, and using ``ac1'' doesn't work with TeX.
36 @c So use something more regular ``acx''.  Then you finish with a printed
37 @c index saying ``index is not existent''.  Of course: you ought to use
38 @c two letters :(  So you use capitals.
40 @c Second, when defining a macro in the TeX world, following spaces are
41 @c eaten.  But then, since we embed @acxindex commands that use the end
42 @c of line as an end marker, the whole things wrecks itself.  So make
43 @c sure you do *force* an additional end of line, add a ``@c''.
45 @c Finally, you might want to get rid of TeX expansion, using --expand
46 @c with texi2dvi.  But then you wake up an old problem: we use macros
47 @c in @defmac etc. where TeX does perform the expansion, but not makeinfo.
49 @c Define an environment variable index.
50 @defcodeindex ev
51 @c Define an output variable index.
52 @defcodeindex ov
53 @c Define a CPP variable index.
54 @defcodeindex cv
55 @c Define an Autoconf macro index that @defmac doesn't write to.
56 @defcodeindex AC
57 @c Define an Autotest macro index that @defmac doesn't write to.
58 @defcodeindex AT
59 @c Define an M4sugar macro index that @defmac doesn't write to.
60 @defcodeindex MS
61 @c Define an index for *foreign* programs: `mv' etc.  Used for the
62 @c portability sections and so on.
63 @defindex pr
65 @c shortindexflag
66 @c --------------
67 @c Shall we factor AC_ out of the Autoconf macro index etc.?
68 @iftex
69 @set shortindexflag
70 @end iftex
72 @c @acindex{MACRO}
73 @c ---------------
74 @c Registering an AC_\MACRO\.
75 @ifset shortindexflag
76 @macro acindex{macro}
77 @ACindex \macro\
79 @end macro
80 @end ifset
81 @ifclear shortindexflag
82 @macro acindex{macro}
83 @ACindex AC_\macro\
84 @end macro
85 @end ifclear
87 @c @ahindex{MACRO}
88 @c ---------------
89 @c Registering an AH_\MACRO\.
90 @macro ahindex{macro}
91 @ACindex AH_\macro\
93 @end macro
95 @c @asindex{MACRO}
96 @c ---------------
97 @c Registering an AS_\MACRO\.
98 @ifset shortindexflag
99 @macro asindex{macro}
100 @MSindex \macro\
102 @end macro
103 @end ifset
104 @ifclear shortindexflag
105 @macro asindex{macro}
106 @MSindex AS_\macro\
107 @end macro
108 @end ifclear
110 @c @atindex{MACRO}
111 @c ---------------
112 @c Registering an AT_\MACRO\.
113 @ifset shortindexflag
114 @macro atindex{macro}
115 @ATindex \macro\
117 @end macro
118 @end ifset
119 @ifclear shortindexflag
120 @macro atindex{macro}
121 @ATindex AT_\macro\
122 @end macro
123 @end ifclear
125 @c @auindex{MACRO}
126 @c ---------------
127 @c Registering an AU_\MACRO\.
128 @macro auindex{macro}
129 @ACindex AU_\macro\
131 @end macro
133 @c @hdrindex{MACRO}
134 @c ----------------
135 @c Indexing a header.
136 @macro hdrindex{macro}
137 @prindex @file{\macro\}
139 @end macro
141 @c @msindex{MACRO}
142 @c ---------------
143 @c Registering an m4_\MACRO\.
144 @ifset shortindexflag
145 @macro msindex{macro}
146 @MSindex \macro\
148 @end macro
149 @end ifset
150 @ifclear shortindexflag
151 @macro msindex{macro}
152 @MSindex m4_\macro\
153 @end macro
154 @end ifclear
157 @c Define an index for functions: `alloca' etc.  Used for the
158 @c portability sections and so on.  We can't use `fn' (aka `fnindex),
159 @c since `@defmac' goes into it => we'd get all the macros too.
161 @c   FIXME: Aaarg!  It seems there are too many indices for TeX :(
163 @c   ! No room for a new @write .
164 @c   l.112 @defcodeindex fu
166 @c   so don't define yet another one :(  Just put some tags before each
167 @c   @prindex which is actually a @funindex.
169 @c   @defcodeindex fu
172 @c   @c Put the programs and functions into their own index.
173 @c   @syncodeindex fu pr
175 @comment %**end of header
176 @comment ========================================================
178 @copying
180 This manual is for @acronym{GNU} Autoconf
181 (version @value{VERSION}, @value{UPDATED}),
182 a package for creating scripts to configure source code packages using
183 templates and an M4 macro package.
185 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
186 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
188 @quotation
189 Permission is granted to copy, distribute and/or modify this document
190 under the terms of the @acronym{GNU} Free Documentation License,
191 Version 1.2 or any later version published by the Free Software
192 Foundation; with no Invariant Sections, with the Front-Cover texts
193 being ``A @acronym{GNU} Manual,'' and with the Back-Cover Texts as in
194 (a) below.  A copy of the license is included in the section entitled
195 ``@acronym{GNU} Free Documentation License.''
197 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and
198 modify this @acronym{GNU} Manual, like @acronym{GNU} software.  Copies
199 published by the Free Software Foundation raise funds for
200 @acronym{GNU} development.''
201 @end quotation
202 @end copying
206 @dircategory Software development
207 @direntry
208 * Autoconf: (autoconf).         Create source code configuration scripts.
209 @end direntry
211 @dircategory Individual utilities
212 @direntry
213 * autoscan: (autoconf)autoscan Invocation.
214                                 Semi-automatic @file{configure.ac} writing
215 * ifnames: (autoconf)ifnames Invocation.        Listing conditionals in source.
216 * autoconf: (autoconf)autoconf Invocation.
217                                 How to create configuration scripts
218 * autoreconf: (autoconf)autoreconf Invocation.
219                                 Remaking multiple @command{configure} scripts
220 * autoheader: (autoconf)autoheader Invocation.
221                                 How to create configuration templates
222 * autom4te: (autoconf)autom4te Invocation.
223                                 The Autoconf executables backbone
224 * configure: (autoconf)configure Invocation.    Configuring a package.
225 * autoupdate: (autoconf)autoupdate Invocation.
226                                 Automatic update of @file{configure.ac}
227 * config.status: (autoconf)config.status Invocation. Recreating configurations.
228 * testsuite: (autoconf)testsuite Invocation.    Running an Autotest test suite.
229 @end direntry
231 @titlepage
232 @title Autoconf
233 @subtitle Creating Automatic Configuration Scripts
234 @subtitle for version @value{VERSION}, @value{UPDATED}
235 @author David MacKenzie
236 @author Ben Elliston
237 @author Akim Demaille
238 @page
239 @vskip 0pt plus 1filll
240 @insertcopying
241 @end titlepage
243 @contents
246 @ifnottex
247 @node Top
248 @top Autoconf
249 @insertcopying
250 @end ifnottex
252 @c The master menu, created with texinfo-master-menu, goes here.
254 @menu
255 * Introduction::                Autoconf's purpose, strengths, and weaknesses
256 * The GNU Build System::        A set of tools for portable software packages
257 * Making configure Scripts::    How to organize and produce Autoconf scripts
258 * Setup::                       Initialization and output
259 * Existing Tests::              Macros that check for particular features
260 * Writing Tests::               How to write new feature checks
261 * Results::                     What to do with results from feature checks
262 * Programming in M4::           Layers on top of which Autoconf is written
263 * Writing Autoconf Macros::     Adding new macros to Autoconf
264 * Portable Shell::              Shell script portability pitfalls
265 * Portable 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 * Copying This Manual::         How to make copies of 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 configure.ac::        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 * configure.ac Layout::         Standard organization of @file{configure.ac}
302 Initialization and Output Files
304 * Initializing configure::      Option processing etc.
305 * Notices::                     Copyright, version numbers in @command{configure}
306 * Input::                       Where Autoconf should find files
307 * Output::                      Outputting results from the configuration
308 * Configuration Actions::       Preparing the output based on results
309 * Configuration Files::         Creating output files
310 * Makefile Substitutions::      Using output variables in makefiles
311 * Configuration Headers::       Creating a configuration header file
312 * Configuration Commands::      Running arbitrary instantiation commands
313 * Configuration Links::         Links depending on the configuration
314 * Subdirectories::              Configuring independent packages together
315 * Default Prefix::              Changing the default installation prefix
317 Substitutions in Makefiles
319 * Preset Output Variables::     Output variables that are always set
320 * Installation Directory Variables::  Other preset output variables
321 * Changed Directory Variables:: Warnings about @file{datarootdir}
322 * Build Directories::           Supporting multiple concurrent compiles
323 * Automatic Remaking::          Makefile rules for configuring
325 Configuration Header Files
327 * Header Templates::            Input for the configuration headers
328 * autoheader Invocation::       How to create configuration templates
329 * Autoheader Macros::           How to specify CPP templates
331 Existing Tests
333 * Common Behavior::             Macros' standard schemes
334 * Alternative Programs::        Selecting between alternative programs
335 * Files::                       Checking for the existence of files
336 * Libraries::                   Library archives that might be missing
337 * Library Functions::           C library functions that might be missing
338 * Header Files::                Header files that might be missing
339 * Declarations::                Declarations that may be missing
340 * Structures::                  Structures or members that might be missing
341 * Types::                       Types that might be missing
342 * Compilers and Preprocessors::  Checking for compiling programs
343 * System Services::             Operating system services
344 * Posix Variants::              Special kludges for specific Posix variants
345 * Erlang Libraries::            Checking for the existence of Erlang libraries
347 Common Behavior
349 * Standard Symbols::            Symbols defined by the macros
350 * Default Includes::            Includes used by the generic macros
352 Alternative Programs
354 * Particular Programs::         Special handling to find certain programs
355 * Generic Programs::            How to find other programs
357 Library Functions
359 * Function Portability::        Pitfalls with usual functions
360 * Particular Functions::        Special handling to find certain functions
361 * Generic Functions::           How to find other functions
363 Header Files
365 * Header Portability::          Collected knowledge on common headers
366 * Particular Headers::          Special handling to find certain headers
367 * Generic Headers::             How to find other headers
369 Declarations
371 * Particular Declarations::     Macros to check for certain declarations
372 * Generic Declarations::        How to find other declarations
374 Structures
376 * Particular Structures::       Macros to check for certain structure members
377 * Generic Structures::          How to find other structure members
379 Types
381 * Particular Types::            Special handling to find certain types
382 * Generic Types::               How to find other types
384 Compilers and Preprocessors
386 * Specific Compiler Characteristics::  Some portability issues
387 * Generic Compiler Characteristics::  Language independent tests and features
388 * C Compiler::                  Checking its characteristics
389 * C++ Compiler::                Likewise
390 * Objective C Compiler::        Likewise
391 * Erlang Compiler and Interpreter::  Likewise
392 * Fortran Compiler::            Likewise
394 Writing Tests
396 * Language Choice::             Selecting which language to use for testing
397 * Writing Test Programs::       Forging source files for compilers
398 * Running the Preprocessor::    Detecting preprocessor symbols
399 * Running the Compiler::        Detecting language or header features
400 * Running the Linker::          Detecting library features
401 * Runtime::                     Testing for runtime features
402 * Systemology::                 A zoology of operating systems
403 * Multiple Cases::              Tests for several possible values
405 Writing Test Programs
407 * Guidelines::                  General rules for writing test programs
408 * Test Functions::              Avoiding pitfalls in test programs
409 * Generating Sources::          Source program boilerplate
411 Results of Tests
413 * Defining Symbols::            Defining C preprocessor symbols
414 * Setting Output Variables::    Replacing variables in output files
415 * Special Chars in Variables::  Characters to beware of in variables
416 * Caching Results::             Speeding up subsequent @command{configure} runs
417 * Printing Messages::           Notifying @command{configure} users
419 Caching Results
421 * Cache Variable Names::        Shell variables used in caches
422 * Cache Files::                 Files @command{configure} uses for caching
423 * Cache Checkpointing::         Loading and saving the cache file
425 Programming in M4
427 * M4 Quotation::                Protecting macros from unwanted expansion
428 * Using autom4te::              The Autoconf executables backbone
429 * Programming in M4sugar::      Convenient pure M4 macros
430 * Programming in M4sh::         Common shell Constructs
431 * File Descriptor Macros::      File descriptor macros for input and output
433 M4 Quotation
435 * Active Characters::           Characters that change the behavior of M4
436 * One Macro Call::              Quotation and one macro call
437 * Quotation and Nested Macros::  Macros calling macros
438 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
439 * Quadrigraphs::                Another way to escape special characters
440 * Quotation Rule Of Thumb::     One parenthesis, one quote
442 Using @command{autom4te}
444 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
445 * Customizing autom4te::        Customizing the Autoconf package
447 Programming in M4sugar
449 * Redefined M4 Macros::         M4 builtins changed in M4sugar
450 * Looping constructs::          Iteration in M4
451 * Evaluation Macros::           More quotation and evaluation control
452 * Text processing Macros::      String manipulation in M4
453 * Forbidden Patterns::          Catching unexpanded macros
455 Writing Autoconf Macros
457 * Macro Definitions::           Basic format of an Autoconf macro
458 * Macro Names::                 What to call your new macros
459 * Reporting Messages::          Notifying @command{autoconf} users
460 * Dependencies Between Macros::  What to do when macros depend on other macros
461 * Obsoleting Macros::           Warning about old ways of doing things
462 * Coding Style::                Writing Autoconf macros @`a la Autoconf
464 Dependencies Between Macros
466 * Prerequisite Macros::         Ensuring required information
467 * Suggested Ordering::          Warning about possible ordering problems
468 * One-Shot Macros::             Ensuring a macro is called only once
470 Portable Shell Programming
472 * Shellology::                  A zoology of shells
473 * Here-Documents::              Quirks and tricks
474 * File Descriptors::            FDs and redirections
475 * File System Conventions::     File names
476 * Shell Substitutions::         Variable and command expansions
477 * Assignments::                 Varying side effects of assignments
478 * Parentheses::                 Parentheses in shell scripts
479 * Slashes::                     Slashes in shell scripts
480 * Special Shell Variables::     Variables you should not change
481 * Limitations of Builtins::     Portable use of not so portable /bin/sh
482 * Limitations of Usual Tools::  Portable use of portable tools
484 Portable Make Programming
486 * $< in Ordinary Make Rules::   $< in ordinary rules
487 * Failure in Make Rules::       Failing portably in rules
488 * Special Chars in Names::      Special Characters in Macro Names
489 * Backslash-Newline-Newline::   Empty last lines in macro definitions
490 * Backslash-Newline Comments::  Spanning comments across line boundaries
491 * Long Lines in Makefiles::     Line length limitations
492 * Macros and Submakes::         @code{make macro=value} and submakes
493 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
494 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
495 * Comments in Make Rules::      Other problems with Make comments
496 * obj/ and Make::               Don't name a subdirectory @file{obj}
497 * make -k Status::              Exit status of @samp{make -k}
498 * VPATH and Make::              @code{VPATH} woes
499 * Single Suffix Rules::         Single suffix rules and separated dependencies
500 * Timestamps and Make::         Subsecond timestamp resolution
502 @code{VPATH} and Make
504 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
505 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
506 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
507 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
508 * Make Target Lookup::          More details about @code{VPATH} lookup
510 Portable C and C++ Programming
512 * Varieties of Unportability::  How to make your programs unportable
513 * Integer Overflow::            When integers get too large
514 * Null Pointers::               Properties of null pointers
515 * Buffer Overruns::             Subscript errors and the like
516 * Volatile Objects::            @code{volatile} and signals
517 * Floating Point Portability::  Portable floating-point arithmetic
518 * Exiting Portably::            Exiting and the exit status
520 Manual Configuration
522 * Specifying Names::            Specifying the system type
523 * Canonicalizing::              Getting the canonical system type
524 * Using System Type::           What to do with the system type
526 Site Configuration
528 * Help Formatting::             Customizing @samp{configure --help}
529 * External Software::           Working with other optional software
530 * Package Options::             Selecting optional features
531 * Pretty Help Strings::         Formatting help string
532 * Site Details::                Configuring site details
533 * Transforming Names::          Changing program names when installing
534 * Site Defaults::               Giving @command{configure} local defaults
536 Transforming Program Names When Installing
538 * Transformation Options::      @command{configure} options to transform names
539 * Transformation Examples::     Sample uses of transforming names
540 * Transformation Rules::        Makefile uses of transforming names
542 Running @command{configure} Scripts
544 * Basic Installation::          Instructions for typical cases
545 * Compilers and Options::       Selecting compilers and optimization
546 * Multiple Architectures::      Compiling for multiple architectures at once
547 * Installation Names::          Installing in different directories
548 * Optional Features::           Selecting optional features
549 * System Type::                 Specifying the system type
550 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
551 * Defining Variables::          Specifying the compiler etc.
552 * configure Invocation::        Changing how @command{configure} runs
554 Obsolete Constructs
556 * Obsolete config.status Use::  Different calling convention
557 * acconfig.h::                  Additional entries in @file{config.h.in}
558 * autoupdate Invocation::       Automatic update of @file{configure.ac}
559 * Obsolete Macros::             Backward compatibility macros
560 * Autoconf 1::                  Tips for upgrading your files
561 * Autoconf 2.13::               Some fresher tips
563 Upgrading From Version 1
565 * Changed File Names::          Files you might rename
566 * Changed Makefiles::           New things to put in @file{Makefile.in}
567 * Changed Macros::              Macro calls you might replace
568 * Changed Results::             Changes in how to check test results
569 * Changed Macro Writing::       Better ways to write your own macros
571 Upgrading From Version 2.13
573 * Changed Quotation::           Broken code which used to work
574 * New Macros::                  Interaction with foreign macros
575 * Hosts and Cross-Compilation::  Bugward compatibility kludges
576 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
577 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
579 Generating Test Suites with Autotest
581 * Using an Autotest Test Suite::  Autotest and the user
582 * Writing testsuite.at::        Autotest macros
583 * testsuite Invocation::        Running @command{testsuite} scripts
584 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
586 Using an Autotest Test Suite
588 * testsuite Scripts::           The concepts of Autotest
589 * Autotest Logs::               Their contents
591 Frequent Autoconf Questions, with answers
593 * Distributing::                Distributing @command{configure} scripts
594 * Why GNU M4::                  Why not use the standard M4?
595 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
596 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
597 * Defining Directories::        Passing @code{datadir} to program
598 * autom4te.cache::              What is it?  Can I remove it?
599 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
601 History of Autoconf
603 * Genesis::                     Prehistory and naming of @command{configure}
604 * Exodus::                      The plagues of M4 and Perl
605 * Leviticus::                   The priestly code of portability arrives
606 * Numbers::                     Growth and contributors
607 * Deuteronomy::                 Approaching the promises of easy configuration
609 Copying This Manual
611 * GNU Free Documentation License::  License for copying this manual
613 Indices
615 * Environment Variable Index::  Index of environment variables used
616 * Output Variable Index::       Index of variables set in output files
617 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
618 * Autoconf Macro Index::        Index of Autoconf macros
619 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
620 * Autotest Macro Index::        Index of Autotest macros
621 * Program & Function Index::    Index of those with portability problems
622 * Concept Index::               General index
624 @end detailmenu
625 @end menu
627 @c ============================================================= Introduction.
629 @node Introduction
630 @chapter Introduction
631 @cindex Introduction
633 @flushright
634 A physicist, an engineer, and a computer scientist were discussing the
635 nature of God.  ``Surely a Physicist,'' said the physicist, ``because
636 early in the Creation, God made Light; and you know, Maxwell's
637 equations, the dual nature of electromagnetic waves, the relativistic
638 consequences@dots{}'' ``An Engineer!,'' said the engineer, ``because
639 before making Light, God split the Chaos into Land and Water; it takes a
640 hell of an engineer to handle that big amount of mud, and orderly
641 separation of solids from liquids@dots{}'' The computer scientist
642 shouted: ``And the Chaos, where do you think it was coming from, hmm?''
644 ---Anonymous
645 @end flushright
646 @c (via Franc,ois Pinard)
648 Autoconf is a tool for producing shell scripts that automatically
649 configure software source code packages to adapt to many kinds of
650 Posix-like systems.  The configuration scripts produced by Autoconf
651 are independent of Autoconf when they are run, so their users do not
652 need to have Autoconf.
654 The configuration scripts produced by Autoconf require no manual user
655 intervention when run; they do not normally even need an argument
656 specifying the system type.  Instead, they individually test for the
657 presence of each feature that the software package they are for might need.
658 (Before each check, they print a one-line message stating what they are
659 checking for, so the user doesn't get too bored while waiting for the
660 script to finish.)  As a result, they deal well with systems that are
661 hybrids or customized from the more common Posix variants.  There is
662 no need to maintain files that list the features supported by each
663 release of each variant of Posix.
665 For each software package that Autoconf is used with, it creates a
666 configuration script from a template file that lists the system features
667 that the package needs or can use.  After the shell code to recognize
668 and respond to a system feature has been written, Autoconf allows it to
669 be shared by many software packages that can use (or need) that feature.
670 If it later turns out that the shell code needs adjustment for some
671 reason, it needs to be changed in only one place; all of the
672 configuration scripts can be regenerated automatically to take advantage
673 of the updated code.
675 The Metaconfig package is similar in purpose to Autoconf, but the
676 scripts it produces require manual user intervention, which is quite
677 inconvenient when configuring large source trees.  Unlike Metaconfig
678 scripts, Autoconf scripts can support cross-compiling, if some care is
679 taken in writing them.
681 Autoconf does not solve all problems related to making portable
682 software packages---for a more complete solution, it should be used in
683 concert with other @acronym{GNU} build tools like Automake and
684 Libtool.  These other tools take on jobs like the creation of a
685 portable, recursive makefile with all of the standard targets,
686 linking of shared libraries, and so on.  @xref{The GNU Build System},
687 for more information.
689 Autoconf imposes some restrictions on the names of macros used with
690 @code{#if} in C programs (@pxref{Preprocessor Symbol Index}).
692 Autoconf requires @acronym{GNU} M4 in order to generate the scripts.  It uses
693 features that some versions of M4, including @acronym{GNU} M4 1.3,
694 do not have.  You should use version 1.4.4 or later of @acronym{GNU} M4.
696 @xref{Autoconf 1}, for information about upgrading from version 1.
697 @xref{History}, for the story of Autoconf's development.  @xref{FAQ},
698 for answers to some common questions about Autoconf.
700 See the @uref{http://www.gnu.org/software/autoconf/,
701 Autoconf web page} for up-to-date information, details on the mailing
702 lists, pointers to a list of known bugs, etc.
704 Mail suggestions to @email{autoconf@@gnu.org, the Autoconf mailing
705 list}.  Past suggestions are
706 @uref{http://lists.gnu.org/archive/html/autoconf/, archived}.
708 Mail bug reports to @email{bug-autoconf@@gnu.org, the
709 Autoconf Bugs mailing list}.  Past bug reports are
710 @uref{http://lists.gnu.org/archive/html/bug-autoconf/, archived}.
712 If possible, first check that your bug is
713 not already solved in current development versions, and that it has not
714 been reported yet.  Be sure to include all the needed information and a
715 short @file{configure.ac} that demonstrates the problem.
717 Autoconf's development tree is accessible via anonymous @acronym{CVS}; see the
718 @uref{http://savannah.gnu.org/projects/autoconf/, Autoconf
719 Summary} for details.  Patches relative to the
720 current @acronym{CVS} version can be sent for review to the
721 @email{autoconf-patches@@gnu.org, Autoconf Patches mailing list}.
722 Past patches are
723 @uref{http://lists.gnu.org/@/archive/@/html/@/autoconf-patches/, archived}.
725 Because of its mission, the Autoconf package itself
726 includes only a set of often-used
727 macros that have already demonstrated their usefulness.  Nevertheless,
728 if you wish to share your macros, or find existing ones, see the
729 @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
730 Archive}, which is kindly run by @email{simons@@cryp.to,
731 Peter Simons}.
734 @c ================================================= The GNU Build System
736 @node The GNU Build System
737 @chapter The @acronym{GNU} Build System
738 @cindex @acronym{GNU} build system
740 Autoconf solves an important problem---reliable discovery of
741 system-specific build and runtime information---but this is only one
742 piece of the puzzle for the development of portable software.  To this
743 end, the @acronym{GNU} project has developed a suite of integrated
744 utilities to finish the job Autoconf started: the @acronym{GNU} build
745 system, whose most important components are Autoconf, Automake, and
746 Libtool.  In this chapter, we introduce you to those tools, point you
747 to sources of more information, and try to convince you to use the
748 entire @acronym{GNU} build system for your software.
750 @menu
751 * Automake::                    Escaping makefile hell
752 * Gnulib::                      The @acronym{GNU} portability library
753 * Libtool::                     Building libraries portably
754 * Pointers::                    More info on the @acronym{GNU} build system
755 @end menu
757 @node Automake
758 @section Automake
760 The ubiquity of @command{make} means that a makefile is almost the
761 only viable way to distribute automatic build rules for software, but
762 one quickly runs into its numerous limitations.  Its lack of
763 support for automatic dependency tracking, recursive builds in
764 subdirectories, reliable timestamps (e.g., for network file systems), and
765 so on, mean that developers must painfully (and often incorrectly)
766 reinvent the wheel for each project.  Portability is non-trivial, thanks
767 to the quirks of @command{make} on many systems.  On top of all this is the
768 manual labor required to implement the many standard targets that users
769 have come to expect (@code{make install}, @code{make distclean},
770 @code{make uninstall}, etc.).  Since you are, of course, using Autoconf,
771 you also have to insert repetitive code in your @code{Makefile.in} to
772 recognize @code{@@CC@@}, @code{@@CFLAGS@@}, and other substitutions
773 provided by @command{configure}.  Into this mess steps @dfn{Automake}.
774 @cindex Automake
776 Automake allows you to specify your build needs in a @code{Makefile.am}
777 file with a vastly simpler and more powerful syntax than that of a plain
778 makefile, and then generates a portable @code{Makefile.in} for
779 use with Autoconf.  For example, the @code{Makefile.am} to build and
780 install a simple ``Hello world'' program might look like:
782 @example
783 bin_PROGRAMS = hello
784 hello_SOURCES = hello.c
785 @end example
787 @noindent
788 The resulting @code{Makefile.in} (~400 lines) automatically supports all
789 the standard targets, the substitutions provided by Autoconf, automatic
790 dependency tracking, @code{VPATH} building, and so on.  @command{make}
791 builds the @code{hello} program, and @code{make install} installs it
792 in @file{/usr/local/bin} (or whatever prefix was given to
793 @command{configure}, if not @file{/usr/local}).
795 The benefits of Automake increase for larger packages (especially ones
796 with subdirectories), but even for small programs the added convenience
797 and portability can be substantial.  And that's not all@enddots{}
799 @node Gnulib
800 @section Gnulib
802 @acronym{GNU} software has a well-deserved reputation for running on
803 many different types of systems.  While our primary goal is to write
804 software for the @acronym{GNU} system, many users and developers have
805 been introduced to us through the systems that they were already using.
807 @cindex Gnulib
808 Gnulib is a central location for common @acronym{GNU} code, intended to
809 be shared among free software packages.  Its components are typically
810 shared at the source level, rather than being a library that gets built,
811 installed, and linked against.  The idea is to copy files from Gnulib
812 into your own source tree.  There is no distribution tarball; developers
813 should just grab source modules from the repository.  The source files
814 are available online, under various licenses, mostly @acronym{GNU}
815 @acronym{GPL} or @acronym{GNU} @acronym{LGPL}.
817 Gnulib modules typically contain C source code along with Autoconf
818 macros used to configure the source code.  For example, the Gnulib
819 @code{stdbool} module implements a @file{stdbool.h} header that nearly
820 conforms to C99, even on old-fashioned hosts that lack @file{stdbool.h}.
821 This module contains a source file for the replacement header, along
822 with an Autoconf macro that arranges to use the replacement header on
823 old-fashioned systems.
825 @node Libtool
826 @section Libtool
828 Often, one wants to build not only programs, but libraries, so that
829 other programs can benefit from the fruits of your labor.  Ideally, one
830 would like to produce @emph{shared} (dynamically linked) libraries,
831 which can be used by multiple programs without duplication on disk or in
832 memory and can be updated independently of the linked programs.
833 Producing shared libraries portably, however, is the stuff of
834 nightmares---each system has its own incompatible tools, compiler flags,
835 and magic incantations.  Fortunately, @acronym{GNU} provides a solution:
836 @dfn{Libtool}.
837 @cindex Libtool
839 Libtool handles all the requirements of building shared libraries for
840 you, and at this time seems to be the @emph{only} way to do so with any
841 portability.  It also handles many other headaches, such as: the
842 interaction of Make rules with the variable suffixes of
843 shared libraries, linking reliably with shared libraries before they are
844 installed by the superuser, and supplying a consistent versioning system
845 (so that different versions of a library can be installed or upgraded
846 without breaking binary compatibility).  Although Libtool, like
847 Autoconf, can be used without Automake, it is most simply utilized in
848 conjunction with Automake---there, Libtool is used automatically
849 whenever shared libraries are needed, and you need not know its syntax.
851 @node Pointers
852 @section Pointers
854 Developers who are used to the simplicity of @command{make} for small
855 projects on a single system might be daunted at the prospect of
856 learning to use Automake and Autoconf.  As your software is
857 distributed to more and more users, however, you otherwise
858 quickly find yourself putting lots of effort into reinventing the
859 services that the @acronym{GNU} build tools provide, and making the
860 same mistakes that they once made and overcame.  (Besides, since
861 you're already learning Autoconf, Automake is a piece of cake.)
863 There are a number of places that you can go to for more information on
864 the @acronym{GNU} build tools.
866 @itemize @minus
868 @item Web
870 The home pages for
871 @uref{http://www.gnu.org/@/software/@/autoconf/, Autoconf},
872 @uref{http://www.gnu.org/@/software/@/automake/, Automake},
873 @uref{http://www.gnu.org/@/software/@/gnulib/, Gnulib}, and
874 @uref{http://www.gnu.org/@/software/@/libtool/, Libtool}.
876 @item Automake Manual
878 @xref{Top, , Automake, automake, @acronym{GNU} Automake}, for more
879 information on Automake.
881 @item Books
883 The book @cite{@acronym{GNU} Autoconf, Automake and
884 Libtool}@footnote{@cite{@acronym{GNU} Autoconf, Automake and Libtool},
885 by G. V. Vaughan, B. Elliston, T. Tromey, and I. L. Taylor.  SAMS (originally
886 New Riders), 2000, ISBN 1578701902.} describes the complete @acronym{GNU}
887 build environment.  You can also find
888 @uref{http://sources.redhat.com/@/autobook/, the entire book on-line}.
890 @end itemize
892 @c ================================================= Making configure Scripts.
894 @node Making configure Scripts
895 @chapter Making @command{configure} Scripts
896 @cindex @file{aclocal.m4}
897 @cindex @command{configure}
899 The configuration scripts that Autoconf produces are by convention
900 called @command{configure}.  When run, @command{configure} creates several
901 files, replacing configuration parameters in them with appropriate
902 values.  The files that @command{configure} creates are:
904 @itemize @minus
905 @item
906 one or more @file{Makefile} files, usually one in each subdirectory of the
907 package (@pxref{Makefile Substitutions});
909 @item
910 optionally, a C header file, the name of which is configurable,
911 containing @code{#define} directives (@pxref{Configuration Headers});
913 @item
914 a shell script called @file{config.status} that, when run, recreates
915 the files listed above (@pxref{config.status Invocation});
917 @item
918 an optional shell script normally called @file{config.cache}
919 (created when using @samp{configure --config-cache}) that
920 saves the results of running many of the tests (@pxref{Cache Files});
922 @item
923 a file called @file{config.log} containing any messages produced by
924 compilers, to help debugging if @command{configure} makes a mistake.
925 @end itemize
927 @cindex @file{configure.in}
928 @cindex @file{configure.ac}
929 To create a @command{configure} script with Autoconf, you need to write an
930 Autoconf input file @file{configure.ac} (or @file{configure.in}) and run
931 @command{autoconf} on it.  If you write your own feature tests to
932 supplement those that come with Autoconf, you might also write files
933 called @file{aclocal.m4} and @file{acsite.m4}.  If you use a C header
934 file to contain @code{#define} directives, you might also run
935 @command{autoheader}, and you can distribute the generated file
936 @file{config.h.in} with the package.
938 Here is a diagram showing how the files that can be used in
939 configuration are produced.  Programs that are executed are suffixed by
940 @samp{*}.  Optional files are enclosed in square brackets (@samp{[]}).
941 @command{autoconf} and @command{autoheader} also read the installed Autoconf
942 macro files (by reading @file{autoconf.m4}).
944 @noindent
945 Files used in preparing a software package for distribution:
946 @example
947 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
949 @group
950 configure.ac --.
951                |   .------> autoconf* -----> configure
952 [aclocal.m4] --+---+
953                |   `-----> [autoheader*] --> [config.h.in]
954 [acsite.m4] ---'
955 @end group
957 Makefile.in -------------------------------> Makefile.in
958 @end example
960 @noindent
961 Files used in configuring a software package:
962 @example
963 @group
964                        .-------------> [config.cache]
965 configure* ------------+-------------> config.log
966                        |
967 [config.h.in] -.       v            .-> [config.h] -.
968                +--> config.status* -+               +--> make*
969 Makefile.in ---'                    `-> Makefile ---'
970 @end group
971 @end example
973 @menu
974 * Writing configure.ac::        What to put in an Autoconf input file
975 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
976 * ifnames Invocation::          Listing the conditionals in source code
977 * autoconf Invocation::         How to create configuration scripts
978 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
979 @end menu
981 @node Writing configure.ac
982 @section Writing @file{configure.ac}
984 To produce a @command{configure} script for a software package, create a
985 file called @file{configure.ac} that contains invocations of the
986 Autoconf macros that test the system features your package needs or can
987 use.  Autoconf macros already exist to check for many features; see
988 @ref{Existing Tests}, for their descriptions.  For most other features,
989 you can use Autoconf template macros to produce custom checks; see
990 @ref{Writing Tests}, for information about them.  For especially tricky
991 or specialized features, @file{configure.ac} might need to contain some
992 hand-crafted shell commands; see @ref{Portable Shell}.  The
993 @command{autoscan} program can give you a good start in writing
994 @file{configure.ac} (@pxref{autoscan Invocation}, for more information).
996 Previous versions of Autoconf promoted the name @file{configure.in},
997 which is somewhat ambiguous (the tool needed to process this file is not
998 described by its extension), and introduces a slight confusion with
999 @file{config.h.in} and so on (for which @samp{.in} means ``to be
1000 processed by @command{configure}'').  Using @file{configure.ac} is now
1001 preferred.
1003 @menu
1004 * Shell Script Compiler::       Autoconf as solution of a problem
1005 * Autoconf Language::           Programming in Autoconf
1006 * configure.ac Layout::         Standard organization of @file{configure.ac}
1007 @end menu
1009 @node Shell Script Compiler
1010 @subsection A Shell Script Compiler
1012 Just as for any other computer language, in order to properly program
1013 @file{configure.ac} in Autoconf you must understand @emph{what} problem
1014 the language tries to address and @emph{how} it does so.
1016 The problem Autoconf addresses is that the world is a mess.  After all,
1017 you are using Autoconf in order to have your package compile easily on
1018 all sorts of different systems, some of them being extremely hostile.
1019 Autoconf itself bears the price for these differences: @command{configure}
1020 must run on all those systems, and thus @command{configure} must limit itself
1021 to their lowest common denominator of features.
1023 Naturally, you might then think of shell scripts; who needs
1024 @command{autoconf}?  A set of properly written shell functions is enough to
1025 make it easy to write @command{configure} scripts by hand.  Sigh!
1026 Unfortunately, shell functions do not belong to the least common
1027 denominator; therefore, where you would like to define a function and
1028 use it ten times, you would instead need to copy its body ten times.
1030 So, what is really needed is some kind of compiler, @command{autoconf},
1031 that takes an Autoconf program, @file{configure.ac}, and transforms it
1032 into a portable shell script, @command{configure}.
1034 How does @command{autoconf} perform this task?
1036 There are two obvious possibilities: creating a brand new language or
1037 extending an existing one.  The former option is attractive: all
1038 sorts of optimizations could easily be implemented in the compiler and
1039 many rigorous checks could be performed on the Autoconf program
1040 (e.g., rejecting any non-portable construct).  Alternatively, you can
1041 extend an existing language, such as the @code{sh} (Bourne shell)
1042 language.
1044 Autoconf does the latter: it is a layer on top of @code{sh}.  It was
1045 therefore most convenient to implement @command{autoconf} as a macro
1046 expander: a program that repeatedly performs @dfn{macro expansions} on
1047 text input, replacing macro calls with macro bodies and producing a pure
1048 @code{sh} script in the end.  Instead of implementing a dedicated
1049 Autoconf macro expander, it is natural to use an existing
1050 general-purpose macro language, such as M4, and implement the extensions
1051 as a set of M4 macros.
1054 @node Autoconf Language
1055 @subsection The Autoconf Language
1056 @cindex quotation
1058 The Autoconf language differs from many other computer
1059 languages because it treats actual code the same as plain text.  Whereas
1060 in C, for instance, data and instructions have different syntactic
1061 status, in Autoconf their status is rigorously the same.  Therefore, we
1062 need a means to distinguish literal strings from text to be expanded:
1063 quotation.
1065 When calling macros that take arguments, there must not be any white
1066 space between the macro name and the open parenthesis.  Arguments should
1067 be enclosed within the M4 quote characters @samp{[} and @samp{]}, and be
1068 separated by commas.  Any leading blanks or newlines in arguments are ignored,
1069 unless they are quoted.  You should always quote an argument that
1070 might contain a macro name, comma, parenthesis, or a leading blank or
1071 newline.  This rule applies recursively for every macro
1072 call, including macros called from other macros.
1074 For instance:
1076 @example
1077 AC_CHECK_HEADER([stdio.h],
1078                 [AC_DEFINE([HAVE_STDIO_H], [1],
1079                    [Define to 1 if you have <stdio.h>.])],
1080                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1081 @end example
1083 @noindent
1084 is quoted properly.  You may safely simplify its quotation to:
1086 @example
1087 AC_CHECK_HEADER([stdio.h],
1088                 [AC_DEFINE([HAVE_STDIO_H], 1,
1089                    [Define to 1 if you have <stdio.h>.])],
1090                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1091 @end example
1093 @noindent
1094 because @samp{1} cannot contain a macro call.  Here, the argument of
1095 @code{AC_MSG_ERROR} must be quoted; otherwise, its comma would be
1096 interpreted as an argument separator.  Also, the second and third arguments
1097 of @samp{AC_CHECK_HEADER} must be quoted, since they contain
1098 macro calls.  The three arguments @samp{HAVE_STDIO_H}, @samp{stdio.h},
1099 and @samp{Define to 1 if you have <stdio.h>.} do not need quoting, but
1100 if you unwisely defined a macro with a name like @samp{Define} or
1101 @samp{stdio} then they would need quoting.  Cautious Autoconf users
1102 would keep the quotes, but many Autoconf users find such precautions
1103 annoying, and would rewrite the example as follows:
1105 @example
1106 AC_CHECK_HEADER(stdio.h,
1107                 [AC_DEFINE(HAVE_STDIO_H, 1,
1108                    [Define to 1 if you have <stdio.h>.])],
1109                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1110 @end example
1112 @noindent
1113 This is safe, so long as you adopt good naming conventions and do not
1114 define macros with names like @samp{HAVE_STDIO_H}, @samp{stdio}, or
1115 @samp{h}.  Though it is also safe here to omit the quotes around
1116 @samp{Define to 1 if you have <stdio.h>.} this is not recommended, as
1117 message strings are more likely to inadvertently contain commas.
1119 The following example is wrong and dangerous, as it is underquoted:
1121 @example
1122 AC_CHECK_HEADER(stdio.h,
1123                 AC_DEFINE(HAVE_STDIO_H, 1,
1124                    Define to 1 if you have <stdio.h>.),
1125                 AC_MSG_ERROR([Sorry, can't do anything for you]))
1126 @end example
1128 In other cases, you may have to use text that also resembles a macro
1129 call.  You must quote that text even when it is not passed as a macro
1130 argument:
1132 @example
1133 echo "Hard rock was here!  --[AC_DC]"
1134 @end example
1136 @noindent
1137 which results in:
1139 @example
1140 echo "Hard rock was here!  --AC_DC"
1141 @end example
1143 @noindent
1144 When you use the same text in a macro argument, you must therefore have
1145 an extra quotation level (since one is stripped away by the macro
1146 substitution).  In general, then, it is a good idea to @emph{use double
1147 quoting for all literal string arguments}:
1149 @example
1150 AC_MSG_WARN([[AC_DC stinks  --Iron Maiden]])
1151 @end example
1153 You are now able to understand one of the constructs of Autoconf that
1154 has been continually misunderstood@dots{}  The rule of thumb is that
1155 @emph{whenever you expect macro expansion, expect quote expansion};
1156 i.e., expect one level of quotes to be lost.  For instance:
1158 @example
1159 AC_COMPILE_IFELSE([char b[10];], [], [AC_MSG_ERROR([you lose])])
1160 @end example
1162 @noindent
1163 is incorrect: here, the first argument of @code{AC_COMPILE_IFELSE} is
1164 @samp{char b[10];} and is expanded once, which results in
1165 @samp{char b10;}.  (There was an idiom common in Autoconf's past to
1166 address this issue via the M4 @code{changequote} primitive, but do not
1167 use it!)  Let's take a closer look: the author meant the first argument
1168 to be understood as a literal, and therefore it must be quoted twice:
1170 @example
1171 AC_COMPILE_IFELSE([[char b[10];]], [], [AC_MSG_ERROR([you lose])])
1172 @end example
1174 @noindent
1175 Voil@`a, you actually produce @samp{char b[10];} this time!
1177 On the other hand, descriptions (e.g., the last parameter of
1178 @code{AC_DEFINE} or @code{AS_HELP_STRING}) are not literals---they
1179 are subject to line breaking, for example---and should not be double quoted.
1180 Even if these descriptions are short and are not actually broken, double
1181 quoting them yields weird results.
1183 Some macros take optional arguments, which this documentation represents
1184 as @ovar{arg} (not to be confused with the quote characters).  You may
1185 just leave them empty, or use @samp{[]} to make the emptiness of the
1186 argument explicit, or you may simply omit the trailing commas.  The
1187 three lines below are equivalent:
1189 @example
1190 AC_CHECK_HEADERS([stdio.h], [], [], [])
1191 AC_CHECK_HEADERS([stdio.h],,,)
1192 AC_CHECK_HEADERS([stdio.h])
1193 @end example
1195 It is best to put each macro call on its own line in
1196 @file{configure.ac}.  Most of the macros don't add extra newlines; they
1197 rely on the newline after the macro call to terminate the commands.
1198 This approach makes the generated @command{configure} script a little
1199 easier to read by not inserting lots of blank lines.  It is generally
1200 safe to set shell variables on the same line as a macro call, because
1201 the shell allows assignments without intervening newlines.
1203 You can include comments in @file{configure.ac} files by starting them
1204 with the @samp{#}.  For example, it is helpful to begin
1205 @file{configure.ac} files with a line like this:
1207 @example
1208 # Process this file with autoconf to produce a configure script.
1209 @end example
1211 @node configure.ac Layout
1212 @subsection Standard @file{configure.ac} Layout
1214 The order in which @file{configure.ac} calls the Autoconf macros is not
1215 important, with a few exceptions.  Every @file{configure.ac} must
1216 contain a call to @code{AC_INIT} before the checks, and a call to
1217 @code{AC_OUTPUT} at the end (@pxref{Output}).  Additionally, some macros
1218 rely on other macros having been called first, because they check
1219 previously set values of some variables to decide what to do.  These
1220 macros are noted in the individual descriptions (@pxref{Existing
1221 Tests}), and they also warn you when @command{configure} is created if they
1222 are called out of order.
1224 To encourage consistency, here is a suggested order for calling the
1225 Autoconf macros.  Generally speaking, the things near the end of this
1226 list are those that could depend on things earlier in it.  For example,
1227 library functions could be affected by types and libraries.
1229 @display
1230 @group
1231 Autoconf requirements
1232 @code{AC_INIT(@var{package}, @var{version}, @var{bug-report-address})}
1233 information on the package
1234 checks for programs
1235 checks for libraries
1236 checks for header files
1237 checks for types
1238 checks for structures
1239 checks for compiler characteristics
1240 checks for library functions
1241 checks for system services
1242 @code{AC_CONFIG_FILES(@r{[}@var{file@dots{}}@r{]})}
1243 @code{AC_OUTPUT}
1244 @end group
1245 @end display
1248 @node autoscan Invocation
1249 @section Using @command{autoscan} to Create @file{configure.ac}
1250 @cindex @command{autoscan}
1252 The @command{autoscan} program can help you create and/or maintain a
1253 @file{configure.ac} file for a software package.  @command{autoscan}
1254 examines source files in the directory tree rooted at a directory given
1255 as a command line argument, or the current directory if none is given.
1256 It searches the source files for common portability problems and creates
1257 a file @file{configure.scan} which is a preliminary @file{configure.ac}
1258 for that package, and checks a possibly existing @file{configure.ac} for
1259 completeness.
1261 When using @command{autoscan} to create a @file{configure.ac}, you
1262 should manually examine @file{configure.scan} before renaming it to
1263 @file{configure.ac}; it probably needs some adjustments.
1264 Occasionally, @command{autoscan} outputs a macro in the wrong order
1265 relative to another macro, so that @command{autoconf} produces a warning;
1266 you need to move such macros manually.  Also, if you want the package to
1267 use a configuration header file, you must add a call to
1268 @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers}).  You might
1269 also have to change or add some @code{#if} directives to your program in
1270 order to make it work with Autoconf (@pxref{ifnames Invocation}, for
1271 information about a program that can help with that job).
1273 When using @command{autoscan} to maintain a @file{configure.ac}, simply
1274 consider adding its suggestions.  The file @file{autoscan.log}
1275 contains detailed information on why a macro is requested.
1277 @command{autoscan} uses several data files (installed along with Autoconf)
1278 to determine which macros to output when it finds particular symbols in
1279 a package's source files.  These data files all have the same format:
1280 each line consists of a symbol, one or more blanks, and the Autoconf macro to
1281 output if that symbol is encountered.  Lines starting with @samp{#} are
1282 comments.
1284 @command{autoscan} accepts the following options:
1286 @table @option
1287 @item --help
1288 @itemx -h
1289 Print a summary of the command line options and exit.
1291 @item --version
1292 @itemx -V
1293 Print the version number of Autoconf and exit.
1295 @item --verbose
1296 @itemx -v
1297 Print the names of the files it examines and the potentially interesting
1298 symbols it finds in them.  This output can be voluminous.
1300 @item --include=@var{dir}
1301 @itemx -I @var{dir}
1302 Append @var{dir} to the include path.  Multiple invocations accumulate.
1304 @item --prepend-include=@var{dir}
1305 @item -B @var{dir}
1306 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1307 @end table
1309 @node ifnames Invocation
1310 @section Using @command{ifnames} to List Conditionals
1311 @cindex @command{ifnames}
1313 @command{ifnames} can help you write @file{configure.ac} for a software
1314 package.  It prints the identifiers that the package already uses in C
1315 preprocessor conditionals.  If a package has already been set up to have
1316 some portability, @command{ifnames} can thus help you figure out what its
1317 @command{configure} needs to check for.  It may help fill in some gaps in a
1318 @file{configure.ac} generated by @command{autoscan} (@pxref{autoscan
1319 Invocation}).
1321 @command{ifnames} scans all of the C source files named on the command line
1322 (or the standard input, if none are given) and writes to the standard
1323 output a sorted list of all the identifiers that appear in those files
1324 in @code{#if}, @code{#elif}, @code{#ifdef}, or @code{#ifndef}
1325 directives.  It prints each identifier on a line, followed by a
1326 space-separated list of the files in which that identifier occurs.
1328 @noindent
1329 @command{ifnames} accepts the following options:
1331 @table @option
1332 @item --help
1333 @itemx -h
1334 Print a summary of the command line options and exit.
1336 @item --version
1337 @itemx -V
1338 Print the version number of Autoconf and exit.
1339 @end table
1341 @node autoconf Invocation
1342 @section Using @command{autoconf} to Create @command{configure}
1343 @cindex @command{autoconf}
1345 To create @command{configure} from @file{configure.ac}, run the
1346 @command{autoconf} program with no arguments.  @command{autoconf} processes
1347 @file{configure.ac} with the M4 macro processor, using the
1348 Autoconf macros.  If you give @command{autoconf} an argument, it reads that
1349 file instead of @file{configure.ac} and writes the configuration script
1350 to the standard output instead of to @command{configure}.  If you give
1351 @command{autoconf} the argument @option{-}, it reads from the standard
1352 input instead of @file{configure.ac} and writes the configuration script
1353 to the standard output.
1355 The Autoconf macros are defined in several files.  Some of the files are
1356 distributed with Autoconf; @command{autoconf} reads them first.  Then it
1357 looks for the optional file @file{acsite.m4} in the directory that
1358 contains the distributed Autoconf macro files, and for the optional file
1359 @file{aclocal.m4} in the current directory.  Those files can contain
1360 your site's or the package's own Autoconf macro definitions
1361 (@pxref{Writing Autoconf Macros}, for more information).  If a macro is
1362 defined in more than one of the files that @command{autoconf} reads, the
1363 last definition it reads overrides the earlier ones.
1365 @command{autoconf} accepts the following options:
1367 @table @option
1368 @item --help
1369 @itemx -h
1370 Print a summary of the command line options and exit.
1372 @item --version
1373 @itemx -V
1374 Print the version number of Autoconf and exit.
1376 @item --verbose
1377 @itemx -v
1378 Report processing steps.
1380 @item --debug
1381 @itemx -d
1382 Don't remove the temporary files.
1384 @item --force
1385 @itemx -f
1386 Remake @file{configure} even if newer than its input files.
1388 @item --include=@var{dir}
1389 @itemx -I @var{dir}
1390 Append @var{dir} to the include path.  Multiple invocations accumulate.
1392 @item --prepend-include=@var{dir}
1393 @item -B @var{dir}
1394 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1396 @item --output=@var{file}
1397 @itemx -o @var{file}
1398 Save output (script or trace) to @var{file}.  The file @option{-} stands
1399 for the standard output.
1401 @item --warnings=@var{category}
1402 @itemx -W @var{category}
1403 @evindex WARNINGS
1404 Report the warnings related to @var{category} (which can actually be a
1405 comma separated list).  @xref{Reporting Messages}, macro
1406 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
1407 values include:
1409 @table @samp
1410 @item all
1411 report all the warnings
1413 @item none
1414 report none
1416 @item error
1417 treats warnings as errors
1419 @item no-@var{category}
1420 disable warnings falling into @var{category}
1421 @end table
1423 Warnings about @samp{syntax} are enabled by default, and the environment
1424 variable @env{WARNINGS}, a comma separated list of categories, is
1425 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1426 you had passed @option{--warnings=syntax,$WARNINGS,@var{category}}.  If
1427 you want to disable the defaults and @env{WARNINGS}, but (for example)
1428 enable the warnings about obsolete constructs, you would use @option{-W
1429 none,obsolete}.
1431 @cindex Back trace
1432 @cindex Macro invocation stack
1433 Because @command{autoconf} uses @command{autom4te} behind the scenes, it
1434 displays a back trace for errors, but not for warnings; if you want
1435 them, just pass @option{-W error}.  @xref{autom4te Invocation}, for some
1436 examples.
1438 @item --trace=@var{macro}[:@var{format}]
1439 @itemx -t @var{macro}[:@var{format}]
1440 Do not create the @command{configure} script, but list the calls to
1441 @var{macro} according to the @var{format}.  Multiple @option{--trace}
1442 arguments can be used to list several macros.  Multiple @option{--trace}
1443 arguments for a single macro are not cumulative; instead, you should
1444 just make @var{format} as long as needed.
1446 The @var{format} is a regular string, with newlines if desired, and
1447 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}; see
1448 @ref{autom4te Invocation}, for details on the @var{format}.
1450 @item --initialization
1451 @itemx -i
1452 By default, @option{--trace} does not trace the initialization of the
1453 Autoconf macros (typically the @code{AC_DEFUN} definitions).  This
1454 results in a noticeable speedup, but can be disabled by this option.
1455 @end table
1458 It is often necessary to check the content of a @file{configure.ac}
1459 file, but parsing it yourself is extremely fragile and error-prone.  It
1460 is suggested that you rely upon @option{--trace} to scan
1461 @file{configure.ac}.  For instance, to find the list of variables that
1462 are substituted, use:
1464 @example
1465 @group
1466 $ @kbd{autoconf -t AC_SUBST}
1467 configure.ac:2:AC_SUBST:ECHO_C
1468 configure.ac:2:AC_SUBST:ECHO_N
1469 configure.ac:2:AC_SUBST:ECHO_T
1470 @i{More traces deleted}
1471 @end group
1472 @end example
1474 @noindent
1475 The example below highlights the difference between @samp{$@@},
1476 @samp{$*}, and @samp{$%}.
1478 @example
1479 @group
1480 $ @kbd{cat configure.ac}
1481 AC_DEFINE(This, is, [an
1482 [example]])
1483 $ @kbd{autoconf -t 'AC_DEFINE:@@: $@@}
1484 *: $*
1485 %: $%'
1486 @@: [This],[is],[an
1487 [example]]
1488 *: This,is,an
1489 [example]
1490 %: This:is:an [example]
1491 @end group
1492 @end example
1494 @noindent
1495 The @var{format} gives you a lot of freedom:
1497 @example
1498 @group
1499 $ @kbd{autoconf -t 'AC_SUBST:$$ac_subst@{"$1"@} = "$f:$l";'}
1500 $ac_subst@{"ECHO_C"@} = "configure.ac:2";
1501 $ac_subst@{"ECHO_N"@} = "configure.ac:2";
1502 $ac_subst@{"ECHO_T"@} = "configure.ac:2";
1503 @i{More traces deleted}
1504 @end group
1505 @end example
1507 @noindent
1508 A long @var{separator} can be used to improve the readability of complex
1509 structures, and to ease their parsing (for instance when no single
1510 character is suitable as a separator):
1512 @example
1513 @group
1514 $ @kbd{autoconf -t 'AM_MISSING_PROG:$@{|:::::|@}*'}
1515 ACLOCAL|:::::|aclocal|:::::|$missing_dir
1516 AUTOCONF|:::::|autoconf|:::::|$missing_dir
1517 AUTOMAKE|:::::|automake|:::::|$missing_dir
1518 @i{More traces deleted}
1519 @end group
1520 @end example
1522 @node autoreconf Invocation
1523 @section Using @command{autoreconf} to Update @command{configure} Scripts
1524 @cindex @command{autoreconf}
1526 Installing the various components of the @acronym{GNU} Build System can be
1527 tedious: running @command{autopoint} for Gettext, @command{automake} for
1528 @file{Makefile.in} etc.@: in each directory.  It may be needed either
1529 because some tools such as @command{automake} have been updated on your
1530 system, or because some of the sources such as @file{configure.ac} have
1531 been updated, or finally, simply in order to install the @acronym{GNU} Build
1532 System in a fresh tree.
1534 @command{autoreconf} runs @command{autoconf}, @command{autoheader},
1535 @command{aclocal}, @command{automake}, @command{libtoolize}, and
1536 @command{autopoint} (when appropriate) repeatedly to update the
1537 @acronym{GNU} Build System in the specified directories and their
1538 subdirectories (@pxref{Subdirectories}).  By default, it only remakes
1539 those files that are older than their sources.
1541 If you install a new version of some tool, you can make
1542 @command{autoreconf} remake @emph{all} of the files by giving it the
1543 @option{--force} option.
1545 @xref{Automatic Remaking}, for Make rules to automatically
1546 remake @command{configure} scripts when their source files change.  That
1547 method handles the timestamps of configuration header templates
1548 properly, but does not pass @option{--autoconf-dir=@var{dir}} or
1549 @option{--localdir=@var{dir}}.
1551 @cindex Gettext
1552 @cindex @command{autopoint}
1553 Gettext supplies the @command{autopoint} command to add translation
1554 infrastructure to a source package.  If you use @command{autopoint},
1555 your @file{configure.ac} should invoke both @code{AM_GNU_GETTEXT} and
1556 @code{AM_GNU_GETTEXT_VERSION(@var{gettext-version})}.  @xref{autopoint
1557 Invocation, , Invoking the @code{autopoint} Program, gettext,
1558 @acronym{GNU} @code{gettext} utilities}, for further details.
1560 @noindent
1561 @command{autoreconf} accepts the following options:
1563 @table @option
1564 @item --help
1565 @itemx -h
1566 Print a summary of the command line options and exit.
1568 @item --version
1569 @itemx -V
1570 Print the version number of Autoconf and exit.
1572 @item --verbose
1573 Print the name of each directory @command{autoreconf} examines and the
1574 commands it runs.  If given two or more times, pass @option{--verbose}
1575 to subordinate tools that support it.
1577 @item --debug
1578 @itemx -d
1579 Don't remove the temporary files.
1581 @item --force
1582 @itemx -f
1583 Remake even @file{configure} scripts and configuration headers that are
1584 newer than their input files (@file{configure.ac} and, if present,
1585 @file{aclocal.m4}).
1587 @item --install
1588 @itemx -i
1589 Install the missing auxiliary files in the package.  By default, files
1590 are copied; this can be changed with @option{--symlink}.
1592 If deemed appropriate, this option triggers calls to
1593 @samp{automake --add-missing},
1594 @samp{libtoolize}, @samp{autopoint}, etc.
1596 @item --no-recursive
1597 Do not rebuild files in subdirectories to configure (see @ref{Subdirectories},
1598 macro @code{AC_CONFIG_SUBDIRS}).
1600 @item --symlink
1601 @itemx -s
1602 When used with @option{--install}, install symbolic links to the missing
1603 auxiliary files instead of copying them.
1605 @item --make
1606 @itemx -m
1607 When the directories were configured, update the configuration by
1608 running @samp{./config.status --recheck && ./config.status}, and then
1609 run @samp{make}.
1611 @item --include=@var{dir}
1612 @itemx -I @var{dir}
1613 Append @var{dir} to the include path.  Multiple invocations accumulate.
1614 Passed on to @command{autoconf} and @command{autoheader} internally.
1616 @item --prepend-include=@var{dir}
1617 @item -B @var{dir}
1618 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1619 Passed on to @command{autoconf} and @command{autoheader} internally.
1621 @item --warnings=@var{category}
1622 @itemx -W @var{category}
1623 @evindex WARNINGS
1624 Report the warnings related to @var{category} (which can actually be a
1625 comma separated list).
1627 @table @samp
1628 @item cross
1629 related to cross compilation issues.
1631 @item obsolete
1632 report the uses of obsolete constructs.
1634 @item portability
1635 portability issues
1637 @item syntax
1638 dubious syntactic constructs.
1640 @item all
1641 report all the warnings
1643 @item none
1644 report none
1646 @item error
1647 treats warnings as errors
1649 @item no-@var{category}
1650 disable warnings falling into @var{category}
1651 @end table
1653 Warnings about @samp{syntax} are enabled by default, and the environment
1654 variable @env{WARNINGS}, a comma separated list of categories, is
1655 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1656 you had passed @option{--warnings=syntax,$WARNINGS,@var{category}}.  If
1657 you want to disable the defaults and @env{WARNINGS}, but (for example)
1658 enable the warnings about obsolete constructs, you would use @option{-W
1659 none,obsolete}.
1660 @end table
1662 If you want @command{autoreconf} to pass flags that are not listed here
1663 on to @command{aclocal}, set @code{ACLOCAL_AMFLAGS} in your @file{Makefile.am}.
1665 @c ========================================= Initialization and Output Files.
1667 @node Setup
1668 @chapter Initialization and Output Files
1670 Autoconf-generated @command{configure} scripts need some information about
1671 how to initialize, such as how to find the package's source files and
1672 about the output files to produce.  The following sections describe the
1673 initialization and the creation of output files.
1675 @menu
1676 * Initializing configure::      Option processing etc.
1677 * Notices::                     Copyright, version numbers in @command{configure}
1678 * Input::                       Where Autoconf should find files
1679 * Output::                      Outputting results from the configuration
1680 * Configuration Actions::       Preparing the output based on results
1681 * Configuration Files::         Creating output files
1682 * Makefile Substitutions::      Using output variables in makefiles
1683 * Configuration Headers::       Creating a configuration header file
1684 * Configuration Commands::      Running arbitrary instantiation commands
1685 * Configuration Links::         Links depending on the configuration
1686 * Subdirectories::              Configuring independent packages together
1687 * Default Prefix::              Changing the default installation prefix
1688 @end menu
1690 @node Initializing configure
1691 @section Initializing @command{configure}
1693 Every @command{configure} script must call @code{AC_INIT} before doing
1694 anything else.  The only other required macro is @code{AC_OUTPUT}
1695 (@pxref{Output}).
1697 @defmac AC_INIT (@var{package}, @var{version}, @ovar{bug-report}, @ovar{tarname})
1698 @acindex{INIT}
1699 Process any command-line arguments and perform various initializations
1700 and verifications.
1702 Set the name of the @var{package} and its @var{version}.  These are
1703 typically used in @option{--version} support, including that of
1704 @command{configure}.  The optional argument @var{bug-report} should be
1705 the email to which users should send bug reports.  The package
1706 @var{tarname} differs from @var{package}: the latter designates the full
1707 package name (e.g., @samp{GNU Autoconf}), while the former is meant for
1708 distribution tar ball names (e.g., @samp{autoconf}).  It defaults to
1709 @var{package} with @samp{GNU } stripped, lower-cased, and all characters
1710 other than alphanumerics and underscores are changed to @samp{-}.
1712 It is preferable that the arguments of @code{AC_INIT} be static, i.e.,
1713 there should not be any shell computation, but they can be computed by
1716 The following M4 macros (e.g., @code{AC_PACKAGE_NAME}), output variables
1717 (e.g., @code{PACKAGE_NAME}), and preprocessor symbols (e.g.,
1718 @code{PACKAGE_NAME}) are defined by @code{AC_INIT}:
1720 @table @asis
1721 @item @code{AC_PACKAGE_NAME}, @code{PACKAGE_NAME}
1722 @acindex{PACKAGE_NAME}
1723 @ovindex PACKAGE_NAME
1724 @cvindex PACKAGE_NAME
1725 Exactly @var{package}.
1727 @item @code{AC_PACKAGE_TARNAME}, @code{PACKAGE_TARNAME}
1728 @acindex{PACKAGE_TARNAME}
1729 @ovindex PACKAGE_TARNAME
1730 @cvindex PACKAGE_TARNAME
1731 Exactly @var{tarname}.
1733 @item @code{AC_PACKAGE_VERSION}, @code{PACKAGE_VERSION}
1734 @acindex{PACKAGE_VERSION}
1735 @ovindex PACKAGE_VERSION
1736 @cvindex PACKAGE_VERSION
1737 Exactly @var{version}.
1739 @item @code{AC_PACKAGE_STRING}, @code{PACKAGE_STRING}
1740 @acindex{PACKAGE_STRING}
1741 @ovindex PACKAGE_STRING
1742 @cvindex PACKAGE_STRING
1743 Exactly @samp{@var{package} @var{version}}.
1745 @item @code{AC_PACKAGE_BUGREPORT}, @code{PACKAGE_BUGREPORT}
1746 @acindex{PACKAGE_BUGREPORT}
1747 @ovindex PACKAGE_BUGREPORT
1748 @cvindex PACKAGE_BUGREPORT
1749 Exactly @var{bug-report}.
1750 @end table
1751 @end defmac
1753 If your @command{configure} script does its own option processing, it
1754 should inspect @samp{$@@} or @samp{$*} immediately after calling
1755 @code{AC_INIT}, because other Autoconf macros liberally use the
1756 @command{set} command to process strings, and this has the side effect
1757 of updating @samp{$@@} and @samp{$*}.  However, we suggest that you use
1758 standard macros like @code{AC_ARG_ENABLE} instead of attempting to
1759 implement your own option processing.  @xref{Site Configuration}.
1762 @node Notices
1763 @section Notices in @command{configure}
1764 @cindex Notices in @command{configure}
1766 The following macros manage version numbers for @command{configure}
1767 scripts.  Using them is optional.
1769 @c FIXME: AC_PREREQ should not be here
1770 @defmac AC_PREREQ (@var{version})
1771 @acindex{PREREQ}
1772 @cindex Version
1773 Ensure that a recent enough version of Autoconf is being used.  If the
1774 version of Autoconf being used to create @command{configure} is
1775 earlier than @var{version}, print an error message to the standard
1776 error output and exit with failure (exit status is 63).  For example:
1778 @example
1779 AC_PREREQ([@value{VERSION}])
1780 @end example
1782 This macro is the only macro that may be used before @code{AC_INIT}, but
1783 for consistency, you are invited not to do so.
1784 @end defmac
1786 @defmac AC_COPYRIGHT (@var{copyright-notice})
1787 @acindex{COPYRIGHT}
1788 @cindex Copyright Notice
1789 State that, in addition to the Free Software Foundation's copyright on
1790 the Autoconf macros, parts of your @command{configure} are covered by the
1791 @var{copyright-notice}.
1793 The @var{copyright-notice} shows up in both the head of
1794 @command{configure} and in @samp{configure --version}.
1795 @end defmac
1798 @defmac AC_REVISION (@var{revision-info})
1799 @acindex{REVISION}
1800 @cindex Revision
1801 Copy revision stamp @var{revision-info} into the @command{configure}
1802 script, with any dollar signs or double-quotes removed.  This macro lets
1803 you put a revision stamp from @file{configure.ac} into @command{configure}
1804 without @acronym{RCS} or @acronym{CVS} changing it when you check in
1805 @command{configure}.  That way, you can determine easily which revision of
1806 @file{configure.ac} a particular @command{configure} corresponds to.
1808 For example, this line in @file{configure.ac}:
1810 @c The asis prevents RCS from changing the example in the manual.
1811 @example
1812 AC_REVISION([$@asis{Revision: 1.30 }$])
1813 @end example
1815 @noindent
1816 produces this in @command{configure}:
1818 @example
1819 #!/bin/sh
1820 # From configure.ac Revision: 1.30
1821 @end example
1822 @end defmac
1825 @node Input
1826 @section Finding @command{configure} Input
1829 @defmac AC_CONFIG_SRCDIR (@var{unique-file-in-source-dir})
1830 @acindex{CONFIG_SRCDIR}
1831 @var{unique-file-in-source-dir} is some file that is in the package's
1832 source directory; @command{configure} checks for this file's existence to
1833 make sure that the directory that it is told contains the source code in
1834 fact does.  Occasionally people accidentally specify the wrong directory
1835 with @option{--srcdir}; this is a safety check.  @xref{configure
1836 Invocation}, for more information.
1837 @end defmac
1840 @c FIXME: Remove definitively once --install explained.
1842 @c Small packages may store all their macros in @code{aclocal.m4}.  As the
1843 @c set of macros grows, or for maintenance reasons, a maintainer may prefer
1844 @c to split the macros in several files.  In this case, Autoconf must be
1845 @c told which files to load, and in which order.
1847 @c @defmac AC_INCLUDE (@var{file}@dots{})
1848 @c @acindex{INCLUDE}
1849 @c @c FIXME: There is no longer shell globbing.
1850 @c Read the macro definitions that appear in the listed files.  A list of
1851 @c space-separated file names or shell globbing patterns is expected.  The
1852 @c files are read in the order they're listed.
1854 @c Because the order of definition of macros is important (only the last
1855 @c definition of a macro is used), beware that it is @code{AC_INIT} that
1856 @c loads @file{acsite.m4} and @file{aclocal.m4}.  Note that
1857 @c @code{AC_INCLUDE}ing a file before @code{AC_INIT} or within
1858 @c @file{aclocal.m4} is different from doing so after @code{AC_INIT}: in
1859 @c the latter case, non-macro lines from included files may end up in the
1860 @c @file{configure} script, whereas in the former case, they'd be discarded
1861 @c just like any text that appear before @code{AC_INIT}.
1862 @c @end defmac
1864 Packages that do manual configuration or use the @command{install} program
1865 might need to tell @command{configure} where to find some other shell
1866 scripts by calling @code{AC_CONFIG_AUX_DIR}, though the default places
1867 it looks are correct for most cases.
1869 @defmac AC_CONFIG_AUX_DIR (@var{dir})
1870 @acindex{CONFIG_AUX_DIR}
1871 Use the auxiliary build tools (e.g., @file{install-sh},
1872 @file{config.sub}, @file{config.guess}, Cygnus @command{configure},
1873 Automake and Libtool scripts, etc.)@: that are in directory @var{dir}.
1874 These are auxiliary files used in configuration.  @var{dir} can be
1875 either absolute or relative to @file{@var{srcdir}}.  The default is
1876 @file{@var{srcdir}} or @file{@var{srcdir}/..} or
1877 @file{@var{srcdir}/../..}, whichever is the first that contains
1878 @file{install-sh}.  The other files are not checked for, so that using
1879 @code{AC_PROG_INSTALL} does not automatically require distributing the
1880 other auxiliary files.  It checks for @file{install.sh} also, but that
1881 name is obsolete because some @code{make} have a rule that creates
1882 @file{install} from it if there is no makefile.
1884 The auxiliary directory is commonly named @file{build-aux}.
1885 If you need portability to @acronym{DOS} variants, do not name the
1886 auxiliary directory @file{aux}.  @xref{File System Conventions}.
1887 @end defmac
1889 @defmac AC_REQUIRE_AUX_FILE (@var{file})
1890 @acindex{REQUIRE_AUX_FILE}
1891 Declares that @var{file} is expected in the directory defined above.  In
1892 Autoconf proper, this macro does nothing: its sole purpose is to be
1893 traced by third-party tools to produce a list of expected auxiliary
1894 files.  For instance it is called by macros like @code{AC_PROG_INSTALL}
1895 (@pxref{Particular Programs}) or @code{AC_CANONICAL_BUILD}
1896 (@pxref{Canonicalizing}) to register the auxiliary files they need.
1897 @end defmac
1899 Similarly, packages that use @command{aclocal} should declare where
1900 local macros can be found using @code{AC_CONFIG_MACRO_DIR}.
1902 @defmac AC_CONFIG_MACRO_DIR (@var{dir})
1903 @acindex{CONFIG_MACRO_DIR}
1904 Future versions of @command{autopoint}, @command{libtoolize},
1905 @command{aclocal} and @command{autoreconf} will use directory
1906 @var{dir} as the location of additional local Autoconf macros.  Be
1907 sure to call this macro directly from @file{configure.ac} so that
1908 tools that install macros for @command{aclocal} can find the
1909 declaration before @option{--trace} can be called safely.
1910 @end defmac
1913 @node Output
1914 @section Outputting Files
1915 @cindex Outputting files
1917 Every Autoconf script, e.g., @file{configure.ac}, should finish by
1918 calling @code{AC_OUTPUT}.  That is the macro that generates and runs
1919 @file{config.status}, which in turn creates the makefiles and any
1920 other files resulting from configuration.  This is the only required
1921 macro besides @code{AC_INIT} (@pxref{Input}).
1923 @defmac AC_OUTPUT
1924 @acindex{OUTPUT}
1925 @cindex Instantiation
1926 Generate @file{config.status} and launch it.  Call this macro once, at
1927 the end of @file{configure.ac}.
1929 @file{config.status} performs all the configuration actions: all the
1930 output files (see @ref{Configuration Files}, macro
1931 @code{AC_CONFIG_FILES}), header files (see @ref{Configuration Headers},
1932 macro @code{AC_CONFIG_HEADERS}), commands (see @ref{Configuration
1933 Commands}, macro @code{AC_CONFIG_COMMANDS}), links (see
1934 @ref{Configuration Links}, macro @code{AC_CONFIG_LINKS}), subdirectories
1935 to configure (see @ref{Subdirectories}, macro @code{AC_CONFIG_SUBDIRS})
1936 are honored.
1938 The location of your @code{AC_OUTPUT} invocation is the exact point
1939 where configuration actions are taken: any code afterwards is
1940 executed by @code{configure} once @command{config.status} was run.  If
1941 you want to bind actions to @command{config.status} itself
1942 (independently of whether @command{configure} is being run), see
1943 @ref{Configuration Commands, , Running Arbitrary Configuration
1944 Commands}.
1945 @end defmac
1947 Historically, the usage of @code{AC_OUTPUT} was somewhat different.
1948 @xref{Obsolete Macros}, for a description of the arguments that
1949 @code{AC_OUTPUT} used to support.
1952 If you run @command{make} in subdirectories, you should run it using the
1953 @code{make} variable @code{MAKE}.  Most versions of @command{make} set
1954 @code{MAKE} to the name of the @command{make} program plus any options it
1955 was given.  (But many do not include in it the values of any variables
1956 set on the command line, so those are not passed on automatically.)
1957 Some old versions of @command{make} do not set this variable.  The
1958 following macro allows you to use it even with those versions.
1960 @defmac AC_PROG_MAKE_SET
1961 @acindex{PROG_MAKE_SET}
1962 @ovindex SET_MAKE
1963 If the Make command, @code{$MAKE} if set or else @samp{make}, predefines
1964 @code{$(MAKE)}, define output variable @code{SET_MAKE} to be empty.
1965 Otherwise, define @code{SET_MAKE} to a macro definition that sets
1966 @code{$(MAKE)}, such as @samp{MAKE=make}.  Calls @code{AC_SUBST} for
1967 @code{SET_MAKE}.
1968 @end defmac
1970 If you use this macro, place a line like this in each @file{Makefile.in}
1971 that runs @code{MAKE} on other directories:
1973 @example
1974 @@SET_MAKE@@
1975 @end example
1979 @node Configuration Actions
1980 @section Performing Configuration Actions
1981 @cindex Configuration actions
1983 @file{configure} is designed so that it appears to do everything itself,
1984 but there is actually a hidden slave: @file{config.status}.
1985 @file{configure} is in charge of examining your system, but it is
1986 @file{config.status} that actually takes the proper actions based on the
1987 results of @file{configure}.  The most typical task of
1988 @file{config.status} is to @emph{instantiate} files.
1990 This section describes the common behavior of the four standard
1991 instantiating macros: @code{AC_CONFIG_FILES}, @code{AC_CONFIG_HEADERS},
1992 @code{AC_CONFIG_COMMANDS} and @code{AC_CONFIG_LINKS}.  They all
1993 have this prototype:
1995 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
1996 @c awful.
1997 @example
1998 AC_CONFIG_FOOS(@var{tag}@dots{}, [@var{commands}], [@var{init-cmds}])
1999 @end example
2001 @noindent
2002 where the arguments are:
2004 @table @var
2005 @item tag@dots{}
2006 A blank-or-newline-separated list of tags, which are typically the names of
2007 the files to instantiate.
2009 You are encouraged to use literals as @var{tags}.  In particular, you
2010 should avoid
2012 @example
2013 @dots{} && my_foos="$my_foos fooo"
2014 @dots{} && my_foos="$my_foos foooo"
2015 AC_CONFIG_FOOS([$my_foos])
2016 @end example
2018 @noindent
2019 and use this instead:
2021 @example
2022 @dots{} && AC_CONFIG_FOOS([fooo])
2023 @dots{} && AC_CONFIG_FOOS([foooo])
2024 @end example
2026 The macros @code{AC_CONFIG_FILES} and @code{AC_CONFIG_HEADERS} use
2027 special @var{tag} values: they may have the form @samp{@var{output}} or
2028 @samp{@var{output}:@var{inputs}}.  The file @var{output} is instantiated
2029 from its templates, @var{inputs} (defaulting to @samp{@var{output}.in}).
2031 @samp{AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk)]},
2032 for example, asks for
2033 the creation of the file @file{Makefile} that contains the expansion of the
2034 output variables in the concatenation of @file{boiler/top.mk} and
2035 @file{boiler/bot.mk}.
2037 The special value @samp{-} might be used to denote the standard output
2038 when used in @var{output}, or the standard input when used in the
2039 @var{inputs}.  You most probably don't need to use this in
2040 @file{configure.ac}, but it is convenient when using the command line
2041 interface of @file{./config.status}, see @ref{config.status Invocation},
2042 for more details.
2044 The @var{inputs} may be absolute or relative file names.  In the latter
2045 case they are first looked for in the build tree, and then in the source
2046 tree.
2048 @item commands
2049 Shell commands output literally into @file{config.status}, and
2050 associated with a tag that the user can use to tell @file{config.status}
2051 which the commands to run.  The commands are run each time a @var{tag}
2052 request is given to @file{config.status}, typically each time the file
2053 @file{@var{tag}} is created.
2055 The variables set during the execution of @command{configure} are
2056 @emph{not} available here: you first need to set them via the
2057 @var{init-cmds}.  Nonetheless the following variables are precomputed:
2059 @table @code
2060 @item srcdir
2061 The name of the top source directory, assuming that the working
2062 directory is the top build directory.  This
2063 is what the @command{configure} option @option{--srcdir} sets.
2065 @item ac_top_srcdir
2066 The name of the top source directory, assuming that the working
2067 directory is the current build directory.
2070 @item ac_top_build_prefix
2071 The name of the top build directory, assuming that the working
2072 directory is the current build directory.
2073 It can be empty, or else ends with a slash, so that you may concatenate
2076 @item ac_srcdir
2077 The name of the corresponding source directory, assuming that the
2078 working directory is the current build directory.
2079 @end table
2081 @noindent
2082 The @dfn{current} directory refers to the directory (or
2083 pseudo-directory) containing the input part of @var{tags}.  For
2084 instance, running
2086 @example
2087 AC_CONFIG_COMMANDS([deep/dir/out:in/in.in], [@dots{}], [@dots{}])
2088 @end example
2090 @noindent
2091  with @option{--srcdir=../package} produces the following values:
2093 @example
2094 # Argument of --srcdir
2095 srcdir='../package'
2096 # Reversing deep/dir
2097 ac_top_build_prefix='../../'
2098 # Concatenation of $ac_top_build_prefix and srcdir
2099 ac_top_srcdir='../../../package'
2100 # Concatenation of $ac_top_srcdir and deep/dir
2101 ac_srcdir='../../../package/deep/dir'
2102 @end example
2104 @noindent
2105 independently of @samp{in/in.in}.
2107 @item init-cmds
2108 Shell commands output @emph{unquoted} near the beginning of
2109 @file{config.status}, and executed each time @file{config.status} runs
2110 (regardless of the tag).  Because they are unquoted, for example,
2111 @samp{$var} is output as the value of @code{var}.  @var{init-cmds}
2112 is typically used by @file{configure} to give @file{config.status} some
2113 variables it needs to run the @var{commands}.
2115 You should be extremely cautious in your variable names: all the
2116 @var{init-cmds} share the same name space and may overwrite each other
2117 in unpredictable ways.  Sorry@enddots{}
2118 @end table
2120 All these macros can be called multiple times, with different
2121 @var{tag} values, of course!
2124 @node Configuration Files
2125 @section Creating Configuration Files
2126 @cindex Creating configuration files
2127 @cindex Configuration file creation
2129 Be sure to read the previous section, @ref{Configuration Actions}.
2131 @defmac AC_CONFIG_FILES (@var{file}@dots{}, @ovar{cmds}, @ovar{init-cmds})
2132 @acindex{CONFIG_FILES}
2133 Make @code{AC_OUTPUT} create each @file{@var{file}} by copying an input
2134 file (by default @file{@var{file}.in}), substituting the output variable
2135 values.
2136 @c Before we used to have this feature, which was later rejected
2137 @c because it complicates the writing of makefiles:
2138 @c If the file would be unchanged, it is left untouched, to preserve
2139 @c timestamp.
2140 This macro is one of the instantiating macros; see @ref{Configuration
2141 Actions}.  @xref{Makefile Substitutions}, for more information on using
2142 output variables.  @xref{Setting Output Variables}, for more information
2143 on creating them.  This macro creates the directory that the file is in
2144 if it doesn't exist.  Usually, makefiles are created this way,
2145 but other files, such as @file{.gdbinit}, can be specified as well.
2147 Typical calls to @code{AC_CONFIG_FILES} look like this:
2149 @example
2150 AC_CONFIG_FILES([Makefile src/Makefile man/Makefile X/Imakefile])
2151 AC_CONFIG_FILES([autoconf], [chmod +x autoconf])
2152 @end example
2154 You can override an input file name by appending to @var{file} a
2155 colon-separated list of input files.  Examples:
2157 @example
2158 AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk]
2159                 [lib/Makefile:boiler/lib.mk])
2160 @end example
2162 @noindent
2163 Doing this allows you to keep your file names acceptable to
2164 @acronym{DOS} variants, or
2165 to prepend and/or append boilerplate to the file.
2166 @end defmac
2170 @node Makefile Substitutions
2171 @section Substitutions in Makefiles
2172 @cindex Substitutions in makefiles
2173 @cindex Makefile substitutions
2175 Each subdirectory in a distribution that contains something to be
2176 compiled or installed should come with a file @file{Makefile.in}, from
2177 which @command{configure} creates a file @file{Makefile} in that directory.
2178 To create @file{Makefile}, @command{configure} performs a simple variable
2179 substitution, replacing occurrences of @samp{@@@var{variable}@@} in
2180 @file{Makefile.in} with the value that @command{configure} has determined
2181 for that variable.  Variables that are substituted into output files in
2182 this way are called @dfn{output variables}.  They are ordinary shell
2183 variables that are set in @command{configure}.  To make @command{configure}
2184 substitute a particular variable into the output files, the macro
2185 @code{AC_SUBST} must be called with that variable name as an argument.
2186 Any occurrences of @samp{@@@var{variable}@@} for other variables are
2187 left unchanged.  @xref{Setting Output Variables}, for more information
2188 on creating output variables with @code{AC_SUBST}.
2190 A software package that uses a @command{configure} script should be
2191 distributed with a file @file{Makefile.in}, but no makefile; that
2192 way, the user has to properly configure the package for the local system
2193 before compiling it.
2195 @xref{Makefile Conventions, , Makefile Conventions, standards, The
2196 @acronym{GNU} Coding Standards}, for more information on what to put in
2197 makefiles.
2199 @menu
2200 * Preset Output Variables::     Output variables that are always set
2201 * Installation Directory Variables::  Other preset output variables
2202 * Changed Directory Variables:: Warnings about @file{datarootdir}
2203 * Build Directories::           Supporting multiple concurrent compiles
2204 * Automatic Remaking::          Makefile rules for configuring
2205 @end menu
2207 @node Preset Output Variables
2208 @subsection Preset Output Variables
2209 @cindex Output variables
2211 Some output variables are preset by the Autoconf macros.  Some of the
2212 Autoconf macros set additional output variables, which are mentioned in
2213 the descriptions for those macros.  @xref{Output Variable Index}, for a
2214 complete list of output variables.  @xref{Installation Directory
2215 Variables}, for the list of the preset ones related to installation
2216 directories.  Below are listed the other preset ones.  They all are
2217 precious variables (@pxref{Setting Output Variables},
2218 @code{AC_ARG_VAR}).
2220 @c Just say no to ASCII sorting!  We're humans, not computers.
2221 @c These variables are listed as they would be in a dictionary:
2222 @c actor
2223 @c Actress
2224 @c actress
2226 @defvar CFLAGS
2227 @ovindex CFLAGS
2228 Debugging and optimization options for the C compiler.  If it is not set
2229 in the environment when @command{configure} runs, the default value is set
2230 when you call @code{AC_PROG_CC} (or empty if you don't).  @command{configure}
2231 uses this variable when compiling programs to test for C features.
2232 @end defvar
2234 @defvar configure_input
2235 @ovindex configure_input
2236 A comment saying that the file was generated automatically by
2237 @command{configure} and giving the name of the input file.
2238 @code{AC_OUTPUT} adds a comment line containing this variable to the top
2239 of every makefile it creates.  For other files, you should
2240 reference this variable in a comment at the top of each input file.  For
2241 example, an input shell script should begin like this:
2243 @example
2244 #!/bin/sh
2245 # @@configure_input@@
2246 @end example
2248 @noindent
2249 The presence of that line also reminds people editing the file that it
2250 needs to be processed by @command{configure} in order to be used.
2251 @end defvar
2253 @defvar CPPFLAGS
2254 @ovindex CPPFLAGS
2255 Header file search directory (@option{-I@var{dir}}) and any other
2256 miscellaneous options for the C and C++ preprocessors and compilers.  If
2257 it is not set in the environment when @command{configure} runs, the default
2258 value is empty.  @command{configure} uses this variable when compiling or
2259 preprocessing programs to test for C and C++ features.
2260 @xref{Special Chars in Variables}, for limitations that @code{CPPFLAGS}
2261 might run into.
2262 @end defvar
2264 @defvar CXXFLAGS
2265 @ovindex CXXFLAGS
2266 Debugging and optimization options for the C++ compiler.  If it is not
2267 set in the environment when @command{configure} runs, the default value is
2268 set when you call @code{AC_PROG_CXX} (or empty if you don't).
2269 @command{configure} uses this variable when compiling programs to test for
2270 C++ features.
2271 @end defvar
2273 @defvar DEFS
2274 @ovindex DEFS
2275 @option{-D} options to pass to the C compiler.  If @code{AC_CONFIG_HEADERS}
2276 is called, @command{configure} replaces @samp{@@DEFS@@} with
2277 @option{-DHAVE_CONFIG_H} instead (@pxref{Configuration Headers}).  This
2278 variable is not defined while @command{configure} is performing its tests,
2279 only when creating the output files.  @xref{Setting Output Variables}, for
2280 how to check the results of previous tests.
2281 @end defvar
2283 @defvar ECHO_C
2284 @defvarx ECHO_N
2285 @defvarx ECHO_T
2286 @ovindex ECHO_C
2287 @ovindex ECHO_N
2288 @ovindex ECHO_T
2289 How does one suppress the trailing newline from @command{echo} for
2290 question-answer message pairs?  These variables provide a way:
2292 @example
2293 echo $ECHO_N "And the winner is... $ECHO_C"
2294 sleep 100000000000
2295 echo "$@{ECHO_T@}dead."
2296 @end example
2298 @noindent
2299 Some old and uncommon @command{echo} implementations offer no means to
2300 achieve this, in which case @code{ECHO_T} is set to tab.  You might not
2301 want to use it.
2302 @end defvar
2304 @defvar ERLCFLAGS
2305 @ovindex ERLCFLAGS
2306 Debugging and optimization options for the Erlang compiler.  If it is not set
2307 in the environment when @command{configure} runs, the default value is empty.
2308 @command{configure} uses this variable when compiling programs to test for
2309 Erlang features.
2310 @end defvar
2312 @defvar FCFLAGS
2313 @ovindex FCFLAGS
2314 Debugging and optimization options for the Fortran compiler.  If it
2315 is not set in the environment when @command{configure} runs, the default
2316 value is set when you call @code{AC_PROG_FC} (or empty if you don't).
2317 @command{configure} uses this variable when compiling programs to test for
2318 Fortran features.
2319 @end defvar
2321 @defvar FFLAGS
2322 @ovindex FFLAGS
2323 Debugging and optimization options for the Fortran 77 compiler.  If it
2324 is not set in the environment when @command{configure} runs, the default
2325 value is set when you call @code{AC_PROG_F77} (or empty if you don't).
2326 @command{configure} uses this variable when compiling programs to test for
2327 Fortran 77 features.
2328 @end defvar
2330 @defvar LDFLAGS
2331 @ovindex LDFLAGS
2332 Stripping (@option{-s}), path (@option{-L}), and any other miscellaneous
2333 options for the linker.  Don't use this variable to pass library names
2334 (@option{-l}) to the linker, use @code{LIBS} instead.  If it is not set
2335 in the environment when @command{configure} runs, the default value is empty.
2336 @command{configure} uses this variable when linking programs to test for
2337 C, C++, and Fortran features.
2338 @end defvar
2340 @defvar LIBS
2341 @ovindex LIBS
2342 @option{-l} options to pass to the linker.  The default value is empty,
2343 but some Autoconf macros may prepend extra libraries to this variable if
2344 those libraries are found and provide necessary functions, see
2345 @ref{Libraries}.  @command{configure} uses this variable when linking
2346 programs to test for C, C++, and Fortran features.
2347 @end defvar
2349 @defvar OBJCFLAGS
2350 @ovindex OBJCFLAGS
2351 Debugging and optimization options for the Objective C compiler.  If it is
2352 not set in the environment when @command{configure} runs, the default value
2353 is set when you call @code{AC_PROG_OBJC} (or empty if you don't).
2354 @command{configure} uses this variable when compiling programs to test for
2355 Objective C features.
2356 @end defvar
2358 @defvar builddir
2359 @ovindex builddir
2360 Rigorously equal to @samp{.}.  Added for symmetry only.
2361 @end defvar
2363 @defvar abs_builddir
2364 @ovindex abs_builddir
2365 Absolute name of @code{builddir}.
2366 @end defvar
2368 @defvar top_builddir
2369 @ovindex top_builddir
2370 The relative name of the top level of the current build tree.  In the
2371 top-level directory, this is the same as @code{builddir}.
2372 @end defvar
2374 @defvar abs_top_builddir
2375 @ovindex abs_top_builddir
2376 Absolute name of @code{top_builddir}.
2377 @end defvar
2379 @defvar srcdir
2380 @ovindex srcdir
2381 The relative name of the directory that contains the source code for
2382 that makefile.
2383 @end defvar
2385 @defvar abs_srcdir
2386 @ovindex abs_srcdir
2387 Absolute name of @code{srcdir}.
2388 @end defvar
2390 @defvar top_srcdir
2391 @ovindex top_srcdir
2392 The relative name of the top-level source code directory for the
2393 package.  In the top-level directory, this is the same as @code{srcdir}.
2394 @end defvar
2396 @defvar abs_top_srcdir
2397 @ovindex abs_top_srcdir
2398 Absolute name of @code{top_srcdir}.
2399 @end defvar
2401 @node Installation Directory Variables
2402 @subsection Installation Directory Variables
2403 @cindex Installation directories
2404 @cindex Directories, installation
2406 The following variables specify the directories for
2407 package installation, see @ref{Directory Variables, , Variables for
2408 Installation Directories, standards, The @acronym{GNU} Coding
2409 Standards}, for more information.  See the end of this section for
2410 details on when and how to use these variables.
2412 @defvar bindir
2413 @ovindex bindir
2414 The directory for installing executables that users run.
2415 @end defvar
2417 @defvar datadir
2418 @ovindex datadir
2419 The directory for installing idiosyncratic read-only
2420 architecture-independent data.
2421 @end defvar
2423 @defvar datarootdir
2424 @ovindex datarootdir
2425 The root of the directory tree for read-only architecture-independent
2426 data files.
2427 @end defvar
2429 @defvar docdir
2430 @ovindex docdir
2431 The directory for installing documentation files (other than Info and
2432 man).
2433 @end defvar
2435 @defvar dvidir
2436 @ovindex dvidir
2437 The directory for installing documentation files in DVI format.
2438 @end defvar
2440 @defvar exec_prefix
2441 @ovindex exec_prefix
2442 The installation prefix for architecture-dependent files.  By default
2443 it's the same as @var{prefix}.  You should avoid installing anything
2444 directly to @var{exec_prefix}.  However, the default value for
2445 directories containing architecture-dependent files should be relative
2446 to @var{exec_prefix}.
2447 @end defvar
2449 @defvar htmldir
2450 @ovindex htmldir
2451 The directory for installing HTML documentation.
2452 @end defvar
2454 @defvar includedir
2455 @ovindex includedir
2456 The directory for installing C header files.
2457 @end defvar
2459 @defvar infodir
2460 @ovindex infodir
2461 The directory for installing documentation in Info format.
2462 @end defvar
2464 @defvar libdir
2465 @ovindex libdir
2466 The directory for installing object code libraries.
2467 @end defvar
2469 @defvar libexecdir
2470 @ovindex libexecdir
2471 The directory for installing executables that other programs run.
2472 @end defvar
2474 @defvar localedir
2475 @ovindex localedir
2476 The directory for installing locale-dependent but
2477 architecture-independent data, such as message catalogs.  This directory
2478 usually has a subdirectory per locale.
2479 @end defvar
2481 @defvar localstatedir
2482 @ovindex localstatedir
2483 The directory for installing modifiable single-machine data.
2484 @end defvar
2486 @defvar mandir
2487 @ovindex mandir
2488 The top-level directory for installing documentation in man format.
2489 @end defvar
2491 @defvar oldincludedir
2492 @ovindex oldincludedir
2493 The directory for installing C header files for non-@acronym{GCC} compilers.
2494 @end defvar
2496 @defvar pdfdir
2497 @ovindex pdfdir
2498 The directory for installing PDF documentation.
2499 @end defvar
2501 @defvar prefix
2502 @ovindex prefix
2503 The common installation prefix for all files.  If @var{exec_prefix}
2504 is defined to a different value, @var{prefix} is used only for
2505 architecture-independent files.
2506 @end defvar
2508 @defvar psdir
2509 @ovindex psdir
2510 The directory for installing PostScript documentation.
2511 @end defvar
2513 @defvar sbindir
2514 @ovindex sbindir
2515 The directory for installing executables that system
2516 administrators run.
2517 @end defvar
2519 @defvar sharedstatedir
2520 @ovindex sharedstatedir
2521 The directory for installing modifiable architecture-independent data.
2522 @end defvar
2524 @defvar sysconfdir
2525 @ovindex sysconfdir
2526 The directory for installing read-only single-machine data.
2527 @end defvar
2530 Most of these variables have values that rely on @code{prefix} or
2531 @code{exec_prefix}.  It is deliberate that the directory output
2532 variables keep them unexpanded: typically @samp{@@datarootdir@@} is
2533 replaced by @samp{$@{prefix@}/share}, not @samp{/usr/local/share}, and
2534 @samp{@@datadir@@} is replaced by @samp{$@{datarootdir@}}.
2536 This behavior is mandated by the @acronym{GNU} coding standards, so that when
2537 the user runs:
2539 @table @samp
2540 @item make
2541 she can still specify a different prefix from the one specified to
2542 @command{configure}, in which case, if needed, the package should hard
2543 code dependencies corresponding to the make-specified prefix.
2545 @item make install
2546 she can specify a different installation location, in which case the
2547 package @emph{must} still depend on the location which was compiled in
2548 (i.e., never recompile when @samp{make install} is run).  This is an
2549 extremely important feature, as many people may decide to install all
2550 the files of a package grouped together, and then install links from
2551 the final locations to there.
2552 @end table
2554 In order to support these features, it is essential that
2555 @code{datarootdir} remains being defined as @samp{$@{prefix@}/share} to
2556 depend upon the current value of @code{prefix}.
2558 A corollary is that you should not use these variables except in
2559 makefiles.  For instance, instead of trying to evaluate @code{datadir}
2560 in @file{configure} and hard-coding it in makefiles using
2561 e.g., @samp{AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])},
2562 you should add
2563 @option{-DDATADIR='$(datadir)'} to your makefile's definition of
2564 @code{CPPFLAGS} (@code{AM_CPPFLAGS} if you are also using Automake).
2566 Similarly, you should not rely on @code{AC_CONFIG_FILES} to replace
2567 @code{datadir} and friends in your shell scripts and other files; instead,
2568 let @command{make} manage their replacement.  For instance Autoconf
2569 ships templates of its shell scripts ending with @samp{.in}, and uses a
2570 makefile snippet similar to the following to build scripts like
2571 @command{autoheader} and @command{autom4te}:
2573 @example
2574 @group
2575 edit = sed \
2576         -e 's|@@datadir[@@]|$(pkgdatadir)|g' \
2577         -e 's|@@prefix[@@]|$(prefix)|g'
2578 @end group
2580 @group
2581 autoheader autom4te: Makefile
2582         rm -f $@@ $@@.tmp
2583         $(edit) '$(srcdir)/$@@.in' >$@@.tmp
2584         chmod +x $@@.tmp
2585         chmod a-w $@@.tmp
2586         mv $@@.tmp $@@
2587 @end group
2589 @group
2590 autoheader: $(srcdir)/autoheader.in
2591 autom4te: $(srcdir)/autom4te.in
2592 @end group
2593 @end example
2595 Some details are noteworthy:
2597 @table @asis
2598 @item @samp{@@datadir[@@]}
2599 The brackets prevent @command{configure} from replacing
2600 @samp{@@datadir@@} in the Sed expression itself.
2601 Brackets are preferable to a backslash here, since
2602 Posix says @samp{\@@} is not portable.
2604 @item @samp{$(pkgdatadir)}
2605 Don't use @samp{@@pkgdatadir@@}!  Use the matching makefile variable
2606 instead.
2608 @item @samp{/}
2609 Don't use @samp{/} in the Sed expressions that replace file names since
2610 most likely the
2611 variables you use, such as @samp{$(pkgdatadir)}, contain @samp{/}.
2612 Use a shell metacharacter instead, such as @samp{|}.
2614 @item special characters
2615 File names, file name components, and the value of @code{VPATH} should
2616 not contain shell metacharacters or white
2617 space.  @xref{Special Chars in Variables}.
2619 @item dependency on @file{Makefile}
2620 Since @code{edit} uses values that depend on the configuration specific
2621 values (@code{prefix}, etc.)@: and not only on @code{VERSION} and so forth,
2622 the output depends on @file{Makefile}, not @file{configure.ac}.
2624 @item @samp{$@@}
2625 The main rule is generic, and uses @samp{$@@} extensively to
2626 avoid the need for multiple copies of the rule.
2628 @item Separated dependencies and single suffix rules
2629 You can't use them!  The above snippet cannot be (portably) rewritten
2632 @example
2633 autoconf autoheader: Makefile
2634 @group
2635 .in:
2636         rm -f $@@ $@@.tmp
2637         $(edit) $< >$@@.tmp
2638         chmod +x $@@.tmp
2639         mv $@@.tmp $@@
2640 @end group
2641 @end example
2643 @xref{Single Suffix Rules}, for details.
2645 @item @samp{$(srcdir)}
2646 Be sure to specify the name of the source directory,
2647 otherwise the package won't support separated builds.
2648 @end table
2650 For the more specific installation of Erlang libraries, the following variables
2651 are defined:
2653 @defvar ERLANG_INSTALL_LIB_DIR
2654 @ovindex ERLANG_INSTALL_LIB_DIR
2655 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
2656 The common parent directory of Erlang library installation directories.
2657 This variable is set by calling the @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR}
2658 macro in @file{configure.ac}.
2659 @end defvar
2661 @defvar ERLANG_INSTALL_LIB_DIR_@var{library}
2662 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
2663 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
2664 The installation directory for Erlang library @var{library}.
2665 This variable is set by calling the
2666 @samp{AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR(@var{library}, @var{version}}
2667 macro in @file{configure.ac}.
2668 @end defvar
2670 @xref{Erlang Libraries}, for details.
2673 @node Changed Directory Variables
2674 @subsection Changed Directory Variables
2675 @cindex @file{datarootdir}
2677 In Autoconf 2.60, the set of directory variables has changed, and the
2678 defaults of some variables have been adjusted
2679 (@pxref{Installation Directory Variables}) to changes in the
2680 @acronym{GNU} Coding Standards.  Notably, @file{datadir}, @file{infodir}, and
2681 @file{mandir} are now expressed in terms of @file{datarootdir}.  If you are
2682 upgrading from an earlier Autoconf version, you may need to adjust your files
2683 to ensure that the directory variables are substituted correctly
2684 (@pxref{Defining Directories}), and that a definition of @file{datarootdir} is
2685 in place.  For example, in a @file{Makefile.in}, adding
2687 @example
2688 datarootdir = @@datarootdir@@
2689 @end example
2691 @noindent
2692 is usually sufficient.  If you use Automake to create @file{Makefile.in},
2693 it will add this for you.
2695 To help with the transition, Autoconf warns about files that seem to use
2696 @code{datarootdir} without defining it.  In some cases, it then expands
2697 the value of @code{$datarootdir} in substitutions of the directory
2698 variables.  The following example shows such a warning:
2700 @example
2701 $ @kbd{cat configure.ac}
2702 AC_INIT
2703 AC_CONFIG_FILES([Makefile])
2704 AC_OUTPUT
2705 $ @kbd{cat Makefile.in}
2706 prefix = @@prefix@@
2707 datadir = @@datadir@@
2708 $ @kbd{autoconf}
2709 $ @kbd{configure}
2710 configure: creating ./config.status
2711 config.status: creating Makefile
2712 config.status: WARNING:
2713                Makefile.in seems to ignore the --datarootdir setting
2714 $ @kbd{cat Makefile}
2715 prefix = /usr/local
2716 datadir = $@{prefix@}/share
2717 @end example
2719 Usually one can easily change the file to accommodate both older and newer
2720 Autoconf releases:
2722 @example
2723 $ @kbd{cat Makefile.in}
2724 prefix = @@prefix@@
2725 datarootdir = @@datarootdir@@
2726 datadir = @@datadir@@
2727 $ @kbd{configure}
2728 configure: creating ./config.status
2729 config.status: creating Makefile
2730 $ @kbd{cat Makefile}
2731 prefix = /usr/local
2732 datarootdir = $@{prefix@}/share
2733 datadir = $@{datarootdir@}
2734 @end example
2736 @acindex{DATAROOTDIR_CHECKED}
2737 In some cases, however, the checks may not be able to detect that a suitable
2738 initialization of @code{datarootdir} is in place, or they may fail to detect
2739 that such an initialization is necessary in the output file.  If, after
2740 auditing your package, there are still spurious @file{configure} warnings about
2741 @code{datarootdir}, you may add the line
2743 @example
2744 AC_DEFUN([AC_DATAROOTDIR_CHECKED])
2745 @end example
2747 @noindent
2748 to your @file{configure.ac} to disable the warnings.  This is an exception
2749 to the usual rule that you should not define a macro whose name begins with
2750 @code{AC_} (@pxref{Macro Names}).
2754 @node Build Directories
2755 @subsection Build Directories
2756 @cindex Build directories
2757 @cindex Directories, build
2759 You can support compiling a software package for several architectures
2760 simultaneously from the same copy of the source code.  The object files
2761 for each architecture are kept in their own directory.
2763 To support doing this, @command{make} uses the @code{VPATH} variable to
2764 find the files that are in the source directory.  @acronym{GNU} Make
2765 and most other recent @command{make} programs can do this.  Older
2766 @command{make} programs do not support @code{VPATH}; when using them, the
2767 source code must be in the same directory as the object files.
2769 To support @code{VPATH}, each @file{Makefile.in} should contain two
2770 lines that look like:
2772 @example
2773 srcdir = @@srcdir@@
2774 VPATH = @@srcdir@@
2775 @end example
2777 Do not set @code{VPATH} to the value of another variable, for example
2778 @samp{VPATH = $(srcdir)}, because some versions of @command{make} do not do
2779 variable substitutions on the value of @code{VPATH}.
2781 @command{configure} substitutes the correct value for @code{srcdir} when
2782 it produces @file{Makefile}.
2784 Do not use the @code{make} variable @code{$<}, which expands to the
2785 file name of the file in the source directory (found with @code{VPATH}),
2786 except in implicit rules.  (An implicit rule is one such as @samp{.c.o},
2787 which tells how to create a @file{.o} file from a @file{.c} file.)  Some
2788 versions of @command{make} do not set @code{$<} in explicit rules; they
2789 expand it to an empty value.
2791 Instead, Make command lines should always refer to source
2792 files by prefixing them with @samp{$(srcdir)/}.  For example:
2794 @example
2795 time.info: time.texinfo
2796         $(MAKEINFO) '$(srcdir)/time.texinfo'
2797 @end example
2799 @node Automatic Remaking
2800 @subsection Automatic Remaking
2801 @cindex Automatic remaking
2802 @cindex Remaking automatically
2804 You can put rules like the following in the top-level @file{Makefile.in}
2805 for a package to automatically update the configuration information when
2806 you change the configuration files.  This example includes all of the
2807 optional files, such as @file{aclocal.m4} and those related to
2808 configuration header files.  Omit from the @file{Makefile.in} rules for
2809 any of these files that your package does not use.
2811 The @samp{$(srcdir)/} prefix is included because of limitations in the
2812 @code{VPATH} mechanism.
2814 The @file{stamp-} files are necessary because the timestamps of
2815 @file{config.h.in} and @file{config.h} are not changed if remaking
2816 them does not change their contents.  This feature avoids unnecessary
2817 recompilation.  You should include the file @file{stamp-h.in} your
2818 package's distribution, so that @command{make} considers
2819 @file{config.h.in} up to date.  Don't use @command{touch}
2820 (@pxref{Limitations of Usual Tools}); instead, use @command{echo} (using
2821 @command{date} would cause needless differences, hence @acronym{CVS}
2822 conflicts, etc.).
2824 @example
2825 @group
2826 $(srcdir)/configure: configure.ac aclocal.m4
2827         cd '$(srcdir)' && autoconf
2829 # autoheader might not change config.h.in, so touch a stamp file.
2830 $(srcdir)/config.h.in: stamp-h.in
2831 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
2832         cd '$(srcdir)' && autoheader
2833         echo timestamp > '$(srcdir)/stamp-h.in'
2835 config.h: stamp-h
2836 stamp-h: config.h.in config.status
2837         ./config.status
2839 Makefile: Makefile.in config.status
2840         ./config.status
2842 config.status: configure
2843         ./config.status --recheck
2844 @end group
2845 @end example
2847 @noindent
2848 (Be careful if you copy these lines directly into your makefile, as you
2849 need to convert the indented lines to start with the tab character.)
2851 In addition, you should use
2853 @example
2854 AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
2855 @end example
2857 @noindent
2858 so @file{config.status} ensures that @file{config.h} is considered up to
2859 date.  @xref{Output}, for more information about @code{AC_OUTPUT}.
2861 @xref{config.status Invocation}, for more examples of handling
2862 configuration-related dependencies.
2864 @node Configuration Headers
2865 @section Configuration Header Files
2866 @cindex Configuration Header
2867 @cindex @file{config.h}
2869 When a package contains more than a few tests that define C preprocessor
2870 symbols, the command lines to pass @option{-D} options to the compiler
2871 can get quite long.  This causes two problems.  One is that the
2872 @command{make} output is hard to visually scan for errors.  More
2873 seriously, the command lines can exceed the length limits of some
2874 operating systems.  As an alternative to passing @option{-D} options to
2875 the compiler, @command{configure} scripts can create a C header file
2876 containing @samp{#define} directives.  The @code{AC_CONFIG_HEADERS}
2877 macro selects this kind of output.  Though it can be called anywhere
2878 between @code{AC_INIT} and @code{AC_OUTPUT}, it is customary to call
2879 it right after @code{AC_INIT}.
2881 The package should @samp{#include} the configuration header file before
2882 any other header files, to prevent inconsistencies in declarations (for
2883 example, if it redefines @code{const}).
2885 To provide for VPATH builds, remember to pass the C compiler a @option{-I.}
2886 option (or @option{-I..}; whichever directory contains @file{config.h}).
2887 Even if you use @samp{#include "config.h"}, the preprocessor searches only
2888 the directory of the currently read file, i.e., the source directory, not
2889 the build directory.
2891 With the appropriate @option{-I} option, you can use
2892 @samp{#include <config.h>}.  Actually, it's a good habit to use it,
2893 because in the rare case when the source directory contains another
2894 @file{config.h}, the build directory should be searched first.
2897 @defmac AC_CONFIG_HEADERS (@var{header} @dots{}, @ovar{cmds}, @ovar{init-cmds})
2898 @acindex{CONFIG_HEADERS}
2899 @cvindex HAVE_CONFIG_H
2900 This macro is one of the instantiating macros; see @ref{Configuration
2901 Actions}.  Make @code{AC_OUTPUT} create the file(s) in the
2902 blank-or-newline-separated list @var{header} containing C preprocessor
2903 @code{#define} statements, and replace @samp{@@DEFS@@} in generated
2904 files with @option{-DHAVE_CONFIG_H} instead of the value of @code{DEFS}.
2905 The usual name for @var{header} is @file{config.h}.
2907 If @var{header} already exists and its contents are identical to what
2908 @code{AC_OUTPUT} would put in it, it is left alone.  Doing this allows
2909 making some changes in the configuration without needlessly causing
2910 object files that depend on the header file to be recompiled.
2912 Usually the input file is named @file{@var{header}.in}; however, you can
2913 override the input file name by appending to @var{header} a
2914 colon-separated list of input files.  Examples:
2916 @example
2917 AC_CONFIG_HEADERS([config.h:config.hin])
2918 AC_CONFIG_HEADERS([defines.h:defs.pre:defines.h.in:defs.post])
2919 @end example
2921 @noindent
2922 Doing this allows you to keep your file names acceptable to
2923 @acronym{DOS} variants, or
2924 to prepend and/or append boilerplate to the file.
2925 @end defmac
2927 @defmac AH_HEADER
2928 @ahindex{HEADER}
2929 This macro is defined as the name of the first declared config header
2930 and undefined if no config headers have been declared up to this point.
2931 A third-party macro may, for example, require use of a config header
2932 without invoking AC_CONFIG_HEADERS twice, like this:
2934 @example
2935 AC_CONFIG_COMMANDS_PRE(
2936         [m4_ifndef([AH_HEADER], [AC_CONFIG_HEADERS([config.h])])])
2937 @end example
2939 @end defmac
2941 @xref{Configuration Actions}, for more details on @var{header}.
2943 @menu
2944 * Header Templates::            Input for the configuration headers
2945 * autoheader Invocation::       How to create configuration templates
2946 * Autoheader Macros::           How to specify CPP templates
2947 @end menu
2949 @node Header Templates
2950 @subsection Configuration Header Templates
2951 @cindex Configuration Header Template
2952 @cindex Header templates
2953 @cindex @file{config.h.in}
2955 Your distribution should contain a template file that looks as you want
2956 the final header file to look, including comments, with @code{#undef}
2957 statements which are used as hooks.  For example, suppose your
2958 @file{configure.ac} makes these calls:
2960 @example
2961 AC_CONFIG_HEADERS([conf.h])
2962 AC_CHECK_HEADERS([unistd.h])
2963 @end example
2965 @noindent
2966 Then you could have code like the following in @file{conf.h.in}.  On
2967 systems that have @file{unistd.h}, @command{configure} defines
2968 @samp{HAVE_UNISTD_H} to 1.  On other systems, the whole line is
2969 commented out (in case the system predefines that symbol).
2971 @example
2972 @group
2973 /* Define as 1 if you have unistd.h.  */
2974 #undef HAVE_UNISTD_H
2975 @end group
2976 @end example
2978 Pay attention that @samp{#undef} is in the first column, and there is
2979 nothing after @samp{HAVE_UNISTD_H}, not even white space.  You can
2980 then decode the configuration header using the preprocessor directives:
2982 @example
2983 @group
2984 #include <conf.h>
2986 #if HAVE_UNISTD_H
2987 # include <unistd.h>
2988 #else
2989 /* We are in trouble.  */
2990 #endif
2991 @end group
2992 @end example
2994 The use of old form templates, with @samp{#define} instead of
2995 @samp{#undef} is strongly discouraged.  Similarly with old templates
2996 with comments on the same line as the @samp{#undef}.  Anyway, putting
2997 comments in preprocessor macros has never been a good idea.
2999 Since it is a tedious task to keep a template header up to date, you may
3000 use @command{autoheader} to generate it, see @ref{autoheader Invocation}.
3003 @node autoheader Invocation
3004 @subsection Using @command{autoheader} to Create @file{config.h.in}
3005 @cindex @command{autoheader}
3007 The @command{autoheader} program can create a template file of C
3008 @samp{#define} statements for @command{configure} to use.  If
3009 @file{configure.ac} invokes @code{AC_CONFIG_HEADERS(@var{file})},
3010 @command{autoheader} creates @file{@var{file}.in}; if multiple file
3011 arguments are given, the first one is used.  Otherwise,
3012 @command{autoheader} creates @file{config.h.in}.
3014 In order to do its job, @command{autoheader} needs you to document all
3015 of the symbols that you might use.  Typically this is done via an
3016 @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED} call whose first argument
3017 is a literal symbol and whose third argument describes the symbol
3018 (@pxref{Defining Symbols}).  Alternatively, you can use
3019 @code{AH_TEMPLATE} (@pxref{Autoheader Macros}), or you can supply a
3020 suitable input file for a subsequent configuration header file.
3021 Symbols defined by Autoconf's builtin tests are already documented properly;
3022 you need to document only those that you
3023 define yourself.
3025 You might wonder why @command{autoheader} is needed: after all, why
3026 would @command{configure} need to ``patch'' a @file{config.h.in} to
3027 produce a @file{config.h} instead of just creating @file{config.h} from
3028 scratch?  Well, when everything rocks, the answer is just that we are
3029 wasting our time maintaining @command{autoheader}: generating
3030 @file{config.h} directly is all that is needed.  When things go wrong,
3031 however, you'll be thankful for the existence of @command{autoheader}.
3033 The fact that the symbols are documented is important in order to
3034 @emph{check} that @file{config.h} makes sense.  The fact that there is a
3035 well-defined list of symbols that should be defined (or not) is
3036 also important for people who are porting packages to environments where
3037 @command{configure} cannot be run: they just have to @emph{fill in the
3038 blanks}.
3040 But let's come back to the point: the invocation of @command{autoheader}@dots{}
3042 If you give @command{autoheader} an argument, it uses that file instead
3043 of @file{configure.ac} and writes the header file to the standard output
3044 instead of to @file{config.h.in}.  If you give @command{autoheader} an
3045 argument of @option{-}, it reads the standard input instead of
3046 @file{configure.ac} and writes the header file to the standard output.
3048 @command{autoheader} accepts the following options:
3050 @table @option
3051 @item --help
3052 @itemx -h
3053 Print a summary of the command line options and exit.
3055 @item --version
3056 @itemx -V
3057 Print the version number of Autoconf and exit.
3059 @item --verbose
3060 @itemx -v
3061 Report processing steps.
3063 @item --debug
3064 @itemx -d
3065 Don't remove the temporary files.
3067 @item --force
3068 @itemx -f
3069 Remake the template file even if newer than its input files.
3071 @item --include=@var{dir}
3072 @itemx -I @var{dir}
3073 Append @var{dir} to the include path.  Multiple invocations accumulate.
3075 @item --prepend-include=@var{dir}
3076 @item -B @var{dir}
3077 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
3079 @item --warnings=@var{category}
3080 @itemx -W @var{category}
3081 @evindex WARNINGS
3082 Report the warnings related to @var{category} (which can actually be a
3083 comma separated list).  Current categories include:
3085 @table @samp
3086 @item obsolete
3087 report the uses of obsolete constructs
3089 @item all
3090 report all the warnings
3092 @item none
3093 report none
3095 @item error
3096 treats warnings as errors
3098 @item no-@var{category}
3099 disable warnings falling into @var{category}
3100 @end table
3102 @end table
3106 @node Autoheader Macros
3107 @subsection Autoheader Macros
3108 @cindex Autoheader macros
3110 @command{autoheader} scans @file{configure.ac} and figures out which C
3111 preprocessor symbols it might define.  It knows how to generate
3112 templates for symbols defined by @code{AC_CHECK_HEADERS},
3113 @code{AC_CHECK_FUNCS} etc., but if you @code{AC_DEFINE} any additional
3114 symbol, you must define a template for it.  If there are missing
3115 templates, @command{autoheader} fails with an error message.
3117 The simplest way to create a template for a @var{symbol} is to supply
3118 the @var{description} argument to an @samp{AC_DEFINE(@var{symbol})}; see
3119 @ref{Defining Symbols}.  You may also use one of the following macros.
3121 @defmac AH_VERBATIM (@var{key}, @var{template})
3122 @ahindex{VERBATIM}
3123 Tell @command{autoheader} to include the @var{template} as-is in the header
3124 template file.  This @var{template} is associated with the @var{key},
3125 which is used to sort all the different templates and guarantee their
3126 uniqueness.  It should be a symbol that can be defined via @code{AC_DEFINE}.
3128 For example:
3130 @example
3131 AH_VERBATIM([_GNU_SOURCE],
3132 [/* Enable GNU extensions on systems that have them.  */
3133 #ifndef _GNU_SOURCE
3134 # define _GNU_SOURCE
3135 #endif])
3136 @end example
3137 @end defmac
3140 @defmac AH_TEMPLATE (@var{key}, @var{description})
3141 @ahindex{TEMPLATE}
3142 Tell @command{autoheader} to generate a template for @var{key}.  This macro
3143 generates standard templates just like @code{AC_DEFINE} when a
3144 @var{description} is given.
3146 For example:
3148 @example
3149 AH_TEMPLATE([CRAY_STACKSEG_END],
3150             [Define to one of _getb67, GETB67, getb67
3151              for Cray-2 and Cray-YMP systems.  This
3152              function is required for alloca.c support
3153              on those systems.])
3154 @end example
3156 @noindent
3157 generates the following template, with the description properly
3158 justified.
3160 @example
3161 /* Define to one of _getb67, GETB67, getb67 for Cray-2 and
3162    Cray-YMP systems.  This function is required for alloca.c
3163    support on those systems.  */
3164 #undef CRAY_STACKSEG_END
3165 @end example
3166 @end defmac
3169 @defmac AH_TOP (@var{text})
3170 @ahindex{TOP}
3171 Include @var{text} at the top of the header template file.
3172 @end defmac
3175 @defmac AH_BOTTOM (@var{text})
3176 @ahindex{BOTTOM}
3177 Include @var{text} at the bottom of the header template file.
3178 @end defmac
3181 @node Configuration Commands
3182 @section Running Arbitrary Configuration Commands
3183 @cindex Configuration commands
3184 @cindex Commands for configuration
3186 You can execute arbitrary commands before, during, and after
3187 @file{config.status} is run.  The three following macros accumulate the
3188 commands to run when they are called multiple times.
3189 @code{AC_CONFIG_COMMANDS} replaces the obsolete macro
3190 @code{AC_OUTPUT_COMMANDS}; see @ref{Obsolete Macros}, for details.
3192 @defmac AC_CONFIG_COMMANDS (@var{tag}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3193 @acindex{CONFIG_COMMANDS}
3194 Specify additional shell commands to run at the end of
3195 @file{config.status}, and shell commands to initialize any variables
3196 from @command{configure}.  Associate the commands with @var{tag}.
3197 Since typically the @var{cmds} create a file, @var{tag} should
3198 naturally be the name of that file.  If needed, the directory hosting
3199 @var{tag} is created.  This macro is one of the instantiating macros;
3200 see @ref{Configuration Actions}.
3202 Here is an unrealistic example:
3203 @example
3204 fubar=42
3205 AC_CONFIG_COMMANDS([fubar],
3206                    [echo this is extra $fubar, and so on.],
3207                    [fubar=$fubar])
3208 @end example
3210 Here is a better one:
3211 @example
3212 AC_CONFIG_COMMANDS([timestamp], [date >timestamp])
3213 @end example
3214 @end defmac
3216 The following two macros look similar, but in fact they are not of the same
3217 breed: they are executed directly by @file{configure}, so you cannot use
3218 @file{config.status} to rerun them.
3220 @c Yet it is good to leave them here.  The user sees them together and
3221 @c decides which best fits their needs.
3223 @defmac AC_CONFIG_COMMANDS_PRE (@var{cmds})
3224 @acindex{CONFIG_COMMANDS_PRE}
3225 Execute the @var{cmds} right before creating @file{config.status}.
3227 This macro presents the last opportunity to call @code{AC_SUBST},
3228 @code{AC_DEFINE}, or @code{AC_CONFIG_FOOS} macros.
3229 @end defmac
3231 @defmac AC_CONFIG_COMMANDS_POST (@var{cmds})
3232 @acindex{CONFIG_COMMANDS_POST}
3233 Execute the @var{cmds} right after creating @file{config.status}.
3234 @end defmac
3239 @node Configuration Links
3240 @section Creating Configuration Links
3241 @cindex Configuration links
3242 @cindex Links for configuration
3244 You may find it convenient to create links whose destinations depend upon
3245 results of tests.  One can use @code{AC_CONFIG_COMMANDS} but the
3246 creation of relative symbolic links can be delicate when the package is
3247 built in a directory different from the source directory.
3249 @defmac AC_CONFIG_LINKS (@var{dest}:@var{source}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3250 @acindex{CONFIG_LINKS}
3251 @cindex Links
3252 Make @code{AC_OUTPUT} link each of the existing files @var{source} to
3253 the corresponding link name @var{dest}.  Makes a symbolic link if
3254 possible, otherwise a hard link if possible, otherwise a copy.  The
3255 @var{dest} and @var{source} names should be relative to the top level
3256 source or build directory.  This macro is one of the instantiating
3257 macros; see @ref{Configuration Actions}.
3259 For example, this call:
3261 @example
3262 AC_CONFIG_LINKS([host.h:config/$machine.h
3263                 object.h:config/$obj_format.h])
3264 @end example
3266 @noindent
3267 creates in the current directory @file{host.h} as a link to
3268 @file{@var{srcdir}/config/$machine.h}, and @file{object.h} as a
3269 link to @file{@var{srcdir}/config/$obj_format.h}.
3271 The tempting value @samp{.} for @var{dest} is invalid: it makes it
3272 impossible for @samp{config.status} to guess the links to establish.
3274 One can then run:
3275 @example
3276 ./config.status host.h object.h
3277 @end example
3278 @noindent
3279 to create the links.
3280 @end defmac
3284 @node Subdirectories
3285 @section Configuring Other Packages in Subdirectories
3286 @cindex Configure subdirectories
3287 @cindex Subdirectory configure
3289 In most situations, calling @code{AC_OUTPUT} is sufficient to produce
3290 makefiles in subdirectories.  However, @command{configure} scripts
3291 that control more than one independent package can use
3292 @code{AC_CONFIG_SUBDIRS} to run @command{configure} scripts for other
3293 packages in subdirectories.
3295 @defmac AC_CONFIG_SUBDIRS (@var{dir} @dots{})
3296 @acindex{CONFIG_SUBDIRS}
3297 @ovindex subdirs
3298 Make @code{AC_OUTPUT} run @command{configure} in each subdirectory
3299 @var{dir} in the given blank-or-newline-separated list.  Each @var{dir} should
3300 be a literal, i.e., please do not use:
3302 @example
3303 if test "$package_foo_enabled" = yes; then
3304   $my_subdirs="$my_subdirs foo"
3306 AC_CONFIG_SUBDIRS([$my_subdirs])
3307 @end example
3309 @noindent
3310 because this prevents @samp{./configure --help=recursive} from
3311 displaying the options of the package @code{foo}.  Instead, you should
3312 write:
3314 @example
3315 if test "$package_foo_enabled" = yes; then
3316   AC_CONFIG_SUBDIRS([foo])
3318 @end example
3320 If a given @var{dir} is not found, an error is reported: if the
3321 subdirectory is optional, write:
3323 @example
3324 if test -d "$srcdir/foo"; then
3325   AC_CONFIG_SUBDIRS([foo])
3327 @end example
3329 @c NB: Yes, below we mean configure.in, not configure.ac.
3330 If a given @var{dir} contains @command{configure.gnu}, it is run instead
3331 of @command{configure}.  This is for packages that might use a
3332 non-Autoconf script @command{Configure}, which can't be called through a
3333 wrapper @command{configure} since it would be the same file on
3334 case-insensitive file systems.  Likewise, if a @var{dir} contains
3335 @file{configure.in} but no @command{configure}, the Cygnus
3336 @command{configure} script found by @code{AC_CONFIG_AUX_DIR} is used.
3338 The subdirectory @command{configure} scripts are given the same command
3339 line options that were given to this @command{configure} script, with minor
3340 changes if needed, which include:
3342 @itemize @minus
3343 @item
3344 adjusting a relative name for the cache file;
3346 @item
3347 adjusting a relative name for the source directory;
3349 @item
3350 propagating the current value of @code{$prefix}, including if it was
3351 defaulted, and if the default values of the top level and of the subdirectory
3352 @file{configure} differ.
3353 @end itemize
3355 This macro also sets the output variable @code{subdirs} to the list of
3356 directories @samp{@var{dir} @dots{}}.  Make rules can use
3357 this variable to determine which subdirectories to recurse into.
3359 This macro may be called multiple times.
3360 @end defmac
3362 @node Default Prefix
3363 @section Default Prefix
3364 @cindex Install prefix
3365 @cindex Prefix for install
3367 By default, @command{configure} sets the prefix for files it installs to
3368 @file{/usr/local}.  The user of @command{configure} can select a different
3369 prefix using the @option{--prefix} and @option{--exec-prefix} options.
3370 There are two ways to change the default: when creating
3371 @command{configure}, and when running it.
3373 Some software packages might want to install in a directory other than
3374 @file{/usr/local} by default.  To accomplish that, use the
3375 @code{AC_PREFIX_DEFAULT} macro.
3377 @defmac AC_PREFIX_DEFAULT (@var{prefix})
3378 @acindex{PREFIX_DEFAULT}
3379 Set the default installation prefix to @var{prefix} instead of
3380 @file{/usr/local}.
3381 @end defmac
3383 It may be convenient for users to have @command{configure} guess the
3384 installation prefix from the location of a related program that they
3385 have already installed.  If you wish to do that, you can call
3386 @code{AC_PREFIX_PROGRAM}.
3388 @defmac AC_PREFIX_PROGRAM (@var{program})
3389 @acindex{PREFIX_PROGRAM}
3390 If the user did not specify an installation prefix (using the
3391 @option{--prefix} option), guess a value for it by looking for
3392 @var{program} in @env{PATH}, the way the shell does.  If @var{program}
3393 is found, set the prefix to the parent of the directory containing
3394 @var{program}, else default the prefix as described above
3395 (@file{/usr/local} or @code{AC_PREFIX_DEFAULT}).  For example, if
3396 @var{program} is @code{gcc} and the @env{PATH} contains
3397 @file{/usr/local/gnu/bin/gcc}, set the prefix to @file{/usr/local/gnu}.
3398 @end defmac
3402 @c ======================================================== Existing tests
3404 @node Existing Tests
3405 @chapter Existing Tests
3407 These macros test for particular system features that packages might
3408 need or want to use.  If you need to test for a kind of feature that
3409 none of these macros check for, you can probably do it by calling
3410 primitive test macros with appropriate arguments (@pxref{Writing
3411 Tests}).
3413 These tests print messages telling the user which feature they're
3414 checking for, and what they find.  They cache their results for future
3415 @command{configure} runs (@pxref{Caching Results}).
3417 Some of these macros set output variables.  @xref{Makefile
3418 Substitutions}, for how to get their values.  The phrase ``define
3419 @var{name}'' is used below as a shorthand to mean ``define the C
3420 preprocessor symbol @var{name} to the value 1''.  @xref{Defining
3421 Symbols}, for how to get those symbol definitions into your program.
3423 @menu
3424 * Common Behavior::             Macros' standard schemes
3425 * Alternative Programs::        Selecting between alternative programs
3426 * Files::                       Checking for the existence of files
3427 * Libraries::                   Library archives that might be missing
3428 * Library Functions::           C library functions that might be missing
3429 * Header Files::                Header files that might be missing
3430 * Declarations::                Declarations that may be missing
3431 * Structures::                  Structures or members that might be missing
3432 * Types::                       Types that might be missing
3433 * Compilers and Preprocessors::  Checking for compiling programs
3434 * System Services::             Operating system services
3435 * Posix Variants::              Special kludges for specific Posix variants
3436 * Erlang Libraries::            Checking for the existence of Erlang libraries
3437 @end menu
3439 @node Common Behavior
3440 @section Common Behavior
3441 @cindex Common autoconf behavior
3443 Much effort has been expended to make Autoconf easy to learn.  The most
3444 obvious way to reach this goal is simply to enforce standard interfaces
3445 and behaviors, avoiding exceptions as much as possible.  Because of
3446 history and inertia, unfortunately, there are still too many exceptions
3447 in Autoconf; nevertheless, this section describes some of the common
3448 rules.
3450 @menu
3451 * Standard Symbols::            Symbols defined by the macros
3452 * Default Includes::            Includes used by the generic macros
3453 @end menu
3455 @node Standard Symbols
3456 @subsection Standard Symbols
3457 @cindex Standard symbols
3459 All the generic macros that @code{AC_DEFINE} a symbol as a result of
3460 their test transform their @var{argument} values to a standard alphabet.
3461 First, @var{argument} is converted to upper case and any asterisks
3462 (@samp{*}) are each converted to @samp{P}.  Any remaining characters
3463 that are not alphanumeric are converted to underscores.
3465 For instance,
3467 @example
3468 AC_CHECK_TYPES([struct $Expensive*])
3469 @end example
3471 @noindent
3472 defines the symbol @samp{HAVE_STRUCT__EXPENSIVEP} if the check
3473 succeeds.
3476 @node Default Includes
3477 @subsection Default Includes
3478 @cindex Default includes
3479 @cindex Includes, default
3481 Several tests depend upon a set of header files.  Since these headers
3482 are not universally available, tests actually have to provide a set of
3483 protected includes, such as:
3485 @example
3486 @group
3487 #if TIME_WITH_SYS_TIME
3488 # include <sys/time.h>
3489 # include <time.h>
3490 #else
3491 # if HAVE_SYS_TIME_H
3492 #  include <sys/time.h>
3493 # else
3494 #  include <time.h>
3495 # endif
3496 #endif
3497 @end group
3498 @end example
3500 @noindent
3501 Unless you know exactly what you are doing, you should avoid using
3502 unconditional includes, and check the existence of the headers you
3503 include beforehand (@pxref{Header Files}).
3505 Most generic macros use the following macro to provide the default set
3506 of includes:
3508 @defmac AC_INCLUDES_DEFAULT (@ovar{include-directives})
3509 @acindex{INCLUDES_DEFAULT}
3510 Expand to @var{include-directives} if defined, otherwise to:
3512 @example
3513 @group
3514 #include <stdio.h>
3515 #if HAVE_SYS_TYPES_H
3516 # include <sys/types.h>
3517 #endif
3518 #if HAVE_SYS_STAT_H
3519 # include <sys/stat.h>
3520 #endif
3521 #if STDC_HEADERS
3522 # include <stdlib.h>
3523 # include <stddef.h>
3524 #else
3525 # if HAVE_STDLIB_H
3526 #  include <stdlib.h>
3527 # endif
3528 #endif
3529 #if HAVE_STRING_H
3530 # if !STDC_HEADERS && HAVE_MEMORY_H
3531 #  include <memory.h>
3532 # endif
3533 # include <string.h>
3534 #endif
3535 #if HAVE_STRINGS_H
3536 # include <strings.h>
3537 #endif
3538 #if HAVE_INTTYPES_H
3539 # include <inttypes.h>
3540 #endif
3541 #if HAVE_STDINT_H
3542 # include <stdint.h>
3543 #endif
3544 #if HAVE_UNISTD_H
3545 # include <unistd.h>
3546 #endif
3547 @end group
3548 @end example
3550 If the default includes are used, then check for the presence of these
3551 headers and their compatibility, i.e., you don't need to run
3552 @code{AC_HEADER_STDC}, nor check for @file{stdlib.h} etc.
3554 These headers are checked for in the same order as they are included.
3555 For instance, on some systems @file{string.h} and @file{strings.h} both
3556 exist, but conflict.  Then @code{HAVE_STRING_H} is defined, not
3557 @code{HAVE_STRINGS_H}.
3558 @end defmac
3560 @node Alternative Programs
3561 @section Alternative Programs
3562 @cindex Programs, checking
3564 These macros check for the presence or behavior of particular programs.
3565 They are used to choose between several alternative programs and to
3566 decide what to do once one has been chosen.  If there is no macro
3567 specifically defined to check for a program you need, and you don't need
3568 to check for any special properties of it, then you can use one of the
3569 general program-check macros.
3571 @menu
3572 * Particular Programs::         Special handling to find certain programs
3573 * Generic Programs::            How to find other programs
3574 @end menu
3576 @node Particular Programs
3577 @subsection Particular Program Checks
3579 These macros check for particular programs---whether they exist, and
3580 in some cases whether they support certain features.
3582 @defmac AC_PROG_AWK
3583 @acindex{PROG_AWK}
3584 @ovindex AWK
3585 Check for @code{gawk}, @code{mawk}, @code{nawk}, and @code{awk}, in that
3586 order, and set output variable @code{AWK} to the first one that is found.
3587 It tries @code{gawk} first because that is reported to be the
3588 best implementation.
3589 @end defmac
3591 @defmac AC_PROG_GREP
3592 @acindex{PROG_GREP}
3593 @ovindex GREP
3594 Look for the best available @code{grep} or @code{ggrep} that accepts the
3595 longest input lines possible, and that supports multiple @option{-e} options.
3596 Set the output variable @code{GREP} to whatever is chosen.
3597 @xref{Limitations of Usual Tools}, for more information about
3598 portability problems with the @command{grep} command family.
3599 @end defmac
3601 @defmac AC_PROG_EGREP
3602 @acindex{PROG_EGREP}
3603 @ovindex EGREP
3604 Check whether @code{$GREP -E} works, or else look for the best available
3605 @code{egrep} or @code{gegrep} that accepts the longest input lines possible.
3606 Set the output variable @code{EGREP} to whatever is chosen.
3607 @end defmac
3609 @defmac AC_PROG_FGREP
3610 @acindex{PROG_FGREP}
3611 @ovindex FGREP
3612 Check whether @code{$GREP -F} works, or else look for the best available
3613 @code{fgrep} or @code{gfgrep} that accepts the longest input lines possible.
3614 Set the output variable @code{FGREP} to whatever is chosen.
3615 @end defmac
3617 @defmac AC_PROG_INSTALL
3618 @acindex{PROG_INSTALL}
3619 @ovindex INSTALL
3620 @ovindex INSTALL_PROGRAM
3621 @ovindex INSTALL_DATA
3622 @ovindex INSTALL_SCRIPT
3623 Set output variable @code{INSTALL} to the name of a @acronym{BSD}-compatible
3624 @command{install} program, if one is found in the current @env{PATH}.
3625 Otherwise, set @code{INSTALL} to @samp{@var{dir}/install-sh -c},
3626 checking the directories specified to @code{AC_CONFIG_AUX_DIR} (or its
3627 default directories) to determine @var{dir} (@pxref{Output}).  Also set
3628 the variables @code{INSTALL_PROGRAM} and @code{INSTALL_SCRIPT} to
3629 @samp{$@{INSTALL@}} and @code{INSTALL_DATA} to @samp{$@{INSTALL@} -m 644}.
3631 This macro screens out various instances of @command{install} known not to
3632 work.  It prefers to find a C program rather than a shell script, for
3633 speed.  Instead of @file{install-sh}, it can also use @file{install.sh},
3634 but that name is obsolete because some @command{make} programs have a rule
3635 that creates @file{install} from it if there is no makefile.
3637 Autoconf comes with a copy of @file{install-sh} that you can use.  If
3638 you use @code{AC_PROG_INSTALL}, you must include either
3639 @file{install-sh} or @file{install.sh} in your distribution; otherwise
3640 @command{configure} produces an error message saying it can't find
3641 them---even if the system you're on has a good @command{install} program.
3642 This check is a safety measure to prevent you from accidentally leaving
3643 that file out, which would prevent your package from installing on
3644 systems that don't have a @acronym{BSD}-compatible @command{install} program.
3646 If you need to use your own installation program because it has features
3647 not found in standard @command{install} programs, there is no reason to use
3648 @code{AC_PROG_INSTALL}; just put the file name of your program into your
3649 @file{Makefile.in} files.
3650 @end defmac
3652 @defmac AC_PROG_MKDIR_P
3653 @acindex{AC_PROG_MKDIR_P}
3654 @ovindex MKDIR_P
3655 Set output variable @code{MKDIR_P} to a program that ensures that for
3656 each argument, a directory named by this argument exists, creating it
3657 and its parent directories if needed, and without race conditions when
3658 two instances of the program attempt to make the same directory at
3659 nearly the same time.
3661 This macro uses the @samp{mkdir -p} command if possible.  Otherwise, it
3662 falls back on invoking @command{install-sh} with the @option{-d} option,
3663 so your package should
3664 contain @file{install-sh} as described under @code{AC_PROG_INSTALL}.
3665 An @file{install-sh} file that predates Autoconf 2.60 or Automake 1.10
3666 is vulnerable to race conditions, so if you want to support parallel
3667 installs from
3668 different packages into the same directory you need to make sure you
3669 have an up-to-date @file{install-sh}.  In particular, be careful about
3670 using @samp{autoreconf -if} if your Automake predates Automake 1.10.
3672 This macro is related to the @code{AS_MKDIR_P} macro (@pxref{Programming
3673 in M4sh}), but it sets an output variable intended for use in other
3674 files, whereas @code{AS_MKDIR_P} is intended for use in scripts like
3675 @command{configure}.  Also, @code{AS_MKDIR_P} does not accept options,
3676 but @code{MKDIR_P} supports the @option{-m} option, e.g., a makefile
3677 might invoke @code{$(MKDIR_P) -m 0 dir} to create an inaccessible
3678 directory, and conversely a makefile should use @code{$(MKDIR_P) --
3679 $(FOO)} if @var{FOO} might yield a value that begins with @samp{-}.
3680 Finally, @code{AS_MKDIR_P} does not check for race condition
3681 vulnerability, whereas @code{AC_PROG_MKDIR_P} does.
3682 @end defmac
3684 @defmac AC_PROG_LEX
3685 @acindex{PROG_LEX}
3686 @ovindex LEX
3687 @ovindex LEXLIB
3688 @cvindex YYTEXT_POINTER
3689 @ovindex LEX_OUTPUT_ROOT
3690 If @code{flex} is found, set output variable @code{LEX} to @samp{flex}
3691 and @code{LEXLIB} to @option{-lfl}, if that library is in a standard
3692 place.  Otherwise set @code{LEX} to @samp{lex} and @code{LEXLIB} to
3693 @option{-ll}.
3695 Define @code{YYTEXT_POINTER} if @code{yytext} is a @samp{char *} instead
3696 of a @samp{char []}.  Also set output variable @code{LEX_OUTPUT_ROOT} to
3697 the base of the file name that the lexer generates; usually
3698 @file{lex.yy}, but sometimes something else.  These results vary
3699 according to whether @code{lex} or @code{flex} is being used.
3701 You are encouraged to use Flex in your sources, since it is both more
3702 pleasant to use than plain Lex and the C source it produces is portable.
3703 In order to ensure portability, however, you must either provide a
3704 function @code{yywrap} or, if you don't use it (e.g., your scanner has
3705 no @samp{#include}-like feature), simply include a @samp{%noyywrap}
3706 statement in the scanner's source.  Once this done, the scanner is
3707 portable (unless @emph{you} felt free to use nonportable constructs) and
3708 does not depend on any library.  In this case, and in this case only, it
3709 is suggested that you use this Autoconf snippet:
3711 @example
3712 AC_PROG_LEX
3713 if test "$LEX" != flex; then
3714   LEX="$SHELL $missing_dir/missing flex"
3715   AC_SUBST([LEX_OUTPUT_ROOT], [lex.yy])
3716   AC_SUBST([LEXLIB], [''])
3718 @end example
3720 The shell script @command{missing} can be found in the Automake
3721 distribution.
3723 To ensure backward compatibility, Automake's @code{AM_PROG_LEX} invokes
3724 (indirectly) this macro twice, which causes an annoying but benign
3725 ``@code{AC_PROG_LEX} invoked multiple times'' warning.  Future versions
3726 of Automake will fix this issue; meanwhile, just ignore this message.
3728 As part of running the test, this macro may delete any file in the
3729 configuration directory named @file{lex.yy.c} or @file{lexyy.c}.
3730 @end defmac
3732 @defmac AC_PROG_LN_S
3733 @acindex{PROG_LN_S}
3734 @ovindex LN_S
3735 If @samp{ln -s} works on the current file system (the operating system
3736 and file system support symbolic links), set the output variable
3737 @code{LN_S} to @samp{ln -s}; otherwise, if @samp{ln} works, set
3738 @code{LN_S} to @samp{ln}, and otherwise set it to @samp{cp -p}.
3740 If you make a link in a directory other than the current directory, its
3741 meaning depends on whether @samp{ln} or @samp{ln -s} is used.  To safely
3742 create links using @samp{$(LN_S)}, either find out which form is used
3743 and adjust the arguments, or always invoke @code{ln} in the directory
3744 where the link is to be created.
3746 In other words, it does not work to do:
3747 @example
3748 $(LN_S) foo /x/bar
3749 @end example
3751 Instead, do:
3753 @example
3754 (cd /x && $(LN_S) foo bar)
3755 @end example
3756 @end defmac
3758 @defmac AC_PROG_RANLIB
3759 @acindex{PROG_RANLIB}
3760 @ovindex RANLIB
3761 Set output variable @code{RANLIB} to @samp{ranlib} if @code{ranlib}
3762 is found, and otherwise to @samp{:} (do nothing).
3763 @end defmac
3765 @defmac AC_PROG_SED
3766 @acindex{PROG_SED}
3767 @ovindex SED
3768 Set output variable @code{SED} to a Sed implementation that conforms to
3769 Posix and does not have arbitrary length limits.  Report an error if no
3770 acceptable Sed is found.  @xref{Limitations of Usual Tools}, for more
3771 information about portability problems with Sed.
3772 @end defmac
3774 @defmac AC_PROG_YACC
3775 @acindex{PROG_YACC}
3776 @ovindex YACC
3777 If @code{bison} is found, set output variable @code{YACC} to @samp{bison
3778 -y}.  Otherwise, if @code{byacc} is found, set @code{YACC} to
3779 @samp{byacc}.  Otherwise set @code{YACC} to @samp{yacc}.
3780 @end defmac
3782 @node Generic Programs
3783 @subsection Generic Program and File Checks
3785 These macros are used to find programs not covered by the ``particular''
3786 test macros.  If you need to check the behavior of a program as well as
3787 find out whether it is present, you have to write your own test for it
3788 (@pxref{Writing Tests}).  By default, these macros use the environment
3789 variable @env{PATH}.  If you need to check for a program that might not
3790 be in the user's @env{PATH}, you can pass a modified path to use
3791 instead, like this:
3793 @example
3794 AC_PATH_PROG([INETD], [inetd], [/usr/libexec/inetd],
3795              [$PATH:/usr/libexec:/usr/sbin:/usr/etc:/etc])
3796 @end example
3798 You are strongly encouraged to declare the @var{variable} passed to
3799 @code{AC_CHECK_PROG} etc.@: as precious, @xref{Setting Output Variables},
3800 @code{AC_ARG_VAR}, for more details.
3802 @defmac AC_CHECK_PROG (@var{variable}, @var{prog-to-check-for}, @var{value-if-found}, @ovar{value-if-not-found}, @ovar{path},  @ovar{reject})
3803 @acindex{CHECK_PROG}
3804 Check whether program @var{prog-to-check-for} exists in @env{PATH}.  If
3805 it is found, set @var{variable} to @var{value-if-found}, otherwise to
3806 @var{value-if-not-found}, if given.  Always pass over @var{reject} (an
3807 absolute file name) even if it is the first found in the search path; in
3808 that case, set @var{variable} using the absolute file name of the
3809 @var{prog-to-check-for} found that is not @var{reject}.  If
3810 @var{variable} was already set, do nothing.  Calls @code{AC_SUBST} for
3811 @var{variable}.
3812 @end defmac
3814 @defmac AC_CHECK_PROGS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3815 @acindex{CHECK_PROGS}
3816 Check for each program in the blank-separated list
3817 @var{progs-to-check-for} existing in the @env{PATH}.  If one is found, set
3818 @var{variable} to the name of that program.  Otherwise, continue
3819 checking the next program in the list.  If none of the programs in the
3820 list are found, set @var{variable} to @var{value-if-not-found}; if
3821 @var{value-if-not-found} is not specified, the value of @var{variable}
3822 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3823 @end defmac
3825 @defmac AC_CHECK_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3826 @acindex{CHECK_TARGET_TOOL}
3827 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3828 with a prefix of the target type as determined by
3829 @code{AC_CANONICAL_TARGET}, followed by a dash (@pxref{Canonicalizing}).
3830 If the tool cannot be found with a prefix, and if the build and target
3831 types are equal, then it is also searched for without a prefix.
3833 As noted in @ref{Specifying Names, , Specifying the system type}, the
3834 target is rarely specified, because most of the time it is the same
3835 as the host: it is the type of system for which any compiler tool in
3836 the package produces code.  What this macro looks for is,
3837 for example, @emph{a tool @r{(assembler, linker, etc.)}@: that the
3838 compiler driver @r{(@command{gcc} for the @acronym{GNU} C Compiler)}
3839 uses to produce objects, archives or executables}.
3840 @end defmac
3842 @defmac AC_CHECK_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3843 @acindex{CHECK_TOOL}
3844 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3845 with a prefix of the host type as determined by
3846 @code{AC_CANONICAL_HOST}, followed by a dash (@pxref{Canonicalizing}).
3847 For example, if the user runs @samp{configure --host=i386-gnu}, then
3848 this call:
3849 @example
3850 AC_CHECK_TOOL([RANLIB], [ranlib], [:])
3851 @end example
3852 @noindent
3853 sets @code{RANLIB} to @file{i386-gnu-ranlib} if that program exists in
3854 @env{PATH}, or otherwise to @samp{ranlib} if that program exists in
3855 @env{PATH}, or to @samp{:} if neither program exists.
3857 In the future, when cross-compiling this macro will @emph{only}
3858 accept program names that are prefixed with the host type.
3859 For more information, see @ref{Specifying Names, , Specifying the
3860 system type}.
3861 @end defmac
3863 @defmac AC_CHECK_TARGET_TOOLS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3864 @acindex{CHECK_TARGET_TOOLS}
3865 Like @code{AC_CHECK_TARGET_TOOL}, each of the tools in the list
3866 @var{progs-to-check-for} are checked with a prefix of the target type as
3867 determined by @code{AC_CANONICAL_TARGET}, followed by a dash
3868 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3869 prefix, and if the build and target types are equal, then the first one
3870 without a prefix is used.  If a tool is found, set @var{variable} to
3871 the name of that program.  If none of the tools in the list are found,
3872 set @var{variable} to @var{value-if-not-found}; if @var{value-if-not-found}
3873 is not specified, the value of @var{variable} is not changed.  Calls
3874 @code{AC_SUBST} for @var{variable}.
3875 @end defmac
3877 @defmac AC_CHECK_TOOLS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3878 @acindex{CHECK_TOOLS}
3879 Like @code{AC_CHECK_TOOL}, each of the tools in the list
3880 @var{progs-to-check-for} are checked with a prefix of the host type as
3881 determined by @code{AC_CANONICAL_HOST}, followed by a dash
3882 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3883 prefix, then the first one without a prefix is used.  If a tool is found,
3884 set @var{variable} to the name of that program.  If none of the tools in
3885 the list are found, set @var{variable} to @var{value-if-not-found}; if
3886 @var{value-if-not-found} is not specified, the value of @var{variable}
3887 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3889 In the future, when cross-compiling this macro will @emph{not}
3890 accept program names that are not prefixed with the host type.
3891 @end defmac
3893 @defmac AC_PATH_PROG (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3894 @acindex{PATH_PROG}
3895 Like @code{AC_CHECK_PROG}, but set @var{variable} to the absolute
3896 name of @var{prog-to-check-for} if found.
3897 @end defmac
3899 @defmac AC_PATH_PROGS (@var{variable}, @var{progs-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3900 @acindex{PATH_PROGS}
3901 Like @code{AC_CHECK_PROGS}, but if any of @var{progs-to-check-for}
3902 are found, set @var{variable} to the absolute name of the program
3903 found.
3904 @end defmac
3906 @defmac AC_PATH_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3907 @acindex{PATH_TARGET_TOOL}
3908 Like @code{AC_CHECK_TARGET_TOOL}, but set @var{variable} to the absolute
3909 name of the program if it is found.
3910 @end defmac
3912 @defmac AC_PATH_TOOL (@var{variable}, @var{prog-to-check-for}, @ovar{value-if-not-found}, @ovar{path})
3913 @acindex{PATH_TOOL}
3914 Like @code{AC_CHECK_TOOL}, but set @var{variable} to the absolute
3915 name of the program if it is found.
3917 In the future, when cross-compiling this macro will @emph{not}
3918 accept program names that are not prefixed with the host type.
3919 @end defmac
3922 @node Files
3923 @section Files
3924 @cindex File, checking
3926 You might also need to check for the existence of files.  Before using
3927 these macros, ask yourself whether a runtime test might not be a better
3928 solution.  Be aware that, like most Autoconf macros, they test a feature
3929 of the host machine, and therefore, they die when cross-compiling.
3931 @defmac AC_CHECK_FILE (@var{file}, @ovar{action-if-found}, @ovar{action-if-not-found})
3932 @acindex{CHECK_FILE}
3933 Check whether file @var{file} exists on the native system.  If it is
3934 found, execute @var{action-if-found}, otherwise do
3935 @var{action-if-not-found}, if given.
3936 @end defmac
3938 @defmac AC_CHECK_FILES (@var{files}, @ovar{action-if-found}, @ovar{action-if-not-found})
3939 @acindex{CHECK_FILES}
3940 Executes @code{AC_CHECK_FILE} once for each file listed in @var{files}.
3941 Additionally, defines @samp{HAVE_@var{file}} (@pxref{Standard Symbols})
3942 for each file found.
3943 @end defmac
3946 @node Libraries
3947 @section Library Files
3948 @cindex Library, checking
3950 The following macros check for the presence of certain C, C++, or Fortran
3951 library archive files.
3953 @defmac AC_CHECK_LIB (@var{library}, @var{function}, @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
3954 @acindex{CHECK_LIB}
3955 Test whether the library @var{library} is available by trying to link
3956 a test program that calls function @var{function} with the library.
3957 @var{function} should be a function provided by the library.
3958 Use the base
3959 name of the library; e.g., to check for @option{-lmp}, use @samp{mp} as
3960 the @var{library} argument.
3962 @var{action-if-found} is a list of shell commands to run if the link
3963 with the library succeeds; @var{action-if-not-found} is a list of shell
3964 commands to run if the link fails.  If @var{action-if-found} is not
3965 specified, the default action prepends @option{-l@var{library}} to
3966 @code{LIBS} and defines @samp{HAVE_LIB@var{library}} (in all
3967 capitals).  This macro is intended to support building @code{LIBS} in
3968 a right-to-left (least-dependent to most-dependent) fashion such that
3969 library dependencies are satisfied as a natural side effect of
3970 consecutive tests.  Linkers are sensitive to library ordering
3971 so the order in which @code{LIBS} is generated is important to reliable
3972 detection of libraries.
3974 If linking with @var{library} results in unresolved symbols that would
3975 be resolved by linking with additional libraries, give those libraries
3976 as the @var{other-libraries} argument, separated by spaces:
3977 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
3978 that @var{library} is present, because linking the test program
3979 always fails with unresolved symbols.  The @var{other-libraries} argument
3980 should be limited to cases where it is desirable to test for one library
3981 in the presence of another that is not already in @code{LIBS}.
3983 @code{AC_CHECK_LIB} requires some care in usage, and should be avoided
3984 in some common cases.  Many standard functions like @code{gethostbyname}
3985 appear the standard C library on some hosts, and in special libraries
3986 like @code{nsl} on other hosts.  On some hosts the special libraries
3987 contain variant implementations that you may not want to use.  These
3988 days it is normally better to use @code{AC_SEARCH_LIBS([gethostbyname],
3989 [nsl])} instead of @code{AC_CHECK_LIB([nsl], [gethostbyname])}.
3990 @end defmac
3993 @defmac AC_SEARCH_LIBS (@var{function}, @var{search-libs}, @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
3994 @acindex{SEARCH_LIBS}
3995 Search for a library defining @var{function} if it's not already
3996 available.  This equates to calling
3997 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])])} first with
3998 no libraries, then for each library listed in @var{search-libs}.
4000 Add @option{-l@var{library}} to @code{LIBS} for the first library found
4001 to contain @var{function}, and run @var{action-if-found}.  If the
4002 function is not found, run @var{action-if-not-found}.
4004 If linking with @var{library} results in unresolved symbols that would
4005 be resolved by linking with additional libraries, give those libraries
4006 as the @var{other-libraries} argument, separated by spaces:
4007 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4008 that @var{function} is present, because linking the test program
4009 always fails with unresolved symbols.
4010 @end defmac
4014 @node Library Functions
4015 @section Library Functions
4017 The following macros check for particular C library functions.
4018 If there is no macro specifically defined to check for a function you need,
4019 and you don't need to check for any special properties of
4020 it, then you can use one of the general function-check macros.
4022 @menu
4023 * Function Portability::        Pitfalls with usual functions
4024 * Particular Functions::        Special handling to find certain functions
4025 * Generic Functions::           How to find other functions
4026 @end menu
4028 @node Function Portability
4029 @subsection Portability of C Functions
4030 @cindex Portability of C functions
4031 @cindex C function portability
4033 Most usual functions can either be missing, or be buggy, or be limited
4034 on some architectures.  This section tries to make an inventory of these
4035 portability issues.  By definition, this list always requires
4036 additions.  Please help us keeping it as complete as possible.
4038 @table @asis
4039 @item @code{exit}
4040 @c @fuindex exit
4041 @prindex @code{exit}
4042 On ancient hosts, @code{exit} returned @code{int}.
4043 This is because @code{exit} predates @code{void}, and there was a long
4044 tradition of it returning @code{int}.
4046 On current hosts, the problem more likely is that @code{exit} is not
4047 declared, due to C++ problems of some sort or another.  For this reason
4048 we suggest that test programs not invoke @code{exit}, but return from
4049 @code{main} instead.
4051 @item @code{free}
4052 @c @fuindex free
4053 @prindex @code{free}
4054 The C standard says a call @code{free (NULL)} does nothing, but
4055 some old systems don't support this (e.g., NextStep).
4057 @item @code{isinf}
4058 @itemx @code{isnan}
4059 @c @fuindex isinf
4060 @c @fuindex isnan
4061 @prindex @code{isinf}
4062 @prindex @code{isnan}
4063 The C99 standard says that @code{isinf} and @code{isnan} are
4064 macros.  On some systems just macros are available
4065 (e.g., @acronym{HP-UX} and Solaris 10), on
4066 some systems both macros and functions (e.g., glibc 2.3.2), and on some
4067 systems only functions (e.g., IRIX 6 and Solaris 9).  In some cases
4068 these functions are declared in nonstandard headers like
4069 @code{<sunmath.h>} and defined in non-default libraries like
4070 @option{-lm} or @option{-lsunmath}.
4072 The C99 @code{isinf} and @code{isnan} macros work correctly with
4073 @code{long double} arguments, but pre-C99 systems that use functions
4074 typically assume @code{double} arguments.  On such a system,
4075 @code{isinf} incorrectly returns true for a finite @code{long double}
4076 argument that is outside the range of @code{double}.
4078 To work around this porting mess, you can use code like the following.
4080 @smallexample
4081 #include <math.h>
4083 #ifndef isnan
4084 # define isnan(x) \
4085     (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
4086      : sizeof (x) == sizeof (double) ? isnan_d (x) \
4087      : isnan_f (x))
4088 static inline int isnan_f  (float       x) @{ return x != x; @}
4089 static inline int isnan_d  (double      x) @{ return x != x; @}
4090 static inline int isnan_ld (long double x) @{ return x != x; @}
4091 #endif
4093 #ifndef isinf
4094 # define isinf(x) \
4095     (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
4096      : sizeof (x) == sizeof (double) ? isinf_d (x) \
4097      : isinf_f (x))
4098 static inline int isinf_f  (float       x) @{ return isnan (x - x); @}
4099 static inline int isinf_d  (double      x) @{ return isnan (x - x); @}
4100 static inline int isinf_ld (long double x) @{ return isnan (x - x); @}
4101 #endif
4102 @end smallexample
4104 Use @code{AC_C_INLINE} (@pxref{C Compiler}) so that this code works on
4105 compilers that lack the @code{inline} keyword.  Some optimizing
4106 compilers mishandle these definitions, but systems with that bug
4107 typically have missing or broken @code{isnan} functions anyway, so it's
4108 probably not worth worrying about.
4110 @item @code{malloc}
4111 @c @fuindex malloc
4112 @prindex @code{malloc}
4113 The C standard says a call @code{malloc (0)} is implementation
4114 dependent.  It can return either @code{NULL} or a new non-null pointer.
4115 The latter is more common (e.g., the @acronym{GNU} C Library) but is by
4116 no means universal.  @code{AC_FUNC_MALLOC}
4117 can be used to insist on non-@code{NULL} (@pxref{Particular Functions}).
4119 @item @code{putenv}
4120 @c @fuindex putenv
4121 @prindex @code{putenv}
4122 Posix prefers @code{setenv} to @code{putenv}; among other things,
4123 @code{putenv} is not required of all Posix implementations, but
4124 @code{setenv} is.
4126 Posix specifies that @code{putenv} puts the given string directly in
4127 @code{environ}, but some systems make a copy of it instead (e.g.,
4128 glibc 2.0, or @acronym{BSD}).  And when a copy is made, @code{unsetenv} might
4129 not free it, causing a memory leak (e.g., Free@acronym{BSD} 4).
4131 On some systems @code{putenv ("FOO")} removes @samp{FOO} from the
4132 environment, but this is not standard usage and it dumps core
4133 on some systems (e.g., AIX).
4135 On MinGW, a call @code{putenv ("FOO=")} removes @samp{FOO} from the
4136 environment, rather than inserting it with an empty value.
4138 @item @code{realloc}
4139 @c @fuindex realloc
4140 @prindex @code{realloc}
4141 The C standard says a call @code{realloc (NULL, size)} is equivalent
4142 to @code{malloc (size)}, but some old systems don't support this (e.g.,
4143 NextStep).
4145 @item @code{signal} handler
4146 @c @fuindex signal
4147 @prindex @code{signal}
4148 Normally @code{signal} takes a handler function with a return type of
4149 @code{void}, but some old systems required @code{int} instead.  Any
4150 actual @code{int} value returned is not used; this is only a
4151 difference in the function prototype demanded.
4153 All systems we know of in current use return @code{void}.  The
4154 @code{int} was to support K&R C, where of course @code{void} is not
4155 available.  @code{AC_TYPE_SIGNAL} (@pxref{Particular Types}) can be
4156 used to establish the correct type in all cases.
4158 @item @code{snprintf}
4159 @c @fuindex snprintf
4160 @prindex @code{snprintf}
4161 @c @fuindex vsnprintf
4162 @prindex @code{vsnprintf}
4163 The C99 standard says that if the output array isn't big enough
4164 and if no other errors occur, @code{snprintf} and @code{vsnprintf}
4165 truncate the output and return the number of bytes that ought to have
4166 been produced.  Some older systems return the truncated length (e.g.,
4167 @acronym{GNU} C Library 2.0.x or @sc{irix} 6.5), some a negative value
4168 (e.g., earlier @acronym{GNU} C Library versions), and some the buffer
4169 length without truncation (e.g., 32-bit Solaris 7).  Also, some buggy
4170 older systems ignore the length and overrun the buffer (e.g., 64-bit
4171 Solaris 7).
4173 @item @code{sprintf}
4174 @c @fuindex sprintf
4175 @prindex @code{sprintf}
4176 @c @fuindex vsprintf
4177 @prindex @code{vsprintf}
4178 The C standard says @code{sprintf} and @code{vsprintf} return the
4179 number of bytes written.  On some ancient systems (SunOS 4 for
4180 instance) they return the buffer pointer instead, but these no
4181 longer need to be worried about.
4183 @item @code{sscanf}
4184 @c @fuindex sscanf
4185 @prindex @code{sscanf}
4186 On various old systems, e.g., @acronym{HP-UX} 9, @code{sscanf} requires that its
4187 input string be writable (though it doesn't actually change it).  This
4188 can be a problem when using @command{gcc} since it normally puts
4189 constant strings in read-only memory (@pxref{Incompatibilities,
4190 Incompatibilities of @acronym{GCC}, , gcc, Using and
4191 Porting the @acronym{GNU} Compiler Collection}).  Apparently in some cases even
4192 having format strings read-only can be a problem.
4194 @item @code{strerror_r}
4195 @c @fuindex strerror_r
4196 @prindex @code{strerror_r}
4197 Posix specifies that @code{strerror_r} returns an @code{int}, but many
4198 systems (e.g., @acronym{GNU} C Library version 2.2.4) provide a
4199 different version returning a @code{char *}.  @code{AC_FUNC_STRERROR_R}
4200 can detect which is in use (@pxref{Particular Functions}).
4202 @item @code{strnlen}
4203 @c @fuindex strnlen
4204 @prindex @code{strnlen}
4205 @acronym{AIX} 4.3 provides a broken version which produces the
4206 following results:
4208 @example
4209 strnlen ("foobar", 0) = 0
4210 strnlen ("foobar", 1) = 3
4211 strnlen ("foobar", 2) = 2
4212 strnlen ("foobar", 3) = 1
4213 strnlen ("foobar", 4) = 0
4214 strnlen ("foobar", 5) = 6
4215 strnlen ("foobar", 6) = 6
4216 strnlen ("foobar", 7) = 6
4217 strnlen ("foobar", 8) = 6
4218 strnlen ("foobar", 9) = 6
4219 @end example
4221 @item @code{sysconf}
4222 @c @fuindex sysconf
4223 @prindex @code{sysconf}
4224 @code{_SC_PAGESIZE} is standard, but some older systems (e.g., @acronym{HP-UX}
4225 9) have @code{_SC_PAGE_SIZE} instead.  This can be tested with
4226 @code{#ifdef}.
4228 @item @code{unlink}
4229 @c @fuindex unlink
4230 @prindex @code{unlink}
4231 The Posix spec says that @code{unlink} causes the given file to be
4232 removed only after there are no more open file handles for it.  Some
4233 non-Posix hosts have trouble with this requirement, though,
4234 and some @acronym{DOS} variants even corrupt the file system.
4236 @item @code{unsetenv}
4237 @c @fuindex unsetenv
4238 @prindex @code{unsetenv}
4239 On MinGW, @code{unsetenv} is not available, but a variable @samp{FOO}
4240 can be removed with a call @code{putenv ("FOO=")}, as described under
4241 @code{putenv} above.
4243 @item @code{va_copy}
4244 @c @fuindex va_copy
4245 @prindex @code{va_copy}
4246 The C99 standard provides @code{va_copy} for copying
4247 @code{va_list} variables.  It may be available in older environments
4248 too, though possibly as @code{__va_copy} (e.g., @command{gcc} in strict
4249 pre-C99 mode).  These can be tested with @code{#ifdef}.  A fallback to
4250 @code{memcpy (&dst, &src, sizeof (va_list))} gives maximum
4251 portability.
4253 @item @code{va_list}
4254 @c @fuindex va_list
4255 @prindex @code{va_list}
4256 @code{va_list} is not necessarily just a pointer.  It can be a
4257 @code{struct} (e.g., @command{gcc} on Alpha), which means @code{NULL} is
4258 not portable.  Or it can be an array (e.g., @command{gcc} in some
4259 PowerPC configurations), which means as a function parameter it can be
4260 effectively call-by-reference and library routines might modify the
4261 value back in the caller (e.g., @code{vsnprintf} in the @acronym{GNU} C Library
4262 2.1).
4264 @item Signed @code{>>}
4265 Normally the C @code{>>} right shift of a signed type replicates the
4266 high bit, giving a so-called ``arithmetic'' shift.  But care should be
4267 taken since Standard C doesn't require that behavior.  On those
4268 few processors without a native arithmetic shift (for instance Cray
4269 vector systems) zero bits may be shifted in, the same as a shift of an
4270 unsigned type.
4272 @item Integer @code{/}
4273 C divides signed integers by truncating their quotient toward zero,
4274 yielding the same result as Fortran.  However, before C99 the standard
4275 allowed C implementations to take the floor or ceiling of the quotient
4276 in some cases.  Hardly any implementations took advantage of this
4277 freedom, though, and it's probably not worth worrying about this issue
4278 nowadays.
4279 @end table
4282 @node Particular Functions
4283 @subsection Particular Function Checks
4284 @cindex Function, checking
4286 These macros check for particular C functions---whether they exist, and
4287 in some cases how they respond when given certain arguments.
4289 @defmac AC_FUNC_ALLOCA
4290 @acindex{FUNC_ALLOCA}
4291 @cvindex C_ALLOCA
4292 @cvindex HAVE_ALLOCA_H
4293 @ovindex ALLOCA
4294 @c @fuindex alloca
4295 @prindex @code{alloca}
4296 @hdrindex{alloca.h}
4297 Check how to get @code{alloca}.  Tries to get a builtin version by
4298 checking for @file{alloca.h} or the predefined C preprocessor macros
4299 @code{__GNUC__} and @code{_AIX}.  If this macro finds @file{alloca.h},
4300 it defines @code{HAVE_ALLOCA_H}.
4302 If those attempts fail, it looks for the function in the standard C
4303 library.  If any of those methods succeed, it defines
4304 @code{HAVE_ALLOCA}.  Otherwise, it sets the output variable
4305 @code{ALLOCA} to @samp{$@{LIBOBJDIR@}alloca.o} and defines
4306 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
4307 garbage collect).  This variable is separate from @code{LIBOBJS} so
4308 multiple programs can share the value of @code{ALLOCA} without needing
4309 to create an actual library, in case only some of them use the code in
4310 @code{LIBOBJS}.  The @samp{$@{LIBOBJDIR@}} prefix serves the same
4311 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
4313 This macro does not try to get @code{alloca} from the System V R3
4314 @file{libPW} or the System V R4 @file{libucb} because those libraries
4315 contain some incompatible functions that cause trouble.  Some versions
4316 do not even contain @code{alloca} or contain a buggy version.  If you
4317 still want to use their @code{alloca}, use @code{ar} to extract
4318 @file{alloca.o} from them instead of compiling @file{alloca.c}.
4320 Source files that use @code{alloca} should start with a piece of code
4321 like the following, to declare it properly.
4323 @example
4324 @group
4325 #if HAVE_ALLOCA_H
4326 # include <alloca.h>
4327 #elif defined __GNUC__
4328 # define alloca __builtin_alloca
4329 #elif defined _AIX
4330 # define alloca __alloca
4331 #elif defined _MSC_VER
4332 # include <malloc.h>
4333 # define alloca _alloca
4334 #else
4335 # include <stddef.h>
4336 # ifdef  __cplusplus
4337 extern "C"
4338 # endif
4339 void *alloca (size_t);
4340 #endif
4341 @end group
4342 @end example
4343 @end defmac
4345 @defmac AC_FUNC_CHOWN
4346 @acindex{FUNC_CHOWN}
4347 @c @fuindex chown
4348 @prindex @code{chown}
4349 If the @code{chown} function is available and works (in particular, it
4350 should accept @option{-1} for @code{uid} and @code{gid}), define
4351 @code{HAVE_CHOWN}.
4352 @end defmac
4355 @defmac AC_FUNC_CLOSEDIR_VOID
4356 @acindex{FUNC_CLOSEDIR_VOID}
4357 @cvindex CLOSEDIR_VOID
4358 @c @fuindex closedir
4359 @prindex @code{closedir}
4360 If the @code{closedir} function does not return a meaningful value,
4361 define @code{CLOSEDIR_VOID}.  Otherwise, callers ought to check its
4362 return value for an error indicator.
4364 Currently this test is implemented by running a test program.  When
4365 cross compiling the pessimistic assumption that @code{closedir} does not
4366 return a meaningful value is made.
4368 This macro is obsolescent, as @code{closedir} returns a meaningful value
4369 on current systems.  New programs need not use this macro.
4370 @end defmac
4372 @defmac AC_FUNC_ERROR_AT_LINE
4373 @acindex{FUNC_ERROR_AT_LINE}
4374 @c @fuindex error_at_line
4375 @prindex @code{error_at_line}
4376 If the @code{error_at_line} function is not found, require an
4377 @code{AC_LIBOBJ} replacement of @samp{error}.
4378 @end defmac
4380 @defmac AC_FUNC_FNMATCH
4381 @acindex{FUNC_FNMATCH}
4382 @c @fuindex fnmatch
4383 @prindex @code{fnmatch}
4384 If the @code{fnmatch} function conforms to Posix, define
4385 @code{HAVE_FNMATCH}.  Detect common implementation bugs, for example,
4386 the bugs in Solaris 2.4.
4388 Unlike the other specific
4389 @code{AC_FUNC} macros, @code{AC_FUNC_FNMATCH} does not replace a
4390 broken/missing @code{fnmatch}.  This is for historical reasons.
4391 See @code{AC_REPLACE_FNMATCH} below.
4392 @end defmac
4394 @defmac AC_FUNC_FNMATCH_GNU
4395 @acindex{FUNC_FNMATCH_GNU}
4396 @c @fuindex fnmatch
4397 @prindex @code{fnmatch}
4398 Behave like @code{AC_REPLACE_FNMATCH} (@emph{replace}) but also test
4399 whether @code{fnmatch} supports @acronym{GNU} extensions.  Detect common
4400 implementation bugs, for example, the bugs in the @acronym{GNU} C
4401 Library 2.1.
4402 @end defmac
4404 @defmac AC_FUNC_FORK
4405 @acindex{FUNC_FORK}
4406 @cvindex HAVE_VFORK_H
4407 @cvindex HAVE_WORKING_FORK
4408 @cvindex HAVE_WORKING_VFORK
4409 @cvindex vfork
4410 @c @fuindex fork
4411 @prindex @code{fork}
4412 @c @fuindex vfork
4413 @prindex @code{vfork}
4414 @hdrindex{vfork.h}
4415 This macro checks for the @code{fork} and @code{vfork} functions.  If a
4416 working @code{fork} is found, define @code{HAVE_WORKING_FORK}.  This macro
4417 checks whether @code{fork} is just a stub by trying to run it.
4419 If @file{vfork.h} is found, define @code{HAVE_VFORK_H}.  If a working
4420 @code{vfork} is found, define @code{HAVE_WORKING_VFORK}.  Otherwise,
4421 define @code{vfork} to be @code{fork} for backward compatibility with
4422 previous versions of @command{autoconf}.  This macro checks for several known
4423 errors in implementations of @code{vfork} and considers the system to not
4424 have a working @code{vfork} if it detects any of them.  It is not considered
4425 to be an implementation error if a child's invocation of @code{signal}
4426 modifies the parent's signal handler, since child processes rarely change
4427 their signal handlers.
4429 Since this macro defines @code{vfork} only for backward compatibility with
4430 previous versions of @command{autoconf} you're encouraged to define it
4431 yourself in new code:
4432 @example
4433 @group
4434 #if !HAVE_WORKING_VFORK
4435 # define vfork fork
4436 #endif
4437 @end group
4438 @end example
4439 @end defmac
4441 @defmac AC_FUNC_FSEEKO
4442 @acindex{FUNC_FSEEKO}
4443 @cvindex _LARGEFILE_SOURCE
4444 @c @fuindex fseeko
4445 @prindex @code{fseeko}
4446 If the @code{fseeko} function is available, define @code{HAVE_FSEEKO}.
4447 Define @code{_LARGEFILE_SOURCE} if necessary to make the prototype
4448 visible on some systems (e.g., glibc 2.2).  Otherwise linkage problems
4449 may occur when compiling with @code{AC_SYS_LARGEFILE} on
4450 largefile-sensitive systems where @code{off_t} does not default to a
4451 64bit entity.
4452 @end defmac
4454 @defmac AC_FUNC_GETGROUPS
4455 @acindex{FUNC_GETGROUPS}
4456 @ovindex GETGROUPS_LIBS
4457 @c @fuindex getgroups
4458 @prindex @code{getgroups}
4459 If the @code{getgroups} function is available and works (unlike on
4460 Ultrix 4.3, where @samp{getgroups (0, 0)} always fails), define
4461 @code{HAVE_GETGROUPS}.  Set @code{GETGROUPS_LIBS} to any libraries
4462 needed to get that function.  This macro runs @code{AC_TYPE_GETGROUPS}.
4463 @end defmac
4465 @defmac AC_FUNC_GETLOADAVG
4466 @acindex{FUNC_GETLOADAVG}
4467 @cvindex SVR4
4468 @cvindex DGUX
4469 @cvindex UMAX
4470 @cvindex UMAX4_3
4471 @cvindex HAVE_NLIST_H
4472 @cvindex NLIST_NAME_UNION
4473 @cvindex GETLODAVG_PRIVILEGED
4474 @cvindex NEED_SETGID
4475 @cvindex C_GETLOADAVG
4476 @ovindex LIBOBJS
4477 @ovindex NEED_SETGID
4478 @ovindex KMEM_GROUP
4479 @ovindex GETLOADAVG_LIBS
4480 @c @fuindex getloadavg
4481 @prindex @code{getloadavg}
4482 Check how to get the system load averages.  To perform its tests
4483 properly, this macro needs the file @file{getloadavg.c}; therefore, be
4484 sure to set the @code{AC_LIBOBJ} replacement directory properly (see
4485 @ref{Generic Functions}, @code{AC_CONFIG_LIBOBJ_DIR}).
4487 If the system has the @code{getloadavg} function, define
4488 @code{HAVE_GETLOADAVG}, and set @code{GETLOADAVG_LIBS} to any libraries
4489 necessary to get that function.  Also add @code{GETLOADAVG_LIBS} to
4490 @code{LIBS}.  Otherwise, require an @code{AC_LIBOBJ} replacement for
4491 @samp{getloadavg} with source code in @file{@var{dir}/getloadavg.c}, and
4492 possibly define several other C preprocessor macros and output
4493 variables:
4495 @enumerate
4496 @item
4497 Define @code{C_GETLOADAVG}.
4499 @item
4500 Define @code{SVR4}, @code{DGUX}, @code{UMAX}, or @code{UMAX4_3} if on
4501 those systems.
4503 @item
4504 @hdrindex{nlist.h}
4505 If @file{nlist.h} is found, define @code{HAVE_NLIST_H}.
4507 @item
4508 If @samp{struct nlist} has an @samp{n_un.n_name} member, define
4509 @code{HAVE_STRUCT_NLIST_N_UN_N_NAME}.  The obsolete symbol
4510 @code{NLIST_NAME_UNION} is still defined, but do not depend upon it.
4512 @item
4513 Programs may need to be installed set-group-ID (or set-user-ID) for
4514 @code{getloadavg} to work.  In this case, define
4515 @code{GETLOADAVG_PRIVILEGED}, set the output variable @code{NEED_SETGID}
4516 to @samp{true} (and otherwise to @samp{false}), and set
4517 @code{KMEM_GROUP} to the name of the group that should own the installed
4518 program.
4519 @end enumerate
4520 @end defmac
4522 @defmac AC_FUNC_GETMNTENT
4523 @acindex{FUNC_GETMNTENT}
4524 @cvindex HAVE_GETMNTENT
4525 @c @fuindex getmntent
4526 @prindex @code{getmntent}
4527 Check for @code{getmntent} in the standard C library, and then in the
4528 @file{sun}, @file{seq}, and @file{gen} libraries, for @sc{unicos},
4529 @sc{irix} 4, @sc{ptx}, and UnixWare, respectively.  Then, if
4530 @code{getmntent} is available, define @code{HAVE_GETMNTENT}.
4531 @end defmac
4533 @defmac AC_FUNC_GETPGRP
4534 @acindex{FUNC_GETPGRP}
4535 @cvindex GETPGRP_VOID
4536 @c @fuindex getpgid
4537 @c @fuindex getpgrp
4538 @prindex @code{getpgid}
4539 @prindex @code{getpgrp}
4540 Define @code{GETPGRP_VOID} if it is an error to pass 0 to
4541 @code{getpgrp}; this is the Posix behavior.  On older @acronym{BSD}
4542 systems, you must pass 0 to @code{getpgrp}, as it takes an argument and
4543 behaves like Posix's @code{getpgid}.
4545 @example
4546 #if GETPGRP_VOID
4547   pid = getpgrp ();
4548 #else
4549   pid = getpgrp (0);
4550 #endif
4551 @end example
4553 This macro does not check whether
4554 @code{getpgrp} exists at all; if you need to work in that situation,
4555 first call @code{AC_CHECK_FUNC} for @code{getpgrp}.
4557 This macro is obsolescent, as current systems have a @code{getpgrp}
4558 whose signature conforms to Posix.  New programs need not use this macro.
4559 @end defmac
4561 @defmac AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
4562 @acindex{FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK}
4563 @cvindex LSTAT_FOLLOWS_SLASHED_SYMLINK
4564 @c @fuindex lstat
4565 @prindex @code{lstat}
4566 If @file{link} is a symbolic link, then @code{lstat} should treat
4567 @file{link/} the same as @file{link/.}.  However, many older
4568 @code{lstat} implementations incorrectly ignore trailing slashes.
4570 It is safe to assume that if @code{lstat} incorrectly ignores
4571 trailing slashes, then other symbolic-link-aware functions like
4572 @code{unlink} also incorrectly ignore trailing slashes.
4574 If @code{lstat} behaves properly, define
4575 @code{LSTAT_FOLLOWS_SLASHED_SYMLINK}, otherwise require an
4576 @code{AC_LIBOBJ} replacement of @code{lstat}.
4577 @end defmac
4579 @defmac AC_FUNC_MALLOC
4580 @acindex{FUNC_MALLOC}
4581 @cvindex HAVE_MALLOC
4582 @cvindex malloc
4583 @c @fuindex malloc
4584 @prindex @code{malloc}
4585 If the @code{malloc} function is compatible with the @acronym{GNU} C
4586 library @code{malloc} (i.e., @samp{malloc (0)} returns a valid
4587 pointer), define @code{HAVE_MALLOC} to 1.  Otherwise define
4588 @code{HAVE_MALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4589 @samp{malloc}, and define @code{malloc} to @code{rpl_malloc} so that the
4590 native @code{malloc} is not used in the main project.
4592 Typically, the replacement file @file{malloc.c} should look like (note
4593 the @samp{#undef malloc}):
4595 @verbatim
4596 #if HAVE_CONFIG_H
4597 # include <config.h>
4598 #endif
4599 #undef malloc
4601 #include <sys/types.h>
4603 void *malloc ();
4605 /* Allocate an N-byte block of memory from the heap.
4606    If N is zero, allocate a 1-byte block.  */
4608 void *
4609 rpl_malloc (size_t n)
4611   if (n == 0)
4612     n = 1;
4613   return malloc (n);
4615 @end verbatim
4616 @end defmac
4618 @defmac AC_FUNC_MEMCMP
4619 @acindex{FUNC_MEMCMP}
4620 @ovindex LIBOBJS
4621 @c @fuindex memcmp
4622 @prindex @code{memcmp}
4623 If the @code{memcmp} function is not available, or does not work on
4624 8-bit data (like the one on SunOS 4.1.3), or fails when comparing 16
4625 bytes or more and with at least one buffer not starting on a 4-byte
4626 boundary (such as the one on NeXT x86 OpenStep), require an
4627 @code{AC_LIBOBJ} replacement for @samp{memcmp}.
4629 This macro is obsolescent, as current systems have a working
4630 @code{memcmp}.  New programs need not use this macro.
4631 @end defmac
4633 @defmac AC_FUNC_MBRTOWC
4634 @acindex{FUNC_MBRTOWC}
4635 @cvindex HAVE_MBRTOWC
4636 @c @fuindex mbrtowc
4637 @prindex @code{mbrtowc}
4638 Define @code{HAVE_MBRTOWC} to 1 if the function @code{mbrtowc} and the
4639 type @code{mbstate_t} are properly declared.
4640 @end defmac
4642 @defmac AC_FUNC_MKTIME
4643 @acindex{FUNC_MKTIME}
4644 @ovindex LIBOBJS
4645 @c @fuindex mktime
4646 @prindex @code{mktime}
4647 If the @code{mktime} function is not available, or does not work
4648 correctly, require an @code{AC_LIBOBJ} replacement for @samp{mktime}.
4649 For the purposes of this test, @code{mktime} should conform to the
4650 Posix standard and should be the inverse of
4651 @code{localtime}.
4652 @end defmac
4654 @defmac AC_FUNC_MMAP
4655 @acindex{FUNC_MMAP}
4656 @cvindex HAVE_MMAP
4657 @c @fuindex mmap
4658 @prindex @code{mmap}
4659 If the @code{mmap} function exists and works correctly, define
4660 @code{HAVE_MMAP}.  This checks only private fixed mapping of already-mapped
4661 memory.
4662 @end defmac
4664 @defmac AC_FUNC_OBSTACK
4665 @acindex{FUNC_OBSTACK}
4666 @cvindex HAVE_OBSTACK
4667 @cindex obstack
4668 If the obstacks are found, define @code{HAVE_OBSTACK}, else require an
4669 @code{AC_LIBOBJ} replacement for @samp{obstack}.
4670 @end defmac
4672 @defmac AC_FUNC_REALLOC
4673 @acindex{FUNC_REALLOC}
4674 @cvindex HAVE_REALLOC
4675 @cvindex realloc
4676 @c @fuindex realloc
4677 @prindex @code{realloc}
4678 If the @code{realloc} function is compatible with the @acronym{GNU} C
4679 library @code{realloc} (i.e., @samp{realloc (NULL, 0)} returns a
4680 valid pointer), define @code{HAVE_REALLOC} to 1.  Otherwise define
4681 @code{HAVE_REALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4682 @samp{realloc}, and define @code{realloc} to @code{rpl_realloc} so that
4683 the native @code{realloc} is not used in the main project.  See
4684 @code{AC_FUNC_MALLOC} for details.
4685 @end defmac
4687 @defmac AC_FUNC_SELECT_ARGTYPES
4688 @acindex{FUNC_SELECT_ARGTYPES}
4689 @cvindex SELECT_TYPE_ARG1
4690 @cvindex SELECT_TYPE_ARG234
4691 @cvindex SELECT_TYPE_ARG5
4692 @c @fuindex select
4693 @prindex @code{select}
4694 Determines the correct type to be passed for each of the
4695 @code{select} function's arguments, and defines those types
4696 in @code{SELECT_TYPE_ARG1}, @code{SELECT_TYPE_ARG234}, and
4697 @code{SELECT_TYPE_ARG5} respectively.  @code{SELECT_TYPE_ARG1} defaults
4698 to @samp{int}, @code{SELECT_TYPE_ARG234} defaults to @samp{int *},
4699 and @code{SELECT_TYPE_ARG5} defaults to @samp{struct timeval *}.
4701 This macro is obsolescent, as current systems have a @code{select} whose
4702 signature conforms to Posix.  New programs need not use this macro.
4703 @end defmac
4705 @defmac AC_FUNC_SETPGRP
4706 @acindex{FUNC_SETPGRP}
4707 @cvindex SETPGRP_VOID
4708 @c @fuindex setpgrp
4709 @prindex @code{setpgrp}
4710 If @code{setpgrp} takes no argument (the Posix version), define
4711 @code{SETPGRP_VOID}.  Otherwise, it is the @acronym{BSD} version, which takes
4712 two process IDs as arguments.  This macro does not check whether
4713 @code{setpgrp} exists at all; if you need to work in that situation,
4714 first call @code{AC_CHECK_FUNC} for @code{setpgrp}.
4716 This macro is obsolescent, as current systems have a @code{setpgrp}
4717 whose signature conforms to Posix.  New programs need not use this macro.
4718 @end defmac
4720 @defmac AC_FUNC_STAT
4721 @defmacx AC_FUNC_LSTAT
4722 @acindex{FUNC_STAT}
4723 @acindex{FUNC_LSTAT}
4724 @cvindex HAVE_STAT_EMPTY_STRING_BUG
4725 @cvindex HAVE_LSTAT_EMPTY_STRING_BUG
4726 @c @fuindex stat
4727 @prindex @code{stat}
4728 @c @fuindex lstat
4729 @prindex @code{lstat}
4730 Determine whether @code{stat} or @code{lstat} have the bug that it
4731 succeeds when given the zero-length file name as argument.  The @code{stat}
4732 and @code{lstat} from SunOS 4.1.4 and the Hurd (as of 1998-11-01) do
4733 this.
4735 If it does, then define @code{HAVE_STAT_EMPTY_STRING_BUG} (or
4736 @code{HAVE_LSTAT_EMPTY_STRING_BUG}) and ask for an @code{AC_LIBOBJ}
4737 replacement of it.
4739 These macros are obsolescent, as no current systems have the bug.
4740 New programs need not use these macros.
4741 @end defmac
4743 @defmac AC_FUNC_SETVBUF_REVERSED
4744 @acindex{FUNC_SETVBUF_REVERSED}
4745 @cvindex SETVBUF_REVERSED
4746 @c @fuindex setvbuf
4747 @prindex @code{setvbuf}
4748 If @code{setvbuf} takes the buffering type as its second argument and
4749 the buffer pointer as the third, instead of the other way around, define
4750 @code{SETVBUF_REVERSED}.
4752 This macro is obsolescent, as no current systems have the bug.
4753 New programs need not use this macro.
4754 @end defmac
4756 @defmac AC_FUNC_STRCOLL
4757 @acindex{FUNC_STRCOLL}
4758 @cvindex HAVE_STRCOLL
4759 @c @fuindex strcoll
4760 @prindex @code{strcoll}
4761 If the @code{strcoll} function exists and works correctly, define
4762 @code{HAVE_STRCOLL}.  This does a bit more than
4763 @samp{AC_CHECK_FUNCS(strcoll)}, because some systems have incorrect
4764 definitions of @code{strcoll} that should not be used.
4765 @end defmac
4767 @defmac AC_FUNC_STRERROR_R
4768 @acindex{FUNC_STRERROR_R}
4769 @cvindex HAVE_STRERROR_R
4770 @cvindex HAVE_DECL_STRERROR_R
4771 @cvindex STRERROR_R_CHAR_P
4772 @c @fuindex strerror_r
4773 @prindex @code{strerror_r}
4774 If @code{strerror_r} is available, define @code{HAVE_STRERROR_R}, and if
4775 it is declared, define @code{HAVE_DECL_STRERROR_R}.  If it returns a
4776 @code{char *} message, define @code{STRERROR_R_CHAR_P}; otherwise it
4777 returns an @code{int} error number.  The Thread-Safe Functions option of
4778 Posix requires @code{strerror_r} to return @code{int}, but
4779 many systems (including, for example, version 2.2.4 of the @acronym{GNU} C
4780 Library) return a @code{char *} value that is not necessarily equal to
4781 the buffer argument.
4782 @end defmac
4784 @defmac AC_FUNC_STRFTIME
4785 @acindex{FUNC_STRFTIME}
4786 @cvindex HAVE_STRFTIME
4787 @c @fuindex strftime
4788 @prindex @code{strftime}
4789 Check for @code{strftime} in the @file{intl} library, for SCO Unix.
4790 Then, if @code{strftime} is available, define @code{HAVE_STRFTIME}.
4792 This macro is obsolescent, as no current systems require the @file{intl}
4793 library for @code{strftime}.  New programs need not use this macro.
4794 @end defmac
4796 @defmac AC_FUNC_STRTOD
4797 @acindex{FUNC_STRTOD}
4798 @ovindex POW_LIB
4799 @c @fuindex strtod
4800 @prindex @code{strtod}
4801 If the @code{strtod} function does not exist or doesn't work correctly,
4802 ask for an @code{AC_LIBOBJ} replacement of @samp{strtod}.  In this case,
4803 because @file{strtod.c} is likely to need @samp{pow}, set the output
4804 variable @code{POW_LIB} to the extra library needed.
4805 @end defmac
4807 @defmac AC_FUNC_STRTOLD
4808 @acindex{FUNC_STRTOLD}
4809 @prindex @code{strtold}
4810 If the @code{strtold} function exists and conforms to C99, define
4811 @code{HAVE_STRTOLD}.
4812 @end defmac
4814 @defmac AC_FUNC_STRNLEN
4815 @acindex{FUNC_STRNLEN}
4816 @cvindex HAVE_STRNLEN
4817 @c @fuindex strnlen
4818 @prindex @code{strnlen}
4819 If the @code{strnlen} function is not available, or is buggy (like the one
4820 from @acronym{AIX} 4.3), require an @code{AC_LIBOBJ} replacement for it.
4821 @end defmac
4823 @defmac AC_FUNC_UTIME_NULL
4824 @acindex{FUNC_UTIME_NULL}
4825 @cvindex HAVE_UTIME_NULL
4826 @c @fuindex utime
4827 @prindex @code{utime}
4828 If @samp{utime (@var{file}, NULL)} sets @var{file}'s timestamp to
4829 the present, define @code{HAVE_UTIME_NULL}.
4831 This macro is obsolescent, as all current systems have a @code{utime}
4832 that behaves this way.  New programs need not use this macro.
4833 @end defmac
4835 @defmac AC_FUNC_VPRINTF
4836 @acindex{FUNC_VPRINTF}
4837 @cvindex HAVE_VPRINTF
4838 @cvindex HAVE_DOPRNT
4839 @c @fuindex vprintf
4840 @prindex @code{vprintf}
4841 If @code{vprintf} is found, define @code{HAVE_VPRINTF}.  Otherwise, if
4842 @code{_doprnt} is found, define @code{HAVE_DOPRNT}.  (If @code{vprintf}
4843 is available, you may assume that @code{vfprintf} and @code{vsprintf}
4844 are also available.)
4846 This macro is obsolescent, as all current systems have @code{vprintf}.
4847 New programs need not use this macro.
4848 @end defmac
4850 @defmac AC_REPLACE_FNMATCH
4851 @acindex{REPLACE_FNMATCH}
4852 @c @fuindex fnmatch
4853 @prindex @code{fnmatch}
4854 @hdrindex{fnmatch.h}
4855 If the @code{fnmatch} function does not conform to Posix (see
4856 @code{AC_FUNC_FNMATCH}), ask for its @code{AC_LIBOBJ} replacement.
4858 The files @file{fnmatch.c}, @file{fnmatch_loop.c}, and @file{fnmatch_.h}
4859 in the @code{AC_LIBOBJ} replacement directory are assumed to contain a
4860 copy of the source code of @acronym{GNU} @code{fnmatch}.  If necessary,
4861 this source code is compiled as an @code{AC_LIBOBJ} replacement, and the
4862 @file{fnmatch_.h} file is linked to @file{fnmatch.h} so that it can be
4863 included in place of the system @code{<fnmatch.h>}.
4864 @end defmac
4868 @node Generic Functions
4869 @subsection Generic Function Checks
4871 These macros are used to find functions not covered by the ``particular''
4872 test macros.  If the functions might be in libraries other than the
4873 default C library, first call @code{AC_CHECK_LIB} for those libraries.
4874 If you need to check the behavior of a function as well as find out
4875 whether it is present, you have to write your own test for
4876 it (@pxref{Writing Tests}).
4878 @defmac AC_CHECK_FUNC (@var{function}, @ovar{action-if-found}, @ovar{action-if-not-found})
4879 @acindex{CHECK_FUNC}
4880 If C function @var{function} is available, run shell commands
4881 @var{action-if-found}, otherwise @var{action-if-not-found}.  If you just
4882 want to define a symbol if the function is available, consider using
4883 @code{AC_CHECK_FUNCS} instead.  This macro checks for functions with C
4884 linkage even when @code{AC_LANG(C++)} has been called, since C is more
4885 standardized than C++.  (@pxref{Language Choice}, for more information
4886 about selecting the language for checks.)
4887 @end defmac
4889 @defmac AC_CHECK_FUNCS (@var{function}@dots{}, @ovar{action-if-found}, @ovar{action-if-not-found})
4890 @acindex{CHECK_FUNCS}
4891 @cvindex HAVE_@var{function}
4892 For each @var{function} enumerated in the blank-or-newline-separated argument
4893 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
4894 If @var{action-if-found} is given, it is additional shell code to
4895 execute when one of the functions is found.  You can give it a value of
4896 @samp{break} to break out of the loop on the first match.  If
4897 @var{action-if-not-found} is given, it is executed when one of the
4898 functions is not found.
4899 @end defmac
4901 @defmac AC_CHECK_FUNCS_ONCE (@var{function}@dots{})
4902 @acindex{CHECK_FUNCS_ONCE}
4903 @cvindex HAVE_@var{function}
4904 For each @var{function} enumerated in the blank-or-newline-separated argument
4905 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
4906 This is a once-only variant of @code{AC_CHECK_FUNCS}.  It generates the
4907 checking code at most once, so that @command{configure} is smaller and
4908 faster; but the checks cannot be conditionalized and are always done once,
4909 early during the @command{configure} run.
4910 @end defmac
4912 @sp 1
4914 Autoconf follows a philosophy that was formed over the years by those
4915 who have struggled for portability: isolate the portability issues in
4916 specific files, and then program as if you were in a Posix
4917 environment.  Some functions may be missing or unfixable, and your
4918 package must be ready to replace them.
4920 Suitable replacements for many such problem functions are available from
4921 Gnulib (@pxref{Gnulib}).
4923 @defmac AC_LIBOBJ (@var{function})
4924 @acindex{LIBOBJ}
4925 @ovindex LIBOBJS
4926 Specify that @samp{@var{function}.c} must be included in the executables
4927 to replace a missing or broken implementation of @var{function}.
4929 Technically, it adds @samp{@var{function}.$ac_objext} to the output
4930 variable @code{LIBOBJS} if it is not already in, and calls
4931 @code{AC_LIBSOURCE} for @samp{@var{function}.c}.  You should not
4932 directly change @code{LIBOBJS}, since this is not traceable.
4933 @end defmac
4935 @defmac AC_LIBSOURCE (@var{file})
4936 @acindex{LIBSOURCE}
4937 Specify that @var{file} might be needed to compile the project.  If you
4938 need to know what files might be needed by a @file{configure.ac}, you
4939 should trace @code{AC_LIBSOURCE}.  @var{file} must be a literal.
4941 This macro is called automatically from @code{AC_LIBOBJ}, but you must
4942 call it explicitly if you pass a shell variable to @code{AC_LIBOBJ}.  In
4943 that case, since shell variables cannot be traced statically, you must
4944 pass to @code{AC_LIBSOURCE} any possible files that the shell variable
4945 might cause @code{AC_LIBOBJ} to need.  For example, if you want to pass
4946 a variable @code{$foo_or_bar} to @code{AC_LIBOBJ} that holds either
4947 @code{"foo"} or @code{"bar"}, you should do:
4949 @example
4950 AC_LIBSOURCE([foo.c])
4951 AC_LIBSOURCE([bar.c])
4952 AC_LIBOBJ([$foo_or_bar])
4953 @end example
4955 @noindent
4956 There is usually a way to avoid this, however, and you are encouraged to
4957 simply call @code{AC_LIBOBJ} with literal arguments.
4959 Note that this macro replaces the obsolete @code{AC_LIBOBJ_DECL}, with
4960 slightly different semantics: the old macro took the function name,
4961 e.g., @code{foo}, as its argument rather than the file name.
4962 @end defmac
4964 @defmac AC_LIBSOURCES (@var{files})
4965 @acindex{LIBSOURCES}
4966 Like @code{AC_LIBSOURCE}, but accepts one or more @var{files} in a
4967 comma-separated M4 list.  Thus, the above example might be rewritten:
4969 @example
4970 AC_LIBSOURCES([foo.c, bar.c])
4971 AC_LIBOBJ([$foo_or_bar])
4972 @end example
4973 @end defmac
4975 @defmac AC_CONFIG_LIBOBJ_DIR (@var{directory})
4976 @acindex{CONFIG_LIBOBJ_DIR}
4977 Specify that @code{AC_LIBOBJ} replacement files are to be found in
4978 @var{directory}, a name relative to the top level of the
4979 source tree.  The replacement directory defaults to @file{.}, the top
4980 level directory, and the most typical value is @file{lib}, corresponding
4981 to @samp{AC_CONFIG_LIBOBJ_DIR([lib])}.
4983 @command{configure} might need to know the replacement directory for the
4984 following reasons: (i) some checks use the replacement files, (ii) some
4985 macros bypass broken system headers by installing links to the
4986 replacement headers (iii) when used in conjunction with Automake,
4987 within each makefile, @var{directory} is used as a relative path
4988 from @code{$(top_srcdir)} to each object named in @code{LIBOBJS} and
4989 @code{LTLIBOBJS}, etc.
4990 @end defmac
4992 @sp 1
4994 It is common to merely check for the existence of a function, and ask
4995 for its @code{AC_LIBOBJ} replacement if missing.  The following macro is
4996 a convenient shorthand.
4998 @defmac AC_REPLACE_FUNCS (@var{function}@dots{})
4999 @acindex{REPLACE_FUNCS}
5000 @ovindex LIBOBJS
5001 Like @code{AC_CHECK_FUNCS}, but uses @samp{AC_LIBOBJ(@var{function})} as
5002 @var{action-if-not-found}.  You can declare your replacement function by
5003 enclosing the prototype in @samp{#if !HAVE_@var{function}}.  If the
5004 system has the function, it probably declares it in a header file you
5005 should be including, so you shouldn't redeclare it lest your declaration
5006 conflict.
5007 @end defmac
5009 @node Header Files
5010 @section Header Files
5011 @cindex Header, checking
5013 The following macros check for the presence of certain C header files.
5014 If there is no macro specifically defined to check for a header file you need,
5015 and you don't need to check for any special properties of
5016 it, then you can use one of the general header-file check macros.
5018 @menu
5019 * Header Portability::          Collected knowledge on common headers
5020 * Particular Headers::          Special handling to find certain headers
5021 * Generic Headers::             How to find other headers
5022 @end menu
5024 @node Header Portability
5025 @subsection Portability of Headers
5026 @cindex Portability of headers
5027 @cindex Header portability
5029 This section tries to collect knowledge about common headers, and the
5030 problems they cause.  By definition, this list always requires
5031 additions.  Please help us keeping it as complete as possible.
5033 @table @asis
5035 @item @file{limits.h}
5036 C99 says that @file{limits.h} defines @code{LLONG_MIN},
5037 @code{LLONG_MAX}, and @code{ULLONG_MAX}, but many almost-C99
5038 environments (e.g., default @acronym{GCC} 4.0.2 + glibc 2.4) do not
5039 define them.
5041 @item @file{inttypes.h} vs.@: @file{stdint.h}
5042 @hdrindex{inttypes.h}
5043 @hdrindex{stdint.h}
5044 The C99 standard says that @file{inttypes.h} includes
5045 @file{stdint.h}, so there's no need to include @file{stdint.h}
5046 separately in a standard environment.  Some implementations have
5047 @file{inttypes.h} but not @file{stdint.h} (e.g., Solaris 7), but we don't
5048 know of any implementation that has @file{stdint.h} but not
5049 @file{inttypes.h}.
5051 @item @file{linux/irda.h}
5052 @hdrindex{linux/irda.h}
5053 It requires @file{linux/types.h} and @file{sys/socket.h}.
5055 @item @file{linux/random.h}
5056 @hdrindex{linux/random.h}
5057 It requires @file{linux/types.h}.
5059 @item @file{net/if.h}
5060 @hdrindex{net/if.h}
5061 On Darwin, this file requires that @file{sys/socket.h} be included
5062 beforehand.  One should run:
5064 @example
5065 AC_CHECK_HEADERS([sys/socket.h])
5066 AC_CHECK_HEADERS([net/if.h], [], [],
5067 [#include <stdio.h>
5068 #if STDC_HEADERS
5069 # include <stdlib.h>
5070 # include <stddef.h>
5071 #else
5072 # if HAVE_STDLIB_H
5073 #  include <stdlib.h>
5074 # endif
5075 #endif
5076 #if HAVE_SYS_SOCKET_H
5077 # include <sys/socket.h>
5078 #endif
5080 @end example
5082 @item @file{netinet/if_ether.h}
5083 @hdrindex{netinet/if_ether.h}
5084 On Darwin, this file requires that @file{stdio.h} and
5085 @file{sys/socket.h} be included beforehand.  One should run:
5087 @example
5088 AC_CHECK_HEADERS([sys/socket.h])
5089 AC_CHECK_HEADERS([netinet/if_ether.h], [], [],
5090 [#include <stdio.h>
5091 #if STDC_HEADERS
5092 # include <stdlib.h>
5093 # include <stddef.h>
5094 #else
5095 # if HAVE_STDLIB_H
5096 #  include <stdlib.h>
5097 # endif
5098 #endif
5099 #if HAVE_SYS_SOCKET_H
5100 # include <sys/socket.h>
5101 #endif
5103 @end example
5105 @item @file{stdint.h}
5106 See above, item @file{inttypes.h} vs.@: @file{stdint.h}.
5108 @item @file{stdlib.h}
5109 @hdrindex{stdlib.h}
5110 On many systems (e.g., Darwin), @file{stdio.h} is a prerequisite.
5112 @item @file{sys/mount.h}
5113 @hdrindex{sys/mount.h}
5114 On Free@acronym{BSD} 4.8 on ia32 and using gcc version 2.95.4,
5115 @file{sys/params.h} is a prerequisite.
5117 @item @file{sys/ptem.h}
5118 @hdrindex{sys/ptem.h}
5119 On Solaris 8, @file{sys/stream.h} is a prerequisite.
5121 @item @file{sys/socket.h}
5122 @hdrindex{sys/socket.h}
5123 On Darwin, @file{stdlib.h} is a prerequisite.
5125 @item @file{sys/ucred.h}
5126 @hdrindex{sys/ucred.h}
5127 On Tru64 5.1, @file{sys/types.h} is a prerequisite.
5129 @item @file{X11/extensions/scrnsaver.h}
5130 @hdrindex{X11/extensions/scrnsaver.h}
5131 Using XFree86, this header requires @file{X11/Xlib.h}, which is probably
5132 so required that you might not even consider looking for it.
5134 @example
5135 AC_CHECK_HEADERS([X11/extensions/scrnsaver.h], [], [],
5136 [[#include <X11/Xlib.h>
5138 @end example
5139 @end table
5142 @node Particular Headers
5143 @subsection Particular Header Checks
5145 These macros check for particular system header files---whether they
5146 exist, and in some cases whether they declare certain symbols.
5148 @defmac AC_HEADER_ASSERT
5149 @acindex{HEADER_ASSERT}
5150 @cvindex NDEBUG
5151 @hdrindex{assert.h}
5152 Check whether to enable assertions in the style of @file{assert.h}.
5153 Assertions are enabled by default, but the user can override this by
5154 invoking @command{configure} with the @option{--disable-assert} option.
5155 @end defmac
5157 @defmac AC_HEADER_DIRENT
5158 @acindex{HEADER_DIRENT}
5159 @cvindex HAVE_DIRENT_H
5160 @cvindex HAVE_NDIR_H
5161 @cvindex HAVE_SYS_DIR_H
5162 @cvindex HAVE_SYS_NDIR_H
5163 @hdrindex{dirent.h}
5164 @hdrindex{sys/ndir.h}
5165 @hdrindex{sys/dir.h}
5166 @hdrindex{ndir.h}
5167 Check for the following header files.  For the first one that is
5168 found and defines @samp{DIR}, define the listed C preprocessor macro:
5170 @multitable {@file{sys/ndir.h}} {@code{HAVE_SYS_NDIR_H}}
5171 @item @file{dirent.h}   @tab @code{HAVE_DIRENT_H}
5172 @item @file{sys/ndir.h} @tab @code{HAVE_SYS_NDIR_H}
5173 @item @file{sys/dir.h}  @tab @code{HAVE_SYS_DIR_H}
5174 @item @file{ndir.h}     @tab @code{HAVE_NDIR_H}
5175 @end multitable
5177 The directory-library declarations in your source code should look
5178 something like the following:
5180 @example
5181 @group
5182 #include <sys/types.h>
5183 #ifdef HAVE_DIRENT_H
5184 # include <dirent.h>
5185 # define NAMLEN(dirent) strlen ((dirent)->d_name)
5186 #else
5187 # define dirent direct
5188 # define NAMLEN(dirent) ((dirent)->d_namlen)
5189 # if HAVE_SYS_NDIR_H
5190 #  include <sys/ndir.h>
5191 # endif
5192 # if HAVE_SYS_DIR_H
5193 #  include <sys/dir.h>
5194 # endif
5195 # if HAVE_NDIR_H
5196 #  include <ndir.h>
5197 # endif
5198 #endif
5199 @end group
5200 @end example
5202 Using the above declarations, the program would declare variables to be
5203 of type @code{struct dirent}, not @code{struct direct}, and would access
5204 the length of a directory entry name by passing a pointer to a
5205 @code{struct dirent} to the @code{NAMLEN} macro.
5207 This macro also checks for the SCO Xenix @file{dir} and @file{x} libraries.
5209 This macro is obsolescent, as all current systems with directory
5210 libraries have @code{<dirent.h>}.  New programs need not use this macro.
5212 Also see @code{AC_STRUCT_DIRENT_D_INO} and
5213 @code{AC_STRUCT_DIRENT_D_TYPE} (@pxref{Particular Structures}).
5214 @end defmac
5216 @defmac AC_HEADER_MAJOR
5217 @acindex{HEADER_MAJOR}
5218 @cvindex MAJOR_IN_MKDEV
5219 @cvindex MAJOR_IN_SYSMACROS
5220 @hdrindex{sys/mkdev.h}
5221 @hdrindex{sys/sysmacros.h}
5222 If @file{sys/types.h} does not define @code{major}, @code{minor}, and
5223 @code{makedev}, but @file{sys/mkdev.h} does, define
5224 @code{MAJOR_IN_MKDEV}; otherwise, if @file{sys/sysmacros.h} does, define
5225 @code{MAJOR_IN_SYSMACROS}.
5226 @end defmac
5228 @defmac AC_HEADER_RESOLV
5229 @acindex{HEADER_RESOLV}
5230 @cvindex HAVE_RESOLV_H
5231 @hdrindex{resolv.h}
5232 Checks for header @file{resolv.h}, checking for prerequisites first.
5233 To properly use @file{resolv.h}, your code should contain something like
5234 the following:
5236 @verbatim
5237 #if HAVE_SYS_TYPES_H
5238 #  include <sys/types.h>
5239 #endif
5240 #ifdef HAVE_NETINET_IN_H
5241 #  include <netinet/in.h>   /* inet_ functions / structs */
5242 #endif
5243 #ifdef HAVE_ARPA_NAMESER_H
5244 #  include <arpa/nameser.h> /* DNS HEADER struct */
5245 #endif
5246 #ifdef HAVE_NETDB_H
5247 #  include <netdb.h>
5248 #endif
5249 #include <resolv.h>
5250 @end verbatim
5251 @end defmac
5253 @defmac AC_HEADER_STAT
5254 @acindex{HEADER_STAT}
5255 @cvindex STAT_MACROS_BROKEN
5256 @hdrindex{sys/stat.h}
5257 If the macros @code{S_ISDIR}, @code{S_ISREG}, etc.@: defined in
5258 @file{sys/stat.h} do not work properly (returning false positives),
5259 define @code{STAT_MACROS_BROKEN}.  This is the case on Tektronix UTekV,
5260 Amdahl UTS and Motorola System V/88.
5262 This macro is obsolescent, as no current systems have the bug.
5263 New programs need not use this macro.
5264 @end defmac
5266 @defmac AC_HEADER_STDBOOL
5267 @acindex{HEADER_STDBOOL}
5268 @cvindex HAVE_STDBOOL_H
5269 @cvindex HAVE__BOOL
5270 @hdrindex{stdbool.h}
5271 @hdrindex{system.h}
5272 If @file{stdbool.h} exists and conforms to C99, define
5273 @code{HAVE_STDBOOL_H} to 1; if the type @code{_Bool} is defined, define
5274 @code{HAVE__BOOL} to 1.  To fulfill the C99 requirements, your
5275 @file{system.h} could contain the following code:
5277 @verbatim
5278 #if HAVE_STDBOOL_H
5279 # include <stdbool.h>
5280 #else
5281 # if ! HAVE__BOOL
5282 #  ifdef __cplusplus
5283 typedef bool _Bool;
5284 #  else
5285 #   define _Bool signed char
5286 #  endif
5287 # endif
5288 # define bool _Bool
5289 # define false 0
5290 # define true 1
5291 # define __bool_true_false_are_defined 1
5292 #endif
5293 @end verbatim
5295 Alternatively you can use the @samp{stdbool} package of Gnulib
5296 (@pxref{Gnulib}); it packages the above code into a replacement header
5297 and contains a few other bells and whistles.
5299 @end defmac
5302 @defmac AC_HEADER_STDC
5303 @acindex{HEADER_STDC}
5304 @cvindex STDC_HEADERS
5305 @hdrindex{stdlib.h}
5306 @hdrindex{stdarg.h}
5307 @hdrindex{string.h}
5308 @hdrindex{float.h}
5309 @hdrindex{ctype.h}
5310 Define @code{STDC_HEADERS} if the system has C header files
5311 conforming to @acronym{ANSI} C89 (@acronym{ISO} C90).
5312 Specifically, this macro checks for @file{stdlib.h}, @file{stdarg.h},
5313 @file{string.h}, and @file{float.h}; if the system has those, it
5314 probably has the rest of the C89 header files.  This macro also
5315 checks whether @file{string.h} declares @code{memchr} (and thus
5316 presumably the other @code{mem} functions), whether @file{stdlib.h}
5317 declare @code{free} (and thus presumably @code{malloc} and other related
5318 functions), and whether the @file{ctype.h} macros work on characters
5319 with the high bit set, as the C standard requires.
5321 If you use this macro, your code can refer to @code{STDC_HEADERS} to
5322 determine whether the system has conforming header files (and probably C
5323 library functions).
5325 This macro is obsolescent, as current systems have conforming header
5326 files.  New programs need not use this macro.
5328 @hdrindex{string.h}
5329 @hdrindex{strings.h}
5330 Nowadays @file{string.h} is part of the C standard and declares functions like
5331 @code{strcpy}, and @file{strings.h} is standardized by Posix and declares
5332 @acronym{BSD} functions like @code{bcopy}; but
5333 historically, string functions were a major sticking point in this area.
5334 If you still want to worry about portability to ancient systems without
5335 standard headers, there is so much variation
5336 that it is probably easier to declare the functions you use than to
5337 figure out exactly what the system header files declare.  Some ancient systems
5338 contained a mix of functions from the C standard and from @acronym{BSD};
5339 some were mostly standard but lacked @samp{memmove}; some defined the
5340 @acronym{BSD} functions as macros in @file{string.h} or
5341 @file{strings.h}; some had only the @acronym{BSD} functions but
5342 @file{string.h}; some declared the memory functions in @file{memory.h},
5343 some in @file{string.h}; etc.  It is probably sufficient to check for
5344 one string function and one memory function; if the library had the
5345 standard versions of those then it probably had most of the others.
5346 If you put the following in @file{configure.ac}:
5348 @example
5349 # This example is obsolescent.
5350 # Nowadays you can omit these macro calls.
5351 AC_HEADER_STDC
5352 AC_CHECK_FUNCS([strchr memcpy])
5353 @end example
5355 @noindent
5356 then, in your code, you can use declarations like this:
5358 @example
5359 @group
5360 /* This example is obsolescent.
5361    Nowadays you can just #include <string.h>.  */
5362 #if STDC_HEADERS
5363 # include <string.h>
5364 #else
5365 # if !HAVE_STRCHR
5366 #  define strchr index
5367 #  define strrchr rindex
5368 # endif
5369 char *strchr (), *strrchr ();
5370 # if !HAVE_MEMCPY
5371 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
5372 #  define memmove(d, s, n) bcopy ((s), (d), (n))
5373 # endif
5374 #endif
5375 @end group
5376 @end example
5378 @noindent
5379 If you use a function like @code{memchr}, @code{memset}, @code{strtok},
5380 or @code{strspn}, which have no @acronym{BSD} equivalent, then macros don't
5381 suffice to port to ancient hosts; you must provide an implementation of
5382 each function.  An easy
5383 way to incorporate your implementations only when needed (since the ones
5384 in system C libraries may be hand optimized) is to, taking @code{memchr}
5385 for example, put it in @file{memchr.c} and use
5386 @samp{AC_REPLACE_FUNCS([memchr])}.
5387 @end defmac
5389 @defmac AC_HEADER_SYS_WAIT
5390 @acindex{HEADER_SYS_WAIT}
5391 @cvindex HAVE_SYS_WAIT_H
5392 @hdrindex{sys/wait.h}
5393 If @file{sys/wait.h} exists and is compatible with Posix, define
5394 @code{HAVE_SYS_WAIT_H}.  Incompatibility can occur if @file{sys/wait.h}
5395 does not exist, or if it uses the old @acronym{BSD} @code{union wait} instead
5396 of @code{int} to store a status value.  If @file{sys/wait.h} is not
5397 Posix compatible, then instead of including it, define the
5398 Posix macros with their usual interpretations.  Here is an
5399 example:
5401 @example
5402 @group
5403 #include <sys/types.h>
5404 #if HAVE_SYS_WAIT_H
5405 # include <sys/wait.h>
5406 #endif
5407 #ifndef WEXITSTATUS
5408 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
5409 #endif
5410 #ifndef WIFEXITED
5411 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
5412 #endif
5413 @end group
5414 @end example
5416 @noindent
5417 This macro is obsolescent, as current systems are compatible with Posix.
5418 New programs need not use this macro.
5419 @end defmac
5421 @cvindex _POSIX_VERSION
5422 @hdrindex{unistd.h}
5423 @code{_POSIX_VERSION} is defined when @file{unistd.h} is included on
5424 Posix systems.  If there is no @file{unistd.h}, it is definitely
5425 not a Posix system.  However, some non-Posix systems do
5426 have @file{unistd.h}.
5428 The way to check whether the system supports Posix is:
5430 @example
5431 @group
5432 #if HAVE_UNISTD_H
5433 # include <sys/types.h>
5434 # include <unistd.h>
5435 #endif
5437 #ifdef _POSIX_VERSION
5438 /* Code for Posix systems.  */
5439 #endif
5440 @end group
5441 @end example
5443 @defmac AC_HEADER_TIME
5444 @acindex{HEADER_TIME}
5445 @cvindex TIME_WITH_SYS_TIME
5446 @hdrindex{time.h}
5447 @hdrindex{sys/time.h}
5448 If a program may include both @file{time.h} and @file{sys/time.h},
5449 define @code{TIME_WITH_SYS_TIME}.  On some ancient systems,
5450 @file{sys/time.h} included @file{time.h}, but @file{time.h} was not
5451 protected against multiple inclusion, so programs could not explicitly
5452 include both files.  This macro is useful in programs that use, for
5453 example, @code{struct timeval} as well as
5454 @code{struct tm}.  It is best used in conjunction with
5455 @code{HAVE_SYS_TIME_H}, which can be checked for using
5456 @code{AC_CHECK_HEADERS([sys/time.h])}.
5458 @example
5459 @group
5460 #if TIME_WITH_SYS_TIME
5461 # include <sys/time.h>
5462 # include <time.h>
5463 #else
5464 # if HAVE_SYS_TIME_H
5465 #  include <sys/time.h>
5466 # else
5467 #  include <time.h>
5468 # endif
5469 #endif
5470 @end group
5471 @end example
5473 @noindent
5474 This macro is obsolescent, as current systems can include both files
5475 when they exist.  New programs need not use this macro.
5476 @end defmac
5479 @defmac AC_HEADER_TIOCGWINSZ
5480 @acindex{HEADER_TIOCGWINSZ}
5481 @cvindex GWINSZ_IN_SYS_IOCTL
5482 @hdrindex{sys/ioctl.h}
5483 @hdrindex{termios.h}
5484 @c FIXME: I need clarifications from Jim.
5485 If the use of @code{TIOCGWINSZ} requires @file{<sys/ioctl.h>}, then
5486 define @code{GWINSZ_IN_SYS_IOCTL}.  Otherwise @code{TIOCGWINSZ} can be
5487 found in @file{<termios.h>}.
5489 Use:
5491 @example
5492 @group
5493 #if HAVE_TERMIOS_H
5494 # include <termios.h>
5495 #endif
5497 #if GWINSZ_IN_SYS_IOCTL
5498 # include <sys/ioctl.h>
5499 #endif
5500 @end group
5501 @end example
5502 @end defmac
5504 @node Generic Headers
5505 @subsection Generic Header Checks
5507 These macros are used to find system header files not covered by the
5508 ``particular'' test macros.  If you need to check the contents of a header
5509 as well as find out whether it is present, you have to write your own
5510 test for it (@pxref{Writing Tests}).
5512 @defmac AC_CHECK_HEADER (@var{header-file}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5513 @acindex{CHECK_HEADER}
5514 If the system header file @var{header-file} is compilable, execute shell
5515 commands @var{action-if-found}, otherwise execute
5516 @var{action-if-not-found}.  If you just want to define a symbol if the
5517 header file is available, consider using @code{AC_CHECK_HEADERS}
5518 instead.
5520 For compatibility issues with older versions of Autoconf, please read
5521 below.
5522 @end defmac
5524 @defmac AC_CHECK_HEADERS (@var{header-file}@dots{}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5525 @acindex{CHECK_HEADERS}
5526 @cvindex HAVE_@var{header}
5527 For each given system header file @var{header-file} in the
5528 blank-separated argument list that exists, define
5529 @code{HAVE_@var{header-file}} (in all capitals).  If @var{action-if-found}
5530 is given, it is additional shell code to execute when one of the header
5531 files is found.  You can give it a value of @samp{break} to break out of
5532 the loop on the first match.  If @var{action-if-not-found} is given, it
5533 is executed when one of the header files is not found.
5535 For compatibility issues with older versions of Autoconf, please read
5536 below.
5537 @end defmac
5539 Previous versions of Autoconf merely checked whether the header was
5540 accepted by the preprocessor.  This was changed because the old test was
5541 inappropriate for typical uses.  Headers are typically used to compile,
5542 not merely to preprocess, and the old behavior sometimes accepted
5543 headers that clashed at compile-time.  If you need to check whether a
5544 header is preprocessable, you can use @code{AC_PREPROC_IFELSE}
5545 (@pxref{Running the Preprocessor}).
5547 This scheme, which improves the robustness of the test, also requires
5548 that you make sure that headers that must be included before the
5549 @var{header-file} be part of the @var{includes}, (@pxref{Default
5550 Includes}).  If looking for @file{bar.h}, which requires that
5551 @file{foo.h} be included before if it exists, we suggest the following
5552 scheme:
5554 @verbatim
5555 AC_CHECK_HEADERS([foo.h])
5556 AC_CHECK_HEADERS([bar.h], [], [],
5557 [#if HAVE_FOO_H
5558 # include <foo.h>
5559 # endif
5561 @end verbatim
5563 The following variant generates smaller, faster @command{configure}
5564 files if you do not need the full power of @code{AC_CHECK_HEADERS}.
5566 @defmac AC_CHECK_HEADERS_ONCE (@var{header-file}@dots{})
5567 @acindex{CHECK_HEADERS_ONCE}
5568 @cvindex HAVE_@var{header}
5569 For each given system header file @var{header-file} in the
5570 blank-separated argument list that exists, define
5571 @code{HAVE_@var{header-file}} (in all capitals).
5572 This is a once-only variant of @code{AC_CHECK_HEADERS}.  It generates the
5573 checking code at most once, so that @command{configure} is smaller and
5574 faster; but the checks cannot be conditionalized and are always done once,
5575 early during the @command{configure} run.
5576 @end defmac
5578 @node Declarations
5579 @section Declarations
5580 @cindex Declaration, checking
5582 The following macros check for the declaration of variables and
5583 functions.  If there is no macro specifically defined to check for a
5584 symbol you need, then you can use the general macros (@pxref{Generic
5585 Declarations}) or, for more complex tests, you may use
5586 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5588 @menu
5589 * Particular Declarations::     Macros to check for certain declarations
5590 * Generic Declarations::        How to find other declarations
5591 @end menu
5593 @node Particular Declarations
5594 @subsection Particular Declaration Checks
5596 There are no specific macros for declarations.
5598 @node Generic Declarations
5599 @subsection Generic Declaration Checks
5601 These macros are used to find declarations not covered by the ``particular''
5602 test macros.
5604 @defmac AC_CHECK_DECL (@var{symbol}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5605 @acindex{CHECK_DECL}
5606 If @var{symbol} (a function or a variable) is not declared in
5607 @var{includes} and a declaration is needed, run the shell commands
5608 @var{action-if-not-found}, otherwise @var{action-if-found}.  If no
5609 @var{includes} are specified, the default includes are used
5610 (@pxref{Default Includes}).
5612 This macro actually tests whether it is valid to use @var{symbol} as an
5613 r-value, not if it is really declared, because it is much safer to avoid
5614 introducing extra declarations when they are not needed.
5615 @end defmac
5617 @defmac AC_CHECK_DECLS (@var{symbols}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5618 @acindex{CHECK_DECLS}
5619 @cvindex HAVE_DECL_@var{symbol}
5620 For each of the @var{symbols} (@emph{comma}-separated list), define
5621 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5622 @var{symbol} is declared, otherwise to @samp{0}.  If
5623 @var{action-if-not-found} is given, it is additional shell code to
5624 execute when one of the function declarations is needed, otherwise
5625 @var{action-if-found} is executed.
5627 This macro uses an M4 list as first argument:
5628 @example
5629 AC_CHECK_DECLS([strdup])
5630 AC_CHECK_DECLS([strlen])
5631 AC_CHECK_DECLS([malloc, realloc, calloc, free])
5632 @end example
5634 Unlike the other @samp{AC_CHECK_*S} macros, when a @var{symbol} is not
5635 declared, @code{HAVE_DECL_@var{symbol}} is defined to @samp{0} instead
5636 of leaving @code{HAVE_DECL_@var{symbol}} undeclared.  When you are
5637 @emph{sure} that the check was performed, use
5638 @code{HAVE_DECL_@var{symbol}} just like any other result of Autoconf:
5640 @example
5641 #if !HAVE_DECL_SYMBOL
5642 extern char *symbol;
5643 #endif
5644 @end example
5646 @noindent
5647 If the test may have not been performed, however, because it is safer
5648 @emph{not} to declare a symbol than to use a declaration that conflicts
5649 with the system's one, you should use:
5651 @example
5652 #if defined HAVE_DECL_MALLOC && !HAVE_DECL_MALLOC
5653 void *malloc (size_t *s);
5654 #endif
5655 @end example
5657 @noindent
5658 You fall into the second category only in extreme situations: either
5659 your files may be used without being configured, or they are used during
5660 the configuration.  In most cases the traditional approach is enough.
5661 @end defmac
5663 @defmac AC_CHECK_DECLS_ONCE (@var{symbols})
5664 @acindex{CHECK_DECLS_ONCE}
5665 @cvindex HAVE_DECL_@var{symbol}
5666 For each of the @var{symbols} (@emph{comma}-separated list), define
5667 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5668 @var{symbol} is declared in the default include files, otherwise to
5669 @samp{0}.  This is a once-only variant of @code{AC_CHECK_DECLS}.  It
5670 generates the checking code at most once, so that @command{configure} is
5671 smaller and faster; but the checks cannot be conditionalized and are
5672 always done once, early during the @command{configure} run.
5673 @end defmac
5676 @node Structures
5677 @section Structures
5678 @cindex Structure, checking
5680 The following macros check for the presence of certain members in C
5681 structures.  If there is no macro specifically defined to check for a
5682 member you need, then you can use the general structure-member macros
5683 (@pxref{Generic Structures}) or, for more complex tests, you may use
5684 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5686 @menu
5687 * Particular Structures::       Macros to check for certain structure members
5688 * Generic Structures::          How to find other structure members
5689 @end menu
5691 @node Particular Structures
5692 @subsection Particular Structure Checks
5694 The following macros check for certain structures or structure members.
5696 @defmac AC_STRUCT_DIRENT_D_INO
5697 @acindex{STRUCT_DIRENT_D_INO}
5698 @cvindex HAVE_STRUCT_DIRENT_D_INO
5699 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5700 Headers}).  Then, if @code{struct dirent} contains a @code{d_ino}
5701 member, define @code{HAVE_STRUCT_DIRENT_D_INO}.
5703 @code{HAVE_STRUCT_DIRENT_D_INO} indicates only the presence of
5704 @code{d_ino}, not whether its contents are always reliable.
5705 Traditionally, a zero @code{d_ino} indicated a deleted directory entry,
5706 though current systems hide this detail from the user and never return
5707 zero @code{d_ino} values.
5708 Many current systems report an incorrect @code{d_ino} for a directory
5709 entry that is a mount point.
5710 @end defmac
5712 @defmac AC_STRUCT_DIRENT_D_TYPE
5713 @acindex{STRUCT_DIRENT_D_TYPE}
5714 @cvindex HAVE_STRUCT_DIRENT_D_TYPE
5715 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5716 Headers}).  Then, if @code{struct dirent} contains a @code{d_type}
5717 member, define @code{HAVE_STRUCT_DIRENT_D_TYPE}.
5718 @end defmac
5720 @defmac AC_STRUCT_ST_BLKSIZE
5721 @acindex{STRUCT_ST_BLKSIZE}
5722 @cvindex HAVE_STRUCT_STAT_ST_BLKSIZE
5723 @cvindex HAVE_ST_BLKSIZE
5724 If @code{struct stat} contains an @code{st_blksize} member, define
5725 @code{HAVE_STRUCT_STAT_ST_BLKSIZE}.  The former name,
5726 @code{HAVE_ST_BLKSIZE} is to be avoided, as its support will cease in
5727 the future.  This macro is obsoleted, and should be replaced by
5729 @example
5730 AC_CHECK_MEMBERS([struct stat.st_blksize])
5731 @end example
5732 @end defmac
5734 @defmac AC_STRUCT_ST_BLOCKS
5735 @acindex{STRUCT_ST_BLOCKS}
5736 @cvindex HAVE_STRUCT_STAT_ST_BLOCKS
5737 @cvindex HAVE_ST_BLOCKS
5738 @ovindex LIBOBJS
5739 If @code{struct stat} contains an @code{st_blocks} member, define
5740 @code{HAVE_STRUCT_STAT_ST_BLOCKS}.  Otherwise, require an
5741 @code{AC_LIBOBJ} replacement of @samp{fileblocks}.  The former name,
5742 @code{HAVE_ST_BLOCKS} is to be avoided, as its support will cease in the
5743 future.
5744 @end defmac
5746 @defmac AC_STRUCT_ST_RDEV
5747 @acindex{STRUCT_ST_RDEV}
5748 @cvindex HAVE_ST_RDEV
5749 @cvindex HAVE_STRUCT_STAT_ST_RDEV
5750 If @code{struct stat} contains an @code{st_rdev} member, define
5751 @code{HAVE_STRUCT_STAT_ST_RDEV}.  The former name for this macro,
5752 @code{HAVE_ST_RDEV}, is to be avoided as it will cease to be supported
5753 in the future.  Actually, even the new macro is obsolete and should be
5754 replaced by:
5755 @example
5756 AC_CHECK_MEMBERS([struct stat.st_rdev])
5757 @end example
5758 @end defmac
5760 @defmac AC_STRUCT_TM
5761 @acindex{STRUCT_TM}
5762 @cvindex TM_IN_SYS_TIME
5763 @hdrindex{time.h}
5764 @hdrindex{sys/time.h}
5765 If @file{time.h} does not define @code{struct tm}, define
5766 @code{TM_IN_SYS_TIME}, which means that including @file{sys/time.h}
5767 had better define @code{struct tm}.
5769 This macro is obsolescent, as @file{time.h} defines @code{struct tm} in
5770 current systems.  New programs need not use this macro.
5771 @end defmac
5773 @defmac AC_STRUCT_TIMEZONE
5774 @acindex{STRUCT_TIMEZONE}
5775 @cvindex HAVE_TM_ZONE
5776 @cvindex HAVE_TZNAME
5777 Figure out how to get the current timezone.  If @code{struct tm} has a
5778 @code{tm_zone} member, define @code{HAVE_STRUCT_TM_TM_ZONE} (and the
5779 obsoleted @code{HAVE_TM_ZONE}).  Otherwise, if the external array
5780 @code{tzname} is found, define @code{HAVE_TZNAME}; if it is declared,
5781 define @code{HAVE_DECL_TZNAME}.
5782 @end defmac
5784 @node Generic Structures
5785 @subsection Generic Structure Checks
5787 These macros are used to find structure members not covered by the
5788 ``particular'' test macros.
5790 @defmac AC_CHECK_MEMBER (@var{aggregate}.@var{member}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5791 @acindex{CHECK_MEMBER}
5792 Check whether @var{member} is a member of the aggregate @var{aggregate}.
5793 If no @var{includes} are specified, the default includes are used
5794 (@pxref{Default Includes}).
5796 @example
5797 AC_CHECK_MEMBER([struct passwd.pw_gecos], [],
5798                 [AC_MSG_ERROR([We need `passwd.pw_gecos'!])],
5799                 [#include <pwd.h>])
5800 @end example
5802 You can use this macro for submembers:
5804 @example
5805 AC_CHECK_MEMBER(struct top.middle.bot)
5806 @end example
5807 @end defmac
5809 @defmac AC_CHECK_MEMBERS (@var{members}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
5810 @acindex{CHECK_MEMBERS}
5811 Check for the existence of each @samp{@var{aggregate}.@var{member}} of
5812 @var{members} using the previous macro.  When @var{member} belongs to
5813 @var{aggregate}, define @code{HAVE_@var{aggregate}_@var{member}} (in all
5814 capitals, with spaces and dots replaced by underscores).  If
5815 @var{action-if-found} is given, it is executed for each of the found
5816 members.  If @var{action-if-not-found} is given, it is executed for each
5817 of the members that could not be found.
5819 This macro uses M4 lists:
5820 @example
5821 AC_CHECK_MEMBERS([struct stat.st_rdev, struct stat.st_blksize])
5822 @end example
5823 @end defmac
5826 @node Types
5827 @section Types
5828 @cindex Types
5829 @cindex C types
5831 The following macros check for C types, either builtin or typedefs.  If
5832 there is no macro specifically defined to check for a type you need, and
5833 you don't need to check for any special properties of it, then you can
5834 use a general type-check macro.
5836 @menu
5837 * Particular Types::            Special handling to find certain types
5838 * Generic Types::               How to find other types
5839 @end menu
5841 @node Particular Types
5842 @subsection Particular Type Checks
5844 @hdrindex{sys/types.h}
5845 @hdrindex{stdlib.h}
5846 @hdrindex{stdint.h}
5847 @hdrindex{inttypes.h}
5848 These macros check for particular C types in @file{sys/types.h},
5849 @file{stdlib.h}, @file{stdint.h}, @file{inttypes.h} and others, if they
5850 exist.
5852 The Gnulib @code{stdint} module is an alternate way to define many of
5853 these symbols; it is useful if you prefer your code to assume a
5854 C99-or-better environment.  @xref{Gnulib}.
5856 @defmac AC_TYPE_GETGROUPS
5857 @acindex{TYPE_GETGROUPS}
5858 @cvindex GETGROUPS_T
5859 Define @code{GETGROUPS_T} to be whichever of @code{gid_t} or @code{int}
5860 is the base type of the array argument to @code{getgroups}.
5861 @end defmac
5863 @defmac AC_TYPE_INT8_T
5864 @acindex{TYPE_INT8_T}
5865 @cvindex HAVE_INT8_T
5866 @cvindex int8_t
5867 If @file{stdint.h} or @file{inttypes.h} defines the type @code{int8_t},
5868 define @code{HAVE_INT8_T}.  Otherwise, define @code{int8_t} to a signed
5869 integer type that is exactly 8 bits wide and that uses two's complement
5870 representation, if such a type exists.
5871 @end defmac
5873 @defmac AC_TYPE_INT16_T
5874 @acindex{TYPE_INT16_T}
5875 @cvindex HAVE_INT16_T
5876 @cvindex int16_t
5877 This is like @code{AC_TYPE_INT8_T}, except for 16-bit integers.
5878 @end defmac
5880 @defmac AC_TYPE_INT32_T
5881 @acindex{TYPE_INT32_T}
5882 @cvindex HAVE_INT32_T
5883 @cvindex int32_t
5884 This is like @code{AC_TYPE_INT8_T}, except for 32-bit integers.
5885 @end defmac
5887 @defmac AC_TYPE_INT64_T
5888 @acindex{TYPE_INT64_T}
5889 @cvindex HAVE_INT64_T
5890 @cvindex int64_t
5891 This is like @code{AC_TYPE_INT8_T}, except for 64-bit integers.
5892 @end defmac
5894 @defmac AC_TYPE_INTMAX_T
5895 @acindex{TYPE_INTMAX_T}
5896 @cvindex HAVE_INTMAX_T
5897 @cvindex intmax_t
5898 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intmax_t},
5899 define @code{HAVE_INTMAX_T}.  Otherwise, define @code{intmax_t} to the
5900 widest signed integer type.
5901 @end defmac
5903 @defmac AC_TYPE_INTPTR_T
5904 @acindex{TYPE_INTPTR_T}
5905 @cvindex HAVE_INTPTR_T
5906 @cvindex intptr_t
5907 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intptr_t},
5908 define @code{HAVE_INTPTR_T}.  Otherwise, define @code{intptr_t} to a
5909 signed integer type wide enough to hold a pointer, if such a type
5910 exists.
5911 @end defmac
5913 @defmac AC_TYPE_LONG_DOUBLE
5914 @acindex{TYPE_LONG_DOUBLE}
5915 @cvindex HAVE_LONG_DOUBLE
5916 If the C compiler supports a working @code{long double} type, define
5917 @code{HAVE_LONG_DOUBLE}.  The @code{long double} type might have the
5918 same range and precision as @code{double}.
5919 @end defmac
5921 @defmac AC_TYPE_LONG_DOUBLE_WIDER
5922 @acindex{TYPE_LONG_DOUBLE_WIDER}
5923 @cvindex HAVE_LONG_DOUBLE_WIDER
5924 If the C compiler supports a working @code{long double} type with more
5925 range or precision than the @code{double} type, define
5926 @code{HAVE_LONG_DOUBLE_WIDER}.
5927 @end defmac
5929 @defmac AC_TYPE_LONG_LONG_INT
5930 @acindex{TYPE_LONG_LONG_INT}
5931 @cvindex HAVE_LONG_LONG_INT
5932 If the C compiler supports a working @code{long long int} type, define
5933 @code{HAVE_LONG_LONG_INT}.
5934 @end defmac
5936 @defmac AC_TYPE_MBSTATE_T
5937 @acindex{TYPE_MBSTATE_T}
5938 @cvindex mbstate_t
5939 @hdrindex{wchar.h}
5940 Define @code{HAVE_MBSTATE_T} if @code{<wchar.h>} declares the
5941 @code{mbstate_t} type.  Also, define @code{mbstate_t} to be a type if
5942 @code{<wchar.h>} does not declare it.
5943 @end defmac
5945 @defmac AC_TYPE_MODE_T
5946 @acindex{TYPE_MODE_T}
5947 @cvindex mode_t
5948 Define @code{mode_t} to a suitable type, if standard headers do not
5949 define it.
5950 @end defmac
5952 @defmac AC_TYPE_OFF_T
5953 @acindex{TYPE_OFF_T}
5954 @cvindex off_t
5955 Define @code{off_t} to a suitable type, if standard headers do not
5956 define it.
5957 @end defmac
5959 @defmac AC_TYPE_PID_T
5960 @acindex{TYPE_PID_T}
5961 @cvindex pid_t
5962 Define @code{pid_t} to a suitable type, if standard headers do not
5963 define it.
5964 @end defmac
5966 @defmac AC_TYPE_SIGNAL
5967 @acindex{TYPE_SIGNAL}
5968 @cvindex RETSIGTYPE
5969 @hdrindex{signal.h}
5970 If @file{signal.h} declares @code{signal} as returning a pointer to a
5971 function returning @code{void}, define @code{RETSIGTYPE} to be
5972 @code{void}; otherwise, define it to be @code{int}.
5974 Define signal handlers as returning type @code{RETSIGTYPE}:
5976 @example
5977 @group
5978 RETSIGTYPE
5979 hup_handler ()
5981 @dots{}
5983 @end group
5984 @end example
5985 @end defmac
5987 @defmac AC_TYPE_SIZE_T
5988 @acindex{TYPE_SIZE_T}
5989 @cvindex size_t
5990 Define @code{size_t} to a suitable type, if standard headers do not
5991 define it.
5992 @end defmac
5994 @defmac AC_TYPE_SSIZE_T
5995 @acindex{TYPE_SSIZE_T}
5996 @cvindex ssize_t
5997 Define @code{ssize_t} to a suitable type, if standard headers do not
5998 define it.
5999 @end defmac
6001 @defmac AC_TYPE_UID_T
6002 @acindex{TYPE_UID_T}
6003 @cvindex uid_t
6004 @cvindex gid_t
6005 Define @code{uid_t} and @code{gid_t} to suitable types, if standard
6006 headers do not define them.
6007 @end defmac
6009 @defmac AC_TYPE_UINT8_T
6010 @acindex{TYPE_UINT8_T}
6011 @cvindex HAVE_UINT8_T
6012 @cvindex uint8_t
6013 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uint8_t},
6014 define @code{HAVE_UINT8_T}.  Otherwise, define @code{uint8_t} to an
6015 unsigned integer type that is exactly 8 bits wide, if such a type
6016 exists.
6017 @end defmac
6019 @defmac AC_TYPE_UINT16_T
6020 @acindex{TYPE_UINT16_T}
6021 @cvindex HAVE_UINT16_T
6022 @cvindex uint16_t
6023 This is like @code{AC_TYPE_UINT8_T}, except for 16-bit unsigned integers.
6024 @end defmac
6026 @defmac AC_TYPE_UINT32_T
6027 @acindex{TYPE_UINT32_T}
6028 @cvindex HAVE_UINT32_T
6029 @cvindex uint32_t
6030 This is like @code{AC_TYPE_UINT8_T}, except for 32-bit unsigned integers.
6031 @end defmac
6033 @defmac AC_TYPE_UINT64_T
6034 @acindex{TYPE_UINT64_T}
6035 @cvindex HAVE_UINT64_T
6036 @cvindex uint64_t
6037 This is like @code{AC_TYPE_UINT8_T}, except for 64-bit unsigned integers.
6038 @end defmac
6040 @defmac AC_TYPE_UINTMAX_T
6041 @acindex{TYPE_UINTMAX_T}
6042 @cvindex HAVE_UINTMAX_T
6043 @cvindex uintmax_t
6044 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintmax_t},
6045 define @code{HAVE_UINTMAX_T}.  Otherwise, define @code{uintmax_t} to the
6046 widest unsigned integer type.
6047 @end defmac
6049 @defmac AC_TYPE_UINTPTR_T
6050 @acindex{TYPE_UINTPTR_T}
6051 @cvindex HAVE_UINTPTR_T
6052 @cvindex uintptr_t
6053 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintptr_t},
6054 define @code{HAVE_UINTPTR_T}.  Otherwise, define @code{uintptr_t} to an
6055 unsigned integer type wide enough to hold a pointer, if such a type
6056 exists.
6057 @end defmac
6059 @defmac AC_TYPE_UNSIGNED_LONG_LONG_INT
6060 @acindex{TYPE_UNSIGNED_LONG_LONG_INT}
6061 @cvindex HAVE_UNSIGNED_LONG_LONG_INT
6062 If the C compiler supports a working @code{unsigned long long int} type,
6063 define @code{HAVE_UNSIGNED_LONG_LONG_INT}.
6064 @end defmac
6066 @node Generic Types
6067 @subsection Generic Type Checks
6069 These macros are used to check for types not covered by the ``particular''
6070 test macros.
6072 @defmac AC_CHECK_TYPE (@var{type}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
6073 @acindex{CHECK_TYPE}
6074 Check whether @var{type} is defined.  It may be a compiler builtin type
6075 or defined by the @var{includes} (@pxref{Default Includes}).
6076 @end defmac
6079 @defmac AC_CHECK_TYPES (@var{types}, @ovar{action-if-found}, @ovar{action-if-not-found}, @dvar{includes, default-includes})
6080 @acindex{CHECK_TYPES}
6081 For each @var{type} of the @var{types} that is defined, define
6082 @code{HAVE_@var{type}} (in all capitals).  If no @var{includes} are
6083 specified, the default includes are used (@pxref{Default Includes}).  If
6084 @var{action-if-found} is given, it is additional shell code to execute
6085 when one of the types is found.  If @var{action-if-not-found} is given,
6086 it is executed when one of the types is not found.
6088 This macro uses M4 lists:
6089 @example
6090 AC_CHECK_TYPES([ptrdiff_t])
6091 AC_CHECK_TYPES([unsigned long long int, uintmax_t])
6092 @end example
6094 @end defmac
6096 Autoconf, up to 2.13, used to provide to another version of
6097 @code{AC_CHECK_TYPE}, broken by design.  In order to keep backward
6098 compatibility, a simple heuristics, quite safe but not totally, is
6099 implemented.  In case of doubt, read the documentation of the former
6100 @code{AC_CHECK_TYPE}, see @ref{Obsolete Macros}.
6103 @node Compilers and Preprocessors
6104 @section Compilers and Preprocessors
6105 @cindex Compilers
6106 @cindex Preprocessors
6108 @ovindex EXEEXT
6109 All the tests for compilers (@code{AC_PROG_CC}, @code{AC_PROG_CXX},
6110 @code{AC_PROG_F77}) define the output variable @code{EXEEXT} based on
6111 the output of the compiler, typically to the empty string if
6112 Posix and @samp{.exe} if a @acronym{DOS} variant.
6114 @ovindex OBJEXT
6115 They also define the output variable @code{OBJEXT} based on the
6116 output of the compiler, after @file{.c} files have been excluded, typically
6117 to @samp{o} if Posix, @samp{obj} if a @acronym{DOS} variant.
6119 If the compiler being used does not produce executables, the tests fail.  If
6120 the executables can't be run, and cross-compilation is not enabled, they
6121 fail too.  @xref{Manual Configuration}, for more on support for cross
6122 compiling.
6124 @menu
6125 * Specific Compiler Characteristics::  Some portability issues
6126 * Generic Compiler Characteristics::  Language independent tests and features
6127 * C Compiler::                  Checking its characteristics
6128 * C++ Compiler::                Likewise
6129 * Objective C Compiler::        Likewise
6130 * Erlang Compiler and Interpreter::  Likewise
6131 * Fortran Compiler::            Likewise
6132 @end menu
6134 @node Specific Compiler Characteristics
6135 @subsection Specific Compiler Characteristics
6137 Some compilers exhibit different behaviors.
6139 @table @asis
6140 @item Static/Dynamic Expressions
6141 Autoconf relies on a trick to extract one bit of information from the C
6142 compiler: using negative array sizes.  For instance the following
6143 excerpt of a C source demonstrates how to test whether @samp{int} objects are 4
6144 bytes wide:
6146 @example
6147 static int test_array[sizeof (int) == 4 ? 1 : -1];
6148 @end example
6150 @noindent
6151 To our knowledge, there is a single compiler that does not support this
6152 trick: the @acronym{HP} C compilers (the real ones, not only the ``bundled'') on
6153 @acronym{HP-UX} 11.00.
6154 They incorrectly reject the above program with the diagnostic
6155 ``Variable-length arrays cannot have static storage.''
6156 This bug comes from @acronym{HP} compilers' mishandling of @code{sizeof (int)},
6157 not from the @code{? 1 : -1}, and
6158 Autoconf works around this problem by casting @code{sizeof (int)} to
6159 @code{long int} before comparing it.
6160 @end table
6162 @node Generic Compiler Characteristics
6163 @subsection Generic Compiler Characteristics
6165 @defmac AC_CHECK_SIZEOF (@var{type}, @ovar{unused}, @dvar{includes, default-includes})
6166 @acindex{CHECK_SIZEOF}
6167 Define @code{SIZEOF_@var{type}} (@pxref{Standard Symbols}) to be the
6168 size in bytes of @var{type}.  If @samp{type} is unknown, it gets a size
6169 of 0.  If no @var{includes} are specified, the default includes are used
6170 (@pxref{Default Includes}).  If you provide @var{include}, be sure to
6171 include @file{stdio.h} which is required for this macro to run.
6173 This macro now works even when cross-compiling.  The @var{unused}
6174 argument was used when cross-compiling.
6176 For example, the call
6178 @example
6179 AC_CHECK_SIZEOF([int *])
6180 @end example
6182 @noindent
6183 defines @code{SIZEOF_INT_P} to be 8 on DEC Alpha AXP systems.
6184 @end defmac
6186 @defmac AC_CHECK_ALIGNOF (@var{type}, @dvar{includes, default-includes})
6187 @acindex{CHECK_ALIGNOF}
6188 Define @code{ALIGNOF_@var{type}} (@pxref{Standard Symbols}) to be the
6189 alignment in bytes of @var{type}.  If @samp{type} is unknown, it gets a size
6190 of 0.  If no @var{includes} are specified, the default includes are used
6191 (@pxref{Default Includes}).  If you provide @var{include}, be sure to
6192 include @file{stddef.h} and @file{stdio.h} which are required for this
6193 macro to work correctly.
6194 @end defmac
6196 @defmac AC_LANG_WERROR
6197 @acindex{LANG_WERROR}
6198 Normally Autoconf ignores warnings generated by the compiler, linker, and
6199 preprocessor.  If this macro is used, warnings count as fatal
6200 errors for the current language.  This macro is useful when the
6201 results of configuration are used where warnings are unacceptable; for
6202 instance, if parts of a program are built with the @acronym{GCC}
6203 @option{-Werror}
6204 option.  If the whole program is built using @option{-Werror} it is
6205 often simpler to put @option{-Werror} in the compiler flags (@code{CFLAGS},
6206 etc.).
6207 @end defmac
6209 @node C Compiler
6210 @subsection C Compiler Characteristics
6212 The following macros provide ways to find and exercise a C Compiler.
6213 There are a few constructs that ought to be avoided, but do not deserve
6214 being checked for, since they can easily be worked around.
6216 @table @asis
6217 @item Don't use lines containing solitary backslashes
6218 They tickle a bug in the @acronym{HP-UX} C compiler (checked on
6219 @acronym{HP-UX} 10.20,
6220 11.00, and 11i).  When given the following source:
6222 @example
6223 #ifdef __STDC__
6225 * A comment with backslash-newlines in it.  %@{ %@} *\
6228 char str[] = "\\
6229 " A string with backslash-newlines in it %@{ %@} \\
6231 char apostrophe = '\\
6235 #endif
6236 @end example
6238 @noindent
6239 the compiler incorrectly fails with the diagnostics ``Non-terminating
6240 comment at end of file'' and ``Missing @samp{#endif} at end of file.''
6241 Removing the lines with solitary backslashes solves the problem.
6243 @item Don't compile several files at once if output matters to you
6244 Some compilers, such as @acronym{HP}'s, report names of files being
6245 compiled when given more than one file operand.  For instance:
6247 @example
6248 $ @kbd{cc a.c b.c}
6249 a.c:
6250 b.c:
6251 @end example
6253 @noindent
6254 This can cause problems if you observe the output of the compiler to
6255 detect failures.  Invoking @samp{cc -c a.c && cc -c b.c && cc -o c a.o
6256 b.o} solves the issue.
6258 @item Don't rely on @code{#error} failing
6259 The @sc{irix} C compiler does not fail when #error is preprocessed; it
6260 simply emits a diagnostic and continues, exiting successfully.  So,
6261 instead of an error directive like @code{#error "Unsupported word size"}
6262 it is more portable to use an invalid directive like @code{#Unsupported
6263 word size} in Autoconf tests.  In ordinary source code, @code{#error} is
6264 OK, since installers with inadequate compilers like @sc{irix} can simply
6265 examine these compilers' diagnostic output.
6267 @item Don't rely on correct @code{#line} support
6268 On Solaris, @command{c89} (at least Sun C 5.3 through 5.8)
6269 diagnoses @code{#line} directives whose line
6270 numbers are greater than 32767.  Nothing in Posix
6271 makes this invalid.  That is why Autoconf stopped issuing
6272 @code{#line} directives.
6273 @end table
6275 @defmac AC_PROG_CC (@ovar{compiler-search-list})
6276 @acindex{PROG_CC}
6277 @ovindex CC
6278 @ovindex CFLAGS
6279 Determine a C compiler to use.  If @code{CC} is not already set in the
6280 environment, check for @code{gcc} and @code{cc}, then for other C
6281 compilers.  Set output variable @code{CC} to the name of the compiler
6282 found.
6284 This macro may, however, be invoked with an optional first argument
6285 which, if specified, must be a blank-separated list of C compilers to
6286 search for.  This just gives the user an opportunity to specify an
6287 alternative search list for the C compiler.  For example, if you didn't
6288 like the default order, then you could invoke @code{AC_PROG_CC} like
6289 this:
6291 @example
6292 AC_PROG_CC([gcc cl cc])
6293 @end example
6295 If the C compiler does not handle function prototypes correctly by
6296 default, try to add an option to output variable @code{CC} to make it
6297 so.  This macro tries various options that select standard-conformance
6298 modes on various systems.
6300 After calling this macro you can check whether the C compiler has been
6301 set to accept @acronym{ANSI} C89 (@acronym{ISO} C90); if not, the shell
6302 variable
6303 @code{ac_cv_prog_cc_c89} is set to @samp{no}.  See also
6304 @code{AC_C_PROTOTYPES} below.
6306 If using the @acronym{GNU} C compiler, set shell variable @code{GCC} to
6307 @samp{yes}.  If output variable @code{CFLAGS} was not already set, set
6308 it to @option{-g -O2} for the @acronym{GNU} C compiler (@option{-O2} on systems
6309 where @acronym{GCC} does not accept @option{-g}), or @option{-g} for
6310 other compilers.
6311 @end defmac
6313 @defmac AC_PROG_CC_C_O
6314 @acindex{PROG_CC_C_O}
6315 @cvindex NO_MINUS_C_MINUS_O
6316 If the C compiler does not accept the @option{-c} and @option{-o} options
6317 simultaneously, define @code{NO_MINUS_C_MINUS_O}.  This macro actually
6318 tests both the compiler found by @code{AC_PROG_CC}, and, if different,
6319 the first @code{cc} in the path.  The test fails if one fails.  This
6320 macro was created for @acronym{GNU} Make to choose the default C compilation
6321 rule.
6322 @end defmac
6325 @defmac AC_PROG_CPP
6326 @acindex{PROG_CPP}
6327 @ovindex CPP
6328 Set output variable @code{CPP} to a command that runs the
6329 C preprocessor.  If @samp{$CC -E} doesn't work, @file{/lib/cpp} is used.
6330 It is only portable to run @code{CPP} on files with a @file{.c}
6331 extension.
6333 Some preprocessors don't indicate missing include files by the error
6334 status.  For such preprocessors an internal variable is set that causes
6335 other macros to check the standard error from the preprocessor and
6336 consider the test failed if any warnings have been reported.
6337 For most preprocessors, though, warnings do not cause include-file
6338 tests to fail unless @code{AC_PROG_CPP_WERROR} is also specified.
6339 @end defmac
6341 @defmac AC_PROG_CPP_WERROR
6342 @acindex{PROG_CPP_WERROR}
6343 @ovindex CPP
6344 This acts like @code{AC_PROG_CPP}, except it treats warnings from the
6345 preprocessor as errors even if the preprocessor exit status indicates
6346 success.  This is useful for avoiding headers that generate mandatory
6347 warnings, such as deprecation notices.
6348 @end defmac
6351 The following macros check for C compiler or machine architecture
6352 features.  To check for characteristics not listed here, use
6353 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
6354 @code{AC_RUN_IFELSE} (@pxref{Runtime}).
6356 @defmac AC_PROG_CC_STDC
6357 @acindex{PROG_CC_STDC}
6358 If the C compiler cannot compile @acronym{ISO} Standard C (currently
6359 C99), try to add an option to output variable @code{CC} to make it work.
6360 If the compiler does not support C99, fall back to supporting
6361 @acronym{ANSI} C89 (@acronym{ISO} C90).
6363 After calling this macro you can check whether the C compiler has been
6364 set to accept Standard C; if not, the shell variable
6365 @code{ac_cv_prog_cc_stdc} is set to @samp{no}.
6366 @end defmac
6368 @defmac AC_PROG_CC_C89
6369 @acindex{PROG_CC_C89}
6370 If the C compiler is not in @acronym{ANSI} C89 (@acronym{ISO} C90) mode by
6371 default, try to add an option to output variable @code{CC} to make it
6372 so.  This macro tries various options that select @acronym{ANSI} C89 on
6373 some system or another.  It considers the compiler to be in
6374 @acronym{ANSI} C89 mode if it handles function prototypes correctly.
6376 After calling this macro you can check whether the C compiler has been
6377 set to accept @acronym{ANSI} C89; if not, the shell variable
6378 @code{ac_cv_prog_cc_c89} is set to @samp{no}.
6380 This macro is called automatically by @code{AC_PROG_CC}.
6381 @end defmac
6383 @defmac AC_PROG_CC_C99
6384 @acindex{PROG_CC_C99}
6385 If the C compiler is not in C99 mode by default, try to add an
6386 option to output variable @code{CC} to make it so.  This macro tries
6387 various options that select C99 on some system or another.  It
6388 considers the compiler to be in C99 mode if it handles @code{_Bool},
6389 flexible arrays, @code{inline}, @code{long long int}, mixed code and
6390 declarations, named initialization of structs, @code{restrict}, varargs
6391 macros, variable declarations in @code{for} loops and variable length
6392 arrays.
6394 After calling this macro you can check whether the C compiler has been
6395 set to accept C99; if not, the shell variable
6396 @code{ac_cv_prog_cc_c99} is set to @samp{no}.
6397 @end defmac
6399 @defmac AC_C_BACKSLASH_A
6400 @acindex{HAVE_C_BACKSLASH_A}
6401 Define @samp{HAVE_C_BACKSLASH_A} to 1 if the C compiler understands
6402 @samp{\a}.
6404 This macro is obsolescent, as current C compilers understand @samp{\a}.
6405 New programs need not use this macro.
6406 @end defmac
6408 @defmac AC_C_BIGENDIAN (@ovar{action-if-true}, @ovar{action-if-false}, @ovar{action-if-unknown})
6409 @acindex{C_BIGENDIAN}
6410 @cvindex WORDS_BIGENDIAN
6411 @cindex Endianness
6412 If words are stored with the most significant byte first (like Motorola
6413 and SPARC CPUs), execute @var{action-if-true}.  If words are stored with
6414 the least significant byte first (like Intel and VAX CPUs), execute
6415 @var{action-if-false}.
6417 This macro runs a test-case if endianness cannot be determined from the
6418 system header files.  When cross-compiling, the test-case is not run but
6419 grep'ed for some magic values.  @var{action-if-unknown} is executed if
6420 the latter case fails to determine the byte sex of the host system.
6422 The default for @var{action-if-true} is to define
6423 @samp{WORDS_BIGENDIAN}.  The default for @var{action-if-false} is to do
6424 nothing.  And finally, the default for @var{action-if-unknown} is to
6425 abort configure and tell the installer which variable he should preset
6426 to bypass this test.
6427 @end defmac
6429 @defmac AC_C_CONST
6430 @acindex{C_CONST}
6431 @cvindex const
6432 If the C compiler does not fully support the @code{const} keyword,
6433 define @code{const} to be empty.  Some C compilers that do
6434 not define @code{__STDC__} do support @code{const}; some compilers that
6435 define @code{__STDC__} do not completely support @code{const}.  Programs
6436 can simply use @code{const} as if every C compiler supported it; for
6437 those that don't, the makefile or configuration header file
6438 defines it as empty.
6440 Occasionally installers use a C++ compiler to compile C code, typically
6441 because they lack a C compiler.  This causes problems with @code{const},
6442 because C and C++ treat @code{const} differently.  For example:
6444 @example
6445 const int foo;
6446 @end example
6448 @noindent
6449 is valid in C but not in C++.  These differences unfortunately cannot be
6450 papered over by defining @code{const} to be empty.
6452 If @command{autoconf} detects this situation, it leaves @code{const} alone,
6453 as this generally yields better results in practice.  However, using a
6454 C++ compiler to compile C code is not recommended or supported, and
6455 installers who run into trouble in this area should get a C compiler
6456 like @acronym{GCC} to compile their C code.
6458 This macro is obsolescent, as current C compilers support @code{const}.
6459 New programs need not use this macro.
6460 @end defmac
6462 @defmac AC_C_RESTRICT
6463 @acindex{C_RESTRICT}
6464 @cvindex restrict
6465 If the C compiler recognizes the @code{restrict} keyword, don't do anything.
6466 If it recognizes only a variant spelling (@code{__restrict},
6467 @code{__restrict__}, or @code{_Restrict}), then define
6468 @code{restrict} to that.
6469 Otherwise, define @code{restrict} to be empty.
6470 Thus, programs may simply use @code{restrict} as if every C compiler
6471 supported it; for those that do not, the makefile
6472 or configuration header defines it away.
6474 Although support in C++ for the @code{restrict} keyword is not
6475 required, several C++ compilers do accept the keyword.
6476 This macro works for them, too.
6477 @end defmac
6479 @defmac AC_C_VOLATILE
6480 @acindex{C_VOLATILE}
6481 @cvindex volatile
6482 If the C compiler does not understand the keyword @code{volatile},
6483 define @code{volatile} to be empty.  Programs can simply use
6484 @code{volatile} as if every C compiler supported it; for those that do
6485 not, the makefile or configuration header defines it as
6486 empty.
6488 If the correctness of your program depends on the semantics of
6489 @code{volatile}, simply defining it to be empty does, in a sense, break
6490 your code.  However, given that the compiler does not support
6491 @code{volatile}, you are at its mercy anyway.  At least your
6492 program compiles, when it wouldn't before.
6494 In general, the @code{volatile} keyword is a standard C feature, so
6495 you might expect that @code{volatile} is available only when
6496 @code{__STDC__} is defined.  However, Ultrix 4.3's native compiler does
6497 support volatile, but does not define @code{__STDC__}.
6499 This macro is obsolescent, as current C compilers support @code{volatile}.
6500 New programs need not use this macro.
6501 @end defmac
6503 @defmac AC_C_INLINE
6504 @acindex{C_INLINE}
6505 @cvindex inline
6506 If the C compiler supports the keyword @code{inline}, do nothing.
6507 Otherwise define @code{inline} to @code{__inline__} or @code{__inline}
6508 if it accepts one of those, otherwise define @code{inline} to be empty.
6509 @end defmac
6511 @defmac AC_C_CHAR_UNSIGNED
6512 @acindex{C_CHAR_UNSIGNED}
6513 @cvindex __CHAR_UNSIGNED__
6514 If the C type @code{char} is unsigned, define @code{__CHAR_UNSIGNED__},
6515 unless the C compiler predefines it.
6516 @end defmac
6518 @defmac AC_C_STRINGIZE
6519 @acindex{C_STRINGIZE}
6520 @cvindex HAVE_STRINGIZE
6521 If the C preprocessor supports the stringizing operator, define
6522 @code{HAVE_STRINGIZE}.  The stringizing operator is @samp{#} and is
6523 found in macros such as this:
6525 @example
6526 #define x(y) #y
6527 @end example
6529 This macro is obsolescent, as current C compilers support the
6530 stringizing operator.  New programs need not use this macro.
6531 @end defmac
6533 @defmac AC_C_TYPEOF
6534 @acindex{C_TYPEOF}
6535 @cvindex HAVE_TYPEOF
6536 @cvindex typeof
6537 If the C compiler supports @acronym{GCC}'s @code{typeof} syntax either
6538 directly or
6539 through a different spelling of the keyword (e.g., @code{__typeof__}),
6540 define @code{HAVE_TYPEOF}.  If the support is available only through a
6541 different spelling, define @code{typeof} to that spelling.
6542 @end defmac
6544 @defmac AC_C_PROTOTYPES
6545 @acindex{C_PROTOTYPES}
6546 @cvindex PROTOTYPES
6547 @cvindex __PROTOTYPES
6548 @cvindex PARAMS
6549 If function prototypes are understood by the compiler (as determined by
6550 @code{AC_PROG_CC}), define @code{PROTOTYPES} and @code{__PROTOTYPES}.
6551 Defining @code{__PROTOTYPES} is for the benefit of
6552 header files that cannot use macros that infringe on user name space.
6554 This macro is obsolescent, as current C compilers support prototypes.
6555 New programs need not use this macro.
6556 @end defmac
6558 @defmac AC_PROG_GCC_TRADITIONAL
6559 @acindex{PROG_GCC_TRADITIONAL}
6560 @ovindex CC
6561 Add @option{-traditional} to output variable @code{CC} if using the
6562 @acronym{GNU} C compiler and @code{ioctl} does not work properly without
6563 @option{-traditional}.  That usually happens when the fixed header files
6564 have not been installed on an old system.
6566 This macro is obsolescent, since current versions of the @acronym{GNU} C
6567 compiler fix the header files automatically when installed.
6568 @end defmac
6571 @node C++ Compiler
6572 @subsection C++ Compiler Characteristics
6575 @defmac AC_PROG_CXX (@ovar{compiler-search-list})
6576 @acindex{PROG_CXX}
6577 @ovindex CXX
6578 @ovindex CXXFLAGS
6579 Determine a C++ compiler to use.  Check whether the environment variable
6580 @code{CXX} or @code{CCC} (in that order) is set; if so, then set output
6581 variable @code{CXX} to its value.
6583 Otherwise, if the macro is invoked without an argument, then search for
6584 a C++ compiler under the likely names (first @code{g++} and @code{c++}
6585 then other names).  If none of those checks succeed, then as a last
6586 resort set @code{CXX} to @code{g++}.
6588 This macro may, however, be invoked with an optional first argument
6589 which, if specified, must be a blank-separated list of C++ compilers to
6590 search for.  This just gives the user an opportunity to specify an
6591 alternative search list for the C++ compiler.  For example, if you
6592 didn't like the default order, then you could invoke @code{AC_PROG_CXX}
6593 like this:
6595 @example
6596 AC_PROG_CXX([gcc cl KCC CC cxx cc++ xlC aCC c++ g++])
6597 @end example
6599 If using the @acronym{GNU} C++ compiler, set shell variable @code{GXX} to
6600 @samp{yes}.  If output variable @code{CXXFLAGS} was not already set, set
6601 it to @option{-g -O2} for the @acronym{GNU} C++ compiler (@option{-O2} on
6602 systems where G++ does not accept @option{-g}), or @option{-g} for other
6603 compilers.
6604 @end defmac
6606 @defmac AC_PROG_CXXCPP
6607 @acindex{PROG_CXXCPP}
6608 @ovindex CXXCPP
6609 Set output variable @code{CXXCPP} to a command that runs the C++
6610 preprocessor.  If @samp{$CXX -E} doesn't work, @file{/lib/cpp} is used.
6611 It is portable to run @code{CXXCPP} only on files with a @file{.c},
6612 @file{.C}, @file{.cc}, or @file{.cpp} extension.
6614 Some preprocessors don't indicate missing include files by the error
6615 status.  For such preprocessors an internal variable is set that causes
6616 other macros to check the standard error from the preprocessor and
6617 consider the test failed if any warnings have been reported.  However,
6618 it is not known whether such broken preprocessors exist for C++.
6619 @end defmac
6621 @defmac AC_PROG_CXX_C_O
6622 @acindex{PROG_CXX_C_O}
6623 @cvindex CXX_NO_MINUS_C_MINUS_O
6624 Test whether the C++ compiler accepts the options @option{-c} and
6625 @option{-o} simultaneously, and define @code{CXX_NO_MINUS_C_MINUS_O},
6626 if it does not.
6627 @end defmac
6630 @node Objective C Compiler
6631 @subsection Objective C Compiler Characteristics
6634 @defmac AC_PROG_OBJC (@ovar{compiler-search-list})
6635 @acindex{PROG_OBJC}
6636 @ovindex OBJC
6637 @ovindex OBJCFLAGS
6638 Determine an Objective C compiler to use.  If @code{OBJC} is not already
6639 set in the environment, check for Objective C compilers.  Set output
6640 variable @code{OBJC} to the name of the compiler found.
6642 This macro may, however, be invoked with an optional first argument
6643 which, if specified, must be a blank-separated list of Objective C compilers to
6644 search for.  This just gives the user an opportunity to specify an
6645 alternative search list for the Objective C compiler.  For example, if you
6646 didn't like the default order, then you could invoke @code{AC_PROG_OBJC}
6647 like this:
6649 @example
6650 AC_PROG_OBJC([gcc objcc objc])
6651 @end example
6653 If using the @acronym{GNU} Objective C compiler, set shell variable
6654 @code{GOBJC} to @samp{yes}.  If output variable @code{OBJCFLAGS} was not
6655 already set, set it to @option{-g -O2} for the @acronym{GNU} Objective C
6656 compiler (@option{-O2} on systems where @command{gcc} does not accept
6657 @option{-g}), or @option{-g} for other compilers.
6658 @end defmac
6660 @defmac AC_PROG_OBJCCPP
6661 @acindex{PROG_OBJCCPP}
6662 @ovindex OBJCCPP
6663 Set output variable @code{OBJCCPP} to a command that runs the Objective C
6664 preprocessor.  If @samp{$OBJC -E} doesn't work, @file{/lib/cpp} is used.
6665 @end defmac
6668 @node Erlang Compiler and Interpreter
6669 @subsection Erlang Compiler and Interpreter Characteristics
6670 @cindex Erlang
6672 Autoconf defines the following macros for determining paths to the essential
6673 Erlang/OTP programs:
6675 @defmac AC_ERLANG_PATH_ERLC (@ovar{value-if-not-found}, @ovar{path})
6676 @acindex{ERLANG_PATH_ERLC}
6677 @ovindex ERLC
6678 @ovindex ERLCFLAGS
6679 Determine an Erlang compiler to use.  If @code{ERLC} is not already set in the
6680 environment, check for @command{erlc}.  Set output variable @code{ERLC} to the
6681 complete path of the compiler command found.  In addition, if @code{ERLCFLAGS}
6682 is not set in the environment, set it to an empty value.
6684 The two optional arguments have the same meaning as the two last arguments of
6685 macro @code{AC_PROG_PATH} for looking for the @command{erlc} program.  For
6686 example, to look for @command{erlc} only in the @file{/usr/lib/erlang/bin}
6687 directory:
6689 @example
6690 AC_ERLANG_PATH_ERLC([not found], [/usr/lib/erlang/bin])
6691 @end example
6692 @end defmac
6694 @defmac AC_ERLANG_NEED_ERLC (@ovar{path})
6695 @acindex{ERLANG_NEED_ERLC}
6696 A simplified variant of the @code{AC_ERLANG_PATH_ERLC} macro, that prints an
6697 error message and exits the @command{configure} script if the @command{erlc}
6698 program is not found.
6699 @end defmac
6701 @defmac AC_ERLANG_PATH_ERL (@ovar{value-if-not-found}, @ovar{path})
6702 @acindex{ERLANG_PATH_ERL}
6703 @ovindex ERL
6704 Determine an Erlang interpreter to use.  If @code{ERL} is not already set in the
6705 environment, check for @command{erl}.  Set output variable @code{ERL} to the
6706 complete path of the interpreter command found.
6708 The two optional arguments have the same meaning as the two last arguments of
6709 macro @code{AC_PROG_PATH} for looking for the @command{erl} program.  For
6710 example, to look for @command{erl} only in the @file{/usr/lib/erlang/bin}
6711 directory:
6713 @example
6714 AC_ERLANG_PATH_ERL([not found], [/usr/lib/erlang/bin])
6715 @end example
6716 @end defmac
6718 @defmac AC_ERLANG_NEED_ERL (@ovar{path})
6719 @acindex{ERLANG_NEED_ERL}
6720 A simplified variant of the @code{AC_ERLANG_PATH_ERL} macro, that prints an
6721 error message and exits the @command{configure} script if the @command{erl}
6722 program is not found.
6723 @end defmac
6726 @node Fortran Compiler
6727 @subsection Fortran Compiler Characteristics
6728 @cindex Fortran
6729 @cindex F77
6731 The Autoconf Fortran support is divided into two categories: legacy
6732 Fortran 77 macros (@code{F77}), and modern Fortran macros (@code{FC}).
6733 The former are intended for traditional Fortran 77 code, and have output
6734 variables like @code{F77}, @code{FFLAGS}, and @code{FLIBS}.  The latter
6735 are for newer programs that can (or must) compile under the newer
6736 Fortran standards, and have output variables like @code{FC},
6737 @code{FCFLAGS}, and @code{FCLIBS}.
6739 Except for two new macros @code{AC_FC_SRCEXT} and
6740 @code{AC_FC_FREEFORM} (see below), the @code{FC} and @code{F77} macros
6741 behave almost identically, and so they are documented together in this
6742 section.
6745 @defmac AC_PROG_F77 (@ovar{compiler-search-list})
6746 @acindex{PROG_F77}
6747 @ovindex F77
6748 @ovindex FFLAGS
6749 Determine a Fortran 77 compiler to use.  If @code{F77} is not already
6750 set in the environment, then check for @code{g77} and @code{f77}, and
6751 then some other names.  Set the output variable @code{F77} to the name
6752 of the compiler found.
6754 This macro may, however, be invoked with an optional first argument
6755 which, if specified, must be a blank-separated list of Fortran 77
6756 compilers to search for.  This just gives the user an opportunity to
6757 specify an alternative search list for the Fortran 77 compiler.  For
6758 example, if you didn't like the default order, then you could invoke
6759 @code{AC_PROG_F77} like this:
6761 @example
6762 AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
6763 @end example
6765 If using @code{g77} (the @acronym{GNU} Fortran 77 compiler), then
6766 set the shell variable @code{G77} to @samp{yes}.
6767 If the output variable @code{FFLAGS} was not already set in the
6768 environment, then set it to @option{-g -02} for @code{g77} (or @option{-O2}
6769 where @code{g77} does not accept @option{-g}).  Otherwise, set
6770 @code{FFLAGS} to @option{-g} for all other Fortran 77 compilers.
6771 @end defmac
6773 @defmac AC_PROG_FC (@ovar{compiler-search-list}, @ovar{dialect})
6774 @acindex{PROG_FC}
6775 @ovindex FC
6776 @ovindex FCFLAGS
6777 Determine a Fortran compiler to use.  If @code{FC} is not already set in
6778 the environment, then @code{dialect} is a hint to indicate what Fortran
6779 dialect to search for; the default is to search for the newest available
6780 dialect.  Set the output variable @code{FC} to the name of the compiler
6781 found.
6783 By default, newer dialects are preferred over older dialects, but if
6784 @code{dialect} is specified then older dialects are preferred starting
6785 with the specified dialect.  @code{dialect} can currently be one of
6786 Fortran 77, Fortran 90, or Fortran 95.  However, this is only a hint of
6787 which compiler @emph{name} to prefer (e.g., @code{f90} or @code{f95}),
6788 and no attempt is made to guarantee that a particular language standard
6789 is actually supported.  Thus, it is preferable that you avoid the
6790 @code{dialect} option, and use AC_PROG_FC only for code compatible with
6791 the latest Fortran standard.
6793 This macro may, alternatively, be invoked with an optional first argument
6794 which, if specified, must be a blank-separated list of Fortran
6795 compilers to search for, just as in @code{AC_PROG_F77}.
6797 If the output variable @code{FCFLAGS} was not already set in the
6798 environment, then set it to @option{-g -02} for @acronym{GNU} @code{g77} (or
6799 @option{-O2} where @code{g77} does not accept @option{-g}).  Otherwise,
6800 set @code{FCFLAGS} to @option{-g} for all other Fortran compilers.
6801 @end defmac
6803 @defmac AC_PROG_F77_C_O
6804 @defmacx AC_PROG_FC_C_O
6805 @acindex{PROG_F77_C_O}
6806 @acindex{PROG_FC_C_O}
6807 @cvindex F77_NO_MINUS_C_MINUS_O
6808 @cvindex FC_NO_MINUS_C_MINUS_O
6809 Test whether the Fortran compiler accepts the options @option{-c} and
6810 @option{-o} simultaneously, and define @code{F77_NO_MINUS_C_MINUS_O} or
6811 @code{FC_NO_MINUS_C_MINUS_O}, respectively, if it does not.
6812 @end defmac
6814 The following macros check for Fortran compiler characteristics.
6815 To check for characteristics not listed here, use
6816 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
6817 @code{AC_RUN_IFELSE} (@pxref{Runtime}), making sure to first set the
6818 current language to Fortran 77 or Fortran via @code{AC_LANG([Fortran 77])}
6819 or @code{AC_LANG(Fortran)} (@pxref{Language Choice}).
6822 @defmac AC_F77_LIBRARY_LDFLAGS
6823 @defmacx AC_FC_LIBRARY_LDFLAGS
6824 @acindex{F77_LIBRARY_LDFLAGS}
6825 @ovindex FLIBS
6826 @acindex{FC_LIBRARY_LDFLAGS}
6827 @ovindex FCLIBS
6828 Determine the linker flags (e.g., @option{-L} and @option{-l}) for the
6829 @dfn{Fortran intrinsic and runtime libraries} that are required to
6830 successfully link a Fortran program or shared library.  The output
6831 variable @code{FLIBS} or @code{FCLIBS} is set to these flags (which
6832 should be included after @code{LIBS} when linking).
6834 This macro is intended to be used in those situations when it is
6835 necessary to mix, e.g., C++ and Fortran source code in a single
6836 program or shared library (@pxref{Mixing Fortran 77 With C and C++, , ,
6837 automake, @acronym{GNU} Automake}).
6839 For example, if object files from a C++ and Fortran compiler must be
6840 linked together, then the C++ compiler/linker must be used for linking
6841 (since special C++-ish things need to happen at link time like calling
6842 global constructors, instantiating templates, enabling exception
6843 support, etc.).
6845 However, the Fortran intrinsic and runtime libraries must be linked in
6846 as well, but the C++ compiler/linker doesn't know by default how to add
6847 these Fortran 77 libraries.  Hence, this macro was created to determine
6848 these Fortran libraries.
6850 The macros @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
6851 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} are probably also necessary to
6852 link C/C++ with Fortran; see below.
6853 @end defmac
6855 @defmac AC_F77_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
6856 @defmacx AC_FC_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
6857 @acindex{F77_DUMMY_MAIN}
6858 @cvindex F77_DUMMY_MAIN
6859 With many compilers, the Fortran libraries detected by
6860 @code{AC_F77_LIBRARY_LDFLAGS} or @code{AC_FC_LIBRARY_LDFLAGS} provide
6861 their own @code{main} entry function that initializes things like
6862 Fortran I/O, and which then calls a user-provided entry function named
6863 (say) @code{MAIN__} to run the user's program.  The
6864 @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
6865 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros figure out how to deal with
6866 this interaction.
6868 When using Fortran for purely numerical functions (no I/O, etc.)@: often
6869 one prefers to provide one's own @code{main} and skip the Fortran
6870 library initializations.  In this case, however, one may still need to
6871 provide a dummy @code{MAIN__} routine in order to prevent linking errors
6872 on some systems.  @code{AC_F77_DUMMY_MAIN} or @code{AC_FC_DUMMY_MAIN}
6873 detects whether any such routine is @emph{required} for linking, and
6874 what its name is; the shell variable @code{F77_DUMMY_MAIN} or
6875 @code{FC_DUMMY_MAIN} holds this name, @code{unknown} when no solution
6876 was found, and @code{none} when no such dummy main is needed.
6878 By default, @var{action-if-found} defines @code{F77_DUMMY_MAIN} or
6879 @code{FC_DUMMY_MAIN} to the name of this routine (e.g., @code{MAIN__})
6880 @emph{if} it is required.  @var{action-if-not-found} defaults to
6881 exiting with an error.
6883 In order to link with Fortran routines, the user's C/C++ program should
6884 then include the following code to define the dummy main if it is
6885 needed:
6887 @example
6888 #ifdef F77_DUMMY_MAIN
6889 #  ifdef __cplusplus
6890      extern "C"
6891 #  endif
6892    int F77_DUMMY_MAIN() @{ return 1; @}
6893 #endif
6894 @end example
6896 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
6898 Note that this macro is called automatically from @code{AC_F77_WRAPPERS}
6899 or @code{AC_FC_WRAPPERS}; there is generally no need to call it
6900 explicitly unless one wants to change the default actions.
6901 @end defmac
6903 @defmac AC_F77_MAIN
6904 @defmacx AC_FC_MAIN
6905 @acindex{F77_MAIN}
6906 @cvindex F77_MAIN
6907 @acindex{FC_MAIN}
6908 @cvindex FC_MAIN
6909 As discussed above, many Fortran libraries allow you to provide an entry
6910 point called (say) @code{MAIN__} instead of the usual @code{main}, which
6911 is then called by a @code{main} function in the Fortran libraries that
6912 initializes things like Fortran I/O@.  The
6913 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros detect whether it is
6914 @emph{possible} to utilize such an alternate main function, and defines
6915 @code{F77_MAIN} and @code{FC_MAIN} to the name of the function.  (If no
6916 alternate main function name is found, @code{F77_MAIN} and @code{FC_MAIN} are
6917 simply defined to @code{main}.)
6919 Thus, when calling Fortran routines from C that perform things like I/O,
6920 one should use this macro and name the "main" function
6921 @code{F77_MAIN} or @code{FC_MAIN} instead of @code{main}.
6922 @end defmac
6924 @defmac AC_F77_WRAPPERS
6925 @defmacx AC_FC_WRAPPERS
6926 @acindex{F77_WRAPPERS}
6927 @cvindex F77_FUNC
6928 @cvindex F77_FUNC_
6929 @acindex{FC_WRAPPERS}
6930 @cvindex FC_FUNC
6931 @cvindex FC_FUNC_
6932 Defines C macros @code{F77_FUNC (name, NAME)}, @code{FC_FUNC (name, NAME)},
6933 @code{F77_FUNC_(name, NAME)}, and @code{FC_FUNC_(name, NAME)} to properly
6934 mangle the names of C/C++ identifiers, and identifiers with underscores,
6935 respectively, so that they match the name-mangling scheme used by the
6936 Fortran compiler.
6938 Fortran is case-insensitive, and in order to achieve this the Fortran
6939 compiler converts all identifiers into a canonical case and format.  To
6940 call a Fortran subroutine from C or to write a C function that is
6941 callable from Fortran, the C program must explicitly use identifiers in
6942 the format expected by the Fortran compiler.  In order to do this, one
6943 simply wraps all C identifiers in one of the macros provided by
6944 @code{AC_F77_WRAPPERS} or @code{AC_FC_WRAPPERS}.  For example, suppose
6945 you have the following Fortran 77 subroutine:
6947 @example
6948       subroutine foobar (x, y)
6949       double precision x, y
6950       y = 3.14159 * x
6951       return
6952       end
6953 @end example
6955 You would then declare its prototype in C or C++ as:
6957 @example
6958 #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
6959 #ifdef __cplusplus
6960 extern "C"  /* prevent C++ name mangling */
6961 #endif
6962 void FOOBAR_F77(double *x, double *y);
6963 @end example
6965 Note that we pass both the lowercase and uppercase versions of the
6966 function name to @code{F77_FUNC} so that it can select the right one.
6967 Note also that all parameters to Fortran 77 routines are passed as
6968 pointers (@pxref{Mixing Fortran 77 With C and C++, , , automake, @acronym{GNU}
6969 Automake}).
6971 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
6973 Although Autoconf tries to be intelligent about detecting the
6974 name-mangling scheme of the Fortran compiler, there may be Fortran
6975 compilers that it doesn't support yet.  In this case, the above code
6976 generates a compile-time error, but some other behavior
6977 (e.g., disabling Fortran-related features) can be induced by checking
6978 whether @code{F77_FUNC} or @code{FC_FUNC} is defined.
6980 Now, to call that routine from a C program, we would do something like:
6982 @example
6984     double x = 2.7183, y;
6985     FOOBAR_F77 (&x, &y);
6987 @end example
6989 If the Fortran identifier contains an underscore (e.g., @code{foo_bar}),
6990 you should use @code{F77_FUNC_} or @code{FC_FUNC_} instead of
6991 @code{F77_FUNC} or @code{FC_FUNC} (with the same arguments).  This is
6992 because some Fortran compilers mangle names differently if they contain
6993 an underscore.
6994 @end defmac
6996 @defmac AC_F77_FUNC (@var{name}, @ovar{shellvar})
6997 @defmacx AC_FC_FUNC (@var{name}, @ovar{shellvar})
6998 @acindex{F77_FUNC}
6999 @acindex{FC_FUNC}
7000 Given an identifier @var{name}, set the shell variable @var{shellvar} to
7001 hold the mangled version @var{name} according to the rules of the
7002 Fortran linker (see also @code{AC_F77_WRAPPERS} or
7003 @code{AC_FC_WRAPPERS}).  @var{shellvar} is optional; if it is not
7004 supplied, the shell variable is simply @var{name}.  The purpose of
7005 this macro is to give the caller a way to access the name-mangling
7006 information other than through the C preprocessor as above, for example,
7007 to call Fortran routines from some language other than C/C++.
7008 @end defmac
7010 @defmac AC_FC_SRCEXT (@var{ext}, @ovar{action-if-success}, @ovar{action-if-failure})
7011 @acindex{FC_SRCEXT}
7012 By default, the @code{FC} macros perform their tests using a @file{.f}
7013 extension for source-code files.  Some compilers, however, only enable
7014 newer language features for appropriately named files, e.g., Fortran 90
7015 features only for @file{.f90} files.  On the other hand, some other
7016 compilers expect all source files to end in @file{.f} and require
7017 special flags to support other file name extensions.  The
7018 @code{AC_FC_SRCEXT} macro deals with both of these issues.
7020 The @code{AC_FC_SRCEXT} tries to get the @code{FC} compiler to accept files
7021 ending with the extension .@var{ext} (i.e., @var{ext} does @emph{not}
7022 contain the dot).  If any special compiler flags are needed for this, it
7023 stores them in the output variable @code{FCFLAGS_}@var{ext}.  This
7024 extension and these flags are then used for all subsequent @code{FC} tests
7025 (until @code{AC_FC_SRCEXT} is called again).
7027 For example, you would use @code{AC_FC_SRCEXT(f90)} to employ the
7028 @file{.f90} extension in future tests, and it would set a
7029 @code{FCFLAGS_f90} output variable with any extra flags that are needed
7030 to compile such files.
7032 The @code{FCFLAGS_}@var{ext} can @emph{not} be simply absorbed into
7033 @code{FCFLAGS}, for two reasons based on the limitations of some
7034 compilers.  First, only one @code{FCFLAGS_}@var{ext} can be used at a
7035 time, so files with different extensions must be compiled separately.
7036 Second, @code{FCFLAGS_}@var{ext} must appear @emph{immediately} before
7037 the source-code file name when compiling.  So, continuing the example
7038 above, you might compile a @file{foo.f90} file in your makefile with the
7039 command:
7041 @example
7042 foo.o: foo.f90
7043      $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) '$(srcdir)/foo.f90'
7044 @end example
7046 If @code{AC_FC_SRCEXT} succeeds in compiling files with the @var{ext}
7047 extension, it calls @var{action-if-success} (defaults to nothing).  If
7048 it fails, and cannot find a way to make the @code{FC} compiler accept such
7049 files, it calls @var{action-if-failure} (defaults to exiting with an
7050 error message).
7052 @end defmac
7054 @defmac AC_FC_FREEFORM (@ovar{action-if-success}, @ovar{action-if-failure})
7055 @acindex{FC_FREEFORM}
7057 The @code{AC_FC_FREEFORM} tries to ensure that the Fortran compiler
7058 (@code{$FC}) allows free-format source code (as opposed to the older
7059 fixed-format style from Fortran 77).  If necessary, it may add some
7060 additional flags to @code{FCFLAGS}.
7062 This macro is most important if you are using the default @file{.f}
7063 extension, since many compilers interpret this extension as indicating
7064 fixed-format source unless an additional flag is supplied.  If you
7065 specify a different extension with @code{AC_FC_SRCEXT}, such as
7066 @file{.f90} or @file{.f95}, then @code{AC_FC_FREEFORM} ordinarily
7067 succeeds without modifying @code{FCFLAGS}.
7069 If @code{AC_FC_FREEFORM} succeeds in compiling free-form source, it
7070 calls @var{action-if-success} (defaults to nothing).  If it fails, it
7071 calls @var{action-if-failure} (defaults to exiting with an error
7072 message).
7073 @end defmac
7075 @node System Services
7076 @section System Services
7078 The following macros check for operating system services or capabilities.
7080 @defmac AC_PATH_X
7081 @acindex{PATH_X}
7082 @evindex XMKMF
7083 @cindex X Window System
7084 Try to locate the X Window System include files and libraries.  If the
7085 user gave the command line options @option{--x-includes=@var{dir}} and
7086 @option{--x-libraries=@var{dir}}, use those directories.
7088 If either or both were not given, get the missing values by running
7089 @code{xmkmf} (or an executable pointed to by the @code{XMKMF}
7090 environment variable) on a trivial @file{Imakefile} and examining the
7091 makefile that it produces.  Setting @code{XMKMF} to @samp{false}
7092 disables this method.
7094 If this method fails to find the X Window System, @command{configure}
7095 looks for the files in several directories where they often reside.
7096 If either method is successful, set the shell variables
7097 @code{x_includes} and @code{x_libraries} to their locations, unless they
7098 are in directories the compiler searches by default.
7100 If both methods fail, or the user gave the command line option
7101 @option{--without-x}, set the shell variable @code{no_x} to @samp{yes};
7102 otherwise set it to the empty string.
7103 @end defmac
7105 @defmac AC_PATH_XTRA
7106 @acindex{PATH_XTRA}
7107 @ovindex X_CFLAGS
7108 @ovindex X_LIBS
7109 @ovindex X_EXTRA_LIBS
7110 @ovindex X_PRE_LIBS
7111 @cvindex X_DISPLAY_MISSING
7112 An enhanced version of @code{AC_PATH_X}.  It adds the C compiler flags
7113 that X needs to output variable @code{X_CFLAGS}, and the X linker flags
7114 to @code{X_LIBS}.  Define @code{X_DISPLAY_MISSING} if X is not
7115 available.
7117 This macro also checks for special libraries that some systems need in
7118 order to compile X programs.  It adds any that the system needs to
7119 output variable @code{X_EXTRA_LIBS}.  And it checks for special X11R6
7120 libraries that need to be linked with before @option{-lX11}, and adds
7121 any found to the output variable @code{X_PRE_LIBS}.
7123 @c This is an incomplete kludge.  Make a real way to do it.
7124 @c If you need to check for other X functions or libraries yourself, then
7125 @c after calling this macro, add the contents of @code{X_EXTRA_LIBS} to
7126 @c @code{LIBS} temporarily, like this: (FIXME - add example)
7127 @end defmac
7129 @defmac AC_SYS_INTERPRETER
7130 @acindex{SYS_INTERPRETER}
7131 Check whether the system supports starting scripts with a line of the
7132 form @samp{#!/bin/sh} to select the interpreter to use for the script.
7133 After running this macro, shell code in @file{configure.ac} can check
7134 the shell variable @code{interpval}; it is set to @samp{yes}
7135 if the system supports @samp{#!}, @samp{no} if not.
7136 @end defmac
7138 @defmac AC_SYS_LARGEFILE
7139 @acindex{SYS_LARGEFILE}
7140 @cvindex _FILE_OFFSET_BITS
7141 @cvindex _LARGE_FILES
7142 @ovindex CC
7143 @cindex Large file support
7144 @cindex LFS
7145 Arrange for
7146 @uref{http://www.unix-systems.org/@/version2/@/whatsnew/@/lfs20mar.html,
7147 large-file support}.  On some hosts, one must use special compiler
7148 options to build programs that can access large files.  Append any such
7149 options to the output variable @code{CC}.  Define
7150 @code{_FILE_OFFSET_BITS} and @code{_LARGE_FILES} if necessary.
7152 Large-file support can be disabled by configuring with the
7153 @option{--disable-largefile} option.
7155 If you use this macro, check that your program works even when
7156 @code{off_t} is wider than @code{long int}, since this is common when
7157 large-file support is enabled.  For example, it is not correct to print
7158 an arbitrary @code{off_t} value @code{X} with @code{printf ("%ld",
7159 (long int) X)}.
7161 The LFS introduced the @code{fseeko} and @code{ftello} functions to
7162 replace their C counterparts @code{fseek} and @code{ftell} that do not
7163 use @code{off_t}.  Take care to use @code{AC_FUNC_FSEEKO} to make their
7164 prototypes available when using them and large-file support is
7165 enabled.
7166 @end defmac
7168 @defmac AC_SYS_LONG_FILE_NAMES
7169 @acindex{SYS_LONG_FILE_NAMES}
7170 @cvindex HAVE_LONG_FILE_NAMES
7171 If the system supports file names longer than 14 characters, define
7172 @code{HAVE_LONG_FILE_NAMES}.
7173 @end defmac
7175 @defmac AC_SYS_POSIX_TERMIOS
7176 @acindex{SYS_POSIX_TERMIOS}
7177 @cindex Posix termios headers
7178 @cindex termios Posix headers
7179 Check to see if the Posix termios headers and functions are available on the
7180 system.  If so, set the shell variable @code{ac_cv_sys_posix_termios} to
7181 @samp{yes}.  If not, set the variable to @samp{no}.
7182 @end defmac
7184 @node Posix Variants
7185 @section Posix Variants
7187 The following macros check for certain operating systems that need
7188 special treatment for some programs, due to exceptional oddities in
7189 their header files or libraries.  These macros are warts; they will be
7190 replaced by a more systematic approach, based on the functions they make
7191 available or the environments they provide.
7193 @defmac AC_AIX
7194 @acindex{AIX}
7195 @cvindex _ALL_SOURCE
7196 If on @acronym{AIX}, define @code{_ALL_SOURCE}.
7197 Allows the use of some @acronym{BSD}
7198 functions.  Should be called before any macros that run the C compiler.
7199 @end defmac
7201 @defmac AC_GNU_SOURCE
7202 @acindex{GNU_SOURCE}
7203 @cvindex _GNU_SOURCE
7204 If using the @acronym{GNU} C library, define @code{_GNU_SOURCE}.
7205 Allows the use of some @acronym{GNU} functions.  Should be called
7206 before any macros that run the C compiler.
7207 @end defmac
7209 @defmac AC_ISC_POSIX
7210 @acindex{ISC_POSIX}
7211 @ovindex LIBS
7212 For @sc{interactive} Systems Corporation Unix, add @option{-lcposix} to output
7213 variable @code{LIBS} if necessary for Posix facilities.  Call this
7214 after @code{AC_PROG_CC} and before any other macros that use Posix
7215 interfaces.  @sc{interactive} Unix is no longer sold, and Sun says that
7216 they will drop support for it on 2006-07-23, so this macro is becoming
7217 obsolescent.
7218 @end defmac
7220 @defmac AC_MINIX
7221 @acindex{MINIX}
7222 @cvindex _MINIX
7223 @cvindex _POSIX_SOURCE
7224 @cvindex _POSIX_1_SOURCE
7225 If on Minix, define @code{_MINIX} and @code{_POSIX_SOURCE} and define
7226 @code{_POSIX_1_SOURCE} to be 2.  This allows the use of Posix
7227 facilities.  Should be called before any macros that run the C compiler.
7228 @end defmac
7230 @defmac AC_USE_SYSTEM_EXTENSIONS
7231 @acindex{USE_SYSTEM_EXTENSIONS}
7232 @cvindex _ALL_SOURCE
7233 @cvindex _GNU_SOURCE
7234 @cvindex _MINIX
7235 @cvindex _POSIX_1_SOURCE
7236 @cvindex _POSIX_PTHREAD_SEMANTICS
7237 @cvindex _POSIX_SOURCE
7238 @cvindex __EXTENSIONS__
7239 If possible, enable extensions to Posix on hosts that normally disable
7240 the extensions, typically due to standards-conformance namespace issues.
7241 This may involve defining @code{__EXTENSIONS__} and
7242 @code{_POSIX_PTHREAD_SEMANTICS}, which are macros used by Solaris.  This
7243 macro also has the combined effects of @code{AC_GNU_SOURCE},
7244 @code{AC_AIX}, and @code{AC_MINIX}.
7245 @end defmac
7248 @node Erlang Libraries
7249 @section Erlang Libraries
7250 @cindex Erlang, Library, checking
7252 The following macros check for an installation of Erlang/OTP, and for the
7253 presence of certain Erlang libraries.  All those macros require the
7254 configuration of an Erlang interpreter and an Erlang compiler
7255 (@pxref{Erlang Compiler and Interpreter}).
7257 @defmac AC_ERLANG_SUBST_ROOT_DIR
7258 @acindex{ERLANG_SUBST_ROOT_DIR}
7259 @ovindex ERLANG_ROOT_DIR
7261 Set the output variable @code{ERLANG_ROOT_DIR} to the path to the base directory
7262 in which Erlang/OTP is installed (as returned by Erlang's @code{code:root_dir/0}
7263 function).  The result of this test is cached if caching is enabled when running
7264 @command{configure}.
7265 @end defmac
7267 @defmac AC_ERLANG_SUBST_LIB_DIR
7268 @acindex{ERLANG_SUBST_LIB_DIR}
7269 @ovindex ERLANG_LIB_DIR
7271 Set the output variable @code{ERLANG_LIB_DIR} to the path of the library
7272 directory of Erlang/OTP (as returned by Erlang's
7273 @code{code:lib_dir/0} function), which subdirectories each contain an installed
7274 Erlang/OTP library.  The result of this test is cached if caching is enabled
7275 when running @command{configure}.
7276 @end defmac
7278 @defmac AC_ERLANG_CHECK_LIB (@var{library}, @ovar{action-if-found}, @ovar{action-if-not-found})
7279 @acindex{ERLANG_CHECK_LIB}
7280 @ovindex ERLANG_LIB_DIR_@var{library}
7282 Test whether the Erlang/OTP library @var{library} is installed by calling
7283 Erlang's @code{code:lib_dir/1} function.  The result of this test is cached if
7284 caching is enabled when running @command{configure}.  @var{action-if-found} is a
7285 list of shell commands to run if the library is installed;
7286 @var{action-if-not-found} is a list of shell commands to run if it is not.
7287 Additionally, if the library is installed, the output variable
7288 @samp{ERLANG_LIB_DIR_@var{library}} is set to the path to the library
7289 installation directory.  For example, to check if library @code{stdlib} is
7290 installed:
7292 @example
7293 AC_ERLANG_CHECK_LIB([stdlib],
7294   [echo "stdlib is installed in $ERLANG_LIB_DIR_stdlib"],
7295   [AC_MSG_ERROR([stdlib was not found!])])
7296 @end example
7297 @end defmac
7299 In addition to the above macros, which test installed Erlang libraries, the
7300 following macros determine the paths to the directories into which newly built
7301 Erlang libraries are to be installed:
7303 @defmac AC_ERLANG_SUBST_INSTALL_LIB_DIR
7304 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
7305 @ovindex ERLANG_INSTALL_LIB_DIR
7307 Set the @code{ERLANG_INSTALL_LIB_DIR} output variable to the directory into
7308 which every built Erlang library should be installed in a separate subdirectory.
7309 If this variable is not set in the environment when @command{configure} runs,
7310 its default value is @code{$ERLANG_LIB_DIR}, which value is set by the
7311 @code{AC_ERLANG_SUBST_LIB_DIR} macro.
7312 @end defmac
7314 @defmac AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR (@var{library}, @var{version})
7315 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
7316 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
7318 Set the @samp{ERLANG_INSTALL_LIB_DIR_@var{library}} output variable to the
7319 directory into which the built Erlang library @var{library} version
7320 @var{version} should be installed.  If this variable is not set in the
7321 environment when @command{configure} runs, its default value is
7322 @samp{$ERLANG_INSTALL_LIB_DIR/@var{library}-@var{version}}, the value of the
7323 @code{ERLANG_INSTALL_LIB_DIR} variable being set by the
7324 @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR} macro.
7325 @end defmac
7331 @c ========================================================= Writing Tests
7333 @node Writing Tests
7334 @chapter Writing Tests
7336 If the existing feature tests don't do something you need, you have to
7337 write new ones.  These macros are the building blocks.  They provide
7338 ways for other macros to check whether various kinds of features are
7339 available and report the results.
7341 This chapter contains some suggestions and some of the reasons why the
7342 existing tests are written the way they are.  You can also learn a lot
7343 about how to write Autoconf tests by looking at the existing ones.  If
7344 something goes wrong in one or more of the Autoconf tests, this
7345 information can help you understand the assumptions behind them, which
7346 might help you figure out how to best solve the problem.
7348 These macros check the output of the compiler system of the current
7349 language (@pxref{Language Choice}).  They do not cache the results of
7350 their tests for future use (@pxref{Caching Results}), because they don't
7351 know enough about the information they are checking for to generate a
7352 cache variable name.  They also do not print any messages, for the same
7353 reason.  The checks for particular kinds of features call these macros
7354 and do cache their results and print messages about what they're
7355 checking for.
7357 When you write a feature test that could be applicable to more than one
7358 software package, the best thing to do is encapsulate it in a new macro.
7359 @xref{Writing Autoconf Macros}, for how to do that.
7361 @menu
7362 * Language Choice::             Selecting which language to use for testing
7363 * Writing Test Programs::       Forging source files for compilers
7364 * Running the Preprocessor::    Detecting preprocessor symbols
7365 * Running the Compiler::        Detecting language or header features
7366 * Running the Linker::          Detecting library features
7367 * Runtime::                     Testing for runtime features
7368 * Systemology::                 A zoology of operating systems
7369 * Multiple Cases::              Tests for several possible values
7370 @end menu
7372 @node Language Choice
7373 @section Language Choice
7374 @cindex Language
7376 Autoconf-generated @command{configure} scripts check for the C compiler and
7377 its features by default.  Packages that use other programming languages
7378 (maybe more than one, e.g., C and C++) need to test features of the
7379 compilers for the respective languages.  The following macros determine
7380 which programming language is used in the subsequent tests in
7381 @file{configure.ac}.
7383 @defmac AC_LANG (@var{language})
7384 Do compilation tests using the compiler, preprocessor, and file
7385 extensions for the specified @var{language}.
7387 Supported languages are:
7389 @table @samp
7390 @item C
7391 Do compilation tests using @code{CC} and @code{CPP} and use extension
7392 @file{.c} for test programs.  Use compilation flags: @code{CPPFLAGS} with
7393 @code{CPP}, and both @code{CPPFLAGS} and @code{CFLAGS} with @code{CC}.
7395 @item C++
7396 Do compilation tests using @code{CXX} and @code{CXXCPP} and use
7397 extension @file{.C} for test programs.  Use compilation flags:
7398 @code{CPPFLAGS} with @code{CXXPP}, and both @code{CPPFLAGS} and
7399 @code{CXXFLAGS} with @code{CXX}.
7401 @item Fortran 77
7402 Do compilation tests using @code{F77} and use extension @file{.f} for
7403 test programs.  Use compilation flags: @code{FFLAGS}.
7405 @item Fortran
7406 Do compilation tests using @code{FC} and use extension @file{.f} (or
7407 whatever has been set by @code{AC_FC_SRCEXT}) for test programs.  Use
7408 compilation flags: @code{FCFLAGS}.
7410 @item Erlang
7411 @ovindex ERLC
7412 @ovindex ERL
7413 @ovindex ERLCFLAGS
7414 Compile and execute tests using @code{ERLC} and @code{ERL} and use extension
7415 @file{.erl} for test Erlang modules.  Use compilation flags: @code{ERLCFLAGS}.
7417 @item Objective C
7418 Do compilation tests using @code{OBJC} and @code{OBJCCPP} and use
7419 extension @file{.m} for test programs.  Use compilation flags:
7420 @code{CPPFLAGS} with @code{OBJCPP}, and both @code{CPPFLAGS} and
7421 @code{OBJCFLAGS} with @code{OBJC}.
7422 @end table
7423 @end defmac
7425 @defmac AC_LANG_PUSH (@var{language})
7426 @acindex{LANG_PUSH}
7427 Remember the current language (as set by @code{AC_LANG}) on a stack, and
7428 then select the @var{language}.  Use this macro and @code{AC_LANG_POP}
7429 in macros that need to temporarily switch to a particular language.
7430 @end defmac
7432 @defmac AC_LANG_POP (@ovar{language})
7433 @acindex{LANG_POP}
7434 Select the language that is saved on the top of the stack, as set by
7435 @code{AC_LANG_PUSH}, and remove it from the stack.
7437 If given, @var{language} specifies the language we just @emph{quit}.  It
7438 is a good idea to specify it when it's known (which should be the
7439 case@dots{}), since Autoconf detects inconsistencies.
7441 @example
7442 AC_LANG_PUSH([Fortran 77])
7443 # Perform some tests on Fortran 77.
7444 # @dots{}
7445 AC_LANG_POP([Fortran 77])
7446 @end example
7447 @end defmac
7449 @defmac AC_LANG_ASSERT (@var{language})
7450 @acindex{LANG_ASSERT} Check statically that the current language is
7451 @var{language}.  You should use this in your language specific macros
7452 to avoid that they be called with an inappropriate language.
7454 This macro runs only at @command{autoconf} time, and incurs no cost at
7455 @command{configure} time.  Sadly enough and because Autoconf is a two
7456 layer language @footnote{Because M4 is not aware of Sh code,
7457 especially conditionals, some optimizations that look nice statically
7458 may produce incorrect results at runtime.}, the macros
7459 @code{AC_LANG_PUSH} and @code{AC_LANG_POP} cannot be ``optimizing'',
7460 therefore as much as possible you ought to avoid using them to wrap
7461 your code, rather, require from the user to run the macro with a
7462 correct current language, and check it with @code{AC_LANG_ASSERT}.
7463 And anyway, that may help the user understand she is running a Fortran
7464 macro while expecting a result about her Fortran 77 compiler@dots{}
7465 @end defmac
7468 @defmac AC_REQUIRE_CPP
7469 @acindex{REQUIRE_CPP}
7470 Ensure that whichever preprocessor would currently be used for tests has
7471 been found.  Calls @code{AC_REQUIRE} (@pxref{Prerequisite Macros}) with an
7472 argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
7473 depending on which language is current.
7474 @end defmac
7477 @node Writing Test Programs
7478 @section Writing Test Programs
7480 Autoconf tests follow a common scheme: feed some program with some
7481 input, and most of the time, feed a compiler with some source file.
7482 This section is dedicated to these source samples.
7484 @menu
7485 * Guidelines::                  General rules for writing test programs
7486 * Test Functions::              Avoiding pitfalls in test programs
7487 * Generating Sources::          Source program boilerplate
7488 @end menu
7490 @node Guidelines
7491 @subsection Guidelines for Test Programs
7493 The most important rule to follow when writing testing samples is:
7495 @center @emph{Look for realism.}
7497 This motto means that testing samples must be written with the same
7498 strictness as real programs are written.  In particular, you should
7499 avoid ``shortcuts'' and simplifications.
7501 Don't just play with the preprocessor if you want to prepare a
7502 compilation.  For instance, using @command{cpp} to check whether a header is
7503 functional might let your @command{configure} accept a header which
7504 causes some @emph{compiler} error.  Do not hesitate to check a header with
7505 other headers included before, especially required headers.
7507 Make sure the symbols you use are properly defined, i.e., refrain for
7508 simply declaring a function yourself instead of including the proper
7509 header.
7511 Test programs should not write to standard output.  They
7512 should exit with status 0 if the test succeeds, and with status 1
7513 otherwise, so that success
7514 can be distinguished easily from a core dump or other failure;
7515 segmentation violations and other failures produce a nonzero exit
7516 status.  Unless you arrange for @code{exit} to be declared, test
7517 programs should @code{return}, not @code{exit}, from @code{main},
7518 because on many systems @code{exit} is not declared by default.
7520 Test programs can use @code{#if} or @code{#ifdef} to check the values of
7521 preprocessor macros defined by tests that have already run.  For
7522 example, if you call @code{AC_HEADER_STDBOOL}, then later on in
7523 @file{configure.ac} you can have a test program that includes
7524 @file{stdbool.h} conditionally:
7526 @example
7527 @group
7528 #if HAVE_STDBOOL_H
7529 # include <stdbool.h>
7530 #endif
7531 @end group
7532 @end example
7534 If a test program needs to use or create a data file, give it a name
7535 that starts with @file{conftest}, such as @file{conftest.data}.  The
7536 @command{configure} script cleans up by running @samp{rm -f -r conftest*}
7537 after running test programs and if the script is interrupted.
7539 @node Test Functions
7540 @subsection Test Functions
7542 These days it's safe to assume support for function prototypes
7543 (introduced in C89).
7545 Functions that test programs declare should also be conditionalized for
7546 C++, which requires @samp{extern "C"} prototypes.  Make sure to not
7547 include any header files containing clashing prototypes.
7549 @example
7550 #ifdef __cplusplus
7551 extern "C"
7552 #endif
7553 void *valloc (size_t);
7554 @end example
7556 If a test program calls a function with invalid parameters (just to see
7557 whether it exists), organize the program to ensure that it never invokes
7558 that function.  You can do this by calling it in another function that is
7559 never invoked.  You can't do it by putting it after a call to
7560 @code{exit}, because @acronym{GCC} version 2 knows that @code{exit}
7561 never returns
7562 and optimizes out any code that follows it in the same block.
7564 If you include any header files, be sure to call the functions
7565 relevant to them with the correct number of arguments, even if they are
7566 just 0, to avoid compilation errors due to prototypes.  @acronym{GCC}
7567 version 2
7568 has internal prototypes for several functions that it automatically
7569 inlines; for example, @code{memcpy}.  To avoid errors when checking for
7570 them, either pass them the correct number of arguments or redeclare them
7571 with a different return type (such as @code{char}).
7574 @node Generating Sources
7575 @subsection Generating Sources
7577 Autoconf provides a set of macros that can be used to generate test
7578 source files.  They are written to be language generic, i.e., they
7579 actually depend on the current language (@pxref{Language Choice}) to
7580 ``format'' the output properly.
7583 @defmac AC_LANG_CONFTEST (@var{source})
7584 @acindex{LANG_CONFTEST}
7585 Save the @var{source} text in the current test source file:
7586 @file{conftest.@var{extension}} where the @var{extension} depends on the
7587 current language.
7589 Note that the @var{source} is evaluated exactly once, like regular
7590 Autoconf macro arguments, and therefore (i) you may pass a macro
7591 invocation, (ii) if not, be sure to double quote if needed.
7592 @end defmac
7594 @defmac AC_LANG_SOURCE (@var{source})
7595 @acindex{LANG_SOURCE}
7596 Expands into the @var{source}, with the definition of
7597 all the @code{AC_DEFINE} performed so far.
7598 @end defmac
7600 For instance executing (observe the double quotation!):
7602 @example
7603 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7604 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7605   [Greetings string.])
7606 AC_LANG(C)
7607 AC_LANG_CONFTEST(
7608    [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
7609 gcc -E -dD -o - conftest.c
7610 @end example
7612 @noindent
7613 results in:
7615 @example
7616 @dots{}
7617 # 1 "conftest.c"
7619 #define PACKAGE_NAME "Hello"
7620 #define PACKAGE_TARNAME "hello"
7621 #define PACKAGE_VERSION "1.0"
7622 #define PACKAGE_STRING "Hello 1.0"
7623 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7624 #define HELLO_WORLD "Hello, World\n"
7626 const char hw[] = "Hello, World\n";
7627 @end example
7629 When the test language is Fortran or Erlang, the @code{AC_DEFINE} definitions
7630 are not automatically translated into constants in the source code by this
7631 macro.
7633 @defmac AC_LANG_PROGRAM (@var{prologue}, @var{body})
7634 @acindex{LANG_PROGRAM}
7635 Expands into a source file which consists of the @var{prologue}, and
7636 then @var{body} as body of the main function (e.g., @code{main} in
7637 C).  Since it uses @code{AC_LANG_SOURCE}, the features of the latter are
7638 available.
7639 @end defmac
7641 For instance:
7643 @example
7644 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7645 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7646   [Greetings string.])
7647 AC_LANG_CONFTEST(
7648 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
7649                  [[fputs (hw, stdout);]])])
7650 gcc -E -dD -o - conftest.c
7651 @end example
7653 @noindent
7654 results in:
7656 @example
7657 @dots{}
7658 # 1 "conftest.c"
7660 #define PACKAGE_NAME "Hello"
7661 #define PACKAGE_TARNAME "hello"
7662 #define PACKAGE_VERSION "1.0"
7663 #define PACKAGE_STRING "Hello 1.0"
7664 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7665 #define HELLO_WORLD "Hello, World\n"
7667 const char hw[] = "Hello, World\n";
7669 main ()
7671 fputs (hw, stdout);
7672   ;
7673   return 0;
7675 @end example
7677 In Erlang tests, the created source file is that of an Erlang module called
7678 @code{conftest} (@file{conftest.erl}).  This module defines and exports at least
7679 one @code{start/0} function, which is called to perform the test.  The
7680 @var{prologue} is optional code that is inserted between the module header and
7681 the @code{start/0} function definition.  @var{body} is the body of the
7682 @code{start/0} function without the final period (@pxref{Runtime}, about
7683 constraints on this function's behaviour).
7685 For instance:
7687 @example
7688 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7689 AC_LANG(Erlang)
7690 AC_LANG_CONFTEST(
7691 [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
7692                  [[io:format("~s~n", [?HELLO_WORLD])]])])
7693 cat conftest.erl
7694 @end example
7696 @noindent
7697 results in:
7699 @example
7700 -module(conftest).
7701 -export([start/0]).
7702 -define(HELLO_WORLD, "Hello, world!").
7703 start() ->
7704 io:format("~s~n", [?HELLO_WORLD])
7706 @end example
7708 @defmac AC_LANG_CALL (@var{prologue}, @var{function})
7709 @acindex{LANG_CALL}
7710 Expands into a source file which consists of the @var{prologue}, and
7711 then a call to the @var{function} as body of the main function (e.g.,
7712 @code{main} in C).  Since it uses @code{AC_LANG_PROGRAM}, the feature
7713 of the latter are available.
7715 This function will probably be replaced in the future by a version
7716 which would enable specifying the arguments.  The use of this macro is
7717 not encouraged, as it violates strongly the typing system.
7719 This macro cannot be used for Erlang tests.
7720 @end defmac
7722 @defmac AC_LANG_FUNC_LINK_TRY (@var{function})
7723 @acindex{LANG_FUNC_LINK_TRY}
7724 Expands into a source file which uses the @var{function} in the body of
7725 the main function (e.g., @code{main} in C).  Since it uses
7726 @code{AC_LANG_PROGRAM}, the features of the latter are available.
7728 As @code{AC_LANG_CALL}, this macro is documented only for completeness.
7729 It is considered to be severely broken, and in the future will be
7730 removed in favor of actual function calls (with properly typed
7731 arguments).
7733 This macro cannot be used for Erlang tests.
7734 @end defmac
7736 @node Running the Preprocessor
7737 @section Running the Preprocessor
7739 Sometimes one might need to run the preprocessor on some source file.
7740 @emph{Usually it is a bad idea}, as you typically need to @emph{compile}
7741 your project, not merely run the preprocessor on it; therefore you
7742 certainly want to run the compiler, not the preprocessor.  Resist the
7743 temptation of following the easiest path.
7745 Nevertheless, if you need to run the preprocessor, then use
7746 @code{AC_PREPROC_IFELSE}.
7748 The macros described in this section cannot be used for tests in Erlang or
7749 Fortran, since those languages require no preprocessor.
7751 @defmac AC_PREPROC_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
7752 @acindex{PREPROC_IFELSE}
7753 Run the preprocessor of the current language (@pxref{Language Choice})
7754 on the @var{input}, run the shell commands @var{action-if-true} on
7755 success, @var{action-if-false} otherwise.  The @var{input} can be made
7756 by @code{AC_LANG_PROGRAM} and friends.
7758 This macro uses @code{CPPFLAGS}, but not @code{CFLAGS}, because
7759 @option{-g}, @option{-O}, etc.@: are not valid options to many C
7760 preprocessors.
7762 It is customary to report unexpected failures with
7763 @code{AC_MSG_FAILURE}.
7764 @end defmac
7766 For instance:
7768 @example
7769 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7770 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7771   [Greetings string.])
7772 AC_PREPROC_IFELSE(
7773    [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
7774                     [[fputs (hw, stdout);]])],
7775    [AC_MSG_RESULT([OK])],
7776    [AC_MSG_FAILURE([unexpected preprocessor failure])])
7777 @end example
7779 @noindent
7780 results in:
7782 @example
7783 checking for gcc... gcc
7784 checking for C compiler default output file name... a.out
7785 checking whether the C compiler works... yes
7786 checking whether we are cross compiling... no
7787 checking for suffix of executables...
7788 checking for suffix of object files... o
7789 checking whether we are using the GNU C compiler... yes
7790 checking whether gcc accepts -g... yes
7791 checking for gcc option to accept ISO C89... none needed
7792 checking how to run the C preprocessor... gcc -E
7794 @end example
7796 @sp 1
7798 The macro @code{AC_TRY_CPP} (@pxref{Obsolete Macros}) used to play the
7799 role of @code{AC_PREPROC_IFELSE}, but double quotes its argument, making
7800 it impossible to use it to elaborate sources.  You are encouraged to
7801 get rid of your old use of the macro @code{AC_TRY_CPP} in favor of
7802 @code{AC_PREPROC_IFELSE}, but, in the first place, are you sure you need
7803 to run the @emph{preprocessor} and not the compiler?
7805 @defmac AC_EGREP_HEADER (@var{pattern}, @var{header-file}, @var{action-if-found}, @ovar{action-if-not-found})
7806 @acindex{EGREP_HEADER}
7807 If the output of running the preprocessor on the system header file
7808 @var{header-file} matches the extended regular expression
7809 @var{pattern}, execute shell commands @var{action-if-found}, otherwise
7810 execute @var{action-if-not-found}.
7811 @end defmac
7813 @defmac AC_EGREP_CPP (@var{pattern}, @var{program}, @ovar{action-if-found}, @ovar{action-if-not-found})
7814 @acindex{EGREP_CPP}
7815 @var{program} is the text of a C or C++ program, on which shell
7816 variable, back quote, and backslash substitutions are performed.  If the
7817 output of running the preprocessor on @var{program} matches the
7818 extended regular expression @var{pattern}, execute shell commands
7819 @var{action-if-found}, otherwise execute @var{action-if-not-found}.
7820 @end defmac
7824 @node Running the Compiler
7825 @section Running the Compiler
7827 To check for a syntax feature of the current language's (@pxref{Language
7828 Choice}) compiler, such as whether it recognizes a certain keyword, or
7829 simply to try some library feature, use @code{AC_COMPILE_IFELSE} to try
7830 to compile a small program that uses that feature.
7832 @defmac AC_COMPILE_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
7833 @acindex{COMPILE_IFELSE}
7834 Run the compiler and compilation flags of the current language
7835 (@pxref{Language Choice}) on the @var{input}, run the shell commands
7836 @var{action-if-true} on success, @var{action-if-false} otherwise.  The
7837 @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
7839 It is customary to report unexpected failures with
7840 @code{AC_MSG_FAILURE}.  This macro does not try to link; use
7841 @code{AC_LINK_IFELSE} if you need to do that (@pxref{Running the
7842 Linker}).
7843 @end defmac
7845 @ovindex ERL
7846 For tests in Erlang, the @var{input} must be the source code of a module named
7847 @code{conftest}.  @code{AC_COMPILE_IFELSE} generates a @file{conftest.beam}
7848 file that can be interpreted by the Erlang virtual machine (@code{ERL}).  It is
7849 recommended to use @code{AC_LANG_PROGRAM} to specify the test program, to ensure
7850 that the Erlang module has the right name.
7852 @node Running the Linker
7853 @section Running the Linker
7855 To check for a library, a function, or a global variable, Autoconf
7856 @command{configure} scripts try to compile and link a small program that
7857 uses it.  This is unlike Metaconfig, which by default uses @code{nm} or
7858 @code{ar} on the C library to try to figure out which functions are
7859 available.  Trying to link with the function is usually a more reliable
7860 approach because it avoids dealing with the variations in the options
7861 and output formats of @code{nm} and @code{ar} and in the location of the
7862 standard libraries.  It also allows configuring for cross-compilation or
7863 checking a function's runtime behavior if needed.  On the other hand,
7864 it can be slower than scanning the libraries once, but accuracy is more
7865 important than speed.
7867 @code{AC_LINK_IFELSE} is used to compile test programs to test for
7868 functions and global variables.  It is also used by @code{AC_CHECK_LIB}
7869 to check for libraries (@pxref{Libraries}), by adding the library being
7870 checked for to @code{LIBS} temporarily and trying to link a small
7871 program.
7874 @defmac AC_LINK_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
7875 @acindex{LINK_IFELSE}
7876 Run the compiler (and compilation flags) and the linker of the current
7877 language (@pxref{Language Choice}) on the @var{input}, run the shell
7878 commands @var{action-if-true} on success, @var{action-if-false}
7879 otherwise.  The @var{input} can be made by @code{AC_LANG_PROGRAM} and
7880 friends.
7882 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
7883 current compilation flags.
7885 It is customary to report unexpected failures with
7886 @code{AC_MSG_FAILURE}.  This macro does not try to execute the program;
7887 use @code{AC_RUN_IFELSE} if you need to do that (@pxref{Runtime}).
7888 @end defmac
7890 The @code{AC_LINK_IFELSE} macro cannot be used for Erlang tests, since Erlang
7891 programs are interpreted and do not require linking.
7895 @node Runtime
7896 @section Checking Runtime Behavior
7898 Sometimes you need to find out how a system performs at runtime, such
7899 as whether a given function has a certain capability or bug.  If you
7900 can, make such checks when your program runs instead of when it is
7901 configured.  You can check for things like the machine's endianness when
7902 your program initializes itself.
7904 If you really need to test for a runtime behavior while configuring,
7905 you can write a test program to determine the result, and compile and
7906 run it using @code{AC_RUN_IFELSE}.  Avoid running test programs if
7907 possible, because this prevents people from configuring your package for
7908 cross-compiling.
7910 @defmac AC_RUN_IFELSE (@var{input}, @ovar{action-if-true}, @ovar{action-if-false}, @ovar{action-if-cross-compiling})
7911 @acindex{RUN_IFELSE}
7912 If @var{program} compiles and links successfully and returns an exit
7913 status of 0 when executed, run shell commands @var{action-if-true}.
7914 Otherwise, run shell commands @var{action-if-false}.
7916 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
7917 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
7918 compilation flags of the current language (@pxref{Language Choice}).
7920 If the compiler being used does not produce executables that run on the
7921 system where @command{configure} is being run, then the test program is
7922 not run.  If the optional shell commands @var{action-if-cross-compiling}
7923 are given, they are run instead.  Otherwise, @command{configure} prints
7924 an error message and exits.
7926 In the @var{action-if-false} section, the failing exit status is
7927 available in the shell variable @samp{$?}.  This exit status might be
7928 that of a failed compilation, or it might be that of a failed program
7929 execution.
7931 It is customary to report unexpected failures with
7932 @code{AC_MSG_FAILURE}.
7933 @end defmac
7935 Try to provide a pessimistic default value to use when cross-compiling
7936 makes runtime tests impossible.  You do this by passing the optional
7937 last argument to @code{AC_RUN_IFELSE}.  @command{autoconf} prints a
7938 warning message when creating @command{configure} each time it
7939 encounters a call to @code{AC_RUN_IFELSE} with no
7940 @var{action-if-cross-compiling} argument given.  You may ignore the
7941 warning, though users cannot configure your package for
7942 cross-compiling.  A few of the macros distributed with Autoconf produce
7943 this warning message.
7945 To configure for cross-compiling you can also choose a value for those
7946 parameters based on the canonical system name (@pxref{Manual
7947 Configuration}).  Alternatively, set up a test results cache file with
7948 the correct values for the host system (@pxref{Caching Results}).
7950 @ovindex cross_compiling
7951 To provide a default for calls of @code{AC_RUN_IFELSE} that are embedded
7952 in other macros, including a few of the ones that come with Autoconf,
7953 you can test whether the shell variable @code{cross_compiling} is set to
7954 @samp{yes}, and then use an alternate method to get the results instead
7955 of calling the macros.
7957 A C or C++ runtime test should be portable.
7958 @xref{Portable C and C++}.
7960 Erlang tests must exit themselves the Erlang VM by calling the @code{halt/1}
7961 function: the given status code is used to determine the success of the test
7962 (status is @code{0}) or its failure (status is different than @code{0}), as
7963 explained above.  It must be noted that data output through the standard output
7964 (e.g. using @code{io:format/2}) may be truncated when halting the VM.
7965 Therefore, if a test must output configuration information, it is recommended
7966 to create and to output data into the temporary file named @file{conftest.out},
7967 using the functions of module @code{file}.  The @code{conftest.out} file is
7968 automatically deleted by the @code{AC_RUN_IFELSE} macro.  For instance, a
7969 simplified implementation of Autoconf's @code{AC_ERLANG_SUBST_LIB_DIR} macro is:
7971 @example
7972 AC_INIT([LibdirTest], [1.0], [bug-libdirtest@@example.org])
7973 AC_ERLANG_NEED_ERL
7974 AC_LANG(Erlang)
7975 AC_RUN_IFELSE(
7976   [AC_LANG_PROGRAM([], [dnl
7977     file:write_file("conftest.out", code:lib_dir()),
7978     halt(0)])],
7979   [echo "code:lib_dir() returned: `cat conftest.out`"],
7980   [AC_MSG_FAILURE([test Erlang program execution failed])])
7981 @end example
7984 @node Systemology
7985 @section Systemology
7986 @cindex Systemology
7988 This section aims at presenting some systems and pointers to
7989 documentation.  It may help you addressing particular problems reported
7990 by users.
7992 @uref{http://www.opengroup.org/susv3, Posix-conforming systems} are
7993 derived from the @uref{http://www.bell-labs.com/history/unix/, Unix
7994 operating system}.
7996 The @uref{http://bhami.com/rosetta.html, Rosetta Stone for Unix}
7997 contains a table correlating the features of various Posix-conforming
7998 systems.  @uref{http://www.levenez.com/unix/, Unix History} is a
7999 simplified diagram of how many Unix systems were derived from each
8000 other.
8002 @uref{http://heirloom.sourceforge.net/, The Heirloom Project}
8003 provides some variants of traditional implementations of Unix utilities.
8005 @table @asis
8006 @item Darwin
8007 @cindex Darwin
8008 Darwin is also known as Mac OS X@.  Beware that the file system @emph{can} be
8009 case-preserving, but case insensitive.  This can cause nasty problems,
8010 since for instance the installation attempt for a package having an
8011 @file{INSTALL} file can result in @samp{make install} report that
8012 nothing was to be done!
8014 That's all dependent on whether the file system is a UFS (case
8015 sensitive) or HFS+ (case preserving).  By default Apple wants you to
8016 install the OS on HFS+.  Unfortunately, there are some pieces of
8017 software which really need to be built on UFS@.  We may want to rebuild
8018 Darwin to have both UFS and HFS+ available (and put the /local/build
8019 tree on the UFS).
8021 @item @acronym{QNX} 4.25
8022 @cindex @acronym{QNX} 4.25
8023 @c FIXME: Please, if you feel like writing something more precise,
8024 @c it'd be great.  In particular, I can't understand the difference with
8025 @c QNX Neutrino.
8026 @acronym{QNX} is a realtime operating system running on Intel architecture
8027 meant to be scalable from the small embedded systems to the hundred
8028 processor super-computer.  It claims to be Posix certified.  More
8029 information is available on the
8030 @uref{http://www.qnx.com/, @acronym{QNX} home page}.
8032 @item Tru64
8033 @cindex Tru64
8034 @uref{http://h30097.www3.hp.com/@/docs/,
8035 Documentation of several versions of Tru64} is available in different
8036 formats.
8038 @item Unix version 7
8039 @cindex Unix version 7
8040 @cindex V7
8041 Officially this was called the ``Seventh Edition'' of ``the @sc{unix}
8042 time-sharing system'' but we use the more-common name ``Unix version 7''.
8043 Documentation is available in the
8044 @uref{http://plan9.bell-labs.com/@/7thEdMan/, Unix Seventh Edition Manual}.
8045 Previous versions of Unix are called ``Unix version 6'', etc., but
8046 they were not as widely used.
8047 @end table
8050 @node Multiple Cases
8051 @section Multiple Cases
8053 Some operations are accomplished in several possible ways, depending on
8054 the OS variant.  Checking for them essentially requires a ``case
8055 statement''.  Autoconf does not directly provide one; however, it is
8056 easy to simulate by using a shell variable to keep track of whether a
8057 way to perform the operation has been found yet.
8059 Here is an example that uses the shell variable @code{fstype} to keep
8060 track of whether the remaining cases need to be checked.
8062 @example
8063 @group
8064 AC_MSG_CHECKING([how to get file system type])
8065 fstype=no
8066 # The order of these tests is important.
8067 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statvfs.h>
8068 #include <sys/fstyp.h>]])],
8069                   [AC_DEFINE([FSTYPE_STATVFS], [1],
8070                      [Define if statvfs exists.])
8071                    fstype=SVR4])
8072 if test $fstype = no; then
8073   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8074 #include <sys/fstyp.h>]])],
8075                   [AC_DEFINE([FSTYPE_USG_STATFS], [1],
8076                      [Define if USG statfs.])
8077                    fstype=SVR3])
8079 if test $fstype = no; then
8080   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8081 #include <sys/vmount.h>]])]),
8082                   [AC_DEFINE([FSTYPE_AIX_STATFS], [1],
8083                      [Define if AIX statfs.])
8084                    fstype=AIX])
8086 # (more cases omitted here)
8087 AC_MSG_RESULT([$fstype])
8088 @end group
8089 @end example
8091 @c ====================================================== Results of Tests.
8093 @node Results
8094 @chapter Results of Tests
8096 Once @command{configure} has determined whether a feature exists, what can
8097 it do to record that information?  There are four sorts of things it can
8098 do: define a C preprocessor symbol, set a variable in the output files,
8099 save the result in a cache file for future @command{configure} runs, and
8100 print a message letting the user know the result of the test.
8102 @menu
8103 * Defining Symbols::            Defining C preprocessor symbols
8104 * Setting Output Variables::    Replacing variables in output files
8105 * Special Chars in Variables::  Characters to beware of in variables
8106 * Caching Results::             Speeding up subsequent @command{configure} runs
8107 * Printing Messages::           Notifying @command{configure} users
8108 @end menu
8110 @node Defining Symbols
8111 @section Defining C Preprocessor Symbols
8113 A common action to take in response to a feature test is to define a C
8114 preprocessor symbol indicating the results of the test.  That is done by
8115 calling @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}.
8117 By default, @code{AC_OUTPUT} places the symbols defined by these macros
8118 into the output variable @code{DEFS}, which contains an option
8119 @option{-D@var{symbol}=@var{value}} for each symbol defined.  Unlike in
8120 Autoconf version 1, there is no variable @code{DEFS} defined while
8121 @command{configure} is running.  To check whether Autoconf macros have
8122 already defined a certain C preprocessor symbol, test the value of the
8123 appropriate cache variable, as in this example:
8125 @example
8126 AC_CHECK_FUNC([vprintf], [AC_DEFINE([HAVE_VPRINTF], [1],
8127                           [Define if vprintf exists.])])
8128 if test "$ac_cv_func_vprintf" != yes; then
8129   AC_CHECK_FUNC([_doprnt], [AC_DEFINE([HAVE_DOPRNT], [1],
8130                             [Define if _doprnt exists.])])
8132 @end example
8134 If @code{AC_CONFIG_HEADERS} has been called, then instead of creating
8135 @code{DEFS}, @code{AC_OUTPUT} creates a header file by substituting the
8136 correct values into @code{#define} statements in a template file.
8137 @xref{Configuration Headers}, for more information about this kind of
8138 output.
8140 @defmac AC_DEFINE (@var{variable}, @var{value}, @ovar{description})
8141 @defmacx AC_DEFINE (@var{variable})
8142 @acindex{DEFINE}
8143 Define the C preprocessor variable @var{variable} to @var{value} (verbatim).
8144 @var{value} should not contain literal newlines, and if you are not
8145 using @code{AC_CONFIG_HEADERS} it should not contain any @samp{#}
8146 characters, as @command{make} tends to eat them.  To use a shell variable,
8147 use @code{AC_DEFINE_UNQUOTED} instead.
8148 @var{description} is only useful if you are using
8149 @code{AC_CONFIG_HEADERS}.  In this case, @var{description} is put into
8150 the generated @file{config.h.in} as the comment before the macro define.
8151 The following example defines the C preprocessor variable
8152 @code{EQUATION} to be the string constant @samp{"$a > $b"}:
8154 @example
8155 AC_DEFINE([EQUATION], ["$a > $b"],
8156   [Equation string.])
8157 @end example
8159 If neither @var{value} nor @var{description} are given, then
8160 @var{value} defaults to 1 instead of to the empty string.  This is for
8161 backwards compatibility with older versions of Autoconf, but this usage
8162 is obsolescent and may be withdrawn in future versions of Autoconf.
8164 If the @var{variable} is a literal string, it is passed to
8165 @code{m4_pattern_allow} (@pxref{Forbidden Patterns}).
8166 @end defmac
8168 @defmac AC_DEFINE_UNQUOTED (@var{variable}, @var{value}, @ovar{description})
8169 @defmacx AC_DEFINE_UNQUOTED (@var{variable})
8170 @acindex{DEFINE_UNQUOTED}
8171 Like @code{AC_DEFINE}, but three shell expansions are
8172 performed---once---on @var{variable} and @var{value}: variable expansion
8173 (@samp{$}), command substitution (@samp{`}), and backslash escaping
8174 (@samp{\}).  Single and double quote characters in the value have no
8175 special meaning.  Use this macro instead of @code{AC_DEFINE} when
8176 @var{variable} or @var{value} is a shell variable.  Examples:
8178 @example
8179 AC_DEFINE_UNQUOTED([config_machfile], ["$machfile"],
8180   [Configuration machine file.])
8181 AC_DEFINE_UNQUOTED([GETGROUPS_T], [$ac_cv_type_getgroups],
8182   [getgroups return type.])
8183 AC_DEFINE_UNQUOTED([$ac_tr_hdr], [1],
8184   [Translated header name.])
8185 @end example
8186 @end defmac
8188 Due to a syntactical bizarreness of the Bourne shell, do not use
8189 semicolons to separate @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}
8190 calls from other macro calls or shell code; that can cause syntax errors
8191 in the resulting @command{configure} script.  Use either blanks or
8192 newlines.  That is, do this:
8194 @example
8195 AC_CHECK_HEADER([elf.h],
8196   [AC_DEFINE([SVR4], [1], [System V Release 4]) LIBS="-lelf $LIBS"])
8197 @end example
8199 @noindent
8200 or this:
8202 @example
8203 AC_CHECK_HEADER([elf.h],
8204   [AC_DEFINE([SVR4], [1], [System V Release 4])
8205    LIBS="-lelf $LIBS"])
8206 @end example
8208 @noindent
8209 instead of this:
8211 @example
8212 AC_CHECK_HEADER([elf.h],
8213   [AC_DEFINE([SVR4], [1], [System V Release 4]); LIBS="-lelf $LIBS"])
8214 @end example
8216 @node Setting Output Variables
8217 @section Setting Output Variables
8218 @cindex Output variables
8220 Another way to record the results of tests is to set @dfn{output
8221 variables}, which are shell variables whose values are substituted into
8222 files that @command{configure} outputs.  The two macros below create new
8223 output variables.  @xref{Preset Output Variables}, for a list of output
8224 variables that are always available.
8226 @defmac AC_SUBST (@var{variable}, @ovar{value})
8227 @acindex{SUBST}
8228 Create an output variable from a shell variable.  Make @code{AC_OUTPUT}
8229 substitute the variable @var{variable} into output files (typically one
8230 or more makefiles).  This means that @code{AC_OUTPUT}
8231 replaces instances of @samp{@@@var{variable}@@} in input files with the
8232 value that the shell variable @var{variable} has when @code{AC_OUTPUT}
8233 is called.  The value can contain newlines.
8234 The substituted value is not rescanned for more output variables;
8235 occurrences of @samp{@@@var{variable}@@} in the value are inserted
8236 literally into the output file.  (The algorithm uses the special marker
8237 @code{|#_!!_#|} internally, so the substituted value cannot contain
8238 @code{|#_!!_#|}.)
8240 If @var{value} is given, in addition assign it to @var{variable}.
8242 The string @var{variable} is passed to @code{m4_pattern_allow}
8243 (@pxref{Forbidden Patterns}).
8244 @end defmac
8246 @defmac AC_SUBST_FILE (@var{variable})
8247 @acindex{SUBST_FILE}
8248 Another way to create an output variable from a shell variable.  Make
8249 @code{AC_OUTPUT} insert (without substitutions) the contents of the file
8250 named by shell variable @var{variable} into output files.  This means
8251 that @code{AC_OUTPUT} replaces instances of
8252 @samp{@@@var{variable}@@} in output files (such as @file{Makefile.in})
8253 with the contents of the file that the shell variable @var{variable}
8254 names when @code{AC_OUTPUT} is called.  Set the variable to
8255 @file{/dev/null} for cases that do not have a file to insert.
8256 This substitution occurs only when the @samp{@@@var{variable}@@} is on a
8257 line by itself, optionally surrounded by spaces and tabs.  The
8258 substitution replaces the whole line, including the spaces, tabs, and
8259 the terminating newline.
8261 This macro is useful for inserting makefile fragments containing
8262 special dependencies or other @code{make} directives for particular host
8263 or target types into makefiles.  For example, @file{configure.ac}
8264 could contain:
8266 @example
8267 AC_SUBST_FILE([host_frag])
8268 host_frag=$srcdir/conf/sun4.mh
8269 @end example
8271 @noindent
8272 and then a @file{Makefile.in} could contain:
8274 @example
8275 @@host_frag@@
8276 @end example
8278 The string @var{variable} is passed to @code{m4_pattern_allow}
8279 (@pxref{Forbidden Patterns}).
8280 @end defmac
8282 @cindex Previous Variable
8283 @cindex Variable, Precious
8284 Running @command{configure} in varying environments can be extremely
8285 dangerous.  If for instance the user runs @samp{CC=bizarre-cc
8286 ./configure}, then the cache, @file{config.h}, and many other output
8287 files depend upon @command{bizarre-cc} being the C compiler.  If
8288 for some reason the user runs @command{./configure} again, or if it is
8289 run via @samp{./config.status --recheck}, (@xref{Automatic Remaking},
8290 and @pxref{config.status Invocation}), then the configuration can be
8291 inconsistent, composed of results depending upon two different
8292 compilers.
8294 Environment variables that affect this situation, such as @samp{CC}
8295 above, are called @dfn{precious variables}, and can be declared as such
8296 by @code{AC_ARG_VAR}.
8298 @defmac AC_ARG_VAR (@var{variable}, @var{description})
8299 @acindex{ARG_VAR}
8300 Declare @var{variable} is a precious variable, and include its
8301 @var{description} in the variable section of @samp{./configure --help}.
8303 Being precious means that
8304 @itemize @minus
8305 @item
8306 @var{variable} is substituted via @code{AC_SUBST}.
8308 @item
8309 The value of @var{variable} when @command{configure} was launched is
8310 saved in the cache, including if it was not specified on the command
8311 line but via the environment.  Indeed, while @command{configure} can
8312 notice the definition of @code{CC} in @samp{./configure CC=bizarre-cc},
8313 it is impossible to notice it in @samp{CC=bizarre-cc ./configure},
8314 which, unfortunately, is what most users do.
8316 We emphasize that it is the @emph{initial} value of @var{variable} which
8317 is saved, not that found during the execution of @command{configure}.
8318 Indeed, specifying @samp{./configure FOO=foo} and letting
8319 @samp{./configure} guess that @code{FOO} is @code{foo} can be two
8320 different things.
8322 @item
8323 @var{variable} is checked for consistency between two
8324 @command{configure} runs.  For instance:
8326 @example
8327 $ @kbd{./configure --silent --config-cache}
8328 $ @kbd{CC=cc ./configure --silent --config-cache}
8329 configure: error: `CC' was not set in the previous run
8330 configure: error: changes in the environment can compromise \
8331 the build
8332 configure: error: run `make distclean' and/or \
8333 `rm config.cache' and start over
8334 @end example
8336 @noindent
8337 and similarly if the variable is unset, or if its content is changed.
8340 @item
8341 @var{variable} is kept during automatic reconfiguration
8342 (@pxref{config.status Invocation}) as if it had been passed as a command
8343 line argument, including when no cache is used:
8345 @example
8346 $ @kbd{CC=/usr/bin/cc ./configure undeclared_var=raboof --silent}
8347 $ @kbd{./config.status --recheck}
8348 running /bin/sh ./configure undeclared_var=raboof --silent \
8349   CC=/usr/bin/cc  --no-create --no-recursion
8350 @end example
8351 @end itemize
8352 @end defmac
8354 @node Special Chars in Variables
8355 @section Special Characters in Output Variables
8356 @cindex Output variables, special characters in
8358 Many output variables are intended to be evaluated both by
8359 @command{make} and by the shell.  Some characters are expanded
8360 differently in these two contexts, so to avoid confusion these
8361 variables' values should not contain any of the following characters:
8363 @example
8364 " # $ & ' ( ) * ; < > ? [ \ ^ ` |
8365 @end example
8367 Also, these variables' values should neither contain newlines, nor start
8368 with @samp{~}, nor contain white space or @samp{:} immediately followed
8369 by @samp{~}.  The values can contain nonempty sequences of white space
8370 characters like tabs and spaces, but each such sequence might
8371 arbitrarily be replaced by a single space during substitution.
8373 These restrictions apply both to the values that @command{configure}
8374 computes, and to the values set directly by the user.  For example, the
8375 following invocations of @command{configure} are problematic, since they
8376 attempt to use special characters within @code{CPPFLAGS} and white space
8377 within @code{$(srcdir)}:
8379 @example
8380 CPPFLAGS='-DOUCH="&\"#$*?"' '../My Source/ouch-1.0/configure'
8382 '../My Source/ouch-1.0/configure' CPPFLAGS='-DOUCH="&\"#$*?"'
8383 @end example
8385 @node Caching Results
8386 @section Caching Results
8387 @cindex Cache
8389 To avoid checking for the same features repeatedly in various
8390 @command{configure} scripts (or in repeated runs of one script),
8391 @command{configure} can optionally save the results of many checks in a
8392 @dfn{cache file} (@pxref{Cache Files}).  If a @command{configure} script
8393 runs with caching enabled and finds a cache file, it reads the results
8394 of previous runs from the cache and avoids rerunning those checks.  As a
8395 result, @command{configure} can then run much faster than if it had to
8396 perform all of the checks every time.
8398 @defmac AC_CACHE_VAL (@var{cache-id}, @var{commands-to-set-it})
8399 @acindex{CACHE_VAL}
8400 Ensure that the results of the check identified by @var{cache-id} are
8401 available.  If the results of the check were in the cache file that was
8402 read, and @command{configure} was not given the @option{--quiet} or
8403 @option{--silent} option, print a message saying that the result was
8404 cached; otherwise, run the shell commands @var{commands-to-set-it}.  If
8405 the shell commands are run to determine the value, the value is
8406 saved in the cache file just before @command{configure} creates its output
8407 files.  @xref{Cache Variable Names}, for how to choose the name of the
8408 @var{cache-id} variable.
8410 The @var{commands-to-set-it} @emph{must have no side effects} except for
8411 setting the variable @var{cache-id}, see below.
8412 @end defmac
8414 @defmac AC_CACHE_CHECK (@var{message}, @var{cache-id}, @var{commands-to-set-it})
8415 @acindex{CACHE_CHECK}
8416 A wrapper for @code{AC_CACHE_VAL} that takes care of printing the
8417 messages.  This macro provides a convenient shorthand for the most
8418 common way to use these macros.  It calls @code{AC_MSG_CHECKING} for
8419 @var{message}, then @code{AC_CACHE_VAL} with the @var{cache-id} and
8420 @var{commands} arguments, and @code{AC_MSG_RESULT} with @var{cache-id}.
8422 The @var{commands-to-set-it} @emph{must have no side effects} except for
8423 setting the variable @var{cache-id}, see below.
8424 @end defmac
8426 It is common to find buggy macros using @code{AC_CACHE_VAL} or
8427 @code{AC_CACHE_CHECK}, because people are tempted to call
8428 @code{AC_DEFINE} in the @var{commands-to-set-it}.  Instead, the code that
8429 @emph{follows} the call to @code{AC_CACHE_VAL} should call
8430 @code{AC_DEFINE}, by examining the value of the cache variable.  For
8431 instance, the following macro is broken:
8433 @example
8434 @group
8435 AC_DEFUN([AC_SHELL_TRUE],
8436 [AC_CACHE_CHECK([whether true(1) works], [ac_cv_shell_true_works],
8437                 [ac_cv_shell_true_works=no
8438                  (true) 2>/dev/null && ac_cv_shell_true_works=yes
8439                  if test "$ac_cv_shell_true_works" = yes; then
8440                    AC_DEFINE([TRUE_WORKS], [1],
8441                              [Define if `true(1)' works properly.])
8442                  fi])
8444 @end group
8445 @end example
8447 @noindent
8448 This fails if the cache is enabled: the second time this macro is run,
8449 @code{TRUE_WORKS} @emph{will not be defined}.  The proper implementation
8452 @example
8453 @group
8454 AC_DEFUN([AC_SHELL_TRUE],
8455 [AC_CACHE_CHECK([whether true(1) works], [ac_cv_shell_true_works],
8456                 [ac_cv_shell_true_works=no
8457                  (true) 2>/dev/null && ac_cv_shell_true_works=yes])
8458  if test "$ac_cv_shell_true_works" = yes; then
8459    AC_DEFINE([TRUE_WORKS], [1],
8460              [Define if `true(1)' works properly.])
8461  fi
8463 @end group
8464 @end example
8466 Also, @var{commands-to-set-it} should not print any messages, for
8467 example with @code{AC_MSG_CHECKING}; do that before calling
8468 @code{AC_CACHE_VAL}, so the messages are printed regardless of whether
8469 the results of the check are retrieved from the cache or determined by
8470 running the shell commands.
8472 @menu
8473 * Cache Variable Names::        Shell variables used in caches
8474 * Cache Files::                 Files @command{configure} uses for caching
8475 * Cache Checkpointing::         Loading and saving the cache file
8476 @end menu
8478 @node Cache Variable Names
8479 @subsection Cache Variable Names
8480 @cindex Cache variable
8482 The names of cache variables should have the following format:
8484 @example
8485 @var{package-prefix}_cv_@var{value-type}_@var{specific-value}_@ovar{additional-options}
8486 @end example
8488 @noindent
8489 for example, @samp{ac_cv_header_stat_broken} or
8490 @samp{ac_cv_prog_gcc_traditional}.  The parts of the variable name are:
8492 @table @asis
8493 @item @var{package-prefix}
8494 An abbreviation for your package or organization; the same prefix you
8495 begin local Autoconf macros with, except lowercase by convention.
8496 For cache values used by the distributed Autoconf macros, this value is
8497 @samp{ac}.
8499 @item @code{_cv_}
8500 Indicates that this shell variable is a cache value.  This string
8501 @emph{must} be present in the variable name, including the leading
8502 underscore.
8504 @item @var{value-type}
8505 A convention for classifying cache values, to produce a rational naming
8506 system.  The values used in Autoconf are listed in @ref{Macro Names}.
8508 @item @var{specific-value}
8509 Which member of the class of cache values this test applies to.
8510 For example, which function (@samp{alloca}), program (@samp{gcc}), or
8511 output variable (@samp{INSTALL}).
8513 @item @var{additional-options}
8514 Any particular behavior of the specific member that this test applies to.
8515 For example, @samp{broken} or @samp{set}.  This part of the name may
8516 be omitted if it does not apply.
8517 @end table
8519 The values assigned to cache variables may not contain newlines.
8520 Usually, their values are Boolean (@samp{yes} or @samp{no}) or the
8521 names of files or functions; so this is not an important restriction.
8523 @node Cache Files
8524 @subsection Cache Files
8526 A cache file is a shell script that caches the results of configure
8527 tests run on one system so they can be shared between configure scripts
8528 and configure runs.  It is not useful on other systems.  If its contents
8529 are invalid for some reason, the user may delete or edit it.
8531 By default, @command{configure} uses no cache file,
8532 to avoid problems caused by accidental
8533 use of stale cache files.
8535 To enable caching, @command{configure} accepts @option{--config-cache} (or
8536 @option{-C}) to cache results in the file @file{config.cache}.
8537 Alternatively, @option{--cache-file=@var{file}} specifies that
8538 @var{file} be the cache file.  The cache file is created if it does not
8539 exist already.  When @command{configure} calls @command{configure} scripts in
8540 subdirectories, it uses the @option{--cache-file} argument so that they
8541 share the same cache.  @xref{Subdirectories}, for information on
8542 configuring subdirectories with the @code{AC_CONFIG_SUBDIRS} macro.
8544 @file{config.status} only pays attention to the cache file if it is
8545 given the @option{--recheck} option, which makes it rerun
8546 @command{configure}.
8548 It is wrong to try to distribute cache files for particular system types.
8549 There is too much room for error in doing that, and too much
8550 administrative overhead in maintaining them.  For any features that
8551 can't be guessed automatically, use the standard method of the canonical
8552 system type and linking files (@pxref{Manual Configuration}).
8554 The site initialization script can specify a site-wide cache file to
8555 use, instead of the usual per-program cache.  In this case, the cache
8556 file gradually accumulates information whenever someone runs a new
8557 @command{configure} script.  (Running @command{configure} merges the new cache
8558 results with the existing cache file.)  This may cause problems,
8559 however, if the system configuration (e.g., the installed libraries or
8560 compilers) changes and the stale cache file is not deleted.
8562 @node Cache Checkpointing
8563 @subsection Cache Checkpointing
8565 If your configure script, or a macro called from @file{configure.ac}, happens
8566 to abort the configure process, it may be useful to checkpoint the cache
8567 a few times at key points using @code{AC_CACHE_SAVE}.  Doing so
8568 reduces the amount of time it takes to rerun the configure script with
8569 (hopefully) the error that caused the previous abort corrected.
8571 @c FIXME: Do we really want to document this guy?
8572 @defmac AC_CACHE_LOAD
8573 @acindex{CACHE_LOAD}
8574 Loads values from existing cache file, or creates a new cache file if a
8575 cache file is not found.  Called automatically from @code{AC_INIT}.
8576 @end defmac
8578 @defmac AC_CACHE_SAVE
8579 @acindex{CACHE_SAVE}
8580 Flushes all cached values to the cache file.  Called automatically from
8581 @code{AC_OUTPUT}, but it can be quite useful to call
8582 @code{AC_CACHE_SAVE} at key points in @file{configure.ac}.
8583 @end defmac
8585 For instance:
8587 @example
8588 @r{ @dots{} AC_INIT, etc. @dots{}}
8589 @group
8590 # Checks for programs.
8591 AC_PROG_CC
8592 AC_PROG_AWK
8593 @r{ @dots{} more program checks @dots{}}
8594 AC_CACHE_SAVE
8595 @end group
8597 @group
8598 # Checks for libraries.
8599 AC_CHECK_LIB([nsl], [gethostbyname])
8600 AC_CHECK_LIB([socket], [connect])
8601 @r{ @dots{} more lib checks @dots{}}
8602 AC_CACHE_SAVE
8603 @end group
8605 @group
8606 # Might abort@dots{}
8607 AM_PATH_GTK([1.0.2], [], [AC_MSG_ERROR([GTK not in path])])
8608 AM_PATH_GTKMM([0.9.5], [], [AC_MSG_ERROR([GTK not in path])])
8609 @end group
8610 @r{ @dots{} AC_OUTPUT, etc. @dots{}}
8611 @end example
8613 @node Printing Messages
8614 @section Printing Messages
8615 @cindex Messages, from @command{configure}
8617 @command{configure} scripts need to give users running them several kinds
8618 of information.  The following macros print messages in ways appropriate
8619 for each kind.  The arguments to all of them get enclosed in shell
8620 double quotes, so the shell performs variable and back-quote
8621 substitution on them.
8623 These macros are all wrappers around the @command{echo} shell command.
8624 They direct output to the appropriate file descriptor (@pxref{File
8625 Descriptor Macros}).
8626 @command{configure} scripts should rarely need to run @command{echo} directly
8627 to print messages for the user.  Using these macros makes it easy to
8628 change how and when each kind of message is printed; such changes need
8629 only be made to the macro definitions and all the callers change
8630 automatically.
8632 To diagnose static issues, i.e., when @command{autoconf} is run, see
8633 @ref{Reporting Messages}.
8635 @defmac AC_MSG_CHECKING (@var{feature-description})
8636 @acindex{MSG_CHECKING}
8637 Notify the user that @command{configure} is checking for a particular
8638 feature.  This macro prints a message that starts with @samp{checking }
8639 and ends with @samp{...} and no newline.  It must be followed by a call
8640 to @code{AC_MSG_RESULT} to print the result of the check and the
8641 newline.  The @var{feature-description} should be something like
8642 @samp{whether the Fortran compiler accepts C++ comments} or @samp{for
8643 c89}.
8645 This macro prints nothing if @command{configure} is run with the
8646 @option{--quiet} or @option{--silent} option.
8647 @end defmac
8649 @defmac AC_MSG_RESULT (@var{result-description})
8650 @acindex{MSG_RESULT}
8651 Notify the user of the results of a check.  @var{result-description} is
8652 almost always the value of the cache variable for the check, typically
8653 @samp{yes}, @samp{no}, or a file name.  This macro should follow a call
8654 to @code{AC_MSG_CHECKING}, and the @var{result-description} should be
8655 the completion of the message printed by the call to
8656 @code{AC_MSG_CHECKING}.
8658 This macro prints nothing if @command{configure} is run with the
8659 @option{--quiet} or @option{--silent} option.
8660 @end defmac
8662 @defmac AC_MSG_NOTICE (@var{message})
8663 @acindex{MSG_NOTICE}
8664 Deliver the @var{message} to the user.  It is useful mainly to print a
8665 general description of the overall purpose of a group of feature checks,
8666 e.g.,
8668 @example
8669 AC_MSG_NOTICE([checking if stack overflow is detectable])
8670 @end example
8672 This macro prints nothing if @command{configure} is run with the
8673 @option{--quiet} or @option{--silent} option.
8674 @end defmac
8676 @defmac AC_MSG_ERROR (@var{error-description}, @ovar{exit-status})
8677 @acindex{MSG_ERROR}
8678 Notify the user of an error that prevents @command{configure} from
8679 completing.  This macro prints an error message to the standard error
8680 output and exits @command{configure} with @var{exit-status} (1 by default).
8681 @var{error-description} should be something like @samp{invalid value
8682 $HOME for \$HOME}.
8684 The @var{error-description} should start with a lower-case letter, and
8685 ``cannot'' is preferred to ``can't''.
8686 @end defmac
8688 @defmac AC_MSG_FAILURE (@var{error-description}, @ovar{exit-status})
8689 @acindex{MSG_FAILURE}
8690 This @code{AC_MSG_ERROR} wrapper notifies the user of an error that
8691 prevents @command{configure} from completing @emph{and} that additional
8692 details are provided in @file{config.log}.  This is typically used when
8693 abnormal results are found during a compilation.
8694 @end defmac
8696 @defmac AC_MSG_WARN (@var{problem-description})
8697 @acindex{MSG_WARN}
8698 Notify the @command{configure} user of a possible problem.  This macro
8699 prints the message to the standard error output; @command{configure}
8700 continues running afterward, so macros that call @code{AC_MSG_WARN} should
8701 provide a default (back-up) behavior for the situations they warn about.
8702 @var{problem-description} should be something like @samp{ln -s seems to
8703 make hard links}.
8704 @end defmac
8708 @c ====================================================== Programming in M4.
8710 @node Programming in M4
8711 @chapter Programming in M4
8712 @cindex M4
8714 Autoconf is written on top of two layers: @dfn{M4sugar}, which provides
8715 convenient macros for pure M4 programming, and @dfn{M4sh}, which
8716 provides macros dedicated to shell script generation.
8718 As of this version of Autoconf, these two layers are still experimental,
8719 and their interface might change in the future.  As a matter of fact,
8720 @emph{anything that is not documented must not be used}.
8722 @menu
8723 * M4 Quotation::                Protecting macros from unwanted expansion
8724 * Using autom4te::              The Autoconf executables backbone
8725 * Programming in M4sugar::      Convenient pure M4 macros
8726 * Programming in M4sh::         Common shell Constructs
8727 * File Descriptor Macros::      File descriptor macros for input and output
8728 @end menu
8730 @node M4 Quotation
8731 @section M4 Quotation
8732 @cindex M4 quotation
8733 @cindex quotation
8735 @c FIXME: Grmph, yet another quoting myth: quotation has *never*
8736 @c prevented `expansion' of $1.  Unless it refers to the expansion
8737 @c of the value of $1?  Anyway, we need a rewrite here@enddots{}
8739 The most common problem with existing macros is an improper quotation.
8740 This section, which users of Autoconf can skip, but which macro writers
8741 @emph{must} read, first justifies the quotation scheme that was chosen
8742 for Autoconf and then ends with a rule of thumb.  Understanding the
8743 former helps one to follow the latter.
8745 @menu
8746 * Active Characters::           Characters that change the behavior of M4
8747 * One Macro Call::              Quotation and one macro call
8748 * Quotation and Nested Macros::  Macros calling macros
8749 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
8750 * Quadrigraphs::                Another way to escape special characters
8751 * Quotation Rule Of Thumb::     One parenthesis, one quote
8752 @end menu
8754 @node Active Characters
8755 @subsection Active Characters
8757 To fully understand where proper quotation is important, you first need
8758 to know what the special characters are in Autoconf: @samp{#} introduces
8759 a comment inside which no macro expansion is performed, @samp{,}
8760 separates arguments, @samp{[} and @samp{]} are the quotes themselves,
8761 and finally @samp{(} and @samp{)} (which M4 tries to match by
8762 pairs).
8764 In order to understand the delicate case of macro calls, we first have
8765 to present some obvious failures.  Below they are ``obvious-ified'',
8766 but when you find them in real life, they are usually in disguise.
8768 Comments, introduced by a hash and running up to the newline, are opaque
8769 tokens to the top level: active characters are turned off, and there is
8770 no macro expansion:
8772 @example
8773 # define([def], ine)
8774 @result{}# define([def], ine)
8775 @end example
8777 Each time there can be a macro expansion, there is a quotation
8778 expansion, i.e., one level of quotes is stripped:
8780 @example
8781 int tab[10];
8782 @result{}int tab10;
8783 [int tab[10];]
8784 @result{}int tab[10];
8785 @end example
8787 Without this in mind, the reader might try hopelessly to use her macro
8788 @code{array}:
8790 @example
8791 define([array], [int tab[10];])
8792 array
8793 @result{}int tab10;
8794 [array]
8795 @result{}array
8796 @end example
8798 @noindent
8799 How can you correctly output the intended results@footnote{Using
8800 @code{defn}.}?
8803 @node One Macro Call
8804 @subsection One Macro Call
8806 Let's proceed on the interaction between active characters and macros
8807 with this small macro, which just returns its first argument:
8809 @example
8810 define([car], [$1])
8811 @end example
8813 @noindent
8814 The two pairs of quotes above are not part of the arguments of
8815 @code{define}; rather, they are understood by the top level when it
8816 tries to find the arguments of @code{define}.  Therefore, assuming
8817 @code{car} is not already defined, it is equivalent to write:
8819 @example
8820 define(car, $1)
8821 @end example
8823 @noindent
8824 But, while it is acceptable for a @file{configure.ac} to avoid unnecessary
8825 quotes, it is bad practice for Autoconf macros which must both be more
8826 robust and also advocate perfect style.
8828 At the top level, there are only two possibilities: either you
8829 quote or you don't:
8831 @example
8832 car(foo, bar, baz)
8833 @result{}foo
8834 [car(foo, bar, baz)]
8835 @result{}car(foo, bar, baz)
8836 @end example
8838 Let's pay attention to the special characters:
8840 @example
8841 car(#)
8842 @error{}EOF in argument list
8843 @end example
8845 The closing parenthesis is hidden in the comment; with a hypothetical
8846 quoting, the top level understood it this way:
8848 @example
8849 car([#)]
8850 @end example
8852 @noindent
8853 Proper quotation, of course, fixes the problem:
8855 @example
8856 car([#])
8857 @result{}#
8858 @end example
8860 Here are more examples:
8862 @example
8863 car(foo, bar)
8864 @result{}foo
8865 car([foo, bar])
8866 @result{}foo, bar
8867 car((foo, bar))
8868 @result{}(foo, bar)
8869 car([(foo], [bar)])
8870 @result{}(foo
8871 define([a], [b])
8872 @result{}
8873 car(a)
8874 @result{}b
8875 car([a])
8876 @result{}b
8877 car([[a]])
8878 @result{}a
8879 car([[[a]]])
8880 @result{}[a]
8881 @end example
8883 With this in mind, we can explore the cases where macros invoke
8884 macros@enddots{}
8887 @node Quotation and Nested Macros
8888 @subsection Quotation and Nested Macros
8890 The examples below use the following macros:
8892 @example
8893 define([car], [$1])
8894 define([active], [ACT, IVE])
8895 define([array], [int tab[10]])
8896 @end example
8898 Each additional embedded macro call introduces other possible
8899 interesting quotations:
8901 @example
8902 car(active)
8903 @result{}ACT
8904 car([active])
8905 @result{}ACT, IVE
8906 car([[active]])
8907 @result{}active
8908 @end example
8910 In the first case, the top level looks for the arguments of @code{car},
8911 and finds @samp{active}.  Because M4 evaluates its arguments
8912 before applying the macro, @samp{active} is expanded, which results in:
8914 @example
8915 car(ACT, IVE)
8916 @result{}ACT
8917 @end example
8919 @noindent
8920 In the second case, the top level gives @samp{active} as first and only
8921 argument of @code{car}, which results in:
8923 @example
8924 active
8925 @result{}ACT, IVE
8926 @end example
8928 @noindent
8929 i.e., the argument is evaluated @emph{after} the macro that invokes it.
8930 In the third case, @code{car} receives @samp{[active]}, which results in:
8932 @example
8933 [active]
8934 @result{}active
8935 @end example
8937 @noindent
8938 exactly as we already saw above.
8940 The example above, applied to a more realistic example, gives:
8942 @example
8943 car(int tab[10];)
8944 @result{}int tab10;
8945 car([int tab[10];])
8946 @result{}int tab10;
8947 car([[int tab[10];]])
8948 @result{}int tab[10];
8949 @end example
8951 @noindent
8952 Huh?  The first case is easily understood, but why is the second wrong,
8953 and the third right?  To understand that, you must know that after
8954 M4 expands a macro, the resulting text is immediately subjected
8955 to macro expansion and quote removal.  This means that the quote removal
8956 occurs twice---first before the argument is passed to the @code{car}
8957 macro, and second after the @code{car} macro expands to the first
8958 argument.
8960 As the author of the Autoconf macro @code{car}, you then consider it to
8961 be incorrect that your users have to double-quote the arguments of
8962 @code{car}, so you ``fix'' your macro.  Let's call it @code{qar} for
8963 quoted car:
8965 @example
8966 define([qar], [[$1]])
8967 @end example
8969 @noindent
8970 and check that @code{qar} is properly fixed:
8972 @example
8973 qar([int tab[10];])
8974 @result{}int tab[10];
8975 @end example
8977 @noindent
8978 Ahhh!  That's much better.
8980 But note what you've done: now that the arguments are literal strings,
8981 if the user wants to use the results of expansions as arguments, she has
8982 to use an @emph{unquoted} macro call:
8984 @example
8985 qar(active)
8986 @result{}ACT
8987 @end example
8989 @noindent
8990 where she wanted to reproduce what she used to do with @code{car}:
8992 @example
8993 car([active])
8994 @result{}ACT, IVE
8995 @end example
8997 @noindent
8998 Worse yet: she wants to use a macro that produces a set of @code{cpp}
8999 macros:
9001 @example
9002 define([my_includes], [#include <stdio.h>])
9003 car([my_includes])
9004 @result{}#include <stdio.h>
9005 qar(my_includes)
9006 @error{}EOF in argument list
9007 @end example
9009 This macro, @code{qar}, because it double quotes its arguments, forces
9010 its users to leave their macro calls unquoted, which is dangerous.
9011 Commas and other active symbols are interpreted by M4 before
9012 they are given to the macro, often not in the way the users expect.
9013 Also, because @code{qar} behaves differently from the other macros,
9014 it's an exception that should be avoided in Autoconf.
9016 @node Changequote is Evil
9017 @subsection @code{changequote} is Evil
9018 @cindex @code{changequote}
9020 The temptation is often high to bypass proper quotation, in particular
9021 when it's late at night.  Then, many experienced Autoconf hackers
9022 finally surrender to the dark side of the force and use the ultimate
9023 weapon: @code{changequote}.
9025 The M4 builtin @code{changequote} belongs to a set of primitives that
9026 allow one to adjust the syntax of the language to adjust it to one's
9027 needs.  For instance, by default M4 uses @samp{`} and @samp{'} as
9028 quotes, but in the context of shell programming (and actually of most
9029 programming languages), that's about the worst choice one can make:
9030 because of strings and back-quoted expressions in shell code (such as
9031 @samp{'this'} and @samp{`that`}), because of literal characters in usual
9032 programming languages (as in @samp{'0'}), there are many unbalanced
9033 @samp{`} and @samp{'}.  Proper M4 quotation then becomes a nightmare, if
9034 not impossible.  In order to make M4 useful in such a context, its
9035 designers have equipped it with @code{changequote}, which makes it
9036 possible to choose another pair of quotes.  M4sugar, M4sh, Autoconf, and
9037 Autotest all have chosen to use @samp{[} and @samp{]}.  Not especially
9038 because they are unlikely characters, but @emph{because they are
9039 characters unlikely to be unbalanced}.
9041 There are other magic primitives, such as @code{changecom} to specify
9042 what syntactic forms are comments (it is common to see
9043 @samp{changecom(<!--, -->)} when M4 is used to produce HTML pages),
9044 @code{changeword} and @code{changesyntax} to change other syntactic
9045 details (such as the character to denote the @var{n}th argument, @samp{$} by
9046 default, the parenthesis around arguments, etc.).
9048 These primitives are really meant to make M4 more useful for specific
9049 domains: they should be considered like command line options:
9050 @option{--quotes}, @option{--comments}, @option{--words}, and
9051 @option{--syntax}.  Nevertheless, they are implemented as M4 builtins, as
9052 it makes M4 libraries self contained (no need for additional options).
9054 There lies the problem@enddots{}
9056 @sp 1
9058 The problem is that it is then tempting to use them in the middle of an
9059 M4 script, as opposed to its initialization.  This, if not carefully
9060 thought out, can lead to disastrous effects: @emph{you are changing the
9061 language in the middle of the execution}.  Changing and restoring the
9062 syntax is often not enough: if you happened to invoke macros in between,
9063 these macros are lost, as the current syntax is probably not
9064 the one they were implemented with.
9066 @c FIXME: I've been looking for a short, real case example, but I
9067 @c lost them all :(
9070 @node Quadrigraphs
9071 @subsection Quadrigraphs
9072 @cindex quadrigraphs
9073 @cindex @samp{@@S|@@}
9074 @cindex @samp{@@&t@@}
9075 @c Info cannot handle `:' in index entries.
9076 @c @cindex @samp{@@<:@@}
9077 @c @cindex @samp{@@:>@@}
9078 @c @cindex @samp{@@%:@@}
9080 When writing an Autoconf macro you may occasionally need to generate
9081 special characters that are difficult to express with the standard
9082 Autoconf quoting rules.  For example, you may need to output the regular
9083 expression @samp{[^[]}, which matches any character other than @samp{[}.
9084 This expression contains unbalanced brackets so it cannot be put easily
9085 into an M4 macro.
9087 You can work around this problem by using one of the following
9088 @dfn{quadrigraphs}:
9090 @table @samp
9091 @item @@<:@@
9092 @samp{[}
9093 @item @@:>@@
9094 @samp{]}
9095 @item @@S|@@
9096 @samp{$}
9097 @item @@%:@@
9098 @samp{#}
9099 @item @@&t@@
9100 Expands to nothing.
9101 @end table
9103 Quadrigraphs are replaced at a late stage of the translation process,
9104 after @command{m4} is run, so they do not get in the way of M4 quoting.
9105 For example, the string @samp{^@@<:@@}, independently of its quotation,
9106 appears as @samp{^[} in the output.
9108 The empty quadrigraph can be used:
9110 @itemize @minus
9111 @item to mark trailing spaces explicitly
9113 Trailing spaces are smashed by @command{autom4te}.  This is a feature.
9115 @item to produce other quadrigraphs
9117 For instance @samp{@@<@@&t@@:@@} produces @samp{@@<:@@}.
9119 @item to escape @emph{occurrences} of forbidden patterns
9121 For instance you might want to mention @code{AC_FOO} in a comment, while
9122 still being sure that @command{autom4te} still catches unexpanded
9123 @samp{AC_*}.  Then write @samp{AC@@&t@@_FOO}.
9124 @end itemize
9126 The name @samp{@@&t@@} was suggested by Paul Eggert:
9128 @quotation
9129 I should give some credit to the @samp{@@&t@@} pun.  The @samp{&} is my
9130 own invention, but the @samp{t} came from the source code of the
9131 @sc{algol68c} compiler, written by Steve Bourne (of Bourne shell fame),
9132 and which used @samp{mt} to denote the empty string.  In C, it would
9133 have looked like something like:
9135 @example
9136 char const mt[] = "";
9137 @end example
9139 @noindent
9140 but of course the source code was written in Algol 68.
9142 I don't know where he got @samp{mt} from: it could have been his own
9143 invention, and I suppose it could have been a common pun around the
9144 Cambridge University computer lab at the time.
9145 @end quotation
9147 @node Quotation Rule Of Thumb
9148 @subsection Quotation Rule Of Thumb
9150 To conclude, the quotation rule of thumb is:
9152 @center @emph{One pair of quotes per pair of parentheses.}
9154 Never over-quote, never under-quote, in particular in the definition of
9155 macros.  In the few places where the macros need to use brackets
9156 (usually in C program text or regular expressions), properly quote
9157 @emph{the arguments}!
9159 It is common to read Autoconf programs with snippets like:
9161 @example
9162 AC_TRY_LINK(
9163 changequote(<<, >>)dnl
9164 <<#include <time.h>
9165 #ifndef tzname /* For SGI.  */
9166 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9167 #endif>>,
9168 changequote([, ])dnl
9169 [atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
9170 @end example
9172 @noindent
9173 which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
9174 double quoting, so you just need:
9176 @example
9177 AC_TRY_LINK(
9178 [#include <time.h>
9179 #ifndef tzname /* For SGI.  */
9180 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9181 #endif],
9182             [atoi (*tzname);],
9183             [ac_cv_var_tzname=yes],
9184             [ac_cv_var_tzname=no])
9185 @end example
9187 @noindent
9188 The M4-fluent reader might note that these two examples are rigorously
9189 equivalent, since M4 swallows both the @samp{changequote(<<, >>)}
9190 and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
9191 quotes are not part of the arguments!
9193 Simplified, the example above is just doing this:
9195 @example
9196 changequote(<<, >>)dnl
9197 <<[]>>
9198 changequote([, ])dnl
9199 @end example
9201 @noindent
9202 instead of simply:
9204 @example
9205 [[]]
9206 @end example
9208 With macros that do not double quote their arguments (which is the
9209 rule), double-quote the (risky) literals:
9211 @example
9212 AC_LINK_IFELSE([AC_LANG_PROGRAM(
9213 [[#include <time.h>
9214 #ifndef tzname /* For SGI.  */
9215 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9216 #endif]],
9217                                 [atoi (*tzname);])],
9218                [ac_cv_var_tzname=yes],
9219                [ac_cv_var_tzname=no])
9220 @end example
9222 Please note that the macro @code{AC_TRY_LINK} is obsolete, so you really
9223 should be using @code{AC_LINK_IFELSE} instead.
9225 @xref{Quadrigraphs}, for what to do if you run into a hopeless case
9226 where quoting does not suffice.
9228 When you create a @command{configure} script using newly written macros,
9229 examine it carefully to check whether you need to add more quotes in
9230 your macros.  If one or more words have disappeared in the M4
9231 output, you need more quotes.  When in doubt, quote.
9233 However, it's also possible to put on too many layers of quotes.  If
9234 this happens, the resulting @command{configure} script may contain
9235 unexpanded macros.  The @command{autoconf} program checks for this problem
9236 by looking for the string @samp{AC_} in @file{configure}.  However, this
9237 heuristic does not work in general: for example, it does not catch
9238 overquoting in @code{AC_DEFINE} descriptions.
9241 @c ---------------------------------------- Using autom4te
9243 @node Using autom4te
9244 @section Using @command{autom4te}
9246 The Autoconf suite, including M4sugar, M4sh, and Autotest, in addition
9247 to Autoconf per se, heavily rely on M4.  All these different uses
9248 revealed common needs factored into a layer over M4:
9249 @command{autom4te}@footnote{
9251 Yet another great name from Lars J. Aas.
9255 @command{autom4te} is a preprocessor that is like @command{m4}.
9256 It supports M4 extensions designed for use in tools like Autoconf.
9258 @menu
9259 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
9260 * Customizing autom4te::        Customizing the Autoconf package
9261 @end menu
9263 @node autom4te Invocation
9264 @subsection Invoking @command{autom4te}
9266 The command line arguments are modeled after M4's:
9268 @example
9269 autom4te @var{options} @var{files}
9270 @end example
9272 @noindent
9273 @evindex M4
9274 where the @var{files} are directly passed to @command{m4}.  By default,
9275 @acronym{GNU} M4 is found during configuration, but the environment
9276 variable
9277 @env{M4} can be set to tell @command{autom4te} where to look.  In addition
9278 to the regular expansion, it handles the replacement of the quadrigraphs
9279 (@pxref{Quadrigraphs}), and of @samp{__oline__}, the current line in the
9280 output.  It supports an extended syntax for the @var{files}:
9282 @table @file
9283 @item @var{file}.m4f
9284 This file is an M4 frozen file.  Note that @emph{all the previous files
9285 are ignored}.  See the option @option{--melt} for the rationale.
9287 @item @var{file}?
9288 If found in the library path, the @var{file} is included for expansion,
9289 otherwise it is ignored instead of triggering a failure.
9290 @end table
9292 @sp 1
9294 Of course, it supports the Autoconf common subset of options:
9296 @table @option
9297 @item --help
9298 @itemx -h
9299 Print a summary of the command line options and exit.
9301 @item --version
9302 @itemx -V
9303 Print the version number of Autoconf and exit.
9305 @item --verbose
9306 @itemx -v
9307 Report processing steps.
9309 @item --debug
9310 @itemx -d
9311 Don't remove the temporary files and be even more verbose.
9313 @item --include=@var{dir}
9314 @itemx -I @var{dir}
9315 Also look for input files in @var{dir}.  Multiple invocations
9316 accumulate.
9318 @item --output=@var{file}
9319 @itemx -o @var{file}
9320 Save output (script or trace) to @var{file}.  The file @option{-} stands
9321 for the standard output.
9322 @end table
9324 @sp 1
9326 As an extension of @command{m4}, it includes the following options:
9328 @table @option
9329 @item --warnings=@var{category}
9330 @itemx -W @var{category}
9331 @evindex WARNINGS
9332 @c FIXME: Point to the M4sugar macros, not Autoconf's.
9333 Report the warnings related to @var{category} (which can actually be a
9334 comma separated list).  @xref{Reporting Messages}, macro
9335 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
9336 values include:
9338 @table @samp
9339 @item all
9340 report all the warnings
9342 @item none
9343 report none
9345 @item error
9346 treats warnings as errors
9348 @item no-@var{category}
9349 disable warnings falling into @var{category}
9350 @end table
9352 Warnings about @samp{syntax} are enabled by default, and the environment
9353 variable @env{WARNINGS}, a comma separated list of categories, is
9354 honored.  @samp{autom4te -W @var{category}} actually
9355 behaves as if you had run:
9357 @example
9358 autom4te --warnings=syntax,$WARNINGS,@var{category}
9359 @end example
9361 @noindent
9362 For example, if you want to disable defaults and @env{WARNINGS}
9363 of @command{autom4te}, but enable the warnings about obsolete
9364 constructs, you would use @option{-W none,obsolete}.
9366 @cindex Back trace
9367 @cindex Macro invocation stack
9368 @command{autom4te} displays a back trace for errors, but not for
9369 warnings; if you want them, just pass @option{-W error}.
9371 @item --melt
9372 @itemx -M
9373 Do not use frozen files.  Any argument @code{@var{file}.m4f} is
9374 replaced by @code{@var{file}.m4}.  This helps tracing the macros which
9375 are executed only when the files are frozen, typically
9376 @code{m4_define}.  For instance, running:
9378 @example
9379 autom4te --melt 1.m4 2.m4f 3.m4 4.m4f input.m4
9380 @end example
9382 @noindent
9383 is roughly equivalent to running:
9385 @example
9386 m4 1.m4 2.m4 3.m4 4.m4 input.m4
9387 @end example
9389 @noindent
9390 while
9392 @example
9393 autom4te 1.m4 2.m4f 3.m4 4.m4f input.m4
9394 @end example
9396 @noindent
9397 is equivalent to:
9399 @example
9400 m4 --reload-state=4.m4f input.m4
9401 @end example
9403 @item --freeze
9404 @itemx -f
9405 Produce a frozen state file.  @command{autom4te} freezing is stricter
9406 than M4's: it must produce no warnings, and no output other than empty
9407 lines (a line with white space is @emph{not} empty) and comments
9408 (starting with @samp{#}).  Unlike @command{m4}'s similarly-named option,
9409 this option takes no argument:
9411 @example
9412 autom4te 1.m4 2.m4 3.m4 --freeze --output=3.m4f
9413 @end example
9415 @noindent
9416 corresponds to
9418 @example
9419 m4 1.m4 2.m4 3.m4 --freeze-state=3.m4f
9420 @end example
9422 @item --mode=@var{octal-mode}
9423 @itemx -m @var{octal-mode}
9424 Set the mode of the non-traces output to @var{octal-mode}; by default
9425 @samp{0666}.
9426 @end table
9428 @sp 1
9430 @cindex @file{autom4te.cache}
9431 As another additional feature over @command{m4}, @command{autom4te}
9432 caches its results.  @acronym{GNU} M4 is able to produce a regular
9433 output and traces at the same time.  Traces are heavily used in the
9434 @acronym{GNU} Build System: @command{autoheader} uses them to build
9435 @file{config.h.in}, @command{autoreconf} to determine what
9436 @acronym{GNU} Build System components are used, @command{automake} to
9437 ``parse'' @file{configure.ac} etc.  To avoid recomputation,
9438 traces are cached while performing regular expansion,
9439 and conversely.  This cache is (actually, the caches are) stored in
9440 the directory @file{autom4te.cache}.  @emph{It can safely be removed}
9441 at any moment (especially if for some reason @command{autom4te}
9442 considers it is trashed).
9444 @table @option
9445 @item --cache=@var{directory}
9446 @itemx -C @var{directory}
9447 Specify the name of the directory where the result should be cached.
9448 Passing an empty value disables caching.  Be sure to pass a relative
9449 file name, as for the time being, global caches are not supported.
9451 @item --no-cache
9452 Don't cache the results.
9454 @item --force
9455 @itemx -f
9456 If a cache is used, consider it obsolete (but update it anyway).
9457 @end table
9459 @sp 1
9461 Because traces are so important to the @acronym{GNU} Build System,
9462 @command{autom4te} provides high level tracing features as compared to
9463 M4, and helps exploiting the cache:
9465 @table @option
9466 @item --trace=@var{macro}[:@var{format}]
9467 @itemx -t @var{macro}[:@var{format}]
9468 Trace the invocations of @var{macro} according to the @var{format}.
9469 Multiple @option{--trace} arguments can be used to list several macros.
9470 Multiple @option{--trace} arguments for a single macro are not
9471 cumulative; instead, you should just make @var{format} as long as
9472 needed.
9474 The @var{format} is a regular string, with newlines if desired, and
9475 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}.  It can
9476 use the following special escapes:
9478 @table @samp
9479 @item $$
9480 The character @samp{$}.
9482 @item $f
9483 The file name from which @var{macro} is called.
9485 @item $l
9486 The line number from which @var{macro} is called.
9488 @item $d
9489 The depth of the @var{macro} call.  This is an M4 technical detail that
9490 you probably don't want to know about.
9492 @item $n
9493 The name of the @var{macro}.
9495 @item $@var{num}
9496 The @var{num}th argument of the call to @var{macro}.
9498 @item $@@
9499 @itemx $@var{sep}@@
9500 @itemx $@{@var{separator}@}@@
9501 All the arguments passed to @var{macro}, separated by the character
9502 @var{sep} or the string @var{separator} (@samp{,} by default).  Each
9503 argument is quoted, i.e., enclosed in a pair of square brackets.
9505 @item $*
9506 @itemx $@var{sep}*
9507 @itemx $@{@var{separator}@}*
9508 As above, but the arguments are not quoted.
9510 @item $%
9511 @itemx $@var{sep}%
9512 @itemx $@{@var{separator}@}%
9513 As above, but the arguments are not quoted, all new line characters in
9514 the arguments are smashed, and the default separator is @samp{:}.
9516 The escape @samp{$%} produces single-line trace outputs (unless you put
9517 newlines in the @samp{separator}), while @samp{$@@} and @samp{$*} do
9518 not.
9519 @end table
9521 @xref{autoconf Invocation}, for examples of trace uses.
9523 @item --preselect=@var{macro}
9524 @itemx -p @var{macro}
9525 Cache the traces of @var{macro}, but do not enable traces.  This is
9526 especially important to save CPU cycles in the future.  For instance,
9527 when invoked, @command{autoconf} preselects all the macros that
9528 @command{autoheader}, @command{automake}, @command{autoreconf}, etc.,
9529 trace, so that running @command{m4} is not needed to trace them: the
9530 cache suffices.  This results in a huge speed-up.
9531 @end table
9533 @sp 1
9535 @cindex Autom4te Library
9536 Finally, @command{autom4te} introduces the concept of @dfn{Autom4te
9537 libraries}.  They consists in a powerful yet extremely simple feature:
9538 sets of combined command line arguments:
9540 @table @option
9541 @item --language=@var{language}
9542 @itemx -l @var{language}
9543 Use the @var{language} Autom4te library.  Current languages include:
9545 @table @code
9546 @item M4sugar
9547 create M4sugar output.
9549 @item M4sh
9550 create M4sh executable shell scripts.
9552 @item Autotest
9553 create Autotest executable test suites.
9555 @item Autoconf-without-aclocal-m4
9556 create Autoconf executable configure scripts without
9557 reading @file{aclocal.m4}.
9559 @item Autoconf
9560 create Autoconf executable configure scripts.  This language inherits
9561 all the characteristics of @code{Autoconf-without-aclocal-m4} and
9562 additionally reads @file{aclocal.m4}.
9563 @end table
9565 @item --prepend-include=@var{dir}
9566 @item -B @var{dir}
9567 Prepend directory @var{dir} to the search path.  This is used to include
9568 the language-specific files before any third-party macros.
9570 @end table
9572 @cindex @file{autom4te.cfg}
9573 As an example, if Autoconf is installed in its default location,
9574 @file{/usr/local}, the command @samp{autom4te -l m4sugar foo.m4} is
9575 strictly equivalent to the command:
9577 @example
9578 autom4te --prepend-include /usr/local/share/autoconf \
9579   m4sugar/m4sugar.m4f --warnings syntax foo.m4
9580 @end example
9582 @noindent
9583 Recursive expansion applies here: the command @samp{autom4te -l m4sh foo.m4}
9584 is the same as @samp{autom4te --language M4sugar m4sugar/m4sh.m4f
9585 foo.m4}, i.e.:
9587 @example
9588 autom4te --prepend-include /usr/local/share/autoconf \
9589   m4sugar/m4sugar.m4f m4sugar/m4sh.m4f --mode 777 foo.m4
9590 @end example
9592 @noindent
9593 The definition of the languages is stored in @file{autom4te.cfg}.
9595 @node Customizing autom4te
9596 @subsection Customizing @command{autom4te}
9598 One can customize @command{autom4te} via @file{~/.autom4te.cfg} (i.e.,
9599 as found in the user home directory), and @file{./.autom4te.cfg} (i.e.,
9600 as found in the directory from which @command{autom4te} is run).  The
9601 order is first reading @file{autom4te.cfg}, then @file{~/.autom4te.cfg},
9602 then @file{./.autom4te.cfg}, and finally the command line arguments.
9604 In these text files, comments are introduced with @code{#}, and empty
9605 lines are ignored.  Customization is performed on a per-language basis,
9606 wrapped in between a @samp{begin-language: "@var{language}"},
9607 @samp{end-language: "@var{language}"} pair.
9609 Customizing a language stands for appending options (@pxref{autom4te
9610 Invocation}) to the current definition of the language.  Options, and
9611 more generally arguments, are introduced by @samp{args:
9612 @var{arguments}}.  You may use the traditional shell syntax to quote the
9613 @var{arguments}.
9615 As an example, to disable Autoconf caches (@file{autom4te.cache})
9616 globally, include the following lines in @file{~/.autom4te.cfg}:
9618 @verbatim
9619 ## ------------------ ##
9620 ## User Preferences.  ##
9621 ## ------------------ ##
9623 begin-language: "Autoconf-without-aclocal-m4"
9624 args: --no-cache
9625 end-language: "Autoconf-without-aclocal-m4"
9626 @end verbatim
9629 @node Programming in M4sugar
9630 @section Programming in M4sugar
9632 @cindex M4sugar
9633 M4 by itself provides only a small, but sufficient, set of all-purpose
9634 macros.  M4sugar introduces additional generic macros.  Its name was
9635 coined by Lars J. Aas: ``Readability And Greater Understanding Stands 4
9636 M4sugar''.
9638 @menu
9639 * Redefined M4 Macros::         M4 builtins changed in M4sugar
9640 * Looping constructs::          Iteration in M4
9641 * Evaluation Macros::           More quotation and evaluation control
9642 * Text processing Macros::      String manipulation in M4
9643 * Forbidden Patterns::          Catching unexpanded macros
9644 @end menu
9646 @node Redefined M4 Macros
9647 @subsection Redefined M4 Macros
9649 @msindex{builtin}
9650 @msindex{decr}
9651 @msindex{define}
9652 @msindex{dumpdef}
9653 @msindex{errprint}
9654 @msindex{esyscmd}
9655 @msindex{eval}
9656 @msindex{format}
9657 @msindex{ifdef}
9658 @msindex{incr}
9659 @msindex{index}
9660 @msindex{indir}
9661 @msindex{len}
9662 @msindex{maketemp}
9663 @msindex{pushdef}
9664 @msindex{shift}
9665 @msindex{substr}
9666 @msindex{syscmd}
9667 @msindex{sysval}
9668 @msindex{translit}
9669 @msindex{undefine}
9670 With a few exceptions, all the M4 native macros are moved in the
9671 @samp{m4_} pseudo-namespace, e.g., M4sugar renames @code{define} as
9672 @code{m4_define} etc.
9674 Some M4 macros are redefined, and are slightly incompatible with their
9675 native equivalent.
9677 @defmac dnl
9678 @msindex{dnl}
9679 This macro kept its original name: no @code{m4_dnl} is defined.
9680 @end defmac
9682 @defmac m4_defn (@var{macro})
9683 @msindex{defn}
9684 Unlike the M4 builtin, this macro fails if @var{macro} is not
9685 defined.  See @code{m4_undefine}.
9686 @end defmac
9688 @defmac m4_exit (@var{exit-status})
9689 @msindex{exit}
9690 This macro corresponds to @code{m4exit}.
9691 @end defmac
9693 @defmac m4_if (@var{comment})
9694 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @ovar{not-equal})
9695 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @dots{})
9696 @msindex{if}
9697 This macro corresponds to @code{ifelse}.
9698 @end defmac
9700 @defmac m4_include (@var{file})
9701 @defmacx m4_sinclude (@var{file})
9702 @msindex{include}
9703 @msindex{sinclude}
9704 Like the M4 builtins, but warn against multiple inclusions of @var{file}.
9705 @end defmac
9707 @defmac m4_bpatsubst (@var{string}, @var{regexp}, @ovar{replacement})
9708 @msindex{bpatsubst}
9709 This macro corresponds to @code{patsubst}.  The name @code{m4_patsubst}
9710 is kept for future versions of M4sh, on top of @acronym{GNU} M4 which will
9711 provide extended regular expression syntax via @code{epatsubst}.
9712 @end defmac
9714 @defmac m4_popdef (@var{macro})
9715 @msindex{popdef}
9716 Unlike the M4 builtin, this macro fails if @var{macro} is not
9717 defined.  See @code{m4_undefine}.
9718 @end defmac
9720 @defmac m4_bregexp (@var{string}, @var{regexp}, @ovar{replacement})
9721 @msindex{bregexp}
9722 This macro corresponds to @code{regexp}.  The name @code{m4_regexp}
9723 is kept for future versions of M4sh, on top of @acronym{GNU} M4 which will
9724 provide extended regular expression syntax via @code{eregexp}.
9725 @end defmac
9727 @defmac m4_wrap (@var{text})
9728 @msindex{wrap}
9729 This macro corresponds to @code{m4wrap}.
9731 Posix requires arguments of multiple @code{m4wrap} calls to be
9732 reprocessed at @acronym{EOF} in the same order as the original calls.
9733 @acronym{GNU} M4 versions through 1.4.x, however, reprocess them in
9734 reverse order.  Your code should not depend on the order.
9736 Also, Posix requires @code{m4wrap} to ignore its second and succeeding
9737 arguments, but @acronym{GNU} M4 versions through 1.4.x concatenate the
9738 arguments with intervening spaces.  Your code should not pass more than
9739 one argument.
9741 You are encouraged to end @var{text} with @samp{[]}, to avoid unexpected
9742 token pasting between consecutive invocations of @code{m4_wrap}, as in:
9744 @example
9745 m4_define([foo], [bar])
9746 m4_define([foofoo], [OUCH])
9747 m4_wrap([foo])
9748 m4_wrap([foo])
9749 @result{}OUCH
9750 @end example
9751 @end defmac
9753 @defmac m4_undefine (@var{macro})
9754 @msindex{undefine}
9755 Unlike the M4 builtin, this macro fails if @var{macro} is not
9756 defined.  Use
9758 @example
9759 m4_ifdef([@var{macro}], [m4_undefine([@var{macro}])])
9760 @end example
9762 @noindent
9763 to recover the behavior of the builtin.
9764 @end defmac
9768 @node Looping constructs
9769 @subsection Looping constructs
9771 The following macros implement loops in M4.
9773 @defmac m4_for (@var{var}, @var{first}, @var{last}, @ovar{step}, @var{expression})
9774 @msindex{for}
9775 Loop over the numeric values between @var{first} and @var{last}
9776 including bounds by increments of @var{step}.  For each iteration,
9777 expand @var{expression} with the numeric value assigned to @var{var}.
9778 If @var{step} is omitted, it defaults to @samp{1} or @samp{-1} depending
9779 on the order of the limits.  If given, @var{step} has to match this
9780 order.
9781 @end defmac
9783 @defmac m4_foreach (@var{var}, @var{list}, @var{expression})
9784 @msindex{foreach}
9785 Loop over the comma-separated M4 list @var{list}, assigning each value
9786 to @var{var}, and expand @var{expression}.  The following example
9787 outputs two lines:
9789 @example
9790 m4_foreach([myvar], [[foo], [bar, baz]],
9791            [echo myvar
9794 @end example
9795 @end defmac
9797 @defmac m4_foreach_w (@var{var}, @var{list}, @var{expression})
9798 @msindex{foreach_w}
9799 Loop over the whitespace-separated list @var{list}, assigning each value
9800 to @var{var}, and expand @var{expression}.
9802 The deprecated macro @code{AC_FOREACH} is an alias of
9803 @code{m4_foreach_w}.
9804 @end defmac
9808 @node Evaluation Macros
9809 @subsection Evaluation Macros
9811 The following macros give some control over the order of the evaluation
9812 by adding or removing levels of quotes.  They are meant for hard-core M4
9813 programmers.
9815 @defmac m4_dquote (@var{arg1}, @dots{})
9816 @msindex{dquote}
9817 Return the arguments as a quoted list of quoted arguments.
9818 @end defmac
9820 @defmac m4_quote (@var{arg1}, @dots{})
9821 @msindex{quote}
9822 Return the arguments as a single entity, i.e., wrap them into a pair of
9823 quotes.
9824 @end defmac
9826 The following example aims at emphasizing the difference between (i), not
9827 using these macros, (ii), using @code{m4_quote}, and (iii), using
9828 @code{m4_dquote}.
9830 @example
9831 $ @kbd{cat example.m4}
9832 # Overquote, so that quotes are visible.
9833 m4_define([show], [$[]1 = [$1], $[]@@ = [$@@]])
9834 m4_divert(0)dnl
9835 show(a, b)
9836 show(m4_quote(a, b))
9837 show(m4_dquote(a, b))
9838 $ @kbd{autom4te -l m4sugar example.m4}
9839 $1 = a, $@@ = [a],[b]
9840 $1 = a,b, $@@ = [a,b]
9841 $1 = [a],[b], $@@ = [[a],[b]]
9842 @end example
9846 @node Text processing Macros
9847 @subsection Text processing Macros
9849 The following macros may be used to manipulate strings in M4.
9850 They are not intended for casual use.
9852 @defmac m4_re_escape (@var{string})
9853 @msindex{re_escape}
9854 Backslash-escape all characters in @var{string} that are active in
9855 regexps.
9856 @end defmac
9858 @defmac m4_tolower (@var{string})
9859 @defmacx m4_toupper (@var{string})
9860 @msindex{tolower}
9861 @msindex{toupper}
9862 Return @var{string} with letters converted to upper or lower case,
9863 respectively.
9864 @end defmac
9866 @defmac m4_split (@var{string}, @ovar{regexp})
9867 @msindex{split}
9868 Split @var{string} into an M4 list of elements quoted by @samp{[} and
9869 @samp{]}, while keeping white space at the beginning and at the end.
9870 If @var{regexp} is given, use it instead of @samp{[\t ]+} for splitting.
9871 If @var{string} is empty, the result is an empty list.
9872 @end defmac
9874 @defmac m4_normalize (@var{string})
9875 @msindex{normalize}
9876 Remove leading and trailing spaces and tabs, sequences of
9877 backslash-then-newline, and replace multiple spaces and tabs with a
9878 single space.
9879 @end defmac
9881 @defmac m4_append (@var{macro-name}, @var{string}, @ovar{separator})
9882 @defmacx m4_append_uniq (@var{macro-name}, @var{string}, @ovar{separator})
9883 @msindex{append}
9884 @msindex{append_uniq}
9885 Redefine @var{macro-name} to its former contents with @var{separator}
9886 and @var{string} added at the end.  If @var{macro-name} was undefined
9887 before (but not if it was defined but empty), then no @var{separator} is
9888 added.  @code{m4_append} can be used to grow strings, and
9889 @code{m4_append_uniq} to grow strings without duplicating substrings.
9890 @end defmac
9894 @node Forbidden Patterns
9895 @subsection Forbidden Patterns
9896 @cindex Forbidden patterns
9897 @cindex Patterns, forbidden
9899 M4sugar provides a means to define suspicious patterns, patterns
9900 describing tokens which should not be found in the output.  For
9901 instance, if an Autoconf @file{configure} script includes tokens such as
9902 @samp{AC_DEFINE}, or @samp{dnl}, then most probably something went
9903 wrong (typically a macro was not evaluated because of overquotation).
9905 M4sugar forbids all the tokens matching @samp{^m4_} and @samp{^dnl$}.
9907 @defmac m4_pattern_forbid (@var{pattern})
9908 @msindex{pattern_forbid}
9909 Declare that no token matching @var{pattern} must be found in the output.
9910 Comments are not checked; this can be a problem if, for instance, you
9911 have some macro left unexpanded after an @samp{#include}.  No consensus
9912 is currently found in the Autoconf community, as some people consider it
9913 should be valid to name macros in comments (which doesn't make sense to
9914 the author of this documentation, as @samp{#}-comments should document
9915 the output, not the input, documented by @samp{dnl} comments).
9916 @end defmac
9918 Of course, you might encounter exceptions to these generic rules, for
9919 instance you might have to refer to @samp{$m4_flags}.
9921 @defmac m4_pattern_allow (@var{pattern})
9922 @msindex{pattern_allow}
9923 Any token matching @var{pattern} is allowed, including if it matches an
9924 @code{m4_pattern_forbid} pattern.
9925 @end defmac
9927 @node Programming in M4sh
9928 @section Programming in M4sh
9930 @c FIXME: Eventually will become a chapter, as it is not related to
9931 @c programming in M4 per se.
9933 M4sh, pronounced ``mash'', is aiming at producing portable Bourne shell
9934 scripts.  This name was coined by Lars J. Aas, who notes that,
9935 according to the Webster's Revised Unabridged Dictionary (1913):
9937 @quotation
9938 Mash \Mash\, n.  [Akin to G. meisch, maisch, meische, maische, mash,
9939 wash, and prob.@: to AS. miscian to mix.  See ``Mix''.]
9941 @enumerate 1
9942 @item
9943 A mass of mixed ingredients reduced to a soft pulpy state by beating or
9944 pressure@enddots{}
9946 @item
9947 A mixture of meal or bran and water fed to animals.
9949 @item
9950 A mess; trouble.  [Obs.] --Beau.@: & Fl.
9951 @end enumerate
9952 @end quotation
9955 For the time being, it is not mature enough to be widely used.
9957 M4sh provides portable alternatives for some common shell constructs
9958 that unfortunately are not portable in practice.
9960 @c Deprecated, to be replaced by a better API
9961 @ignore
9962 @defmac AS_BASENAME (@var{file-name})
9963 @asindex{BASENAME}
9964 Output the non-directory portion of @var{file-name}.  For example,
9965 if @code{$file} is @samp{/one/two/three}, the command
9966 @code{base=`AS_BASENAME(["$file"])`} sets @code{base} to @samp{three}.
9967 @end defmac
9968 @end ignore
9970 @defmac AS_BOURNE_COMPATIBLE
9971 @asindex{BOURNE_COMPATIBLE}
9972 Set up the shell to be more compatible with the Bourne shell as
9973 standardized by Posix, if possible.  This may involve setting
9974 environment variables, or setting options, or similar
9975 implementation-specific actions.
9976 @end defmac
9978 @defmac AS_CASE (@var{word}, @ovar{pattern1}, @ovar{if-matched1}, @dots{}, @ovar{default})
9979 @asindex{CASE}
9980 Expand into a shell @samp{case} statement, where @var{word} is matched
9981 against one or more patterns.  @var{if-matched} is run if the
9982 corresponding pattern matched @var{word}, else @var{default} is run.
9983 @end defmac
9985 @defmac AS_DIRNAME (@var{file-name})
9986 @asindex{DIRNAME}
9987 Output the directory portion of @var{file-name}.  For example,
9988 if @code{$file} is @samp{/one/two/three}, the command
9989 @code{dir=`AS_DIRNAME(["$file"])`} sets @code{dir} to @samp{/one/two}.
9990 @end defmac
9992 @defmac AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{}, @ovar{run-if-false})
9993 @asindex{IF}
9994 Run shell code @var{test1}.  If @var{test1} exits with a zero status then
9995 run shell code @var{run-if-true1}, else examine further tests.  If no test
9996 exits with a zero status, run shell code @var{run-if-false}, with
9997 simplifications if either @var{run-if-true1} or @var{run-if-false1}
9998 is empty.  For example,
10000 @example
10001 AS_IF([test "$foo" = yes], [HANDLE_FOO([yes])],
10002       [test "$foo" != no], [HANDLE_FOO([maybe])],
10003       [echo foo not specified])
10004 @end example
10006 @noindent
10007 ensures any required macros of @code{HANDLE_FOO}
10008 are expanded before the first test.
10009 @end defmac
10011 @defmac AS_MKDIR_P (@var{file-name})
10012 @asindex{MKDIR_P}
10013 Make the directory @var{file-name}, including intervening directories
10014 as necessary.  This is equivalent to @samp{mkdir -p @var{file-name}},
10015 except that it is portable to older versions of @command{mkdir} that
10016 lack support for the @option{-p} option.  Also, @code{AS_MKDIR_P}
10017 succeeds if @var{file-name} is a symbolic link to an existing directory,
10018 even though Posix is unclear whether @samp{mkdir -p} should
10019 succeed in that case.  If creation of @var{file-name} fails, exit the
10020 script.
10022 Also see the @code{AC_PROG_MKDIR_P} macro (@pxref{Particular Programs}).
10023 @end defmac
10025 @defmac AS_SHELL_SANITIZE
10026 @asindex{SHELL_SANITIZE}
10027 Initialize the shell suitably for @code{configure} scripts.  This has
10028 the effect of @code{AS_BOURNE_COMPATIBLE}, and sets some other
10029 environment variables for predictable results from configuration tests.
10030 For example, it sets @env{LC_ALL} to change to the default C locale.
10031 @xref{Special Shell Variables}.
10032 @end defmac
10034 @defmac AS_TR_CPP (@var{expression})
10035 @asindex{TR_CPP}
10036 Transform @var{expression} into a valid right-hand side for a C @code{#define}.
10037 For example:
10039 @example
10040 # This outputs "#define HAVE_CHAR_P 1".
10041 type="char *"
10042 echo "#define AS_TR_CPP([HAVE_$type]) 1"
10043 @end example
10044 @end defmac
10046 @defmac AS_TR_SH (@var{expression})
10047 @asindex{TR_SH}
10048 Transform @var{expression} into a valid shell variable name.  For example:
10050 @example
10051 # This outputs "Have it!".
10052 header="sys/some file.h"
10053 AS_TR_SH([HAVE_$header])=yes
10054 if test "$HAVE_sys_some_file_h" = yes; then echo "Have it!"; fi
10055 @end example
10056 @end defmac
10058 @defmac AS_SET_CATFILE (@var{var}, @var{dir}, @var{file})
10059 @asindex{SET_CATFILE}
10060 Set the shell variable @var{var} to @var{dir}/@var{file}, but
10061 optimizing the common cases (@var{dir} or @var{file} is @samp{.},
10062 @var{file} is absolute, etc.).
10063 @end defmac
10066 @node File Descriptor Macros
10067 @section File Descriptor Macros
10068 @cindex input
10069 @cindex standard input
10070 @cindex file descriptors
10071 @cindex descriptors
10072 @cindex low-level output
10073 @cindex output, low-level
10075 The following macros define file descriptors used to output messages
10076 (or input values) from @file{configure} scripts.
10077 For example:
10079 @example
10080 echo "$wombats found" >&AS_MESSAGE_LOG_FD
10081 echo 'Enter desired kangaroo count:' >&AS_MESSAGE_FD
10082 read kangaroos <&AS_ORIGINAL_STDIN_FD`
10083 @end example
10085 @noindent
10086 However doing so is seldom needed, because Autoconf provides higher
10087 level macros as described below.
10089 @defmac AS_MESSAGE_FD
10090 @asindex{MESSAGE_FD}
10091 The file descriptor for @samp{checking for...}  messages and results.
10092 Normally this directs messages to the standard output, however when
10093 @command{configure} is run with the @option{-q} option, messages sent to
10094 @code{AS_MESSAGE_FD} are discarded.
10096 If you want to display some messages, consider using one of the printing
10097 macros (@pxref{Printing Messages}) instead.  Copies of messages output
10098 via these macros are also recorded in @file{config.log}.
10099 @end defmac
10101 @defmac AS_MESSAGE_LOG_FD
10102 @asindex{MESSAGE_LOG_FD}
10104 The file descriptor for messages logged to @file{config.log}.  Macros
10105 that run tools, like @code{AC_COMPILE_IFELSE} (@pxref{Running the
10106 Compiler}), redirect all output to this descriptor.  You may want to do
10107 so if you develop such a low-level macro.
10108 @end defmac
10110 @defmac AS_ORIGINAL_STDIN_FD
10111 @asindex{ORIGINAL_STDIN_FD}
10112 The file descriptor for the original standard input.
10114 When @command{configure} runs, it may accidentally execute an
10115 interactive command that has the same name as the non-interactive meant
10116 to be used or checked.  If the standard input was the terminal, such
10117 interactive programs would cause @command{configure} to stop, pending
10118 some user input.  Therefore @command{configure} redirects its standard
10119 input from @file{/dev/null} during its initialization.  This is not
10120 normally a problem, since @command{configure} normally does not need
10121 user input.
10123 In the extreme case where your @file{configure} script really needs to
10124 obtain some values from the original standard input, you can read them
10125 explicitly from @code{AS_ORIGINAL_STDIN_FD}.
10126 @end defmac
10129 @c =================================================== Writing Autoconf Macros.
10131 @node Writing Autoconf Macros
10132 @chapter Writing Autoconf Macros
10134 When you write a feature test that could be applicable to more than one
10135 software package, the best thing to do is encapsulate it in a new macro.
10136 Here are some instructions and guidelines for writing Autoconf macros.
10138 @menu
10139 * Macro Definitions::           Basic format of an Autoconf macro
10140 * Macro Names::                 What to call your new macros
10141 * Reporting Messages::          Notifying @command{autoconf} users
10142 * Dependencies Between Macros::  What to do when macros depend on other macros
10143 * Obsoleting Macros::           Warning about old ways of doing things
10144 * Coding Style::                Writing Autoconf macros @`a la Autoconf
10145 @end menu
10147 @node Macro Definitions
10148 @section Macro Definitions
10150 @acindex{DEFUN}
10151 Autoconf macros are defined using the @code{AC_DEFUN} macro, which is
10152 similar to the M4 builtin @code{m4_define} macro.  In addition to
10153 defining a macro, @code{AC_DEFUN} adds to it some code that is used to
10154 constrain the order in which macros are called (@pxref{Prerequisite
10155 Macros}).
10157 An Autoconf macro definition looks like this:
10159 @example
10160 AC_DEFUN(@var{macro-name}, @var{macro-body})
10161 @end example
10163 You can refer to any arguments passed to the macro as @samp{$1},
10164 @samp{$2}, etc.  @xref{Definitions, , How to define new macros, m4.info,
10165 @acronym{GNU} M4}, for more complete information on writing M4 macros.
10167 Be sure to properly quote both the @var{macro-body} @emph{and} the
10168 @var{macro-name} to avoid any problems if the macro happens to have
10169 been previously defined.
10171 Each macro should have a header comment that gives its prototype, and a
10172 brief description.  When arguments have default values, display them in
10173 the prototype.  For example:
10175 @example
10176 # AC_MSG_ERROR(ERROR, [EXIT-STATUS = 1])
10177 # --------------------------------------
10178 m4_define([AC_MSG_ERROR],
10179   [@{ AS_MESSAGE([error: $1], [2])
10180      exit m4_default([$2], [1]); @}])
10181 @end example
10183 Comments about the macro should be left in the header comment.  Most
10184 other comments make their way into @file{configure}, so just keep
10185 using @samp{#} to introduce comments.
10187 @cindex @code{dnl}
10188 If you have some special comments about pure M4 code, comments
10189 that make no sense in @file{configure} and in the header comment, then
10190 use the builtin @code{dnl}: it causes M4 to discard the text
10191 through the next newline.
10193 Keep in mind that @code{dnl} is rarely needed to introduce comments;
10194 @code{dnl} is more useful to get rid of the newlines following macros
10195 that produce no output, such as @code{AC_REQUIRE}.
10198 @node Macro Names
10199 @section Macro Names
10201 All of the Autoconf macros have all-uppercase names starting with
10202 @samp{AC_} to prevent them from accidentally conflicting with other
10203 text.  All shell variables that they use for internal purposes have
10204 mostly-lowercase names starting with @samp{ac_}.  To ensure that your
10205 macros don't conflict with present or future Autoconf macros, you should
10206 prefix your own macro names and any shell variables they use with some
10207 other sequence.  Possibilities include your initials, or an abbreviation
10208 for the name of your organization or software package.
10210 Most of the Autoconf macros' names follow a structured naming convention
10211 that indicates the kind of feature check by the name.  The macro names
10212 consist of several words, separated by underscores, going from most
10213 general to most specific.  The names of their cache variables use the
10214 same convention (@pxref{Cache Variable Names}, for more information on
10215 them).
10217 The first word of the name after @samp{AC_} usually tells the category
10218 of the feature being tested.  Here are the categories used in Autoconf for
10219 specific test macros, the kind of macro that you are more likely to
10220 write.  They are also used for cache variables, in all-lowercase.  Use
10221 them where applicable; where they're not, invent your own categories.
10223 @table @code
10224 @item C
10225 C language builtin features.
10226 @item DECL
10227 Declarations of C variables in header files.
10228 @item FUNC
10229 Functions in libraries.
10230 @item GROUP
10231 Posix group owners of files.
10232 @item HEADER
10233 Header files.
10234 @item LIB
10235 C libraries.
10236 @item PATH
10237 Absolute names of files, including programs.
10238 @item PROG
10239 The base names of programs.
10240 @item MEMBER
10241 Members of aggregates.
10242 @item SYS
10243 Operating system features.
10244 @item TYPE
10245 C builtin or declared types.
10246 @item VAR
10247 C variables in libraries.
10248 @end table
10250 After the category comes the name of the particular feature being
10251 tested.  Any further words in the macro name indicate particular aspects
10252 of the feature.  For example, @code{AC_FUNC_FNMATCH_GNU} checks whether
10253 the @code{fnmatch} function supports @acronym{GNU} extensions.
10255 An internal macro should have a name that starts with an underscore;
10256 Autoconf internals should therefore start with @samp{_AC_}.
10257 Additionally, a macro that is an internal subroutine of another macro
10258 should have a name that starts with an underscore and the name of that
10259 other macro, followed by one or more words saying what the internal
10260 macro does.  For example, @code{AC_PATH_X} has internal macros
10261 @code{_AC_PATH_X_XMKMF} and @code{_AC_PATH_X_DIRECT}.
10263 @node Reporting Messages
10264 @section Reporting Messages
10265 @cindex Messages, from @command{autoconf}
10267 When macros statically diagnose abnormal situations, benign or fatal,
10268 they should report them using these macros.  For dynamic issues, i.e.,
10269 when @command{configure} is run, see @ref{Printing Messages}.
10271 @defmac AC_DIAGNOSE (@var{category}, @var{message})
10272 @acindex{DIAGNOSE}
10273 Report @var{message} as a warning (or as an error if requested by the
10274 user) if warnings of the @var{category} are turned on.  You are
10275 encouraged to use standard categories, which currently include:
10277 @table @samp
10278 @item all
10279 messages that don't fall into one of the following categories.  Use of an
10280 empty @var{category} is equivalent.
10282 @item cross
10283 related to cross compilation issues.
10285 @item obsolete
10286 use of an obsolete construct.
10288 @item syntax
10289 dubious syntactic constructs, incorrectly ordered macro calls.
10290 @end table
10291 @end defmac
10293 @defmac AC_WARNING (@var{message})
10294 @acindex{WARNING}
10295 Equivalent to @samp{AC_DIAGNOSE([syntax], @var{message})}, but you are
10296 strongly encouraged to use a finer grained category.
10297 @end defmac
10299 @defmac AC_FATAL (@var{message})
10300 @acindex{FATAL}
10301 Report a severe error @var{message}, and have @command{autoconf} die.
10302 @end defmac
10304 When the user runs @samp{autoconf -W error}, warnings from
10305 @code{AC_DIAGNOSE} and @code{AC_WARNING} are reported as error, see
10306 @ref{autoconf Invocation}.
10308 @node Dependencies Between Macros
10309 @section Dependencies Between Macros
10310 @cindex Dependencies between macros
10312 Some Autoconf macros depend on other macros having been called first in
10313 order to work correctly.  Autoconf provides a way to ensure that certain
10314 macros are called if needed and a way to warn the user if macros are
10315 called in an order that might cause incorrect operation.
10317 @menu
10318 * Prerequisite Macros::         Ensuring required information
10319 * Suggested Ordering::          Warning about possible ordering problems
10320 * One-Shot Macros::             Ensuring a macro is called only once
10321 @end menu
10323 @node Prerequisite Macros
10324 @subsection Prerequisite Macros
10325 @cindex Prerequisite macros
10326 @cindex Macros, prerequisites
10328 A macro that you write might need to use values that have previously
10329 been computed by other macros.  For example, @code{AC_DECL_YYTEXT}
10330 examines the output of @code{flex} or @code{lex}, so it depends on
10331 @code{AC_PROG_LEX} having been called first to set the shell variable
10332 @code{LEX}.
10334 Rather than forcing the user of the macros to keep track of the
10335 dependencies between them, you can use the @code{AC_REQUIRE} macro to do
10336 it automatically.  @code{AC_REQUIRE} can ensure that a macro is only
10337 called if it is needed, and only called once.
10339 @defmac AC_REQUIRE (@var{macro-name})
10340 @acindex{REQUIRE}
10341 If the M4 macro @var{macro-name} has not already been called, call it
10342 (without any arguments).  Make sure to quote @var{macro-name} with
10343 square brackets.  @var{macro-name} must have been defined using
10344 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
10345 that it has been called.
10347 @code{AC_REQUIRE} must be used inside a macro defined by @code{AC_DEFUN}; it
10348 must not be called from the top level.
10349 @end defmac
10351 @code{AC_REQUIRE} is often misunderstood.  It really implements
10352 dependencies between macros in the sense that if one macro depends upon
10353 another, the latter is expanded @emph{before} the body of the
10354 former.  To be more precise, the required macro is expanded before
10355 the outermost defined macro in the current expansion stack.
10356 In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
10357 @code{FOO}.  For instance, this definition of macros:
10359 @example
10360 @group
10361 AC_DEFUN([TRAVOLTA],
10362 [test "$body_temperature_in_celsius" -gt "38" &&
10363   dance_floor=occupied])
10364 AC_DEFUN([NEWTON_JOHN],
10365 [test "$hair_style" = "curly" &&
10366   dance_floor=occupied])
10367 @end group
10369 @group
10370 AC_DEFUN([RESERVE_DANCE_FLOOR],
10371 [if date | grep '^Sat.*pm' >/dev/null 2>&1; then
10372   AC_REQUIRE([TRAVOLTA])
10373   AC_REQUIRE([NEWTON_JOHN])
10374 fi])
10375 @end group
10376 @end example
10378 @noindent
10379 with this @file{configure.ac}
10381 @example
10382 AC_INIT([Dance Manager], [1.0], [bug-dance@@example.org])
10383 RESERVE_DANCE_FLOOR
10384 if test "$dance_floor" = occupied; then
10385   AC_MSG_ERROR([cannot pick up here, let's move])
10387 @end example
10389 @noindent
10390 does not leave you with a better chance to meet a kindred soul at
10391 other times than Saturday night since it expands into:
10393 @example
10394 @group
10395 test "$body_temperature_in_Celsius" -gt "38" &&
10396   dance_floor=occupied
10397 test "$hair_style" = "curly" &&
10398   dance_floor=occupied
10400 if date | grep '^Sat.*pm' >/dev/null 2>&1; then
10404 @end group
10405 @end example
10407 This behavior was chosen on purpose: (i) it prevents messages in
10408 required macros from interrupting the messages in the requiring macros;
10409 (ii) it avoids bad surprises when shell conditionals are used, as in:
10411 @example
10412 @group
10413 if @dots{}; then
10414   AC_REQUIRE([SOME_CHECK])
10416 @dots{}
10417 SOME_CHECK
10418 @end group
10419 @end example
10421 The helper macros @code{AS_IF} and @code{AS_CASE} may be used to
10422 enforce expansion of required macros outside of shell conditional
10423 constructs.  You are furthermore encouraged to put all @code{AC_REQUIRE} calls
10424 at the beginning of a macro.  You can use @code{dnl} to avoid the empty
10425 lines they leave.
10427 @node Suggested Ordering
10428 @subsection Suggested Ordering
10429 @cindex Macros, ordering
10430 @cindex Ordering macros
10432 Some macros should be run before another macro if both are called, but
10433 neither @emph{requires} that the other be called.  For example, a macro
10434 that changes the behavior of the C compiler should be called before any
10435 macros that run the C compiler.  Many of these dependencies are noted in
10436 the documentation.
10438 Autoconf provides the @code{AC_BEFORE} macro to warn users when macros
10439 with this kind of dependency appear out of order in a
10440 @file{configure.ac} file.  The warning occurs when creating
10441 @command{configure} from @file{configure.ac}, not when running
10442 @command{configure}.
10444 For example, @code{AC_PROG_CPP} checks whether the C compiler
10445 can run the C preprocessor when given the @option{-E} option.  It should
10446 therefore be called after any macros that change which C compiler is
10447 being used, such as @code{AC_PROG_CC}.  So @code{AC_PROG_CC} contains:
10449 @example
10450 AC_BEFORE([$0], [AC_PROG_CPP])dnl
10451 @end example
10453 @noindent
10454 This warns the user if a call to @code{AC_PROG_CPP} has already occurred
10455 when @code{AC_PROG_CC} is called.
10457 @defmac AC_BEFORE (@var{this-macro-name}, @var{called-macro-name})
10458 @acindex{BEFORE}
10459 Make M4 print a warning message to the standard error output if
10460 @var{called-macro-name} has already been called.  @var{this-macro-name}
10461 should be the name of the macro that is calling @code{AC_BEFORE}.  The
10462 macro @var{called-macro-name} must have been defined using
10463 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
10464 that it has been called.
10465 @end defmac
10467 @node One-Shot Macros
10468 @subsection One-Shot Macros
10469 @cindex One-shot macros
10470 @cindex Macros, called once
10472 Some macros should be called only once, either because calling them
10473 multiple time is unsafe, or because it is bad style.  For instance
10474 Autoconf ensures that @code{AC_CANONICAL_BUILD} and cousins
10475 (@pxref{Canonicalizing}) are evaluated only once, because it makes no
10476 sense to run these expensive checks more than once.  Such one-shot
10477 macros can be defined using @code{AC_DEFUN_ONCE}.
10479 @defmac AC_DEFUN_ONCE (@var{macro-name}, @var{macro-body})
10480 @acindex{DEFUN_ONCE}
10482 Declare macro @var{macro-name} like @code{AC_DEFUN} would (@pxref{Macro
10483 Definitions}), and emit a warning any time the macro is called more than
10484 once.
10485 @end defmac
10487 Obviously it is not sensible to evaluate a macro defined by
10488 @code{AC_DEFUN_ONCE} in a macro defined by @code{AC_DEFUN}.
10489 Most of the time you want to use @code{AC_REQUIRE} (@pxref{Prerequisite
10490 Macros}).
10492 @node Obsoleting Macros
10493 @section Obsoleting Macros
10494 @cindex Obsoleting macros
10495 @cindex Macros, obsoleting
10497 Configuration and portability technology has evolved over the years.
10498 Often better ways of solving a particular problem are developed, or
10499 ad-hoc approaches are systematized.  This process has occurred in many
10500 parts of Autoconf.  One result is that some of the macros are now
10501 considered @dfn{obsolete}; they still work, but are no longer considered
10502 the best thing to do, hence they should be replaced with more modern
10503 macros.  Ideally, @command{autoupdate} should replace the old macro calls
10504 with their modern implementation.
10506 Autoconf provides a simple means to obsolete a macro.
10508 @defmac AU_DEFUN (@var{old-macro}, @var{implementation}, @ovar{message})
10509 @auindex{DEFUN}
10510 Define @var{old-macro} as @var{implementation}.  The only difference
10511 with @code{AC_DEFUN} is that the user is warned that
10512 @var{old-macro} is now obsolete.
10514 If she then uses @command{autoupdate}, the call to @var{old-macro} is
10515 replaced by the modern @var{implementation}.  @var{message} should
10516 include information on what to do after running @command{autoupdate};
10517 @command{autoupdate} prints it as a warning, and includes it
10518 in the updated @file{configure.ac} file.
10520 The details of this macro are hairy: if @command{autoconf} encounters an
10521 @code{AU_DEFUN}ed macro, all macros inside its second argument are expanded
10522 as usual.  However, when @command{autoupdate} is run, only M4 and M4sugar
10523 macros are expanded here, while all other macros are disabled and
10524 appear literally in the updated @file{configure.ac}.
10525 @end defmac
10527 @defmac AU_ALIAS (@var{old-name}, @var{new-name})
10528 @auindex{ALIAS}
10529 Used if the @var{old-name} is to be replaced by a call to @var{new-macro}
10530 with the same parameters.  This happens for example if the macro was renamed.
10531 @end defmac
10533 @node Coding Style
10534 @section Coding Style
10535 @cindex Coding style
10537 The Autoconf macros follow a strict coding style.  You are encouraged to
10538 follow this style, especially if you intend to distribute your macro,
10539 either by contributing it to Autoconf itself, or via other means.
10541 The first requirement is to pay great attention to the quotation.  For
10542 more details, see @ref{Autoconf Language}, and @ref{M4 Quotation}.
10544 Do not try to invent new interfaces.  It is likely that there is a macro
10545 in Autoconf that resembles the macro you are defining: try to stick to
10546 this existing interface (order of arguments, default values, etc.).  We
10547 @emph{are} conscious that some of these interfaces are not perfect;
10548 nevertheless, when harmless, homogeneity should be preferred over
10549 creativity.
10551 Be careful about clashes both between M4 symbols and between shell
10552 variables.
10554 If you stick to the suggested M4 naming scheme (@pxref{Macro Names}),
10555 you are unlikely to generate conflicts.  Nevertheless, when you need to
10556 set a special value, @emph{avoid using a regular macro name}; rather,
10557 use an ``impossible'' name.  For instance, up to version 2.13, the macro
10558 @code{AC_SUBST} used to remember what @var{symbol} macros were already defined
10559 by setting @code{AC_SUBST_@var{symbol}}, which is a regular macro name.
10560 But since there is a macro named @code{AC_SUBST_FILE}, it was just
10561 impossible to @samp{AC_SUBST(FILE)}!  In this case,
10562 @code{AC_SUBST(@var{symbol})} or @code{_AC_SUBST(@var{symbol})} should
10563 have been used (yes, with the parentheses).
10564 @c or better yet, high-level macros such as @code{m4_expand_once}
10566 No Autoconf macro should ever enter the user-variable name space; i.e.,
10567 except for the variables that are the actual result of running the
10568 macro, all shell variables should start with @code{ac_}.  In
10569 addition, small macros or any macro that is likely to be embedded in
10570 other macros should be careful not to use obvious names.
10572 @cindex @code{dnl}
10573 Do not use @code{dnl} to introduce comments: most of the comments you
10574 are likely to write are either header comments which are not output
10575 anyway, or comments that should make their way into @file{configure}.
10576 There are exceptional cases where you do want to comment special M4
10577 constructs, in which case @code{dnl} is right, but keep in mind that it
10578 is unlikely.
10580 M4 ignores the leading blanks and newlines before each argument.
10581 Use this feature to
10582 indent in such a way that arguments are (more or less) aligned with the
10583 opening parenthesis of the macro being called.  For instance, instead of
10585 @example
10586 AC_CACHE_CHECK(for EMX OS/2 environment,
10587 ac_cv_emxos2,
10588 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [return __EMX__;])],
10589 [ac_cv_emxos2=yes], [ac_cv_emxos2=no])])
10590 @end example
10592 @noindent
10593 write
10595 @example
10596 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
10597 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
10598                    [ac_cv_emxos2=yes],
10599                    [ac_cv_emxos2=no])])
10600 @end example
10602 @noindent
10603 or even
10605 @example
10606 AC_CACHE_CHECK([for EMX OS/2 environment],
10607                [ac_cv_emxos2],
10608                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
10609                                                    [return __EMX__;])],
10610                                   [ac_cv_emxos2=yes],
10611                                   [ac_cv_emxos2=no])])
10612 @end example
10614 When using @code{AC_RUN_IFELSE} or any macro that cannot work when
10615 cross-compiling, provide a pessimistic value (typically @samp{no}).
10617 Feel free to use various tricks to prevent auxiliary tools, such as
10618 syntax-highlighting editors, from behaving improperly.  For instance,
10619 instead of:
10621 @example
10622 m4_bpatsubst([$1], [$"])
10623 @end example
10625 @noindent
10628 @example
10629 m4_bpatsubst([$1], [$""])
10630 @end example
10632 @noindent
10633 so that Emacsen do not open an endless ``string'' at the first quote.
10634 For the same reasons, avoid:
10636 @example
10637 test $[#] != 0
10638 @end example
10640 @noindent
10641 and use:
10643 @example
10644 test $[@@%:@@] != 0
10645 @end example
10647 @noindent
10648 Otherwise, the closing bracket would be hidden inside a @samp{#}-comment,
10649 breaking the bracket-matching highlighting from Emacsen.  Note the
10650 preferred style to escape from M4: @samp{$[1]}, @samp{$[@@]}, etc.  Do
10651 not escape when it is unnecessary.  Common examples of useless quotation
10652 are @samp{[$]$1} (write @samp{$$1}), @samp{[$]var} (use @samp{$var}),
10653 etc.  If you add portability issues to the picture, you'll prefer
10654 @samp{$@{1+"$[@@]"@}} to @samp{"[$]@@"}, and you'll prefer do something
10655 better than hacking Autoconf @code{:-)}.
10657 When using @command{sed}, don't use @option{-e} except for indenting
10658 purposes.  With the @code{s} and @code{y} commands, the preferred
10659 separator is @samp{/} unless @samp{/} itself might appear in the pattern
10660 or replacement, in which case you should use @samp{|}, or optionally
10661 @samp{,} if you know the pattern and replacement cannot contain a file
10662 name.  If none of these characters will do, choose a printable character
10663 that cannot appear in the pattern or replacement.  Characters from the
10664 set @samp{"#$&'()*;<=>?`|~} are good choices if the pattern or
10665 replacement might contain a file name, since they have special meaning
10666 to the shell and are less likely to occur in file names.
10668 @xref{Macro Definitions}, for details on how to define a macro.  If a
10669 macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
10670 of an @code{AC_REQUIRE} directive, and macros required by other macros
10671 inside arguments do not need to be expanded before this macro, then
10672 use @code{m4_define}.  In case of doubt, use @code{AC_DEFUN}.
10673 All the @code{AC_REQUIRE} statements should be at the beginning of the
10674 macro, and each statement should be followed by @code{dnl}.
10676 You should not rely on the number of arguments: instead of checking
10677 whether an argument is missing, test that it is not empty.  It provides
10678 both a simpler and a more predictable interface to the user, and saves
10679 room for further arguments.
10681 Unless the macro is short, try to leave the closing @samp{])} at the
10682 beginning of a line, followed by a comment that repeats the name of the
10683 macro being defined.  This introduces an additional newline in
10684 @command{configure}; normally, that is not a problem, but if you want to
10685 remove it you can use @samp{[]dnl} on the last line.  You can similarly
10686 use @samp{[]dnl} after a macro call to remove its newline.  @samp{[]dnl}
10687 is recommended instead of @samp{dnl} to ensure that M4 does not
10688 interpret the @samp{dnl} as being attached to the preceding text or
10689 macro output.  For example, instead of:
10691 @example
10692 AC_DEFUN([AC_PATH_X],
10693 [AC_MSG_CHECKING([for X])
10694 AC_REQUIRE_CPP()
10695 @r{# @dots{}omitted@dots{}}
10696   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
10697 fi])
10698 @end example
10700 @noindent
10701 you would write:
10703 @example
10704 AC_DEFUN([AC_PATH_X],
10705 [AC_REQUIRE_CPP()[]dnl
10706 AC_MSG_CHECKING([for X])
10707 @r{# @dots{}omitted@dots{}}
10708   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
10709 fi[]dnl
10710 ])# AC_PATH_X
10711 @end example
10713 If the macro is long, try to split it into logical chunks.  Typically,
10714 macros that check for a bug in a function and prepare its
10715 @code{AC_LIBOBJ} replacement should have an auxiliary macro to perform
10716 this setup.  Do not hesitate to introduce auxiliary macros to factor
10717 your code.
10719 In order to highlight the recommended coding style, here is a macro
10720 written the old way:
10722 @example
10723 dnl Check for EMX on OS/2.
10724 dnl _AC_EMXOS2
10725 AC_DEFUN(_AC_EMXOS2,
10726 [AC_CACHE_CHECK(for EMX OS/2 environment, ac_cv_emxos2,
10727 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, return __EMX__;)],
10728 ac_cv_emxos2=yes, ac_cv_emxos2=no)])
10729 test "$ac_cv_emxos2" = yes && EMXOS2=yes])
10730 @end example
10732 @noindent
10733 and the new way:
10735 @example
10736 # _AC_EMXOS2
10737 # ----------
10738 # Check for EMX on OS/2.
10739 m4_define([_AC_EMXOS2],
10740 [AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
10741 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
10742                    [ac_cv_emxos2=yes],
10743                    [ac_cv_emxos2=no])])
10744 test "$ac_cv_emxos2" = yes && EMXOS2=yes[]dnl
10745 ])# _AC_EMXOS2
10746 @end example
10751 @c ============================================= Portable Shell Programming
10753 @node Portable Shell
10754 @chapter Portable Shell Programming
10755 @cindex Portable shell programming
10757 When writing your own checks, there are some shell-script programming
10758 techniques you should avoid in order to make your code portable.  The
10759 Bourne shell and upward-compatible shells like the Korn shell and Bash
10760 have evolved over the years, but to prevent trouble, do not take
10761 advantage of features that were added after Unix version 7, circa
10762 1977 (@pxref{Systemology}).
10764 You should not use shell functions, aliases, negated character
10765 classes, or other features that are not found in all Bourne-compatible
10766 shells; restrict yourself to the lowest common denominator.  Even
10767 @code{unset} is not supported by all shells!
10769 Some ancient systems have quite
10770 small limits on the length of the @samp{#!} line; for instance, 32
10771 bytes (not including the newline) on SunOS 4.
10772 A few ancient 4.2@acronym{BSD} based systems (such as Dynix circa 1984)
10773 required a single space between the @samp{#!} and the @samp{/}.
10774 However, these ancient systems are no longer of practical concern.
10776 The set of external programs you should run in a @command{configure} script
10777 is fairly small.  @xref{Utilities in Makefiles, , Utilities in
10778 Makefiles, standards, @acronym{GNU} Coding Standards}, for the list.  This
10779 restriction allows users to start out with a fairly small set of
10780 programs and build the rest, avoiding too many interdependencies between
10781 packages.
10783 Some of these external utilities have a portable subset of features; see
10784 @ref{Limitations of Usual Tools}.
10786 There are other sources of documentation about shells.  The
10787 specification for the Posix
10788 @uref{http://www.opengroup.org/@/susv3/@/utilities/@/xcu_chap02.html, Shell
10789 Command Language}, though more generous than the restrictive shell
10790 subset described above, is fairly portable nowadays.  Also please see
10791 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/, the Shell FAQs}.
10793 @menu
10794 * Shellology::                  A zoology of shells
10795 * Here-Documents::              Quirks and tricks
10796 * File Descriptors::            FDs and redirections
10797 * File System Conventions::     File names
10798 * Shell Substitutions::         Variable and command expansions
10799 * Assignments::                 Varying side effects of assignments
10800 * Parentheses::                 Parentheses in shell scripts
10801 * Slashes::                     Slashes in shell scripts
10802 * Special Shell Variables::     Variables you should not change
10803 * Limitations of Builtins::     Portable use of not so portable /bin/sh
10804 * Limitations of Usual Tools::  Portable use of portable tools
10805 @end menu
10807 @node Shellology
10808 @section Shellology
10809 @cindex Shellology
10811 There are several families of shells, most prominently the Bourne family
10812 and the C shell family which are deeply incompatible.  If you want to
10813 write portable shell scripts, avoid members of the C shell family.  The
10814 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/@/shell-differences/, the
10815 Shell difference FAQ} includes a small history of Posix shells, and a
10816 comparison between several of them.
10818 Below we describe some of the members of the Bourne shell family.
10820 @table @asis
10821 @item Ash
10822 @cindex Ash
10823 Ash is often used on @acronym{GNU}/Linux and @acronym{BSD}
10824 systems as a light-weight Bourne-compatible shell.  Ash 0.2 has some
10825 bugs that are fixed in the 0.3.x series, but portable shell scripts
10826 should work around them, since version 0.2 is still shipped with many
10827 @acronym{GNU}/Linux distributions.
10829 To be compatible with Ash 0.2:
10831 @itemize @minus
10832 @item
10833 don't use @samp{$?} after expanding empty or unset variables,
10834 or at the start of an @command{eval}:
10836 @example
10837 foo=
10838 false
10839 $foo
10840 echo "Do not use it: $?"
10841 false
10842 eval 'echo "Do not use it: $?"'
10843 @end example
10845 @item
10846 don't use command substitution within variable expansion:
10848 @example
10849 cat $@{FOO=`bar`@}
10850 @end example
10852 @item
10853 beware that single builtin substitutions are not performed by a
10854 subshell, hence their effect applies to the current shell!  @xref{Shell
10855 Substitutions}, item ``Command Substitution''.
10856 @end itemize
10858 @item Bash
10859 @cindex Bash
10860 To detect whether you are running Bash, test whether
10861 @code{BASH_VERSION} is set.  To require
10862 Posix compatibility, run @samp{set -o posix}.  @xref{Bash POSIX
10863 Mode, , Bash Posix Mode, bash, The @acronym{GNU} Bash Reference
10864 Manual}, for details.
10866 @item Bash 2.05 and later
10867 @cindex Bash 2.05 and later
10868 Versions 2.05 and later of Bash use a different format for the
10869 output of the @command{set} builtin, designed to make evaluating its
10870 output easier.  However, this output is not compatible with earlier
10871 versions of Bash (or with many other shells, probably).  So if
10872 you use Bash 2.05 or higher to execute @command{configure},
10873 you'll need to use Bash 2.05 for all other build tasks as well.
10875 @item Ksh
10876 @cindex Ksh
10877 @cindex Korn shell
10878 @prindex @samp{ksh}
10879 @prindex @samp{ksh88}
10880 @prindex @samp{ksh93}
10881 The Korn shell is compatible with the Bourne family and it mostly
10882 conforms to Posix.  It has two major variants commonly
10883 called @samp{ksh88} and @samp{ksh93}, named after the years of initial
10884 release.  It is usually called @command{ksh}, but is called @command{sh}
10885 on some hosts if you set your path appropriately.
10887 Solaris systems have three variants:
10888 @prindex @command{/usr/bin/ksh} on Solaris
10889 @command{/usr/bin/ksh} is @samp{ksh88}; it is
10890 standard on Solaris 2.0 and later.
10891 @prindex @command{/usr/xpg4/bin/sh} on Solaris
10892 @command{/usr/xpg4/bin/sh} is a Posix-compliant variant of
10893 @samp{ksh88}; it is standard on Solaris 9 and later.
10894 @prindex @command{/usr/dt/bin/dtksh} on Solaris
10895 @command{/usr/dt/bin/dtksh} is @samp{ksh93}.
10896 Variants that are not standard may be parts of optional
10897 packages.  There is no extra charge for these packages, but they are
10898 not part of a minimal OS install and therefore some installations may
10899 not have it.
10901 Starting with Tru64 Version 4.0, the Korn shell @command{/usr/bin/ksh}
10902 is also available as @command{/usr/bin/posix/sh}.  If the environment
10903 variable @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
10904 the standard shell conform to Posix.
10906 @item Pdksh
10907 @prindex @samp{pdksh}
10908 A public-domain clone of the Korn shell called @command{pdksh} is widely
10909 available: it has most of the @samp{ksh88} features along with a few of
10910 its own.  It usually sets @code{KSH_VERSION}, except if invoked as
10911 @command{/bin/sh} on Open@acronym{BSD}, and similarly to Bash you can require
10912 Posix compatibility by running @samp{set -o posix}.  Unfortunately, with
10913 @command{pdksh} 5.2.14 (the latest stable version as of February 2006)
10914 Posix mode is buggy and causes @command{pdksh} to depart from Posix in
10915 at least one respect:
10917 @example
10918 $ @kbd{echo "`echo \"hello\"`"}
10919 hello
10920 $ @kbd{set -o posix}
10921 $ @kbd{echo "`echo \"hello\"`"}
10922 "hello"
10923 @end example
10925 The last line of output contains spurious quotes.  This is yet another
10926 reason why portable shell code should not contain
10927 @code{"`@dots{}\"@dots{}\"@dots{}`"} constructs (@pxref{Shell
10928 Substitutions}).
10930 @item Zsh
10931 @cindex Zsh
10932 To detect whether you are running @command{zsh}, test whether
10933 @code{ZSH_VERSION} is set.  By default @command{zsh} is @emph{not}
10934 compatible with the Bourne shell: you must execute @samp{emulate sh},
10935 and for @command{zsh} versions before 3.1.6-dev-18 you must also
10936 set @code{NULLCMD} to @samp{:}.  @xref{Compatibility, , Compatibility,
10937 zsh, The Z Shell Manual}, for details.
10939 The default Mac OS X @command{sh} was originally Zsh; it was changed to
10940 Bash in Mac OS X 10.2.
10941 @end table
10943 The following discussion between Russ Allbery and Robert Lipe is worth
10944 reading:
10946 @noindent
10947 Russ Allbery:
10949 @quotation
10950 The @acronym{GNU} assumption that @command{/bin/sh} is the one and only shell
10951 leads to a permanent deadlock.  Vendors don't want to break users'
10952 existing shell scripts, and there are some corner cases in the Bourne
10953 shell that are not completely compatible with a Posix shell.  Thus,
10954 vendors who have taken this route will @emph{never} (OK@dots{}``never say
10955 never'') replace the Bourne shell (as @command{/bin/sh}) with a
10956 Posix shell.
10957 @end quotation
10959 @noindent
10960 Robert Lipe:
10962 @quotation
10963 This is exactly the problem.  While most (at least most System V's) do
10964 have a Bourne shell that accepts shell functions most vendor
10965 @command{/bin/sh} programs are not the Posix shell.
10967 So while most modern systems do have a shell @emph{somewhere} that meets the
10968 Posix standard, the challenge is to find it.
10969 @end quotation
10971 @node Here-Documents
10972 @section Here-Documents
10973 @cindex Here-documents
10974 @cindex Shell here-documents
10976 Don't rely on @samp{\} being preserved just because it has no special
10977 meaning together with the next symbol.  In the native @command{sh}
10978 on Open@acronym{BSD} 2.7 @samp{\"} expands to @samp{"} in here-documents with
10979 unquoted delimiter.  As a general rule, if @samp{\\} expands to @samp{\}
10980 use @samp{\\} to get @samp{\}.
10982 With Open@acronym{BSD} 2.7's @command{sh}
10984 @example
10985 @group
10986 $ @kbd{cat <<EOF
10987 > \" \\
10988 > EOF}
10989 " \
10990 @end group
10991 @end example
10993 @noindent
10994 and with Bash:
10996 @example
10997 @group
10998 bash-2.04$ @kbd{cat <<EOF
10999 > \" \\
11000 > EOF}
11001 \" \
11002 @end group
11003 @end example
11005 Some shells mishandle large here-documents: for example,
11006 Solaris 10 @command{dtksh} and the UnixWare 7.1.1 Posix shell, which are
11007 derived from Korn shell version M-12/28/93d, mishandle braced variable
11008 expansion that crosses a 1024- or 4096-byte buffer boundary
11009 within a here-document.  Only the part of the variable name after the boundary
11010 is used.  For example, @code{$@{variable@}} could be replaced by the expansion
11011 of @code{$@{ble@}}.  If the end of the variable name is aligned with the block
11012 boundary, the shell reports an error, as if you used @code{$@{@}}.
11013 Instead of @code{$@{variable-default@}}, the shell may expand
11014 @code{$@{riable-default@}}, or even @code{$@{fault@}}.  This bug can often
11015 be worked around by omitting the braces: @code{$variable}.  The bug was fixed in
11016 @samp{ksh93g} (1998-04-30) but as of 2006 many operating systems were
11017 still shipping older versions with the bug.
11019 Many older shells (including the Bourne shell) implement here-documents
11020 inefficiently.  In particular, some shells can be extremely inefficient when
11021 a single statement contains many here-documents.  For instance if your
11022 @file{configure.ac} includes something like:
11024 @example
11025 @group
11026 if <cross_compiling>; then
11027   assume this and that
11028 else
11029   check this
11030   check that
11031   check something else
11032   @dots{}
11033   on and on forever
11034   @dots{}
11036 @end group
11037 @end example
11039 A shell parses the whole @code{if}/@code{fi} construct, creating
11040 temporary files for each here-document in it.  Some shells create links
11041 for such here-documents on every @code{fork}, so that the clean-up code
11042 they had installed correctly removes them.  It is creating the links
11043 that can take the shell forever.
11045 Moving the tests out of the @code{if}/@code{fi}, or creating multiple
11046 @code{if}/@code{fi} constructs, would improve the performance
11047 significantly.  Anyway, this kind of construct is not exactly the
11048 typical use of Autoconf.  In fact, it's even not recommended, because M4
11049 macros can't look into shell conditionals, so we may fail to expand a
11050 macro when it was expanded before in a conditional path, and the
11051 condition turned out to be false at runtime, and we end up not
11052 executing the macro at all.
11054 @node File Descriptors
11055 @section File Descriptors
11056 @cindex Descriptors
11057 @cindex File descriptors
11058 @cindex Shell file descriptors
11060 Most shells, if not all (including Bash, Zsh, Ash), output traces on
11061 stderr, even for subshells.  This might result in undesirable content
11062 if you meant to capture the standard-error output of the inner command:
11064 @example
11065 $ @kbd{ash -x -c '(eval "echo foo >&2") 2>stderr'}
11066 $ @kbd{cat stderr}
11067 + eval echo foo >&2
11068 + echo foo
11070 $ @kbd{bash -x -c '(eval "echo foo >&2") 2>stderr'}
11071 $ @kbd{cat stderr}
11072 + eval 'echo foo >&2'
11073 ++ echo foo
11075 $ @kbd{zsh -x -c '(eval "echo foo >&2") 2>stderr'}
11076 @i{# Traces on startup files deleted here.}
11077 $ @kbd{cat stderr}
11078 +zsh:1> eval echo foo >&2
11079 +zsh:1> echo foo
11081 @end example
11083 @noindent
11084 One workaround is to grep out uninteresting lines, hoping not to remove
11085 good ones.
11087 If you intend to redirect both standard error and standard output,
11088 redirect standard output first.  This works better with @acronym{HP-UX},
11089 since its shell mishandles tracing if standard error is redirected
11090 first:
11092 @example
11093 $ @kbd{sh -x -c ': 2>err >out'}
11094 + :
11095 + 2> err $ @kbd{cat err}
11096 1> out
11097 @end example
11099 Don't try to redirect the standard error of a command substitution.  It
11100 must be done @emph{inside} the command substitution.  When running
11101 @samp{: `cd /zorglub` 2>/dev/null} expect the error message to
11102 escape, while @samp{: `cd /zorglub 2>/dev/null`} works properly.
11104 It is worth noting that Zsh (but not Ash nor Bash) makes it possible
11105 in assignments though: @samp{foo=`cd /zorglub` 2>/dev/null}.
11107 Don't redirect the same file descriptor several times, as you are doomed
11108 to failure under Ultrix.
11110 @example
11111 ULTRIX V4.4 (Rev. 69) System #31: Thu Aug 10 19:42:23 GMT 1995
11112 UWS V4.4 (Rev. 11)
11113 $ @kbd{eval 'echo matter >fullness' >void}
11114 illegal io
11115 $ @kbd{eval '(echo matter >fullness)' >void}
11116 illegal io
11117 $ @kbd{(eval '(echo matter >fullness)') >void}
11118 Ambiguous output redirect.
11119 @end example
11121 @noindent
11122 In each case the expected result is of course @file{fullness} containing
11123 @samp{matter} and @file{void} being empty.
11125 Don't rely on file descriptors 0, 1, and 2 remaining closed in a
11126 subsidiary program.  If any of these descriptors is closed, the
11127 operating system may open an unspecified file for the descriptor in the
11128 new process image.  Posix says this may be done only if the subsidiary
11129 program is set-user-ID or set-group-ID, but @acronym{HP-UX} 11.23 does it even for
11130 ordinary programs.
11132 Don't rely on open file descriptors being open in child processes.  In
11133 @command{ksh}, file descriptors above 2 which are opened using
11134 @samp{exec @var{n}>file} are closed by a subsequent @samp{exec} (such as
11135 that involved in the fork-and-exec which runs a program or script).
11136 Thus, using @command{sh}, we have:
11138 @example
11139 $ @kbd{cat ./descrips}
11140 #!/bin/sh -
11141 echo hello >&5
11142 $ @kbd{exec 5>t}
11143 $ @kbd{./descrips}
11144 $ @kbd{cat t}
11146 hello
11147 @end example
11149 @noindent
11150 But using ksh:
11152 @example
11153 $ @kbd{exec 5>t}
11154 $ @kbd{./descrips}
11155 hello
11156 $ @kbd{cat t}
11158 @end example
11160 @noindent
11161 Within the process which runs the @samp{descrips} script, file
11162 descriptor 5 is closed.
11164 @acronym{DOS} variants cannot rename or remove open files, such as in
11165 @samp{mv foo bar >foo} or @samp{rm foo >foo}, even though this is
11166 perfectly portable among Posix hosts.
11168 A few ancient systems reserved some file descriptors.  By convention,
11169 file descriptor 3 was opened to @file{/dev/tty} when you logged into
11170 Eighth Edition (1985) through Tenth Edition Unix (1989).  File
11171 descriptor 4 had a special use on the Stardent/Kubota Titan (circa
11172 1990), though we don't now remember what it was.  Both these systems are
11173 obsolete, so it's now safe to treat file descriptors 3 and 4 like any
11174 other file descriptors.
11176 @node File System Conventions
11177 @section File System Conventions
11178 @cindex File system conventions
11180 Autoconf uses shell-script processing extensively, so the file names
11181 that it processes should not contain characters that are special to the
11182 shell.  Special characters include space, tab, newline, @sc{nul}, and
11183 the following:
11185 @example
11186 " # $ & ' ( ) * ; < = > ? [ \ ` |
11187 @end example
11189 Also, file names should not begin with @samp{~} or @samp{-}, and should
11190 contain neither @samp{-} immediately after @samp{/} nor @samp{~}
11191 immediately after @samp{:}.  On Posix-like platforms, directory names
11192 should not contain @samp{:}, as this runs afoul of @samp{:} used as the
11193 path separator.
11195 These restrictions apply not only to the files that you distribute, but
11196 also to the absolute file names of your source, build, and destination
11197 directories.
11199 On some Posix-like platforms, @samp{!} and @samp{^} are special too, so
11200 they should be avoided.
11202 Posix lets implementations treat leading @file{//} specially, but
11203 requires leading @file{///} and beyond to be equivalent to @file{/}.
11204 Most Unix variants treat @file{//} like @file{/}.  However, some treat
11205 @file{//} as a ``super-root'' that can provide access to files that are
11206 not otherwise reachable from @file{/}.  The super-root tradition began
11207 with Apollo Domain/OS, which died out long ago, but unfortunately Cygwin
11208 has revived it.
11210 While @command{autoconf} and friends are usually run on some Posix
11211 variety, they can be used on other systems, most notably @acronym{DOS}
11212 variants.  This impacts several assumptions regarding file names.
11214 @noindent
11215 For example, the following code:
11217 @example
11218 case $foo_dir in
11219   /*) # Absolute
11220      ;;
11221   *)
11222      foo_dir=$dots$foo_dir ;;
11223 esac
11224 @end example
11226 @noindent
11227 fails to properly detect absolute file names on those systems, because
11228 they can use a drivespec, and usually use a backslash as directory
11229 separator.  If you want to be portable to @acronym{DOS} variants (at the
11230 price of rejecting valid but oddball Posix file names like @file{a:\b}),
11231 you can check for absolute file names like this:
11233 @example
11234 case $foo_dir in
11235   [\\/]* | ?:[\\/]* ) # Absolute
11236      ;;
11237   *)
11238      foo_dir=$dots$foo_dir ;;
11239 esac
11240 @end example
11242 @noindent
11243 Make sure you quote the brackets if appropriate and keep the backslash as
11244 first character (@pxref{Limitations of Builtins}).
11246 Also, because the colon is used as part of a drivespec, these systems don't
11247 use it as path separator.  When creating or accessing paths, you can use the
11248 @code{PATH_SEPARATOR} output variable instead.  @command{configure} sets this
11249 to the appropriate value (@samp{:} or @samp{;}) when it starts up.
11251 File names need extra care as well.  While @acronym{DOS} variants
11252 that are Posixy enough to run @command{autoconf} (such as @acronym{DJGPP})
11253 are usually able to handle long file names properly, there are still
11254 limitations that can seriously break packages.  Several of these issues
11255 can be easily detected by the
11256 @uref{ftp://ftp.gnu.org/gnu/non-gnu/doschk/doschk-1.1.tar.gz, doschk}
11257 package.
11259 A short overview follows; problems are marked with @sc{sfn}/@sc{lfn} to
11260 indicate where they apply: @sc{sfn} means the issues are only relevant to
11261 plain @acronym{DOS}, not to @acronym{DOS} under Microsoft Windows
11262 variants, while @sc{lfn} identifies problems that exist even under
11263 Microsoft Windows variants.
11265 @table @asis
11266 @item No multiple dots (@sc{sfn})
11267 @acronym{DOS} cannot handle multiple dots in file names.  This is an especially
11268 important thing to remember when building a portable configure script,
11269 as @command{autoconf} uses a .in suffix for template files.
11271 This is perfectly OK on Posix variants:
11273 @example
11274 AC_CONFIG_HEADERS([config.h])
11275 AC_CONFIG_FILES([source.c foo.bar])
11276 AC_OUTPUT
11277 @end example
11279 @noindent
11280 but it causes problems on @acronym{DOS}, as it requires @samp{config.h.in},
11281 @samp{source.c.in} and @samp{foo.bar.in}.  To make your package more portable
11282 to @acronym{DOS}-based environments, you should use this instead:
11284 @example
11285 AC_CONFIG_HEADERS([config.h:config.hin])
11286 AC_CONFIG_FILES([source.c:source.cin foo.bar:foobar.in])
11287 AC_OUTPUT
11288 @end example
11290 @item No leading dot (@sc{sfn})
11291 @acronym{DOS} cannot handle file names that start with a dot.  This is usually
11292 not important for @command{autoconf}.
11294 @item Case insensitivity (@sc{lfn})
11295 @acronym{DOS} is case insensitive, so you cannot, for example, have both a
11296 file called @samp{INSTALL} and a directory called @samp{install}.  This
11297 also affects @command{make}; if there's a file called @samp{INSTALL} in
11298 the directory, @samp{make install} does nothing (unless the
11299 @samp{install} target is marked as PHONY).
11301 @item The 8+3 limit (@sc{sfn})
11302 Because the @acronym{DOS} file system only stores the first 8 characters of
11303 the file name and the first 3 of the extension, those must be unique.
11304 That means that @file{foobar-part1.c}, @file{foobar-part2.c} and
11305 @file{foobar-prettybird.c} all resolve to the same file name
11306 (@file{FOOBAR-P.C}).  The same goes for @file{foo.bar} and
11307 @file{foo.bartender}.
11309 The 8+3 limit is not usually a problem under Microsoft Windows, as it
11310 uses numeric
11311 tails in the short version of file names to make them unique.  However, a
11312 registry setting can turn this behavior off.  While this makes it
11313 possible to share file trees containing long file names between @sc{sfn}
11314 and @sc{lfn} environments, it also means the above problem applies there
11315 as well.
11317 @item Invalid characters (@sc{lfn})
11318 Some characters are invalid in @acronym{DOS} file names, and should therefore
11319 be avoided.  In a @sc{lfn} environment, these are @samp{/}, @samp{\},
11320 @samp{?}, @samp{*}, @samp{:}, @samp{<}, @samp{>}, @samp{|} and @samp{"}.
11321 In a @sc{sfn} environment, other characters are also invalid.  These
11322 include @samp{+}, @samp{,}, @samp{[} and @samp{]}.
11324 @item Invalid names (@sc{lfn})
11325 Some @acronym{DOS} file names are reserved, and cause problems if you
11326 try to use files with those names.  These names include @file{CON},
11327 @file{AUX}, @file{COM1}, @file{COM2}, @file{COM3}, @file{COM4},
11328 @file{LPT1}, @file{LPT2}, @file{LPT3}, @file{NUL}, and @file{PRN}.
11329 File names are case insensitive, so even names like
11330 @file{aux/config.guess} are disallowed.
11332 @end table
11334 @node Shell Substitutions
11335 @section Shell Substitutions
11336 @cindex Shell substitutions
11338 Contrary to a persistent urban legend, the Bourne shell does not
11339 systematically split variables and back-quoted expressions, in particular
11340 on the right-hand side of assignments and in the argument of @code{case}.
11341 For instance, the following code:
11343 @example
11344 case "$given_srcdir" in
11345 .)  top_srcdir="`echo "$dots" | sed 's,/$,,'`" ;;
11346 *)  top_srcdir="$dots$given_srcdir" ;;
11347 esac
11348 @end example
11350 @noindent
11351 is more readable when written as:
11353 @example
11354 case $given_srcdir in
11355 .)  top_srcdir=`echo "$dots" | sed 's,/$,,'` ;;
11356 *)  top_srcdir=$dots$given_srcdir ;;
11357 esac
11358 @end example
11360 @noindent
11361 and in fact it is even @emph{more} portable: in the first case of the
11362 first attempt, the computation of @code{top_srcdir} is not portable,
11363 since not all shells properly understand @code{"`@dots{}"@dots{}"@dots{}`"}.
11364 Worse yet, not all shells understand @code{"`@dots{}\"@dots{}\"@dots{}`"}
11365 the same way.  There is just no portable way to use double-quoted
11366 strings inside double-quoted back-quoted expressions (pfew!).
11368 @table @code
11369 @item $@@
11370 @cindex @samp{"$@@"}
11371 One of the most famous shell-portability issues is related to
11372 @samp{"$@@"}.  When there are no positional arguments, Posix says
11373 that @samp{"$@@"} is supposed to be equivalent to nothing, but the
11374 original Unix version 7 Bourne shell treated it as equivalent to
11375 @samp{""} instead, and this behavior survives in later implementations
11376 like Digital Unix 5.0.
11378 The traditional way to work around this portability problem is to use
11379 @samp{$@{1+"$@@"@}}.  Unfortunately this method does not work with
11380 Zsh (3.x and 4.x), which is used on Mac OS X@.  When emulating
11381 the Bourne shell, Zsh performs word splitting on @samp{$@{1+"$@@"@}}:
11383 @example
11384 zsh $ @kbd{emulate sh}
11385 zsh $ @kbd{for i in "$@@"; do echo $i; done}
11386 Hello World
11388 zsh $ @kbd{for i in $@{1+"$@@"@}; do echo $i; done}
11389 Hello
11390 World
11392 @end example
11394 @noindent
11395 Zsh handles plain @samp{"$@@"} properly, but we can't use plain
11396 @samp{"$@@"} because of the portability problems mentioned above.
11397 One workaround relies on Zsh's ``global aliases'' to convert
11398 @samp{$@{1+"$@@"@}} into @samp{"$@@"} by itself:
11400 @example
11401 test "$@{ZSH_VERSION+set@}" = set && alias -g '$@{1+"$@@"@}'='"$@@"'
11402 @end example
11404 A more conservative workaround is to avoid @samp{"$@@"} if it is
11405 possible that there may be no positional arguments.  For example,
11406 instead of:
11408 @example
11409 cat conftest.c "$@@"
11410 @end example
11412 you can use this instead:
11414 @example
11415 case $# in
11416 0) cat conftest.c;;
11417 *) cat conftest.c "$@@";;
11418 esac
11419 @end example
11421 Autoconf macros often use the @command{set} command to update
11422 @samp{$@@}, so if you are writing shell code intended for
11423 @command{configure} you should not assume that the value of @samp{$@@}
11424 persists for any length of time.
11427 @item $@{10@}
11428 @cindex positional parameters
11429 The 10th, 11th, @dots{} positional parameters can be accessed only after
11430 a @code{shift}.  The 7th Edition shell reported an error if given
11431 @code{$@{10@}}, and
11432 Solaris 10 @command{/bin/sh} still acts that way:
11434 @example
11435 $ @kbd{set 1 2 3 4 5 6 7 8 9 10}
11436 $ @kbd{echo $@{10@}}
11437 bad substitution
11438 @end example
11440 @item $@{@var{var}:-@var{value}@}
11441 @c Info cannot handle `:' in index entries.
11442 @c @cindex $@{@var{var}:-@var{value}@}
11443 Old @acronym{BSD} shells, including the Ultrix @code{sh}, don't accept the
11444 colon for any shell substitution, and complain and die.
11446 @item $@{@var{var}=@var{literal}@}
11447 @cindex $@{@var{var}=@var{literal}@}
11448 Be sure to quote:
11450 @example
11451 : $@{var='Some words'@}
11452 @end example
11454 @noindent
11455 otherwise some shells, such as on Digital Unix V 5.0, die because
11456 of a ``bad substitution''.
11458 @sp 1
11460 Solaris @command{/bin/sh} has a frightening bug in its interpretation
11461 of this.  Imagine you need set a variable to a string containing
11462 @samp{@}}.  This @samp{@}} character confuses Solaris @command{/bin/sh}
11463 when the affected variable was already set.  This bug can be exercised
11464 by running:
11466 @example
11467 $ @kbd{unset foo}
11468 $ @kbd{foo=$@{foo='@}'@}}
11469 $ @kbd{echo $foo}
11471 $ @kbd{foo=$@{foo='@}'   # no error; this hints to what the bug is}
11472 $ @kbd{echo $foo}
11474 $ @kbd{foo=$@{foo='@}'@}}
11475 $ @kbd{echo $foo}
11476 @}@}
11477  ^ ugh!
11478 @end example
11480 It seems that @samp{@}} is interpreted as matching @samp{$@{}, even
11481 though it is enclosed in single quotes.  The problem doesn't happen
11482 using double quotes.
11484 @item $@{@var{var}=@var{expanded-value}@}
11485 @cindex $@{@var{var}=@var{expanded-value}@}
11486 On Ultrix,
11487 running
11489 @example
11490 default="yu,yaa"
11491 : $@{var="$default"@}
11492 @end example
11494 @noindent
11495 sets @var{var} to @samp{M-yM-uM-,M-yM-aM-a}, i.e., the 8th bit of
11496 each char is set.  You don't observe the phenomenon using a simple
11497 @samp{echo $var} since apparently the shell resets the 8th bit when it
11498 expands $var.  Here are two means to make this shell confess its sins:
11500 @example
11501 $ @kbd{cat -v <<EOF
11502 $var
11503 EOF}
11504 @end example
11506 @noindent
11509 @example
11510 $ @kbd{set | grep '^var=' | cat -v}
11511 @end example
11513 One classic incarnation of this bug is:
11515 @example
11516 default="a b c"
11517 : $@{list="$default"@}
11518 for c in $list; do
11519   echo $c
11520 done
11521 @end example
11523 @noindent
11524 You'll get @samp{a b c} on a single line.  Why?  Because there are no
11525 spaces in @samp{$list}: there are @samp{M- }, i.e., spaces with the 8th
11526 bit set, hence no IFS splitting is performed!!!
11528 One piece of good news is that Ultrix works fine with @samp{:
11529 $@{list=$default@}}; i.e., if you @emph{don't} quote.  The bad news is
11530 then that @acronym{QNX} 4.25 then sets @var{list} to the @emph{last} item of
11531 @var{default}!
11533 The portable way out consists in using a double assignment, to switch
11534 the 8th bit twice on Ultrix:
11536 @example
11537 list=$@{list="$default"@}
11538 @end example
11540 @noindent
11541 @dots{}but beware of the @samp{@}} bug from Solaris (see above).  For safety,
11542 use:
11544 @example
11545 test "$@{var+set@}" = set || var=@var{@{value@}}
11546 @end example
11549 @item `@var{commands}`
11550 @cindex `@var{commands}`
11551 @cindex Command Substitution
11552 Posix requires shells to trim all trailing newlines from command
11553 output before substituting it, so assignments like
11554 @samp{dir=`echo "$file" | tr a A`} do not work as expected if
11555 @samp{$file} ends in a newline.
11557 While in general it makes no sense, do not substitute a single builtin
11558 with side effects, because Ash 0.2, trying to optimize, does not fork a
11559 subshell to perform the command.
11561 For instance, if you wanted to check that @command{cd} is silent, do not
11562 use @samp{test -z "`cd /`"} because the following can happen:
11564 @example
11565 $ @kbd{pwd}
11566 /tmp
11567 $ @kbd{test -z "`cd /`" && pwd}
11569 @end example
11571 @noindent
11572 The result of @samp{foo=`exit 1`} is left as an exercise to the reader.
11574 The MSYS shell leaves a stray byte in the expansion of a double-quoted
11575 command substitution of a native program, if the end of the substution
11576 is not aligned with the end of the double quote.  This may be worked
11577 around by inserting another pair of quotes:
11579 @example
11580 $ @kbd{echo "`printf 'foo\r\n'` bar" > broken}
11581 $ @kbd{echo "`printf 'foo\r\n'`"" bar" | cmp - broken}
11582 - broken differ: char 4, line 1
11583 @end example
11586 @item $(@var{commands})
11587 @cindex $(@var{commands})
11588 This construct is meant to replace @samp{`@var{commands}`},
11589 and it has most of the problems listed under @code{`@var{commands}`}.
11591 This construct can be
11592 nested while this is impossible to do portably with back quotes.
11593 Unfortunately it is not yet universally supported.  Most notably, even recent
11594 releases of Solaris don't support it:
11596 @example
11597 $ @kbd{showrev -c /bin/sh | grep version}
11598 Command version: SunOS 5.10 Generic 121004-01 Oct 2005
11599 $ @kbd{echo $(echo blah)}
11600 syntax error: `(' unexpected
11601 @end example
11603 @noindent
11604 nor does @sc{irix} 6.5's Bourne shell:
11605 @example
11606 $ @kbd{uname -a}
11607 IRIX firebird-image 6.5 07151432 IP22
11608 $ @kbd{echo $(echo blah)}
11609 $(echo blah)
11610 @end example
11612 If you do use @samp{$(@var{commands})}, make sure that the commands
11613 do not start with a parenthesis, as that would cause confusion with
11614 a different notation @samp{$((@var{expression}))} that in modern
11615 shells is an arithmetic expression not a command.  To avoid the
11616 confusion, insert a space between the two opening parentheses.
11618 Avoid @var{commands} that contain unbalanced parentheses in
11619 here-documents, comments, or case statement patterns, as many shells
11620 mishandle them.  For example, Bash 3.1, @samp{ksh88}, @command{pdksh}
11621 5.2.14, and Zsh 4.2.6 all mishandle the following valid command:
11623 @example
11624 echo $(case x in x) echo hello;; esac)
11625 @end example
11627 @item ^
11628 @cindex ^ quoting
11629 Always quote @samp{^}, otherwise traditional shells such as
11630 @command{/bin/sh} on Solaris 10 treat this like @samp{|}.
11632 @end table
11635 @node Assignments
11636 @section Assignments
11637 @cindex Shell assignments
11639 When setting several variables in a row, be aware that the order of the
11640 evaluation is undefined.  For instance @samp{foo=1 foo=2; echo $foo}
11641 gives @samp{1} with Solaris @command{/bin/sh}, but @samp{2} with Bash.
11642 You must use
11643 @samp{;} to enforce the order: @samp{foo=1; foo=2; echo $foo}.
11645 Don't rely on the following to find @file{subdir/program}:
11647 @example
11648 PATH=subdir$PATH_SEPARATOR$PATH program
11649 @end example
11651 @noindent
11652 as this does not work with Zsh 3.0.6.  Use something like this
11653 instead:
11655 @example
11656 (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)
11657 @end example
11659 Don't rely on the exit status of an assignment: Ash 0.2 does not change
11660 the status and propagates that of the last statement:
11662 @example
11663 $ @kbd{false || foo=bar; echo $?}
11665 $ @kbd{false || foo=`:`; echo $?}
11667 @end example
11669 @noindent
11670 and to make things even worse, @acronym{QNX} 4.25 just sets the exit status
11671 to 0 in any case:
11673 @example
11674 $ @kbd{foo=`exit 1`; echo $?}
11676 @end example
11678 To assign default values, follow this algorithm:
11680 @enumerate
11681 @item
11682 If the default value is a literal and does not contain any closing
11683 brace, use:
11685 @example
11686 : $@{var='my literal'@}
11687 @end example
11689 @item
11690 If the default value contains no closing brace, has to be expanded, and
11691 the variable being initialized is not intended to be IFS-split
11692 (i.e., it's not a list), then use:
11694 @example
11695 : $@{var="$default"@}
11696 @end example
11698 @item
11699 If the default value contains no closing brace, has to be expanded, and
11700 the variable being initialized is intended to be IFS-split (i.e., it's a list),
11701 then use:
11703 @example
11704 var=$@{var="$default"@}
11705 @end example
11707 @item
11708 If the default value contains a closing brace, then use:
11710 @example
11711 test "$@{var+set@}" = set || var="has a '@}'"
11712 @end example
11713 @end enumerate
11715 In most cases @samp{var=$@{var="$default"@}} is fine, but in case of
11716 doubt, just use the last form.  @xref{Shell Substitutions}, items
11717 @samp{$@{@var{var}:-@var{value}@}} and @samp{$@{@var{var}=@var{value}@}}
11718 for the rationale.
11720 @node Parentheses
11721 @section Parentheses in Shell Scripts
11722 @cindex Shell parentheses
11724 Beware of two opening parentheses in a row, as some shell
11725 implementations mishandle them.  For example, @samp{pdksh} 5.2.14
11726 misparses the following code:
11728 @example
11729 if ((true) || false); then
11730   echo ok
11732 @end example
11734 @noindent
11735 To work around this problem, insert a space between the two opening
11736 parentheses.  There is a similar problem and workaround with
11737 @samp{$((}; see @ref{Shell Substitutions}.
11739 Posix requires support for @code{case} patterns with opening
11740 parentheses like this:
11742 @example
11743 case $file_name in
11744 (*.c) echo "C source code";;
11745 esac
11746 @end example
11748 @noindent
11749 but the @code{(} in this example is not portable to many older Bourne
11750 shell implementations.  It can be omitted safely.
11752 @node Slashes
11753 @section Slashes in Shell Scripts
11754 @cindex Shell slashes
11756 Unpatched Tru64 5.1 @command{sh} omits the last slash of command-line
11757 arguments that contain two trailing slashes:
11759 @example
11760 $ @kbd{echo / // /// //// .// //.}
11761 / / // /// ./ //.
11762 $ @kbd{x=//}
11763 $ @kbd{eval "echo \$x"}
11765 $ @kbd{set -x}
11766 $ @kbd{echo abc | tr -t ab //}
11767 + echo abc
11768 + tr -t ab /
11770 @end example
11772 However, our understanding is that patches are available, so perhaps
11773 it's not worth worrying about working around this horrendous bug.
11775 @node Special Shell Variables
11776 @section Special Shell Variables
11777 @cindex Shell variables
11778 @cindex Special shell variables
11780 Some shell variables should not be used, since they can have a deep
11781 influence on the behavior of the shell.  In order to recover a sane
11782 behavior from the shell, some variables should be unset, but
11783 @command{unset} is not portable (@pxref{Limitations of Builtins}) and a
11784 fallback value is needed.
11786 As a general rule, shell variable names containing a lower-case letter
11787 are safe; you can define and use these variables without worrying about
11788 their effect on the underlying system, and without worrying about
11789 whether the shell changes them unexpectedly.  (The exception is the
11790 shell variable @code{status}, as described below.)
11792 Here is a list of names that are known to cause trouble.  This list is
11793 not exhaustive, but you should be safe if you avoid the name
11794 @code{status} and names containing only upper-case letters and
11795 underscores.
11797 @c Alphabetical order, case insensitive, `A' before `a'.
11798 @table @code
11799 @item _
11800 Many shells reserve @samp{$_} for various purposes, e.g., the name of
11801 the last command executed.
11803 @item BIN_SH
11804 @evindex BIN_SH
11805 In Tru64, if @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
11806 the standard shell conform to Posix.
11807 Autoconf-generated scripts export this variable when they start up.
11809 @item CDPATH
11810 @evindex CDPATH
11811 When this variable is set it specifies a list of directories to search
11812 when invoking @code{cd} with a relative file name that did not start
11813 with @samp{./} or @samp{../}.  Posix
11814 1003.1-2001 says that if a nonempty directory name from @env{CDPATH}
11815 is used successfully, @code{cd} prints the resulting absolute
11816 file name.  Unfortunately this output can break idioms like
11817 @samp{abs=`cd src && pwd`} because @code{abs} receives the name twice.
11818 Also, many shells do not conform to this part of Posix; for
11819 example, @command{zsh} prints the result only if a directory name
11820 other than @file{.} was chosen from @env{CDPATH}.
11822 In practice the shells that have this problem also support
11823 @command{unset}, so you can work around the problem as follows:
11825 @example
11826 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
11827 @end example
11829 You can also avoid output by ensuring that your directory name is
11830 absolute or anchored at @samp{./}, as in @samp{abs=`cd ./src && pwd`}.
11832 Autoconf-generated scripts automatically unset @env{CDPATH} if
11833 possible, so you need not worry about this problem in those scripts.
11835 @item DUALCASE
11836 @evindex DUALCASE
11837 In the MKS shell, case statements and file name generation are
11838 case-insensitive unless @env{DUALCASE} is nonzero.
11839 Autoconf-generated scripts export this variable when they start up.
11841 @item ENV
11842 @itemx MAIL
11843 @itemx MAILPATH
11844 @itemx PS1
11845 @itemx PS2
11846 @itemx PS4
11847 @evindex ENV
11848 @evindex MAIL
11849 @evindex MAILPATH
11850 @evindex PS1
11851 @evindex PS2
11852 @evindex PS4
11853 These variables should not matter for shell scripts, since they are
11854 supposed to affect only interactive shells.  However, at least one
11855 shell (the pre-3.0 @sc{uwin} Korn shell) gets confused about
11856 whether it is interactive, which means that (for example) a @env{PS1}
11857 with a side effect can unexpectedly modify @samp{$?}.  To work around
11858 this bug, Autoconf-generated scripts do something like this:
11860 @example
11861 (unset ENV) >/dev/null 2>&1 && unset ENV MAIL MAILPATH
11862 PS1='$ '
11863 PS2='> '
11864 PS4='+ '
11865 @end example
11867 @item IFS
11868 @evindex IFS
11869 Long ago, shell scripts inherited @env{IFS} from the environment,
11870 but this caused many problems so modern shells ignore any environment
11871 settings for @env{IFS}.
11873 Don't set the first character of @code{IFS} to backslash.  Indeed,
11874 Bourne shells use the first character (backslash) when joining the
11875 components in @samp{"$@@"} and some shells then reinterpret (!)@: the
11876 backslash escapes, so you can end up with backspace and other strange
11877 characters.
11879 The proper value for @code{IFS} (in regular code, not when performing
11880 splits) is @samp{@key{SPC}@key{TAB}@key{RET}}.  The first character is
11881 especially important, as it is used to join the arguments in @samp{$*};
11882 however, note that traditional shells, but also bash-2.04, fail to adhere
11883 to this and join with a space anyway.
11885 @item LANG
11886 @itemx LC_ALL
11887 @itemx LC_COLLATE
11888 @itemx LC_CTYPE
11889 @itemx LC_MESSAGES
11890 @itemx LC_MONETARY
11891 @itemx LC_NUMERIC
11892 @itemx LC_TIME
11893 @evindex LANG
11894 @evindex LC_ALL
11895 @evindex LC_COLLATE
11896 @evindex LC_CTYPE
11897 @evindex LC_MESSAGES
11898 @evindex LC_MONETARY
11899 @evindex LC_NUMERIC
11900 @evindex LC_TIME
11902 Autoconf-generated scripts normally set all these variables to
11903 @samp{C} because so much configuration code assumes the C locale and
11904 Posix requires that locale environment variables be set to
11905 @samp{C} if the C locale is desired.  However, some older, nonstandard
11906 systems (notably @acronym{SCO}) break if locale environment variables
11907 are set to @samp{C}, so when running on these systems
11908 Autoconf-generated scripts unset the variables instead.
11910 @item LANGUAGE
11911 @evindex LANGUAGE
11913 @env{LANGUAGE} is not specified by Posix, but it is a @acronym{GNU}
11914 extension that overrides @env{LC_ALL} in some cases, so
11915 Autoconf-generated scripts set it too.
11917 @item LC_ADDRESS
11918 @itemx LC_IDENTIFICATION
11919 @itemx LC_MEASUREMENT
11920 @itemx LC_NAME
11921 @itemx LC_PAPER
11922 @itemx LC_TELEPHONE
11923 @evindex LC_ADDRESS
11924 @evindex LC_IDENTIFICATION
11925 @evindex LC_MEASUREMENT
11926 @evindex LC_NAME
11927 @evindex LC_PAPER
11928 @evindex LC_TELEPHONE
11930 These locale environment variables are @acronym{GNU} extensions.  They
11931 are treated like their Posix brethren (@env{LC_COLLATE},
11932 etc.)@: as described above.
11934 @item LINENO
11935 Most modern shells provide the current line number in @code{LINENO}.
11936 Its value is the line number of the beginning of the current command.
11937 Autoconf attempts to execute @command{configure} with a shell that
11938 supports @code{LINENO}.
11939 If no such shell is available, it attempts to implement @code{LINENO}
11940 with a Sed prepass that replaces each instance of the string
11941 @code{$LINENO} (not followed by an alphanumeric character) with the
11942 line's number.
11944 You should not rely on @code{LINENO} within @command{eval}, as the
11945 behavior differs in practice.  Also, the possibility of the Sed
11946 prepass means that you should not rely on @code{$LINENO} when quoted,
11947 when in here-documents, or when in long commands that cross line
11948 boundaries.  Subshells should be OK, though.  In the following
11949 example, lines 1, 6, and 9 are portable, but the other instances of
11950 @code{LINENO} are not:
11952 @example
11953 @group
11954 $ @kbd{cat lineno}
11955 echo 1. $LINENO
11956 cat <<EOF
11957 3. $LINENO
11958 4. $LINENO
11960 ( echo 6. $LINENO )
11961 eval 'echo 7. $LINENO'
11962 echo 8. '$LINENO'
11963 echo 9. $LINENO '
11964 10.' $LINENO
11965 @end group
11966 @group
11967 $ @kbd{bash-2.05 lineno}
11968 1. 1
11969 3. 2
11970 4. 2
11971 6. 6
11972 7. 1
11973 8. $LINENO
11974 9. 9
11975 10. 9
11976 @end group
11977 @group
11978 $ @kbd{zsh-3.0.6 lineno}
11979 1. 1
11980 3. 2
11981 4. 2
11982 6. 6
11983 7. 7
11984 8. $LINENO
11985 9. 9
11986 10. 9
11987 @end group
11988 @group
11989 $ @kbd{pdksh-5.2.14 lineno}
11990 1. 1
11991 3. 2
11992 4. 2
11993 6. 6
11994 7. 0
11995 8. $LINENO
11996 9. 9
11997 10. 9
11998 @end group
11999 @group
12000 $ @kbd{sed '=' <lineno |}
12001 > @kbd{  sed '}
12002 > @kbd{    N}
12003 > @kbd{    s,$,-,}
12004 > @kbd{    t loop}
12005 > @kbd{    :loop}
12006 > @kbd{    s,^\([0-9]*\)\(.*\)[$]LINENO\([^a-zA-Z0-9_]\),\1\2\1\3,}
12007 > @kbd{    t loop}
12008 > @kbd{    s,-$,,}
12009 > @kbd{    s,^[0-9]*\n,,}
12010 > @kbd{  ' |}
12011 > @kbd{  sh}
12012 1. 1
12013 3. 3
12014 4. 4
12015 6. 6
12016 7. 7
12017 8. 8
12018 9. 9
12019 10. 10
12020 @end group
12021 @end example
12023 @item NULLCMD
12024 @evindex NULLCMD
12025 When executing the command @samp{>foo}, @command{zsh} executes
12026 @samp{$NULLCMD >foo} unless it is operating in Bourne shell
12027 compatibility mode and the @command{zsh} version is newer
12028 than 3.1.6-dev-18.  If you are using an older @command{zsh}
12029 and forget to set @env{NULLCMD},
12030 your script might be suspended waiting for data on its standard input.
12032 @item PATH_SEPARATOR
12033 @evindex PATH_SEPARATOR
12034 On @acronym{DJGPP} systems, the @env{PATH_SEPARATOR} environment
12035 variable can be set to either @samp{:} or @samp{;} to control the path
12036 separator Bash uses to set up certain environment variables (such as
12037 @env{PATH}).  You can set this variable to @samp{;} if you want
12038 @command{configure} to use @samp{;} as a separator; this might be useful
12039 if you plan to use non-Posix shells to execute files.  @xref{File System
12040 Conventions}, for more information about @code{PATH_SEPARATOR}.
12042 @item PWD
12043 @evindex PWD
12044 Posix 1003.1-2001 requires that @command{cd} and
12045 @command{pwd} must update the @env{PWD} environment variable to point
12046 to the logical name of the current directory, but traditional shells
12047 do not support this.  This can cause confusion if one shell instance
12048 maintains @env{PWD} but a subsidiary and different shell does not know
12049 about @env{PWD} and executes @command{cd}; in this case @env{PWD}
12050 points to the wrong directory.  Use @samp{`pwd`} rather than
12051 @samp{$PWD}.
12053 @item RANDOM
12054 Many shells provide @code{RANDOM}, a variable that returns a different
12055 integer each time it is used.  Most of the time, its value does not
12056 change when it is not used, but on @sc{irix} 6.5 the value changes all
12057 the time.  This can be observed by using @command{set}.  It is common
12058 practice to use @code{$RANDOM} as part of a file name, but code
12059 shouldn't rely on @code{$RANDOM} expanding to a nonempty string.
12061 @item status
12062 This variable is an alias to @samp{$?} for @code{zsh} (at least 3.1.6),
12063 hence read-only.  Do not use it.
12064 @end table
12066 @node Limitations of Builtins
12067 @section Limitations of Shell Builtins
12068 @cindex Shell builtins
12069 @cindex Limitations of shell builtins
12071 No, no, we are serious: some shells do have limitations!  :)
12073 You should always keep in mind that any builtin or command may support
12074 options, and therefore differ in behavior with arguments
12075 starting with a dash.  For instance, the innocent @samp{echo "$word"}
12076 can give unexpected results when @code{word} starts with a dash.  It is
12077 often possible to avoid this problem using @samp{echo "x$word"}, taking
12078 the @samp{x} into account later in the pipe.
12080 @table @asis
12081 @item @command{.}
12082 @prindex @command{.}
12083 Use @command{.} only with regular files (use @samp{test -f}).  Bash
12084 2.03, for instance, chokes on @samp{. /dev/null}.  Also, remember that
12085 @command{.} uses @env{PATH} if its argument contains no slashes, so if
12086 you want to use @command{.} on a file @file{foo} in the current
12087 directory, you must use @samp{. ./foo}.
12089 @item @command{!}
12090 @prindex @command{!}
12091 The Unix version 7 shell did not support
12092 negating the exit status of commands with @command{!}, and this feature
12093 is still absent from some shells (e.g., Solaris @command{/bin/sh}).
12094 Shell code like this:
12096 @example
12097 if ! cmp file1 file2 >/dev/null 2>&1; then
12098   echo files differ or trouble
12100 @end example
12102 is therefore not portable in practice.  Typically it is easy to rewrite
12103 such code, e.g.:
12105 @example
12106 cmp file1 file2 >/dev/null 2>&1 ||
12107   echo files differ or trouble
12108 @end example
12110 More generally, one can always rewrite @samp{! @var{command}} as:
12112 @example
12113 if @var{command}; then (exit 1); else :; fi
12114 @end example
12116 @item @command{break}
12117 @c ------------------
12118 @prindex @command{break}
12119 The use of @samp{break 2} etc.@: is safe.
12122 @item @command{case}
12123 @c -----------------
12124 @prindex @command{case}
12125 You don't need to quote the argument; no splitting is performed.
12127 You don't need the final @samp{;;}, but you should use it.
12129 Because of a bug in its @code{fnmatch}, Bash fails to properly
12130 handle backslashes in character classes:
12132 @example
12133 bash-2.02$ @kbd{case /tmp in [/\\]*) echo OK;; esac}
12134 bash-2.02$
12135 @end example
12137 @noindent
12138 This is extremely unfortunate, since you are likely to use this code to
12139 handle Posix or @sc{ms-dos} absolute file names.  To work around this
12140 bug, always put the backslash first:
12142 @example
12143 bash-2.02$ @kbd{case '\TMP' in [\\/]*) echo OK;; esac}
12145 bash-2.02$ @kbd{case /tmp in [\\/]*) echo OK;; esac}
12147 @end example
12149 Many Bourne shells cannot handle closing brackets in character classes
12150 correctly.
12152 Some shells also have problems with backslash escaping in case you do not want
12153 to match the backslash: both a backslash and the escaped character match this
12154 pattern.  To work around this, specify the character class in a variable, so
12155 that quote removal does not apply afterwards, and the special characters don't
12156 have to be backslash-escaped:
12158 @example
12159 $ @kbd{case '\' in [\<]) echo OK;; esac}
12161 $ @kbd{scanset='[<]'; case '\' in $scanset) echo OK;; esac}
12163 @end example
12165 Even with this, Solaris @command{ksh} matches a backslash if the set
12166 contains any
12167 of the characters @samp{|}, @samp{&}, @samp{(}, or @samp{)}.
12169 Conversely, Tru64 @command{ksh} (circa 2003) erroneously always matches
12170 a closing parenthesis if not specified in a character class:
12172 @example
12173 $ @kbd{case foo in *\)*) echo fail ;; esac}
12174 fail
12175 $ @kbd{case foo in *')'*) echo fail ;; esac}
12176 fail
12177 @end example
12179 Some shells, such as Ash 0.3.8, are confused by an empty
12180 @code{case}/@code{esac}:
12182 @example
12183 ash-0.3.8 $ @kbd{case foo in esac;}
12184 @error{}Syntax error: ";" unexpected (expecting ")")
12185 @end example
12187 Many shells still do not support parenthesized cases, which is a pity
12188 for those of us using tools that rely on balanced parentheses.  For
12189 instance, Solaris @command{/bin/sh}:
12191 @example
12192 $ @kbd{case foo in (foo) echo foo;; esac}
12193 @error{}syntax error: `(' unexpected
12194 @end example
12197 @item @command{cd}
12198 @c ---------------
12199 @prindex @command{cd}
12200 Posix 1003.1-2001 requires that @command{cd} must support
12201 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
12202 with @option{-L} being the default.  However, traditional shells do
12203 not support these options, and their @command{cd} command has the
12204 @option{-P} behavior.
12206 Portable scripts should assume neither option is supported, and should
12207 assume neither behavior is the default.  This can be a bit tricky,
12208 since the Posix default behavior means that, for example,
12209 @samp{ls ..} and @samp{cd ..} may refer to different directories if
12210 the current logical directory is a symbolic link.  It is safe to use
12211 @command{cd @var{dir}} if @var{dir} contains no @file{..} components.
12212 Also, Autoconf-generated scripts check for this problem when computing
12213 variables like @code{ac_top_srcdir} (@pxref{Configuration Actions}),
12214 so it is safe to @command{cd} to these variables.
12216 See @xref{Special Shell Variables}, for portability problems involving
12217 @command{cd} and the @env{CDPATH} environment variable.
12218 Also please see the discussion of the @command{pwd} command.
12221 @item @command{echo}
12222 @c -----------------
12223 @prindex @command{echo}
12224 The simple @command{echo} is probably the most surprising source of
12225 portability troubles.  It is not possible to use @samp{echo} portably
12226 unless both options and escape sequences are omitted.  New applications
12227 which are not aiming at portability should use @samp{printf} instead of
12228 @samp{echo}.
12230 Don't expect any option.  @xref{Preset Output Variables}, @code{ECHO_N}
12231 etc.@: for a means to simulate @option{-n}.
12233 Do not use backslashes in the arguments, as there is no consensus on
12234 their handling.  For @samp{echo '\n' | wc -l}, the @command{sh} of
12235 Solaris outputs 2, but Bash and Zsh (in @command{sh} emulation mode) output 1.
12236 The problem is truly @command{echo}: all the shells
12237 understand @samp{'\n'} as the string composed of a backslash and an
12238 @samp{n}.
12240 Because of these problems, do not pass a string containing arbitrary
12241 characters to @command{echo}.  For example, @samp{echo "$foo"} is safe
12242 if you know that @var{foo}'s value cannot contain backslashes and cannot
12243 start with @samp{-}, but otherwise you should use a here-document like
12244 this:
12246 @example
12247 cat <<EOF
12248 $foo
12250 @end example
12253 @item @command{eval}
12254 @c -----------------
12255 @prindex @command{eval}
12256 The @command{eval} command is useful in limited circumstances, e.g.,
12257 using commands like @samp{eval table_$key=\$value} and @samp{eval
12258 value=table_$key} to simulate a hash table when the key is known to be
12259 alphanumeric.  However, @command{eval} is tricky to use on arbitrary
12260 arguments, even when it is implemented correctly.
12262 It is obviously unwise to use @samp{eval $cmd} if the string value of
12263 @samp{cmd} was derived from an untrustworthy source.  But even if the
12264 string value is valid, @samp{eval $cmd} might not work as intended,
12265 since it causes field splitting and file name expansion to occur twice,
12266 once for the @command{eval} and once for the command itself.  It is
12267 therefore safer to use @samp{eval "$cmd"}.  For example, if @var{cmd}
12268 has the value @samp{cat test?.c}, @samp{eval $cmd} might expand to the
12269 equivalent of @samp{cat test;.c} if there happens to be a file named
12270 @file{test;.c} in the current directory; and this in turn
12271 mistakenly attempts to invoke @command{cat} on the file @file{test} and
12272 then execute the command @command{.c}.  To avoid this problem, use
12273 @samp{eval "$cmd"} rather than @samp{eval $cmd}.
12275 However, suppose that you want to output the text of the evaluated
12276 command just before executing it.  Assuming the previous example,
12277 @samp{echo "Executing: $cmd"} outputs @samp{Executing: cat test?.c}, but
12278 this output doesn't show the user that @samp{test;.c} is the actual name
12279 of the copied file.  Conversely, @samp{eval "echo Executing: $cmd"}
12280 works on this example, but it fails with @samp{cmd='cat foo >bar'},
12281 since it mistakenly replaces the contents of @file{bar} by the
12282 string @samp{cat foo}.  No simple, general, and portable solution to
12283 this problem is known.
12285 You should also be wary of common bugs in @command{eval} implementations.
12286 In some shell implementations (e.g., older @command{ash}, Open@acronym{BSD} 3.8
12287 @command{sh}, @command{pdksh} v5.2.14 99/07/13.2, and @command{zsh}
12288 4.2.5), the arguments of @samp{eval} are evaluated in a context where
12289 @samp{$?} is 0, so they exhibit behavior like this:
12291 @example
12292 $ @kbd{false; eval 'echo $?'}
12294 @end example
12296 The correct behavior here is to output a nonzero value,
12297 but portable scripts should not rely on this.
12299 You should not rely on @code{LINENO} within @command{eval}.
12300 @xref{Special Shell Variables}.
12302 @item @command{exit}
12303 @c -----------------
12304 @prindex @command{exit}
12305 The default value of @command{exit} is supposed to be @code{$?};
12306 unfortunately, some shells, such as the @acronym{DJGPP} port of Bash 2.04, just
12307 perform @samp{exit 0}.
12309 @example
12310 bash-2.04$ @kbd{foo=`exit 1` || echo fail}
12311 fail
12312 bash-2.04$ @kbd{foo=`(exit 1)` || echo fail}
12313 fail
12314 bash-2.04$ @kbd{foo=`(exit 1); exit` || echo fail}
12315 bash-2.04$
12316 @end example
12318 Using @samp{exit $?} restores the expected behavior.
12320 Some shell scripts, such as those generated by @command{autoconf}, use a
12321 trap to clean up before exiting.  If the last shell command exited with
12322 nonzero status, the trap also exits with nonzero status so that the
12323 invoker can tell that an error occurred.
12325 Unfortunately, in some shells, such as Solaris @command{/bin/sh}, an exit
12326 trap ignores the @code{exit} command's argument.  In these shells, a trap
12327 cannot determine whether it was invoked by plain @code{exit} or by
12328 @code{exit 1}.  Instead of calling @code{exit} directly, use the
12329 @code{AC_MSG_ERROR} macro that has a workaround for this problem.
12332 @item @command{export}
12333 @c -------------------
12334 @prindex @command{export}
12335 The builtin @command{export} dubs a shell variable @dfn{environment
12336 variable}.  Each update of exported variables corresponds to an update
12337 of the environment variables.  Conversely, each environment variable
12338 received by the shell when it is launched should be imported as a shell
12339 variable marked as exported.
12341 Alas, many shells, such as Solaris @command{/bin/sh},
12342 @sc{irix} 6.3, @sc{irix} 5.2,
12343 @acronym{AIX} 4.1.5, and Digital Unix 4.0, forget to
12344 @command{export} the environment variables they receive.  As a result,
12345 two variables coexist: the environment variable and the shell
12346 variable.  The following code demonstrates this failure:
12348 @example
12349 #!/bin/sh
12350 echo $FOO
12351 FOO=bar
12352 echo $FOO
12353 exec /bin/sh $0
12354 @end example
12356 @noindent
12357 when run with @samp{FOO=foo} in the environment, these shells print
12358 alternately @samp{foo} and @samp{bar}, although they should print only
12359 @samp{foo} and then a sequence of @samp{bar}s.
12361 Therefore you should @command{export} again each environment variable
12362 that you update.
12365 @item @command{false}
12366 @c ------------------
12367 @prindex @command{false}
12368 Don't expect @command{false} to exit with status 1: in native
12369 Solaris @file{/bin/false} exits with status 255.
12372 @item @command{for}
12373 @c ----------------
12374 @prindex @command{for}
12375 To loop over positional arguments, use:
12377 @example
12378 for arg
12380   echo "$arg"
12381 done
12382 @end example
12384 @noindent
12385 You may @emph{not} leave the @code{do} on the same line as @code{for},
12386 since some shells improperly grok:
12388 @example
12389 for arg; do
12390   echo "$arg"
12391 done
12392 @end example
12394 If you want to explicitly refer to the positional arguments, given the
12395 @samp{$@@} bug (@pxref{Shell Substitutions}), use:
12397 @example
12398 for arg in $@{1+"$@@"@}; do
12399   echo "$arg"
12400 done
12401 @end example
12403 @noindent
12404 But keep in mind that Zsh, even in Bourne shell emulation mode, performs
12405 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
12406 item @samp{$@@}, for more.
12409 @item @command{if}
12410 @c ---------------
12411 @prindex @command{if}
12412 Using @samp{!} is not portable.  Instead of:
12414 @example
12415 if ! cmp -s file file.new; then
12416   mv file.new file
12418 @end example
12420 @noindent
12421 use:
12423 @example
12424 if cmp -s file file.new; then :; else
12425   mv file.new file
12427 @end example
12429 There are shells that do not reset the exit status from an @command{if}:
12431 @example
12432 $ @kbd{if (exit 42); then true; fi; echo $?}
12434 @end example
12436 @noindent
12437 whereas a proper shell should have printed @samp{0}.  This is especially
12438 bad in makefiles since it produces false failures.  This is why properly
12439 written makefiles, such as Automake's, have such hairy constructs:
12441 @example
12442 if test -f "$file"; then
12443   install "$file" "$dest"
12444 else
12445   :
12447 @end example
12450 @item @command{printf}
12451 @c ------------------
12452 @prindex @command{printf}
12453 A format string starting with a @samp{-} can cause problems.
12454 Bash (e.g., 2.05b) interprets it as an options argument and
12455 gives an error.  And @samp{--} to mark the end of options is not good
12456 in the Net@acronym{BSD} Almquist shell (e.g., 0.4.6) which takes that
12457 literally as the format string.  Putting the @samp{-} in a @samp{%c}
12458 or @samp{%s} is probably the easiest way to avoid doubt,
12460 @example
12461 printf %s -foo
12462 @end example
12465 @item @command{read}
12466 @c ------------------
12467 @prindex @command{read}
12468 Not all shells support @option{-r} (Solaris @command{/bin/sh} for example).
12471 @item @command{pwd}
12472 @c ----------------
12473 @prindex @command{pwd}
12474 With modern shells, plain @command{pwd} outputs a ``logical''
12475 directory name, some of whose components may be symbolic links.  These
12476 directory names are in contrast to ``physical'' directory names, whose
12477 components are all directories.
12479 Posix 1003.1-2001 requires that @command{pwd} must support
12480 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
12481 with @option{-L} being the default.  However, traditional shells do
12482 not support these options, and their @command{pwd} command has the
12483 @option{-P} behavior.
12485 Portable scripts should assume neither option is supported, and should
12486 assume neither behavior is the default.  Also, on many hosts
12487 @samp{/bin/pwd} is equivalent to @samp{pwd -P}, but Posix
12488 does not require this behavior and portable scripts should not rely on
12491 Typically it's best to use plain @command{pwd}.  On modern hosts this
12492 outputs logical directory names, which have the following advantages:
12494 @itemize @bullet
12495 @item
12496 Logical names are what the user specified.
12497 @item
12498 Physical names may not be portable from one installation
12499 host to another due to network file system gymnastics.
12500 @item
12501 On modern hosts @samp{pwd -P} may fail due to lack of permissions to
12502 some parent directory, but plain @command{pwd} cannot fail for this
12503 reason.
12504 @end itemize
12506 Also please see the discussion of the @command{cd} command.
12509 @item @command{set}
12510 @c ----------------
12511 @prindex @command{set}
12512 With the Free@acronym{BSD} 6.0 shell, the @command{set} command (without
12513 any options) does not sort its output.
12515 The @command{set} builtin faces the usual problem with arguments starting with a
12516 dash.  Modern shells such as Bash or Zsh understand @option{--} to specify
12517 the end of the options (any argument after @option{--} is a parameter,
12518 even @samp{-x} for instance), but many traditional shells (e.g., Solaris
12519 10 @command{/bin/sh}) simply stop option
12520 processing as soon as a non-option argument is found.  Therefore, use
12521 @samp{dummy} or simply @samp{x} to end the option processing, and use
12522 @command{shift} to pop it out:
12524 @example
12525 set x $my_list; shift
12526 @end example
12528 Avoid @samp{set -}, e.g., @samp{set - $my_list}.  Posix no
12529 longer requires support for this command, and in traditional shells
12530 @samp{set - $my_list} resets the @option{-v} and @option{-x} options, which
12531 makes scripts harder to debug.
12533 Some nonstandard shells do not recognize more than one option
12534 (e.g., @samp{set -e -x} assigns @samp{-x} to the command line).  It is
12535 better to combine them:
12537 @example
12538 set -ex
12539 @end example
12541 The @acronym{BSD} shell has had several problems with the @option{-e}
12542 option, partly because @acronym{BSD} @command{make} traditionally used
12543 @option{-e} even though this was incompatible with Posix
12544 (@pxref{Failure in Make Rules}).  Older versions of the @acronym{BSD}
12545 shell (circa 1990) mishandled @samp{&&}, @samp{||}, @samp{if}, and
12546 @samp{case} when @option{-e} was in effect, causing the shell to exit
12547 unexpectedly in some cases.  This was particularly a problem with
12548 makefiles, and led to circumlocutions like @samp{sh -c 'test -f file ||
12549 touch file'}, where the seemingly-unnecessary @samp{sh -c '@dots{}'}
12550 wrapper works around the bug.
12552 Even relatively-recent versions of the @acronym{BSD} shell (e.g.,
12553 Open@acronym{BSD} 3.4) wrongly exit with @option{-e} if a command within
12554 @samp{&&} fails inside a compound statement.  For example:
12556 @example
12557 #! /bin/sh
12558 set -e
12559 foo=''
12560 test -n "$foo" && exit 1
12561 echo one
12562 if :; then
12563   test -n "$foo" && exit 1
12565 echo two
12566 @end example
12568 @noindent
12569 does not print @samp{two}.  One workaround is to use @samp{if test -n
12570 "$foo"; then exit 1; fi} rather than @samp{test -n "$foo" && exit 1}.
12571 Another possibility is to warn @acronym{BSD} users not to use @samp{sh -e}.
12574 @item @command{shift}
12575 @c ------------------
12576 @prindex @command{shift}
12577 Not only is @command{shift}ing a bad idea when there is nothing left to
12578 shift, but in addition it is not portable: the shell of @acronym{MIPS
12579 RISC/OS} 4.52 refuses to do it.
12581 Don't use @samp{shift 2} etc.; it was not in the 7th Edition Bourne shell,
12582 and it is also absent in many pre-Posix shells.
12585 @item @command{source}
12586 @c -------------------
12587 @prindex @command{source}
12588 This command is not portable, as Posix does not require it; use
12589 @command{.} instead.
12592 @item @command{test}
12593 @c -----------------
12594 @prindex @command{test}
12595 The @code{test} program is the way to perform many file and string
12596 tests.  It is often invoked by the alternate name @samp{[}, but using
12597 that name in Autoconf code is asking for trouble since it is an M4 quote
12598 character.
12600 If you need to make multiple checks using @code{test}, combine them with
12601 the shell operators @samp{&&} and @samp{||} instead of using the
12602 @code{test} operators @option{-a} and @option{-o}.  On System V, the
12603 precedence of @option{-a} and @option{-o} is wrong relative to the unary
12604 operators; consequently, Posix does not specify them, so using them
12605 is nonportable.  If you combine @samp{&&} and @samp{||} in the same
12606 statement, keep in mind that they have equal precedence.
12608 It is safe to use @samp{!} as a @command{test} operator.  For example,
12609 @samp{if test ! -d foo; @dots{}} is portable even though @samp{if ! test
12610 -d foo; @dots{}} is not.
12613 @item @command{test} (files)
12614 @c -------------------------
12615 To enable @command{configure} scripts to support cross-compilation, they
12616 shouldn't do anything that tests features of the build system instead of
12617 the host system.  But occasionally you may find it necessary to check
12618 whether some arbitrary file exists.  To do so, use @samp{test -f} or
12619 @samp{test -r}.  Do not use @samp{test -x}, because 4.3@acronym{BSD} does not
12620 have it.  Do not use @samp{test -e} either, because Solaris @command{/bin/sh}
12621 lacks it.  To test for symbolic links on systems that have them, use
12622 @samp{test -h} rather than @samp{test -L}; either form conforms to
12623 Posix 1003.1-2001, but older shells like Solaris 8
12624 @code{/bin/sh} support only @option{-h}.
12626 @item @command{test} (strings)
12627 @c ---------------------------
12628 Avoid @samp{test "@var{string}"}, in particular if @var{string} might
12629 start with a dash, since @code{test} might interpret its argument as an
12630 option (e.g., @samp{@var{string} = "-n"}).
12632 Contrary to a common belief, @samp{test -n @var{string}} and
12633 @samp{test -z @var{string}} @strong{are} portable.  Nevertheless many
12634 shells (such as Solaris, @acronym{AIX} 3.2, @sc{unicos} 10.0.0.6,
12635 Digital Unix 4, etc.)@: have bizarre precedence and may be confused if
12636 @var{string} looks like an operator:
12638 @example
12639 $ @kbd{test -n =}
12640 test: argument expected
12641 @end example
12643 If there are risks, use @samp{test "x@var{string}" = x} or @samp{test
12644 "x@var{string}" != x} instead.
12646 It is common to find variations of the following idiom:
12648 @example
12649 test -n "`echo $ac_feature | sed 's/[-a-zA-Z0-9_]//g'`" &&
12650   @var{action}
12651 @end example
12653 @noindent
12654 to take an action when a token matches a given pattern.  Such constructs
12655 should always be avoided by using:
12657 @example
12658 echo "$ac_feature" | grep '[^-a-zA-Z0-9_]' >/dev/null 2>&1 &&
12659   @var{action}
12660 @end example
12662 @noindent
12663 Use @code{case} where possible since it is faster, being a shell builtin:
12666 @example
12667 case $ac_feature in
12668   *[!-a-zA-Z0-9_]*) @var{action};;
12669 esac
12670 @end example
12672 Alas, negated character classes are probably not portable, although no
12673 shell is known to not support the Posix syntax @samp{[!@dots{}]}
12674 (when in interactive mode, @command{zsh} is confused by the
12675 @samp{[!@dots{}]} syntax and looks for an event in its history because of
12676 @samp{!}).  Many shells do not support the alternative syntax
12677 @samp{[^@dots{}]} (Solaris, Digital Unix, etc.).
12679 One solution can be:
12681 @example
12682 expr "$ac_feature" : '.*[^-a-zA-Z0-9_]' >/dev/null &&
12683   @var{action}
12684 @end example
12686 @noindent
12687 or better yet
12689 @example
12690 expr "X$ac_feature" : '.*[^-a-zA-Z0-9_]' >/dev/null &&
12691   @var{action}
12692 @end example
12694 @samp{expr "X@var{foo}" : "X@var{bar}"} is more robust than @samp{echo
12695 "X@var{foo}" | grep "^X@var{bar}"}, because it avoids problems when
12696 @samp{@var{foo}} contains backslashes.
12699 @item @command{trap}
12700 @c -----------------
12701 @prindex @command{trap}
12702 It is safe to trap at least the signals 1, 2, 13, and 15.  You can also
12703 trap 0, i.e., have the @command{trap} run when the script ends (either via an
12704 explicit @command{exit}, or the end of the script).
12706 Posix says that @samp{trap - 1 2 13 15} resets the traps for the
12707 specified signals to their default values, but many common shells (e.g.,
12708 Solaris @command{/bin/sh}) misinterpret this and attempt to execute a
12709 ``command'' named @command{-} when the specified conditions arise.
12710 There is no portable workaround, except for @samp{trap - 0}, for which
12711 @samp{trap '' 0} is a portable substitute.
12713 Although Posix is not absolutely clear on this point, it is widely
12714 admitted that when entering the trap @samp{$?} should be set to the exit
12715 status of the last command run before the trap.  The ambiguity can be
12716 summarized as: ``when the trap is launched by an @command{exit}, what is
12717 the @emph{last} command run: that before @command{exit}, or
12718 @command{exit} itself?''
12720 Bash considers @command{exit} to be the last command, while Zsh and
12721 Solaris @command{/bin/sh} consider that when the trap is run it is
12722 @emph{still} in the @command{exit}, hence it is the previous exit status
12723 that the trap receives:
12725 @example
12726 $ @kbd{cat trap.sh}
12727 trap 'echo $?' 0
12728 (exit 42); exit 0
12729 $ @kbd{zsh trap.sh}
12731 $ @kbd{bash trap.sh}
12733 @end example
12735 The portable solution is then simple: when you want to @samp{exit 42},
12736 run @samp{(exit 42); exit 42}, the first @command{exit} being used to
12737 set the exit status to 42 for Zsh, and the second to trigger the trap
12738 and pass 42 as exit status for Bash.
12740 The shell in Free@acronym{BSD} 4.0 has the following bug: @samp{$?} is
12741 reset to 0 by empty lines if the code is inside @command{trap}.
12743 @example
12744 $ @kbd{trap 'false}
12746 echo $?' 0
12747 $ @kbd{exit}
12749 @end example
12751 @noindent
12752 Fortunately, this bug only affects @command{trap}.
12754 @item @command{true}
12755 @c -----------------
12756 @prindex @command{true}
12757 @c Info cannot handle `:' in index entries.
12758 @c @prindex @command{:}
12759 Don't worry: as far as we know @command{true} is portable.
12760 Nevertheless, it's not always a builtin (e.g., Bash 1.x), and the
12761 portable shell community tends to prefer using @command{:}.  This has a
12762 funny side effect: when asked whether @command{false} is more portable
12763 than @command{true} Alexandre Oliva answered:
12765 @quotation
12766 In a sense, yes, because if it doesn't exist, the shell will produce an
12767 exit status of failure, which is correct for @command{false}, but not
12768 for @command{true}.
12769 @end quotation
12772 @item @command{unset}
12773 @c ------------------
12774 @prindex @command{unset}
12775 You cannot assume the support of @command{unset}.  Nevertheless, because
12776 it is extremely useful to disable embarrassing variables such as
12777 @code{PS1}, you can test for its existence and use
12778 it @emph{provided} you give a neutralizing value when @command{unset} is
12779 not supported:
12781 @example
12782 if (unset FOO) >/dev/null 2>&1; then
12783   unset=unset
12784 else
12785   unset=false
12787 $unset PS1 || PS1='$ '
12788 @end example
12790 @xref{Special Shell Variables}, for some neutralizing values.  Also, see
12791 @ref{Limitations of Builtins}, documentation of @command{export}, for
12792 the case of environment variables.
12793 @end table
12795 @node Limitations of Usual Tools
12796 @section Limitations of Usual Tools
12797 @cindex Limitations of usual tools
12799 The small set of tools you can expect to find on any machine can still
12800 include some limitations you should be aware of.
12802 @table @asis
12803 @item Awk
12804 @c ------
12805 @prindex Awk
12806 Don't leave white space before the opening parenthesis in a user function call.
12807 Posix does not allow this and @acronym{GNU} Awk rejects it:
12809 @example
12810 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
12811         BEGIN @{ die () @}'}
12812 gawk: cmd. line:2:         BEGIN @{ die () @}
12813 gawk: cmd. line:2:                      ^ parse error
12814 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
12815         BEGIN @{ die() @}'}
12816 Aaaaarg!
12817 @end example
12819 If you want your program to be deterministic, don't depend on @code{for}
12820 on arrays:
12822 @example
12823 $ @kbd{cat for.awk}
12824 END @{
12825   arr["foo"] = 1
12826   arr["bar"] = 1
12827   for (i in arr)
12828     print i
12830 $ @kbd{gawk -f for.awk </dev/null}
12833 $ @kbd{nawk -f for.awk </dev/null}
12836 @end example
12838 Some Awk implementations, such as @acronym{HP-UX} 11.0's native one, mishandle anchors:
12840 @example
12841 $ @kbd{echo xfoo | $AWK '/foo|^bar/ @{ print @}'}
12842 $ @kbd{echo bar | $AWK '/foo|^bar/ @{ print @}'}
12844 $ @kbd{echo xfoo | $AWK '/^bar|foo/ @{ print @}'}
12845 xfoo
12846 $ @kbd{echo bar | $AWK '/^bar|foo/ @{ print @}'}
12848 @end example
12850 @noindent
12851 Either do not depend on such patterns (i.e., use @samp{/^(.*foo|bar)/},
12852 or use a simple test to reject such implementations.
12854 @acronym{AIX} version 5.2 has an arbitrary limit of 399 on the the
12855 length of regular expressions and literal strings in an Awk program.
12857 Traditional Awk implementations derived from Unix version 7, such as
12858 Solaris @command{/bin/awk}, have many limitations and do not
12859 conform to Posix.  Nowadays @code{AC_PROG_AWK} (@pxref{Particular
12860 Programs}) finds you an Awk that doesn't have these problems, but if
12861 for some reason you prefer not to use @code{AC_PROG_AWK} you may need to
12862 address them.
12864 Traditional Awk does not support multidimensional arrays or user-defined
12865 functions.
12867 Traditional Awk does not support the @option{-v} option.  You can use
12868 assignments after the program instead, e.g., @command{$AWK '@{print v
12869 $1@}' v=x}; however, don't forget that such assignments are not
12870 evaluated until they are encountered (e.g., after any @code{BEGIN}
12871 action).
12873 Traditional Awk does not support the keywords @code{delete} or @code{do}.
12875 Traditional Awk does not support the expressions
12876 @code{@var{a}?@var{b}:@var{c}}, @code{!@var{a}}, @code{@var{a}^@var{b}},
12877 or @code{@var{a}^=@var{b}}.
12879 Traditional Awk does not support the predefined @code{CONVFMT} variable.
12881 Traditional Awk supports only the predefined functions @code{exp},
12882 @code{int}, @code{length}, @code{log}, @code{split}, @code{sprintf},
12883 @code{sqrt}, and @code{substr}.
12885 Traditional Awk @code{getline} is not at all compatible with Posix;
12886 avoid it.
12888 Traditional Awk @code{split} supports only two arguments.
12890 Traditional Awk has a limit of 99
12891 fields in a record.  You may be able to circumvent this problem by using
12892 @code{split}.
12895 @item @command{basename}
12896 @c ---------------------
12897 @prindex @command{basename}
12898 Not all hosts have a working @command{basename}.
12899 You can use @command{expr} instead.
12901 @c AS_BASENAME is to be replaced by a better API.
12902 @ignore
12903 Not all hosts have a working @command{basename}, and you should instead
12904 use @code{AS_BASENAME} (@pxref{Programming in M4sh}), followed by
12905 @command{expr} if you need to strip a suffix.  For example:
12907 @example
12908 a=`basename "$aname"`       # This is not portable.
12909 a=`AS_BASENAME(["$aname"])` # This is more portable.
12911 # This is not portable.
12912 c=`basename "$cname" .c`
12914 # This is more portable.
12915 c=`AS_BASENAME(["$cname"])`
12916 case $c in
12917 ?*.c) c=`expr "X$c" : 'X\(.*\)\.c'`;;
12918 esac
12919 @end example
12920 @end ignore
12923 @item @command{cat}
12924 @c ----------------
12925 @prindex @command{cat}
12926 Don't rely on any option.
12929 @item @command{cc}
12930 @c ---------------
12931 @prindex @command{cc}
12932 The command @samp{cc -c foo.c} traditionally produces an object file
12933 named @file{foo.o}.  Most compilers allow @option{-c} to be combined
12934 with @option{-o} to specify a different object file name, but
12935 Posix does not require this combination and a few compilers
12936 lack support for it.  @xref{C Compiler}, for how @acronym{GNU} Make
12937 tests for this feature with @code{AC_PROG_CC_C_O}.
12939 When a compilation such as @samp{cc -o foo foo.c} fails, some compilers
12940 (such as @sc{cds} on Reliant Unix) leave a @file{foo.o}.
12942 @acronym{HP-UX} @command{cc} doesn't accept @file{.S} files to preprocess and
12943 assemble.  @samp{cc -c foo.S} appears to succeed, but in fact does
12944 nothing.
12946 The default executable, produced by @samp{cc foo.c}, can be
12948 @itemize
12949 @item @file{a.out} --- usual Posix convention.
12950 @item @file{b.out} --- i960 compilers (including @command{gcc}).
12951 @item @file{a.exe} --- @acronym{DJGPP} port of @command{gcc}.
12952 @item @file{a_out.exe} --- GNV @command{cc} wrapper for DEC C on OpenVMS.
12953 @item @file{foo.exe} --- various MS-DOS compilers.
12954 @end itemize
12956 The C compiler's traditional name is @command{cc}, but other names like
12957 @command{gcc} are common.  Posix 1003.1-2001 specifies the
12958 name @command{c99}, but older Posix editions specified
12959 @command{c89} and anyway these standard names are rarely used in
12960 practice.  Typically the C compiler is invoked from makefiles that use
12961 @samp{$(CC)}, so the value of the @samp{CC} make variable selects the
12962 compiler name.
12965 @item @command{chmod}
12966 @c ------------------
12967 @prindex @command{chmod}
12968 Avoid usages like @samp{chmod -w file}; use @samp{chmod a-w file}
12969 instead, for two reasons.  First, plain @option{-w} does not necessarily
12970 make the file unwritable, since it does not affect mode bits that
12971 correspond to bits in the file mode creation mask.  Second,
12972 Posix says that the @option{-w} might be interpreted as an
12973 implementation-specific option, not as a mode; Posix suggests
12974 using @samp{chmod -- -w file} to avoid this confusion, but unfortunately
12975 @samp{--} does not work on some older hosts.
12978 @item @command{cmp}
12979 @c ----------------
12980 @prindex @command{cmp}
12981 @command{cmp} performs a raw data comparison of two files, while
12982 @command{diff} compares two text files.  Therefore, if you might compare
12983 DOS files, even if only checking whether two files are different, use
12984 @command{diff} to avoid spurious differences due to differences of
12985 newline encoding.
12988 @item @command{cp}
12989 @c ---------------
12990 @prindex @command{cp}
12991 Avoid the @option{-r} option, since Posix 1003.1-2004 marks it as
12992 obsolescent and its behavior on special files is implementation-defined.
12993 Use @option{-R} instead.  On @acronym{GNU} hosts the two options
12994 are equivalent, but on Solaris hosts (for example) @command{cp -r}
12995 reads from pipes instead of replicating them.
12997 Some @command{cp} implementations (e.g., @acronym{BSD/OS} 4.2) do not allow
12998 trailing slashes at the end of nonexistent destination directories.  To
12999 avoid this problem, omit the trailing slashes.  For example, use
13000 @samp{cp -R source /tmp/newdir} rather than @samp{cp -R source
13001 /tmp/newdir/} if @file{/tmp/newdir} does not exist.
13003 @c This is thanks to Ian.
13004 The ancient SunOS 4 @command{cp} does not support @option{-f}, although
13005 its @command{mv} does.
13007 @cindex timestamp resolution
13008 Traditionally, file timestamps had 1-second resolution, and @samp{cp
13009 -p} copied the timestamps exactly.  However, many modern file systems
13010 have timestamps with 1-nanosecond resolution.  Unfortunately, @samp{cp
13011 -p} implementations truncate timestamps when copying files, so this
13012 can result in the destination file appearing to be older than the
13013 source.  The exact amount of truncation depends on the resolution of
13014 the system calls that @command{cp} uses; traditionally this was
13015 @code{utime}, which has 1-second resolution, but some newer
13016 @command{cp} implementations use @code{utimes}, which has
13017 1-microsecond resolution.  These newer implementations include @acronym{GNU}
13018 Core Utilities 5.0.91 or later, and Solaris 8 (sparc) patch 109933-02 or
13019 later.  Unfortunately as of January 2006 there is still no system
13020 call to set timestamps to the full nanosecond resolution.
13022 Bob Proulx notes that @samp{cp -p} always @emph{tries} to copy
13023 ownerships.  But whether it actually does copy ownerships or not is a
13024 system dependent policy decision implemented by the kernel.  If the
13025 kernel allows it then it happens.  If the kernel does not allow it then
13026 it does not happen.  It is not something @command{cp} itself has control
13027 over.
13029 In Unix System V any user can chown files to any other user, and System
13030 V also has a non-sticky @file{/tmp}.  That probably derives from the
13031 heritage of System V in a business environment without hostile users.
13032 @acronym{BSD} changed this
13033 to be a more secure model where only root can @command{chown} files and
13034 a sticky @file{/tmp} is used.  That undoubtedly derives from the heritage
13035 of @acronym{BSD} in a campus environment.
13037 @acronym{GNU}/Linux and Solaris by default follow @acronym{BSD}, but
13038 can be configured to allow a System V style @command{chown}.  On the
13039 other hand, @acronym{HP-UX} follows System V, but can
13040 be configured to use the modern security model and disallow
13041 @command{chown}.  Since it is an administrator-configurable parameter
13042 you can't use the name of the kernel as an indicator of the behavior.
13046 @item @command{date}
13047 @c -----------------
13048 @prindex @command{date}
13049 Some versions of @command{date} do not recognize special @samp{%} directives,
13050 and unfortunately, instead of complaining, they just pass them through,
13051 and exit with success:
13053 @example
13054 $ @kbd{uname -a}
13055 OSF1 medusa.sis.pasteur.fr V5.1 732 alpha
13056 $ @kbd{date "+%s"}
13058 @end example
13061 @item @command{diff}
13062 @c -----------------
13063 @prindex @command{diff}
13064 Option @option{-u} is nonportable.
13066 Some implementations, such as Tru64's, fail when comparing to
13067 @file{/dev/null}.  Use an empty file instead.
13070 @item @command{dirname}
13071 @c --------------------
13072 @prindex @command{dirname}
13073 Not all hosts have a working @command{dirname}, and you should instead
13074 use @code{AS_DIRNAME} (@pxref{Programming in M4sh}).  For example:
13076 @example
13077 dir=`dirname "$file"`       # This is not portable.
13078 dir=`AS_DIRNAME(["$file"])` # This is more portable.
13079 @end example
13082 @item @command{egrep}
13083 @c ------------------
13084 @prindex @command{egrep}
13085 Posix 1003.1-2001 no longer requires @command{egrep},
13086 but many older hosts do not yet support the Posix
13087 replacement @code{grep -E}.  Also, some traditional implementations do
13088 not work on long input lines.  To work around these problems, invoke
13089 @code{AC_PROG_EGREP} and then use @code{$EGREP}.
13091 Portable extended regular expressions should use @samp{\} only to escape
13092 characters in the string @samp{$()*+.?[\^@{|}.  For example, @samp{\@}}
13093 is not portable, even though it typically matches @samp{@}}.
13095 The empty alternative is not portable.  Use @samp{?} instead.  For
13096 instance with Digital Unix v5.0:
13098 @example
13099 > printf "foo\n|foo\n" | $EGREP '^(|foo|bar)$'
13100 |foo
13101 > printf "bar\nbar|\n" | $EGREP '^(foo|bar|)$'
13102 bar|
13103 > printf "foo\nfoo|\n|bar\nbar\n" | $EGREP '^(foo||bar)$'
13105 |bar
13106 @end example
13108 @command{$EGREP} also suffers the limitations of @command{grep}.
13110 @item @command{expr}
13111 @c -----------------
13112 @prindex @command{expr}
13113 No @command{expr} keyword starts with @samp{X}, so use @samp{expr
13114 X"@var{word}" : 'X@var{regex}'} to keep @command{expr} from
13115 misinterpreting @var{word}.
13117 Don't use @code{length}, @code{substr}, @code{match} and @code{index}.
13119 @item @command{expr} (@samp{|})
13120 @prindex @command{expr} (@samp{|})
13121 You can use @samp{|}.  Although Posix does require that @samp{expr
13122 ''} return the empty string, it does not specify the result when you
13123 @samp{|} together the empty string (or zero) with the empty string.  For
13124 example:
13126 @example
13127 expr '' \| ''
13128 @end example
13130 Posix 1003.2-1992 returns the empty string
13131 for this case, but traditional Unix returns @samp{0} (Solaris is
13132 one such example).  In Posix 1003.1-2001, the specification was
13133 changed to match traditional Unix's behavior (which is
13134 bizarre, but it's too late to fix this).  Please note that the same
13135 problem does arise when the empty string results from a computation,
13136 as in:
13138 @example
13139 expr bar : foo \| foo : bar
13140 @end example
13142 @noindent
13143 Avoid this portability problem by avoiding the empty string.
13146 @item @command{expr} (@samp{:})
13147 @c ----------------------------
13148 @prindex @command{expr}
13149 Portable @command{expr} regular expressions should use @samp{\} to
13150 escape only characters in the string @samp{$()*.0123456789[\^n@{@}}.
13151 For example, alternation, @samp{\|}, is common but Posix does not
13152 require its support, so it should be avoided in portable scripts.
13153 Similarly, @samp{\+} and @samp{\?} should be avoided.
13155 Portable @command{expr} regular expressions should not begin with
13156 @samp{^}.  Patterns are automatically anchored so leading @samp{^} is
13157 not needed anyway.
13159 The Posix standard is ambiguous as to whether
13160 @samp{expr 'a' : '\(b\)'} outputs @samp{0} or the empty string.
13161 In practice, it outputs the empty string on most platforms, but portable
13162 scripts should not assume this.  For instance, the @acronym{QNX} 4.25 native
13163 @command{expr} returns @samp{0}.
13165 One might think that a way to get a uniform behavior would be to use
13166 the empty string as a default value:
13168 @example
13169 expr a : '\(b\)' \| ''
13170 @end example
13172 @noindent
13173 Unfortunately this behaves exactly as the original expression; see the
13174 @command{expr} (@samp{|}) entry for more information.
13176 Ancient @command{expr} implementations (e.g., SunOS 4 @command{expr} and
13177 Solaris 8 @command{/usr/ucb/expr}) have a silly length limit that causes
13178 @command{expr} to fail if the matched substring is longer than 120
13179 bytes.  In this case, you might want to fall back on @samp{echo|sed} if
13180 @command{expr} fails.  Nowadays this is of practical importance only for
13181 the rare installer who mistakenly puts @file{/usr/ucb} before
13182 @file{/usr/bin} in @env{PATH}.
13184 On Mac OS X 10.4, @command{expr} mishandles the pattern @samp{[^-]} in
13185 some cases.  For example, the command
13186 @example
13187 expr Xpowerpc-apple-darwin8.1.0 : 'X[^-]*-[^-]*-\(.*\)'
13188 @end example
13190 @noindent
13191 outputs @samp{apple-darwin8.1.0} rather than the correct @samp{darwin8.1.0}.
13192 This particular case can be worked around by substituting @samp{[^--]}
13193 for @samp{[^-]}.
13195 Don't leave, there is some more!
13197 The @acronym{QNX} 4.25 @command{expr}, in addition of preferring @samp{0} to
13198 the empty string, has a funny behavior in its exit status: it's always 1
13199 when parentheses are used!
13201 @example
13202 $ @kbd{val=`expr 'a' : 'a'`; echo "$?: $val"}
13203 0: 1
13204 $ @kbd{val=`expr 'a' : 'b'`; echo "$?: $val"}
13205 1: 0
13207 $ @kbd{val=`expr 'a' : '\(a\)'`; echo "?: $val"}
13208 1: a
13209 $ @kbd{val=`expr 'a' : '\(b\)'`; echo "?: $val"}
13210 1: 0
13211 @end example
13213 @noindent
13214 In practice this can be a big problem if you are ready to catch failures
13215 of @command{expr} programs with some other method (such as using
13216 @command{sed}), since you may get twice the result.  For instance
13218 @example
13219 $ @kbd{expr 'a' : '\(a\)' || echo 'a' | sed 's/^\(a\)$/\1/'}
13220 @end example
13222 @noindent
13223 outputs @samp{a} on most hosts, but @samp{aa} on @acronym{QNX} 4.25.  A
13224 simple workaround consists of testing @command{expr} and using a variable
13225 set to @command{expr} or to @command{false} according to the result.
13227 Tru64 @command{expr} incorrectly treats the result as a number, if it
13228 can be interpreted that way:
13230 @example
13231 $ @kbd{expr 00001 : '.*\(...\)'}
13233 @end example
13236 @item @command{fgrep}
13237 @c ------------------
13238 @prindex @command{fgrep}
13239 Posix 1003.1-2001 no longer requires @command{fgrep},
13240 but many older hosts do not yet support the Posix
13241 replacement @code{grep -F}.  Also, some traditional implementations do
13242 not work on long input lines.  To work around these problems, invoke
13243 @code{AC_PROG_FGREP} and then use @code{$FGREP}.
13246 @item @command{find}
13247 @c -----------------
13248 @prindex @command{find}
13249 The option @option{-maxdepth} seems to be @acronym{GNU} specific.
13250 Tru64 v5.1, Net@acronym{BSD} 1.5 and Solaris @command{find}
13251 commands do not understand it.
13253 The replacement of @samp{@{@}} is guaranteed only if the argument is
13254 exactly @emph{@{@}}, not if it's only a part of an argument.  For
13255 instance on DU, and @acronym{HP-UX} 10.20 and @acronym{HP-UX} 11:
13257 @example
13258 $ @kbd{touch foo}
13259 $ @kbd{find . -name foo -exec echo "@{@}-@{@}" \;}
13260 @{@}-@{@}
13261 @end example
13263 @noindent
13264 while @acronym{GNU} @command{find} reports @samp{./foo-./foo}.
13267 @item @command{grep}
13268 @c -----------------
13269 @prindex @command{grep}
13270 Portable scripts can rely on the @command{grep} options @option{-c},
13271 @option{-l}, @option{-n}, and @option{-v}, but should avoid other
13272 options.  For example, don't use @option{-w}, as Posix does not require
13273 it and Irix 6.5.16m's @command{grep} does not support it.  Also,
13274 portable scripts should not combine @option{-c} with @option{-l},
13275 as Posix does not allow this.
13277 Some of the options required by Posix are not portable in practice.
13278 Don't use @samp{grep -q} to suppress output, because many @command{grep}
13279 implementations (e.g., Solaris) do not support @option{-q}.
13280 Don't use @samp{grep -s} to suppress output either, because Posix
13281 says @option{-s} does not suppress output, only some error messages;
13282 also, the @option{-s} option of traditional @command{grep} behaved
13283 like @option{-q} does in most modern implementations.  Instead,
13284 redirect the standard output and standard error (in case the file
13285 doesn't exist) of @code{grep} to @file{/dev/null}.  Check the exit
13286 status of @code{grep} to determine whether it found a match.
13288 Some traditional @command{grep} implementations do not work on long
13289 input lines.  On AIX the default @code{grep} silently truncates long
13290 lines on the input before matching.
13292 Also, many implementations do not support multiple regexps
13293 with @option{-e}: they either reject @option{-e} entirely (e.g., Solaris)
13294 or honor only the last pattern (e.g., @acronym{IRIX} 6.5 and NeXT).  To
13295 work around these problems, invoke @code{AC_PROG_GREP} and then use
13296 @code{$GREP}.
13298 Another possible workaround for the multiple @option{-e} problem is to
13299 separate the patterns by newlines, for example:
13301 @example
13302 grep 'foo
13303 bar' in.txt
13304 @end example
13306 @noindent
13307 except that this fails with traditional @command{grep}
13308 implementations and with Open@acronym{BSD} 3.8 @command{grep}.
13310 Traditional @command{grep} implementations (e.g., Solaris) do not
13311 support the @option{-E} or @option{-F} options.  To work around these
13312 problems, invoke @code{AC_PROG_EGREP} and then use @code{$EGREP}, and
13313 similarly for @code{AC_PROG_FGREP} and @code{$FGREP}.  Even if you are
13314 willing to require support for Posix @command{grep}, your script should
13315 not use both @option{-E} and @option{-F}, since Posix does not allow
13316 this combination.
13318 Portable @command{grep} regular expressions should use @samp{\} only to
13319 escape characters in the string @samp{$()*.0123456789[\^@{@}}.  For example,
13320 alternation, @samp{\|}, is common but Posix does not require its
13321 support in basic regular expressions, so it should be avoided in
13322 portable scripts.  Solaris @command{grep} does not support it.
13323 Similarly, @samp{\+} and @samp{\?} should be avoided.
13326 @item @command{join}
13327 @c -----------------
13328 @prindex @command{join}
13329 Solaris 8 @command{join} has bugs when the second operand is standard
13330 input, and when standard input is a pipe.  For example, the following
13331 shell script causes Solaris 8 @command{join} to loop forever:
13333 @example
13334 cat >file <<'EOF'
13335 1 x
13336 2 y
13338 cat file | join file -
13339 @end example
13341 Use @samp{join - file} instead.
13344 @item @command{ln}
13345 @c ---------------
13346 @prindex @command{ln}
13347 @cindex Symbolic links
13348 Don't rely on @command{ln} having a @option{-f} option.  Symbolic links
13349 are not available on old systems; use @samp{$(LN_S)} as a portable substitute.
13351 For versions of the @acronym{DJGPP} before 2.04,
13352 @command{ln} emulates symbolic links
13353 to executables by generating a stub that in turn calls the real
13354 program.  This feature also works with nonexistent files like in the
13355 Posix spec.  So @samp{ln -s file link} generates @file{link.exe},
13356 which attempts to call @file{file.exe} if run.  But this feature only
13357 works for executables, so @samp{cp -p} is used instead for these
13358 systems.  @acronym{DJGPP} versions 2.04 and later have full support
13359 for symbolic links.
13362 @item @command{ls}
13363 @c ---------------
13364 @prindex @command{ls}
13365 @cindex Listing directories
13366 The portable options are @option{-acdilrtu}.  Current practice is for
13367 @option{-l} to output both owner and group, even though ancient versions
13368 of @command{ls} omitted the group.
13370 On ancient hosts, @samp{ls foo} sent the diagnostic @samp{foo not found}
13371 to standard output if @file{foo} did not exist.  Hence a shell command
13372 like @samp{sources=`ls *.c 2>/dev/null`} did not always work, since it
13373 was equivalent to @samp{sources='*.c not found'} in the absence of
13374 @samp{.c} files.  This is no longer a practical problem, since current
13375 @command{ls} implementations send diagnostics to standard error.
13377 @item @command{mkdir}
13378 @c ------------------
13379 @prindex @command{mkdir}
13380 @cindex Making directories
13381 No @command{mkdir} option is portable to older systems.  Instead of
13382 @samp{mkdir -p @var{file-name}}, you should use use
13383 @code{AS_MKDIR_P(@var{file-name})} (@pxref{Programming in M4sh})
13384 or @code{AC_PROG_MKDIR_P} (@pxref{Particular Programs}).
13386 Posix does not clearly specify whether @samp{mkdir -p foo}
13387 should succeed when @file{foo} is a symbolic link to an already-existing
13388 directory.  The @acronym{GNU} Core Utilities 5.1.0 @command{mkdir}
13389 succeeds, but Solaris @command{mkdir} fails.
13391 Traditional @code{mkdir -p} implementations suffer from race conditions.
13392 For example, if you invoke @code{mkdir -p a/b} and @code{mkdir -p a/c}
13393 at the same time, both processes might detect that @file{a} is missing,
13394 one might create @file{a}, then the other might try to create @file{a}
13395 and fail with a @code{File exists} diagnostic.  The @acronym{GNU} Core
13396 Utilities (@samp{fileutils} version 4.1), Free@acronym{BSD} 5.0,
13397 Net@acronym{BSD} 2.0.2, and Open@acronym{BSD} 2.4 are known to be
13398 race-free when two processes invoke @code{mkdir -p} simultaneously, but
13399 earlier versions are vulnerable.  Solaris @command{mkdir} is still
13400 vulnerable as of Solaris 10, and other traditional Unix systems are
13401 probably vulnerable too.  This possible race is harmful in parallel
13402 builds when several Make rules call @code{mkdir -p} to
13403 construct directories.  You may use
13404 @code{install-sh -d} as a safe replacement, provided this script is
13405 recent enough; the copy shipped with Autoconf 2.60 and Automake 1.10 is
13406 OK, but copies from older versions are vulnerable.
13409 @item @command{mktemp}
13410 @c -------------------
13411 @prindex @command{mktemp}
13412 @cindex Creating temporary files
13413 Shell scripts can use temporary files safely with @command{mktemp}, but
13414 it does not exist on all systems.  A portable way to create a safe
13415 temporary file name is to create a temporary directory with mode 700 and
13416 use a file inside this directory.  Both methods prevent attackers from
13417 gaining control, though @command{mktemp} is far less likely to fail
13418 gratuitously under attack.
13420 Here is sample code to create a new temporary directory safely:
13422 @example
13423 # Create a temporary directory $tmp in $TMPDIR (default /tmp).
13424 # Use mktemp if possible; otherwise fall back on mkdir,
13425 # with $RANDOM to make collisions less likely.
13426 : $@{TMPDIR=/tmp@}
13428   tmp=`
13429     (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
13430   ` &&
13431   test -n "$tmp" && test -d "$tmp"
13432 @} || @{
13433   tmp=$TMPDIR/foo$$-$RANDOM
13434   (umask 077 && mkdir "$tmp")
13435 @} || exit $?
13436 @end example
13439 @item @command{mv}
13440 @c ---------------
13441 @prindex @command{mv}
13442 @cindex Moving open files
13443 The only portable options are @option{-f} and @option{-i}.
13445 Moving individual files between file systems is portable (it was in Unix
13446 version 6),
13447 but it is not always atomic: when doing @samp{mv new existing}, there's
13448 a critical section where neither the old nor the new version of
13449 @file{existing} actually exists.
13451 On some systems moving files from @file{/tmp} can sometimes cause
13452 undesirable (but perfectly valid) warnings, even if you created these
13453 files.  This is because @file{/tmp} belongs to a group that ordinary
13454 users are not members of, and files created in @file{/tmp} inherit
13455 the group of @file{/tmp}.  When the file is copied, @command{mv} issues
13456 a diagnostic without failing:
13458 @smallexample
13459 $ @kbd{touch /tmp/foo}
13460 $ @kbd{mv /tmp/foo .}
13461 @error{}mv: ./foo: set owner/group (was: 100/0): Operation not permitted
13462 $ @kbd{echo $?}
13464 $ @kbd{ls foo}
13466 @end smallexample
13468 @noindent
13469 This annoying behavior conforms to Posix, unfortunately.
13471 Moving directories across mount points is not portable, use @command{cp}
13472 and @command{rm}.
13474 @acronym{DOS} variants cannot rename or remove open files, and do not
13475 support commands like @samp{mv foo bar >foo}, even though this is
13476 perfectly portable among Posix hosts.
13479 @item @command{od}
13480 @c ---------------
13481 @prindex @command{od}
13483 In Mac OS X 10.3, @command{od} does not support the
13484 standard Posix options @option{-A}, @option{-j}, @option{-N}, or
13485 @option{-t}, or the @acronym{XSI} option @option{-s}.  The only
13486 supported Posix option is @option{-v}, and the only supported
13487 @acronym{XSI} options are those in @option{-bcdox}.  The @acronym{BSD}
13488 @command{hexdump} program can be used instead.
13490 This problem no longer exists in Mac OS X 10.4.3.
13493 @item @command{rm}
13494 @c ---------------
13495 @prindex @command{rm}
13496 The @option{-f} and @option{-r} options are portable.
13498 A file might not be be removed even if its parent directory is writable
13499 and searchable.  Many Posix hosts cannot remove a mount point, a named
13500 stream, a working directory, or a last link to a file that is being
13501 executed.
13503 @acronym{DOS} variants cannot rename or remove open files, and do not
13504 support commands like @samp{rm foo >foo}, even though this is
13505 perfectly portable among Posix hosts.
13508 @item @command{sed}
13509 @c ----------------
13510 @prindex @command{sed}
13511 Patterns should not include the separator (unless escaped), even as part
13512 of a character class.  In conformance with Posix, the Cray
13513 @command{sed} rejects @samp{s/[^/]*$//}: use @samp{s,[^/]*$,,}.
13515 Avoid empty patterns within parentheses (i.e., @samp{\(\)}).  Posix does
13516 not require support for empty patterns, and Unicos 9 @command{sed} rejects
13517 them.
13519 Unicos 9 @command{sed} loops endlessly on patterns like @samp{.*\n.*}.
13521 Sed scripts should not use branch labels longer than 8 characters and
13522 should not contain comments.  @acronym{HP-UX} sed has a limit of 99 commands
13523 (not counting @samp{:} commands) and
13524 48 labels, which can not be circumvented by using more than one script
13525 file.  It can execute up to 19 reads with the @samp{r} command per cycle.
13526 Solaris @command{/usr/ucb/sed} rejects usages that exceed an limit of
13527 about 6000 bytes for the internal representation of commands.
13529 Avoid redundant @samp{;}, as some @command{sed} implementations, such as
13530 Net@acronym{BSD} 1.4.2's, incorrectly try to interpret the second
13531 @samp{;} as a command:
13533 @example
13534 $ @kbd{echo a | sed 's/x/x/;;s/x/x/'}
13535 sed: 1: "s/x/x/;;s/x/x/": invalid command code ;
13536 @end example
13538 Input should not have unreasonably long lines, since some @command{sed}
13539 implementations have an input buffer limited to 4000 bytes.
13541 Portable @command{sed} regular expressions should use @samp{\} only to escape
13542 characters in the string @samp{$()*.0123456789[\^n@{@}}.  For example,
13543 alternation, @samp{\|}, is common but Posix does not require its
13544 support, so it should be avoided in portable scripts.  Solaris
13545 @command{sed} does not support alternation; e.g., @samp{sed '/a\|b/d'}
13546 deletes only lines that contain the literal string @samp{a|b}.
13547 Similarly, @samp{\+} and @samp{\?} should be avoided.
13549 Anchors (@samp{^} and @samp{$}) inside groups are not portable.
13551 Nested parenthesization in patterns (e.g., @samp{\(\(a*\)b*)\)}) is
13552 quite portable to current hosts, but was not supported by some ancient
13553 @command{sed} implementations like SVR3.
13555 Some @command{sed} implementations, e.g., Solaris,
13556 restrict the special role of the asterisk to one-character regular expressions.
13557 This may lead to unexpected behavior:
13559 @example
13560 $ @kbd{echo '1*23*4' | /usr/bin/sed 's/\(.\)*/x/g'}
13561 x2x4
13562 $ @kbd{echo '1*23*4' | /usr/xpg4/bin/sed 's/\(.\)*/x/g'}
13564 @end example
13566 The @option{-e} option is portable.
13567 Some people prefer to use it:
13569 @example
13570 sed -e '@var{command-1}' \
13571     -e '@var{command-2}'
13572 @end example
13574 @noindent
13575 as opposed to the equivalent:
13577 @example
13578 sed '
13579   @var{command-1}
13580   @var{command-2}
13582 @end example
13584 @noindent
13585 The following usage is sometimes equivalent:
13587 @example
13588 sed '@var{command-1};@var{command-2}'
13589 @end example
13591 but Posix says that this use of a semicolon has undefined effect if
13592 @var{command-1}'s verb is @samp{@{}, @samp{a}, @samp{b}, @samp{c},
13593 @samp{i}, @samp{r}, @samp{t}, @samp{w}, @samp{:}, or @samp{#}, so you
13594 should use semicolon only with simple scripts that do not use these
13595 verbs.
13597 Commands inside @{ @} brackets are further restricted.  Posix says that
13598 they cannot be preceded by addresses, @samp{!}, or @samp{;}, and that
13599 each command must be followed immediately by a newline, without any
13600 intervening blanks or semicolons.  The closing bracket must be alone on
13601 a line, other than white space preceding or following it.
13603 Contrary to yet another urban legend, you may portably use @samp{&} in
13604 the replacement part of the @code{s} command to mean ``what was
13605 matched''.  All descendants of Unix version 7 @command{sed}
13606 (at least; we
13607 don't have first hand experience with older @command{sed} implementations) have
13608 supported it.
13610 Posix requires that you must not have any white space between
13611 @samp{!} and the following command.  It is OK to have blanks between
13612 the address and the @samp{!}.  For instance, on Solaris:
13614 @example
13615 $ @kbd{echo "foo" | sed -n '/bar/ ! p'}
13616 @error{}Unrecognized command: /bar/ ! p
13617 $ @kbd{echo "foo" | sed -n '/bar/! p'}
13618 @error{}Unrecognized command: /bar/! p
13619 $ @kbd{echo "foo" | sed -n '/bar/ !p'}
13621 @end example
13623 Posix also says that you should not combine @samp{!} and @samp{;}.  If
13624 you use @samp{!}, it is best to put it on a command that is delimited by
13625 newlines rather than @samp{;}.
13627 Also note that Posix requires that the @samp{b}, @samp{t}, @samp{r}, and
13628 @samp{w} commands be followed by exactly one space before their argument.
13629 On the other hand, no white space is allowed between @samp{:} and the
13630 subsequent label name.
13632 @item @command{sed} (@samp{t})
13633 @c ---------------------------
13634 @prindex @command{sed} (@samp{t})
13635 Some old systems have @command{sed} that ``forget'' to reset their
13636 @samp{t} flag when starting a new cycle.  For instance on @acronym{MIPS
13637 RISC/OS}, and on @sc{irix} 5.3, if you run the following @command{sed}
13638 script (the line numbers are not actual part of the texts):
13640 @example
13641 s/keep me/kept/g  # a
13642 t end             # b
13643 s/.*/deleted/g    # c
13644 :end              # d
13645 @end example
13647 @noindent
13650 @example
13651 delete me         # 1
13652 delete me         # 2
13653 keep me           # 3
13654 delete me         # 4
13655 @end example
13657 @noindent
13658 you get
13660 @example
13661 deleted
13662 delete me
13663 kept
13664 deleted
13665 @end example
13667 @noindent
13668 instead of
13670 @example
13671 deleted
13672 deleted
13673 kept
13674 deleted
13675 @end example
13677 Why?  When processing line 1, (c) matches, therefore sets the @samp{t}
13678 flag, and the output is produced.  When processing
13679 line 2, the @samp{t} flag is still set (this is the bug).  Command (a)
13680 fails to match, but @command{sed} is not supposed to clear the @samp{t}
13681 flag when a substitution fails.  Command (b) sees that the flag is set,
13682 therefore it clears it, and jumps to (d), hence you get @samp{delete me}
13683 instead of @samp{deleted}.  When processing line (3), @samp{t} is clear,
13684 (a) matches, so the flag is set, hence (b) clears the flags and jumps.
13685 Finally, since the flag is clear, line 4 is processed properly.
13687 There are two things one should remember about @samp{t} in @command{sed}.
13688 Firstly, always remember that @samp{t} jumps if @emph{some} substitution
13689 succeeded, not only the immediately preceding substitution.  Therefore,
13690 always use a fake @samp{t clear} followed by a @samp{:clear} on the next
13691 line, to reset the @samp{t} flag where needed.
13693 Secondly, you cannot rely on @command{sed} to clear the flag at each new
13694 cycle.
13696 One portable implementation of the script above is:
13698 @example
13699 t clear
13700 :clear
13701 s/keep me/kept/g
13702 t end
13703 s/.*/deleted/g
13704 :end
13705 @end example
13707 @item @command{touch}
13708 @c ------------------
13709 @prindex @command{touch}
13710 @cindex timestamp resolution
13711 If you specify the desired timestamp (e.g., with the @option{-r}
13712 option), @command{touch} typically uses the @code{utime} or
13713 @code{utimes} system call, which can result in the same kind of
13714 timestamp truncation problems that @samp{cp -p} has.
13716 On ancient @acronym{BSD} systems, @command{touch} or any command that
13717 results in an empty file does not update the timestamps, so use a
13718 command like @command{echo} as a workaround.
13719 Also,
13720 @acronym{GNU} @command{touch} 3.16r (and presumably all before that)
13721 fails to work on SunOS 4.1.3 when the empty file is on an
13722 @acronym{NFS}-mounted 4.2 volume.
13723 However, these problems are no longer of practical concern.
13725 @end table
13728 @node Portable Make
13729 @chapter Portable Make Programming
13730 @prindex @command{make}
13731 @cindex Limitations of @command{make}
13733 Writing portable makefiles is an art.  Since a makefile's commands are
13734 executed by the shell, you must consider the shell portability issues
13735 already mentioned.  However, other issues are specific to @command{make}
13736 itself.
13738 @menu
13739 * $< in Ordinary Make Rules::   $< in ordinary rules
13740 * Failure in Make Rules::       Failing portably in rules
13741 * Special Chars in Names::      Special Characters in Macro Names
13742 * Backslash-Newline-Newline::   Empty last lines in macro definitions
13743 * Backslash-Newline Comments::  Spanning comments across line boundaries
13744 * Long Lines in Makefiles::     Line length limitations
13745 * Macros and Submakes::         @code{make macro=value} and submakes
13746 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
13747 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
13748 * Comments in Make Rules::      Other problems with Make comments
13749 * obj/ and Make::               Don't name a subdirectory @file{obj}
13750 * make -k Status::              Exit status of @samp{make -k}
13751 * VPATH and Make::              @code{VPATH} woes
13752 * Single Suffix Rules::         Single suffix rules and separated dependencies
13753 * Timestamps and Make::         Subsecond timestamp resolution
13754 @end menu
13756 @node $< in Ordinary Make Rules
13757 @section @code{$<} in Ordinary Make Rules
13759 Posix says that the @samp{$<} construct in makefiles can be
13760 used only in inference rules and in the @samp{.DEFAULT} rule; its
13761 meaning in ordinary rules is unspecified.  Solaris @command{make}
13762 for instance replaces it with the empty string.  Open@acronym{BSD} (3.0 and
13763 later) @command{make} diagnoses these uses and errors out.
13765 @node Failure in Make Rules
13766 @section Failure in Make Rules
13768 Since 1992 Posix has required that @command{make} must invoke
13769 each command with the equivalent of a @samp{sh -c} subshell.  However,
13770 many @command{make} implementations, including @acronym{BSD} make through 2004,
13771 use @samp{sh -e -c} instead, and the @option{-e} option causes the
13772 subshell to exit immediately if a subsidiary simple-command fails.  For
13773 example, the command @samp{touch T; rm -f U} always attempts to
13774 remove @file{U} with Posix make, but incompatible
13775 @command{make} implementations skip the @command{rm} if the
13776 @command{touch} fails.  One way to work around this is to reword the
13777 affected simple-commands so that they always succeed, e.g., @samp{touch
13778 T || :; rm -f U}.
13779 However, even this approach can run into common bugs in @acronym{BSD}
13780 implementations of the @option{-e} option of @command{sh} and
13781 @command{set} (@pxref{Limitations of Builtins}), so if you are worried
13782 about porting to buggy @acronym{BSD} shells it may be simpler to migrate
13783 complicated @command{make} actions into separate scripts.
13785 @node Special Chars in Names
13786 @section Special Characters in Make Macro Names
13788 Posix limits macro names to nonempty strings containing only
13789 @acronym{ASCII} letters and digits, @samp{.}, and @samp{_}.  Many
13790 @command{make} implementations allow a wider variety of characters, but
13791 portable makefiles should avoid them.  It is portable to start a name
13792 with a special character, e.g., @samp{$(.FOO)}.
13794 Some ancient @command{make} implementations don't support leading
13795 underscores in macro names.  An example is @acronym{NEWS-OS} 4.2R.
13797 @example
13798 $ @kbd{cat Makefile}
13799 _am_include = #
13800 _am_quote =
13801 all:; @@echo this is test
13802 $ @kbd{make}
13803 Make: Must be a separator on rules line 2.  Stop.
13804 $ @kbd{cat Makefile2}
13805 am_include = #
13806 am_quote =
13807 all:; @@echo this is test
13808 $ @kbd{make -f Makefile2}
13809 this is test
13810 @end example
13812 @noindent
13813 However, this problem is no longer of practical concern.
13815 @node Backslash-Newline-Newline
13816 @section Backslash-Newline-Newline in Make Macro Values
13818 @c  This has been seen on ia64 hpux 11.20, and on one hppa hpux 10.20,
13819 @c  but another hppa hpux 10.20 didn't have it.  Bob Proulx
13820 @c  <bob@proulx.com> thinks it was in hpux 8.0 too.
13821 On some versions of @acronym{HP-UX}, @command{make} reads multiple newlines
13822 following a backslash, continuing to the next non-empty line.  For
13823 example,
13825 @example
13826 FOO = one \
13828 BAR = two
13830 test:
13831         : FOO is "$(FOO)"
13832         : BAR is "$(BAR)"
13833 @end example
13835 @noindent
13836 shows @code{FOO} equal to @code{one BAR = two}.  Other implementations
13837 sensibly let a backslash continue only to the immediately following
13838 line.
13840 @node Backslash-Newline Comments
13841 @section Backslash-Newline in Make Comments
13843 According to Posix, Make comments start with @code{#}
13844 and continue until an unescaped newline is reached.
13846 @example
13847 $ @kbd{cat Makefile}
13848 # A = foo \
13849       bar \
13850       baz
13852 all:
13853         @@echo ok
13854 $ @kbd{make}   # GNU make
13856 @end example
13858 @noindent
13859 However this is not always the case.  Some implementations
13860 discard everything from @code{#} through the end of the line, ignoring any
13861 trailing backslash.
13863 @example
13864 $ @kbd{pmake}  # BSD make
13865 "Makefile", line 3: Need an operator
13866 Fatal errors encountered -- cannot continue
13867 @end example
13869 @noindent
13870 Therefore, if you want to comment out a multi-line definition, prefix each
13871 line with @code{#}, not only the first.
13873 @example
13874 # A = foo \
13875 #     bar \
13876 #     baz
13877 @end example
13879 @node Long Lines in Makefiles
13880 @section Long Lines in Makefiles
13882 Tru64 5.1's @command{make} has been reported to crash when given a
13883 makefile with lines longer than around 20 kB.  Earlier versions are
13884 reported to exit with @code{Line too long} diagnostics.
13886 @node Macros and Submakes
13887 @section @code{make macro=value} and Submakes
13889 A command-line variable definition such as @code{foo=bar} overrides any
13890 definition of @code{foo} in a makefile.  Some @command{make}
13891 implementations (such as @acronym{GNU} @command{make}) propagate this
13892 override to subsidiary invocations of @command{make}.  Some other
13893 implementations do not pass the substitution along to submakes.
13895 @example
13896 $ @kbd{cat Makefile}
13897 foo = foo
13898 one:
13899         @@echo $(foo)
13900         $(MAKE) two
13901 two:
13902         @@echo $(foo)
13903 $ @kbd{make foo=bar}            # GNU make 3.79.1
13905 make two
13906 make[1]: Entering directory `/home/adl'
13908 make[1]: Leaving directory `/home/adl'
13909 $ @kbd{pmake foo=bar}           # BSD make
13911 pmake two
13913 @end example
13915 You have a few possibilities if you do want the @code{foo=bar} override
13916 to propagate to submakes.  One is to use the @option{-e}
13917 option, which causes all environment variables to have precedence over
13918 the makefile macro definitions, and declare foo as an environment
13919 variable:
13921 @example
13922 $ @kbd{env foo=bar make -e}
13923 @end example
13925 The @option{-e} option is propagated to submakes automatically,
13926 and since the environment is inherited between @command{make}
13927 invocations, the @code{foo} macro is overridden in
13928 submakes as expected.
13930 This syntax (@code{foo=bar make -e}) is portable only when used
13931 outside of a makefile, for instance from a script or from the
13932 command line.  When run inside a @command{make} rule, @acronym{GNU}
13933 @command{make} 3.80 and prior versions forget to propagate the
13934 @option{-e} option to submakes.
13936 Moreover, using @option{-e} could have unexpected side effects if your
13937 environment contains some other macros usually defined by the
13938 makefile.  (See also the note about @code{make -e} and @code{SHELL}
13939 below.)
13941 Another way to propagate overrides to submakes is to do it
13942 manually, from your makefile:
13944 @example
13945 foo = foo
13946 one:
13947         @@echo $(foo)
13948         $(MAKE) foo=$(foo) two
13949 two:
13950         @@echo $(foo)
13951 @end example
13953 You need to foresee all macros that a user might want to override if
13954 you do that.
13956 @node The Make Macro MAKEFLAGS
13957 @section The Make Macro MAKEFLAGS
13958 @cindex @code{MAKEFLAGS} and @command{make}
13959 @cindex @command{make} and @code{MAKEFLAGS}
13961 Posix requires @command{make} to use @code{MAKEFLAGS} to affect the
13962 current and recursive invocations of make, but allows implementations
13963 several formats for the variable.  It is tricky to parse
13964 @code{$MAKEFLAGS} to determine whether @option{-s} for silent execution
13965 or @option{-k} for continued execution are in effect.  For example, you
13966 cannot assume that the first space-separated word in @code{$MAKEFLAGS}
13967 contains single-letter options, since in the Cygwin version of
13968 @acronym{GNU} @command{make} it is either @option{--unix} or
13969 @option{--win32} with the second word containing single-letter options.
13971 @example
13972 $ @kbd{cat Makefile}
13973 all:
13974         @@echo MAKEFLAGS = $(MAKEFLAGS)
13975 $ @kbd{make}
13976 MAKEFLAGS = --unix
13977 $ @kbd{make -k}
13978 MAKEFLAGS = --unix -k
13979 @end example
13981 @node The Make Macro SHELL
13982 @section The Make Macro @code{SHELL}
13983 @cindex @code{SHELL} and @command{make}
13984 @cindex @command{make} and @code{SHELL}
13986 Posix-compliant @command{make} internally uses the @code{$(SHELL)}
13987 macro to spawn shell processes and execute Make rules.  This
13988 is a builtin macro supplied by @command{make}, but it can be modified
13989 by a makefile or by a command-line argument.
13991 Not all @command{make} implementations define this @code{SHELL} macro.
13992 Tru64
13993 @command{make} is an example; this implementation always uses
13994 @code{/bin/sh}.  So it's a good idea to always define @code{SHELL} in
13995 your makefiles.  If you use Autoconf, do
13997 @example
13998 SHELL = @@SHELL@@
13999 @end example
14001 Do not force @code{SHELL = /bin/sh} because that is not correct
14002 everywhere.  For instance @acronym{DJGPP} lacks @code{/bin/sh}, and when
14003 its @acronym{GNU} @code{make} port sees such a setting it enters a special
14004 emulation mode where features like pipes and redirections are emulated
14005 on top of DOS's @command{command.com}.  Unfortunately this emulation is
14006 incomplete; for instance it does not handle command substitutions.
14007 On @acronym{DJGPP} @code{SHELL} should point to Bash.
14009 Posix-compliant @command{make} should never acquire the value of
14010 $(SHELL) from the environment, even when @code{make -e} is used
14011 (otherwise, think about what would happen to your rules if
14012 @code{SHELL=/bin/tcsh}).
14014 However not all @command{make} implementations have this exception.
14015 For instance it's not surprising that Tru64 @command{make} doesn't
14016 protect @code{SHELL}, since it doesn't use it.
14018 @example
14019 $ @kbd{cat Makefile}
14020 SHELL = /bin/sh
14021 FOO = foo
14022 all:
14023         @@echo $(SHELL)
14024         @@echo $(FOO)
14025 $ @kbd{env SHELL=/bin/tcsh FOO=bar make -e}   # Tru64 Make
14026 /bin/tcsh
14028 $ @kbd{env SHELL=/bin/tcsh FOO=bar gmake -e}  # GNU make
14029 /bin/sh
14031 @end example
14033 @node Comments in Make Rules
14034 @section Comments in Make Rules
14035 @cindex Comments in @file{Makefile} rules
14036 @cindex @file{Makefile} rules and comments
14038 Never put comments in a rule.
14040 Some @command{make} treat anything starting with a tab as a command for
14041 the current rule, even if the tab is immediately followed by a @code{#}.
14042 The @command{make} from Tru64 Unix V5.1 is one of them.  The following
14043 makefile runs @code{# foo} through the shell.
14045 @example
14046 all:
14047         # foo
14048 @end example
14050 @node obj/ and Make
14051 @section The @file{obj/} Subdirectory and Make
14052 @cindex @file{obj/}, subdirectory
14053 @cindex @acronym{BSD} @command{make} and @file{obj/}
14055 Never name one of your subdirectories @file{obj/} if you don't like
14056 surprises.
14058 If an @file{obj/} directory exists, @acronym{BSD} @command{make} enters it
14059 before reading the makefile.  Hence the makefile in the
14060 current directory is not read.
14062 @example
14063 $ @kbd{cat Makefile}
14064 all:
14065         echo Hello
14066 $ @kbd{cat obj/Makefile}
14067 all:
14068         echo World
14069 $ @kbd{make}      # GNU make
14070 echo Hello
14071 Hello
14072 $ @kbd{pmake}     # BSD make
14073 echo World
14074 World
14075 @end example
14077 @node make -k Status
14078 @section Exit Status of @code{make -k}
14079 @cindex @code{make -k}
14081 Do not rely on the exit status of @code{make -k}.  Some implementations
14082 reflect whether they encountered an error in their exit status; other
14083 implementations always succeed.
14085 @example
14086 $ @kbd{cat Makefile}
14087 all:
14088         false
14089 $ @kbd{make -k; echo exit status: $?}    # GNU make
14090 false
14091 make: *** [all] Error 1
14092 exit status: 2
14093 $ @kbd{pmake -k; echo exit status: $?}   # BSD make
14094 false
14095 *** Error code 1 (continuing)
14096 exit status: 0
14097 @end example
14099 @node VPATH and Make
14100 @section @code{VPATH} and Make
14101 @cindex @code{VPATH}
14103 Posix does not specify the semantics of @code{VPATH}.  Typically,
14104 @command{make} supports @code{VPATH}, but its implementation is not
14105 consistent.
14107 Autoconf and Automake support makefiles whose usages of @code{VPATH} are
14108 portable to recent-enough popular implementations of @command{make}, but
14109 to keep the resulting makefiles portable, a package's makefile
14110 prototypes must take the following issues into account.  These issues
14111 are complicated and are often poorly understood, and installers who use
14112 @code{VPATH} should expect to find many bugs in this area.  If you use
14113 @code{VPATH}, the simplest way to avoid these portability bugs is to
14114 stick with @acronym{GNU} @command{make}, since it is the most
14115 commonly-used @command{make} among Autoconf users.
14117 Here are some known issues with some @code{VPATH}
14118 implementations.
14120 @menu
14121 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
14122 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
14123 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
14124 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
14125 * Make Target Lookup::          More details about @code{VPATH} lookup
14126 @end menu
14128 @node VPATH and Double-colon
14129 @subsection @code{VPATH} and Double-colon Rules
14130 @cindex @code{VPATH} and double-colon rules
14131 @cindex double-colon rules and @code{VPATH}
14133 With ancient versions of Sun @command{make},
14134 any assignment to @code{VPATH} causes @command{make} to execute only
14135 the first set of double-colon rules.
14136 However, this problem is no longer of practical concern.
14138 @node $< in Explicit Rules
14139 @subsection @code{$<} Not Supported in Explicit Rules
14140 @cindex explicit rules, @code{$<}, and @code{VPATH}
14141 @cindex @code{$<}, explicit rules, and @code{VPATH}
14142 @cindex @code{VPATH}, explicit rules, and @code{$<}
14144 Using @code{$<} in explicit rules is not portable.
14145 The prerequisite file must be named explicitly in the rule.  If you want
14146 to find the prerequisite via a @code{VPATH} search, you have to code the
14147 whole thing manually.  @xref{Build Directories}.
14149 @node Automatic Rule Rewriting
14150 @subsection Automatic Rule Rewriting
14151 @cindex @code{VPATH} and automatic rule rewriting
14152 @cindex automatic rule rewriting and @code{VPATH}
14154 Some @command{make} implementations, such as Solaris and Tru64,
14155 search for prerequisites in @code{VPATH} and
14156 then rewrite each occurrence as a plain word in the rule.
14157 For instance:
14159 @example
14160 # This isn't portable to GNU make.
14161 VPATH = ../pkg/src
14162 f.c: if.c
14163         cp if.c f.c
14164 @end example
14166 @noindent
14167 executes @code{cp ../pkg/src/if.c f.c} if @file{if.c} is
14168 found in @file{../pkg/src}.
14170 However, this rule leads to real problems in practice.  For example, if
14171 the source directory contains an ordinary file named @file{test} that is
14172 used in a dependency, Solaris @command{make} rewrites commands like
14173 @samp{if test -r foo; @dots{}} to @samp{if ../pkg/src/test -r foo;
14174 @dots{}}, which is typically undesirable.  To avoid this problem,
14175 portable makefiles should never mention a source file whose name is that
14176 of a shell keyword like @file{until} or a shell command like
14177 @command{cat} or @command{gcc} or @command{test}.
14179 Because of these problems @acronym{GNU} @command{make} and many other
14180 @command{make} implementations do not rewrite commands, so portable
14181 makefiles should
14182 search @code{VPATH} manually.  It is tempting to write this:
14184 @smallexample
14185 # This isn't portable to Solaris make.
14186 VPATH = ../pkg/src
14187 f.c: if.c
14188         cp `test -f if.c || echo $(VPATH)/`if.c f.c
14189 @end smallexample
14191 @noindent
14192 However, the ``prerequisite rewriting'' still applies here.  So if
14193 @file{if.c} is in @file{../pkg/src}, Solaris and Tru64 @command{make}
14194 execute
14196 @smallexample
14197 cp `test -f ../pkg/src/if.c || echo ../pkg/src/`if.c f.c
14198 @end smallexample
14200 @noindent
14201 which reduces to
14203 @example
14204 cp if.c f.c
14205 @end example
14207 @noindent
14208 and thus fails.  Oops.
14210 A simple workaround, and good practice anyway, is to use @samp{$?} and
14211 @samp{$@@} when possible:
14213 @smallexample
14214 VPATH = ../pkg/src
14215 f.c: if.c
14216         cp $? $@@
14217 @end smallexample
14219 @noindent
14220 but this does not generalize well to commands with multiple
14221 prerequisites.  A more general workaround is to rewrite the rule so that
14222 the prerequisite @file{if.c} never appears as a plain word.  For
14223 example, these three rules would be safe, assuming @file{if.c} is in
14224 @file{../pkg/src} and the other files are in the working directory:
14226 @smallexample
14227 VPATH = ../pkg/src
14228 f.c: if.c f1.c
14229         cat `test -f ./if.c || echo $(VPATH)/`if.c f1.c >$@@
14230 g.c: if.c g1.c
14231         cat `test -f 'if.c' || echo $(VPATH)/`if.c g1.c >$@@
14232 h.c: if.c h1.c
14233         cat `test -f "if.c" || echo $(VPATH)/`if.c h1.c >$@@
14234 @end smallexample
14236 Things get worse when your prerequisites are in a macro.
14238 @example
14239 VPATH = ../pkg/src
14240 HEADERS = f.h g.h h.h
14241 install-HEADERS: $(HEADERS)
14242         for i in $(HEADERS); do \
14243           $(INSTALL) -m 644 \
14244             `test -f $$i || echo $(VPATH)/`$$i \
14245             $(DESTDIR)$(includedir)/$$i; \
14246         done
14247 @end example
14249 The above @code{install-HEADERS} rule is not Solaris-proof because @code{for
14250 i in $(HEADERS);} is expanded to @code{for i in f.h g.h h.h;}
14251 where @code{f.h} and @code{g.h} are plain words and are hence
14252 subject to @code{VPATH} adjustments.
14254 If the three files are in @file{../pkg/src}, the rule is run as:
14256 @example
14257 for i in ../pkg/src/f.h ../pkg/src/g.h h.h; do \
14258   install -m 644 \
14259      `test -f $i || echo ../pkg/src/`$i \
14260      /usr/local/include/$i; \
14261 done
14262 @end example
14264 where the two first @command{install} calls fail.  For instance,
14265 consider the @code{f.h} installation:
14267 @example
14268 install -m 644 \
14269   `test -f ../pkg/src/f.h || \
14270     echo ../pkg/src/ \
14271   `../pkg/src/f.h \
14272   /usr/local/include/../pkg/src/f.h;
14273 @end example
14275 @noindent
14276 It reduces to:
14278 @example
14279 install -m 644 \
14280   ../pkg/src/f.h \
14281   /usr/local/include/../pkg/src/f.h;
14282 @end example
14284 Note that the manual @code{VPATH} search did not cause any problems here;
14285 however this command installs @file{f.h} in an incorrect directory.
14287 Trying to quote @code{$(HEADERS)} in some way, as we did for
14288 @code{foo.c} a few makefiles ago, does not help:
14290 @example
14291 install-HEADERS: $(HEADERS)
14292         headers='$(HEADERS)'; \
14293         for i in $$headers; do \
14294           $(INSTALL) -m 644 \
14295             `test -f $$i || echo $(VPATH)/`$$i \
14296             $(DESTDIR)$(includedir)/$$i; \
14297         done
14298 @end example
14300 Now, @code{headers='$(HEADERS)'} macroexpands to:
14302 @example
14303 headers='f.h g.h h.h'
14304 @end example
14306 @noindent
14307 but @code{g.h} is still a plain word.  (As an aside, the idiom
14308 @code{headers='$(HEADERS)'; for i in $$headers;} is a good
14309 idea if @code{$(HEADERS)} can be empty, because some shells diagnose a
14310 syntax error on @code{for i in;}.)
14312 One workaround is to strip this unwanted @file{../pkg/src/} prefix manually:
14314 @example
14315 VPATH = ../pkg/src
14316 HEADERS = f.h g.h h.h
14317 install-HEADERS: $(HEADERS)
14318         headers='$(HEADERS)'; \
14319         for i in $$headers; do \
14320           i=`expr "$$i" : '$(VPATH)/\(.*\)'`;
14321           $(INSTALL) -m 644 \
14322             `test -f $$i || echo $(VPATH)/`$$i \
14323             $(DESTDIR)$(includedir)/$$i; \
14324         done
14325 @end example
14327 Automake does something similar.  However the above hack works only if
14328 the files listed in @code{HEADERS} are in the current directory or a
14329 subdirectory; they should not be in an enclosing directory.  If we had
14330 @code{HEADERS = ../f.h}, the above fragment would fail in a VPATH
14331 build with Tru64 @command{make}.  The reason is that not only does
14332 Tru64 @command{make} rewrite dependencies, but it also simplifies
14333 them.  Hence @code{../f.h} becomes @code{../pkg/f.h} instead of
14334 @code{../pkg/src/../f.h}.  This obviously defeats any attempt to strip
14335 a leading @file{../pkg/src/} component.
14337 The following example makes the behavior of Tru64 @command{make}
14338 more apparent.
14340 @example
14341 $ @kbd{cat Makefile}
14342 VPATH = sub
14343 all: ../foo
14344         echo ../foo
14345 $ @kbd{ls}
14346 Makefile foo
14347 $ @kbd{make}
14348 echo foo
14350 @end example
14352 @noindent
14353 Dependency @file{../foo} was found in @file{sub/../foo}, but Tru64
14354 @command{make} simplified it as @file{foo}.  (Note that the @file{sub/}
14355 directory does not even exist, this just means that the simplification
14356 occurred before the file was checked for.)
14358 For the record here is how SunOS 4 @command{make} behaves on this
14359 example.
14361 @smallexample
14362 $ @kbd{make}
14363 make: Fatal error: Don't know how to make target `../foo'
14364 $ @kbd{mkdir sub}
14365 $ @kbd{make}
14366 echo sub/../foo
14367 sub/../foo
14368 @end smallexample
14371 @node Tru64 Directory Magic
14372 @subsection Tru64 @command{make} Creates Prerequisite Directories Magically
14373 @cindex @code{VPATH} and prerequisite directories
14374 @cindex prerequisite directories and @code{VPATH}
14376 When a prerequisite is a subdirectory of @code{VPATH}, Tru64
14377 @command{make} creates it in the current directory.
14379 @example
14380 $ @kbd{mkdir -p foo/bar build}
14381 $ @kbd{cd build}
14382 $ @kbd{cat >Makefile <<END
14383 VPATH = ..
14384 all: foo/bar
14385 END}
14386 $ @kbd{make}
14387 mkdir foo
14388 mkdir foo/bar
14389 @end example
14391 This can yield unexpected results if a rule uses a manual @code{VPATH}
14392 search as presented before.
14394 @example
14395 VPATH = ..
14396 all : foo/bar
14397         command `test -d foo/bar || echo ../`foo/bar
14398 @end example
14400 The above @command{command} is run on the empty @file{foo/bar}
14401 directory that was created in the current directory.
14403 @node Make Target Lookup
14404 @subsection Make Target Lookup
14405 @cindex @code{VPATH}, resolving target pathnames
14407 @acronym{GNU} @command{make} uses a complex algorithm to decide when it
14408 should use files found via a @code{VPATH} search.  @xref{Search
14409 Algorithm, , How Directory Searches are Performed, make, The @acronym{GNU} Make
14410 Manual}.
14412 If a target needs to be rebuilt, @acronym{GNU} @command{make} discards the
14413 file name found during the @code{VPATH} search for this target, and
14414 builds the file locally using the file name given in the makefile.
14415 If a target does not need to be rebuilt, @acronym{GNU} @command{make} uses the
14416 file name found during the @code{VPATH} search.
14418 Other @command{make} implementations, like Net@acronym{BSD} @command{make}, are
14419 easier to describe: the file name found during the @code{VPATH} search
14420 is used whether the target needs to be rebuilt or not.  Therefore
14421 new files are created locally, but existing files are updated at their
14422 @code{VPATH} location.
14424 Open@acronym{BSD} and Free@acronym{BSD} @command{make}, however,
14425 never perform a
14426 @code{VPATH} search for a dependency that has an explicit rule.
14427 This is extremely annoying.
14429 When attempting a @code{VPATH} build for an autoconfiscated package
14430 (e.g., @code{mkdir build && cd build && ../configure}), this means
14431 @acronym{GNU}
14432 @command{make} builds everything locally in the @file{build}
14433 directory, while @acronym{BSD} @command{make} builds new files locally and
14434 updates existing files in the source directory.
14436 @example
14437 $ @kbd{cat Makefile}
14438 VPATH = ..
14439 all: foo.x bar.x
14440 foo.x bar.x: newer.x
14441         @@echo Building $@@
14442 $ @kbd{touch ../bar.x}
14443 $ @kbd{touch ../newer.x}
14444 $ @kbd{make}        # GNU make
14445 Building foo.x
14446 Building bar.x
14447 $ @kbd{pmake}       # NetBSD make
14448 Building foo.x
14449 Building ../bar.x
14450 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14451 Building foo.x
14452 Building bar.x
14453 $ @kbd{tmake}       # Tru64 make
14454 Building foo.x
14455 Building bar.x
14456 $ @kbd{touch ../bar.x}
14457 $ @kbd{make}        # GNU make
14458 Building foo.x
14459 $ @kbd{pmake}       # NetBSD make
14460 Building foo.x
14461 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14462 Building foo.x
14463 Building bar.x
14464 $ @kbd{tmake}       # Tru64 make
14465 Building foo.x
14466 Building bar.x
14467 @end example
14469 Note how Net@acronym{BSD} @command{make} updates @file{../bar.x} in its
14470 VPATH location, and how Free@acronym{BSD}, Open@acronym{BSD}, and Tru64
14471 @command{make} always
14472 update @file{bar.x}, even when @file{../bar.x} is up to date.
14474 Another point worth mentioning is that once @acronym{GNU} @command{make} has
14475 decided to ignore a @code{VPATH} file name (e.g., it ignored
14476 @file{../bar.x} in the above example) it continues to ignore it when
14477 the target occurs as a prerequisite of another rule.
14479 The following example shows that @acronym{GNU} @command{make} does not look up
14480 @file{bar.x} in @code{VPATH} before performing the @code{.x.y} rule,
14481 because it ignored the @code{VPATH} result of @file{bar.x} while running
14482 the @code{bar.x: newer.x} rule.
14484 @example
14485 $ @kbd{cat Makefile}
14486 VPATH = ..
14487 all: bar.y
14488 bar.x: newer.x
14489         @@echo Building $@@
14490 .SUFFIXES: .x .y
14491 .x.y:
14492         cp $< $@@
14493 $ @kbd{touch ../bar.x}
14494 $ @kbd{touch ../newer.x}
14495 $ @kbd{make}        # GNU make
14496 Building bar.x
14497 cp bar.x bar.y
14498 cp: cannot stat `bar.x': No such file or directory
14499 make: *** [bar.y] Error 1
14500 $ @kbd{pmake}       # NetBSD make
14501 Building ../bar.x
14502 cp ../bar.x bar.y
14503 $ @kbd{rm bar.y}
14504 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14505 echo Building bar.x
14506 cp bar.x bar.y
14507 cp: cannot stat `bar.x': No such file or directory
14508 *** Error code 1
14509 $ @kbd{tmake}       # Tru64 make
14510 Building bar.x
14511 cp: bar.x: No such file or directory
14512 *** Exit 1
14513 @end example
14515 Note that if you drop away the command from the @code{bar.x: newer.x}
14516 rule, @acronym{GNU} @command{make} magically starts to work: it
14517 knows that @code{bar.x} hasn't been updated, therefore it doesn't
14518 discard the result from @code{VPATH} (@file{../bar.x}) in succeeding
14519 uses.  Tru64 also works, but Free@acronym{BSD} and Open@acronym{BSD}
14520 still don't.
14522 @example
14523 $ @kbd{cat Makefile}
14524 VPATH = ..
14525 all: bar.y
14526 bar.x: newer.x
14527 .SUFFIXES: .x .y
14528 .x.y:
14529         cp $< $@@
14530 $ @kbd{touch ../bar.x}
14531 $ @kbd{touch ../newer.x}
14532 $ @kbd{make}        # GNU make
14533 cp ../bar.x bar.y
14534 $ @kbd{rm bar.y}
14535 $ @kbd{pmake}       # NetBSD make
14536 cp ../bar.x bar.y
14537 $ @kbd{rm bar.y}
14538 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
14539 cp bar.x bar.y
14540 cp: cannot stat `bar.x': No such file or directory
14541 *** Error code 1
14542 $ @kbd{tmake}       # Tru64 make
14543 cp ../bar.x bar.y
14544 @end example
14546 It seems the sole solution that would please every @command{make}
14547 implementation is to never rely on @code{VPATH} searches for targets.
14548 In other words, @code{VPATH} should be reserved to unbuilt sources.
14551 @node Single Suffix Rules
14552 @section Single Suffix Rules and Separated Dependencies
14553 @cindex Single Suffix Inference Rule
14554 @cindex Rule, Single Suffix Inference
14555 A @dfn{Single Suffix Rule} is basically a usual suffix (inference) rule
14556 (@samp{.from.to:}), but which @emph{destination} suffix is empty
14557 (@samp{.from:}).
14559 @cindex Separated Dependencies
14560 @dfn{Separated dependencies} simply refers to listing the prerequisite
14561 of a target, without defining a rule.  Usually one can list on the one
14562 hand side, the rules, and on the other hand side, the dependencies.
14564 Solaris @command{make} does not support separated dependencies for
14565 targets defined by single suffix rules:
14567 @example
14568 $ @kbd{cat Makefile}
14569 .SUFFIXES: .in
14570 foo: foo.in
14571 .in:
14572         cp $< $@@
14573 $ @kbd{touch foo.in}
14574 $ @kbd{make}
14575 $ @kbd{ls}
14576 Makefile  foo.in
14577 @end example
14579 @noindent
14580 while @acronym{GNU} Make does:
14582 @example
14583 $ @kbd{gmake}
14584 cp foo.in foo
14585 $ @kbd{ls}
14586 Makefile  foo       foo.in
14587 @end example
14589 Note it works without the @samp{foo: foo.in} dependency.
14591 @example
14592 $ @kbd{cat Makefile}
14593 .SUFFIXES: .in
14594 .in:
14595         cp $< $@@
14596 $ @kbd{make foo}
14597 cp foo.in foo
14598 @end example
14600 @noindent
14601 and it works with double suffix inference rules:
14603 @example
14604 $ @kbd{cat Makefile}
14605 foo.out: foo.in
14606 .SUFFIXES: .in .out
14607 .in.out:
14608         cp $< $@@
14609 $ @kbd{make}
14610 cp foo.in foo.out
14611 @end example
14613 As a result, in such a case, you have to write target rules.
14615 @node Timestamps and Make
14616 @section Timestamp Resolution and Make
14617 @cindex timestamp resolution
14618 Traditionally, file timestamps had 1-second resolution, and
14619 @command{make} used those timestamps to determine whether one file was
14620 newer than the other.  However, many modern file systems have
14621 timestamps with 1-nanosecond resolution.  Some @command{make}
14622 implementations look at the entire timestamp; others ignore the
14623 fractional part, which can lead to incorrect results.  Normally this
14624 is not a problem, but in some extreme cases you may need to use tricks
14625 like @samp{sleep 1} to work around timestamp truncation bugs.
14627 Commands like @samp{cp -p} and @samp{touch -r} typically do not copy
14628 file timestamps to their full resolutions (@pxref{Limitations of Usual
14629 Tools}).  Hence you should be wary of rules like this:
14631 @example
14632 dest: src
14633         cp -p src dest
14634 @end example
14636 as @file{dest} often appears to be older than @file{src} after the
14637 timestamp is truncated, and this can cause @command{make} to do
14638 needless rework the next time it is invoked.  To work around this
14639 problem, you can use a timestamp file, e.g.:
14641 @example
14642 dest-stamp: src
14643         cp -p src dest
14644         date >dest-stamp
14645 @end example
14650 @c ======================================== Portable C and C++ Programming
14652 @node Portable C and C++
14653 @chapter Portable C and C++ Programming
14654 @cindex Portable C and C++ programming
14656 C and C++ programs often use low-level features of the underlying
14657 system, and therefore are often more difficult to make portable to other
14658 platforms.
14660 Several standards have been developed to help make your programs more
14661 portable.  If you write programs with these standards in mind, you can
14662 have greater confidence that your programs work on a wide variety
14663 of systems.  @xref{Standards, , Language Standards Supported by
14664 @acronym{GCC}, gcc, Using the @acronym{GNU} Compiler Collection
14665 (@acronym{GCC})}, for a list of C-related
14666 standards.  Many programs also assume the
14667 @uref{http://www.opengroup.org/susv3, Posix standard}.
14669 Some old code is written to be portable to K&R C, which predates any C
14670 standard.  K&R C compilers are no longer of practical interest, though,
14671 and the rest of section assumes at least C89, the first C standard.
14673 Program portability is a huge topic, and this section can only briefly
14674 introduce common pitfalls.  @xref{System Portability, , Portability
14675 between System Types, standards, @acronym{GNU} Coding Standards}, for
14676 more information.
14678 @menu
14679 * Varieties of Unportability::  How to make your programs unportable
14680 * Integer Overflow::            When integers get too large
14681 * Null Pointers::               Properties of null pointers
14682 * Buffer Overruns::             Subscript errors and the like
14683 * Volatile Objects::            @code{volatile} and signals
14684 * Floating Point Portability::  Portable floating-point arithmetic
14685 * Exiting Portably::            Exiting and the exit status
14686 @end menu
14688 @node Varieties of Unportability
14689 @section Varieties of Unportability
14690 @cindex portability
14692 Autoconf tests and ordinary programs often need to test what is allowed
14693 on a system, and therefore they may need to deliberately exceed the
14694 boundaries of what the standards allow, if only to see whether an
14695 optional feature is present.  When you write such a program, you should
14696 keep in mind the difference between constraints, unspecified behavior,
14697 and undefined behavior.
14699 In C, a @dfn{constraint} is a rule that the compiler must enforce.  An
14700 example constraint is that C programs must not declare a bit-field with
14701 negative width.  Tests can therefore reliably assume that programs with
14702 negative-width bit-fields are rejected by a compiler that conforms
14703 to the standard.
14705 @dfn{Unspecified behavior} is valid behavior, where the standard allows
14706 multiple possibilities.  For example, the order of evaluation of
14707 function arguments is unspecified.  Some unspecified behavior is
14708 @dfn{implementation-defined}, i.e., documented by the implementation,
14709 but since Autoconf tests cannot read the documentation they cannot
14710 distinguish between implementation-defined and other unspecified
14711 behavior.  It is common for Autoconf tests to probe implementations to
14712 determine otherwise-unspecified behavior.
14714 @dfn{Undefined behavior} is invalid behavior, where the standard allows
14715 the implementation to do anything it pleases.  For example,
14716 dereferencing a null pointer leads to undefined behavior.  If possible,
14717 test programs should avoid undefined behavior, since a program with
14718 undefined behavior might succeed on a test that should fail.
14720 The above rules apply to programs that are intended to conform to the
14721 standard.  However, strictly-conforming programs are quite rare, since
14722 the standards are so limiting.  A major goal of Autoconf is to support
14723 programs that use implementation features not described by the standard,
14724 and it is fairly common for test programs to violate the above rules, if
14725 the programs work well enough in practice.
14727 @node Integer Overflow
14728 @section Integer Overflow
14729 @cindex overflow, arithmetic
14731 In C, signed integer overflow leads to undefined behavior.  However,
14732 many programs and Autoconf tests assume that signed integer overflow after
14733 addition, subtraction, or multiplication silently
14734 wraps around modulo a power of two, using two's complement arithmetic,
14735 so long as you cast the resulting value
14736 to an integer type or store it into an integer variable.  Such programs
14737 are portable to the vast majority of modern platforms.  However, signed
14738 integer division is not always harmless: for example, on CPUs of the
14739 i386 family, dividing @code{INT_MIN} by @code{-1} yields a SIGFPE signal
14740 which by default terminates the program.  Worse, taking the remainder
14741 of these two values typically yields the same signal on these CPUs,
14742 even though the C standard requires @code{INT_MIN % -1} to yield zero
14743 because the expression does not overflow.
14745 @acronym{GCC} users might consider using the
14746 @option{-ftrapv} option if they are worried about porting their code to
14747 the rare platforms where signed integer overflow does not wrap around
14748 after addition, subtraction, or multiplication.
14750 Unsigned integer overflow reliably wraps around modulo the word size.
14751 This is guaranteed by the C standard and is portable in practice.
14753 @node Null Pointers
14754 @section Properties of Null Pointers
14755 @cindex null pointers
14757 Most modern hosts reliably fail when you attempt to dereference a null
14758 pointer.
14760 On almost all modern hosts, null pointers use an all-bits-zero internal
14761 representation, so you can reliably use @code{memset} with 0 to set all
14762 the pointers in an array to null values.
14764 If @code{p} is a null pointer to an object type, the C expression
14765 @code{p + 0} always evaluates to @code{p} on modern hosts, even though
14766 the standard says that it has undefined behavior.
14768 @node Buffer Overruns
14769 @section Buffer Overruns and Subscript Errors
14770 @cindex buffer overruns
14772 Buffer overruns and subscript errors are the most common dangerous
14773 errors in C programs.  They result in undefined behavior because storing
14774 outside an array typically modifies storage that is used by some other
14775 object, and most modern systems lack runtime checks to catch these
14776 errors.  Programs should not rely on buffer overruns being caught.
14778 There is one exception to the usual rule that a portable program cannot
14779 address outside an array.  In C, it is valid to compute the address just
14780 past an object, e.g., @code{&a[N]} where @code{a} has @code{N} elements,
14781 so long as you do not dereference the resulting pointer.  But it is not
14782 valid to compute the address just before an object, e.g., @code{&a[-1]};
14783 nor is it valid to compute two past the end, e.g., @code{&a[N+1]}.  On
14784 most platforms @code{&a[-1] < &a[0] && &a[N] < &a[N+1]}, but this is not
14785 reliable in general, and it is usually easy enough to avoid the
14786 potential portability problem, e.g., by allocating an extra unused array
14787 element at the start or end.
14789 @uref{http://valgrind.org/, Valgrind} can catch many overruns.
14790 @acronym{GCC}
14791 users might also consider using the @option{-fmudflap} option to catch
14792 overruns.
14794 Buffer overruns are usually caused by off-by-one errors, but there are
14795 more subtle ways to get them.
14797 Using @code{int} values to index into an array or compute array sizes
14798 causes problems on typical 64-bit hosts where an array index might
14799 be @math{2^31} or larger.  Index values of type @code{size_t} avoid this
14800 problem, but cannot be negative.  Index values of type @code{ptrdiff_t}
14801 are signed, and are wide enough in practice.
14803 If you add or multiply two numbers to calculate an array size, e.g.,
14804 @code{malloc (x * sizeof y + z)}, havoc ensues if the addition or
14805 multiplication overflows.
14807 Many implementations of the @code{alloca} function silently misbehave
14808 and can generate buffer overflows if given sizes that are too large.
14809 The size limits are implementation dependent, but are at least 4000
14810 bytes on all platforms that we know about.
14812 The standard functions @code{asctime}, @code{asctime_r}, @code{ctime},
14813 @code{ctime_r}, and @code{gets} are prone to buffer overflows, and
14814 portable code should not use them unless the inputs are known to be
14815 within certain limits.  The time-related functions can overflow their
14816 buffers if given timestamps out of range (e.g., a year less than -999
14817 or greater than 9999).  Time-related buffer overflows cannot happen with
14818 recent-enough versions of the @acronym{GNU} C library, but are possible
14819 with other
14820 implementations.  The @code{gets} function is the worst, since it almost
14821 invariably overflows its buffer when presented with an input line larger
14822 than the buffer.
14824 @node Volatile Objects
14825 @section Volatile Objects
14826 @cindex volatile objects
14828 The keyword @code{volatile} is often misunderstood in portable code.
14829 Its use inhibits some memory-access optimizations, but programmers often
14830 wish that it had a different meaning that it actually does.
14832 One area of confusion is the distinction between a volatile objects and
14833 volatile lvalues.  From the C standard's point of view, a volatile
14834 object has externally visible behavior.  You can think of such objects
14835 as having little oscilloscope probes attached to them, so that the user
14836 can observe every access to them, just as the user can observe data
14837 written to output files.  This is not true of ordinary objects accessed
14838 via volatile lvalues; only volatile objects can be observed by the user.
14839 Hence in general it does not help to use pointer-to-volatile to control
14840 access to ordinary objects.  For example:
14842 @example
14843 /* Declare and access a volatile object.
14844    The keyword 'volatile' has an effect here.  */
14845 static int volatile x;
14846 x = 1;
14848 /* Access two ordinary objects via a volatile lvalue.
14849    The keyword 'volatile' has no effect here.  */
14850 int y;
14851 int volatile *p;
14852 p = &y;
14853 *p = 1;
14854 p = malloc (sizeof (int));
14855 *p = 1;
14856 @end example
14858 Programmers often wish that @code{volatile} meant ``Perform the memory
14859 access here and now, without merging several memory accesses, without
14860 changing the memory word size width, and without reordering.''  But the
14861 C standard does not require this.  For volatile @emph{objects}, accesses
14862 must be done before the next sequence point; but otherwise merging,
14863 reordering, and word-size change is allowed.  Worse, in general volatile
14864 @emph{lvalues} provide no more guarantees than nonvolatile lvalues, when
14865 the underlying objects are nonvolatile.
14867 Even when accessing volatile objects, the C standard allows only
14868 extremely limited signal handlers: the behavior is undefined if a signal
14869 handler reads any nonlocal object, or writes to any nonlocal object
14870 whose type is not @code{sig_atomic_t volatile}, or calls any standard
14871 library function other than @code{abort}, @code{signal}, and (if C99)
14872 @code{_Exit}.  Hence C compilers do not need to worry about a signal
14873 disturbing ordinary computation, unless the computation accesses a
14874 @code{sig_atomic_t volatile} object that is not a local variable.  Posix
14875 adds to the list of library functions callable from a portable signal
14876 handler, but otherwise is like the C standard in this area.
14878 Some C implementations allow memory-access optimizations within each
14879 translation unit, such that actual behavior agrees with the behavior
14880 required by the standard only when calling a function in some other
14881 translation unit, and a signal handler acts like it was called from a
14882 different translation unit.  The C standard hints that in these
14883 implementations, objects referred to by signal handlers ``would require
14884 explicit specification of @code{volatile} storage, as well as other
14885 implementation-defined restrictions.''  But unfortunately even for this
14886 special case these other restrictions are often not documented well.
14887 @xref{Volatiles, , When is a Volatile Object Accessed?, gcc, Using the
14888 @acronym{GNU} Compiler Collection (@acronym{GCC})}, for some
14889 restrictions imposed by @acronym{GCC}.  @xref{Defining Handlers, ,
14890 Defining Signal Handlers, libc, The @acronym{GNU} C Library}, for some
14891 restrictions imposed by the @acronym{GNU} C library.  Restrictions
14892 differ on other platforms.
14894 If possible, it is best to use a signal handler that fits within the
14895 limits imposed by the C and Posix standards.  If this is not practical,
14896 then a signal handler should access only volatile objects, and should
14897 not assume that volatile objects larger than a machine word have an
14898 internally consistent state.  If that is not practical either, then it
14899 may be difficult to write portable code, and it is not clear whether
14900 using volatile lvalues will help much.
14902 For @code{volatile}, C++ has the same problems that C does.
14903 Multithreaded applications have even more problems with @code{volatile},
14904 but they are beyond the scope of this section.
14906 @node Floating Point Portability
14907 @section Floating Point Portability
14908 @cindex floating point
14910 Almost all modern systems use IEEE-754 floating point, and it is safe to
14911 assume IEEE-754 in most portable code these days.  For more information,
14912 please see David Goldberg's classic paper
14913 @uref{http://www.validlab.com/goldberg/paper.pdf, What Every Computer
14914 Scientist Should Know About Floating-Point Arithmetic}.
14916 @node Exiting Portably
14917 @section Exiting Portably
14918 @cindex exiting portably
14920 A C or C++ program can exit with status @var{N} by returning
14921 @var{N} from the @code{main} function.  Portable programs are supposed
14922 to exit either with status 0 or @code{EXIT_SUCCESS} to succeed, or with
14923 status @code{EXIT_FAILURE} to fail, but in practice it is portable to
14924 fail by exiting with status 1, and test programs that assume Posix can
14925 fail by exiting with status values from 1 through 255.  Programs on
14926 SunOS 2.0 (1985) through 3.5.2 (1988) incorrectly exited with zero
14927 status when @code{main} returned nonzero, but ancient systems like these
14928 are no longer of practical concern.
14930 A program can also exit with status @var{N} by passing @var{N} to the
14931 @code{exit} function, and a program can fail by calling the @code{abort}
14932 function.  If a program is specialized to just some platforms, it can fail
14933 by calling functions specific to those platforms, e.g., @code{_exit}
14934 (Posix) and @code{_Exit} (C99).  However, like other functions, an exit
14935 function should be declared, typically by including a header.  For
14936 example, if a C program calls @code{exit}, it should include @file{stdlib.h}
14937 either directly or via the default includes (@pxref{Default Includes}).
14939 A program can fail due to undefined behavior such as dereferencing a null
14940 pointer, but this is not recommended as undefined behavior allows an
14941 implementation to do whatever it pleases and this includes exiting
14942 successfully.
14945 @c ================================================== Manual Configuration
14947 @node Manual Configuration
14948 @chapter Manual Configuration
14950 A few kinds of features can't be guessed automatically by running test
14951 programs.  For example, the details of the object-file format, or
14952 special options that need to be passed to the compiler or linker.  You
14953 can check for such features using ad-hoc means, such as having
14954 @command{configure} check the output of the @code{uname} program, or
14955 looking for libraries that are unique to particular systems.  However,
14956 Autoconf provides a uniform method for handling unguessable features.
14958 @menu
14959 * Specifying Names::            Specifying the system type
14960 * Canonicalizing::              Getting the canonical system type
14961 * Using System Type::           What to do with the system type
14962 @end menu
14964 @node Specifying Names
14965 @section Specifying the System Type
14966 @cindex System type
14968 Like other @acronym{GNU} @command{configure} scripts, Autoconf-generated
14969 @command{configure} scripts can make decisions based on a canonical name
14970 for the system type, which has the form:
14971 @samp{@var{cpu}-@var{vendor}-@var{os}}, where @var{os} can be
14972 @samp{@var{system}} or @samp{@var{kernel}-@var{system}}
14974 @command{configure} can usually guess the canonical name for the type of
14975 system it's running on.  To do so it runs a script called
14976 @command{config.guess}, which infers the name using the @code{uname}
14977 command or symbols predefined by the C preprocessor.
14979 Alternately, the user can specify the system type with command line
14980 arguments to @command{configure}.  Doing so is necessary when
14981 cross-compiling.  In the most complex case of cross-compiling, three
14982 system types are involved.  The options to specify them are:
14984 @table @option
14985 @item --build=@var{build-type}
14986 the type of system on which the package is being configured and
14987 compiled.  It defaults to the result of running @command{config.guess}.
14989 @item --host=@var{host-type}
14990 the type of system on which the package runs.  By default it is the
14991 same as the build machine.  Specifying it enables the cross-compilation
14992 mode.
14994 @item --target=@var{target-type}
14995 the type of system for which any compiler tools in the package
14996 produce code (rarely needed).  By default, it is the same as host.
14997 @end table
14999 If you mean to override the result of @command{config.guess}, use
15000 @option{--build}, not @option{--host}, since the latter enables
15001 cross-compilation.  For historical reasons, passing @option{--host} also
15002 changes the build type.  Therefore, whenever you specify @option{--host},
15003 be sure to specify @option{--build} too; this will be fixed in the
15004 future.  So, to enter cross-compilation mode, use a command like this
15006 @example
15007 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
15008 @end example
15010 @noindent
15011 Note that if you do not specify @option{--host}, @command{configure}
15012 fails if it can't run the code generated by the specified compiler.  For
15013 example, configuring as follows fails:
15015 @example
15016 ./configure CC=m68k-coff-gcc
15017 @end example
15019 In the future, when cross-compiling Autoconf will @emph{not}
15020 accept tools (compilers, linkers, assemblers) whose name is not
15021 prefixed with the host type.  The only case when this may be
15022 useful is when you really are not cross-compiling, but only
15023 building for a least-common-denominator architecture: an example
15024 is building for @code{i386-pc-linux-gnu} while running on an
15025 @code{i686-pc-linux-gnu} architecture.  In this case, some particular
15026 pairs might be similar enough to let you get away with the system
15027 compilers, but in general the compiler might make bogus assumptions
15028 on the host: if you know what you are doing, please create symbolic
15029 links from the host compiler to the build compiler.
15031 @cindex @command{config.sub}
15032 @command{configure} recognizes short aliases for many system types; for
15033 example, @samp{decstation} can be used instead of
15034 @samp{mips-dec-ultrix4.2}.  @command{configure} runs a script called
15035 @command{config.sub} to canonicalize system type aliases.
15037 This section deliberately omits the description of the obsolete
15038 interface; see @ref{Hosts and Cross-Compilation}.
15041 @node Canonicalizing
15042 @section Getting the Canonical System Type
15043 @cindex System type
15044 @cindex Canonical system type
15046 The following macros make the system type available to @command{configure}
15047 scripts.
15049 @ovindex build_alias
15050 @ovindex host_alias
15051 @ovindex target_alias
15053 The variables @samp{build_alias}, @samp{host_alias}, and
15054 @samp{target_alias} are always exactly the arguments of @option{--build},
15055 @option{--host}, and @option{--target}; in particular, they are left empty
15056 if the user did not use them, even if the corresponding
15057 @code{AC_CANONICAL} macro was run.  Any configure script may use these
15058 variables anywhere.  These are the variables that should be used when in
15059 interaction with the user.
15061 If you need to recognize some special environments based on their system
15062 type, run the following macros to get canonical system names.  These
15063 variables are not set before the macro call.
15065 If you use these macros, you must distribute @command{config.guess} and
15066 @command{config.sub} along with your source code.  @xref{Output}, for
15067 information about the @code{AC_CONFIG_AUX_DIR} macro which you can use
15068 to control in which directory @command{configure} looks for those scripts.
15071 @defmac AC_CANONICAL_BUILD
15072 @acindex{CANONICAL_BUILD}
15073 @ovindex build
15074 @ovindex build_cpu
15075 @ovindex build_vendor
15076 @ovindex build_os
15077 Compute the canonical build-system type variable, @code{build}, and its
15078 three individual parts @code{build_cpu}, @code{build_vendor}, and
15079 @code{build_os}.
15081 If @option{--build} was specified, then @code{build} is the
15082 canonicalization of @code{build_alias} by @command{config.sub},
15083 otherwise it is determined by the shell script @command{config.guess}.
15084 @end defmac
15086 @defmac AC_CANONICAL_HOST
15087 @acindex{CANONICAL_HOST}
15088 @ovindex host
15089 @ovindex host_cpu
15090 @ovindex host_vendor
15091 @ovindex host_os
15092 Compute the canonical host-system type variable, @code{host}, and its
15093 three individual parts @code{host_cpu}, @code{host_vendor}, and
15094 @code{host_os}.
15096 If @option{--host} was specified, then @code{host} is the
15097 canonicalization of @code{host_alias} by @command{config.sub},
15098 otherwise it defaults to @code{build}.
15099 @end defmac
15101 @defmac AC_CANONICAL_TARGET
15102 @acindex{CANONICAL_TARGET}
15103 @ovindex target
15104 @ovindex target_cpu
15105 @ovindex target_vendor
15106 @ovindex target_os
15107 Compute the canonical target-system type variable, @code{target}, and its
15108 three individual parts @code{target_cpu}, @code{target_vendor}, and
15109 @code{target_os}.
15111 If @option{--target} was specified, then @code{target} is the
15112 canonicalization of @code{target_alias} by @command{config.sub},
15113 otherwise it defaults to @code{host}.
15114 @end defmac
15116 Note that there can be artifacts due to the backward compatibility
15117 code.  See @xref{Hosts and Cross-Compilation}, for more.
15119 @node Using System Type
15120 @section Using the System Type
15122 In @file{configure.ac} the system type is generally used by one or more
15123 @code{case} statements to select system-specifics.  Shell wildcards can
15124 be used to match a group of system types.
15126 For example, an extra assembler code object file could be chosen, giving
15127 access to a CPU cycle counter register.  @code{$(CYCLE_OBJ)} in the
15128 following would be used in a makefile to add the object to a
15129 program or library.
15131 @example
15132 case $host in
15133   alpha*-*-*) CYCLE_OBJ=rpcc.o ;;
15134   i?86-*-*)   CYCLE_OBJ=rdtsc.o ;;
15135   *)          CYCLE_OBJ= ;;
15136 esac
15137 AC_SUBST([CYCLE_OBJ])
15138 @end example
15140 @code{AC_CONFIG_LINKS} (@pxref{Configuration Links}) is another good way
15141 to select variant source files, for example optimized code for some
15142 CPUs.  The configured CPU type doesn't always indicate exact CPU types,
15143 so some runtime capability checks may be necessary too.
15145 @example
15146 case $host in
15147   alpha*-*-*)   AC_CONFIG_LINKS([dither.c:alpha/dither.c]) ;;
15148   powerpc*-*-*) AC_CONFIG_LINKS([dither.c:powerpc/dither.c]) ;;
15149   *-*-*)        AC_CONFIG_LINKS([dither.c:generic/dither.c]) ;;
15150 esac
15151 @end example
15153 The host system type can also be used to find cross-compilation tools
15154 with @code{AC_CHECK_TOOL} (@pxref{Generic Programs}).
15156 The above examples all show @samp{$host}, since this is where the code
15157 is going to run.  Only rarely is it necessary to test @samp{$build}
15158 (which is where the build is being done).
15160 Whenever you're tempted to use @samp{$host} it's worth considering
15161 whether some sort of probe would be better.  New system types come along
15162 periodically or previously missing features are added.  Well-written
15163 probes can adapt themselves to such things, but hard-coded lists of
15164 names can't.  Here are some guidelines,
15166 @itemize @bullet
15167 @item
15168 Availability of libraries and library functions should always be checked
15169 by probing.
15170 @item
15171 Variant behavior of system calls is best identified with runtime tests
15172 if possible, but bug workarounds or obscure difficulties might have to
15173 be driven from @samp{$host}.
15174 @item
15175 Assembler code is inevitably highly CPU-specific and is best selected
15176 according to @samp{$host_cpu}.
15177 @item
15178 Assembler variations like underscore prefix on globals or ELF versus
15179 COFF type directives are however best determined by probing, perhaps
15180 even examining the compiler output.
15181 @end itemize
15183 @samp{$target} is for use by a package creating a compiler or similar.
15184 For ordinary packages it's meaningless and should not be used.  It
15185 indicates what the created compiler should generate code for, if it can
15186 cross-compile.  @samp{$target} generally selects various hard-coded CPU
15187 and system conventions, since usually the compiler or tools under
15188 construction themselves determine how the target works.
15191 @c ===================================================== Site Configuration.
15193 @node Site Configuration
15194 @chapter Site Configuration
15196 @command{configure} scripts support several kinds of local configuration
15197 decisions.  There are ways for users to specify where external software
15198 packages are, include or exclude optional features, install programs
15199 under modified names, and set default values for @command{configure}
15200 options.
15202 @menu
15203 * Help Formatting::             Customizing @samp{configure --help}
15204 * External Software::           Working with other optional software
15205 * Package Options::             Selecting optional features
15206 * Pretty Help Strings::         Formatting help string
15207 * Site Details::                Configuring site details
15208 * Transforming Names::          Changing program names when installing
15209 * Site Defaults::               Giving @command{configure} local defaults
15210 @end menu
15212 @node Help Formatting
15213 @section Controlling Help Output
15215 Users consult @samp{configure --help} to learn of configuration
15216 decisions specific to your package.  By default, @command{configure}
15217 breaks this output into sections for each type of option; within each
15218 section, help strings appear in the order @file{configure.ac} defines
15219 them:
15221 @example
15222 Optional Features:
15223   @dots{}
15224   --enable-bar            include bar
15226 Optional Packages:
15227   @dots{}
15228   --with-foo              use foo
15229 @end example
15231 @defmac AC_PRESERVE_HELP_ORDER
15232 @acindex{PRESERVE_HELP_ORDER}
15234 Request an alternate @option{--help} format, in which options of all
15235 types appear together, in the order defined.  Call this macro before any
15236 @code{AC_ARG_ENABLE} or @code{AC_ARG_WITH}.
15238 @example
15239 Optional Features and Packages:
15240   @dots{}
15241   --enable-bar            include bar
15242   --with-foo              use foo
15243 @end example
15245 @end defmac
15247 @node External Software
15248 @section Working With External Software
15249 @cindex External software
15251 Some packages require, or can optionally use, other software packages
15252 that are already installed.  The user can give @command{configure}
15253 command line options to specify which such external software to use.
15254 The options have one of these forms:
15256 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
15257 @c awful.
15258 @example
15259 --with-@var{package}[=@var{arg}]
15260 --without-@var{package}
15261 @end example
15263 For example, @option{--with-gnu-ld} means work with the @acronym{GNU} linker
15264 instead of some other linker.  @option{--with-x} means work with The X
15265 Window System.
15267 The user can give an argument by following the package name with
15268 @samp{=} and the argument.  Giving an argument of @samp{no} is for
15269 packages that are used by default; it says to @emph{not} use the
15270 package.  An argument that is neither @samp{yes} nor @samp{no} could
15271 include a name or number of a version of the other package, to specify
15272 more precisely which other package this program is supposed to work
15273 with.  If no argument is given, it defaults to @samp{yes}.
15274 @option{--without-@var{package}} is equivalent to
15275 @option{--with-@var{package}=no}.
15277 @command{configure} scripts do not complain about
15278 @option{--with-@var{package}} options that they do not support.  This
15279 behavior permits configuring a source tree containing multiple packages
15280 with a top-level @command{configure} script when the packages support
15281 different options, without spurious error messages about options that
15282 some of the packages support.  An unfortunate side effect is that option
15283 spelling errors are not diagnosed.  No better approach to this problem
15284 has been suggested so far.
15286 For each external software package that may be used, @file{configure.ac}
15287 should call @code{AC_ARG_WITH} to detect whether the @command{configure}
15288 user asked to use it.  Whether each package is used or not by default,
15289 and which arguments are valid, is up to you.
15291 @defmac AC_ARG_WITH (@var{package}, @var{help-string}, @ovar{action-if-given}, @ovar{action-if-not-given})
15292 @acindex{ARG_WITH}
15293 If the user gave @command{configure} the option @option{--with-@var{package}}
15294 or @option{--without-@var{package}}, run shell commands
15295 @var{action-if-given}.  If neither option was given, run shell commands
15296 @var{action-if-not-given}.  The name @var{package} indicates another
15297 software package that this program should work with.  It should consist
15298 only of alphanumeric characters and dashes.
15300 The option's argument is available to the shell commands
15301 @var{action-if-given} in the shell variable @code{withval}, which is
15302 actually just the value of the shell variable @code{with_@var{package}},
15303 with any @option{-} characters changed into @samp{_}.  You may use that
15304 variable instead, if you wish.
15306 The argument @var{help-string} is a description of the option that
15307 looks like this:
15308 @example
15309   --with-readline         support fancy command line editing
15310 @end example
15312 @noindent
15313 @var{help-string} may be more than one line long, if more detail is
15314 needed.  Just make sure the columns line up in @samp{configure
15315 --help}.  Avoid tabs in the help string.  You'll need to enclose the
15316 help string in @samp{[} and @samp{]} in order to produce the leading
15317 blanks.
15319 You should format your @var{help-string} with the macro
15320 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
15322 The following example shows how to use the @code{AC_ARG_WITH} macro in
15323 a common situation.  You want to let the user decide whether to enable
15324 support for an external library (e.g., the readline library); if the user
15325 specified neither @option{--with-readline} nor @option{--without-readline},
15326 you want to enable support for readline only if the library is available
15327 on the system.
15329 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
15330 @example
15331 AC_ARG_WITH([readline],
15332   [AS_HELP_STRING([--with-readline],
15333     [support fancy command line editing @@<:@@default=check@@:>@@])],
15334   [],
15335   [with_readline=check])
15337 LIBREADLINE=
15338 AS_IF([test "x$with_readline" != xno],
15339   [AC_CHECK_LIB([readline], [main],
15340     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
15341      AC_DEFINE([HAVE_LIBREADLINE], [1],
15342                [Define if you have libreadline])
15343     ],
15344     [if test "x$with_readline" != xcheck; then
15345        AC_MSG_FAILURE(
15346          [--with-readline was given, but test for readline failed])
15347      fi
15348     ], -lncurses)])
15349 @end example
15351 The next example shows how to use @code{AC_ARG_WITH} to give the user the
15352 possibility to enable support for the readline library, in case it is still
15353 experimental and not well tested, and is therefore disabled by default.
15355 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
15356 @example
15357 AC_ARG_WITH([readline],
15358   [AS_HELP_STRING([--with-readline],
15359     [enable experimental support for readline])],
15360   [],
15361   [with_readline=no])
15363 LIBREADLINE=
15364 AS_IF([test "x$with_readline" != xno],
15365   [AC_CHECK_LIB([readline], [main],
15366     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
15367      AC_DEFINE([HAVE_LIBREADLINE], [1],
15368                [Define if you have libreadline])
15369     ],
15370     [AC_MSG_FAILURE(
15371        [--with-readline was given, but test for readline failed])],
15372     [-lncurses])])
15373 @end example
15375 The last example shows how to use @code{AC_ARG_WITH} to give the user the
15376 possibility to disable support for the readline library, given that it is
15377 an important feature and that it should be enabled by default.
15379 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
15380 @example
15381 AC_ARG_WITH([readline],
15382   [AS_HELP_STRING([--without-readline],
15383     [disable support for readline])],
15384   [],
15385   [with_readline=yes])
15387 LIBREADLINE=
15388 AS_IF([test "x$with_readline" != xno],
15389   [AC_CHECK_LIB([readline], [main],
15390     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
15391      AC_DEFINE([HAVE_LIBREADLINE], [1],
15392                [Define if you have libreadline])
15393     ],
15394     [AC_MSG_FAILURE(
15395        [readline test failed (--without-readline to disable)])],
15396     [-lncurses])])
15397 @end example
15399 These three examples can be easily adapted to the case where
15400 @code{AC_ARG_ENABLE} should be preferred to @code{AC_ARG_WITH} (see
15401 @ref{Package Options}).
15402 @end defmac
15404 @defmac AC_WITH (@var{package}, @var{action-if-given}, @ovar{action-if-not-given})
15405 @acindex{WITH}
15406 This is an obsolete version of @code{AC_ARG_WITH} that does not
15407 support providing a help string.
15408 @end defmac
15410 @node Package Options
15411 @section Choosing Package Options
15412 @cindex Package options
15413 @cindex Options, package
15415 If a software package has optional compile-time features, the user can
15416 give @command{configure} command line options to specify whether to
15417 compile them.  The options have one of these forms:
15419 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
15420 @c awful.
15421 @example
15422 --enable-@var{feature}[=@var{arg}]
15423 --disable-@var{feature}
15424 @end example
15426 These options allow users to choose which optional features to build and
15427 install.  @option{--enable-@var{feature}} options should never make a
15428 feature behave differently or cause one feature to replace another.
15429 They should only cause parts of the program to be built rather than left
15430 out.
15432 The user can give an argument by following the feature name with
15433 @samp{=} and the argument.  Giving an argument of @samp{no} requests
15434 that the feature @emph{not} be made available.  A feature with an
15435 argument looks like @option{--enable-debug=stabs}.  If no argument is
15436 given, it defaults to @samp{yes}.  @option{--disable-@var{feature}} is
15437 equivalent to @option{--enable-@var{feature}=no}.
15439 @command{configure} scripts do not complain about
15440 @option{--enable-@var{feature}} options that they do not support.
15441 This behavior permits configuring a source tree containing multiple
15442 packages with a top-level @command{configure} script when the packages
15443 support different options, without spurious error messages about options
15444 that some of the packages support.
15445 An unfortunate side effect is that option spelling errors are not diagnosed.
15446 No better approach to this problem has been suggested so far.
15448 For each optional feature, @file{configure.ac} should call
15449 @code{AC_ARG_ENABLE} to detect whether the @command{configure} user asked
15450 to include it.  Whether each feature is included or not by default, and
15451 which arguments are valid, is up to you.
15453 @defmac AC_ARG_ENABLE (@var{feature}, @var{help-string}, @ovar{action-if-given}, @ovar{action-if-not-given})
15454 @acindex{ARG_ENABLE}
15455 If the user gave @command{configure} the option
15456 @option{--enable-@var{feature}} or @option{--disable-@var{feature}}, run
15457 shell commands @var{action-if-given}.  If neither option was given, run
15458 shell commands @var{action-if-not-given}.  The name @var{feature}
15459 indicates an optional user-level facility.  It should consist only of
15460 alphanumeric characters and dashes.
15462 The option's argument is available to the shell commands
15463 @var{action-if-given} in the shell variable @code{enableval}, which is
15464 actually just the value of the shell variable
15465 @code{enable_@var{feature}}, with any @option{-} characters changed into
15466 @samp{_}.  You may use that variable instead, if you wish.  The
15467 @var{help-string} argument is like that of @code{AC_ARG_WITH}
15468 (@pxref{External Software}).
15470 You should format your @var{help-string} with the macro
15471 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
15473 See the examples suggested with the definition of @code{AC_ARG_WITH}
15474 (@pxref{External Software}) to get an idea of possible applications of
15475 @code{AC_ARG_ENABLE}.
15476 @end defmac
15478 @defmac AC_ENABLE (@var{feature}, @var{action-if-given}, @ovar{action-if-not-given})
15479 @acindex{ENABLE}
15480 This is an obsolete version of @code{AC_ARG_ENABLE} that does not
15481 support providing a help string.
15482 @end defmac
15485 @node Pretty Help Strings
15486 @section Making Your Help Strings Look Pretty
15487 @cindex Help strings
15489 Properly formatting the @samp{help strings} which are used in
15490 @code{AC_ARG_WITH} (@pxref{External Software}) and @code{AC_ARG_ENABLE}
15491 (@pxref{Package Options}) can be challenging.  Specifically, you want
15492 your own @samp{help strings} to line up in the appropriate columns of
15493 @samp{configure --help} just like the standard Autoconf @samp{help
15494 strings} do.  This is the purpose of the @code{AS_HELP_STRING} macro.
15496 @defmac AS_HELP_STRING (@var{left-hand-side}, @var{right-hand-side})
15497 @acindex{HELP_STRING}
15499 Expands into an help string that looks pretty when the user executes
15500 @samp{configure --help}.  It is typically used in @code{AC_ARG_WITH}
15501 (@pxref{External Software}) or @code{AC_ARG_ENABLE} (@pxref{Package
15502 Options}).  The following example makes this clearer.
15504 @example
15505 AC_ARG_WITH([foo],
15506   [AS_HELP_STRING([--with-foo],
15507      [use foo (default is no)])],
15508   [use_foo=$withval],
15509   [use_foo=no])
15510 @end example
15512 The second argument of @code{AS_HELP_STRING} is
15513 not a literal, and should not be double quoted.
15514 @xref{Autoconf Language}, for a more detailed explanation.
15515 Then the last few lines of @samp{configure --help} appear like
15516 this:
15518 @example
15519 --enable and --with options recognized:
15520   --with-foo              use foo (default is no)
15521 @end example
15523 The @code{AS_HELP_STRING} macro is particularly helpful when the
15524 @var{left-hand-side} and/or @var{right-hand-side} are composed of macro
15525 arguments, as shown in the following example.
15527 @example
15528 AC_DEFUN([MY_ARG_WITH],
15529   [AC_ARG_WITH([$1],
15530      [AS_HELP_STRING([--with-$1], [use $1 (default is $2)])],
15531      [use_[]$1=$withval],
15532      [use_[]$1=$2])])
15533 @end example
15534 @end defmac
15537 @node Site Details
15538 @section Configuring Site Details
15539 @cindex Site details
15541 Some software packages require complex site-specific information.  Some
15542 examples are host names to use for certain services, company names, and
15543 email addresses to contact.  Since some configuration scripts generated
15544 by Metaconfig ask for such information interactively, people sometimes
15545 wonder how to get that information in Autoconf-generated configuration
15546 scripts, which aren't interactive.
15548 Such site configuration information should be put in a file that is
15549 edited @emph{only by users}, not by programs.  The location of the file
15550 can either be based on the @code{prefix} variable, or be a standard
15551 location such as the user's home directory.  It could even be specified
15552 by an environment variable.  The programs should examine that file at
15553 runtime, rather than at compile time.  Runtime configuration is more
15554 convenient for users and makes the configuration process simpler than
15555 getting the information while configuring.  @xref{Directory Variables, ,
15556 Variables for Installation Directories, standards, @acronym{GNU} Coding
15557 Standards}, for more information on where to put data files.
15559 @node Transforming Names
15560 @section Transforming Program Names When Installing
15561 @cindex Transforming program names
15562 @cindex Program names, transforming
15564 Autoconf supports changing the names of programs when installing them.
15565 In order to use these transformations, @file{configure.ac} must call the
15566 macro @code{AC_ARG_PROGRAM}.
15568 @defmac AC_ARG_PROGRAM
15569 @acindex{ARG_PROGRAM}
15570 @ovindex program_transform_name
15571 Place in output variable @code{program_transform_name} a sequence of
15572 @code{sed} commands for changing the names of installed programs.
15574 If any of the options described below are given to @command{configure},
15575 program names are transformed accordingly.  Otherwise, if
15576 @code{AC_CANONICAL_TARGET} has been called and a @option{--target} value
15577 is given, the target type followed by a dash is used as a prefix.
15578 Otherwise, no program name transformation is done.
15579 @end defmac
15581 @menu
15582 * Transformation Options::      @command{configure} options to transform names
15583 * Transformation Examples::     Sample uses of transforming names
15584 * Transformation Rules::        Makefile uses of transforming names
15585 @end menu
15587 @node Transformation Options
15588 @subsection Transformation Options
15590 You can specify name transformations by giving @command{configure} these
15591 command line options:
15593 @table @option
15594 @item --program-prefix=@var{prefix}
15595 prepend @var{prefix} to the names;
15597 @item --program-suffix=@var{suffix}
15598 append @var{suffix} to the names;
15600 @item --program-transform-name=@var{expression}
15601 perform @code{sed} substitution @var{expression} on the names.
15602 @end table
15604 @node Transformation Examples
15605 @subsection Transformation Examples
15607 These transformations are useful with programs that can be part of a
15608 cross-compilation development environment.  For example, a
15609 cross-assembler running on a Sun 4 configured with
15610 @option{--target=i960-vxworks} is normally installed as
15611 @file{i960-vxworks-as}, rather than @file{as}, which could be confused
15612 with a native Sun 4 assembler.
15614 You can force a program name to begin with @file{g}, if you don't want
15615 @acronym{GNU} programs installed on your system to shadow other programs with
15616 the same name.  For example, if you configure @acronym{GNU} @code{diff} with
15617 @option{--program-prefix=g}, then when you run @samp{make install} it is
15618 installed as @file{/usr/local/bin/gdiff}.
15620 As a more sophisticated example, you could use
15622 @example
15623 --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/'
15624 @end example
15625 @noindent
15627 to prepend @samp{g} to most of the program names in a source tree,
15628 excepting those like @code{gdb} that already have one and those like
15629 @code{less} and @code{lesskey} that aren't @acronym{GNU} programs.  (That is
15630 assuming that you have a source tree containing those programs that is
15631 set up to use this feature.)
15633 One way to install multiple versions of some programs simultaneously is
15634 to append a version number to the name of one or both.  For example, if
15635 you want to keep Autoconf version 1 around for awhile, you can configure
15636 Autoconf version 2 using @option{--program-suffix=2} to install the
15637 programs as @file{/usr/local/bin/autoconf2},
15638 @file{/usr/local/bin/autoheader2}, etc.  Nevertheless, pay attention
15639 that only the binaries are renamed, therefore you'd have problems with
15640 the library files which might overlap.
15642 @node Transformation Rules
15643 @subsection Transformation Rules
15645 Here is how to use the variable @code{program_transform_name} in a
15646 @file{Makefile.in}:
15648 @example
15649 PROGRAMS = cp ls rm
15650 transform = @@program_transform_name@@
15651 install:
15652         for p in $(PROGRAMS); do \
15653           $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p | \
15654                                               sed '$(transform)'`; \
15655         done
15657 uninstall:
15658         for p in $(PROGRAMS); do \
15659           rm -f $(DESTDIR)$(bindir)/`echo $$p | sed '$(transform)'`; \
15660         done
15661 @end example
15663 It is guaranteed that @code{program_transform_name} is never empty, and
15664 that there are no useless separators.  Therefore you may safely embed
15665 @code{program_transform_name} within a sed program using @samp{;}:
15667 @example
15668 transform = @@program_transform_name@@
15669 transform_exe = s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/
15670 @end example
15672 Whether to do the transformations on documentation files (Texinfo or
15673 @code{man}) is a tricky question; there seems to be no perfect answer,
15674 due to the several reasons for name transforming.  Documentation is not
15675 usually particular to a specific architecture, and Texinfo files do not
15676 conflict with system documentation.  But they might conflict with
15677 earlier versions of the same files, and @code{man} pages sometimes do
15678 conflict with system documentation.  As a compromise, it is probably
15679 best to do name transformations on @code{man} pages but not on Texinfo
15680 manuals.
15682 @node Site Defaults
15683 @section Setting Site Defaults
15684 @cindex Site defaults
15686 Autoconf-generated @command{configure} scripts allow your site to provide
15687 default values for some configuration values.  You do this by creating
15688 site- and system-wide initialization files.
15690 @evindex CONFIG_SITE
15691 If the environment variable @code{CONFIG_SITE} is set, @command{configure}
15692 uses its value as the name of a shell script to read.  Otherwise, it
15693 reads the shell script @file{@var{prefix}/share/config.site} if it exists,
15694 then @file{@var{prefix}/etc/config.site} if it exists.  Thus,
15695 settings in machine-specific files override those in machine-independent
15696 ones in case of conflict.
15698 Site files can be arbitrary shell scripts, but only certain kinds of
15699 code are really appropriate to be in them.  Because @command{configure}
15700 reads any cache file after it has read any site files, a site file can
15701 define a default cache file to be shared between all Autoconf-generated
15702 @command{configure} scripts run on that system (@pxref{Cache Files}).  If
15703 you set a default cache file in a site file, it is a good idea to also
15704 set the output variable @code{CC} in that site file, because the cache
15705 file is only valid for a particular compiler, but many systems have
15706 several available.
15708 You can examine or override the value set by a command line option to
15709 @command{configure} in a site file; options set shell variables that have
15710 the same names as the options, with any dashes turned into underscores.
15711 The exceptions are that @option{--without-} and @option{--disable-} options
15712 are like giving the corresponding @option{--with-} or @option{--enable-}
15713 option and the value @samp{no}.  Thus, @option{--cache-file=localcache}
15714 sets the variable @code{cache_file} to the value @samp{localcache};
15715 @option{--enable-warnings=no} or @option{--disable-warnings} sets the variable
15716 @code{enable_warnings} to the value @samp{no}; @option{--prefix=/usr} sets the
15717 variable @code{prefix} to the value @samp{/usr}; etc.
15719 Site files are also good places to set default values for other output
15720 variables, such as @code{CFLAGS}, if you need to give them non-default
15721 values: anything you would normally do, repetitively, on the command
15722 line.  If you use non-default values for @var{prefix} or
15723 @var{exec_prefix} (wherever you locate the site file), you can set them
15724 in the site file if you specify it with the @code{CONFIG_SITE}
15725 environment variable.
15727 You can set some cache values in the site file itself.  Doing this is
15728 useful if you are cross-compiling, where it is impossible to check features
15729 that require running a test program.  You could ``prime the cache'' by
15730 setting those values correctly for that system in
15731 @file{@var{prefix}/etc/config.site}.  To find out the names of the cache
15732 variables you need to set, look for shell variables with @samp{_cv_} in
15733 their names in the affected @command{configure} scripts, or in the Autoconf
15734 M4 source code for those macros.
15736 The cache file is careful to not override any variables set in the site
15737 files.  Similarly, you should not override command-line options in the
15738 site files.  Your code should check that variables such as @code{prefix}
15739 and @code{cache_file} have their default values (as set near the top of
15740 @command{configure}) before changing them.
15742 Here is a sample file @file{/usr/share/local/gnu/share/config.site}.  The
15743 command @samp{configure --prefix=/usr/share/local/gnu} would read this
15744 file (if @code{CONFIG_SITE} is not set to a different file).
15746 @example
15747 # config.site for configure
15749 # Change some defaults.
15750 test "$prefix" = NONE && prefix=/usr/share/local/gnu
15751 test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu
15752 test "$sharedstatedir" = '$prefix/com' && sharedstatedir=/var
15753 test "$localstatedir" = '$prefix/var' && localstatedir=/var
15755 # Give Autoconf 2.x generated configure scripts a shared default
15756 # cache file for feature test results, architecture-specific.
15757 if test "$cache_file" = /dev/null; then
15758   cache_file="$prefix/var/config.cache"
15759   # A cache file is only valid for one C compiler.
15760   CC=gcc
15762 @end example
15765 @c ============================================== Running configure Scripts.
15767 @node Running configure Scripts
15768 @chapter Running @command{configure} Scripts
15769 @cindex @command{configure}
15771 Below are instructions on how to configure a package that uses a
15772 @command{configure} script, suitable for inclusion as an @file{INSTALL}
15773 file in the package.  A plain-text version of @file{INSTALL} which you
15774 may use comes with Autoconf.
15776 @menu
15777 * Basic Installation::          Instructions for typical cases
15778 * Compilers and Options::       Selecting compilers and optimization
15779 * Multiple Architectures::      Compiling for multiple architectures at once
15780 * Installation Names::          Installing in different directories
15781 * Optional Features::           Selecting optional features
15782 * System Type::                 Specifying the system type
15783 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
15784 * Defining Variables::          Specifying the compiler etc.
15785 * configure Invocation::        Changing how @command{configure} runs
15786 @end menu
15788 @set autoconf
15789 @include install.texi
15792 @c ============================================== Recreating a Configuration
15794 @node config.status Invocation
15795 @chapter Recreating a Configuration
15796 @cindex @command{config.status}
15798 The @command{configure} script creates a file named @file{config.status},
15799 which actually configures, @dfn{instantiates}, the template files.  It
15800 also records the configuration options that were specified when the
15801 package was last configured in case reconfiguring is needed.
15803 Synopsis:
15804 @example
15805 ./config.status @var{option}@dots{} [@var{file}@dots{}]
15806 @end example
15808 It configures the @var{files}; if none are specified, all the templates
15809 are instantiated.  The files must be specified without their
15810 dependencies, as in
15812 @example
15813 ./config.status foobar
15814 @end example
15816 @noindent
15819 @example
15820 ./config.status foobar:foo.in:bar.in
15821 @end example
15823 The supported options are:
15825 @table @option
15826 @item --help
15827 @itemx -h
15828 Print a summary of the command line options, the list of the template
15829 files, and exit.
15831 @item --version
15832 @itemx -V
15833 Print the version number of Autoconf and exit.
15835 @item --silent
15836 @itemx --quiet
15837 @itemx -q
15838 Do not print progress messages.
15840 @item --debug
15841 @itemx -d
15842 Don't remove the temporary files.
15844 @item --file=@var{file}[:@var{template}]
15845 Require that @var{file} be instantiated as if
15846 @samp{AC_CONFIG_FILES(@var{file}:@var{template})} was used.  Both
15847 @var{file} and @var{template} may be @samp{-} in which case the standard
15848 output and/or standard input, respectively, is used.  If a
15849 @var{template} file name is relative, it is first looked for in the build
15850 tree, and then in the source tree.  @xref{Configuration Actions}, for
15851 more details.
15853 This option and the following ones provide one way for separately
15854 distributed packages to share the values computed by @command{configure}.
15855 Doing so can be useful if some of the packages need a superset of the
15856 features that one of them, perhaps a common library, does.  These
15857 options allow a @file{config.status} file to create files other than the
15858 ones that its @file{configure.ac} specifies, so it can be used for a
15859 different package.
15861 @item --header=@var{file}[:@var{template}]
15862 Same as @option{--file} above, but with @samp{AC_CONFIG_HEADERS}.
15864 @item --recheck
15865 Ask @file{config.status} to update itself and exit (no instantiation).
15866 This option is useful if you change @command{configure}, so that the
15867 results of some tests might be different from the previous run.  The
15868 @option{--recheck} option reruns @command{configure} with the same arguments
15869 you used before, plus the @option{--no-create} option, which prevents
15870 @command{configure} from running @file{config.status} and creating
15871 @file{Makefile} and other files, and the @option{--no-recursion} option,
15872 which prevents @command{configure} from running other @command{configure}
15873 scripts in subdirectories.  (This is so other Make rules can
15874 run @file{config.status} when it changes; @pxref{Automatic Remaking},
15875 for an example).
15876 @end table
15878 @file{config.status} checks several optional environment variables that
15879 can alter its behavior:
15881 @defvar CONFIG_SHELL
15882 @evindex CONFIG_SHELL
15883 The shell with which to run @command{configure} for the @option{--recheck}
15884 option.  It must be Bourne-compatible.  The default is a shell that
15885 supports @code{LINENO} if available, and @file{/bin/sh} otherwise.
15886 Invoking @command{configure} by hand bypasses this setting, so you may
15887 need to use a command like @samp{CONFIG_SHELL=/bin/bash /bin/bash ./configure}
15888 to insure that the same shell is used everywhere.  The absolute name of the
15889 shell should be passed.
15890 @end defvar
15892 @defvar CONFIG_STATUS
15893 @evindex CONFIG_STATUS
15894 The file name to use for the shell script that records the
15895 configuration.  The default is @file{./config.status}.  This variable is
15896 useful when one package uses parts of another and the @command{configure}
15897 scripts shouldn't be merged because they are maintained separately.
15898 @end defvar
15900 You can use @file{./config.status} in your makefiles.  For example, in
15901 the dependencies given above (@pxref{Automatic Remaking}),
15902 @file{config.status} is run twice when @file{configure.ac} has changed.
15903 If that bothers you, you can make each run only regenerate the files for
15904 that rule:
15905 @example
15906 @group
15907 config.h: stamp-h
15908 stamp-h: config.h.in config.status
15909         ./config.status config.h
15910         echo > stamp-h
15912 Makefile: Makefile.in config.status
15913         ./config.status Makefile
15914 @end group
15915 @end example
15917 The calling convention of @file{config.status} has changed; see
15918 @ref{Obsolete config.status Use}, for details.
15921 @c =================================================== Obsolete Constructs
15923 @node Obsolete Constructs
15924 @chapter Obsolete Constructs
15925 @cindex Obsolete constructs
15927 Autoconf changes, and throughout the years some constructs have been
15928 obsoleted.  Most of the changes involve the macros, but in some cases
15929 the tools themselves, or even some concepts, are now considered
15930 obsolete.
15932 You may completely skip this chapter if you are new to Autoconf.  Its
15933 intention is mainly to help maintainers updating their packages by
15934 understanding how to move to more modern constructs.
15936 @menu
15937 * Obsolete config.status Use::  Different calling convention
15938 * acconfig.h::                  Additional entries in @file{config.h.in}
15939 * autoupdate Invocation::       Automatic update of @file{configure.ac}
15940 * Obsolete Macros::             Backward compatibility macros
15941 * Autoconf 1::                  Tips for upgrading your files
15942 * Autoconf 2.13::               Some fresher tips
15943 @end menu
15945 @node Obsolete config.status Use
15946 @section Obsolete @file{config.status} Invocation
15948 @file{config.status} now supports arguments to specify the files to
15949 instantiate; see @ref{config.status Invocation}, for more details.
15950 Before, environment variables had to be used.
15952 @defvar CONFIG_COMMANDS
15953 @evindex CONFIG_COMMANDS
15954 The tags of the commands to execute.  The default is the arguments given
15955 to @code{AC_OUTPUT} and @code{AC_CONFIG_COMMANDS} in
15956 @file{configure.ac}.
15957 @end defvar
15959 @defvar CONFIG_FILES
15960 @evindex CONFIG_FILES
15961 The files in which to perform @samp{@@@var{variable}@@} substitutions.
15962 The default is the arguments given to @code{AC_OUTPUT} and
15963 @code{AC_CONFIG_FILES} in @file{configure.ac}.
15964 @end defvar
15966 @defvar CONFIG_HEADERS
15967 @evindex CONFIG_HEADERS
15968 The files in which to substitute C @code{#define} statements.  The
15969 default is the arguments given to @code{AC_CONFIG_HEADERS}; if that
15970 macro was not called, @file{config.status} ignores this variable.
15971 @end defvar
15973 @defvar CONFIG_LINKS
15974 @evindex CONFIG_LINKS
15975 The symbolic links to establish.  The default is the arguments given to
15976 @code{AC_CONFIG_LINKS}; if that macro was not called,
15977 @file{config.status} ignores this variable.
15978 @end defvar
15980 In @ref{config.status Invocation}, using this old interface, the example
15981 would be:
15983 @example
15984 @group
15985 config.h: stamp-h
15986 stamp-h: config.h.in config.status
15987         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_FILES= \
15988           CONFIG_HEADERS=config.h ./config.status
15989         echo > stamp-h
15991 Makefile: Makefile.in config.status
15992         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_HEADERS= \
15993           CONFIG_FILES=Makefile ./config.status
15994 @end group
15995 @end example
15997 @noindent
15998 (If @file{configure.ac} does not call @code{AC_CONFIG_HEADERS}, there is
15999 no need to set @code{CONFIG_HEADERS} in the @code{make} rules.  Equally
16000 for @code{CONFIG_COMMANDS}, etc.)
16003 @node acconfig.h
16004 @section @file{acconfig.h}
16006 @cindex @file{acconfig.h}
16007 @cindex @file{config.h.top}
16008 @cindex @file{config.h.bot}
16010 In order to produce @file{config.h.in}, @command{autoheader} needs to
16011 build or to find templates for each symbol.  Modern releases of Autoconf
16012 use @code{AH_VERBATIM} and @code{AH_TEMPLATE} (@pxref{Autoheader
16013 Macros}), but in older releases a file, @file{acconfig.h}, contained the
16014 list of needed templates.  @command{autoheader} copied comments and
16015 @code{#define} and @code{#undef} statements from @file{acconfig.h} in
16016 the current directory, if present.  This file used to be mandatory if
16017 you @code{AC_DEFINE} any additional symbols.
16019 Modern releases of Autoconf also provide @code{AH_TOP} and
16020 @code{AH_BOTTOM} if you need to prepend/append some information to
16021 @file{config.h.in}.  Ancient versions of Autoconf had a similar feature:
16022 if @file{./acconfig.h} contains the string @samp{@@TOP@@},
16023 @command{autoheader} copies the lines before the line containing
16024 @samp{@@TOP@@} into the top of the file that it generates.  Similarly,
16025 if @file{./acconfig.h} contains the string @samp{@@BOTTOM@@},
16026 @command{autoheader} copies the lines after that line to the end of the
16027 file it generates.  Either or both of those strings may be omitted.  An
16028 even older alternate way to produce the same effect in ancient versions
16029 of Autoconf is to create the files @file{@var{file}.top} (typically
16030 @file{config.h.top}) and/or @file{@var{file}.bot} in the current
16031 directory.  If they exist, @command{autoheader} copies them to the
16032 beginning and end, respectively, of its output.
16034 In former versions of Autoconf, the files used in preparing a software
16035 package for distribution were:
16036 @example
16037 @group
16038 configure.ac --.   .------> autoconf* -----> configure
16039                +---+
16040 [aclocal.m4] --+   `---.
16041 [acsite.m4] ---'       |
16042                        +--> [autoheader*] -> [config.h.in]
16043 [acconfig.h] ----.     |
16044                  +-----'
16045 [config.h.top] --+
16046 [config.h.bot] --'
16047 @end group
16048 @end example
16050 Using only the @code{AH_} macros, @file{configure.ac} should be
16051 self-contained, and should not depend upon @file{acconfig.h} etc.
16054 @node autoupdate Invocation
16055 @section Using @command{autoupdate} to Modernize @file{configure.ac}
16056 @cindex @command{autoupdate}
16058 The @command{autoupdate} program updates a @file{configure.ac} file that
16059 calls Autoconf macros by their old names to use the current macro names.
16060 In version 2 of Autoconf, most of the macros were renamed to use a more
16061 uniform and descriptive naming scheme.  @xref{Macro Names}, for a
16062 description of the new scheme.  Although the old names still work
16063 (@pxref{Obsolete Macros}, for a list of the old macros and the corresponding
16064 new names), you can make your @file{configure.ac} files more readable
16065 and make it easier to use the current Autoconf documentation if you
16066 update them to use the new macro names.
16068 @evindex SIMPLE_BACKUP_SUFFIX
16069 If given no arguments, @command{autoupdate} updates @file{configure.ac},
16070 backing up the original version with the suffix @file{~} (or the value
16071 of the environment variable @code{SIMPLE_BACKUP_SUFFIX}, if that is
16072 set).  If you give @command{autoupdate} an argument, it reads that file
16073 instead of @file{configure.ac} and writes the updated file to the
16074 standard output.
16076 @noindent
16077 @command{autoupdate} accepts the following options:
16079 @table @option
16080 @item --help
16081 @itemx -h
16082 Print a summary of the command line options and exit.
16084 @item --version
16085 @itemx -V
16086 Print the version number of Autoconf and exit.
16088 @item --verbose
16089 @itemx -v
16090 Report processing steps.
16092 @item --debug
16093 @itemx -d
16094 Don't remove the temporary files.
16096 @item --force
16097 @itemx -f
16098 Force the update even if the file has not changed.  Disregard the cache.
16100 @item --include=@var{dir}
16101 @itemx -I @var{dir}
16102 Also look for input files in @var{dir}.  Multiple invocations accumulate.
16103 Directories are browsed from last to first.
16104 @end table
16106 @node Obsolete Macros
16107 @section Obsolete Macros
16109 Several macros are obsoleted in Autoconf, for various reasons (typically
16110 they failed to quote properly, couldn't be extended for more recent
16111 issues, etc.).  They are still supported, but deprecated: their use
16112 should be avoided.
16114 During the jump from Autoconf version 1 to version 2, most of the
16115 macros were renamed to use a more uniform and descriptive naming scheme,
16116 but their signature did not change.  @xref{Macro Names}, for a
16117 description of the new naming scheme.  Below, if there is just the mapping
16118 from old names to new names for these macros, the reader is invited to
16119 refer to the definition of the new macro for the signature and the
16120 description.
16122 @defmac AC_ALLOCA
16123 @acindex{ALLOCA}
16124 @code{AC_FUNC_ALLOCA}
16125 @end defmac
16127 @defmac AC_ARG_ARRAY
16128 @acindex{ARG_ARRAY}
16129 removed because of limited usefulness
16130 @end defmac
16132 @defmac AC_C_CROSS
16133 @acindex{C_CROSS}
16134 This macro is obsolete; it does nothing.
16135 @end defmac
16137 @defmac AC_C_LONG_DOUBLE
16138 @acindex{C_LONG_DOUBLE}
16139 @cvindex HAVE_LONG_DOUBLE
16140 If the C compiler supports a working @code{long double} type with more
16141 range or precision than the @code{double} type, define
16142 @code{HAVE_LONG_DOUBLE}.
16144 You should use @code{AC_TYPE_LONG_DOUBLE} or
16145 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
16146 @end defmac
16148 @defmac AC_CANONICAL_SYSTEM
16149 @acindex{CANONICAL_SYSTEM}
16150 Determine the system type and set output variables to the names of the
16151 canonical system types.  @xref{Canonicalizing}, for details about the
16152 variables this macro sets.
16154 The user is encouraged to use either @code{AC_CANONICAL_BUILD}, or
16155 @code{AC_CANONICAL_HOST}, or @code{AC_CANONICAL_TARGET}, depending on
16156 the needs.  Using @code{AC_CANONICAL_TARGET} is enough to run the two
16157 other macros.
16158 @end defmac
16160 @defmac AC_CHAR_UNSIGNED
16161 @acindex{CHAR_UNSIGNED}
16162 @code{AC_C_CHAR_UNSIGNED}
16163 @end defmac
16165 @defmac AC_CHECK_TYPE (@var{type}, @var{default})
16166 @acindex{CHECK_TYPE}
16167 Autoconf, up to 2.13, used to provide this version of
16168 @code{AC_CHECK_TYPE}, deprecated because of its flaws.  First, although
16169 it is a member of the @code{CHECK} clan, it does
16170 more than just checking.  Secondly, missing types are defined
16171 using @code{#define}, not @code{typedef}, and this can lead to
16172 problems in the case of pointer types.
16174 This use of @code{AC_CHECK_TYPE} is obsolete and discouraged; see
16175 @ref{Generic Types}, for the description of the current macro.
16177 If the type @var{type} is not defined, define it to be the C (or C++)
16178 builtin type @var{default}, e.g., @samp{short int} or @samp{unsigned int}.
16180 This macro is equivalent to:
16182 @example
16183 AC_CHECK_TYPE([@var{type}], [],
16184   [AC_DEFINE_UNQUOTED([@var{type}], [@var{default}],
16185      [Define to `@var{default}'
16186       if <sys/types.h> does not define.])])
16187 @end example
16189 In order to keep backward compatibility, the two versions of
16190 @code{AC_CHECK_TYPE} are implemented, selected by a simple heuristics:
16192 @enumerate
16193 @item
16194 If there are three or four arguments, the modern version is used.
16196 @item
16197 If the second argument appears to be a C or C++ type, then the
16198 obsolete version is used.  This happens if the argument is a C or C++
16199 @emph{builtin} type or a C identifier ending in @samp{_t}, optionally
16200 followed by one of @samp{[(* } and then by a string of zero or more
16201 characters taken from the set @samp{[]()* _a-zA-Z0-9}.
16203 @item
16204 If the second argument is spelled with the alphabet of valid C and C++
16205 types, the user is warned and the modern version is used.
16207 @item
16208 Otherwise, the modern version is used.
16209 @end enumerate
16211 @noindent
16212 You are encouraged either to use a valid builtin type, or to use the
16213 equivalent modern code (see above), or better yet, to use
16214 @code{AC_CHECK_TYPES} together with
16216 @example
16217 #if !HAVE_LOFF_T
16218 typedef loff_t off_t;
16219 #endif
16220 @end example
16221 @end defmac
16222 @c end of AC_CHECK_TYPE
16224 @defmac AC_CHECKING (@var{feature-description})
16225 @acindex{CHECKING}
16226 Same as @samp{AC_MSG_NOTICE([checking @var{feature-description}@dots{}]}.
16227 @end defmac
16229 @defmac AC_COMPILE_CHECK (@var{echo-text}, @var{includes}, @var{function-body}, @var{action-if-true}, @ovar{action-if-false})
16230 @acindex{COMPILE_CHECK}
16231 This is an obsolete version of @code{AC_TRY_COMPILE} itself replaced by
16232 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}), with the
16233 addition that it prints @samp{checking for @var{echo-text}} to the
16234 standard output first, if @var{echo-text} is non-empty.  Use
16235 @code{AC_MSG_CHECKING} and @code{AC_MSG_RESULT} instead to print
16236 messages (@pxref{Printing Messages}).
16237 @end defmac
16239 @defmac AC_CONST
16240 @acindex{CONST}
16241 @code{AC_C_CONST}
16242 @end defmac
16244 @defmac AC_CROSS_CHECK
16245 @acindex{CROSS_CHECK}
16246 Same as @code{AC_C_CROSS}, which is obsolete too, and does nothing
16247 @code{:-)}.
16248 @end defmac
16250 @defmac AC_CYGWIN
16251 @acindex{CYGWIN}
16252 Check for the Cygwin environment in which case the shell variable
16253 @code{CYGWIN} is set to @samp{yes}.  Don't use this macro, the dignified
16254 means to check the nature of the host is using
16255 @code{AC_CANONICAL_HOST}.  As a matter of fact this macro is defined as:
16257 @example
16258 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
16259 case $host_os in
16260   *cygwin* ) CYGWIN=yes;;
16261          * ) CYGWIN=no;;
16262 esac
16263 @end example
16265 Beware that the variable @code{CYGWIN} has a special meaning when
16266 running Cygwin, and should not be changed.  That's yet another reason
16267 not to use this macro.
16268 @end defmac
16270 @defmac AC_DECL_SYS_SIGLIST
16271 @acindex{DECL_SYS_SIGLIST}
16272 @cvindex SYS_SIGLIST_DECLARED
16273 Same as:
16275 @example
16276 AC_CHECK_DECLS([sys_siglist], [], [],
16277 [#include <signal.h>
16278 /* NetBSD declares sys_siglist in unistd.h.  */
16279 #if HAVE_UNISTD_H
16280 # include <unistd.h>
16281 #endif
16283 @end example
16284 @end defmac
16286 @defmac AC_DECL_YYTEXT
16287 @acindex{DECL_YYTEXT}
16288 Does nothing, now integrated in @code{AC_PROG_LEX}.
16289 @end defmac
16291 @defmac AC_DIR_HEADER
16292 @acindex{DIR_HEADER}
16293 @cvindex DIRENT
16294 @cvindex SYSNDIR
16295 @cvindex SYSDIR
16296 @cvindex NDIR
16297 Like calling @code{AC_FUNC_CLOSEDIR_VOID} and@code{AC_HEADER_DIRENT},
16298 but defines a different set of C preprocessor macros to indicate which
16299 header file is found:
16301 @multitable {@file{sys/ndir.h}} {Old Symbol} {@code{HAVE_SYS_NDIR_H}}
16302 @item Header            @tab Old Symbol     @tab New Symbol
16303 @item @file{dirent.h}   @tab @code{DIRENT}  @tab @code{HAVE_DIRENT_H}
16304 @item @file{sys/ndir.h} @tab @code{SYSNDIR} @tab @code{HAVE_SYS_NDIR_H}
16305 @item @file{sys/dir.h}  @tab @code{SYSDIR}  @tab @code{HAVE_SYS_DIR_H}
16306 @item @file{ndir.h}     @tab @code{NDIR}    @tab @code{HAVE_NDIR_H}
16307 @end multitable
16308 @end defmac
16310 @defmac AC_DYNIX_SEQ
16311 @acindex{DYNIX_SEQ}
16312 If on DYNIX/ptx, add @option{-lseq} to output variable
16313 @code{LIBS}.  This macro used to be defined as
16315 @example
16316 AC_CHECK_LIB([seq], [getmntent], [LIBS="-lseq $LIBS"])
16317 @end example
16319 @noindent
16320 now it is just @code{AC_FUNC_GETMNTENT}.
16321 @end defmac
16323 @defmac AC_EXEEXT
16324 @acindex{EXEEXT}
16325 @ovindex EXEEXT
16326 Defined the output variable @code{EXEEXT} based on the output of the
16327 compiler, which is now done automatically.  Typically set to empty
16328 string if Posix and @samp{.exe} if a @acronym{DOS} variant.
16329 @end defmac
16331 @defmac AC_EMXOS2
16332 @acindex{EMXOS2}
16333 Similar to @code{AC_CYGWIN} but checks for the EMX environment on OS/2
16334 and sets @code{EMXOS2}.
16335 @end defmac
16337 @defmac AC_ERROR
16338 @acindex{ERROR}
16339 @code{AC_MSG_ERROR}
16340 @end defmac
16342 @defmac AC_FIND_X
16343 @acindex{FIND_X}
16344 @code{AC_PATH_X}
16345 @end defmac
16347 @defmac AC_FIND_XTRA
16348 @acindex{FIND_XTRA}
16349 @code{AC_PATH_XTRA}
16350 @end defmac
16352 @defmac AC_FOREACH
16353 @acindex{FOREACH}
16354 @code{m4_foreach_w}
16355 @end defmac
16357 @defmac AC_FUNC_CHECK
16358 @acindex{FUNC_CHECK}
16359 @code{AC_CHECK_FUNC}
16360 @end defmac
16362 @defmac AC_FUNC_WAIT3
16363 @acindex{FUNC_WAIT3}
16364 @cvindex HAVE_WAIT3
16365 If @code{wait3} is found and fills in the contents of its third argument
16366 (a @samp{struct rusage *}), which @acronym{HP-UX} does not do, define
16367 @code{HAVE_WAIT3}.
16369 These days portable programs should use @code{waitpid}, not
16370 @code{wait3}, as @code{wait3} has been removed from Posix.
16371 @end defmac
16373 @defmac AC_GCC_TRADITIONAL
16374 @acindex{GCC_TRADITIONAL}
16375 @code{AC_PROG_GCC_TRADITIONAL}
16376 @end defmac
16378 @defmac AC_GETGROUPS_T
16379 @acindex{GETGROUPS_T}
16380 @code{AC_TYPE_GETGROUPS}
16381 @end defmac
16383 @defmac AC_GETLOADAVG
16384 @acindex{GETLOADAVG}
16385 @code{AC_FUNC_GETLOADAVG}
16386 @end defmac
16388 @defmac AC_HAVE_FUNCS
16389 @acindex{HAVE_FUNCS}
16390 @code{AC_CHECK_FUNCS}
16391 @end defmac
16393 @defmac AC_HAVE_HEADERS
16394 @acindex{HAVE_HEADERS}
16395 @code{AC_CHECK_HEADERS}
16396 @end defmac
16398 @defmac AC_HAVE_LIBRARY (@var{library}, @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
16399 @acindex{HAVE_LIBRARY}
16400 This macro is equivalent to calling @code{AC_CHECK_LIB} with a
16401 @var{function} argument of @code{main}.  In addition, @var{library} can
16402 be written as any of @samp{foo}, @option{-lfoo}, or @samp{libfoo.a}.  In
16403 all of those cases, the compiler is passed @option{-lfoo}.  However,
16404 @var{library} cannot be a shell variable; it must be a literal name.
16405 @end defmac
16407 @defmac AC_HAVE_POUNDBANG
16408 @acindex{HAVE_POUNDBANG}
16409 @code{AC_SYS_INTERPRETER} (different calling convention)
16410 @end defmac
16412 @defmac AC_HEADER_CHECK
16413 @acindex{HEADER_CHECK}
16414 @code{AC_CHECK_HEADER}
16415 @end defmac
16417 @defmac AC_HEADER_EGREP
16418 @acindex{HEADER_EGREP}
16419 @code{AC_EGREP_HEADER}
16420 @end defmac
16422 @defmac AC_HELP_STRING
16423 @acindex{HELP_STRING}
16424 @code{AS_HELP_STRING}
16425 @end defmac
16427 @defmac AC_INIT (@var{unique-file-in-source-dir})
16428 @acindex{INIT}
16429 Formerly @code{AC_INIT} used to have a single argument, and was
16430 equivalent to:
16432 @example
16433 AC_INIT
16434 AC_CONFIG_SRCDIR(@var{unique-file-in-source-dir})
16435 @end example
16436 @end defmac
16438 @defmac AC_INLINE
16439 @acindex{INLINE}
16440 @code{AC_C_INLINE}
16441 @end defmac
16443 @defmac AC_INT_16_BITS
16444 @acindex{INT_16_BITS}
16445 @cvindex INT_16_BITS
16446 If the C type @code{int} is 16 bits wide, define @code{INT_16_BITS}.
16447 Use @samp{AC_CHECK_SIZEOF(int)} instead.
16448 @end defmac
16450 @defmac AC_IRIX_SUN
16451 @acindex{IRIX_SUN}
16452 If on @sc{irix} (Silicon Graphics Unix), add @option{-lsun} to output
16453 @code{LIBS}.  If you were using it to get @code{getmntent}, use
16454 @code{AC_FUNC_GETMNTENT} instead.  If you used it for the NIS versions
16455 of the password and group functions, use @samp{AC_CHECK_LIB(sun,
16456 getpwnam)}.  Up to Autoconf 2.13, it used to be
16458 @example
16459 AC_CHECK_LIB([sun], [getmntent], [LIBS="-lsun $LIBS"])
16460 @end example
16462 @noindent
16463 now it is defined as
16465 @example
16466 AC_FUNC_GETMNTENT
16467 AC_CHECK_LIB([sun], [getpwnam])
16468 @end example
16469 @end defmac
16471 @defmac AC_LANG_C
16472 @acindex{LANG_C}
16473 Same as @samp{AC_LANG([C])}.
16474 @end defmac
16476 @defmac AC_LANG_CPLUSPLUS
16477 @acindex{LANG_CPLUSPLUS}
16478 Same as @samp{AC_LANG([C++])}.
16479 @end defmac
16481 @defmac AC_LANG_FORTRAN77
16482 @acindex{LANG_FORTRAN77}
16483 Same as @samp{AC_LANG([Fortran 77])}.
16484 @end defmac
16486 @defmac AC_LANG_RESTORE
16487 @acindex{LANG_RESTORE}
16488 Select the @var{language} that is saved on the top of the stack, as set
16489 by @code{AC_LANG_SAVE}, remove it from the stack, and call
16490 @code{AC_LANG(@var{language})}.
16491 @end defmac
16493 @defmac AC_LANG_SAVE
16494 @acindex{LANG_SAVE}
16495 Remember the current language (as set by @code{AC_LANG}) on a stack.
16496 The current language does not change.  @code{AC_LANG_PUSH} is preferred.
16497 @end defmac
16499 @defmac AC_LINK_FILES (@var{source}@dots{}, @var{dest}@dots{})
16500 @acindex{LINK_FILES}
16501 This is an obsolete version of @code{AC_CONFIG_LINKS}.  An updated
16502 version of:
16504 @example
16505 AC_LINK_FILES(config/$machine.h config/$obj_format.h,
16506               host.h            object.h)
16507 @end example
16509 @noindent
16512 @example
16513 AC_CONFIG_LINKS([host.h:config/$machine.h
16514                 object.h:config/$obj_format.h])
16515 @end example
16516 @end defmac
16518 @defmac AC_LN_S
16519 @acindex{LN_S}
16520 @code{AC_PROG_LN_S}
16521 @end defmac
16523 @defmac AC_LONG_64_BITS
16524 @acindex{LONG_64_BITS}
16525 @cvindex LONG_64_BITS
16526 Define @code{LONG_64_BITS} if the C type @code{long int} is 64 bits wide.
16527 Use the generic macro @samp{AC_CHECK_SIZEOF([long int])} instead.
16528 @end defmac
16530 @defmac AC_LONG_DOUBLE
16531 @acindex{LONG_DOUBLE}
16532 If the C compiler supports a working @code{long double} type with more
16533 range or precision than the @code{double} type, define
16534 @code{HAVE_LONG_DOUBLE}.
16536 You should use @code{AC_TYPE_LONG_DOUBLE} or
16537 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
16538 @end defmac
16540 @defmac AC_LONG_FILE_NAMES
16541 @acindex{LONG_FILE_NAMES}
16542 @code{AC_SYS_LONG_FILE_NAMES}
16543 @end defmac
16545 @defmac AC_MAJOR_HEADER
16546 @acindex{MAJOR_HEADER}
16547 @code{AC_HEADER_MAJOR}
16548 @end defmac
16550 @defmac AC_MEMORY_H
16551 @acindex{MEMORY_H}
16552 @cvindex NEED_MEMORY_H
16553 Used to define @code{NEED_MEMORY_H} if the @code{mem} functions were
16554 defined in @file{memory.h}.  Today it is equivalent to
16555 @samp{AC_CHECK_HEADERS([memory.h])}.  Adjust your code to depend upon
16556 @code{HAVE_MEMORY_H}, not @code{NEED_MEMORY_H}; see @ref{Standard
16557 Symbols}.
16558 @end defmac
16560 @defmac AC_MINGW32
16561 @acindex{MINGW32}
16562 Similar to @code{AC_CYGWIN} but checks for the MinGW compiler
16563 environment and sets @code{MINGW32}.
16564 @end defmac
16566 @defmac AC_MINUS_C_MINUS_O
16567 @acindex{MINUS_C_MINUS_O}
16568 @code{AC_PROG_CC_C_O}
16569 @end defmac
16571 @defmac AC_MMAP
16572 @acindex{MMAP}
16573 @code{AC_FUNC_MMAP}
16574 @end defmac
16576 @defmac AC_MODE_T
16577 @acindex{MODE_T}
16578 @code{AC_TYPE_MODE_T}
16579 @end defmac
16581 @defmac AC_OBJEXT
16582 @acindex{OBJEXT}
16583 @ovindex OBJEXT
16584 Defined the output variable @code{OBJEXT} based on the output of the
16585 compiler, after .c files have been excluded.  Typically set to @samp{o}
16586 if Posix, @samp{obj} if a @acronym{DOS} variant.
16587 Now the compiler checking macros handle
16588 this automatically.
16589 @end defmac
16591 @defmac AC_OBSOLETE (@var{this-macro-name}, @ovar{suggestion})
16592 @acindex{OBSOLETE}
16593 Make M4 print a message to the standard error output warning that
16594 @var{this-macro-name} is obsolete, and giving the file and line number
16595 where it was called.  @var{this-macro-name} should be the name of the
16596 macro that is calling @code{AC_OBSOLETE}.  If @var{suggestion} is given,
16597 it is printed at the end of the warning message; for example, it can be
16598 a suggestion for what to use instead of @var{this-macro-name}.
16600 For instance
16602 @example
16603 AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl
16604 @end example
16606 You are encouraged to use @code{AU_DEFUN} instead, since it gives better
16607 services to the user.
16608 @end defmac
16610 @defmac AC_OFF_T
16611 @acindex{OFF_T}
16612 @code{AC_TYPE_OFF_T}
16613 @end defmac
16615 @defmac AC_OUTPUT (@ovar{file}@dots{}, @ovar{extra-cmds}, @ovar{init-cmds})
16616 @acindex{OUTPUT}
16617 The use of @code{AC_OUTPUT} with argument is deprecated.  This obsoleted
16618 interface is equivalent to:
16620 @example
16621 @group
16622 AC_CONFIG_FILES(@var{file}@dots{})
16623 AC_CONFIG_COMMANDS([default],
16624                    @var{extra-cmds}, @var{init-cmds})
16625 AC_OUTPUT
16626 @end group
16627 @end example
16628 @end defmac
16630 @defmac AC_OUTPUT_COMMANDS (@var{extra-cmds}, @ovar{init-cmds})
16631 @acindex{OUTPUT_COMMANDS}
16632 Specify additional shell commands to run at the end of
16633 @file{config.status}, and shell commands to initialize any variables
16634 from @command{configure}.  This macro may be called multiple times.  It is
16635 obsolete, replaced by @code{AC_CONFIG_COMMANDS}.
16637 Here is an unrealistic example:
16639 @example
16640 fubar=27
16641 AC_OUTPUT_COMMANDS([echo this is extra $fubar, and so on.],
16642                    [fubar=$fubar])
16643 AC_OUTPUT_COMMANDS([echo this is another, extra, bit],
16644                    [echo init bit])
16645 @end example
16647 Aside from the fact that @code{AC_CONFIG_COMMANDS} requires an
16648 additional key, an important difference is that
16649 @code{AC_OUTPUT_COMMANDS} is quoting its arguments twice, unlike
16650 @code{AC_CONFIG_COMMANDS}.  This means that @code{AC_CONFIG_COMMANDS}
16651 can safely be given macro calls as arguments:
16653 @example
16654 AC_CONFIG_COMMANDS(foo, [my_FOO()])
16655 @end example
16657 @noindent
16658 Conversely, where one level of quoting was enough for literal strings
16659 with @code{AC_OUTPUT_COMMANDS}, you need two with
16660 @code{AC_CONFIG_COMMANDS}.  The following lines are equivalent:
16662 @example
16663 @group
16664 AC_OUTPUT_COMMANDS([echo "Square brackets: []"])
16665 AC_CONFIG_COMMANDS([default], [[echo "Square brackets: []"]])
16666 @end group
16667 @end example
16668 @end defmac
16670 @defmac AC_PID_T
16671 @acindex{PID_T}
16672 @code{AC_TYPE_PID_T}
16673 @end defmac
16675 @defmac AC_PREFIX
16676 @acindex{PREFIX}
16677 @code{AC_PREFIX_PROGRAM}
16678 @end defmac
16680 @defmac AC_PROGRAMS_CHECK
16681 @acindex{PROGRAMS_CHECK}
16682 @code{AC_CHECK_PROGS}
16683 @end defmac
16685 @defmac AC_PROGRAMS_PATH
16686 @acindex{PROGRAMS_PATH}
16687 @code{AC_PATH_PROGS}
16688 @end defmac
16690 @defmac AC_PROGRAM_CHECK
16691 @acindex{PROGRAM_CHECK}
16692 @code{AC_CHECK_PROG}
16693 @end defmac
16695 @defmac AC_PROGRAM_EGREP
16696 @acindex{PROGRAM_EGREP}
16697 @code{AC_EGREP_CPP}
16698 @end defmac
16700 @defmac AC_PROGRAM_PATH
16701 @acindex{PROGRAM_PATH}
16702 @code{AC_PATH_PROG}
16703 @end defmac
16705 @defmac AC_REMOTE_TAPE
16706 @acindex{REMOTE_TAPE}
16707 removed because of limited usefulness
16708 @end defmac
16710 @defmac AC_RESTARTABLE_SYSCALLS
16711 @acindex{RESTARTABLE_SYSCALLS}
16712 @code{AC_SYS_RESTARTABLE_SYSCALLS}
16713 @end defmac
16715 @defmac AC_RETSIGTYPE
16716 @acindex{RETSIGTYPE}
16717 @code{AC_TYPE_SIGNAL}
16718 @end defmac
16720 @defmac AC_RSH
16721 @acindex{RSH}
16722 removed because of limited usefulness
16723 @end defmac
16725 @defmac AC_SCO_INTL
16726 @acindex{SCO_INTL}
16727 @ovindex LIBS
16728 If on SCO Unix, add @option{-lintl} to output variable @code{LIBS}.  This
16729 macro used to do this:
16731 @example
16732 AC_CHECK_LIB([intl], [strftime], [LIBS="-lintl $LIBS"])
16733 @end example
16735 @noindent
16736 Now it just calls @code{AC_FUNC_STRFTIME} instead.
16737 @end defmac
16739 @defmac AC_SETVBUF_REVERSED
16740 @acindex{SETVBUF_REVERSED}
16741 @code{AC_FUNC_SETVBUF_REVERSED}
16742 @end defmac
16744 @defmac AC_SET_MAKE
16745 @acindex{SET_MAKE}
16746 @code{AC_PROG_MAKE_SET}
16747 @end defmac
16749 @defmac AC_SIZEOF_TYPE
16750 @acindex{SIZEOF_TYPE}
16751 @code{AC_CHECK_SIZEOF}
16752 @end defmac
16754 @defmac AC_SIZE_T
16755 @acindex{SIZE_T}
16756 @code{AC_TYPE_SIZE_T}
16757 @end defmac
16759 @defmac AC_STAT_MACROS_BROKEN
16760 @acindex{STAT_MACROS_BROKEN}
16761 @code{AC_HEADER_STAT}
16762 @end defmac
16764 @defmac AC_STDC_HEADERS
16765 @acindex{STDC_HEADERS}
16766 @code{AC_HEADER_STDC}
16767 @end defmac
16769 @defmac AC_STRCOLL
16770 @acindex{STRCOLL}
16771 @code{AC_FUNC_STRCOLL}
16772 @end defmac
16774 @defmac AC_ST_BLKSIZE
16775 @acindex{ST_BLKSIZE}
16776 @code{AC_CHECK_MEMBERS}
16777 @end defmac
16779 @defmac AC_ST_BLOCKS
16780 @acindex{ST_BLOCKS}
16781 @code{AC_STRUCT_ST_BLOCKS}
16782 @end defmac
16784 @defmac AC_ST_RDEV
16785 @acindex{ST_RDEV}
16786 @code{AC_CHECK_MEMBERS}
16787 @end defmac
16789 @defmac AC_SYS_RESTARTABLE_SYSCALLS
16790 @acindex{SYS_RESTARTABLE_SYSCALLS}
16791 @cvindex HAVE_RESTARTABLE_SYSCALLS
16792 If the system automatically restarts a system call that is interrupted
16793 by a signal, define @code{HAVE_RESTARTABLE_SYSCALLS}.  This macro does
16794 not check whether system calls are restarted in general---it checks whether a
16795 signal handler installed with @code{signal} (but not @code{sigaction})
16796 causes system calls to be restarted.  It does not check whether system calls
16797 can be restarted when interrupted by signals that have no handler.
16799 These days portable programs should use @code{sigaction} with
16800 @code{SA_RESTART} if they want restartable system calls.  They should
16801 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
16802 system call is restartable is a dynamic issue, not a configuration-time
16803 issue.
16804 @end defmac
16806 @defmac AC_SYS_SIGLIST_DECLARED
16807 @acindex{SYS_SIGLIST_DECLARED}
16808 @code{AC_DECL_SYS_SIGLIST}
16809 @end defmac
16811 @defmac AC_TEST_CPP
16812 @acindex{TEST_CPP}
16813 @code{AC_TRY_CPP}, replaced by @code{AC_PREPROC_IFELSE}.
16814 @end defmac
16816 @defmac AC_TEST_PROGRAM
16817 @acindex{TEST_PROGRAM}
16818 @code{AC_TRY_RUN}, replaced by @code{AC_RUN_IFELSE}.
16819 @end defmac
16821 @defmac AC_TIMEZONE
16822 @acindex{TIMEZONE}
16823 @code{AC_STRUCT_TIMEZONE}
16824 @end defmac
16826 @defmac AC_TIME_WITH_SYS_TIME
16827 @acindex{TIME_WITH_SYS_TIME}
16828 @code{AC_HEADER_TIME}
16829 @end defmac
16831 @defmac AC_TRY_COMPILE (@var{includes}, @var{function-body}, @ovar{action-if-true}, @ovar{action-if-false})
16832 @acindex{TRY_COMPILE}
16833 Same as:
16835 @example
16836 AC_COMPILE_IFELSE(
16837   [AC_LANG_PROGRAM([[@var{includes}]],
16838      [[@var{function-body}]])],
16839   [@var{action-if-true}],
16840   [@var{action-if-false}])
16841 @end example
16843 @noindent
16844 @xref{Running the Compiler}.
16846 This macro double quotes both @var{includes} and @var{function-body}.
16848 For C and C++, @var{includes} is any @code{#include} statements needed
16849 by the code in @var{function-body} (@var{includes} is ignored if
16850 the currently selected language is Fortran or Fortran 77).  The compiler
16851 and compilation flags are determined by the current language
16852 (@pxref{Language Choice}).
16853 @end defmac
16855 @defmac AC_TRY_CPP (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
16856 @acindex{TRY_CPP}
16857 Same as:
16859 @example
16860 AC_PREPROC_IFELSE(
16861   [AC_LANG_SOURCE([[@var{input}]])],
16862   [@var{action-if-true}],
16863   [@var{action-if-false}])
16864 @end example
16866 @noindent
16867 @xref{Running the Preprocessor}.
16869 This macro double quotes the @var{input}.
16870 @end defmac
16872 @defmac AC_TRY_LINK (@var{includes}, @var{function-body}, @ovar{action-if-true}, @ovar{action-if-false})
16873 @acindex{TRY_LINK}
16874 Same as:
16876 @example
16877 AC_LINK_IFELSE(
16878   [AC_LANG_PROGRAM([[@var{includes}]],
16879      [[@var{function-body}]])],
16880   [@var{action-if-true}],
16881   [@var{action-if-false}])
16882 @end example
16884 @noindent
16885 @xref{Running the Compiler}.
16887 This macro double quotes both @var{includes} and @var{function-body}.
16889 Depending on the current language (@pxref{Language Choice}), create a
16890 test program to see whether a function whose body consists of
16891 @var{function-body} can be compiled and linked.  If the file compiles
16892 and links successfully, run shell commands @var{action-if-found},
16893 otherwise run @var{action-if-not-found}.
16895 This macro double quotes both @var{includes} and @var{function-body}.
16897 For C and C++, @var{includes} is any @code{#include} statements needed
16898 by the code in @var{function-body} (@var{includes} is ignored if
16899 the currently selected language is Fortran or Fortran 77).  The compiler
16900 and compilation flags are determined by the current language
16901 (@pxref{Language Choice}), and in addition @code{LDFLAGS} and
16902 @code{LIBS} are used for linking.
16903 @end defmac
16905 @defmac AC_TRY_LINK_FUNC (@var{function}, @ovar{action-if-found}, @ovar{action-if-not-found})
16906 @acindex{TRY_LINK_FUNC}
16907 This macro is equivalent to
16908 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])],
16909 [@var{action-if-found}], [@var{action-if-not-found}])}.
16910 @end defmac
16912 @defmac AC_TRY_RUN (@var{program}, @ovar{action-if-true}, @ovar{action-if-false}, @ovar{action-if-cross-compiling})
16913 @acindex{TRY_RUN}
16914 Same as:
16916 @example
16917 AC_RUN_IFELSE(
16918   [AC_LANG_SOURCE([[@var{program}]])],
16919   [@var{action-if-true}],
16920   [@var{action-if-false}],
16921   [@var{action-if-cross-compiling}])
16922 @end example
16924 @noindent
16925 @xref{Runtime}.
16926 @end defmac
16928 @defmac AC_UID_T
16929 @acindex{UID_T}
16930 @code{AC_TYPE_UID_T}
16931 @end defmac
16933 @defmac AC_UNISTD_H
16934 @acindex{UNISTD_H}
16935 Same as @samp{AC_CHECK_HEADERS([unistd.h])}.
16936 @end defmac
16938 @defmac AC_USG
16939 @acindex{USG}
16940 @cvindex USG
16941 Define @code{USG} if the @acronym{BSD} string functions are defined in
16942 @file{strings.h}.  You should no longer depend upon @code{USG}, but on
16943 @code{HAVE_STRING_H}; see @ref{Standard Symbols}.
16944 @end defmac
16946 @defmac AC_UTIME_NULL
16947 @acindex{UTIME_NULL}
16948 @code{AC_FUNC_UTIME_NULL}
16949 @end defmac
16951 @defmac AC_VALIDATE_CACHED_SYSTEM_TUPLE (@ovar{cmd})
16952 @acindex{VALIDATE_CACHED_SYSTEM_TUPLE}
16953 If the cache file is inconsistent with the current host, target and
16954 build system types, it used to execute @var{cmd} or print a default
16955 error message.  This is now handled by default.
16956 @end defmac
16958 @defmac AC_VERBOSE (@var{result-description})
16959 @acindex{VERBOSE}
16960 @code{AC_MSG_RESULT}.
16961 @end defmac
16963 @defmac AC_VFORK
16964 @acindex{VFORK}
16965 @code{AC_FUNC_VFORK}
16966 @end defmac
16968 @defmac AC_VPRINTF
16969 @acindex{VPRINTF}
16970 @code{AC_FUNC_VPRINTF}
16971 @end defmac
16973 @defmac AC_WAIT3
16974 @acindex{WAIT3}
16975 @code{AC_FUNC_WAIT3}
16976 @end defmac
16978 @defmac AC_WARN
16979 @acindex{WARN}
16980 @code{AC_MSG_WARN}
16981 @end defmac
16983 @defmac AC_WORDS_BIGENDIAN
16984 @acindex{WORDS_BIGENDIAN}
16985 @code{AC_C_BIGENDIAN}
16986 @end defmac
16988 @defmac AC_XENIX_DIR
16989 @acindex{XENIX_DIR}
16990 @ovindex LIBS
16991 This macro used to add @option{-lx} to output variable @code{LIBS} if on
16992 Xenix.  Also, if @file{dirent.h} is being checked for, added
16993 @option{-ldir} to @code{LIBS}.  Now it is merely an alias of
16994 @code{AC_HEADER_DIRENT} instead, plus some code to detect whether
16995 running @sc{xenix} on which you should not depend:
16997 @example
16998 AC_MSG_CHECKING([for Xenix])
16999 AC_EGREP_CPP([yes],
17000 [#if defined M_XENIX && !defined M_UNIX
17001   yes
17002 #endif],
17003              [AC_MSG_RESULT([yes]); XENIX=yes],
17004              [AC_MSG_RESULT([no]); XENIX=])
17005 @end example
17006 @end defmac
17008 @defmac AC_YYTEXT_POINTER
17009 @acindex{YYTEXT_POINTER}
17010 @code{AC_DECL_YYTEXT}
17011 @end defmac
17013 @node Autoconf 1
17014 @section Upgrading From Version 1
17015 @cindex Upgrading autoconf
17016 @cindex Autoconf upgrading
17018 Autoconf version 2 is mostly backward compatible with version 1.
17019 However, it introduces better ways to do some things, and doesn't
17020 support some of the ugly things in version 1.  So, depending on how
17021 sophisticated your @file{configure.ac} files are, you might have to do
17022 some manual work in order to upgrade to version 2.  This chapter points
17023 out some problems to watch for when upgrading.  Also, perhaps your
17024 @command{configure} scripts could benefit from some of the new features in
17025 version 2; the changes are summarized in the file @file{NEWS} in the
17026 Autoconf distribution.
17028 @menu
17029 * Changed File Names::          Files you might rename
17030 * Changed Makefiles::           New things to put in @file{Makefile.in}
17031 * Changed Macros::              Macro calls you might replace
17032 * Changed Results::             Changes in how to check test results
17033 * Changed Macro Writing::       Better ways to write your own macros
17034 @end menu
17036 @node Changed File Names
17037 @subsection Changed File Names
17039 If you have an @file{aclocal.m4} installed with Autoconf (as opposed to
17040 in a particular package's source directory), you must rename it to
17041 @file{acsite.m4}.  @xref{autoconf Invocation}.
17043 If you distribute @file{install.sh} with your package, rename it to
17044 @file{install-sh} so @code{make} builtin rules don't inadvertently
17045 create a file called @file{install} from it.  @code{AC_PROG_INSTALL}
17046 looks for the script under both names, but it is best to use the new name.
17048 If you were using @file{config.h.top}, @file{config.h.bot}, or
17049 @file{acconfig.h}, you still can, but you have less clutter if you
17050 use the @code{AH_} macros.  @xref{Autoheader Macros}.
17052 @node Changed Makefiles
17053 @subsection Changed Makefiles
17055 Add @samp{@@CFLAGS@@}, @samp{@@CPPFLAGS@@}, and @samp{@@LDFLAGS@@} in
17056 your @file{Makefile.in} files, so they can take advantage of the values
17057 of those variables in the environment when @command{configure} is run.
17058 Doing this isn't necessary, but it's a convenience for users.
17060 Also add @samp{@@configure_input@@} in a comment to each input file for
17061 @code{AC_OUTPUT}, so that the output files contain a comment saying
17062 they were produced by @command{configure}.  Automatically selecting the
17063 right comment syntax for all the kinds of files that people call
17064 @code{AC_OUTPUT} on became too much work.
17066 Add @file{config.log} and @file{config.cache} to the list of files you
17067 remove in @code{distclean} targets.
17069 If you have the following in @file{Makefile.in}:
17071 @example
17072 prefix = /usr/local
17073 exec_prefix = $(prefix)
17074 @end example
17076 @noindent
17077 you must change it to:
17079 @example
17080 prefix = @@prefix@@
17081 exec_prefix = @@exec_prefix@@
17082 @end example
17084 @noindent
17085 The old behavior of replacing those variables without @samp{@@}
17086 characters around them has been removed.
17088 @node Changed Macros
17089 @subsection Changed Macros
17091 Many of the macros were renamed in Autoconf version 2.  You can still
17092 use the old names, but the new ones are clearer, and it's easier to find
17093 the documentation for them.  @xref{Obsolete Macros}, for a table showing the
17094 new names for the old macros.  Use the @command{autoupdate} program to
17095 convert your @file{configure.ac} to using the new macro names.
17096 @xref{autoupdate Invocation}.
17098 Some macros have been superseded by similar ones that do the job better,
17099 but are not call-compatible.  If you get warnings about calling obsolete
17100 macros while running @command{autoconf}, you may safely ignore them, but
17101 your @command{configure} script generally works better if you follow
17102 the advice that is printed about what to replace the obsolete macros with.  In
17103 particular, the mechanism for reporting the results of tests has
17104 changed.  If you were using @command{echo} or @code{AC_VERBOSE} (perhaps
17105 via @code{AC_COMPILE_CHECK}), your @command{configure} script's output
17106 looks better if you switch to @code{AC_MSG_CHECKING} and
17107 @code{AC_MSG_RESULT}.  @xref{Printing Messages}.  Those macros work best
17108 in conjunction with cache variables.  @xref{Caching Results}.
17112 @node Changed Results
17113 @subsection Changed Results
17115 If you were checking the results of previous tests by examining the
17116 shell variable @code{DEFS}, you need to switch to checking the values of
17117 the cache variables for those tests.  @code{DEFS} no longer exists while
17118 @command{configure} is running; it is only created when generating output
17119 files.  This difference from version 1 is because properly quoting the
17120 contents of that variable turned out to be too cumbersome and
17121 inefficient to do every time @code{AC_DEFINE} is called.  @xref{Cache
17122 Variable Names}.
17124 For example, here is a @file{configure.ac} fragment written for Autoconf
17125 version 1:
17127 @example
17128 AC_HAVE_FUNCS(syslog)
17129 case "$DEFS" in
17130 *-DHAVE_SYSLOG*) ;;
17131 *) # syslog is not in the default libraries.  See if it's in some other.
17132   saved_LIBS="$LIBS"
17133   for lib in bsd socket inet; do
17134     AC_CHECKING(for syslog in -l$lib)
17135     LIBS="-l$lib $saved_LIBS"
17136     AC_HAVE_FUNCS(syslog)
17137     case "$DEFS" in
17138     *-DHAVE_SYSLOG*) break ;;
17139     *) ;;
17140     esac
17141     LIBS="$saved_LIBS"
17142   done ;;
17143 esac
17144 @end example
17146 Here is a way to write it for version 2:
17148 @example
17149 AC_CHECK_FUNCS([syslog])
17150 if test $ac_cv_func_syslog = no; then
17151   # syslog is not in the default libraries.  See if it's in some other.
17152   for lib in bsd socket inet; do
17153     AC_CHECK_LIB([$lib], [syslog], [AC_DEFINE([HAVE_SYSLOG])
17154       LIBS="-l$lib $LIBS"; break])
17155   done
17157 @end example
17159 If you were working around bugs in @code{AC_DEFINE_UNQUOTED} by adding
17160 backslashes before quotes, you need to remove them.  It now works
17161 predictably, and does not treat quotes (except back quotes) specially.
17162 @xref{Setting Output Variables}.
17164 All of the Boolean shell variables set by Autoconf macros now use
17165 @samp{yes} for the true value.  Most of them use @samp{no} for false,
17166 though for backward compatibility some use the empty string instead.  If
17167 you were relying on a shell variable being set to something like 1 or
17168 @samp{t} for true, you need to change your tests.
17170 @node Changed Macro Writing
17171 @subsection Changed Macro Writing
17173 When defining your own macros, you should now use @code{AC_DEFUN}
17174 instead of @code{define}.  @code{AC_DEFUN} automatically calls
17175 @code{AC_PROVIDE} and ensures that macros called via @code{AC_REQUIRE}
17176 do not interrupt other macros, to prevent nested @samp{checking@dots{}}
17177 messages on the screen.  There's no actual harm in continuing to use the
17178 older way, but it's less convenient and attractive.  @xref{Macro
17179 Definitions}.
17181 You probably looked at the macros that came with Autoconf as a guide for
17182 how to do things.  It would be a good idea to take a look at the new
17183 versions of them, as the style is somewhat improved and they take
17184 advantage of some new features.
17186 If you were doing tricky things with undocumented Autoconf internals
17187 (macros, variables, diversions), check whether you need to change
17188 anything to account for changes that have been made.  Perhaps you can
17189 even use an officially supported technique in version 2 instead of
17190 kludging.  Or perhaps not.
17192 To speed up your locally written feature tests, add caching to them.
17193 See whether any of your tests are of general enough usefulness to
17194 encapsulate them into macros that you can share.
17197 @node Autoconf 2.13
17198 @section Upgrading From Version 2.13
17199 @cindex Upgrading autoconf
17200 @cindex Autoconf upgrading
17202 The introduction of the previous section (@pxref{Autoconf 1}) perfectly
17203 suits this section@enddots{}
17205 @quotation
17206 Autoconf version 2.50 is mostly backward compatible with version 2.13.
17207 However, it introduces better ways to do some things, and doesn't
17208 support some of the ugly things in version 2.13.  So, depending on how
17209 sophisticated your @file{configure.ac} files are, you might have to do
17210 some manual work in order to upgrade to version 2.50.  This chapter
17211 points out some problems to watch for when upgrading.  Also, perhaps
17212 your @command{configure} scripts could benefit from some of the new
17213 features in version 2.50; the changes are summarized in the file
17214 @file{NEWS} in the Autoconf distribution.
17215 @end quotation
17217 @menu
17218 * Changed Quotation::           Broken code which used to work
17219 * New Macros::                  Interaction with foreign macros
17220 * Hosts and Cross-Compilation::  Bugward compatibility kludges
17221 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
17222 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
17223 @end menu
17225 @node Changed Quotation
17226 @subsection Changed Quotation
17228 The most important changes are invisible to you: the implementation of
17229 most macros have completely changed.  This allowed more factorization of
17230 the code, better error messages, a higher uniformity of the user's
17231 interface etc.  Unfortunately, as a side effect, some construct which
17232 used to (miraculously) work might break starting with Autoconf 2.50.
17233 The most common culprit is bad quotation.
17235 For instance, in the following example, the message is not properly
17236 quoted:
17238 @example
17239 AC_INIT
17240 AC_CHECK_HEADERS(foo.h, ,
17241   AC_MSG_ERROR(cannot find foo.h, bailing out))
17242 AC_OUTPUT
17243 @end example
17245 @noindent
17246 Autoconf 2.13 simply ignores it:
17248 @example
17249 $ @kbd{autoconf-2.13; ./configure --silent}
17250 creating cache ./config.cache
17251 configure: error: cannot find foo.h
17253 @end example
17255 @noindent
17256 while Autoconf 2.50 produces a broken @file{configure}:
17258 @example
17259 $ @kbd{autoconf-2.50; ./configure --silent}
17260 configure: error: cannot find foo.h
17261 ./configure: exit: bad non-numeric arg `bailing'
17262 ./configure: exit: bad non-numeric arg `bailing'
17264 @end example
17266 The message needs to be quoted, and the @code{AC_MSG_ERROR} invocation
17267 too!
17269 @example
17270 AC_INIT([Example], [1.0], [bug-example@@example.org])
17271 AC_CHECK_HEADERS([foo.h], [],
17272   [AC_MSG_ERROR([cannot find foo.h, bailing out])])
17273 AC_OUTPUT
17274 @end example
17276 Many many (and many more) Autoconf macros were lacking proper quotation,
17277 including no less than@dots{} @code{AC_DEFUN} itself!
17279 @example
17280 $ @kbd{cat configure.in}
17281 AC_DEFUN([AC_PROG_INSTALL],
17282 [# My own much better version
17284 AC_INIT
17285 AC_PROG_INSTALL
17286 AC_OUTPUT
17287 $ @kbd{autoconf-2.13}
17288 autoconf: Undefined macros:
17289 ***BUG in Autoconf--please report*** AC_FD_MSG
17290 ***BUG in Autoconf--please report*** AC_EPI
17291 configure.in:1:AC_DEFUN([AC_PROG_INSTALL],
17292 configure.in:5:AC_PROG_INSTALL
17293 $ @kbd{autoconf-2.50}
17295 @end example
17298 @node New Macros
17299 @subsection New Macros
17301 @cindex undefined macro
17302 @cindex @code{_m4_divert_diversion}
17304 While Autoconf was relatively dormant in the late 1990s, Automake
17305 provided Autoconf-like macros for a while.  Starting with Autoconf 2.50
17306 in 2001, Autoconf provided
17307 versions of these macros, integrated in the @code{AC_} namespace,
17308 instead of @code{AM_}.  But in order to ease the upgrading via
17309 @command{autoupdate}, bindings to such @code{AM_} macros are provided.
17311 Unfortunately older versions of Automake (e.g., Automake 1.4)
17312 did not quote the names of these macros.
17313 Therefore, when @command{m4} finds something like
17314 @samp{AC_DEFUN(AM_TYPE_PTRDIFF_T, @dots{})} in @file{aclocal.m4},
17315 @code{AM_TYPE_PTRDIFF_T} is
17316 expanded, replaced with its Autoconf definition.
17318 Fortunately Autoconf catches pre-@code{AC_INIT} expansions, and
17319 complains, in its own words:
17321 @example
17322 $ @kbd{cat configure.ac}
17323 AC_INIT([Example], [1.0], [bug-example@@example.org])
17324 AM_TYPE_PTRDIFF_T
17325 $ @kbd{aclocal-1.4}
17326 $ @kbd{autoconf}
17327 aclocal.m4:17: error: m4_defn: undefined macro: _m4_divert_diversion
17328 aclocal.m4:17: the top level
17329 autom4te: m4 failed with exit status: 1
17331 @end example
17333 Modern versions of Automake no longer define most of these
17334 macros, and properly quote the names of the remaining macros.
17335 If you must use an old Automake, do not depend upon macros from Automake
17336 as it is simply not its job
17337 to provide macros (but the one it requires itself):
17339 @example
17340 $ @kbd{cat configure.ac}
17341 AC_INIT([Example], [1.0], [bug-example@@example.org])
17342 AM_TYPE_PTRDIFF_T
17343 $ @kbd{rm aclocal.m4}
17344 $ @kbd{autoupdate}
17345 autoupdate: `configure.ac' is updated
17346 $ @kbd{cat configure.ac}
17347 AC_INIT([Example], [1.0], [bug-example@@example.org])
17348 AC_CHECK_TYPES([ptrdiff_t])
17349 $ @kbd{aclocal-1.4}
17350 $ @kbd{autoconf}
17352 @end example
17355 @node Hosts and Cross-Compilation
17356 @subsection Hosts and Cross-Compilation
17357 @cindex Cross compilation
17359 Based on the experience of compiler writers, and after long public
17360 debates, many aspects of the cross-compilation chain have changed:
17362 @itemize @minus
17363 @item
17364 the relationship between the build, host, and target architecture types,
17366 @item
17367 the command line interface for specifying them to @command{configure},
17369 @item
17370 the variables defined in @command{configure},
17372 @item
17373 the enabling of cross-compilation mode.
17374 @end itemize
17376 @sp 1
17378 The relationship between build, host, and target have been cleaned up:
17379 the chain of default is now simply: target defaults to host, host to
17380 build, and build to the result of @command{config.guess}.  Nevertheless,
17381 in order to ease the transition from 2.13 to 2.50, the following
17382 transition scheme is implemented.  @emph{Do not rely on it}, as it will
17383 be completely disabled in a couple of releases (we cannot keep it, as it
17384 proves to cause more problems than it cures).
17386 They all default to the result of running @command{config.guess}, unless
17387 you specify either @option{--build} or @option{--host}.  In this case,
17388 the default becomes the system type you specified.  If you specify both,
17389 and they're different, @command{configure} enters cross compilation
17390 mode, so it doesn't run any tests that require execution.
17392 Hint: if you mean to override the result of @command{config.guess},
17393 prefer @option{--build} over @option{--host}.  In the future,
17394 @option{--host} will not override the name of the build system type.
17395 Whenever you specify @option{--host}, be sure to specify @option{--build}
17396 too.
17398 @sp 1
17400 For backward compatibility, @command{configure} accepts a system
17401 type as an option by itself.  Such an option overrides the
17402 defaults for build, host, and target system types.  The following
17403 configure statement configures a cross toolchain that runs on
17404 Net@acronym{BSD}/alpha but generates code for @acronym{GNU} Hurd/sparc,
17405 which is also the build platform.
17407 @example
17408 ./configure --host=alpha-netbsd sparc-gnu
17409 @end example
17411 @sp 1
17413 In Autoconf 2.13 and before, the variables @code{build}, @code{host},
17414 and @code{target} had a different semantics before and after the
17415 invocation of @code{AC_CANONICAL_BUILD} etc.  Now, the argument of
17416 @option{--build} is strictly copied into @code{build_alias}, and is left
17417 empty otherwise.  After the @code{AC_CANONICAL_BUILD}, @code{build} is
17418 set to the canonicalized build type.  To ease the transition, before,
17419 its contents is the same as that of @code{build_alias}.  Do @emph{not}
17420 rely on this broken feature.
17422 For consistency with the backward compatibility scheme exposed above,
17423 when @option{--host} is specified but @option{--build} isn't, the build
17424 system is assumed to be the same as @option{--host}, and
17425 @samp{build_alias} is set to that value.  Eventually, this
17426 historically incorrect behavior will go away.
17428 @sp 1
17430 The former scheme to enable cross-compilation proved to cause more harm
17431 than good, in particular, it used to be triggered too easily, leaving
17432 regular end users puzzled in front of cryptic error messages.
17433 @command{configure} could even enter cross-compilation mode only
17434 because the compiler was not functional.  This is mainly because
17435 @command{configure} used to try to detect cross-compilation, instead of
17436 waiting for an explicit flag from the user.
17438 Now, @command{configure} enters cross-compilation mode if and only if
17439 @option{--host} is passed.
17441 That's the short documentation.  To ease the transition between 2.13 and
17442 its successors, a more complicated scheme is implemented.  @emph{Do not
17443 rely on the following}, as it will be removed in the near future.
17445 If you specify @option{--host}, but not @option{--build}, when
17446 @command{configure} performs the first compiler test it tries to run
17447 an executable produced by the compiler.  If the execution fails, it
17448 enters cross-compilation mode.  This is fragile.  Moreover, by the time
17449 the compiler test is performed, it may be too late to modify the
17450 build-system type: other tests may have already been performed.
17451 Therefore, whenever you specify @option{--host}, be sure to specify
17452 @option{--build} too.
17454 @example
17455 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
17456 @end example
17458 @noindent
17459 enters cross-compilation mode.  The former interface, which
17460 consisted in setting the compiler to a cross-compiler without informing
17461 @command{configure} is obsolete.  For instance, @command{configure}
17462 fails if it can't run the code generated by the specified compiler if you
17463 configure as follows:
17465 @example
17466 ./configure CC=m68k-coff-gcc
17467 @end example
17470 @node AC_LIBOBJ vs LIBOBJS
17471 @subsection @code{AC_LIBOBJ} vs.@: @code{LIBOBJS}
17473 Up to Autoconf 2.13, the replacement of functions was triggered via the
17474 variable @code{LIBOBJS}.  Since Autoconf 2.50, the macro
17475 @code{AC_LIBOBJ} should be used instead (@pxref{Generic Functions}).
17476 Starting at Autoconf 2.53, the use of @code{LIBOBJS} is an error.
17478 This change is mandated by the unification of the @acronym{GNU} Build System
17479 components.  In particular, the various fragile techniques used to parse
17480 a @file{configure.ac} are all replaced with the use of traces.  As a
17481 consequence, any action must be traceable, which obsoletes critical
17482 variable assignments.  Fortunately, @code{LIBOBJS} was the only problem,
17483 and it can even be handled gracefully (read, ``without your having to
17484 change something'').
17486 There were two typical uses of @code{LIBOBJS}: asking for a replacement
17487 function, and adjusting @code{LIBOBJS} for Automake and/or Libtool.
17489 @sp 1
17491 As for function replacement, the fix is immediate: use
17492 @code{AC_LIBOBJ}.  For instance:
17494 @example
17495 LIBOBJS="$LIBOBJS fnmatch.o"
17496 LIBOBJS="$LIBOBJS malloc.$ac_objext"
17497 @end example
17499 @noindent
17500 should be replaced with:
17502 @example
17503 AC_LIBOBJ([fnmatch])
17504 AC_LIBOBJ([malloc])
17505 @end example
17507 @sp 1
17509 @ovindex LIBOBJDIR
17510 When used with Automake 1.10 or newer, a suitable value for
17511 @code{LIBOBJDIR} is set so that the @code{LIBOBJS} and @code{LTLIBOBJS}
17512 can be referenced from any @file{Makefile.am}.  Even without Automake,
17513 arranging for @code{LIBOBJDIR} to be set correctly enables
17514 referencing @code{LIBOBJS} and @code{LTLIBOBJS} in another directory.
17515 The @code{LIBOJBDIR} feature is experimental.
17518 @node AC_FOO_IFELSE vs AC_TRY_FOO
17519 @subsection @code{AC_FOO_IFELSE} vs.@: @code{AC_TRY_FOO}
17521 Since Autoconf 2.50, internal codes uses @code{AC_PREPROC_IFELSE},
17522 @code{AC_COMPILE_IFELSE}, @code{AC_LINK_IFELSE}, and
17523 @code{AC_RUN_IFELSE} on one hand and @code{AC_LANG_SOURCES},
17524 and @code{AC_LANG_PROGRAM} on the other hand instead of the deprecated
17525 @code{AC_TRY_CPP}, @code{AC_TRY_COMPILE}, @code{AC_TRY_LINK}, and
17526 @code{AC_TRY_RUN}.  The motivations where:
17527 @itemize @minus
17528 @item
17529 a more consistent interface: @code{AC_TRY_COMPILE} etc.@: were double
17530 quoting their arguments;
17532 @item
17533 the combinatoric explosion is solved by decomposing on the one hand the
17534 generation of sources, and on the other hand executing the program;
17536 @item
17537 this scheme helps supporting more languages than plain C and C++.
17538 @end itemize
17540 In addition to the change of syntax, the philosophy has changed too:
17541 while emphasis was put on speed at the expense of accuracy, today's
17542 Autoconf promotes accuracy of the testing framework at, ahem@dots{}, the
17543 expense of speed.
17546 As a perfect example of what is @emph{not} to be done, here is how to
17547 find out whether a header file contains a particular declaration, such
17548 as a typedef, a structure, a structure member, or a function.  Use
17549 @code{AC_EGREP_HEADER} instead of running @code{grep} directly on the
17550 header file; on some systems the symbol might be defined in another
17551 header file that the file you are checking includes.
17553 As a (bad) example, here is how you should not check for C preprocessor
17554 symbols, either defined by header files or predefined by the C
17555 preprocessor: using @code{AC_EGREP_CPP}:
17557 @example
17558 @group
17559 AC_EGREP_CPP(yes,
17560 [#ifdef _AIX
17561   yes
17562 #endif
17563 ], is_aix=yes, is_aix=no)
17564 @end group
17565 @end example
17567 The above example, properly written would (i) use
17568 @code{AC_LANG_PROGRAM}, and (ii) run the compiler:
17570 @example
17571 @group
17572 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
17573 [[#if !defined _AIX
17574  error: This isn't AIX!
17575 #endif
17576 ]])],
17577                    [is_aix=yes],
17578                    [is_aix=no])
17579 @end group
17580 @end example
17583 @c ============================= Generating Test Suites with Autotest
17585 @node Using Autotest
17586 @chapter Generating Test Suites with Autotest
17588 @cindex Autotest
17590 @display
17591 @strong{N.B.: This section describes an experimental feature which will
17592 be part of Autoconf in a forthcoming release.  Although we believe
17593 Autotest is stabilizing, this documentation describes an interface which
17594 might change in the future: do not depend upon Autotest without
17595 subscribing to the Autoconf mailing lists.}
17596 @end display
17598 It is paradoxical that portable projects depend on nonportable tools
17599 to run their test suite.  Autoconf by itself is the paragon of this
17600 problem: although it aims at perfectly portability, up to 2.13 its
17601 test suite was using Deja@acronym{GNU}, a rich and complex testing
17602 framework, but which is far from being standard on Posix systems.
17603 Worse yet, it was likely to be missing on the most fragile platforms,
17604 the very platforms that are most likely to torture Autoconf and
17605 exhibit deficiencies.
17607 To circumvent this problem, many package maintainers have developed their
17608 own testing framework, based on simple shell scripts whose sole outputs
17609 are exit status values describing whether the test succeeded.  Most of
17610 these tests share common patterns, and this can result in lots of
17611 duplicated code and tedious maintenance.
17613 Following exactly the same reasoning that yielded to the inception of
17614 Autoconf, Autotest provides a test suite generation framework, based on
17615 M4 macros building a portable shell script.  The suite itself is
17616 equipped with automatic logging and tracing facilities which greatly
17617 diminish the interaction with bug reporters, and simple timing reports.
17619 Autoconf itself has been using Autotest for years, and we do attest that
17620 it has considerably improved the strength of the test suite and the
17621 quality of bug reports.  Other projects are known to use some generation
17622 of Autotest, such as Bison, Free Recode, Free Wdiff, @acronym{GNU} Tar, each of
17623 them with different needs, and this usage has validated Autotest as a general
17624 testing framework.
17626 Nonetheless, compared to Deja@acronym{GNU}, Autotest is inadequate for
17627 interactive tool testing, which is probably its main limitation.
17629 @menu
17630 * Using an Autotest Test Suite::  Autotest and the user
17631 * Writing testsuite.at::        Autotest macros
17632 * testsuite Invocation::        Running @command{testsuite} scripts
17633 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
17634 @end menu
17636 @node Using an Autotest Test Suite
17637 @section Using an Autotest Test Suite
17639 @menu
17640 * testsuite Scripts::           The concepts of Autotest
17641 * Autotest Logs::               Their contents
17642 @end menu
17644 @node testsuite Scripts
17645 @subsection @command{testsuite} Scripts
17647 @cindex @command{testsuite}
17649 Generating testing or validation suites using Autotest is rather easy.
17650 The whole validation suite is held in a file to be processed through
17651 @command{autom4te}, itself using @acronym{GNU} M4 under the scene, to
17652 produce a stand-alone Bourne shell script which then gets distributed.
17653 Neither @command{autom4te} nor @acronym{GNU} M4 are needed at
17654 the installer's end.
17656 @cindex test group
17657 Each test of the validation suite should be part of some test group.  A
17658 @dfn{test group} is a sequence of interwoven tests that ought to be
17659 executed together, usually because one test in the group creates data
17660 files than a later test in the same group needs to read.  Complex test
17661 groups make later debugging more tedious.  It is much better to
17662 keep only a few tests per test group.  Ideally there is only one test
17663 per test group.
17665 For all but the simplest packages, some file such as @file{testsuite.at}
17666 does not fully hold all test sources, as these are often easier to
17667 maintain in separate files.  Each of these separate files holds a single
17668 test group, or a sequence of test groups all addressing some common
17669 functionality in the package.  In such cases, @file{testsuite.at}
17670 merely initializes the validation suite, and sometimes does elementary
17671 health checking, before listing include statements for all other test
17672 files.  The special file @file{package.m4}, containing the
17673 identification of the package, is automatically included if found.
17675 A convenient alternative consists in moving all the global issues
17676 (local Autotest macros, elementary health checking, and @code{AT_INIT}
17677 invocation) into the file @code{local.at}, and making
17678 @file{testsuite.at} be a simple list of @code{m4_include} of sub test
17679 suites.  In such case, generating the whole test suite or pieces of it
17680 is only a matter of choosing the @command{autom4te} command line
17681 arguments.
17683 The validation scripts that Autotest produces are by convention called
17684 @command{testsuite}.  When run, @command{testsuite} executes each test
17685 group in turn, producing only one summary line per test to say if that
17686 particular test succeeded or failed.  At end of all tests, summarizing
17687 counters get printed.  One debugging directory is left for each test
17688 group which failed, if any: such directories are named
17689 @file{testsuite.dir/@var{nn}}, where @var{nn} is the sequence number of
17690 the test group, and they include:
17692 @itemize @bullet
17693 @item a debugging script named @file{run} which reruns the test in
17694 @dfn{debug mode} (@pxref{testsuite Invocation}).  The automatic generation
17695 of debugging scripts has the purpose of easing the chase for bugs.
17697 @item all the files created with @code{AT_DATA}
17699 @item a log of the run, named @file{testsuite.log}
17700 @end itemize
17702 In the ideal situation, none of the tests fail, and consequently no
17703 debugging directory is left behind for validation.
17705 It often happens in practice that individual tests in the validation
17706 suite need to get information coming out of the configuration process.
17707 Some of this information, common for all validation suites, is provided
17708 through the file @file{atconfig}, automatically created by
17709 @code{AC_CONFIG_TESTDIR}.  For configuration informations which your
17710 testing environment specifically needs, you might prepare an optional
17711 file named @file{atlocal.in}, instantiated by @code{AC_CONFIG_FILES}.
17712 The configuration process produces @file{atconfig} and @file{atlocal}
17713 out of these two input files, and these two produced files are
17714 automatically read by the @file{testsuite} script.
17716 Here is a diagram showing the relationship between files.
17718 @noindent
17719 Files used in preparing a software package for distribution:
17721 @example
17722                 [package.m4] -->.
17723                                  \
17724 subfile-1.at ->.  [local.at] ---->+
17725     ...         \                  \
17726 subfile-i.at ---->-- testsuite.at -->-- autom4te* -->testsuite
17727     ...         /
17728 subfile-n.at ->'
17729 @end example
17731 @noindent
17732 Files used in configuring a software package:
17734 @example
17735                                      .--> atconfig
17736                                     /
17737 [atlocal.in] -->  config.status* --<
17738                                     \
17739                                      `--> [atlocal]
17740 @end example
17742 @noindent
17743 Files created during the test suite execution:
17745 @example
17746 atconfig -->.                    .--> testsuite.log
17747              \                  /
17748               >-- testsuite* --<
17749              /                  \
17750 [atlocal] ->'                    `--> [testsuite.dir]
17751 @end example
17754 @node Autotest Logs
17755 @subsection Autotest Logs
17757 When run, the test suite creates a log file named after itself, e.g., a
17758 test suite named @command{testsuite} creates @file{testsuite.log}.  It
17759 contains a lot of information, usually more than maintainers actually
17760 need, but therefore most of the time it contains all that is needed:
17762 @table @asis
17763 @item command line arguments
17764 @c akim s/to consist in/to consist of/
17765 A bad but unfortunately widespread habit consists of
17766 setting environment variables before the command, such as in
17767 @samp{CC=my-home-grown-cc ./testsuite}.  The test suite does not
17768 know this change, hence (i) it cannot report it to you, and (ii)
17769 it cannot preserve the value of @code{CC} for subsequent runs.
17770 Autoconf faced exactly the same problem, and solved it by asking
17771 users to pass the variable definitions as command line arguments.
17772 Autotest requires this rule, too, but has no means to enforce it; the log
17773 then contains a trace of the variables that were changed by the user.
17775 @item @file{ChangeLog} excerpts
17776 The topmost lines of all the @file{ChangeLog} files found in the source
17777 hierarchy.  This is especially useful when bugs are reported against
17778 development versions of the package, since the version string does not
17779 provide sufficient information to know the exact state of the sources
17780 the user compiled.  Of course, this relies on the use of a
17781 @file{ChangeLog}.
17783 @item build machine
17784 Running a test suite in a cross-compile environment is not an easy task,
17785 since it would mean having the test suite run on a machine @var{build},
17786 while running programs on a machine @var{host}.  It is much simpler to
17787 run both the test suite and the programs on @var{host}, but then, from
17788 the point of view of the test suite, there remains a single environment,
17789 @var{host} = @var{build}.  The log contains relevant information on the
17790 state of the build machine, including some important environment
17791 variables.
17792 @c FIXME: How about having an M4sh macro to say `hey, log the value
17793 @c of `@dots{}'?  This would help both Autoconf and Autotest.
17795 @item tested programs
17796 The absolute file name and answers to @option{--version} of the tested
17797 programs (see @ref{Writing testsuite.at}, @code{AT_TESTED}).
17799 @item configuration log
17800 The contents of @file{config.log}, as created by @command{configure},
17801 are appended.  It contains the configuration flags and a detailed report
17802 on the configuration itself.
17803 @end table
17806 @node Writing testsuite.at
17807 @section Writing @file{testsuite.at}
17809 The @file{testsuite.at} is a Bourne shell script making use of special
17810 Autotest M4 macros.  It often contains a call to @code{AT_INIT} near
17811 its beginning followed by one call to @code{m4_include} per source file
17812 for tests.  Each such included file, or the remainder of
17813 @file{testsuite.at} if include files are not used, contain a sequence of
17814 test groups.  Each test group begins with a call to @code{AT_SETUP},
17815 then an arbitrary number of shell commands or calls to @code{AT_CHECK},
17816 and then completes with a call to @code{AT_CLEANUP}.
17818 @defmac AT_INIT (@ovar{name})
17819 @atindex{INIT}
17820 @c FIXME: Not clear, plus duplication of the information.
17821 Initialize Autotest.  Giving a @var{name} to the test suite is
17822 encouraged if your package includes several test suites.  In any case,
17823 the test suite always displays the package name and version.  It also
17824 inherits the package bug report address.
17825 @end defmac
17827 @defmac AT_COPYRIGHT (@var{copyright-notice})
17828 @atindex{COPYRIGHT}
17829 @cindex Copyright Notice
17830 State that, in addition to the Free Software Foundation's copyright on
17831 the Autotest macros, parts of your test suite are covered by
17832 @var{copyright-notice}.
17834 The @var{copyright-notice} shows up in both the head of
17835 @command{testsuite} and in @samp{testsuite --version}.
17836 @end defmac
17838 @defmac AT_TESTED (@var{executables})
17839 @atindex{TESTED}
17840 Log the file name and answer to @option{--version} of each program in
17841 space-separated list @var{executables}.  Several invocations register
17842 new executables, in other words, don't fear registering one program
17843 several times.
17844 @end defmac
17846 Autotest test suites rely on @env{PATH} to find the tested program.
17847 This avoids the need to generate absolute names of the various tools, and
17848 makes it possible to test installed programs.  Therefore, knowing which
17849 programs are being exercised is crucial to understanding problems in
17850 the test suite itself, or its occasional misuses.  It is a good idea to
17851 also subscribe foreign programs you depend upon, to avoid incompatible
17852 diagnostics.
17854 @sp 1
17856 @defmac AT_SETUP (@var{test-group-name})
17857 @atindex{SETUP}
17858 This macro starts a group of related tests, all to be executed in the
17859 same subshell.  It accepts a single argument, which holds a few words
17860 (no more than about 30 or 40 characters) quickly describing the purpose
17861 of the test group being started.
17862 @end defmac
17864 @defmac AT_KEYWORDS (@var{keywords})
17865 @atindex{KEYWORDS}
17866 Associate the space-separated list of @var{keywords} to the enclosing
17867 test group.  This makes it possible to run ``slices'' of the test suite.
17868 For instance, if some of your test groups exercise some @samp{foo}
17869 feature, then using @samp{AT_KEYWORDS(foo)} lets you run
17870 @samp{./testsuite -k foo} to run exclusively these test groups.  The
17871 @var{title} of the test group is automatically recorded to
17872 @code{AT_KEYWORDS}.
17874 Several invocations within a test group accumulate new keywords.  In
17875 other words, don't fear registering the same keyword several times in a
17876 test group.
17877 @end defmac
17879 @defmac AT_CAPTURE_FILE (@var{file})
17880 @atindex{CAPTURE_FILE}
17881 If the current test group fails, log the contents of @var{file}.
17882 Several identical calls within one test group have no additional effect.
17883 @end defmac
17885 @defmac AT_XFAIL_IF (@var{shell-condition})
17886 @atindex{XFAIL_IF}
17887 Determine whether the test is expected to fail because it is a known
17888 bug (for unsupported features, you should skip the test).
17889 @var{shell-condition} is a shell expression such as a @code{test}
17890 command; you can instantiate this macro many times from within the
17891 same test group, and one of the conditions is enough to turn
17892 the test into an expected failure.
17893 @end defmac
17895 @defmac AT_CLEANUP
17896 @atindex{CLEANUP}
17897 End the current test group.
17898 @end defmac
17900 @sp 1
17902 @defmac AT_DATA (@var{file}, @var{contents})
17903 @atindex{DATA}
17904 Initialize an input data @var{file} with given @var{contents}.  Of
17905 course, the @var{contents} have to be properly quoted between square
17906 brackets to protect against included commas or spurious M4
17907 expansion.  The contents ought to end with an end of line.
17908 @end defmac
17910 @defmac AT_CHECK (@var{commands}, @dvar{status, 0}, @dvar{stdout, }, @dvar{stderr, }, @ovar{run-if-fail}, @ovar{run-if-pass})
17911 @atindex{CHECK}
17912 Execute a test by performing given shell @var{commands}.  These commands
17913 should normally exit with @var{status}, while producing expected
17914 @var{stdout} and @var{stderr} contents.  If @var{commands} exit with
17915 status 77, then the whole test group is skipped.  Otherwise, if this test
17916 fails, run shell commands @var{run-if-fail} or, if this test passes, run shell
17917 commands @var{run-if-pass}.
17919 The @var{commands} @emph{must not} redirect the standard output, nor the
17920 standard error.
17922 If @var{status}, or @var{stdout}, or @var{stderr} is @samp{ignore}, then
17923 the corresponding value is not checked.
17925 The special value @samp{expout} for @var{stdout} means the expected
17926 output of the @var{commands} is the content of the file @file{expout}.
17927 If @var{stdout} is @samp{stdout}, then the standard output of the
17928 @var{commands} is available for further tests in the file @file{stdout}.
17929 Similarly for @var{stderr} with @samp{expout} and @samp{stderr}.
17930 @end defmac
17933 @node testsuite Invocation
17934 @section Running @command{testsuite} Scripts
17935 @cindex @command{testsuite}
17937 Autotest test suites support the following arguments:
17939 @table @option
17940 @item --help
17941 @itemx -h
17942 Display the list of options and exit successfully.
17944 @item --version
17945 @itemx -V
17946 Display the version of the test suite and exit successfully.
17948 @item --clean
17949 @itemx -c
17950 Remove all the files the test suite might have created and exit.  Meant
17951 for @code{clean} Make targets.
17953 @item --list
17954 @itemx -l
17955 List all the tests (or only the selection), including their possible
17956 keywords.
17957 @end table
17959 @sp 1
17961 By default all tests are performed (or described with
17962 @option{--list}) in the default environment first silently, then
17963 verbosely, but the environment, set of tests, and verbosity level can be
17964 tuned:
17966 @table @samp
17967 @item @var{variable}=@var{value}
17968 Set the environment @var{variable} to @var{value}.  Use this rather
17969 than @samp{FOO=foo ./testsuite} as debugging scripts would then run in a
17970 different environment.
17972 @cindex @code{AUTOTEST_PATH}
17973 The variable @code{AUTOTEST_PATH} specifies the testing path to prepend
17974 to @env{PATH}.  Relative directory names (not starting with
17975 @samp{/}) are considered to be relative to the top level of the
17976 package being built.  All directories are made absolute, first
17977 starting from the top level @emph{build} tree, then from the
17978 @emph{source} tree.  For instance @samp{./testsuite
17979 AUTOTEST_PATH=tests:bin} for a @file{/src/foo-1.0} source package built
17980 in @file{/tmp/foo} results in @samp{/tmp/foo/tests:/tmp/foo/bin} and
17981 then @samp{/src/foo-1.0/tests:/src/foo-1.0/bin} being prepended to
17982 @env{PATH}.
17984 @item @var{number}
17985 @itemx @var{number}-@var{number}
17986 @itemx @var{number}-
17987 @itemx -@var{number}
17988 Add the corresponding test groups, with obvious semantics, to the
17989 selection.
17991 @item --keywords=@var{keywords}
17992 @itemx -k @var{keywords}
17993 Add to the selection the test groups with title or keywords (arguments
17994 to @code{AT_SETUP} or @code{AT_KEYWORDS}) that match @emph{all} keywords
17995 of the comma separated list @var{keywords}, case-insensitively.  Use
17996 @samp{!} immediately before the keyword to invert the selection for this
17997 keyword.  By default, the keywords match whole words; enclose them in
17998 @samp{.*} to also match parts of words.
18000 For example, running
18002 @example
18003 @kbd{./testsuite -k 'autoupdate,.*FUNC.*'}
18004 @end example
18006 @noindent
18007 selects all tests tagged @samp{autoupdate} @emph{and} with tags
18008 containing @samp{FUNC} (as in @samp{AC_CHECK_FUNC}, @samp{AC_FUNC_FNMATCH},
18009 etc.), while
18011 @example
18012 @kbd{./testsuite -k '!autoupdate' -k '.*FUNC.*'}
18013 @end example
18015 @noindent
18016 selects all tests not tagged @samp{autoupdate} @emph{or} with tags
18017 containing @samp{FUNC}.
18019 @item --errexit
18020 @itemx -e
18021 If any test fails, immediately abort testing.  It implies
18022 @option{--debug}: post test group clean up, and top-level logging
18023 are inhibited.  This option is meant for the full test
18024 suite, it is not really useful for generated debugging scripts.
18026 @item --verbose
18027 @itemx -v
18028 Force more verbosity in the detailed output of what is being done.  This
18029 is the default for debugging scripts.
18031 @item --debug
18032 @itemx -d
18033 Do not remove the files after a test group was performed ---but they are
18034 still removed @emph{before}, therefore using this option is sane when
18035 running several test groups.  Create debugging scripts.  Do not
18036 overwrite the top-level
18037 log (in order to preserve supposedly existing full log file).  This is
18038 the default for debugging scripts, but it can also be useful to debug
18039 the testsuite itself.
18041 @item --trace
18042 @itemx -x
18043 Trigger shell tracing of the test groups.
18044 @end table
18047 @node Making testsuite Scripts
18048 @section Making @command{testsuite} Scripts
18050 For putting Autotest into movement, you need some configuration and
18051 makefile machinery.  We recommend, at least if your package uses deep or
18052 shallow hierarchies, that you use @file{tests/} as the name of the
18053 directory holding all your tests and their makefile.  Here is a
18054 check list of things to do.
18056 @itemize @minus
18058 @item
18059 @cindex @file{package.m4}
18060 Make sure to create the file @file{package.m4}, which defines the
18061 identity of the package.  It must define @code{AT_PACKAGE_STRING}, the
18062 full signature of the package, and @code{AT_PACKAGE_BUGREPORT}, the
18063 address to which bug reports should be sent.  For sake of completeness,
18064 we suggest that you also define @code{AT_PACKAGE_NAME},
18065 @code{AT_PACKAGE_TARNAME}, and @code{AT_PACKAGE_VERSION}.
18066 @xref{Initializing configure}, for a description of these variables.  We
18067 suggest the following makefile excerpt:
18069 @smallexample
18070 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
18071         @{                                      \
18072           echo '# Signature of the current package.'; \
18073           echo 'm4_define([AT_PACKAGE_NAME],      [@@PACKAGE_NAME@@])'; \
18074           echo 'm4_define([AT_PACKAGE_TARNAME],   [@@PACKAGE_TARNAME@@])'; \
18075           echo 'm4_define([AT_PACKAGE_VERSION],   [@@PACKAGE_VERSION@@])'; \
18076           echo 'm4_define([AT_PACKAGE_STRING],    [@@PACKAGE_STRING@@])'; \
18077           echo 'm4_define([AT_PACKAGE_BUGREPORT], [@@PACKAGE_BUGREPORT@@])'; \
18078         @} >'$(srcdir)/package.m4'
18079 @end smallexample
18081 @noindent
18082 Be sure to distribute @file{package.m4} and to put it into the source
18083 hierarchy: the test suite ought to be shipped!
18085 @item
18086 Invoke @code{AC_CONFIG_TESTDIR}.
18088 @defmac AC_CONFIG_TESTDIR (@var{directory}, @dvar{test-path, directory})
18089 @acindex{CONFIG_TESTDIR}
18090 An Autotest test suite is to be configured in @var{directory}.  This
18091 macro requires the instantiation of @file{@var{directory}/atconfig} from
18092 @file{@var{directory}/atconfig.in}, and sets the default
18093 @code{AUTOTEST_PATH} to @var{test-path} (@pxref{testsuite Invocation}).
18094 @end defmac
18096 @item
18097 Still within @file{configure.ac}, as appropriate, ensure that some
18098 @code{AC_CONFIG_FILES} command includes substitution for
18099 @file{tests/atlocal}.
18101 @item
18102 The @file{tests/Makefile.in} should be modified so the validation in
18103 your package is triggered by @samp{make check}.  An example is provided
18104 below.
18105 @end itemize
18107 With Automake, here is a minimal example about how to link @samp{make
18108 check} with a validation suite.
18110 @example
18111 EXTRA_DIST = testsuite.at $(TESTSUITE) atlocal.in
18112 TESTSUITE = $(srcdir)/testsuite
18114 check-local: atconfig atlocal $(TESTSUITE)
18115         $(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS)
18117 installcheck-local: atconfig atlocal $(TESTSUITE)
18118         $(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \
18119           $(TESTSUITEFLAGS)
18121 clean-local:
18122         test ! -f '$(TESTSUITE)' || \
18123          $(SHELL) '$(TESTSUITE)' --clean
18125 AUTOTEST = $(AUTOM4TE) --language=autotest
18126 $(TESTSUITE): $(srcdir)/testsuite.at
18127         $(AUTOTEST) -I '$(srcdir)' -o $@@.tmp $@@.at
18128         mv $@@.tmp $@@
18129 @end example
18131 You might want to list explicitly the dependencies, i.e., the list of
18132 the files @file{testsuite.at} includes.
18134 With strict Autoconf, you might need to add lines inspired from the
18135 following:
18137 @example
18138 subdir = tests
18140 atconfig: $(top_builddir)/config.status
18141         cd $(top_builddir) && \
18142            $(SHELL) ./config.status $(subdir)/$@@
18144 atlocal: $(srcdir)/atlocal.in $(top_builddir)/config.status
18145         cd $(top_builddir) && \
18146            $(SHELL) ./config.status $(subdir)/$@@
18147 @end example
18149 @noindent
18150 and manage to have @file{atconfig.in} and @code{$(EXTRA_DIST)}
18151 distributed.
18153 With all this in place, and if you have not initialized @samp{TESTSUITEFLAGS}
18154 within your makefile, you can fine-tune test suite execution with this variable,
18155 for example:
18157 @example
18158 make check TESTSUITEFLAGS='-v -d -x 75 -k AC_PROG_CC CFLAGS=-g'
18159 @end example
18163 @c =============================== Frequent Autoconf Questions, with answers
18165 @node FAQ
18166 @chapter Frequent Autoconf Questions, with answers
18168 Several questions about Autoconf come up occasionally.  Here some of them
18169 are addressed.
18171 @menu
18172 * Distributing::                Distributing @command{configure} scripts
18173 * Why GNU M4::                  Why not use the standard M4?
18174 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
18175 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
18176 * Defining Directories::        Passing @code{datadir} to program
18177 * autom4te.cache::              What is it?  Can I remove it?
18178 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
18179 @end menu
18181 @node Distributing
18182 @section Distributing @command{configure} Scripts
18183 @cindex License
18185 @display
18186 What are the restrictions on distributing @command{configure}
18187 scripts that Autoconf generates?  How does that affect my
18188 programs that use them?
18189 @end display
18191 There are no restrictions on how the configuration scripts that Autoconf
18192 produces may be distributed or used.  In Autoconf version 1, they were
18193 covered by the @acronym{GNU} General Public License.  We still encourage
18194 software authors to distribute their work under terms like those of the
18195 @acronym{GPL}, but doing so is not required to use Autoconf.
18197 Of the other files that might be used with @command{configure},
18198 @file{config.h.in} is under whatever copyright you use for your
18199 @file{configure.ac}.  @file{config.sub} and @file{config.guess} have an
18200 exception to the @acronym{GPL} when they are used with an Autoconf-generated
18201 @command{configure} script, which permits you to distribute them under the
18202 same terms as the rest of your package.  @file{install-sh} is from the X
18203 Consortium and is not copyrighted.
18205 @node Why GNU M4
18206 @section Why Require @acronym{GNU} M4?
18208 @display
18209 Why does Autoconf require @acronym{GNU} M4?
18210 @end display
18212 Many M4 implementations have hard-coded limitations on the size and
18213 number of macros that Autoconf exceeds.  They also lack several
18214 builtin macros that it would be difficult to get along without in a
18215 sophisticated application like Autoconf, including:
18217 @example
18218 m4_builtin
18219 m4_indir
18220 m4_bpatsubst
18221 __file__
18222 __line__
18223 @end example
18225 Autoconf requires version 1.4.4 or later of @acronym{GNU} M4.
18227 Since only software maintainers need to use Autoconf, and since @acronym{GNU}
18228 M4 is simple to configure and install, it seems reasonable to require
18229 @acronym{GNU} M4 to be installed also.  Many maintainers of @acronym{GNU} and
18230 other free software already have most of the @acronym{GNU} utilities
18231 installed, since they prefer them.
18233 @node Bootstrapping
18234 @section How Can I Bootstrap?
18235 @cindex Bootstrap
18237 @display
18238 If Autoconf requires @acronym{GNU} M4 and @acronym{GNU} M4 has an Autoconf
18239 @command{configure} script, how do I bootstrap?  It seems like a chicken
18240 and egg problem!
18241 @end display
18243 This is a misunderstanding.  Although @acronym{GNU} M4 does come with a
18244 @command{configure} script produced by Autoconf, Autoconf is not required
18245 in order to run the script and install @acronym{GNU} M4.  Autoconf is only
18246 required if you want to change the M4 @command{configure} script, which few
18247 people have to do (mainly its maintainer).
18249 @node Why Not Imake
18250 @section Why Not Imake?
18251 @cindex Imake
18253 @display
18254 Why not use Imake instead of @command{configure} scripts?
18255 @end display
18257 Several people have written addressing this question, so I include
18258 adaptations of their explanations here.
18260 The following answer is based on one written by Richard Pixley:
18262 @quotation
18263 Autoconf generated scripts frequently work on machines that it has
18264 never been set up to handle before.  That is, it does a good job of
18265 inferring a configuration for a new system.  Imake cannot do this.
18267 Imake uses a common database of host specific data.  For X11, this makes
18268 sense because the distribution is made as a collection of tools, by one
18269 central authority who has control over the database.
18271 @acronym{GNU} tools are not released this way.  Each @acronym{GNU} tool has a
18272 maintainer; these maintainers are scattered across the world.  Using a
18273 common database would be a maintenance nightmare.  Autoconf may appear
18274 to be this kind of database, but in fact it is not.  Instead of listing
18275 host dependencies, it lists program requirements.
18277 If you view the @acronym{GNU} suite as a collection of native tools, then the
18278 problems are similar.  But the @acronym{GNU} development tools can be
18279 configured as cross tools in almost any host+target permutation.  All of
18280 these configurations can be installed concurrently.  They can even be
18281 configured to share host independent files across hosts.  Imake doesn't
18282 address these issues.
18284 Imake templates are a form of standardization.  The @acronym{GNU} coding
18285 standards address the same issues without necessarily imposing the same
18286 restrictions.
18287 @end quotation
18290 Here is some further explanation, written by Per Bothner:
18292 @quotation
18293 One of the advantages of Imake is that it easy to generate large
18294 makefiles using the @samp{#include} and macro mechanisms of @command{cpp}.
18295 However, @code{cpp} is not programmable: it has limited conditional
18296 facilities, and no looping.  And @code{cpp} cannot inspect its
18297 environment.
18299 All of these problems are solved by using @code{sh} instead of
18300 @code{cpp}.  The shell is fully programmable, has macro substitution,
18301 can execute (or source) other shell scripts, and can inspect its
18302 environment.
18303 @end quotation
18306 Paul Eggert elaborates more:
18308 @quotation
18309 With Autoconf, installers need not assume that Imake itself is already
18310 installed and working well.  This may not seem like much of an advantage
18311 to people who are accustomed to Imake.  But on many hosts Imake is not
18312 installed or the default installation is not working well, and requiring
18313 Imake to install a package hinders the acceptance of that package on
18314 those hosts.  For example, the Imake template and configuration files
18315 might not be installed properly on a host, or the Imake build procedure
18316 might wrongly assume that all source files are in one big directory
18317 tree, or the Imake configuration might assume one compiler whereas the
18318 package or the installer needs to use another, or there might be a
18319 version mismatch between the Imake expected by the package and the Imake
18320 supported by the host.  These problems are much rarer with Autoconf,
18321 where each package comes with its own independent configuration
18322 processor.
18324 Also, Imake often suffers from unexpected interactions between
18325 @command{make} and the installer's C preprocessor.  The fundamental problem
18326 here is that the C preprocessor was designed to preprocess C programs,
18327 not makefiles.  This is much less of a problem with Autoconf,
18328 which uses the general-purpose preprocessor M4, and where the
18329 package's author (rather than the installer) does the preprocessing in a
18330 standard way.
18331 @end quotation
18334 Finally, Mark Eichin notes:
18336 @quotation
18337 Imake isn't all that extensible, either.  In order to add new features to
18338 Imake, you need to provide your own project template, and duplicate most
18339 of the features of the existing one.  This means that for a sophisticated
18340 project, using the vendor-provided Imake templates fails to provide any
18341 leverage---since they don't cover anything that your own project needs
18342 (unless it is an X11 program).
18344 On the other side, though:
18346 The one advantage that Imake has over @command{configure}:
18347 @file{Imakefile} files tend to be much shorter (likewise, less redundant)
18348 than @file{Makefile.in} files.  There is a fix to this, however---at least
18349 for the Kerberos V5 tree, we've modified things to call in common
18350 @file{post.in} and @file{pre.in} makefile fragments for the
18351 entire tree.  This means that a lot of common things don't have to be
18352 duplicated, even though they normally are in @command{configure} setups.
18353 @end quotation
18356 @node Defining Directories
18357 @section How Do I @code{#define} Installation Directories?
18359 @display
18360 My program needs library files, installed in @code{datadir} and
18361 similar.  If I use
18363 @example
18364 AC_DEFINE_UNQUOTED([DATADIR], [$datadir],
18365   [Define to the read-only architecture-independent
18366    data directory.])
18367 @end example
18369 @noindent
18370 I get
18372 @example
18373 #define DATADIR "$@{prefix@}/share"
18374 @end example
18375 @end display
18377 As already explained, this behavior is on purpose, mandated by the
18378 @acronym{GNU} Coding Standards, see @ref{Installation Directory
18379 Variables}.  There are several means to achieve a similar goal:
18381 @itemize @minus
18382 @item
18383 Do not use @code{AC_DEFINE} but use your makefile to pass the
18384 actual value of @code{datadir} via compilation flags.
18385 @xref{Installation Directory Variables}, for the details.
18387 @item
18388 This solution can be simplified when compiling a program: you may either
18389 extend the @code{CPPFLAGS}:
18391 @example
18392 CPPFLAGS = -DDATADIR='"$(datadir)"' @@CPPFLAGS@@
18393 @end example
18395 @noindent
18396 or create a dedicated header file:
18398 @example
18399 DISTCLEANFILES = datadir.h
18400 datadir.h: Makefile
18401         echo '#define DATADIR "$(datadir)"' >$@@
18402 @end example
18404 @item
18405 Use @code{AC_DEFINE} but have @command{configure} compute the literal
18406 value of @code{datadir} and others.  Many people have wrapped macros to
18407 automate this task.  For instance, the macro @code{AC_DEFINE_DIR} from
18408 the @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
18409 Archive}.
18411 This solution does not conform to the @acronym{GNU} Coding Standards.
18413 @item
18414 Note that all the previous solutions hard wire the absolute name of
18415 these directories in the executables, which is not a good property.  You
18416 may try to compute the names relative to @code{prefix}, and try to
18417 find @code{prefix} at runtime, this way your package is relocatable.
18418 Some macros are already available to address this issue: see
18419 @code{adl_COMPUTE_RELATIVE_PATHS} and
18420 @code{adl_COMPUTE_STANDARD_RELATIVE_PATHS} on the
18421 @uref{http://autoconf-archive.cryp.to/,
18422 Autoconf Macro Archive}.
18423 @end itemize
18426 @node autom4te.cache
18427 @section What is @file{autom4te.cache}?
18429 @display
18430 What is this directory @file{autom4te.cache}?  Can I safely remove it?
18431 @end display
18433 In the @acronym{GNU} Build System, @file{configure.ac} plays a central
18434 role and is read by many tools: @command{autoconf} to create
18435 @file{configure}, @command{autoheader} to create @file{config.h.in},
18436 @command{automake} to create @file{Makefile.in}, @command{autoscan} to
18437 check the completeness of @file{configure.ac}, @command{autoreconf} to
18438 check the @acronym{GNU} Build System components that are used.  To
18439 ``read @file{configure.ac}'' actually means to compile it with M4,
18440 which can be a long process for complex @file{configure.ac}.
18442 This is why all these tools, instead of running directly M4, invoke
18443 @command{autom4te} (@pxref{autom4te Invocation}) which, while answering to
18444 a specific demand, stores additional information in
18445 @file{autom4te.cache} for future runs.  For instance, if you run
18446 @command{autoconf}, behind the scenes, @command{autom4te} also
18447 stores information for the other tools, so that when you invoke
18448 @command{autoheader} or @command{automake} etc., reprocessing
18449 @file{configure.ac} is not needed.  The speed up is frequently of 30%,
18450 and is increasing with the size of @file{configure.ac}.
18452 But it is and remains being simply a cache: you can safely remove it.
18454 @sp 1
18456 @display
18457 Can I permanently get rid of it?
18458 @end display
18460 The creation of this cache can be disabled from
18461 @file{~/.autom4te.cfg}, see @ref{Customizing autom4te}, for more
18462 details.  You should be aware that disabling the cache slows down the
18463 Autoconf test suite by 40%.  The more @acronym{GNU} Build System
18464 components are used, the more the cache is useful; for instance
18465 running @samp{autoreconf -f} on the Core Utilities is twice slower without
18466 the cache @emph{although @option{--force} implies that the cache is
18467 not fully exploited}, and eight times slower than without
18468 @option{--force}.
18471 @node Present But Cannot Be Compiled
18472 @section Header Present But Cannot Be Compiled
18474 The most important guideline to bear in mind when checking for
18475 features is to mimic as much as possible the intended use.
18476 Unfortunately, old versions of @code{AC_CHECK_HEADER} and
18477 @code{AC_CHECK_HEADERS} failed to follow this idea, and called
18478 the preprocessor, instead of the compiler, to check for headers.  As a
18479 result, incompatibilities between headers went unnoticed during
18480 configuration, and maintainers finally had to deal with this issue
18481 elsewhere.
18483 As of Autoconf 2.56 both checks are performed, and @code{configure}
18484 complains loudly if the compiler and the preprocessor do not agree.
18485 For the time being the result used is that of the preprocessor, to give
18486 maintainers time to adjust their @file{configure.ac}, but in the
18487 future, only the compiler will be considered.
18489 Consider the following example:
18491 @smallexample
18492 $ @kbd{cat number.h}
18493 typedef int number;
18494 $ @kbd{cat pi.h}
18495 const number pi = 3;
18496 $ @kbd{cat configure.ac}
18497 AC_INIT([Example], [1.0], [bug-example@@example.org])
18498 AC_CHECK_HEADERS([pi.h])
18499 $ @kbd{autoconf -Wall}
18500 $ @kbd{./configure}
18501 checking for gcc... gcc
18502 checking for C compiler default output file name... a.out
18503 checking whether the C compiler works... yes
18504 checking whether we are cross compiling... no
18505 checking for suffix of executables...
18506 checking for suffix of object files... o
18507 checking whether we are using the GNU C compiler... yes
18508 checking whether gcc accepts -g... yes
18509 checking for gcc option to accept ISO C89... none needed
18510 checking how to run the C preprocessor... gcc -E
18511 checking for grep that handles long lines and -e... grep
18512 checking for egrep... grep -E
18513 checking for ANSI C header files... yes
18514 checking for sys/types.h... yes
18515 checking for sys/stat.h... yes
18516 checking for stdlib.h... yes
18517 checking for string.h... yes
18518 checking for memory.h... yes
18519 checking for strings.h... yes
18520 checking for inttypes.h... yes
18521 checking for stdint.h... yes
18522 checking for unistd.h... yes
18523 checking pi.h usability... no
18524 checking pi.h presence... yes
18525 configure: WARNING: pi.h: present but cannot be compiled
18526 configure: WARNING: pi.h:     check for missing prerequisite headers?
18527 configure: WARNING: pi.h: see the Autoconf documentation
18528 configure: WARNING: pi.h:     section "Present But Cannot Be Compiled"
18529 configure: WARNING: pi.h: proceeding with the preprocessor's result
18530 configure: WARNING: pi.h: in the future, the compiler will take precedence
18531 configure: WARNING:     ## -------------------------------------- ##
18532 configure: WARNING:     ## Report this to bug-example@@example.org ##
18533 configure: WARNING:     ## -------------------------------------- ##
18534 checking for pi.h... yes
18535 @end smallexample
18537 @noindent
18538 The proper way the handle this case is using the fourth argument
18539 (@pxref{Generic Headers}):
18541 @example
18542 $ @kbd{cat configure.ac}
18543 AC_INIT([Example], [1.0], [bug-example@@example.org])
18544 AC_CHECK_HEADERS([number.h pi.h], [], [],
18545 [[#if HAVE_NUMBER_H
18546 # include <number.h>
18547 #endif
18549 $ @kbd{autoconf -Wall}
18550 $ @kbd{./configure}
18551 checking for gcc... gcc
18552 checking for C compiler default output... a.out
18553 checking whether the C compiler works... yes
18554 checking whether we are cross compiling... no
18555 checking for suffix of executables...
18556 checking for suffix of object files... o
18557 checking whether we are using the GNU C compiler... yes
18558 checking whether gcc accepts -g... yes
18559 checking for gcc option to accept ANSI C... none needed
18560 checking for number.h... yes
18561 checking for pi.h... yes
18562 @end example
18564 See @ref{Particular Headers}, for a list of headers with their
18565 prerequisite.
18567 @c ===================================================== History of Autoconf.
18569 @node History
18570 @chapter History of Autoconf
18571 @cindex History of autoconf
18573 You may be wondering, Why was Autoconf originally written?  How did it
18574 get into its present form?  (Why does it look like gorilla spit?)  If
18575 you're not wondering, then this chapter contains no information useful
18576 to you, and you might as well skip it.  If you @emph{are} wondering,
18577 then let there be light@enddots{}
18579 @menu
18580 * Genesis::                     Prehistory and naming of @command{configure}
18581 * Exodus::                      The plagues of M4 and Perl
18582 * Leviticus::                   The priestly code of portability arrives
18583 * Numbers::                     Growth and contributors
18584 * Deuteronomy::                 Approaching the promises of easy configuration
18585 @end menu
18587 @node Genesis
18588 @section Genesis
18590 In June 1991 I was maintaining many of the @acronym{GNU} utilities for the
18591 Free Software Foundation.  As they were ported to more platforms and
18592 more programs were added, the number of @option{-D} options that users
18593 had to select in the makefile (around 20) became burdensome.
18594 Especially for me---I had to test each new release on a bunch of
18595 different systems.  So I wrote a little shell script to guess some of
18596 the correct settings for the fileutils package, and released it as part
18597 of fileutils 2.0.  That @command{configure} script worked well enough that
18598 the next month I adapted it (by hand) to create similar @command{configure}
18599 scripts for several other @acronym{GNU} utilities packages.  Brian Berliner
18600 also adapted one of my scripts for his @acronym{CVS} revision control system.
18602 Later that summer, I learned that Richard Stallman and Richard Pixley
18603 were developing similar scripts to use in the @acronym{GNU} compiler tools;
18604 so I adapted my @command{configure} scripts to support their evolving
18605 interface: using the file name @file{Makefile.in} as the templates;
18606 adding @samp{+srcdir}, the first option (of many); and creating
18607 @file{config.status} files.
18609 @node Exodus
18610 @section Exodus
18612 As I got feedback from users, I incorporated many improvements, using
18613 Emacs to search and replace, cut and paste, similar changes in each of
18614 the scripts.  As I adapted more @acronym{GNU} utilities packages to use
18615 @command{configure} scripts, updating them all by hand became impractical.
18616 Rich Murphey, the maintainer of the @acronym{GNU} graphics utilities, sent me
18617 mail saying that the @command{configure} scripts were great, and asking if
18618 I had a tool for generating them that I could send him.  No, I thought,
18619 but I should!  So I started to work out how to generate them.  And the
18620 journey from the slavery of hand-written @command{configure} scripts to the
18621 abundance and ease of Autoconf began.
18623 Cygnus @command{configure}, which was being developed at around that time,
18624 is table driven; it is meant to deal mainly with a discrete number of
18625 system types with a small number of mainly unguessable features (such as
18626 details of the object file format).  The automatic configuration system
18627 that Brian Fox had developed for Bash takes a similar approach.  For
18628 general use, it seems to me a hopeless cause to try to maintain an
18629 up-to-date database of which features each variant of each operating
18630 system has.  It's easier and more reliable to check for most features on
18631 the fly---especially on hybrid systems that people have hacked on
18632 locally or that have patches from vendors installed.
18634 I considered using an architecture similar to that of Cygnus
18635 @command{configure}, where there is a single @command{configure} script that
18636 reads pieces of @file{configure.in} when run.  But I didn't want to have
18637 to distribute all of the feature tests with every package, so I settled
18638 on having a different @command{configure} made from each
18639 @file{configure.in} by a preprocessor.  That approach also offered more
18640 control and flexibility.
18642 I looked briefly into using the Metaconfig package, by Larry Wall,
18643 Harlan Stenn, and Raphael Manfredi, but I decided not to for several
18644 reasons.  The @command{Configure} scripts it produces are interactive,
18645 which I find quite inconvenient; I didn't like the ways it checked for
18646 some features (such as library functions); I didn't know that it was
18647 still being maintained, and the @command{Configure} scripts I had
18648 seen didn't work on many modern systems (such as System V R4 and NeXT);
18649 it wasn't flexible in what it could do in response to a feature's
18650 presence or absence; I found it confusing to learn; and it was too big
18651 and complex for my needs (I didn't realize then how much Autoconf would
18652 eventually have to grow).
18654 I considered using Perl to generate my style of @command{configure}
18655 scripts, but decided that M4 was better suited to the job of simple
18656 textual substitutions: it gets in the way less, because output is
18657 implicit.  Plus, everyone already has it.  (Initially I didn't rely on
18658 the @acronym{GNU} extensions to M4.)  Also, some of my friends at the
18659 University of Maryland had recently been putting M4 front ends on
18660 several programs, including @code{tvtwm}, and I was interested in trying
18661 out a new language.
18663 @node Leviticus
18664 @section Leviticus
18666 Since my @command{configure} scripts determine the system's capabilities
18667 automatically, with no interactive user intervention, I decided to call
18668 the program that generates them Autoconfig.  But with a version number
18669 tacked on, that name would be too long for old Unix file systems,
18670 so I shortened it to Autoconf.
18672 In the fall of 1991 I called together a group of fellow questers after
18673 the Holy Grail of portability (er, that is, alpha testers) to give me
18674 feedback as I encapsulated pieces of my handwritten scripts in M4 macros
18675 and continued to add features and improve the techniques used in the
18676 checks.  Prominent among the testers were Fran@,{c}ois Pinard, who came up
18677 with the idea of making an Autoconf shell script to run M4
18678 and check for unresolved macro calls; Richard Pixley, who suggested
18679 running the compiler instead of searching the file system to find
18680 include files and symbols, for more accurate results; Karl Berry, who
18681 got Autoconf to configure @TeX{} and added the macro index to the
18682 documentation; and Ian Lance Taylor, who added support for creating a C
18683 header file as an alternative to putting @option{-D} options in a
18684 makefile, so he could use Autoconf for his @acronym{UUCP} package.
18685 The alpha testers cheerfully adjusted their files again and again as the
18686 names and calling conventions of the Autoconf macros changed from
18687 release to release.  They all contributed many specific checks, great
18688 ideas, and bug fixes.
18690 @node Numbers
18691 @section Numbers
18693 In July 1992, after months of alpha testing, I released Autoconf 1.0,
18694 and converted many @acronym{GNU} packages to use it.  I was surprised by how
18695 positive the reaction to it was.  More people started using it than I
18696 could keep track of, including people working on software that wasn't
18697 part of the @acronym{GNU} Project (such as TCL, FSP, and Kerberos V5).
18698 Autoconf continued to improve rapidly, as many people using the
18699 @command{configure} scripts reported problems they encountered.
18701 Autoconf turned out to be a good torture test for M4 implementations.
18702 Unix M4 started to dump core because of the length of the
18703 macros that Autoconf defined, and several bugs showed up in @acronym{GNU}
18704 M4 as well.  Eventually, we realized that we needed to use some
18705 features that only @acronym{GNU} M4 has.  4.3@acronym{BSD} M4, in
18706 particular, has an impoverished set of builtin macros; the System V
18707 version is better, but still doesn't provide everything we need.
18709 More development occurred as people put Autoconf under more stresses
18710 (and to uses I hadn't anticipated).  Karl Berry added checks for X11.
18711 david zuhn contributed C++ support.  Fran@,{c}ois Pinard made it diagnose
18712 invalid arguments.  Jim Blandy bravely coerced it into configuring
18713 @acronym{GNU} Emacs, laying the groundwork for several later improvements.
18714 Roland McGrath got it to configure the @acronym{GNU} C Library, wrote the
18715 @command{autoheader} script to automate the creation of C header file
18716 templates, and added a @option{--verbose} option to @command{configure}.
18717 Noah Friedman added the @option{--autoconf-dir} option and
18718 @code{AC_MACRODIR} environment variable.  (He also coined the term
18719 @dfn{autoconfiscate} to mean ``adapt a software package to use
18720 Autoconf''.)  Roland and Noah improved the quoting protection in
18721 @code{AC_DEFINE} and fixed many bugs, especially when I got sick of
18722 dealing with portability problems from February through June, 1993.
18724 @node Deuteronomy
18725 @section Deuteronomy
18727 A long wish list for major features had accumulated, and the effect of
18728 several years of patching by various people had left some residual
18729 cruft.  In April 1994, while working for Cygnus Support, I began a major
18730 revision of Autoconf.  I added most of the features of the Cygnus
18731 @command{configure} that Autoconf had lacked, largely by adapting the
18732 relevant parts of Cygnus @command{configure} with the help of david zuhn
18733 and Ken Raeburn.  These features include support for using
18734 @file{config.sub}, @file{config.guess}, @option{--host}, and
18735 @option{--target}; making links to files; and running @command{configure}
18736 scripts in subdirectories.  Adding these features enabled Ken to convert
18737 @acronym{GNU} @code{as}, and Rob Savoye to convert Deja@acronym{GNU}, to using
18738 Autoconf.
18740 I added more features in response to other peoples' requests.  Many
18741 people had asked for @command{configure} scripts to share the results of
18742 the checks between runs, because (particularly when configuring a large
18743 source tree, like Cygnus does) they were frustratingly slow.  Mike
18744 Haertel suggested adding site-specific initialization scripts.  People
18745 distributing software that had to unpack on MS-DOS asked for a way to
18746 override the @file{.in} extension on the file names, which produced file
18747 names like @file{config.h.in} containing two dots.  Jim Avera did an
18748 extensive examination of the problems with quoting in @code{AC_DEFINE}
18749 and @code{AC_SUBST}; his insights led to significant improvements.
18750 Richard Stallman asked that compiler output be sent to @file{config.log}
18751 instead of @file{/dev/null}, to help people debug the Emacs
18752 @command{configure} script.
18754 I made some other changes because of my dissatisfaction with the quality
18755 of the program.  I made the messages showing results of the checks less
18756 ambiguous, always printing a result.  I regularized the names of the
18757 macros and cleaned up coding style inconsistencies.  I added some
18758 auxiliary utilities that I had developed to help convert source code
18759 packages to use Autoconf.  With the help of Fran@,{c}ois Pinard, I made
18760 the macros not interrupt each others' messages.  (That feature revealed
18761 some performance bottlenecks in @acronym{GNU} M4, which he hastily
18762 corrected!)  I reorganized the documentation around problems people want
18763 to solve.  And I began a test suite, because experience had shown that
18764 Autoconf has a pronounced tendency to regress when we change it.
18766 Again, several alpha testers gave invaluable feedback, especially
18767 Fran@,{c}ois Pinard, Jim Meyering, Karl Berry, Rob Savoye, Ken Raeburn,
18768 and Mark Eichin.
18770 Finally, version 2.0 was ready.  And there was much rejoicing.  (And I
18771 have free time again.  I think.  Yeah, right.)
18774 @c ========================================================== Appendices
18776 @node Copying This Manual
18777 @appendix Copying This Manual
18778 @cindex License
18780 @menu
18781 * GNU Free Documentation License::  License for copying this manual
18782 @end menu
18784 @include fdl.texi
18786 @node Indices
18787 @appendix Indices
18789 @menu
18790 * Environment Variable Index::  Index of environment variables used
18791 * Output Variable Index::       Index of variables set in output files
18792 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
18793 * Autoconf Macro Index::        Index of Autoconf macros
18794 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
18795 * Autotest Macro Index::        Index of Autotest macros
18796 * Program & Function Index::    Index of those with portability problems
18797 * Concept Index::               General index
18798 @end menu
18800 @node Environment Variable Index
18801 @appendixsec Environment Variable Index
18803 This is an alphabetical list of the environment variables that Autoconf
18804 checks.
18806 @printindex ev
18808 @node Output Variable Index
18809 @appendixsec Output Variable Index
18811 This is an alphabetical list of the variables that Autoconf can
18812 substitute into files that it creates, typically one or more
18813 makefiles.  @xref{Setting Output Variables}, for more information
18814 on how this is done.
18816 @printindex ov
18818 @node Preprocessor Symbol Index
18819 @appendixsec Preprocessor Symbol Index
18821 This is an alphabetical list of the C preprocessor symbols that the
18822 Autoconf macros define.  To work with Autoconf, C source code needs to
18823 use these names in @code{#if} directives.
18825 @printindex cv
18827 @node Autoconf Macro Index
18828 @appendixsec Autoconf Macro Index
18830 This is an alphabetical list of the Autoconf macros.
18831 @ifset shortindexflag
18832 To make the list easier to use, the macros are listed without their
18833 preceding @samp{AC_}.
18834 @end ifset
18836 @printindex AC
18838 @node M4 Macro Index
18839 @appendixsec M4 Macro Index
18841 This is an alphabetical list of the M4, M4sugar, and M4sh macros.
18842 @ifset shortindexflag
18843 To make the list easier to use, the macros are listed without their
18844 preceding @samp{m4_} or @samp{AS_}.
18845 @end ifset
18847 @printindex MS
18849 @node Autotest Macro Index
18850 @appendixsec Autotest Macro Index
18852 This is an alphabetical list of the Autotest macros.
18853 @ifset shortindexflag
18854 To make the list easier to use, the macros are listed without their
18855 preceding @samp{AT_}.
18856 @end ifset
18858 @printindex AT
18860 @node Program & Function Index
18861 @appendixsec Program and Function Index
18863 This is an alphabetical list of the programs and functions which
18864 portability is discussed in this document.
18866 @printindex pr
18868 @node Concept Index
18869 @appendixsec Concept Index
18871 This is an alphabetical list of the files, tools, and concepts
18872 introduced in this document.
18874 @printindex cp
18876 @bye
18878 @c  LocalWords:  texinfo setfilename autoconf texi settitle setchapternewpage
18879 @c  LocalWords:  setcontentsaftertitlepage finalout ARG ovar varname dvar acx
18880 @c  LocalWords:  makeinfo dvi defcodeindex ev ov CPP cv Autotest mv defindex fn
18881 @c  LocalWords:  shortindexflag iftex ifset acindex ACindex ifclear ahindex fu
18882 @c  LocalWords:  asindex MSindex atindex ATindex auindex hdrindex prindex FIXME
18883 @c  LocalWords:  msindex alloca fnindex Aaarg indices FSF's dircategory ifnames
18884 @c  LocalWords:  direntry autoscan autoreconf autoheader autoupdate config FDs
18885 @c  LocalWords:  testsuite titlepage Elliston Demaille vskip filll ifnottex hmm
18886 @c  LocalWords:  insertcopying Autoconf's detailmenu Automake Libtool Posix ois
18887 @c  LocalWords:  Systemology Checkpointing Changequote INTERCAL changequote dfn
18888 @c  LocalWords:  Quadrigraphs builtins Shellology acconfig Bugward LIBOBJ Imake
18889 @c  LocalWords:  LIBOBJS IFELSE cindex flushright Pinard Metaconfig uref Simons
18890 @c  LocalWords:  distclean uninstall noindent versioning Tromey dir
18891 @c  LocalWords:  SAMS samp aclocal acsite underquoted emph itemx prepend SUBST
18892 @c  LocalWords:  evindex automake Gettext autopoint gettext symlink libtoolize
18893 @c  LocalWords:  defmac INIT tarname ovindex cvindex BUGREPORT PREREQ asis PROG
18894 @c  LocalWords:  SRCDIR srcdir globbing afterwards cmds foos fooo foooo init cd
18895 @c  LocalWords:  builddir timestamp src Imakefile chmod defvar CFLAGS CPPFLAGS
18896 @c  LocalWords:  CXXFLAGS DEFS DHAVE defvarx FCFLAGS FFLAGS LDFLAGS bindir GCC
18897 @c  LocalWords:  datadir datarootdir docdir dvidir htmldir libdir ifnothtml kbd
18898 @c  LocalWords:  includedir infodir libexecdir localedir localstatedir mandir
18899 @c  LocalWords:  oldincludedir pdfdir PDF psdir PostScript sbindir sysconfdir
18900 @c  LocalWords:  sharedstatedir DDATADIR sed tmp pkgdatadir VPATH conf unistd
18901 @c  LocalWords:  undef endif builtin FUNCS ifndef STACKSEG getb GETB YMP fubar
18902 @c  LocalWords:  PRE dest SUBDIRS subdirs fi struct STDC stdlib stddef INTTYPES
18903 @c  LocalWords:  inttypes STDINT stdint AWK AIX Solaris NeXT env EGREP FGREP yy
18904 @c  LocalWords:  LEXLIB YYTEXT lfl nonportable Automake's LN RANLIB byacc INETD
18905 @c  LocalWords:  inetd prog PROGS progs ranlib lmp lXt lX nsl gethostbyname UX
18906 @c  LocalWords:  NextStep isinf isnan glibc IRIX sunmath lm lsunmath pre sizeof
18907 @c  LocalWords:  ld inline malloc putenv setenv FreeBSD realloc SunOS MinGW
18908 @c  LocalWords:  snprintf vsnprintf sprintf vsprintf sscanf gcc strerror ifdef
18909 @c  LocalWords:  strnlen sysconf PAGESIZE unsetenv va fallback memcpy dst FUNC
18910 @c  LocalWords:  PowerPC GNUC libPW pragma Olibcalls CHOWN chown CLOSEDIR VFORK
18911 @c  LocalWords:  closedir FNMATCH fnmatch vfork FSEEKO LARGEFILE fseeko SVR sc
18912 @c  LocalWords:  largefile GETGROUPS getgroups GETLOADAVG DGUX UMAX NLIST KMEM
18913 @c  LocalWords:  GETLODAVG SETGID getloadavg nlist GETMNTENT irix
18914 @c  LocalWords:  getmntent UnixWare GETPGRP getpgid getpgrp Posix's pid LSTAT
18915 @c  LocalWords:  lstat rpl MEMCMP memcmp OpenStep MBRTOWC mbrtowc MKTIME mktime
18916 @c  LocalWords:  localtime MMAP mmap OBSTACK obstack obstacks ARGTYPES timeval
18917 @c  LocalWords:  SETPGRP setpgrp defmacx Hurd SETVBUF setvbuf STRCOLL strcoll
18918 @c  LocalWords:  STRTOD strtod DECL STRFTIME strftime SCO UTIME utime VPRINTF
18919 @c  LocalWords:  DOPRNT vprintf doprnt sp unfixable LIBSOURCE LIBSOURCES Eggert
18920 @c  LocalWords:  linux netinet ia Tru XFree DIRENT NDIR dirent ndir multitable
18921 @c  LocalWords:  NAMLEN strlen namlen MKDEV SYSMACROS makedev RESOLV resolv DNS
18922 @c  LocalWords:  inet structs NAMESER arpa NETDB netdb UTekV UTS autom GCC's kB
18923 @c  LocalWords:  STDBOOL BOOL stdbool conformant cplusplus bool Bool stdarg tm
18924 @c  LocalWords:  ctype strchr strrchr rindex bcopy memmove memchr WEXITSTATUS
18925 @c  LocalWords:  WIFEXITED TIOCGWINSZ GWINSZ termios preprocess preprocessable
18926 @c  LocalWords:  DECLS strdup calloc BLKSIZE blksize RDEV rdev TZNAME tzname pw
18927 @c  LocalWords:  passwd gecos pwd MBSTATE mbstate wchar RETSIGTYPE hup UID uid
18928 @c  LocalWords:  gid ptrdiff uintmax EXEEXT OBJEXT Ae Onolimit conftest AXP str
18929 @c  LocalWords:  ALIGNOF WERROR Werror cpp HP's WorkShop egcs un fied stdc CXX
18930 @c  LocalWords:  varargs BIGENDIAN Endianness SPARC endianness grep'ed CONST FC
18931 @c  LocalWords:  const STRINGIZE stringizing PARAMS unprotoize protos KCC cxx
18932 @c  LocalWords:  xlC aCC CXXCPP FREEFORM xlf FLIBS FCLIBS ish SRCEXT XTRA LFS
18933 @c  LocalWords:  ISC lcposix MINIX Minix conditionalized inlines hw dD confdefs
18934 @c  LocalWords:  fputs stdout PREPROC ar UFS HFS QNX realtime fstype STATVFS se
18935 @c  LocalWords:  statvfs STATFS statfs func machfile hdr lelf raboof DEFUN GTK
18936 @c  LocalWords:  GTKMM Grmph ified ine defn baz EOF qar Ahhh changecom algol io
18937 @c  LocalWords:  changeword quadrigraphs quadrigraph dnl SGI atoi overquoting
18938 @c  LocalWords:  Aas Wcross sep args namespace undefine bpatsubst popdef dquote
18939 @c  LocalWords:  bregexp Overquote overquotation meisch maisch meische maische
18940 @c  LocalWords:  miscian DIRNAME dirname MKDIR CATFILE XMKMF TRAVOLTA celsius
18941 @c  LocalWords:  EMX emxos Emacsen Korn DYNIX subshell posix Ksh ksh Pdksh Zsh
18942 @c  LocalWords:  pdksh zsh Allbery Lipe Kubota UWS zorglub stderr eval esac lfn
18943 @c  LocalWords:  drivespec Posixy DJGPP doschk prettybird LPT pfew Zsh's yu yaa
18944 @c  LocalWords:  yM uM aM firebird IP subdir misparses ok Unpatched abc bc zA
18945 @c  LocalWords:  CDPATH DUALCASE LINENO prepass Subshells lineno NULLCMD cmp wc
18946 @c  LocalWords:  MAILPATH scanset arg NetBSD Almquist printf expr cp
18947 @c  LocalWords:  Oliva awk Aaaaarg cmd regex xfoo GNV OpenVMS VM
18948 @c  LocalWords:  sparc Proulx SysV nbar nfoo maxdepth acdilrtu TWG mc
18949 @c  LocalWords:  mkdir exe uname OpenBSD Fileutils mktemp umask TMPDIR guid os
18950 @c  LocalWords:  fooXXXXXX Unicos parenthesization utimes hpux hppa unescaped
18951 @c  LocalWords:  pmake DOS's gmake ifoo DESTDIR autoconfiscated pc coff mips gg
18952 @c  LocalWords:  dec ultrix cpu wildcards rpcc rdtsc powerpc behaviour readline
18953 @c  LocalWords:  withval vxworks gless localcache usr LOFF loff CYGWIN Cygwin
18954 @c  LocalWords:  cygwin SIGLIST siglist SYSNDIR SYSDIR ptx lseq rusage elif MSC
18955 @c  LocalWords:  lfoo POUNDBANG lsun NIS getpwnam SYSCALLS RSH INTL lintl aix
18956 @c  LocalWords:  intl lx ldir syslog bsd EPI toolchain netbsd objext de KNR nn
18957 @c  LocalWords:  fication LTLIBOBJS Wdiff TESTDIR atconfig atlocal akim XFAIL
18958 @c  LocalWords:  ChangeLog prepended errexit smallexample TESTSUITEFLAGS GPL er
18959 @c  LocalWords:  installcheck autotest indir Pixley Bothner Eichin Kerberos adl
18960 @c  LocalWords:  DISTCLEANFILES preprocessor's fileutils Stallman Murphey Stenn
18961 @c  LocalWords:  Manfredi Autoconfig TCL FSP david zuhn Blandy MACRODIR Raeburn
18962 @c  LocalWords:  autoconfiscate Savoye Haertel Avera Meyering fdl appendixsec
18963 @c  LocalWords:  printindex american LIBOBJDIR LibdirTest ERLCFLAGS OBJCFLAGS
18964 @c  LocalWords:  VER Gnulib online xyes strcpy TYPEOF typeof OBJC objcc objc ln
18965 @c  LocalWords:  GOBJC OBJCCPP OTP ERLC erl valloc decr dumpdef errprint incr
18966 @c  LocalWords:  esyscmd len maketemp pushdef substr syscmd sysval translit txt
18967 @c  LocalWords:  sinclude foreach myvar tolower toupper uniq BASENAME STDIN
18968 @c  LocalWords:  Dynix descrips basename aname cname macroexpands xno xcheck
18969 @c  LocalWords:  LIBREADLINE lreadline lncurses libreadline
18971 @c Local Variables:
18972 @c fill-column: 72
18973 @c ispell-local-dictionary: "american"
18974 @c End: