New config files output variable `top_build_prefix'.
[autoconf/tsuna.git] / doc / autoconf.texi
blob421056e0305c285c0be2d41af9a5f5684267de63
1 \input texinfo @c -*-texinfo-*-
2 @comment ========================================================
3 @comment %**start of header
4 @setfilename autoconf.info
5 @include version.texi
6 @settitle Autoconf
7 @setchapternewpage odd
8 @ifnothtml
9 @setcontentsaftertitlepage
10 @end ifnothtml
11 @finalout
13 @c @ovar(ARG, DEFAULT)
14 @c -------------------
15 @c The ARG is an optional argument.  To be used for macro arguments in
16 @c their documentation (@defmac).
17 @macro ovar{varname}
18 @r{[}@var{\varname\}@r{]}
19 @end macro
21 @c @dvar(ARG, DEFAULT)
22 @c -------------------
23 @c The ARG is an optional argument, defaulting to DEFAULT.  To be used
24 @c for macro arguments in their documentation (@defmac).
25 @macro dvar{varname, default}
26 @r{[}@var{\varname\} = @samp{\default\}@r{]}
27 @end macro
29 @c Handling the indexes with Texinfo yields several different problems.
31 @c Because we want to drop out the AC_ part of the macro names in the
32 @c printed manual, but not in the other outputs, we need a layer above
33 @c the usual @acindex{} etc.  That's why we first define indexes such as
34 @c acx meant to become the macro @acindex.  First of all, using ``ac_''
35 @c does not work with makeinfo, and using ``ac1'' doesn't work with TeX.
36 @c So use something more regular ``acx''.  Then you finish with a printed
37 @c index saying ``index is not existent''.  Of course: you ought to use
38 @c two letters :(  So you use capitals.
40 @c Second, when defining a macro in the TeX world, following spaces are
41 @c eaten.  But then, since we embed @acxindex commands that use the end
42 @c of line as an end marker, the whole things wrecks itself.  So make
43 @c sure you do *force* an additional end of line, add a ``@c''.
45 @c Finally, you might want to get rid of TeX expansion, using --expand
46 @c with texi2dvi.  But then you wake up an old problem: we use macros
47 @c in @defmac etc. where TeX does perform the expansion, but not makeinfo.
49 @c Define an environment variable index.
50 @defcodeindex ev
51 @c Define an output variable index.
52 @defcodeindex ov
53 @c Define a CPP variable index.
54 @defcodeindex cv
55 @c Define an Autoconf macro index that @defmac doesn't write to.
56 @defcodeindex AC
57 @c Define an Autotest macro index that @defmac doesn't write to.
58 @defcodeindex AT
59 @c Define an M4sugar macro index that @defmac doesn't write to.
60 @defcodeindex MS
61 @c Define an index for *foreign* programs: `mv' etc.  Used for the
62 @c portability sections and so on.
63 @defindex pr
65 @c shortindexflag
66 @c --------------
67 @c Shall we factor AC_ out of the Autoconf macro index etc.?
68 @iftex
69 @set shortindexflag
70 @end iftex
72 @c @acindex{MACRO}
73 @c ---------------
74 @c Registering an AC_\MACRO\.
75 @ifset shortindexflag
76 @macro acindex{macro}
77 @ACindex \macro\
79 @end macro
80 @end ifset
81 @ifclear shortindexflag
82 @macro acindex{macro}
83 @ACindex AC_\macro\
84 @end macro
85 @end ifclear
87 @c @ahindex{MACRO}
88 @c ---------------
89 @c Registering an AH_\MACRO\.
90 @macro ahindex{macro}
91 @ACindex AH_\macro\
93 @end macro
95 @c @asindex{MACRO}
96 @c ---------------
97 @c Registering an AS_\MACRO\.
98 @ifset shortindexflag
99 @macro asindex{macro}
100 @MSindex \macro\
102 @end macro
103 @end ifset
104 @ifclear shortindexflag
105 @macro asindex{macro}
106 @MSindex AS_\macro\
107 @end macro
108 @end ifclear
110 @c @atindex{MACRO}
111 @c ---------------
112 @c Registering an AT_\MACRO\.
113 @ifset shortindexflag
114 @macro atindex{macro}
115 @ATindex \macro\
117 @end macro
118 @end ifset
119 @ifclear shortindexflag
120 @macro atindex{macro}
121 @ATindex AT_\macro\
122 @end macro
123 @end ifclear
125 @c @auindex{MACRO}
126 @c ---------------
127 @c Registering an AU_\MACRO\.
128 @macro auindex{macro}
129 @ACindex AU_\macro\
131 @end macro
133 @c @hdrindex{MACRO}
134 @c ----------------
135 @c Indexing a header.
136 @macro hdrindex{macro}
137 @prindex @file{\macro\}
139 @end macro
141 @c @msindex{MACRO}
142 @c ---------------
143 @c Registering an m4_\MACRO\.
144 @ifset shortindexflag
145 @macro msindex{macro}
146 @MSindex \macro\
148 @end macro
149 @end ifset
150 @ifclear shortindexflag
151 @macro msindex{macro}
152 @MSindex m4_\macro\
153 @end macro
154 @end ifclear
157 @c Define an index for functions: `alloca' etc.  Used for the
158 @c portability sections and so on.  We can't use `fn' (aka `fnindex),
159 @c since `@defmac' goes into it => we'd get all the macros too.
161 @c   FIXME: Aaarg!  It seems there are too many indices for TeX :(
163 @c   ! No room for a new @write .
164 @c   l.112 @defcodeindex fu
166 @c   so don't define yet another one :(  Just put some tags before each
167 @c   @prindex which is actually a @funindex.
169 @c   @defcodeindex fu
172 @c   @c Put the programs and functions into their own index.
173 @c   @syncodeindex fu pr
175 @comment %**end of header
176 @comment ========================================================
178 @copying
180 This manual is for @acronym{GNU} Autoconf
181 (version @value{VERSION}, @value{UPDATED}),
182 a package for creating scripts to configure source code packages using
183 templates and an M4 macro package.
185 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
186 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
188 @quotation
189 Permission is granted to copy, distribute and/or modify this document
190 under the terms of the @acronym{GNU} Free Documentation License,
191 Version 1.2 or any later version published by the Free Software
192 Foundation; with no Invariant Sections, with the Front-Cover texts
193 being ``A @acronym{GNU} Manual,'' and with the Back-Cover Texts as in
194 (a) below.  A copy of the license is included in the section entitled
195 ``@acronym{GNU} Free Documentation License.''
197 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and
198 modify this @acronym{GNU} Manual, like @acronym{GNU} software.  Copies
199 published by the Free Software Foundation raise funds for
200 @acronym{GNU} development.''
201 @end quotation
202 @end copying
206 @dircategory Software development
207 @direntry
208 * Autoconf: (autoconf).         Create source code configuration scripts.
209 @end direntry
211 @dircategory Individual utilities
212 @direntry
213 * autoscan: (autoconf)autoscan Invocation.
214                                 Semi-automatic @file{configure.ac} writing
215 * ifnames: (autoconf)ifnames Invocation.        Listing conditionals in source.
216 * autoconf-invocation: (autoconf)autoconf Invocation.
217                                 How to create configuration scripts
218 * autoreconf: (autoconf)autoreconf Invocation.
219                                 Remaking multiple @command{configure} scripts
220 * autoheader: (autoconf)autoheader Invocation.
221                                 How to create configuration templates
222 * autom4te: (autoconf)autom4te Invocation.
223                                 The Autoconf executables backbone
224 * configure: (autoconf)configure Invocation.    Configuring a package.
225 * autoupdate: (autoconf)autoupdate Invocation.
226                                 Automatic update of @file{configure.ac}
227 * config.status: (autoconf)config.status Invocation. Recreating configurations.
228 * testsuite: (autoconf)testsuite Invocation.    Running an Autotest test suite.
229 @end direntry
231 @titlepage
232 @title Autoconf
233 @subtitle Creating Automatic Configuration Scripts
234 @subtitle for version @value{VERSION}, @value{UPDATED}
235 @author David MacKenzie
236 @author Ben Elliston
237 @author Akim Demaille
238 @page
239 @vskip 0pt plus 1filll
240 @insertcopying
241 @end titlepage
243 @contents
246 @ifnottex
247 @node Top
248 @top Autoconf
249 @insertcopying
250 @end ifnottex
252 @c The master menu, created with texinfo-master-menu, goes here.
254 @menu
255 * Introduction::                Autoconf's purpose, strengths, and weaknesses
256 * The GNU Build System::        A set of tools for portable software packages
257 * Making configure Scripts::    How to organize and produce Autoconf scripts
258 * Setup::                       Initialization and output
259 * Existing Tests::              Macros that check for particular features
260 * Writing Tests::               How to write new feature checks
261 * Results::                     What to do with results from feature checks
262 * Programming in M4::           Layers on top of which Autoconf is written
263 * Writing Autoconf Macros::     Adding new macros to Autoconf
264 * Portable Shell::              Shell script portability pitfalls
265 * Portable Make::               Makefile portability pitfalls
266 * Portable C and C++::          C and C++ portability pitfalls
267 * Manual Configuration::        Selecting features that can't be guessed
268 * Site Configuration::          Local defaults for @command{configure}
269 * Running configure Scripts::   How to use the Autoconf output
270 * config.status Invocation::    Recreating a configuration
271 * Obsolete Constructs::         Kept for backward compatibility
272 * Using Autotest::              Creating portable test suites
273 * FAQ::                         Frequent Autoconf Questions, with answers
274 * History::                     History of Autoconf
275 * GNU Free Documentation License::  License for copying this manual
276 * Indices::                     Indices of symbols, concepts, etc.
278 @detailmenu
279  --- The Detailed Node Listing ---
281 The @acronym{GNU} Build System
283 * Automake::                    Escaping makefile hell
284 * Gnulib::                      The @acronym{GNU} portability library
285 * Libtool::                     Building libraries portably
286 * Pointers::                    More info on the @acronym{GNU} build system
288 Making @command{configure} Scripts
290 * Writing Autoconf Input::      What to put in an Autoconf input file
291 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
292 * ifnames Invocation::          Listing the conditionals in source code
293 * autoconf Invocation::         How to create configuration scripts
294 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
296 Writing @file{configure.ac}
298 * Shell Script Compiler::       Autoconf as solution of a problem
299 * Autoconf Language::           Programming in Autoconf
300 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
302 Initialization and Output Files
304 * Initializing configure::      Option processing etc.
305 * Versioning::                  Dealing with Autoconf versions
306 * Notices::                     Copyright, version numbers in @command{configure}
307 * Input::                       Where Autoconf should find files
308 * Output::                      Outputting results from the configuration
309 * Configuration Actions::       Preparing the output based on results
310 * Configuration Files::         Creating output files
311 * Makefile Substitutions::      Using output variables in makefiles
312 * Configuration Headers::       Creating a configuration header file
313 * Configuration Commands::      Running arbitrary instantiation commands
314 * Configuration Links::         Links depending on the configuration
315 * Subdirectories::              Configuring independent packages together
316 * Default Prefix::              Changing the default installation prefix
318 Substitutions in Makefiles
320 * Preset Output Variables::     Output variables that are always set
321 * Installation Directory Variables::  Other preset output variables
322 * Changed Directory Variables:: Warnings about @file{datarootdir}
323 * Build Directories::           Supporting multiple concurrent compiles
324 * Automatic Remaking::          Makefile rules for configuring
326 Configuration Header Files
328 * Header Templates::            Input for the configuration headers
329 * autoheader Invocation::       How to create configuration templates
330 * Autoheader Macros::           How to specify CPP templates
332 Existing Tests
334 * Common Behavior::             Macros' standard schemes
335 * Alternative Programs::        Selecting between alternative programs
336 * Files::                       Checking for the existence of files
337 * Libraries::                   Library archives that might be missing
338 * Library Functions::           C library functions that might be missing
339 * Header Files::                Header files that might be missing
340 * Declarations::                Declarations that may be missing
341 * Structures::                  Structures or members that might be missing
342 * Types::                       Types that might be missing
343 * Compilers and Preprocessors::  Checking for compiling programs
344 * System Services::             Operating system services
345 * Posix Variants::              Special kludges for specific Posix variants
346 * Erlang Libraries::            Checking for the existence of Erlang libraries
348 Common Behavior
350 * Standard Symbols::            Symbols defined by the macros
351 * Default Includes::            Includes used by the generic macros
353 Alternative Programs
355 * Particular Programs::         Special handling to find certain programs
356 * Generic Programs::            How to find other programs
358 Library Functions
360 * Function Portability::        Pitfalls with usual functions
361 * Particular Functions::        Special handling to find certain functions
362 * Generic Functions::           How to find other functions
364 Header Files
366 * Header Portability::          Collected knowledge on common headers
367 * Particular Headers::          Special handling to find certain headers
368 * Generic Headers::             How to find other headers
370 Declarations
372 * Particular Declarations::     Macros to check for certain declarations
373 * Generic Declarations::        How to find other declarations
375 Structures
377 * Particular Structures::       Macros to check for certain structure members
378 * Generic Structures::          How to find other structure members
380 Types
382 * Particular Types::            Special handling to find certain types
383 * Generic Types::               How to find other types
385 Compilers and Preprocessors
387 * Specific Compiler Characteristics::  Some portability issues
388 * Generic Compiler Characteristics::  Language independent tests and features
389 * C Compiler::                  Checking its characteristics
390 * C++ Compiler::                Likewise
391 * Objective C Compiler::        Likewise
392 * Erlang Compiler and Interpreter::  Likewise
393 * Fortran Compiler::            Likewise
395 Writing Tests
397 * Language Choice::             Selecting which language to use for testing
398 * Writing Test Programs::       Forging source files for compilers
399 * Running the Preprocessor::    Detecting preprocessor symbols
400 * Running the Compiler::        Detecting language or header features
401 * Running the Linker::          Detecting library features
402 * Runtime::                     Testing for runtime features
403 * Systemology::                 A zoology of operating systems
404 * Multiple Cases::              Tests for several possible values
406 Writing Test Programs
408 * Guidelines::                  General rules for writing test programs
409 * Test Functions::              Avoiding pitfalls in test programs
410 * Generating Sources::          Source program boilerplate
412 Results of Tests
414 * Defining Symbols::            Defining C preprocessor symbols
415 * Setting Output Variables::    Replacing variables in output files
416 * Special Chars in Variables::  Characters to beware of in variables
417 * Caching Results::             Speeding up subsequent @command{configure} runs
418 * Printing Messages::           Notifying @command{configure} users
420 Caching Results
422 * Cache Variable Names::        Shell variables used in caches
423 * Cache Files::                 Files @command{configure} uses for caching
424 * Cache Checkpointing::         Loading and saving the cache file
426 Programming in M4
428 * M4 Quotation::                Protecting macros from unwanted expansion
429 * Using autom4te::              The Autoconf executables backbone
430 * Programming in M4sugar::      Convenient pure M4 macros
431 * Programming in M4sh::         Common shell Constructs
432 * File Descriptor Macros::      File descriptor macros for input and output
434 M4 Quotation
436 * Active Characters::           Characters that change the behavior of M4
437 * One Macro Call::              Quotation and one macro call
438 * Quoting and Parameters::      M4 vs. shell parameters
439 * Quotation and Nested Macros::  Macros calling macros
440 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
441 * Quadrigraphs::                Another way to escape special characters
442 * Quotation Rule Of Thumb::     One parenthesis, one quote
444 Using @command{autom4te}
446 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
447 * Customizing autom4te::        Customizing the Autoconf package
449 Programming in M4sugar
451 * Redefined M4 Macros::         M4 builtins changed in M4sugar
452 * Diagnostic Macros::           Diagnostic messages from M4sugar
453 * Diversion support::           Diversions in M4sugar
454 * Conditional constructs::      Conditions in M4
455 * Looping constructs::          Iteration in M4
456 * Evaluation Macros::           More quotation and evaluation control
457 * Text processing Macros::      String manipulation in M4
458 * Number processing Macros::    Arithmetic computation in M4
459 * Forbidden Patterns::          Catching unexpanded macros
461 Writing Autoconf Macros
463 * Macro Definitions::           Basic format of an Autoconf macro
464 * Macro Names::                 What to call your new macros
465 * Reporting Messages::          Notifying @command{autoconf} users
466 * Dependencies Between Macros::  What to do when macros depend on other macros
467 * Obsoleting Macros::           Warning about old ways of doing things
468 * Coding Style::                Writing Autoconf macros @`a la Autoconf
470 Dependencies Between Macros
472 * Prerequisite Macros::         Ensuring required information
473 * Suggested Ordering::          Warning about possible ordering problems
474 * One-Shot Macros::             Ensuring a macro is called only once
476 Portable Shell Programming
478 * Shellology::                  A zoology of shells
479 * Here-Documents::              Quirks and tricks
480 * File Descriptors::            FDs and redirections
481 * File System Conventions::     File names
482 * Shell Pattern Matching::      Pattern matching
483 * Shell Substitutions::         Variable and command expansions
484 * Assignments::                 Varying side effects of assignments
485 * Parentheses::                 Parentheses in shell scripts
486 * Slashes::                     Slashes in shell scripts
487 * Special Shell Variables::     Variables you should not change
488 * Limitations of Builtins::     Portable use of not so portable /bin/sh
489 * Limitations of Usual Tools::  Portable use of portable tools
491 Portable Make Programming
493 * $< in Ordinary Make Rules::   $< in ordinary rules
494 * Failure in Make Rules::       Failing portably in rules
495 * Special Chars in Names::      Special Characters in Macro Names
496 * Backslash-Newline-Newline::   Empty last lines in macro definitions
497 * Backslash-Newline Comments::  Spanning comments across line boundaries
498 * Long Lines in Makefiles::     Line length limitations
499 * Macros and Submakes::         @code{make macro=value} and submakes
500 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
501 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
502 * Comments in Make Rules::      Other problems with Make comments
503 * obj/ and Make::               Don't name a subdirectory @file{obj}
504 * make -k Status::              Exit status of @samp{make -k}
505 * VPATH and Make::              @code{VPATH} woes
506 * Single Suffix Rules::         Single suffix rules and separated dependencies
507 * Timestamps and Make::         Subsecond timestamp resolution
509 @code{VPATH} and Make
511 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
512 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
513 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
514 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
515 * Make Target Lookup::          More details about @code{VPATH} lookup
517 Portable C and C++ Programming
519 * Varieties of Unportability::  How to make your programs unportable
520 * Integer Overflow::            When integers get too large
521 * Null Pointers::               Properties of null pointers
522 * Buffer Overruns::             Subscript errors and the like
523 * Volatile Objects::            @code{volatile} and signals
524 * Floating Point Portability::  Portable floating-point arithmetic
525 * Exiting Portably::            Exiting and the exit status
527 Manual Configuration
529 * Specifying Names::            Specifying the system type
530 * Canonicalizing::              Getting the canonical system type
531 * Using System Type::           What to do with the system type
533 Site Configuration
535 * Help Formatting::             Customizing @samp{configure --help}
536 * External Software::           Working with other optional software
537 * Package Options::             Selecting optional features
538 * Pretty Help Strings::         Formatting help string
539 * Option Checking::             Controlling checking of @command{configure} options
540 * Site Details::                Configuring site details
541 * Transforming Names::          Changing program names when installing
542 * Site Defaults::               Giving @command{configure} local defaults
544 Transforming Program Names When Installing
546 * Transformation Options::      @command{configure} options to transform names
547 * Transformation Examples::     Sample uses of transforming names
548 * Transformation Rules::        Makefile uses of transforming names
550 Running @command{configure} Scripts
552 * Basic Installation::          Instructions for typical cases
553 * Compilers and Options::       Selecting compilers and optimization
554 * Multiple Architectures::      Compiling for multiple architectures at once
555 * Installation Names::          Installing in different directories
556 * Optional Features::           Selecting optional features
557 * System Type::                 Specifying the system type
558 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
559 * Defining Variables::          Specifying the compiler etc.
560 * configure Invocation::        Changing how @command{configure} runs
562 Obsolete Constructs
564 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
565 * acconfig Header::             Additional entries in @file{config.h.in}
566 * autoupdate Invocation::       Automatic update of @file{configure.ac}
567 * Obsolete Macros::             Backward compatibility macros
568 * Autoconf 1::                  Tips for upgrading your files
569 * Autoconf 2.13::               Some fresher tips
571 Upgrading From Version 1
573 * Changed File Names::          Files you might rename
574 * Changed Makefiles::           New things to put in @file{Makefile.in}
575 * Changed Macros::              Macro calls you might replace
576 * Changed Results::             Changes in how to check test results
577 * Changed Macro Writing::       Better ways to write your own macros
579 Upgrading From Version 2.13
581 * Changed Quotation::           Broken code which used to work
582 * New Macros::                  Interaction with foreign macros
583 * Hosts and Cross-Compilation::  Bugward compatibility kludges
584 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
585 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
587 Generating Test Suites with Autotest
589 * Using an Autotest Test Suite::  Autotest and the user
590 * Writing Testsuites::          Autotest macros
591 * testsuite Invocation::        Running @command{testsuite} scripts
592 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
594 Using an Autotest Test Suite
596 * testsuite Scripts::           The concepts of Autotest
597 * Autotest Logs::               Their contents
599 Frequent Autoconf Questions, with answers
601 * Distributing::                Distributing @command{configure} scripts
602 * Why GNU M4::                  Why not use the standard M4?
603 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
604 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
605 * Defining Directories::        Passing @code{datadir} to program
606 * Autom4te Cache::              What is it?  Can I remove it?
607 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
609 History of Autoconf
611 * Genesis::                     Prehistory and naming of @command{configure}
612 * Exodus::                      The plagues of M4 and Perl
613 * Leviticus::                   The priestly code of portability arrives
614 * Numbers::                     Growth and contributors
615 * Deuteronomy::                 Approaching the promises of easy configuration
617 Indices
619 * Environment Variable Index::  Index of environment variables used
620 * Output Variable Index::       Index of variables set in output files
621 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
622 * Autoconf Macro Index::        Index of Autoconf macros
623 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
624 * Autotest Macro Index::        Index of Autotest macros
625 * Program & Function Index::    Index of those with portability problems
626 * Concept Index::               General index
628 @end detailmenu
629 @end menu
631 @c ============================================================= Introduction.
633 @node Introduction
634 @chapter Introduction
635 @cindex Introduction
637 @flushright
638 A physicist, an engineer, and a computer scientist were discussing the
639 nature of God.  ``Surely a Physicist,'' said the physicist, ``because
640 early in the Creation, God made Light; and you know, Maxwell's
641 equations, the dual nature of electromagnetic waves, the relativistic
642 consequences@dots{}'' ``An Engineer!,'' said the engineer, ``because
643 before making Light, God split the Chaos into Land and Water; it takes a
644 hell of an engineer to handle that big amount of mud, and orderly
645 separation of solids from liquids@dots{}'' The computer scientist
646 shouted: ``And the Chaos, where do you think it was coming from, hmm?''
648 ---Anonymous
649 @end flushright
650 @c (via Franc,ois Pinard)
652 Autoconf is a tool for producing shell scripts that automatically
653 configure software source code packages to adapt to many kinds of
654 Posix-like systems.  The configuration scripts produced by Autoconf
655 are independent of Autoconf when they are run, so their users do not
656 need to have Autoconf.
658 The configuration scripts produced by Autoconf require no manual user
659 intervention when run; they do not normally even need an argument
660 specifying the system type.  Instead, they individually test for the
661 presence of each feature that the software package they are for might need.
662 (Before each check, they print a one-line message stating what they are
663 checking for, so the user doesn't get too bored while waiting for the
664 script to finish.)  As a result, they deal well with systems that are
665 hybrids or customized from the more common Posix variants.  There is
666 no need to maintain files that list the features supported by each
667 release of each variant of Posix.
669 For each software package that Autoconf is used with, it creates a
670 configuration script from a template file that lists the system features
671 that the package needs or can use.  After the shell code to recognize
672 and respond to a system feature has been written, Autoconf allows it to
673 be shared by many software packages that can use (or need) that feature.
674 If it later turns out that the shell code needs adjustment for some
675 reason, it needs to be changed in only one place; all of the
676 configuration scripts can be regenerated automatically to take advantage
677 of the updated code.
679 The Metaconfig package is similar in purpose to Autoconf, but the
680 scripts it produces require manual user intervention, which is quite
681 inconvenient when configuring large source trees.  Unlike Metaconfig
682 scripts, Autoconf scripts can support cross-compiling, if some care is
683 taken in writing them.
685 Autoconf does not solve all problems related to making portable
686 software packages---for a more complete solution, it should be used in
687 concert with other @acronym{GNU} build tools like Automake and
688 Libtool.  These other tools take on jobs like the creation of a
689 portable, recursive makefile with all of the standard targets,
690 linking of shared libraries, and so on.  @xref{The GNU Build System},
691 for more information.
693 Autoconf imposes some restrictions on the names of macros used with
694 @code{#if} in C programs (@pxref{Preprocessor Symbol Index}).
696 Autoconf requires @acronym{GNU} M4 version 1.4.5 or later in order to
697 generate the scripts.  It uses features that some versions of M4,
698 including @acronym{GNU} M4 1.3, do not have.  Autoconf works better
699 with @acronym{GNU} M4 version 1.4.8 or later, though this is not
700 required.
702 @xref{Autoconf 1}, for information about upgrading from version 1.
703 @xref{History}, for the story of Autoconf's development.  @xref{FAQ},
704 for answers to some common questions about Autoconf.
706 See the @uref{http://www.gnu.org/software/autoconf/,
707 Autoconf web page} for up-to-date information, details on the mailing
708 lists, pointers to a list of known bugs, etc.
710 Mail suggestions to @email{autoconf@@gnu.org, the Autoconf mailing
711 list}.  Past suggestions are
712 @uref{http://lists.gnu.org/archive/html/autoconf/, archived}.
714 Mail bug reports to @email{bug-autoconf@@gnu.org, the
715 Autoconf Bugs mailing list}.  Past bug reports are
716 @uref{http://lists.gnu.org/archive/html/bug-autoconf/, archived}.
718 If possible, first check that your bug is
719 not already solved in current development versions, and that it has not
720 been reported yet.  Be sure to include all the needed information and a
721 short @file{configure.ac} that demonstrates the problem.
723 Autoconf's development tree is accessible via @command{git}; see the
724 @uref{http://savannah.gnu.org/projects/autoconf/, Autoconf
725 Summary} for details, or view
726 @uref{http://git.sv.gnu.org/gitweb/?p=autoconf.git, the actual
727 repository}.  Anonymous @acronym{CVS} access is also available, see
728 @file{README} for more details.  Patches relative to the
729 current @command{git} version can be sent for review to the
730 @email{autoconf-patches@@gnu.org, Autoconf Patches mailing list}.
731 Discussions on past patches are
732 @uref{http://lists.gnu.org/@/archive/@/html/@/autoconf-patches/,
733 archived}, and all commits are archived in the read-only
734 @email{autoconf-commit@@gnu.org, Autoconf Commit mailing list}, which is
735 also @uref{http://lists.gnu.org/@/archive/@/html/@/autoconf-commit/,
736 archived}.
738 Because of its mission, the Autoconf package itself
739 includes only a set of often-used
740 macros that have already demonstrated their usefulness.  Nevertheless,
741 if you wish to share your macros, or find existing ones, see the
742 @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
743 Archive}, which is kindly run by @email{simons@@cryp.to,
744 Peter Simons}.
747 @c ================================================= The GNU Build System
749 @node The GNU Build System
750 @chapter The @acronym{GNU} Build System
751 @cindex @acronym{GNU} build system
753 Autoconf solves an important problem---reliable discovery of
754 system-specific build and runtime information---but this is only one
755 piece of the puzzle for the development of portable software.  To this
756 end, the @acronym{GNU} project has developed a suite of integrated
757 utilities to finish the job Autoconf started: the @acronym{GNU} build
758 system, whose most important components are Autoconf, Automake, and
759 Libtool.  In this chapter, we introduce you to those tools, point you
760 to sources of more information, and try to convince you to use the
761 entire @acronym{GNU} build system for your software.
763 @menu
764 * Automake::                    Escaping makefile hell
765 * Gnulib::                      The @acronym{GNU} portability library
766 * Libtool::                     Building libraries portably
767 * Pointers::                    More info on the @acronym{GNU} build system
768 @end menu
770 @node Automake
771 @section Automake
773 The ubiquity of @command{make} means that a makefile is almost the
774 only viable way to distribute automatic build rules for software, but
775 one quickly runs into its numerous limitations.  Its lack of
776 support for automatic dependency tracking, recursive builds in
777 subdirectories, reliable timestamps (e.g., for network file systems), and
778 so on, mean that developers must painfully (and often incorrectly)
779 reinvent the wheel for each project.  Portability is non-trivial, thanks
780 to the quirks of @command{make} on many systems.  On top of all this is the
781 manual labor required to implement the many standard targets that users
782 have come to expect (@code{make install}, @code{make distclean},
783 @code{make uninstall}, etc.).  Since you are, of course, using Autoconf,
784 you also have to insert repetitive code in your @code{Makefile.in} to
785 recognize @code{@@CC@@}, @code{@@CFLAGS@@}, and other substitutions
786 provided by @command{configure}.  Into this mess steps @dfn{Automake}.
787 @cindex Automake
789 Automake allows you to specify your build needs in a @code{Makefile.am}
790 file with a vastly simpler and more powerful syntax than that of a plain
791 makefile, and then generates a portable @code{Makefile.in} for
792 use with Autoconf.  For example, the @code{Makefile.am} to build and
793 install a simple ``Hello world'' program might look like:
795 @example
796 bin_PROGRAMS = hello
797 hello_SOURCES = hello.c
798 @end example
800 @noindent
801 The resulting @code{Makefile.in} (~400 lines) automatically supports all
802 the standard targets, the substitutions provided by Autoconf, automatic
803 dependency tracking, @code{VPATH} building, and so on.  @command{make}
804 builds the @code{hello} program, and @code{make install} installs it
805 in @file{/usr/local/bin} (or whatever prefix was given to
806 @command{configure}, if not @file{/usr/local}).
808 The benefits of Automake increase for larger packages (especially ones
809 with subdirectories), but even for small programs the added convenience
810 and portability can be substantial.  And that's not all@enddots{}
812 @node Gnulib
813 @section Gnulib
815 @acronym{GNU} software has a well-deserved reputation for running on
816 many different types of systems.  While our primary goal is to write
817 software for the @acronym{GNU} system, many users and developers have
818 been introduced to us through the systems that they were already using.
820 @cindex Gnulib
821 Gnulib is a central location for common @acronym{GNU} code, intended to
822 be shared among free software packages.  Its components are typically
823 shared at the source level, rather than being a library that gets built,
824 installed, and linked against.  The idea is to copy files from Gnulib
825 into your own source tree.  There is no distribution tarball; developers
826 should just grab source modules from the repository.  The source files
827 are available online, under various licenses, mostly @acronym{GNU}
828 @acronym{GPL} or @acronym{GNU} @acronym{LGPL}.
830 Gnulib modules typically contain C source code along with Autoconf
831 macros used to configure the source code.  For example, the Gnulib
832 @code{stdbool} module implements a @file{stdbool.h} header that nearly
833 conforms to C99, even on old-fashioned hosts that lack @file{stdbool.h}.
834 This module contains a source file for the replacement header, along
835 with an Autoconf macro that arranges to use the replacement header on
836 old-fashioned systems.
838 @node Libtool
839 @section Libtool
841 Often, one wants to build not only programs, but libraries, so that
842 other programs can benefit from the fruits of your labor.  Ideally, one
843 would like to produce @emph{shared} (dynamically linked) libraries,
844 which can be used by multiple programs without duplication on disk or in
845 memory and can be updated independently of the linked programs.
846 Producing shared libraries portably, however, is the stuff of
847 nightmares---each system has its own incompatible tools, compiler flags,
848 and magic incantations.  Fortunately, @acronym{GNU} provides a solution:
849 @dfn{Libtool}.
850 @cindex Libtool
852 Libtool handles all the requirements of building shared libraries for
853 you, and at this time seems to be the @emph{only} way to do so with any
854 portability.  It also handles many other headaches, such as: the
855 interaction of Make rules with the variable suffixes of
856 shared libraries, linking reliably with shared libraries before they are
857 installed by the superuser, and supplying a consistent versioning system
858 (so that different versions of a library can be installed or upgraded
859 without breaking binary compatibility).  Although Libtool, like
860 Autoconf, can be used without Automake, it is most simply utilized in
861 conjunction with Automake---there, Libtool is used automatically
862 whenever shared libraries are needed, and you need not know its syntax.
864 @node Pointers
865 @section Pointers
867 Developers who are used to the simplicity of @command{make} for small
868 projects on a single system might be daunted at the prospect of
869 learning to use Automake and Autoconf.  As your software is
870 distributed to more and more users, however, you otherwise
871 quickly find yourself putting lots of effort into reinventing the
872 services that the @acronym{GNU} build tools provide, and making the
873 same mistakes that they once made and overcame.  (Besides, since
874 you're already learning Autoconf, Automake is a piece of cake.)
876 There are a number of places that you can go to for more information on
877 the @acronym{GNU} build tools.
879 @itemize @minus
881 @item Web
883 The home pages for
884 @uref{http://www.gnu.org/@/software/@/autoconf/, Autoconf},
885 @uref{http://www.gnu.org/@/software/@/automake/, Automake},
886 @uref{http://www.gnu.org/@/software/@/gnulib/, Gnulib}, and
887 @uref{http://www.gnu.org/@/software/@/libtool/, Libtool}.
889 @item Automake Manual
891 @xref{Top, , Automake, automake, @acronym{GNU} Automake}, for more
892 information on Automake.
894 @item Books
896 The book @cite{@acronym{GNU} Autoconf, Automake and
897 Libtool}@footnote{@cite{@acronym{GNU} Autoconf, Automake and Libtool},
898 by G. V. Vaughan, B. Elliston, T. Tromey, and I. L. Taylor.  SAMS (originally
899 New Riders), 2000, ISBN 1578701902.} describes the complete @acronym{GNU}
900 build environment.  You can also find
901 @uref{http://sources.redhat.com/@/autobook/, the entire book on-line}.
903 @end itemize
905 @c ================================================= Making configure Scripts.
907 @node Making configure Scripts
908 @chapter Making @command{configure} Scripts
909 @cindex @file{aclocal.m4}
910 @cindex @command{configure}
912 The configuration scripts that Autoconf produces are by convention
913 called @command{configure}.  When run, @command{configure} creates several
914 files, replacing configuration parameters in them with appropriate
915 values.  The files that @command{configure} creates are:
917 @itemize @minus
918 @item
919 one or more @file{Makefile} files, usually one in each subdirectory of the
920 package (@pxref{Makefile Substitutions});
922 @item
923 optionally, a C header file, the name of which is configurable,
924 containing @code{#define} directives (@pxref{Configuration Headers});
926 @item
927 a shell script called @file{config.status} that, when run, recreates
928 the files listed above (@pxref{config.status Invocation});
930 @item
931 an optional shell script normally called @file{config.cache}
932 (created when using @samp{configure --config-cache}) that
933 saves the results of running many of the tests (@pxref{Cache Files});
935 @item
936 a file called @file{config.log} containing any messages produced by
937 compilers, to help debugging if @command{configure} makes a mistake.
938 @end itemize
940 @cindex @file{configure.in}
941 @cindex @file{configure.ac}
942 To create a @command{configure} script with Autoconf, you need to write an
943 Autoconf input file @file{configure.ac} (or @file{configure.in}) and run
944 @command{autoconf} on it.  If you write your own feature tests to
945 supplement those that come with Autoconf, you might also write files
946 called @file{aclocal.m4} and @file{acsite.m4}.  If you use a C header
947 file to contain @code{#define} directives, you might also run
948 @command{autoheader}, and you can distribute the generated file
949 @file{config.h.in} with the package.
951 Here is a diagram showing how the files that can be used in
952 configuration are produced.  Programs that are executed are suffixed by
953 @samp{*}.  Optional files are enclosed in square brackets (@samp{[]}).
954 @command{autoconf} and @command{autoheader} also read the installed Autoconf
955 macro files (by reading @file{autoconf.m4}).
957 @noindent
958 Files used in preparing a software package for distribution:
959 @example
960 your source files --> [autoscan*] --> [configure.scan] --> configure.ac
962 @group
963 configure.ac --.
964                |   .------> autoconf* -----> configure
965 [aclocal.m4] --+---+
966                |   `-----> [autoheader*] --> [config.h.in]
967 [acsite.m4] ---'
968 @end group
970 Makefile.in -------------------------------> Makefile.in
971 @end example
973 @noindent
974 Files used in configuring a software package:
975 @example
976 @group
977                        .-------------> [config.cache]
978 configure* ------------+-------------> config.log
979                        |
980 [config.h.in] -.       v            .-> [config.h] -.
981                +--> config.status* -+               +--> make*
982 Makefile.in ---'                    `-> Makefile ---'
983 @end group
984 @end example
986 @menu
987 * Writing Autoconf Input::      What to put in an Autoconf input file
988 * autoscan Invocation::         Semi-automatic @file{configure.ac} writing
989 * ifnames Invocation::          Listing the conditionals in source code
990 * autoconf Invocation::         How to create configuration scripts
991 * autoreconf Invocation::       Remaking multiple @command{configure} scripts
992 @end menu
994 @node Writing Autoconf Input
995 @section Writing @file{configure.ac}
997 To produce a @command{configure} script for a software package, create a
998 file called @file{configure.ac} that contains invocations of the
999 Autoconf macros that test the system features your package needs or can
1000 use.  Autoconf macros already exist to check for many features; see
1001 @ref{Existing Tests}, for their descriptions.  For most other features,
1002 you can use Autoconf template macros to produce custom checks; see
1003 @ref{Writing Tests}, for information about them.  For especially tricky
1004 or specialized features, @file{configure.ac} might need to contain some
1005 hand-crafted shell commands; see @ref{Portable Shell}.  The
1006 @command{autoscan} program can give you a good start in writing
1007 @file{configure.ac} (@pxref{autoscan Invocation}, for more information).
1009 Previous versions of Autoconf promoted the name @file{configure.in},
1010 which is somewhat ambiguous (the tool needed to process this file is not
1011 described by its extension), and introduces a slight confusion with
1012 @file{config.h.in} and so on (for which @samp{.in} means ``to be
1013 processed by @command{configure}'').  Using @file{configure.ac} is now
1014 preferred.
1016 @menu
1017 * Shell Script Compiler::       Autoconf as solution of a problem
1018 * Autoconf Language::           Programming in Autoconf
1019 * Autoconf Input Layout::       Standard organization of @file{configure.ac}
1020 @end menu
1022 @node Shell Script Compiler
1023 @subsection A Shell Script Compiler
1025 Just as for any other computer language, in order to properly program
1026 @file{configure.ac} in Autoconf you must understand @emph{what} problem
1027 the language tries to address and @emph{how} it does so.
1029 The problem Autoconf addresses is that the world is a mess.  After all,
1030 you are using Autoconf in order to have your package compile easily on
1031 all sorts of different systems, some of them being extremely hostile.
1032 Autoconf itself bears the price for these differences: @command{configure}
1033 must run on all those systems, and thus @command{configure} must limit itself
1034 to their lowest common denominator of features.
1036 Naturally, you might then think of shell scripts; who needs
1037 @command{autoconf}?  A set of properly written shell functions is enough to
1038 make it easy to write @command{configure} scripts by hand.  Sigh!
1039 Unfortunately, shell functions do not belong to the least common
1040 denominator; therefore, where you would like to define a function and
1041 use it ten times, you would instead need to copy its body ten times.
1043 So, what is really needed is some kind of compiler, @command{autoconf},
1044 that takes an Autoconf program, @file{configure.ac}, and transforms it
1045 into a portable shell script, @command{configure}.
1047 How does @command{autoconf} perform this task?
1049 There are two obvious possibilities: creating a brand new language or
1050 extending an existing one.  The former option is attractive: all
1051 sorts of optimizations could easily be implemented in the compiler and
1052 many rigorous checks could be performed on the Autoconf program
1053 (e.g., rejecting any non-portable construct).  Alternatively, you can
1054 extend an existing language, such as the @code{sh} (Bourne shell)
1055 language.
1057 Autoconf does the latter: it is a layer on top of @code{sh}.  It was
1058 therefore most convenient to implement @command{autoconf} as a macro
1059 expander: a program that repeatedly performs @dfn{macro expansions} on
1060 text input, replacing macro calls with macro bodies and producing a pure
1061 @code{sh} script in the end.  Instead of implementing a dedicated
1062 Autoconf macro expander, it is natural to use an existing
1063 general-purpose macro language, such as M4, and implement the extensions
1064 as a set of M4 macros.
1067 @node Autoconf Language
1068 @subsection The Autoconf Language
1069 @cindex quotation
1071 The Autoconf language differs from many other computer
1072 languages because it treats actual code the same as plain text.  Whereas
1073 in C, for instance, data and instructions have different syntactic
1074 status, in Autoconf their status is rigorously the same.  Therefore, we
1075 need a means to distinguish literal strings from text to be expanded:
1076 quotation.
1078 When calling macros that take arguments, there must not be any white
1079 space between the macro name and the open parenthesis.  Arguments should
1080 be enclosed within the M4 quote characters @samp{[} and @samp{]}, and be
1081 separated by commas.  Any leading blanks or newlines in arguments are ignored,
1082 unless they are quoted.  You should always quote an argument that
1083 might contain a macro name, comma, parenthesis, or a leading blank or
1084 newline.  This rule applies recursively for every macro
1085 call, including macros called from other macros.
1087 For instance:
1089 @example
1090 AC_CHECK_HEADER([stdio.h],
1091                 [AC_DEFINE([HAVE_STDIO_H], [1],
1092                    [Define to 1 if you have <stdio.h>.])],
1093                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1094 @end example
1096 @noindent
1097 is quoted properly.  You may safely simplify its quotation to:
1099 @example
1100 AC_CHECK_HEADER([stdio.h],
1101                 [AC_DEFINE([HAVE_STDIO_H], 1,
1102                    [Define to 1 if you have <stdio.h>.])],
1103                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1104 @end example
1106 @noindent
1107 because @samp{1} cannot contain a macro call.  Here, the argument of
1108 @code{AC_MSG_ERROR} must be quoted; otherwise, its comma would be
1109 interpreted as an argument separator.  Also, the second and third arguments
1110 of @samp{AC_CHECK_HEADER} must be quoted, since they contain
1111 macro calls.  The three arguments @samp{HAVE_STDIO_H}, @samp{stdio.h},
1112 and @samp{Define to 1 if you have <stdio.h>.} do not need quoting, but
1113 if you unwisely defined a macro with a name like @samp{Define} or
1114 @samp{stdio} then they would need quoting.  Cautious Autoconf users
1115 would keep the quotes, but many Autoconf users find such precautions
1116 annoying, and would rewrite the example as follows:
1118 @example
1119 AC_CHECK_HEADER(stdio.h,
1120                 [AC_DEFINE(HAVE_STDIO_H, 1,
1121                    [Define to 1 if you have <stdio.h>.])],
1122                 [AC_MSG_ERROR([Sorry, can't do anything for you])])
1123 @end example
1125 @noindent
1126 This is safe, so long as you adopt good naming conventions and do not
1127 define macros with names like @samp{HAVE_STDIO_H}, @samp{stdio}, or
1128 @samp{h}.  Though it is also safe here to omit the quotes around
1129 @samp{Define to 1 if you have <stdio.h>.} this is not recommended, as
1130 message strings are more likely to inadvertently contain commas.
1132 The following example is wrong and dangerous, as it is underquoted:
1134 @example
1135 AC_CHECK_HEADER(stdio.h,
1136                 AC_DEFINE(HAVE_STDIO_H, 1,
1137                    Define to 1 if you have <stdio.h>.),
1138                 AC_MSG_ERROR([Sorry, can't do anything for you]))
1139 @end example
1141 In other cases, you may have to use text that also resembles a macro
1142 call.  You must quote that text even when it is not passed as a macro
1143 argument:
1145 @example
1146 echo "Hard rock was here!  --[AC_DC]"
1147 @end example
1149 @noindent
1150 which results in:
1152 @example
1153 echo "Hard rock was here!  --AC_DC"
1154 @end example
1156 @noindent
1157 When you use the same text in a macro argument, you must therefore have
1158 an extra quotation level (since one is stripped away by the macro
1159 substitution).  In general, then, it is a good idea to @emph{use double
1160 quoting for all literal string arguments}:
1162 @example
1163 AC_MSG_WARN([[AC_DC stinks  --Iron Maiden]])
1164 @end example
1166 You are now able to understand one of the constructs of Autoconf that
1167 has been continually misunderstood@dots{}  The rule of thumb is that
1168 @emph{whenever you expect macro expansion, expect quote expansion};
1169 i.e., expect one level of quotes to be lost.  For instance:
1171 @example
1172 AC_COMPILE_IFELSE([char b[10];], [], [AC_MSG_ERROR([you lose])])
1173 @end example
1175 @noindent
1176 is incorrect: here, the first argument of @code{AC_COMPILE_IFELSE} is
1177 @samp{char b[10];} and is expanded once, which results in
1178 @samp{char b10;}.  (There was an idiom common in Autoconf's past to
1179 address this issue via the M4 @code{changequote} primitive, but do not
1180 use it!)  Let's take a closer look: the author meant the first argument
1181 to be understood as a literal, and therefore it must be quoted twice:
1183 @example
1184 AC_COMPILE_IFELSE([[char b[10];]], [], [AC_MSG_ERROR([you lose])])
1185 @end example
1187 @noindent
1188 Voil@`a, you actually produce @samp{char b[10];} this time!
1190 On the other hand, descriptions (e.g., the last parameter of
1191 @code{AC_DEFINE} or @code{AS_HELP_STRING}) are not literals---they
1192 are subject to line breaking, for example---and should not be double quoted.
1193 Even if these descriptions are short and are not actually broken, double
1194 quoting them yields weird results.
1196 Some macros take optional arguments, which this documentation represents
1197 as @ovar{arg} (not to be confused with the quote characters).  You may
1198 just leave them empty, or use @samp{[]} to make the emptiness of the
1199 argument explicit, or you may simply omit the trailing commas.  The
1200 three lines below are equivalent:
1202 @example
1203 AC_CHECK_HEADERS([stdio.h], [], [], [])
1204 AC_CHECK_HEADERS([stdio.h],,,)
1205 AC_CHECK_HEADERS([stdio.h])
1206 @end example
1208 It is best to put each macro call on its own line in
1209 @file{configure.ac}.  Most of the macros don't add extra newlines; they
1210 rely on the newline after the macro call to terminate the commands.
1211 This approach makes the generated @command{configure} script a little
1212 easier to read by not inserting lots of blank lines.  It is generally
1213 safe to set shell variables on the same line as a macro call, because
1214 the shell allows assignments without intervening newlines.
1216 You can include comments in @file{configure.ac} files by starting them
1217 with the @samp{#}.  For example, it is helpful to begin
1218 @file{configure.ac} files with a line like this:
1220 @example
1221 # Process this file with autoconf to produce a configure script.
1222 @end example
1224 @node Autoconf Input Layout
1225 @subsection Standard @file{configure.ac} Layout
1227 The order in which @file{configure.ac} calls the Autoconf macros is not
1228 important, with a few exceptions.  Every @file{configure.ac} must
1229 contain a call to @code{AC_INIT} before the checks, and a call to
1230 @code{AC_OUTPUT} at the end (@pxref{Output}).  Additionally, some macros
1231 rely on other macros having been called first, because they check
1232 previously set values of some variables to decide what to do.  These
1233 macros are noted in the individual descriptions (@pxref{Existing
1234 Tests}), and they also warn you when @command{configure} is created if they
1235 are called out of order.
1237 To encourage consistency, here is a suggested order for calling the
1238 Autoconf macros.  Generally speaking, the things near the end of this
1239 list are those that could depend on things earlier in it.  For example,
1240 library functions could be affected by types and libraries.
1242 @display
1243 @group
1244 Autoconf requirements
1245 @code{AC_INIT(@var{package}, @var{version}, @var{bug-report-address})}
1246 information on the package
1247 checks for programs
1248 checks for libraries
1249 checks for header files
1250 checks for types
1251 checks for structures
1252 checks for compiler characteristics
1253 checks for library functions
1254 checks for system services
1255 @code{AC_CONFIG_FILES(@r{[}@var{file@dots{}}@r{]})}
1256 @code{AC_OUTPUT}
1257 @end group
1258 @end display
1261 @node autoscan Invocation
1262 @section Using @command{autoscan} to Create @file{configure.ac}
1263 @cindex @command{autoscan}
1265 The @command{autoscan} program can help you create and/or maintain a
1266 @file{configure.ac} file for a software package.  @command{autoscan}
1267 examines source files in the directory tree rooted at a directory given
1268 as a command line argument, or the current directory if none is given.
1269 It searches the source files for common portability problems and creates
1270 a file @file{configure.scan} which is a preliminary @file{configure.ac}
1271 for that package, and checks a possibly existing @file{configure.ac} for
1272 completeness.
1274 When using @command{autoscan} to create a @file{configure.ac}, you
1275 should manually examine @file{configure.scan} before renaming it to
1276 @file{configure.ac}; it probably needs some adjustments.
1277 Occasionally, @command{autoscan} outputs a macro in the wrong order
1278 relative to another macro, so that @command{autoconf} produces a warning;
1279 you need to move such macros manually.  Also, if you want the package to
1280 use a configuration header file, you must add a call to
1281 @code{AC_CONFIG_HEADERS} (@pxref{Configuration Headers}).  You might
1282 also have to change or add some @code{#if} directives to your program in
1283 order to make it work with Autoconf (@pxref{ifnames Invocation}, for
1284 information about a program that can help with that job).
1286 When using @command{autoscan} to maintain a @file{configure.ac}, simply
1287 consider adding its suggestions.  The file @file{autoscan.log}
1288 contains detailed information on why a macro is requested.
1290 @command{autoscan} uses several data files (installed along with Autoconf)
1291 to determine which macros to output when it finds particular symbols in
1292 a package's source files.  These data files all have the same format:
1293 each line consists of a symbol, one or more blanks, and the Autoconf macro to
1294 output if that symbol is encountered.  Lines starting with @samp{#} are
1295 comments.
1297 @command{autoscan} accepts the following options:
1299 @table @option
1300 @item --help
1301 @itemx -h
1302 Print a summary of the command line options and exit.
1304 @item --version
1305 @itemx -V
1306 Print the version number of Autoconf and exit.
1308 @item --verbose
1309 @itemx -v
1310 Print the names of the files it examines and the potentially interesting
1311 symbols it finds in them.  This output can be voluminous.
1313 @item --include=@var{dir}
1314 @itemx -I @var{dir}
1315 Append @var{dir} to the include path.  Multiple invocations accumulate.
1317 @item --prepend-include=@var{dir}
1318 @item -B @var{dir}
1319 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1320 @end table
1322 @node ifnames Invocation
1323 @section Using @command{ifnames} to List Conditionals
1324 @cindex @command{ifnames}
1326 @command{ifnames} can help you write @file{configure.ac} for a software
1327 package.  It prints the identifiers that the package already uses in C
1328 preprocessor conditionals.  If a package has already been set up to have
1329 some portability, @command{ifnames} can thus help you figure out what its
1330 @command{configure} needs to check for.  It may help fill in some gaps in a
1331 @file{configure.ac} generated by @command{autoscan} (@pxref{autoscan
1332 Invocation}).
1334 @command{ifnames} scans all of the C source files named on the command line
1335 (or the standard input, if none are given) and writes to the standard
1336 output a sorted list of all the identifiers that appear in those files
1337 in @code{#if}, @code{#elif}, @code{#ifdef}, or @code{#ifndef}
1338 directives.  It prints each identifier on a line, followed by a
1339 space-separated list of the files in which that identifier occurs.
1341 @noindent
1342 @command{ifnames} accepts the following options:
1344 @table @option
1345 @item --help
1346 @itemx -h
1347 Print a summary of the command line options and exit.
1349 @item --version
1350 @itemx -V
1351 Print the version number of Autoconf and exit.
1352 @end table
1354 @node autoconf Invocation
1355 @section Using @command{autoconf} to Create @command{configure}
1356 @cindex @command{autoconf}
1358 To create @command{configure} from @file{configure.ac}, run the
1359 @command{autoconf} program with no arguments.  @command{autoconf} processes
1360 @file{configure.ac} with the M4 macro processor, using the
1361 Autoconf macros.  If you give @command{autoconf} an argument, it reads that
1362 file instead of @file{configure.ac} and writes the configuration script
1363 to the standard output instead of to @command{configure}.  If you give
1364 @command{autoconf} the argument @option{-}, it reads from the standard
1365 input instead of @file{configure.ac} and writes the configuration script
1366 to the standard output.
1368 The Autoconf macros are defined in several files.  Some of the files are
1369 distributed with Autoconf; @command{autoconf} reads them first.  Then it
1370 looks for the optional file @file{acsite.m4} in the directory that
1371 contains the distributed Autoconf macro files, and for the optional file
1372 @file{aclocal.m4} in the current directory.  Those files can contain
1373 your site's or the package's own Autoconf macro definitions
1374 (@pxref{Writing Autoconf Macros}, for more information).  If a macro is
1375 defined in more than one of the files that @command{autoconf} reads, the
1376 last definition it reads overrides the earlier ones.
1378 @command{autoconf} accepts the following options:
1380 @table @option
1381 @item --help
1382 @itemx -h
1383 Print a summary of the command line options and exit.
1385 @item --version
1386 @itemx -V
1387 Print the version number of Autoconf and exit.
1389 @item --verbose
1390 @itemx -v
1391 Report processing steps.
1393 @item --debug
1394 @itemx -d
1395 Don't remove the temporary files.
1397 @item --force
1398 @itemx -f
1399 Remake @file{configure} even if newer than its input files.
1401 @item --include=@var{dir}
1402 @itemx -I @var{dir}
1403 Append @var{dir} to the include path.  Multiple invocations accumulate.
1405 @item --prepend-include=@var{dir}
1406 @item -B @var{dir}
1407 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1409 @item --output=@var{file}
1410 @itemx -o @var{file}
1411 Save output (script or trace) to @var{file}.  The file @option{-} stands
1412 for the standard output.
1414 @item --warnings=@var{category}
1415 @itemx -W @var{category}
1416 @evindex WARNINGS
1417 Report the warnings related to @var{category} (which can actually be a
1418 comma separated list).  @xref{Reporting Messages}, macro
1419 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
1420 values include:
1422 @table @samp
1423 @item all
1424 report all the warnings
1426 @item none
1427 report none
1429 @item error
1430 treats warnings as errors
1432 @item no-@var{category}
1433 disable warnings falling into @var{category}
1434 @end table
1436 Warnings about @samp{syntax} are enabled by default, and the environment
1437 variable @env{WARNINGS}, a comma separated list of categories, is
1438 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1439 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  If
1440 you want to disable the defaults and @env{WARNINGS}, but (for example)
1441 enable the warnings about obsolete constructs, you would use @option{-W
1442 none,obsolete}.
1444 @cindex Back trace
1445 @cindex Macro invocation stack
1446 Because @command{autoconf} uses @command{autom4te} behind the scenes, it
1447 displays a back trace for errors, but not for warnings; if you want
1448 them, just pass @option{-W error}.  @xref{autom4te Invocation}, for some
1449 examples.
1451 @item --trace=@var{macro}[:@var{format}]
1452 @itemx -t @var{macro}[:@var{format}]
1453 Do not create the @command{configure} script, but list the calls to
1454 @var{macro} according to the @var{format}.  Multiple @option{--trace}
1455 arguments can be used to list several macros.  Multiple @option{--trace}
1456 arguments for a single macro are not cumulative; instead, you should
1457 just make @var{format} as long as needed.
1459 The @var{format} is a regular string, with newlines if desired, and
1460 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}; see
1461 @ref{autom4te Invocation}, for details on the @var{format}.
1463 @item --initialization
1464 @itemx -i
1465 By default, @option{--trace} does not trace the initialization of the
1466 Autoconf macros (typically the @code{AC_DEFUN} definitions).  This
1467 results in a noticeable speedup, but can be disabled by this option.
1468 @end table
1471 It is often necessary to check the content of a @file{configure.ac}
1472 file, but parsing it yourself is extremely fragile and error-prone.  It
1473 is suggested that you rely upon @option{--trace} to scan
1474 @file{configure.ac}.  For instance, to find the list of variables that
1475 are substituted, use:
1477 @example
1478 @group
1479 $ @kbd{autoconf -t AC_SUBST}
1480 configure.ac:2:AC_SUBST:ECHO_C
1481 configure.ac:2:AC_SUBST:ECHO_N
1482 configure.ac:2:AC_SUBST:ECHO_T
1483 @i{More traces deleted}
1484 @end group
1485 @end example
1487 @noindent
1488 The example below highlights the difference between @samp{$@@},
1489 @samp{$*}, and @samp{$%}.
1491 @example
1492 @group
1493 $ @kbd{cat configure.ac}
1494 AC_DEFINE(This, is, [an
1495 [example]])
1496 $ @kbd{autoconf -t 'AC_DEFINE:@@: $@@}
1497 *: $*
1498 %: $%'
1499 @@: [This],[is],[an
1500 [example]]
1501 *: This,is,an
1502 [example]
1503 %: This:is:an [example]
1504 @end group
1505 @end example
1507 @noindent
1508 The @var{format} gives you a lot of freedom:
1510 @example
1511 @group
1512 $ @kbd{autoconf -t 'AC_SUBST:$$ac_subst@{"$1"@} = "$f:$l";'}
1513 $ac_subst@{"ECHO_C"@} = "configure.ac:2";
1514 $ac_subst@{"ECHO_N"@} = "configure.ac:2";
1515 $ac_subst@{"ECHO_T"@} = "configure.ac:2";
1516 @i{More traces deleted}
1517 @end group
1518 @end example
1520 @noindent
1521 A long @var{separator} can be used to improve the readability of complex
1522 structures, and to ease their parsing (for instance when no single
1523 character is suitable as a separator):
1525 @example
1526 @group
1527 $ @kbd{autoconf -t 'AM_MISSING_PROG:$@{|:::::|@}*'}
1528 ACLOCAL|:::::|aclocal|:::::|$missing_dir
1529 AUTOCONF|:::::|autoconf|:::::|$missing_dir
1530 AUTOMAKE|:::::|automake|:::::|$missing_dir
1531 @i{More traces deleted}
1532 @end group
1533 @end example
1535 @node autoreconf Invocation
1536 @section Using @command{autoreconf} to Update @command{configure} Scripts
1537 @cindex @command{autoreconf}
1539 Installing the various components of the @acronym{GNU} Build System can be
1540 tedious: running @command{autopoint} for Gettext, @command{automake} for
1541 @file{Makefile.in} etc.@: in each directory.  It may be needed either
1542 because some tools such as @command{automake} have been updated on your
1543 system, or because some of the sources such as @file{configure.ac} have
1544 been updated, or finally, simply in order to install the @acronym{GNU} Build
1545 System in a fresh tree.
1547 @command{autoreconf} runs @command{autoconf}, @command{autoheader},
1548 @command{aclocal}, @command{automake}, @command{libtoolize}, and
1549 @command{autopoint} (when appropriate) repeatedly to update the
1550 @acronym{GNU} Build System in the specified directories and their
1551 subdirectories (@pxref{Subdirectories}).  By default, it only remakes
1552 those files that are older than their sources.
1554 If you install a new version of some tool, you can make
1555 @command{autoreconf} remake @emph{all} of the files by giving it the
1556 @option{--force} option.
1558 @xref{Automatic Remaking}, for Make rules to automatically
1559 rebuild @command{configure} scripts when their source files change.  That
1560 method handles the timestamps of configuration header templates
1561 properly, but does not pass @option{--autoconf-dir=@var{dir}} or
1562 @option{--localdir=@var{dir}}.
1564 @cindex Gettext
1565 @cindex @command{autopoint}
1566 Gettext supplies the @command{autopoint} command to add translation
1567 infrastructure to a source package.  If you use @command{autopoint},
1568 your @file{configure.ac} should invoke both @code{AM_GNU_GETTEXT} and
1569 @code{AM_GNU_GETTEXT_VERSION(@var{gettext-version})}.  @xref{autopoint
1570 Invocation, , Invoking the @code{autopoint} Program, gettext,
1571 @acronym{GNU} @code{gettext} utilities}, for further details.
1573 @noindent
1574 @command{autoreconf} accepts the following options:
1576 @table @option
1577 @item --help
1578 @itemx -h
1579 Print a summary of the command line options and exit.
1581 @item --version
1582 @itemx -V
1583 Print the version number of Autoconf and exit.
1585 @item --verbose
1586 Print the name of each directory @command{autoreconf} examines and the
1587 commands it runs.  If given two or more times, pass @option{--verbose}
1588 to subordinate tools that support it.
1590 @item --debug
1591 @itemx -d
1592 Don't remove the temporary files.
1594 @item --force
1595 @itemx -f
1596 Remake even @file{configure} scripts and configuration headers that are
1597 newer than their input files (@file{configure.ac} and, if present,
1598 @file{aclocal.m4}).
1600 @item --install
1601 @itemx -i
1602 Install the missing auxiliary files in the package.  By default, files
1603 are copied; this can be changed with @option{--symlink}.
1605 If deemed appropriate, this option triggers calls to
1606 @samp{automake --add-missing},
1607 @samp{libtoolize}, @samp{autopoint}, etc.
1609 @item --no-recursive
1610 Do not rebuild files in subdirectories to configure (see @ref{Subdirectories},
1611 macro @code{AC_CONFIG_SUBDIRS}).
1613 @item --symlink
1614 @itemx -s
1615 When used with @option{--install}, install symbolic links to the missing
1616 auxiliary files instead of copying them.
1618 @item --make
1619 @itemx -m
1620 When the directories were configured, update the configuration by
1621 running @samp{./config.status --recheck && ./config.status}, and then
1622 run @samp{make}.
1624 @item --include=@var{dir}
1625 @itemx -I @var{dir}
1626 Append @var{dir} to the include path.  Multiple invocations accumulate.
1627 Passed on to @command{autoconf} and @command{autoheader} internally.
1629 @item --prepend-include=@var{dir}
1630 @item -B @var{dir}
1631 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
1632 Passed on to @command{autoconf} and @command{autoheader} internally.
1634 @item --warnings=@var{category}
1635 @itemx -W @var{category}
1636 @evindex WARNINGS
1637 Report the warnings related to @var{category} (which can actually be a
1638 comma separated list).
1640 @table @samp
1641 @item cross
1642 related to cross compilation issues.
1644 @item obsolete
1645 report the uses of obsolete constructs.
1647 @item portability
1648 portability issues
1650 @item syntax
1651 dubious syntactic constructs.
1653 @item all
1654 report all the warnings
1656 @item none
1657 report none
1659 @item error
1660 treats warnings as errors
1662 @item no-@var{category}
1663 disable warnings falling into @var{category}
1664 @end table
1666 Warnings about @samp{syntax} are enabled by default, and the environment
1667 variable @env{WARNINGS}, a comma separated list of categories, is
1668 honored as well.  Passing @option{-W @var{category}} actually behaves as if
1669 you had passed @option{--warnings syntax,$WARNINGS,@var{category}}.  If
1670 you want to disable the defaults and @env{WARNINGS}, but (for example)
1671 enable the warnings about obsolete constructs, you would use @option{-W
1672 none,obsolete}.
1673 @end table
1675 If you want @command{autoreconf} to pass flags that are not listed here
1676 on to @command{aclocal}, set @code{ACLOCAL_AMFLAGS} in your @file{Makefile.am}.
1677 Due to a limitation in the Autoconf implementation these flags currently
1678 must be set on a single line in @file{Makefile.am}, without any
1679 backslash-newlines.
1681 @c ========================================= Initialization and Output Files.
1683 @node Setup
1684 @chapter Initialization and Output Files
1686 Autoconf-generated @command{configure} scripts need some information about
1687 how to initialize, such as how to find the package's source files and
1688 about the output files to produce.  The following sections describe the
1689 initialization and the creation of output files.
1691 @menu
1692 * Initializing configure::      Option processing etc.
1693 * Versioning::                  Dealing with Autoconf versions
1694 * Notices::                     Copyright, version numbers in @command{configure}
1695 * Input::                       Where Autoconf should find files
1696 * Output::                      Outputting results from the configuration
1697 * Configuration Actions::       Preparing the output based on results
1698 * Configuration Files::         Creating output files
1699 * Makefile Substitutions::      Using output variables in makefiles
1700 * Configuration Headers::       Creating a configuration header file
1701 * Configuration Commands::      Running arbitrary instantiation commands
1702 * Configuration Links::         Links depending on the configuration
1703 * Subdirectories::              Configuring independent packages together
1704 * Default Prefix::              Changing the default installation prefix
1705 @end menu
1707 @node Initializing configure
1708 @section Initializing @command{configure}
1710 Every @command{configure} script must call @code{AC_INIT} before doing
1711 anything else.  The only other required macro is @code{AC_OUTPUT}
1712 (@pxref{Output}).
1714 @anchor{AC_INIT}
1715 @defmac AC_INIT (@var{package}, @var{version}, @ovar{bug-report}, @
1716   @ovar{tarname})
1717 @acindex{INIT}
1718 Process any command-line arguments and perform various initializations
1719 and verifications.
1721 Set the name of the @var{package} and its @var{version}.  These are
1722 typically used in @option{--version} support, including that of
1723 @command{configure}.  The optional argument @var{bug-report} should be
1724 the email to which users should send bug reports.  The package
1725 @var{tarname} differs from @var{package}: the latter designates the full
1726 package name (e.g., @samp{GNU Autoconf}), while the former is meant for
1727 distribution tar ball names (e.g., @samp{autoconf}).  It defaults to
1728 @var{package} with @samp{GNU } stripped, lower-cased, and all characters
1729 other than alphanumerics and underscores are changed to @samp{-}.
1731 It is preferable that the arguments of @code{AC_INIT} be static, i.e.,
1732 there should not be any shell computation, but they can be computed by
1735 The following M4 macros (e.g., @code{AC_PACKAGE_NAME}), output variables
1736 (e.g., @code{PACKAGE_NAME}), and preprocessor symbols (e.g.,
1737 @code{PACKAGE_NAME}), are defined by @code{AC_INIT}:
1739 @table @asis
1740 @item @code{AC_PACKAGE_NAME}, @code{PACKAGE_NAME}
1741 @acindex{PACKAGE_NAME}
1742 @ovindex PACKAGE_NAME
1743 @cvindex PACKAGE_NAME
1744 Exactly @var{package}.
1746 @item @code{AC_PACKAGE_TARNAME}, @code{PACKAGE_TARNAME}
1747 @acindex{PACKAGE_TARNAME}
1748 @ovindex PACKAGE_TARNAME
1749 @cvindex PACKAGE_TARNAME
1750 Exactly @var{tarname}.
1752 @item @code{AC_PACKAGE_VERSION}, @code{PACKAGE_VERSION}
1753 @acindex{PACKAGE_VERSION}
1754 @ovindex PACKAGE_VERSION
1755 @cvindex PACKAGE_VERSION
1756 Exactly @var{version}.
1758 @item @code{AC_PACKAGE_STRING}, @code{PACKAGE_STRING}
1759 @acindex{PACKAGE_STRING}
1760 @ovindex PACKAGE_STRING
1761 @cvindex PACKAGE_STRING
1762 Exactly @samp{@var{package} @var{version}}.
1764 @item @code{AC_PACKAGE_BUGREPORT}, @code{PACKAGE_BUGREPORT}
1765 @acindex{PACKAGE_BUGREPORT}
1766 @ovindex PACKAGE_BUGREPORT
1767 @cvindex PACKAGE_BUGREPORT
1768 Exactly @var{bug-report}.
1769 @end table
1770 @end defmac
1772 If your @command{configure} script does its own option processing, it
1773 should inspect @samp{$@@} or @samp{$*} immediately after calling
1774 @code{AC_INIT}, because other Autoconf macros liberally use the
1775 @command{set} command to process strings, and this has the side effect
1776 of updating @samp{$@@} and @samp{$*}.  However, we suggest that you use
1777 standard macros like @code{AC_ARG_ENABLE} instead of attempting to
1778 implement your own option processing.  @xref{Site Configuration}.
1780 @node Versioning
1781 @section Dealing with Autoconf versions
1782 @cindex Autoconf version
1783 @cindex version, Autoconf
1785 The following optional macros can be used to help choose the minimum
1786 version of Autoconf that can successfully compile a given
1787 @file{configure.ac}.
1789 @defmac AC_PREREQ (@var{version})
1790 @acindex{PREREQ}
1791 @cindex Version
1792 Ensure that a recent enough version of Autoconf is being used.  If the
1793 version of Autoconf being used to create @command{configure} is
1794 earlier than @var{version}, print an error message to the standard
1795 error output and exit with failure (exit status is 63).  For example:
1797 @example
1798 AC_PREREQ([@value{VERSION}])
1799 @end example
1801 This macro is the only macro that may be used before @code{AC_INIT}, but
1802 for consistency, you are invited not to do so.
1803 @end defmac
1805 @defmac AC_AUTOCONF_VERSION
1806 @acindex{AUTOCONF_VERSION}
1807 This macro was introduced in Autoconf 2.62.  It identifies the version
1808 of Autoconf that is currently parsing the input file, in a format
1809 suitable for @code{m4_version_compare} (@pxref{m4_version_compare}); in
1810 other words, for this release of Autoconf, its value is
1811 @samp{@value{VERSION}}.  One potential use of this macro is for writing
1812 conditional fallbacks based on when a feature was added to Autoconf,
1813 rather than using @code{AC_PREREQ} to require the newer version of
1814 Autoconf.  However, remember that the Autoconf philosophy favors feature
1815 checks over version checks.
1816 @end defmac
1818 @node Notices
1819 @section Notices in @command{configure}
1820 @cindex Notices in @command{configure}
1822 The following macros manage version numbers for @command{configure}
1823 scripts.  Using them is optional.
1825 @defmac AC_COPYRIGHT (@var{copyright-notice})
1826 @acindex{COPYRIGHT}
1827 @cindex Copyright Notice
1828 State that, in addition to the Free Software Foundation's copyright on
1829 the Autoconf macros, parts of your @command{configure} are covered by the
1830 @var{copyright-notice}.
1832 The @var{copyright-notice} shows up in both the head of
1833 @command{configure} and in @samp{configure --version}.
1834 @end defmac
1837 @defmac AC_REVISION (@var{revision-info})
1838 @acindex{REVISION}
1839 @cindex Revision
1840 Copy revision stamp @var{revision-info} into the @command{configure}
1841 script, with any dollar signs or double-quotes removed.  This macro lets
1842 you put a revision stamp from @file{configure.ac} into @command{configure}
1843 without @acronym{RCS} or @acronym{CVS} changing it when you check in
1844 @command{configure}.  That way, you can determine easily which revision of
1845 @file{configure.ac} a particular @command{configure} corresponds to.
1847 For example, this line in @file{configure.ac}:
1849 @c The asis prevents RCS from changing the example in the manual.
1850 @example
1851 AC_REVISION([$@asis{Revision: 1.30 }$])
1852 @end example
1854 @noindent
1855 produces this in @command{configure}:
1857 @example
1858 #!/bin/sh
1859 # From configure.ac Revision: 1.30
1860 @end example
1861 @end defmac
1864 @node Input
1865 @section Finding @command{configure} Input
1867 @anchor{AC_CONFIG_SRCDIR}
1868 @defmac AC_CONFIG_SRCDIR (@var{unique-file-in-source-dir})
1869 @acindex{CONFIG_SRCDIR}
1870 @var{unique-file-in-source-dir} is some file that is in the package's
1871 source directory; @command{configure} checks for this file's existence to
1872 make sure that the directory that it is told contains the source code in
1873 fact does.  Occasionally people accidentally specify the wrong directory
1874 with @option{--srcdir}; this is a safety check.  @xref{configure
1875 Invocation}, for more information.
1876 @end defmac
1879 @c FIXME: Remove definitively once --install explained.
1881 @c Small packages may store all their macros in @code{aclocal.m4}.  As the
1882 @c set of macros grows, or for maintenance reasons, a maintainer may prefer
1883 @c to split the macros in several files.  In this case, Autoconf must be
1884 @c told which files to load, and in which order.
1886 @c @defmac AC_INCLUDE (@var{file}@dots{})
1887 @c @acindex{INCLUDE}
1888 @c @c FIXME: There is no longer shell globbing.
1889 @c Read the macro definitions that appear in the listed files.  A list of
1890 @c space-separated file names or shell globbing patterns is expected.  The
1891 @c files are read in the order they're listed.
1893 @c Because the order of definition of macros is important (only the last
1894 @c definition of a macro is used), beware that it is @code{AC_INIT} that
1895 @c loads @file{acsite.m4} and @file{aclocal.m4}.  Note that
1896 @c @code{AC_INCLUDE}ing a file before @code{AC_INIT} or within
1897 @c @file{aclocal.m4} is different from doing so after @code{AC_INIT}: in
1898 @c the latter case, non-macro lines from included files may end up in the
1899 @c @file{configure} script, whereas in the former case, they'd be discarded
1900 @c just like any text that appear before @code{AC_INIT}.
1901 @c @end defmac
1903 Packages that do manual configuration or use the @command{install} program
1904 might need to tell @command{configure} where to find some other shell
1905 scripts by calling @code{AC_CONFIG_AUX_DIR}, though the default places
1906 it looks are correct for most cases.
1908 @defmac AC_CONFIG_AUX_DIR (@var{dir})
1909 @acindex{CONFIG_AUX_DIR}
1910 Use the auxiliary build tools (e.g., @file{install-sh},
1911 @file{config.sub}, @file{config.guess}, Cygnus @command{configure},
1912 Automake and Libtool scripts, etc.)@: that are in directory @var{dir}.
1913 These are auxiliary files used in configuration.  @var{dir} can be
1914 either absolute or relative to @file{@var{srcdir}}.  The default is
1915 @file{@var{srcdir}} or @file{@var{srcdir}/..} or
1916 @file{@var{srcdir}/../..}, whichever is the first that contains
1917 @file{install-sh}.  The other files are not checked for, so that using
1918 @code{AC_PROG_INSTALL} does not automatically require distributing the
1919 other auxiliary files.  It checks for @file{install.sh} also, but that
1920 name is obsolete because some @code{make} have a rule that creates
1921 @file{install} from it if there is no makefile.
1923 The auxiliary directory is commonly named @file{build-aux}.
1924 If you need portability to @acronym{DOS} variants, do not name the
1925 auxiliary directory @file{aux}.  @xref{File System Conventions}.
1926 @end defmac
1928 @defmac AC_REQUIRE_AUX_FILE (@var{file})
1929 @acindex{REQUIRE_AUX_FILE}
1930 Declares that @var{file} is expected in the directory defined above.  In
1931 Autoconf proper, this macro does nothing: its sole purpose is to be
1932 traced by third-party tools to produce a list of expected auxiliary
1933 files.  For instance it is called by macros like @code{AC_PROG_INSTALL}
1934 (@pxref{Particular Programs}) or @code{AC_CANONICAL_BUILD}
1935 (@pxref{Canonicalizing}) to register the auxiliary files they need.
1936 @end defmac
1938 Similarly, packages that use @command{aclocal} should declare where
1939 local macros can be found using @code{AC_CONFIG_MACRO_DIR}.
1941 @defmac AC_CONFIG_MACRO_DIR (@var{dir})
1942 @acindex{CONFIG_MACRO_DIR}
1943 Specify @var{dir} as the location of additional local Autoconf macros.
1944 This macro is intended for use by future versions of commands like
1945 @command{autoreconf} that trace macro calls.  It should be called
1946 directly from @file{configure.ac} so that tools that install macros for
1947 @command{aclocal} can find the macros' declarations.
1948 @end defmac
1951 @node Output
1952 @section Outputting Files
1953 @cindex Outputting files
1955 Every Autoconf script, e.g., @file{configure.ac}, should finish by
1956 calling @code{AC_OUTPUT}.  That is the macro that generates and runs
1957 @file{config.status}, which in turn creates the makefiles and any
1958 other files resulting from configuration.  This is the only required
1959 macro besides @code{AC_INIT} (@pxref{Input}).
1961 @anchor{AC_OUTPUT}
1962 @defmac AC_OUTPUT
1963 @acindex{OUTPUT}
1964 @cindex Instantiation
1965 Generate @file{config.status} and launch it.  Call this macro once, at
1966 the end of @file{configure.ac}.
1968 @file{config.status} performs all the configuration actions: all the
1969 output files (see @ref{Configuration Files}, macro
1970 @code{AC_CONFIG_FILES}), header files (see @ref{Configuration Headers},
1971 macro @code{AC_CONFIG_HEADERS}), commands (see @ref{Configuration
1972 Commands}, macro @code{AC_CONFIG_COMMANDS}), links (see
1973 @ref{Configuration Links}, macro @code{AC_CONFIG_LINKS}), subdirectories
1974 to configure (see @ref{Subdirectories}, macro @code{AC_CONFIG_SUBDIRS})
1975 are honored.
1977 The location of your @code{AC_OUTPUT} invocation is the exact point
1978 where configuration actions are taken: any code afterwards is
1979 executed by @code{configure} once @command{config.status} was run.  If
1980 you want to bind actions to @command{config.status} itself
1981 (independently of whether @command{configure} is being run), see
1982 @ref{Configuration Commands, , Running Arbitrary Configuration
1983 Commands}.
1984 @end defmac
1986 Historically, the usage of @code{AC_OUTPUT} was somewhat different.
1987 @xref{Obsolete Macros}, for a description of the arguments that
1988 @code{AC_OUTPUT} used to support.
1991 If you run @command{make} in subdirectories, you should run it using the
1992 @code{make} variable @code{MAKE}.  Most versions of @command{make} set
1993 @code{MAKE} to the name of the @command{make} program plus any options it
1994 was given.  (But many do not include in it the values of any variables
1995 set on the command line, so those are not passed on automatically.)
1996 Some old versions of @command{make} do not set this variable.  The
1997 following macro allows you to use it even with those versions.
1999 @anchor{AC_PROG_MAKE_SET}
2000 @defmac AC_PROG_MAKE_SET
2001 @acindex{PROG_MAKE_SET}
2002 @ovindex SET_MAKE
2003 If the Make command, @code{$MAKE} if set or else @samp{make}, predefines
2004 @code{$(MAKE)}, define output variable @code{SET_MAKE} to be empty.
2005 Otherwise, define @code{SET_MAKE} to a macro definition that sets
2006 @code{$(MAKE)}, such as @samp{MAKE=make}.  Calls @code{AC_SUBST} for
2007 @code{SET_MAKE}.
2008 @end defmac
2010 If you use this macro, place a line like this in each @file{Makefile.in}
2011 that runs @code{MAKE} on other directories:
2013 @example
2014 @@SET_MAKE@@
2015 @end example
2019 @node Configuration Actions
2020 @section Performing Configuration Actions
2021 @cindex Configuration actions
2023 @file{configure} is designed so that it appears to do everything itself,
2024 but there is actually a hidden slave: @file{config.status}.
2025 @file{configure} is in charge of examining your system, but it is
2026 @file{config.status} that actually takes the proper actions based on the
2027 results of @file{configure}.  The most typical task of
2028 @file{config.status} is to @emph{instantiate} files.
2030 This section describes the common behavior of the four standard
2031 instantiating macros: @code{AC_CONFIG_FILES}, @code{AC_CONFIG_HEADERS},
2032 @code{AC_CONFIG_COMMANDS} and @code{AC_CONFIG_LINKS}.  They all
2033 have this prototype:
2035 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
2036 @c awful.
2037 @example
2038 AC_CONFIG_FOOS(@var{tag}@dots{}, [@var{commands}], [@var{init-cmds}])
2039 @end example
2041 @noindent
2042 where the arguments are:
2044 @table @var
2045 @item tag@dots{}
2046 A blank-or-newline-separated list of tags, which are typically the names of
2047 the files to instantiate.
2049 You are encouraged to use literals as @var{tags}.  In particular, you
2050 should avoid
2052 @example
2053 @dots{} && my_foos="$my_foos fooo"
2054 @dots{} && my_foos="$my_foos foooo"
2055 AC_CONFIG_FOOS([$my_foos])
2056 @end example
2058 @noindent
2059 and use this instead:
2061 @example
2062 @dots{} && AC_CONFIG_FOOS([fooo])
2063 @dots{} && AC_CONFIG_FOOS([foooo])
2064 @end example
2066 The macros @code{AC_CONFIG_FILES} and @code{AC_CONFIG_HEADERS} use
2067 special @var{tag} values: they may have the form @samp{@var{output}} or
2068 @samp{@var{output}:@var{inputs}}.  The file @var{output} is instantiated
2069 from its templates, @var{inputs} (defaulting to @samp{@var{output}.in}).
2071 @samp{AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk)]},
2072 for example, asks for
2073 the creation of the file @file{Makefile} that contains the expansion of the
2074 output variables in the concatenation of @file{boiler/top.mk} and
2075 @file{boiler/bot.mk}.
2077 The special value @samp{-} might be used to denote the standard output
2078 when used in @var{output}, or the standard input when used in the
2079 @var{inputs}.  You most probably don't need to use this in
2080 @file{configure.ac}, but it is convenient when using the command line
2081 interface of @file{./config.status}, see @ref{config.status Invocation},
2082 for more details.
2084 The @var{inputs} may be absolute or relative file names.  In the latter
2085 case they are first looked for in the build tree, and then in the source
2086 tree.
2088 @item commands
2089 Shell commands output literally into @file{config.status}, and
2090 associated with a tag that the user can use to tell @file{config.status}
2091 which the commands to run.  The commands are run each time a @var{tag}
2092 request is given to @file{config.status}, typically each time the file
2093 @file{@var{tag}} is created.
2095 The variables set during the execution of @command{configure} are
2096 @emph{not} available here: you first need to set them via the
2097 @var{init-cmds}.  Nonetheless the following variables are precomputed:
2099 @table @code
2100 @item srcdir
2101 The name of the top source directory, assuming that the working
2102 directory is the top build directory.  This
2103 is what the @command{configure} option @option{--srcdir} sets.
2105 @item ac_top_srcdir
2106 The name of the top source directory, assuming that the working
2107 directory is the current build directory.
2110 @item ac_top_build_prefix
2111 The name of the top build directory, assuming that the working
2112 directory is the current build directory.
2113 It can be empty, or else ends with a slash, so that you may concatenate
2116 @item ac_srcdir
2117 The name of the corresponding source directory, assuming that the
2118 working directory is the current build directory.
2119 @end table
2121 @noindent
2122 The @dfn{current} directory refers to the directory (or
2123 pseudo-directory) containing the input part of @var{tags}.  For
2124 instance, running
2126 @example
2127 AC_CONFIG_COMMANDS([deep/dir/out:in/in.in], [@dots{}], [@dots{}])
2128 @end example
2130 @noindent
2131  with @option{--srcdir=../package} produces the following values:
2133 @example
2134 # Argument of --srcdir
2135 srcdir='../package'
2136 # Reversing deep/dir
2137 ac_top_build_prefix='../../'
2138 # Concatenation of $ac_top_build_prefix and srcdir
2139 ac_top_srcdir='../../../package'
2140 # Concatenation of $ac_top_srcdir and deep/dir
2141 ac_srcdir='../../../package/deep/dir'
2142 @end example
2144 @noindent
2145 independently of @samp{in/in.in}.
2147 @item init-cmds
2148 Shell commands output @emph{unquoted} near the beginning of
2149 @file{config.status}, and executed each time @file{config.status} runs
2150 (regardless of the tag).  Because they are unquoted, for example,
2151 @samp{$var} is output as the value of @code{var}.  @var{init-cmds}
2152 is typically used by @file{configure} to give @file{config.status} some
2153 variables it needs to run the @var{commands}.
2155 You should be extremely cautious in your variable names: all the
2156 @var{init-cmds} share the same name space and may overwrite each other
2157 in unpredictable ways.  Sorry@enddots{}
2158 @end table
2160 All these macros can be called multiple times, with different
2161 @var{tag} values, of course!
2164 @node Configuration Files
2165 @section Creating Configuration Files
2166 @cindex Creating configuration files
2167 @cindex Configuration file creation
2169 Be sure to read the previous section, @ref{Configuration Actions}.
2171 @anchor{AC_CONFIG_FILES}
2172 @defmac AC_CONFIG_FILES (@var{file}@dots{}, @ovar{cmds}, @ovar{init-cmds})
2173 @acindex{CONFIG_FILES}
2174 Make @code{AC_OUTPUT} create each @file{@var{file}} by copying an input
2175 file (by default @file{@var{file}.in}), substituting the output variable
2176 values.
2177 @c Before we used to have this feature, which was later rejected
2178 @c because it complicates the writing of makefiles:
2179 @c If the file would be unchanged, it is left untouched, to preserve
2180 @c timestamp.
2181 This macro is one of the instantiating macros; see @ref{Configuration
2182 Actions}.  @xref{Makefile Substitutions}, for more information on using
2183 output variables.  @xref{Setting Output Variables}, for more information
2184 on creating them.  This macro creates the directory that the file is in
2185 if it doesn't exist.  Usually, makefiles are created this way,
2186 but other files, such as @file{.gdbinit}, can be specified as well.
2188 Typical calls to @code{AC_CONFIG_FILES} look like this:
2190 @example
2191 AC_CONFIG_FILES([Makefile src/Makefile man/Makefile X/Imakefile])
2192 AC_CONFIG_FILES([autoconf], [chmod +x autoconf])
2193 @end example
2195 You can override an input file name by appending to @var{file} a
2196 colon-separated list of input files.  Examples:
2198 @example
2199 AC_CONFIG_FILES([Makefile:boiler/top.mk:boiler/bot.mk]
2200                 [lib/Makefile:boiler/lib.mk])
2201 @end example
2203 @noindent
2204 Doing this allows you to keep your file names acceptable to
2205 @acronym{DOS} variants, or
2206 to prepend and/or append boilerplate to the file.
2207 @end defmac
2211 @node Makefile Substitutions
2212 @section Substitutions in Makefiles
2213 @cindex Substitutions in makefiles
2214 @cindex Makefile substitutions
2216 Each subdirectory in a distribution that contains something to be
2217 compiled or installed should come with a file @file{Makefile.in}, from
2218 which @command{configure} creates a file @file{Makefile} in that directory.
2219 To create @file{Makefile}, @command{configure} performs a simple variable
2220 substitution, replacing occurrences of @samp{@@@var{variable}@@} in
2221 @file{Makefile.in} with the value that @command{configure} has determined
2222 for that variable.  Variables that are substituted into output files in
2223 this way are called @dfn{output variables}.  They are ordinary shell
2224 variables that are set in @command{configure}.  To make @command{configure}
2225 substitute a particular variable into the output files, the macro
2226 @code{AC_SUBST} must be called with that variable name as an argument.
2227 Any occurrences of @samp{@@@var{variable}@@} for other variables are
2228 left unchanged.  @xref{Setting Output Variables}, for more information
2229 on creating output variables with @code{AC_SUBST}.
2231 A software package that uses a @command{configure} script should be
2232 distributed with a file @file{Makefile.in}, but no makefile; that
2233 way, the user has to properly configure the package for the local system
2234 before compiling it.
2236 @xref{Makefile Conventions, , Makefile Conventions, standards, The
2237 @acronym{GNU} Coding Standards}, for more information on what to put in
2238 makefiles.
2240 @menu
2241 * Preset Output Variables::     Output variables that are always set
2242 * Installation Directory Variables::  Other preset output variables
2243 * Changed Directory Variables:: Warnings about @file{datarootdir}
2244 * Build Directories::           Supporting multiple concurrent compiles
2245 * Automatic Remaking::          Makefile rules for configuring
2246 @end menu
2248 @node Preset Output Variables
2249 @subsection Preset Output Variables
2250 @cindex Output variables
2252 Some output variables are preset by the Autoconf macros.  Some of the
2253 Autoconf macros set additional output variables, which are mentioned in
2254 the descriptions for those macros.  @xref{Output Variable Index}, for a
2255 complete list of output variables.  @xref{Installation Directory
2256 Variables}, for the list of the preset ones related to installation
2257 directories.  Below are listed the other preset ones.  They all are
2258 precious variables (@pxref{Setting Output Variables},
2259 @code{AC_ARG_VAR}).
2261 @c Just say no to ASCII sorting!  We're humans, not computers.
2262 @c These variables are listed as they would be in a dictionary:
2263 @c actor
2264 @c Actress
2265 @c actress
2267 @defvar CFLAGS
2268 @ovindex CFLAGS
2269 Debugging and optimization options for the C compiler.  If it is not set
2270 in the environment when @command{configure} runs, the default value is set
2271 when you call @code{AC_PROG_CC} (or empty if you don't).  @command{configure}
2272 uses this variable when compiling or linking programs to test for C features.
2274 If a compiler option affects only the behavior of the preprocessor
2275 (e.g., @option{-D @var{name}}), it should be put into @code{CPPFLAGS}
2276 instead.  If it affects only the linker (e.g., @option{-L
2277 @var{directory}}), it should be put into @code{LDFLAGS} instead.  If it
2278 affects only the compiler proper, @code{CFLAGS} is the natural home for
2279 it.  If an option affects multiple phases of the compiler, though,
2280 matters get tricky.  One approach to put such options directly into
2281 @code{CC}, e.g., @code{CC='gcc -m64'}.  Another is to put them into both
2282 @code{CPPFLAGS} and @code{LDFLAGS}, but not into @code{CFLAGS}.
2284 @end defvar
2286 @defvar configure_input
2287 @ovindex configure_input
2288 A comment saying that the file was generated automatically by
2289 @command{configure} and giving the name of the input file.
2290 @code{AC_OUTPUT} adds a comment line containing this variable to the top
2291 of every makefile it creates.  For other files, you should
2292 reference this variable in a comment at the top of each input file.  For
2293 example, an input shell script should begin like this:
2295 @example
2296 #!/bin/sh
2297 # @@configure_input@@
2298 @end example
2300 @noindent
2301 The presence of that line also reminds people editing the file that it
2302 needs to be processed by @command{configure} in order to be used.
2303 @end defvar
2305 @defvar CPPFLAGS
2306 @ovindex CPPFLAGS
2307 Preprocessor options for the C, C++, and Objective C preprocessors and
2308 compilers.  If
2309 it is not set in the environment when @command{configure} runs, the default
2310 value is empty.  @command{configure} uses this variable when preprocessing
2311 or compiling programs to test for C, C++, and Objective C features.
2313 This variable's contents should contain options like @option{-I},
2314 @option{-D}, and @option{-U} that affect only the behavior of the
2315 preprocessor.  Please see the explanation of @code{CFLAGS} for what you
2316 can do if an option affects other phases of the compiler as well.
2318 Currently, @command{configure} always links as part of a single
2319 invocation of the compiler that also preprocesses and compiles, so it
2320 uses this variable also when linking programs.  However, it is unwise to
2321 depend on this behavior because the @acronym{GNU} coding standards do
2322 not require it and many packages do not use @code{CPPFLAGS} when linking
2323 programs.
2325 @xref{Special Chars in Variables}, for limitations that @code{CPPFLAGS}
2326 might run into.
2327 @end defvar
2329 @defvar CXXFLAGS
2330 @ovindex CXXFLAGS
2331 Debugging and optimization options for the C++ compiler.  It acts like
2332 @code{CFLAGS}, but for C++ instead of C.
2333 @end defvar
2335 @defvar DEFS
2336 @ovindex DEFS
2337 @option{-D} options to pass to the C compiler.  If @code{AC_CONFIG_HEADERS}
2338 is called, @command{configure} replaces @samp{@@DEFS@@} with
2339 @option{-DHAVE_CONFIG_H} instead (@pxref{Configuration Headers}).  This
2340 variable is not defined while @command{configure} is performing its tests,
2341 only when creating the output files.  @xref{Setting Output Variables}, for
2342 how to check the results of previous tests.
2343 @end defvar
2345 @defvar ECHO_C
2346 @defvarx ECHO_N
2347 @defvarx ECHO_T
2348 @ovindex ECHO_C
2349 @ovindex ECHO_N
2350 @ovindex ECHO_T
2351 How does one suppress the trailing newline from @command{echo} for
2352 question-answer message pairs?  These variables provide a way:
2354 @example
2355 echo $ECHO_N "And the winner is... $ECHO_C"
2356 sleep 100000000000
2357 echo "$@{ECHO_T@}dead."
2358 @end example
2360 @noindent
2361 Some old and uncommon @command{echo} implementations offer no means to
2362 achieve this, in which case @code{ECHO_T} is set to tab.  You might not
2363 want to use it.
2364 @end defvar
2366 @defvar ERLCFLAGS
2367 @ovindex ERLCFLAGS
2368 Debugging and optimization options for the Erlang compiler.  If it is not set
2369 in the environment when @command{configure} runs, the default value is empty.
2370 @command{configure} uses this variable when compiling
2371 programs to test for Erlang features.
2372 @end defvar
2374 @defvar FCFLAGS
2375 @ovindex FCFLAGS
2376 Debugging and optimization options for the Fortran compiler.  If it
2377 is not set in the environment when @command{configure} runs, the default
2378 value is set when you call @code{AC_PROG_FC} (or empty if you don't).
2379 @command{configure} uses this variable when compiling or linking
2380 programs to test for Fortran features.
2381 @end defvar
2383 @defvar FFLAGS
2384 @ovindex FFLAGS
2385 Debugging and optimization options for the Fortran 77 compiler.  If it
2386 is not set in the environment when @command{configure} runs, the default
2387 value is set when you call @code{AC_PROG_F77} (or empty if you don't).
2388 @command{configure} uses this variable when compiling or linking
2389 programs to test for Fortran 77 features.
2390 @end defvar
2392 @defvar LDFLAGS
2393 @ovindex LDFLAGS
2394 Options for the linker.  If it is not set
2395 in the environment when @command{configure} runs, the default value is empty.
2396 @command{configure} uses this variable when linking programs to test for
2397 C, C++, Objective C, and Fortran features.
2399 This variable's contents should contain options like @option{-s} and
2400 @option{-L} that affect only the behavior of the linker.  Please see the
2401 explanation of @code{CFLAGS} for what you can do if an option also
2402 affects other phases of the compiler.
2404 Don't use this variable to pass library names
2405 (@option{-l}) to the linker; use @code{LIBS} instead.
2406 @end defvar
2408 @defvar LIBS
2409 @ovindex LIBS
2410 @option{-l} options to pass to the linker.  The default value is empty,
2411 but some Autoconf macros may prepend extra libraries to this variable if
2412 those libraries are found and provide necessary functions, see
2413 @ref{Libraries}.  @command{configure} uses this variable when linking
2414 programs to test for C, C++, and Fortran features.
2415 @end defvar
2417 @defvar OBJCFLAGS
2418 @ovindex OBJCFLAGS
2419 Debugging and optimization options for the Objective C compiler.  It
2420 acts like @code{CFLAGS}, but for Objective C instead of C.
2421 @end defvar
2423 @defvar builddir
2424 @ovindex builddir
2425 Rigorously equal to @samp{.}.  Added for symmetry only.
2426 @end defvar
2428 @defvar abs_builddir
2429 @ovindex abs_builddir
2430 Absolute name of @code{builddir}.
2431 @end defvar
2433 @defvar top_builddir
2434 @ovindex top_builddir
2435 The relative name of the top level of the current build tree.  In the
2436 top-level directory, this is the same as @code{builddir}.
2437 @end defvar
2439 @defvar top_build_prefix
2440 @ovindex top_build_prefix
2441 The relative name of the top level of the current build tree with final
2442 slash if nonemtpy.  This is the same as @code{top_builddir}, except that
2443 it contains of zero of more runs of @code{../}, so it should not be
2444 appended with a slash for concatenation.  This helps for @command{make}
2445 implementations that otherwise do not treat @file{./file} and @file{file}
2446 as equal in the toplevel build directory.
2447 @end defvar
2449 @defvar abs_top_builddir
2450 @ovindex abs_top_builddir
2451 Absolute name of @code{top_builddir}.
2452 @end defvar
2454 @defvar srcdir
2455 @ovindex srcdir
2456 The name of the directory that contains the source code for
2457 that makefile.
2458 @end defvar
2460 @defvar abs_srcdir
2461 @ovindex abs_srcdir
2462 Absolute name of @code{srcdir}.
2463 @end defvar
2465 @defvar top_srcdir
2466 @ovindex top_srcdir
2467 The name of the top-level source code directory for the
2468 package.  In the top-level directory, this is the same as @code{srcdir}.
2469 @end defvar
2471 @defvar abs_top_srcdir
2472 @ovindex abs_top_srcdir
2473 Absolute name of @code{top_srcdir}.
2474 @end defvar
2476 @node Installation Directory Variables
2477 @subsection Installation Directory Variables
2478 @cindex Installation directories
2479 @cindex Directories, installation
2481 The following variables specify the directories for
2482 package installation, see @ref{Directory Variables, , Variables for
2483 Installation Directories, standards, The @acronym{GNU} Coding
2484 Standards}, for more information.  Each variable corresponds to an
2485 argument of @command{configure}; trailing slashes are stripped so that
2486 expressions such as @samp{$@{prefix@}/lib} expand with only one slash
2487 between directory names.  See the end of this section for
2488 details on when and how to use these variables.
2490 @defvar bindir
2491 @ovindex bindir
2492 The directory for installing executables that users run.
2493 @end defvar
2495 @defvar datadir
2496 @ovindex datadir
2497 The directory for installing idiosyncratic read-only
2498 architecture-independent data.
2499 @end defvar
2501 @defvar datarootdir
2502 @ovindex datarootdir
2503 The root of the directory tree for read-only architecture-independent
2504 data files.
2505 @end defvar
2507 @defvar docdir
2508 @ovindex docdir
2509 The directory for installing documentation files (other than Info and
2510 man).
2511 @end defvar
2513 @defvar dvidir
2514 @ovindex dvidir
2515 The directory for installing documentation files in DVI format.
2516 @end defvar
2518 @defvar exec_prefix
2519 @ovindex exec_prefix
2520 The installation prefix for architecture-dependent files.  By default
2521 it's the same as @var{prefix}.  You should avoid installing anything
2522 directly to @var{exec_prefix}.  However, the default value for
2523 directories containing architecture-dependent files should be relative
2524 to @var{exec_prefix}.
2525 @end defvar
2527 @defvar htmldir
2528 @ovindex htmldir
2529 The directory for installing HTML documentation.
2530 @end defvar
2532 @defvar includedir
2533 @ovindex includedir
2534 The directory for installing C header files.
2535 @end defvar
2537 @defvar infodir
2538 @ovindex infodir
2539 The directory for installing documentation in Info format.
2540 @end defvar
2542 @defvar libdir
2543 @ovindex libdir
2544 The directory for installing object code libraries.
2545 @end defvar
2547 @defvar libexecdir
2548 @ovindex libexecdir
2549 The directory for installing executables that other programs run.
2550 @end defvar
2552 @defvar localedir
2553 @ovindex localedir
2554 The directory for installing locale-dependent but
2555 architecture-independent data, such as message catalogs.  This directory
2556 usually has a subdirectory per locale.
2557 @end defvar
2559 @defvar localstatedir
2560 @ovindex localstatedir
2561 The directory for installing modifiable single-machine data.
2562 @end defvar
2564 @defvar mandir
2565 @ovindex mandir
2566 The top-level directory for installing documentation in man format.
2567 @end defvar
2569 @defvar oldincludedir
2570 @ovindex oldincludedir
2571 The directory for installing C header files for non-@acronym{GCC} compilers.
2572 @end defvar
2574 @defvar pdfdir
2575 @ovindex pdfdir
2576 The directory for installing PDF documentation.
2577 @end defvar
2579 @defvar prefix
2580 @ovindex prefix
2581 The common installation prefix for all files.  If @var{exec_prefix}
2582 is defined to a different value, @var{prefix} is used only for
2583 architecture-independent files.
2584 @end defvar
2586 @defvar psdir
2587 @ovindex psdir
2588 The directory for installing PostScript documentation.
2589 @end defvar
2591 @defvar sbindir
2592 @ovindex sbindir
2593 The directory for installing executables that system
2594 administrators run.
2595 @end defvar
2597 @defvar sharedstatedir
2598 @ovindex sharedstatedir
2599 The directory for installing modifiable architecture-independent data.
2600 @end defvar
2602 @defvar sysconfdir
2603 @ovindex sysconfdir
2604 The directory for installing read-only single-machine data.
2605 @end defvar
2608 Most of these variables have values that rely on @code{prefix} or
2609 @code{exec_prefix}.  It is deliberate that the directory output
2610 variables keep them unexpanded: typically @samp{@@datarootdir@@} is
2611 replaced by @samp{$@{prefix@}/share}, not @samp{/usr/local/share}, and
2612 @samp{@@datadir@@} is replaced by @samp{$@{datarootdir@}}.
2614 This behavior is mandated by the @acronym{GNU} coding standards, so that when
2615 the user runs:
2617 @table @samp
2618 @item make
2619 she can still specify a different prefix from the one specified to
2620 @command{configure}, in which case, if needed, the package should hard
2621 code dependencies corresponding to the make-specified prefix.
2623 @item make install
2624 she can specify a different installation location, in which case the
2625 package @emph{must} still depend on the location which was compiled in
2626 (i.e., never recompile when @samp{make install} is run).  This is an
2627 extremely important feature, as many people may decide to install all
2628 the files of a package grouped together, and then install links from
2629 the final locations to there.
2630 @end table
2632 In order to support these features, it is essential that
2633 @code{datarootdir} remains being defined as @samp{$@{prefix@}/share} to
2634 depend upon the current value of @code{prefix}.
2636 A corollary is that you should not use these variables except in
2637 makefiles.  For instance, instead of trying to evaluate @code{datadir}
2638 in @file{configure} and hard-coding it in makefiles using
2639 e.g., @samp{AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])},
2640 you should add
2641 @option{-DDATADIR='$(datadir)'} to your makefile's definition of
2642 @code{CPPFLAGS} (@code{AM_CPPFLAGS} if you are also using Automake).
2644 Similarly, you should not rely on @code{AC_CONFIG_FILES} to replace
2645 @code{datadir} and friends in your shell scripts and other files; instead,
2646 let @command{make} manage their replacement.  For instance Autoconf
2647 ships templates of its shell scripts ending with @samp{.in}, and uses a
2648 makefile snippet similar to the following to build scripts like
2649 @command{autoheader} and @command{autom4te}:
2651 @example
2652 @group
2653 edit = sed \
2654         -e 's|@@datadir[@@]|$(pkgdatadir)|g' \
2655         -e 's|@@prefix[@@]|$(prefix)|g'
2656 @end group
2658 @group
2659 autoheader autom4te: Makefile
2660         rm -f $@@ $@@.tmp
2661         $(edit) '$(srcdir)/$@@.in' >$@@.tmp
2662         chmod +x $@@.tmp
2663         chmod a-w $@@.tmp
2664         mv $@@.tmp $@@
2665 @end group
2667 @group
2668 autoheader: $(srcdir)/autoheader.in
2669 autom4te: $(srcdir)/autom4te.in
2670 @end group
2671 @end example
2673 Some details are noteworthy:
2675 @table @asis
2676 @item @samp{@@datadir[@@]}
2677 The brackets prevent @command{configure} from replacing
2678 @samp{@@datadir@@} in the Sed expression itself.
2679 Brackets are preferable to a backslash here, since
2680 Posix says @samp{\@@} is not portable.
2682 @item @samp{$(pkgdatadir)}
2683 Don't use @samp{@@pkgdatadir@@}!  Use the matching makefile variable
2684 instead.
2686 @item @samp{/}
2687 Don't use @samp{/} in the Sed expressions that replace file names since
2688 most likely the
2689 variables you use, such as @samp{$(pkgdatadir)}, contain @samp{/}.
2690 Use a shell metacharacter instead, such as @samp{|}.
2692 @item special characters
2693 File names, file name components, and the value of @code{VPATH} should
2694 not contain shell metacharacters or white
2695 space.  @xref{Special Chars in Variables}.
2697 @item dependency on @file{Makefile}
2698 Since @code{edit} uses values that depend on the configuration specific
2699 values (@code{prefix}, etc.)@: and not only on @code{VERSION} and so forth,
2700 the output depends on @file{Makefile}, not @file{configure.ac}.
2702 @item @samp{$@@}
2703 The main rule is generic, and uses @samp{$@@} extensively to
2704 avoid the need for multiple copies of the rule.
2706 @item Separated dependencies and single suffix rules
2707 You can't use them!  The above snippet cannot be (portably) rewritten
2710 @example
2711 autoconf autoheader: Makefile
2712 @group
2713 .in:
2714         rm -f $@@ $@@.tmp
2715         $(edit) $< >$@@.tmp
2716         chmod +x $@@.tmp
2717         mv $@@.tmp $@@
2718 @end group
2719 @end example
2721 @xref{Single Suffix Rules}, for details.
2723 @item @samp{$(srcdir)}
2724 Be sure to specify the name of the source directory,
2725 otherwise the package won't support separated builds.
2726 @end table
2728 For the more specific installation of Erlang libraries, the following variables
2729 are defined:
2731 @defvar ERLANG_INSTALL_LIB_DIR
2732 @ovindex ERLANG_INSTALL_LIB_DIR
2733 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
2734 The common parent directory of Erlang library installation directories.
2735 This variable is set by calling the @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR}
2736 macro in @file{configure.ac}.
2737 @end defvar
2739 @defvar ERLANG_INSTALL_LIB_DIR_@var{library}
2740 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
2741 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
2742 The installation directory for Erlang library @var{library}.
2743 This variable is set by calling the
2744 @samp{AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR(@var{library}, @var{version}}
2745 macro in @file{configure.ac}.
2746 @end defvar
2748 @xref{Erlang Libraries}, for details.
2751 @node Changed Directory Variables
2752 @subsection Changed Directory Variables
2753 @cindex @file{datarootdir}
2755 In Autoconf 2.60, the set of directory variables has changed, and the
2756 defaults of some variables have been adjusted
2757 (@pxref{Installation Directory Variables}) to changes in the
2758 @acronym{GNU} Coding Standards.  Notably, @file{datadir}, @file{infodir}, and
2759 @file{mandir} are now expressed in terms of @file{datarootdir}.  If you are
2760 upgrading from an earlier Autoconf version, you may need to adjust your files
2761 to ensure that the directory variables are substituted correctly
2762 (@pxref{Defining Directories}), and that a definition of @file{datarootdir} is
2763 in place.  For example, in a @file{Makefile.in}, adding
2765 @example
2766 datarootdir = @@datarootdir@@
2767 @end example
2769 @noindent
2770 is usually sufficient.  If you use Automake to create @file{Makefile.in},
2771 it will add this for you.
2773 To help with the transition, Autoconf warns about files that seem to use
2774 @code{datarootdir} without defining it.  In some cases, it then expands
2775 the value of @code{$datarootdir} in substitutions of the directory
2776 variables.  The following example shows such a warning:
2778 @example
2779 $ @kbd{cat configure.ac}
2780 AC_INIT
2781 AC_CONFIG_FILES([Makefile])
2782 AC_OUTPUT
2783 $ @kbd{cat Makefile.in}
2784 prefix = @@prefix@@
2785 datadir = @@datadir@@
2786 $ @kbd{autoconf}
2787 $ @kbd{configure}
2788 configure: creating ./config.status
2789 config.status: creating Makefile
2790 config.status: WARNING:
2791                Makefile.in seems to ignore the --datarootdir setting
2792 $ @kbd{cat Makefile}
2793 prefix = /usr/local
2794 datadir = $@{prefix@}/share
2795 @end example
2797 Usually one can easily change the file to accommodate both older and newer
2798 Autoconf releases:
2800 @example
2801 $ @kbd{cat Makefile.in}
2802 prefix = @@prefix@@
2803 datarootdir = @@datarootdir@@
2804 datadir = @@datadir@@
2805 $ @kbd{configure}
2806 configure: creating ./config.status
2807 config.status: creating Makefile
2808 $ @kbd{cat Makefile}
2809 prefix = /usr/local
2810 datarootdir = $@{prefix@}/share
2811 datadir = $@{datarootdir@}
2812 @end example
2814 @acindex{DATAROOTDIR_CHECKED}
2815 In some cases, however, the checks may not be able to detect that a suitable
2816 initialization of @code{datarootdir} is in place, or they may fail to detect
2817 that such an initialization is necessary in the output file.  If, after
2818 auditing your package, there are still spurious @file{configure} warnings about
2819 @code{datarootdir}, you may add the line
2821 @example
2822 AC_DEFUN([AC_DATAROOTDIR_CHECKED])
2823 @end example
2825 @noindent
2826 to your @file{configure.ac} to disable the warnings.  This is an exception
2827 to the usual rule that you should not define a macro whose name begins with
2828 @code{AC_} (@pxref{Macro Names}).
2832 @node Build Directories
2833 @subsection Build Directories
2834 @cindex Build directories
2835 @cindex Directories, build
2837 You can support compiling a software package for several architectures
2838 simultaneously from the same copy of the source code.  The object files
2839 for each architecture are kept in their own directory.
2841 To support doing this, @command{make} uses the @code{VPATH} variable to
2842 find the files that are in the source directory.  @acronym{GNU} Make
2843 can do this.  Most other recent @command{make} programs can do this as
2844 well, though they may have difficulties and it is often simpler to
2845 recommend @acronym{GNU} @command{make} (@pxref{VPATH and Make}).  Older
2846 @command{make} programs do not support @code{VPATH}; when using them, the
2847 source code must be in the same directory as the object files.
2849 To support @code{VPATH}, each @file{Makefile.in} should contain two
2850 lines that look like:
2852 @example
2853 srcdir = @@srcdir@@
2854 VPATH = @@srcdir@@
2855 @end example
2857 Do not set @code{VPATH} to the value of another variable, for example
2858 @samp{VPATH = $(srcdir)}, because some versions of @command{make} do not do
2859 variable substitutions on the value of @code{VPATH}.
2861 @command{configure} substitutes the correct value for @code{srcdir} when
2862 it produces @file{Makefile}.
2864 Do not use the @code{make} variable @code{$<}, which expands to the
2865 file name of the file in the source directory (found with @code{VPATH}),
2866 except in implicit rules.  (An implicit rule is one such as @samp{.c.o},
2867 which tells how to create a @file{.o} file from a @file{.c} file.)  Some
2868 versions of @command{make} do not set @code{$<} in explicit rules; they
2869 expand it to an empty value.
2871 Instead, Make command lines should always refer to source
2872 files by prefixing them with @samp{$(srcdir)/}.  For example:
2874 @example
2875 time.info: time.texinfo
2876         $(MAKEINFO) '$(srcdir)/time.texinfo'
2877 @end example
2879 @node Automatic Remaking
2880 @subsection Automatic Remaking
2881 @cindex Automatic remaking
2882 @cindex Remaking automatically
2884 You can put rules like the following in the top-level @file{Makefile.in}
2885 for a package to automatically update the configuration information when
2886 you change the configuration files.  This example includes all of the
2887 optional files, such as @file{aclocal.m4} and those related to
2888 configuration header files.  Omit from the @file{Makefile.in} rules for
2889 any of these files that your package does not use.
2891 The @samp{$(srcdir)/} prefix is included because of limitations in the
2892 @code{VPATH} mechanism.
2894 The @file{stamp-} files are necessary because the timestamps of
2895 @file{config.h.in} and @file{config.h} are not changed if remaking
2896 them does not change their contents.  This feature avoids unnecessary
2897 recompilation.  You should include the file @file{stamp-h.in} your
2898 package's distribution, so that @command{make} considers
2899 @file{config.h.in} up to date.  Don't use @command{touch}
2900 (@pxref{Limitations of Usual Tools}); instead, use @command{echo} (using
2901 @command{date} would cause needless differences, hence @acronym{CVS}
2902 conflicts, etc.).
2904 @example
2905 @group
2906 $(srcdir)/configure: configure.ac aclocal.m4
2907         cd '$(srcdir)' && autoconf
2909 # autoheader might not change config.h.in, so touch a stamp file.
2910 $(srcdir)/config.h.in: stamp-h.in
2911 $(srcdir)/stamp-h.in: configure.ac aclocal.m4
2912         cd '$(srcdir)' && autoheader
2913         echo timestamp > '$(srcdir)/stamp-h.in'
2915 config.h: stamp-h
2916 stamp-h: config.h.in config.status
2917         ./config.status
2919 Makefile: Makefile.in config.status
2920         ./config.status
2922 config.status: configure
2923         ./config.status --recheck
2924 @end group
2925 @end example
2927 @noindent
2928 (Be careful if you copy these lines directly into your makefile, as you
2929 need to convert the indented lines to start with the tab character.)
2931 In addition, you should use
2933 @example
2934 AC_CONFIG_FILES([stamp-h], [echo timestamp > stamp-h])
2935 @end example
2937 @noindent
2938 so @file{config.status} ensures that @file{config.h} is considered up to
2939 date.  @xref{Output}, for more information about @code{AC_OUTPUT}.
2941 @xref{config.status Invocation}, for more examples of handling
2942 configuration-related dependencies.
2944 @node Configuration Headers
2945 @section Configuration Header Files
2946 @cindex Configuration Header
2947 @cindex @file{config.h}
2949 When a package contains more than a few tests that define C preprocessor
2950 symbols, the command lines to pass @option{-D} options to the compiler
2951 can get quite long.  This causes two problems.  One is that the
2952 @command{make} output is hard to visually scan for errors.  More
2953 seriously, the command lines can exceed the length limits of some
2954 operating systems.  As an alternative to passing @option{-D} options to
2955 the compiler, @command{configure} scripts can create a C header file
2956 containing @samp{#define} directives.  The @code{AC_CONFIG_HEADERS}
2957 macro selects this kind of output.  Though it can be called anywhere
2958 between @code{AC_INIT} and @code{AC_OUTPUT}, it is customary to call
2959 it right after @code{AC_INIT}.
2961 The package should @samp{#include} the configuration header file before
2962 any other header files, to prevent inconsistencies in declarations (for
2963 example, if it redefines @code{const}).
2965 To provide for VPATH builds, remember to pass the C compiler a @option{-I.}
2966 option (or @option{-I..}; whichever directory contains @file{config.h}).
2967 Even if you use @samp{#include "config.h"}, the preprocessor searches only
2968 the directory of the currently read file, i.e., the source directory, not
2969 the build directory.
2971 With the appropriate @option{-I} option, you can use
2972 @samp{#include <config.h>}.  Actually, it's a good habit to use it,
2973 because in the rare case when the source directory contains another
2974 @file{config.h}, the build directory should be searched first.
2977 @defmac AC_CONFIG_HEADERS (@var{header} @dots{}, @ovar{cmds}, @ovar{init-cmds})
2978 @acindex{CONFIG_HEADERS}
2979 @cvindex HAVE_CONFIG_H
2980 This macro is one of the instantiating macros; see @ref{Configuration
2981 Actions}.  Make @code{AC_OUTPUT} create the file(s) in the
2982 blank-or-newline-separated list @var{header} containing C preprocessor
2983 @code{#define} statements, and replace @samp{@@DEFS@@} in generated
2984 files with @option{-DHAVE_CONFIG_H} instead of the value of @code{DEFS}.
2985 The usual name for @var{header} is @file{config.h}.
2987 If @var{header} already exists and its contents are identical to what
2988 @code{AC_OUTPUT} would put in it, it is left alone.  Doing this allows
2989 making some changes in the configuration without needlessly causing
2990 object files that depend on the header file to be recompiled.
2992 Usually the input file is named @file{@var{header}.in}; however, you can
2993 override the input file name by appending to @var{header} a
2994 colon-separated list of input files.  For example, you might need to make
2995 the input file name acceptable to @acronym{DOS} variants:
2997 @example
2998 AC_CONFIG_HEADERS([config.h:config.hin])
2999 @end example
3001 @end defmac
3003 @defmac AH_HEADER
3004 @ahindex{HEADER}
3005 This macro is defined as the name of the first declared config header
3006 and undefined if no config headers have been declared up to this point.
3007 A third-party macro may, for example, require use of a config header
3008 without invoking AC_CONFIG_HEADERS twice, like this:
3010 @example
3011 AC_CONFIG_COMMANDS_PRE(
3012         [m4_ifndef([AH_HEADER], [AC_CONFIG_HEADERS([config.h])])])
3013 @end example
3015 @end defmac
3017 @xref{Configuration Actions}, for more details on @var{header}.
3019 @menu
3020 * Header Templates::            Input for the configuration headers
3021 * autoheader Invocation::       How to create configuration templates
3022 * Autoheader Macros::           How to specify CPP templates
3023 @end menu
3025 @node Header Templates
3026 @subsection Configuration Header Templates
3027 @cindex Configuration Header Template
3028 @cindex Header templates
3029 @cindex @file{config.h.in}
3031 Your distribution should contain a template file that looks as you want
3032 the final header file to look, including comments, with @code{#undef}
3033 statements which are used as hooks.  For example, suppose your
3034 @file{configure.ac} makes these calls:
3036 @example
3037 AC_CONFIG_HEADERS([conf.h])
3038 AC_CHECK_HEADERS([unistd.h])
3039 @end example
3041 @noindent
3042 Then you could have code like the following in @file{conf.h.in}.  On
3043 systems that have @file{unistd.h}, @command{configure} defines
3044 @samp{HAVE_UNISTD_H} to 1.  On other systems, the whole line is
3045 commented out (in case the system predefines that symbol).
3047 @example
3048 @group
3049 /* Define as 1 if you have unistd.h.  */
3050 #undef HAVE_UNISTD_H
3051 @end group
3052 @end example
3054 Pay attention that @samp{#undef} is in the first column, and there is
3055 nothing after @samp{HAVE_UNISTD_H}, not even white space.  You can
3056 then decode the configuration header using the preprocessor directives:
3058 @example
3059 @group
3060 #include <conf.h>
3062 #ifdef HAVE_UNISTD_H
3063 # include <unistd.h>
3064 #else
3065 /* We are in trouble.  */
3066 #endif
3067 @end group
3068 @end example
3070 The use of old form templates, with @samp{#define} instead of
3071 @samp{#undef} is strongly discouraged.  Similarly with old templates
3072 with comments on the same line as the @samp{#undef}.  Anyway, putting
3073 comments in preprocessor macros has never been a good idea.
3075 Since it is a tedious task to keep a template header up to date, you may
3076 use @command{autoheader} to generate it, see @ref{autoheader Invocation}.
3079 @node autoheader Invocation
3080 @subsection Using @command{autoheader} to Create @file{config.h.in}
3081 @cindex @command{autoheader}
3083 The @command{autoheader} program can create a template file of C
3084 @samp{#define} statements for @command{configure} to use.
3085 It searches for the first invocation of @code{AC_CONFIG_HEADERS} in
3086 @file{configure} sources to determine the name of the template.
3087 (If the first call of @code{AC_CONFIG_HEADERS} specifies more than one
3088 input file name, @command{autoheader} uses the first one.)
3090 It is recommended that only one input file is used.  If you want to append
3091 a boilerplate code, it is preferable to use
3092 @samp{AH_BOTTOM([#include <conf_post.h>])}.
3093 File @file{conf_post.h} is not processed during the configuration then,
3094 which make things clearer.  Analogically, @code{AH_TOP} can be used to
3095 prepend a boilerplate code.
3097 In order to do its job, @command{autoheader} needs you to document all
3098 of the symbols that you might use.  Typically this is done via an
3099 @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED} call whose first argument
3100 is a literal symbol and whose third argument describes the symbol
3101 (@pxref{Defining Symbols}).  Alternatively, you can use
3102 @code{AH_TEMPLATE} (@pxref{Autoheader Macros}), or you can supply a
3103 suitable input file for a subsequent configuration header file.
3104 Symbols defined by Autoconf's builtin tests are already documented properly;
3105 you need to document only those that you
3106 define yourself.
3108 You might wonder why @command{autoheader} is needed: after all, why
3109 would @command{configure} need to ``patch'' a @file{config.h.in} to
3110 produce a @file{config.h} instead of just creating @file{config.h} from
3111 scratch?  Well, when everything rocks, the answer is just that we are
3112 wasting our time maintaining @command{autoheader}: generating
3113 @file{config.h} directly is all that is needed.  When things go wrong,
3114 however, you'll be thankful for the existence of @command{autoheader}.
3116 The fact that the symbols are documented is important in order to
3117 @emph{check} that @file{config.h} makes sense.  The fact that there is a
3118 well-defined list of symbols that should be defined (or not) is
3119 also important for people who are porting packages to environments where
3120 @command{configure} cannot be run: they just have to @emph{fill in the
3121 blanks}.
3123 But let's come back to the point: the invocation of @command{autoheader}@dots{}
3125 If you give @command{autoheader} an argument, it uses that file instead
3126 of @file{configure.ac} and writes the header file to the standard output
3127 instead of to @file{config.h.in}.  If you give @command{autoheader} an
3128 argument of @option{-}, it reads the standard input instead of
3129 @file{configure.ac} and writes the header file to the standard output.
3131 @command{autoheader} accepts the following options:
3133 @table @option
3134 @item --help
3135 @itemx -h
3136 Print a summary of the command line options and exit.
3138 @item --version
3139 @itemx -V
3140 Print the version number of Autoconf and exit.
3142 @item --verbose
3143 @itemx -v
3144 Report processing steps.
3146 @item --debug
3147 @itemx -d
3148 Don't remove the temporary files.
3150 @item --force
3151 @itemx -f
3152 Remake the template file even if newer than its input files.
3154 @item --include=@var{dir}
3155 @itemx -I @var{dir}
3156 Append @var{dir} to the include path.  Multiple invocations accumulate.
3158 @item --prepend-include=@var{dir}
3159 @item -B @var{dir}
3160 Prepend @var{dir} to the include path.  Multiple invocations accumulate.
3162 @item --warnings=@var{category}
3163 @itemx -W @var{category}
3164 @evindex WARNINGS
3165 Report the warnings related to @var{category} (which can actually be a
3166 comma separated list).  Current categories include:
3168 @table @samp
3169 @item obsolete
3170 report the uses of obsolete constructs
3172 @item all
3173 report all the warnings
3175 @item none
3176 report none
3178 @item error
3179 treats warnings as errors
3181 @item no-@var{category}
3182 disable warnings falling into @var{category}
3183 @end table
3185 @end table
3189 @node Autoheader Macros
3190 @subsection Autoheader Macros
3191 @cindex Autoheader macros
3193 @command{autoheader} scans @file{configure.ac} and figures out which C
3194 preprocessor symbols it might define.  It knows how to generate
3195 templates for symbols defined by @code{AC_CHECK_HEADERS},
3196 @code{AC_CHECK_FUNCS} etc., but if you @code{AC_DEFINE} any additional
3197 symbol, you must define a template for it.  If there are missing
3198 templates, @command{autoheader} fails with an error message.
3200 The template for a @var{symbol} is created
3201 by @command{autoheader} from
3202 the @var{description} argument to an @code{AC_DEFINE};
3203 see @ref{Defining Symbols}.
3205 For special needs, you can use the following macros.
3208 @defmac AH_TEMPLATE (@var{key}, @var{description})
3209 @ahindex{TEMPLATE}
3210 Tell @command{autoheader} to generate a template for @var{key}.  This macro
3211 generates standard templates just like @code{AC_DEFINE} when a
3212 @var{description} is given.
3214 For example:
3216 @example
3217 AH_TEMPLATE([CRAY_STACKSEG_END],
3218             [Define to one of _getb67, GETB67, getb67
3219              for Cray-2 and Cray-YMP systems.  This
3220              function is required for alloca.c support
3221              on those systems.])
3222 @end example
3224 @noindent
3225 generates the following template, with the description properly
3226 justified.
3228 @example
3229 /* Define to one of _getb67, GETB67, getb67 for Cray-2 and
3230    Cray-YMP systems.  This function is required for alloca.c
3231    support on those systems.  */
3232 #undef CRAY_STACKSEG_END
3233 @end example
3234 @end defmac
3237 @defmac AH_VERBATIM (@var{key}, @var{template})
3238 @ahindex{VERBATIM}
3239 Tell @command{autoheader} to include the @var{template} as-is in the header
3240 template file.  This @var{template} is associated with the @var{key},
3241 which is used to sort all the different templates and guarantee their
3242 uniqueness.  It should be a symbol that can be defined via @code{AC_DEFINE}.
3243 @end defmac
3246 @defmac AH_TOP (@var{text})
3247 @ahindex{TOP}
3248 Include @var{text} at the top of the header template file.
3249 @end defmac
3252 @defmac AH_BOTTOM (@var{text})
3253 @ahindex{BOTTOM}
3254 Include @var{text} at the bottom of the header template file.
3255 @end defmac
3258 Please note that @var{text} gets included ``verbatim'' to the template file,
3259 not to the resulting config header, so it can easily get mangled when the
3260 template is processed.  There is rarely a need for something other than
3262 @example
3263 AH_BOTTOM([#include <custom.h>])
3264 @end example
3268 @node Configuration Commands
3269 @section Running Arbitrary Configuration Commands
3270 @cindex Configuration commands
3271 @cindex Commands for configuration
3273 You can execute arbitrary commands before, during, and after
3274 @file{config.status} is run.  The three following macros accumulate the
3275 commands to run when they are called multiple times.
3276 @code{AC_CONFIG_COMMANDS} replaces the obsolete macro
3277 @code{AC_OUTPUT_COMMANDS}; see @ref{Obsolete Macros}, for details.
3279 @anchor{AC_CONFIG_COMMANDS}
3280 @defmac AC_CONFIG_COMMANDS (@var{tag}@dots{}, @ovar{cmds}, @ovar{init-cmds})
3281 @acindex{CONFIG_COMMANDS}
3282 Specify additional shell commands to run at the end of
3283 @file{config.status}, and shell commands to initialize any variables
3284 from @command{configure}.  Associate the commands with @var{tag}.
3285 Since typically the @var{cmds} create a file, @var{tag} should
3286 naturally be the name of that file.  If needed, the directory hosting
3287 @var{tag} is created.  This macro is one of the instantiating macros;
3288 see @ref{Configuration Actions}.
3290 Here is an unrealistic example:
3291 @example
3292 fubar=42
3293 AC_CONFIG_COMMANDS([fubar],
3294                    [echo this is extra $fubar, and so on.],
3295                    [fubar=$fubar])
3296 @end example
3298 Here is a better one:
3299 @example
3300 AC_CONFIG_COMMANDS([timestamp], [date >timestamp])
3301 @end example
3302 @end defmac
3304 The following two macros look similar, but in fact they are not of the same
3305 breed: they are executed directly by @file{configure}, so you cannot use
3306 @file{config.status} to rerun them.
3308 @c Yet it is good to leave them here.  The user sees them together and
3309 @c decides which best fits their needs.
3311 @defmac AC_CONFIG_COMMANDS_PRE (@var{cmds})
3312 @acindex{CONFIG_COMMANDS_PRE}
3313 Execute the @var{cmds} right before creating @file{config.status}.
3315 This macro presents the last opportunity to call @code{AC_SUBST},
3316 @code{AC_DEFINE}, or @code{AC_CONFIG_FOOS} macros.
3317 @end defmac
3319 @defmac AC_CONFIG_COMMANDS_POST (@var{cmds})
3320 @acindex{CONFIG_COMMANDS_POST}
3321 Execute the @var{cmds} right after creating @file{config.status}.
3322 @end defmac
3327 @node Configuration Links
3328 @section Creating Configuration Links
3329 @cindex Configuration links
3330 @cindex Links for configuration
3332 You may find it convenient to create links whose destinations depend upon
3333 results of tests.  One can use @code{AC_CONFIG_COMMANDS} but the
3334 creation of relative symbolic links can be delicate when the package is
3335 built in a directory different from the source directory.
3337 @anchor{AC_CONFIG_LINKS}
3338 @defmac AC_CONFIG_LINKS (@var{dest}:@var{source}@dots{}, @ovar{cmds}, @
3339   @ovar{init-cmds})
3340 @acindex{CONFIG_LINKS}
3341 @cindex Links
3342 Make @code{AC_OUTPUT} link each of the existing files @var{source} to
3343 the corresponding link name @var{dest}.  Makes a symbolic link if
3344 possible, otherwise a hard link if possible, otherwise a copy.  The
3345 @var{dest} and @var{source} names should be relative to the top level
3346 source or build directory.  This macro is one of the instantiating
3347 macros; see @ref{Configuration Actions}.
3349 For example, this call:
3351 @example
3352 AC_CONFIG_LINKS([host.h:config/$machine.h
3353                 object.h:config/$obj_format.h])
3354 @end example
3356 @noindent
3357 creates in the current directory @file{host.h} as a link to
3358 @file{@var{srcdir}/config/$machine.h}, and @file{object.h} as a
3359 link to @file{@var{srcdir}/config/$obj_format.h}.
3361 The tempting value @samp{.} for @var{dest} is invalid: it makes it
3362 impossible for @samp{config.status} to guess the links to establish.
3364 One can then run:
3365 @example
3366 ./config.status host.h object.h
3367 @end example
3368 @noindent
3369 to create the links.
3370 @end defmac
3374 @node Subdirectories
3375 @section Configuring Other Packages in Subdirectories
3376 @cindex Configure subdirectories
3377 @cindex Subdirectory configure
3379 In most situations, calling @code{AC_OUTPUT} is sufficient to produce
3380 makefiles in subdirectories.  However, @command{configure} scripts
3381 that control more than one independent package can use
3382 @code{AC_CONFIG_SUBDIRS} to run @command{configure} scripts for other
3383 packages in subdirectories.
3385 @defmac AC_CONFIG_SUBDIRS (@var{dir} @dots{})
3386 @acindex{CONFIG_SUBDIRS}
3387 @ovindex subdirs
3388 Make @code{AC_OUTPUT} run @command{configure} in each subdirectory
3389 @var{dir} in the given blank-or-newline-separated list.  Each @var{dir} should
3390 be a literal, i.e., please do not use:
3392 @example
3393 if test "$package_foo_enabled" = yes; then
3394   $my_subdirs="$my_subdirs foo"
3396 AC_CONFIG_SUBDIRS([$my_subdirs])
3397 @end example
3399 @noindent
3400 because this prevents @samp{./configure --help=recursive} from
3401 displaying the options of the package @code{foo}.  Instead, you should
3402 write:
3404 @example
3405 if test "$package_foo_enabled" = yes; then
3406   AC_CONFIG_SUBDIRS([foo])
3408 @end example
3410 If a given @var{dir} is not found, an error is reported: if the
3411 subdirectory is optional, write:
3413 @example
3414 if test -d "$srcdir/foo"; then
3415   AC_CONFIG_SUBDIRS([foo])
3417 @end example
3419 @c NB: Yes, below we mean configure.in, not configure.ac.
3420 If a given @var{dir} contains @command{configure.gnu}, it is run instead
3421 of @command{configure}.  This is for packages that might use a
3422 non-Autoconf script @command{Configure}, which can't be called through a
3423 wrapper @command{configure} since it would be the same file on
3424 case-insensitive file systems.  Likewise, if a @var{dir} contains
3425 @file{configure.in} but no @command{configure}, the Cygnus
3426 @command{configure} script found by @code{AC_CONFIG_AUX_DIR} is used.
3428 The subdirectory @command{configure} scripts are given the same command
3429 line options that were given to this @command{configure} script, with minor
3430 changes if needed, which include:
3432 @itemize @minus
3433 @item
3434 adjusting a relative name for the cache file;
3436 @item
3437 adjusting a relative name for the source directory;
3439 @item
3440 propagating the current value of @code{$prefix}, including if it was
3441 defaulted, and if the default values of the top level and of the subdirectory
3442 @file{configure} differ.
3443 @end itemize
3445 This macro also sets the output variable @code{subdirs} to the list of
3446 directories @samp{@var{dir} @dots{}}.  Make rules can use
3447 this variable to determine which subdirectories to recurse into.
3449 This macro may be called multiple times.
3450 @end defmac
3452 @node Default Prefix
3453 @section Default Prefix
3454 @cindex Install prefix
3455 @cindex Prefix for install
3457 By default, @command{configure} sets the prefix for files it installs to
3458 @file{/usr/local}.  The user of @command{configure} can select a different
3459 prefix using the @option{--prefix} and @option{--exec-prefix} options.
3460 There are two ways to change the default: when creating
3461 @command{configure}, and when running it.
3463 Some software packages might want to install in a directory other than
3464 @file{/usr/local} by default.  To accomplish that, use the
3465 @code{AC_PREFIX_DEFAULT} macro.
3467 @defmac AC_PREFIX_DEFAULT (@var{prefix})
3468 @acindex{PREFIX_DEFAULT}
3469 Set the default installation prefix to @var{prefix} instead of
3470 @file{/usr/local}.
3471 @end defmac
3473 It may be convenient for users to have @command{configure} guess the
3474 installation prefix from the location of a related program that they
3475 have already installed.  If you wish to do that, you can call
3476 @code{AC_PREFIX_PROGRAM}.
3478 @anchor{AC_PREFIX_PROGRAM}
3479 @defmac AC_PREFIX_PROGRAM (@var{program})
3480 @acindex{PREFIX_PROGRAM}
3481 If the user did not specify an installation prefix (using the
3482 @option{--prefix} option), guess a value for it by looking for
3483 @var{program} in @env{PATH}, the way the shell does.  If @var{program}
3484 is found, set the prefix to the parent of the directory containing
3485 @var{program}, else default the prefix as described above
3486 (@file{/usr/local} or @code{AC_PREFIX_DEFAULT}).  For example, if
3487 @var{program} is @code{gcc} and the @env{PATH} contains
3488 @file{/usr/local/gnu/bin/gcc}, set the prefix to @file{/usr/local/gnu}.
3489 @end defmac
3493 @c ======================================================== Existing tests
3495 @node Existing Tests
3496 @chapter Existing Tests
3498 These macros test for particular system features that packages might
3499 need or want to use.  If you need to test for a kind of feature that
3500 none of these macros check for, you can probably do it by calling
3501 primitive test macros with appropriate arguments (@pxref{Writing
3502 Tests}).
3504 These tests print messages telling the user which feature they're
3505 checking for, and what they find.  They cache their results for future
3506 @command{configure} runs (@pxref{Caching Results}).
3508 Some of these macros set output variables.  @xref{Makefile
3509 Substitutions}, for how to get their values.  The phrase ``define
3510 @var{name}'' is used below as a shorthand to mean ``define the C
3511 preprocessor symbol @var{name} to the value 1''.  @xref{Defining
3512 Symbols}, for how to get those symbol definitions into your program.
3514 @menu
3515 * Common Behavior::             Macros' standard schemes
3516 * Alternative Programs::        Selecting between alternative programs
3517 * Files::                       Checking for the existence of files
3518 * Libraries::                   Library archives that might be missing
3519 * Library Functions::           C library functions that might be missing
3520 * Header Files::                Header files that might be missing
3521 * Declarations::                Declarations that may be missing
3522 * Structures::                  Structures or members that might be missing
3523 * Types::                       Types that might be missing
3524 * Compilers and Preprocessors::  Checking for compiling programs
3525 * System Services::             Operating system services
3526 * Posix Variants::              Special kludges for specific Posix variants
3527 * Erlang Libraries::            Checking for the existence of Erlang libraries
3528 @end menu
3530 @node Common Behavior
3531 @section Common Behavior
3532 @cindex Common autoconf behavior
3534 Much effort has been expended to make Autoconf easy to learn.  The most
3535 obvious way to reach this goal is simply to enforce standard interfaces
3536 and behaviors, avoiding exceptions as much as possible.  Because of
3537 history and inertia, unfortunately, there are still too many exceptions
3538 in Autoconf; nevertheless, this section describes some of the common
3539 rules.
3541 @menu
3542 * Standard Symbols::            Symbols defined by the macros
3543 * Default Includes::            Includes used by the generic macros
3544 @end menu
3546 @node Standard Symbols
3547 @subsection Standard Symbols
3548 @cindex Standard symbols
3550 All the generic macros that @code{AC_DEFINE} a symbol as a result of
3551 their test transform their @var{argument} values to a standard alphabet.
3552 First, @var{argument} is converted to upper case and any asterisks
3553 (@samp{*}) are each converted to @samp{P}.  Any remaining characters
3554 that are not alphanumeric are converted to underscores.
3556 For instance,
3558 @example
3559 AC_CHECK_TYPES([struct $Expensive*])
3560 @end example
3562 @noindent
3563 defines the symbol @samp{HAVE_STRUCT__EXPENSIVEP} if the check
3564 succeeds.
3567 @node Default Includes
3568 @subsection Default Includes
3569 @cindex Default includes
3570 @cindex Includes, default
3572 Several tests depend upon a set of header files.  Since these headers
3573 are not universally available, tests actually have to provide a set of
3574 protected includes, such as:
3576 @example
3577 @group
3578 #ifdef TIME_WITH_SYS_TIME
3579 # include <sys/time.h>
3580 # include <time.h>
3581 #else
3582 # ifdef HAVE_SYS_TIME_H
3583 #  include <sys/time.h>
3584 # else
3585 #  include <time.h>
3586 # endif
3587 #endif
3588 @end group
3589 @end example
3591 @noindent
3592 Unless you know exactly what you are doing, you should avoid using
3593 unconditional includes, and check the existence of the headers you
3594 include beforehand (@pxref{Header Files}).
3596 Most generic macros use the following macro to provide the default set
3597 of includes:
3599 @defmac AC_INCLUDES_DEFAULT (@ovar{include-directives})
3600 @acindex{INCLUDES_DEFAULT}
3601 Expand to @var{include-directives} if defined, otherwise to:
3603 @example
3604 @group
3605 #include <stdio.h>
3606 #ifdef HAVE_SYS_TYPES_H
3607 # include <sys/types.h>
3608 #endif
3609 #ifdef HAVE_SYS_STAT_H
3610 # include <sys/stat.h>
3611 #endif
3612 #ifdef STDC_HEADERS
3613 # include <stdlib.h>
3614 # include <stddef.h>
3615 #else
3616 # ifdef HAVE_STDLIB_H
3617 #  include <stdlib.h>
3618 # endif
3619 #endif
3620 #ifdef HAVE_STRING_H
3621 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
3622 #  include <memory.h>
3623 # endif
3624 # include <string.h>
3625 #endif
3626 #ifdef HAVE_STRINGS_H
3627 # include <strings.h>
3628 #endif
3629 #ifdef HAVE_INTTYPES_H
3630 # include <inttypes.h>
3631 #endif
3632 #ifdef HAVE_STDINT_H
3633 # include <stdint.h>
3634 #endif
3635 #ifdef HAVE_UNISTD_H
3636 # include <unistd.h>
3637 #endif
3638 @end group
3639 @end example
3641 If the default includes are used, then check for the presence of these
3642 headers and their compatibility, i.e., you don't need to run
3643 @code{AC_HEADER_STDC}, nor check for @file{stdlib.h} etc.
3645 These headers are checked for in the same order as they are included.
3646 For instance, on some systems @file{string.h} and @file{strings.h} both
3647 exist, but conflict.  Then @code{HAVE_STRING_H} is defined, not
3648 @code{HAVE_STRINGS_H}.
3649 @end defmac
3651 @node Alternative Programs
3652 @section Alternative Programs
3653 @cindex Programs, checking
3655 These macros check for the presence or behavior of particular programs.
3656 They are used to choose between several alternative programs and to
3657 decide what to do once one has been chosen.  If there is no macro
3658 specifically defined to check for a program you need, and you don't need
3659 to check for any special properties of it, then you can use one of the
3660 general program-check macros.
3662 @menu
3663 * Particular Programs::         Special handling to find certain programs
3664 * Generic Programs::            How to find other programs
3665 @end menu
3667 @node Particular Programs
3668 @subsection Particular Program Checks
3670 These macros check for particular programs---whether they exist, and
3671 in some cases whether they support certain features.
3673 @defmac AC_PROG_AWK
3674 @acindex{PROG_AWK}
3675 @ovindex AWK
3676 Check for @code{gawk}, @code{mawk}, @code{nawk}, and @code{awk}, in that
3677 order, and set output variable @code{AWK} to the first one that is found.
3678 It tries @code{gawk} first because that is reported to be the
3679 best implementation.
3680 @end defmac
3682 @defmac AC_PROG_GREP
3683 @acindex{PROG_GREP}
3684 @ovindex GREP
3685 Look for the best available @code{grep} or @code{ggrep} that accepts the
3686 longest input lines possible, and that supports multiple @option{-e} options.
3687 Set the output variable @code{GREP} to whatever is chosen.
3688 @xref{Limitations of Usual Tools}, for more information about
3689 portability problems with the @command{grep} command family.
3690 @end defmac
3692 @defmac AC_PROG_EGREP
3693 @acindex{PROG_EGREP}
3694 @ovindex EGREP
3695 Check whether @code{$GREP -E} works, or else look for the best available
3696 @code{egrep} or @code{gegrep} that accepts the longest input lines possible.
3697 Set the output variable @code{EGREP} to whatever is chosen.
3698 @end defmac
3700 @defmac AC_PROG_FGREP
3701 @acindex{PROG_FGREP}
3702 @ovindex FGREP
3703 Check whether @code{$GREP -F} works, or else look for the best available
3704 @code{fgrep} or @code{gfgrep} that accepts the longest input lines possible.
3705 Set the output variable @code{FGREP} to whatever is chosen.
3706 @end defmac
3708 @defmac AC_PROG_INSTALL
3709 @acindex{PROG_INSTALL}
3710 @ovindex INSTALL
3711 @ovindex INSTALL_PROGRAM
3712 @ovindex INSTALL_DATA
3713 @ovindex INSTALL_SCRIPT
3714 Set output variable @code{INSTALL} to the name of a @acronym{BSD}-compatible
3715 @command{install} program, if one is found in the current @env{PATH}.
3716 Otherwise, set @code{INSTALL} to @samp{@var{dir}/install-sh -c},
3717 checking the directories specified to @code{AC_CONFIG_AUX_DIR} (or its
3718 default directories) to determine @var{dir} (@pxref{Output}).  Also set
3719 the variables @code{INSTALL_PROGRAM} and @code{INSTALL_SCRIPT} to
3720 @samp{$@{INSTALL@}} and @code{INSTALL_DATA} to @samp{$@{INSTALL@} -m 644}.
3722 @samp{@@INSTALL@@} is special, as its value may vary for different
3723 configuration files.
3725 This macro screens out various instances of @command{install} known not to
3726 work.  It prefers to find a C program rather than a shell script, for
3727 speed.  Instead of @file{install-sh}, it can also use @file{install.sh},
3728 but that name is obsolete because some @command{make} programs have a rule
3729 that creates @file{install} from it if there is no makefile.
3731 Autoconf comes with a copy of @file{install-sh} that you can use.  If
3732 you use @code{AC_PROG_INSTALL}, you must include either
3733 @file{install-sh} or @file{install.sh} in your distribution; otherwise
3734 @command{configure} produces an error message saying it can't find
3735 them---even if the system you're on has a good @command{install} program.
3736 This check is a safety measure to prevent you from accidentally leaving
3737 that file out, which would prevent your package from installing on
3738 systems that don't have a @acronym{BSD}-compatible @command{install} program.
3740 If you need to use your own installation program because it has features
3741 not found in standard @command{install} programs, there is no reason to use
3742 @code{AC_PROG_INSTALL}; just put the file name of your program into your
3743 @file{Makefile.in} files.
3744 @end defmac
3746 @defmac AC_PROG_MKDIR_P
3747 @acindex{PROG_MKDIR_P}
3748 @ovindex MKDIR_P
3749 Set output variable @code{MKDIR_P} to a program that ensures that for
3750 each argument, a directory named by this argument exists, creating it
3751 and its parent directories if needed, and without race conditions when
3752 two instances of the program attempt to make the same directory at
3753 nearly the same time.
3755 This macro uses the @samp{mkdir -p} command if possible.  Otherwise, it
3756 falls back on invoking @command{install-sh} with the @option{-d} option,
3757 so your package should
3758 contain @file{install-sh} as described under @code{AC_PROG_INSTALL}.
3759 An @file{install-sh} file that predates Autoconf 2.60 or Automake 1.10
3760 is vulnerable to race conditions, so if you want to support parallel
3761 installs from
3762 different packages into the same directory you need to make sure you
3763 have an up-to-date @file{install-sh}.  In particular, be careful about
3764 using @samp{autoreconf -if} if your Automake predates Automake 1.10.
3766 This macro is related to the @code{AS_MKDIR_P} macro (@pxref{Programming
3767 in M4sh}), but it sets an output variable intended for use in other
3768 files, whereas @code{AS_MKDIR_P} is intended for use in scripts like
3769 @command{configure}.  Also, @code{AS_MKDIR_P} does not accept options,
3770 but @code{MKDIR_P} supports the @option{-m} option, e.g., a makefile
3771 might invoke @code{$(MKDIR_P) -m 0 dir} to create an inaccessible
3772 directory, and conversely a makefile should use @code{$(MKDIR_P) --
3773 $(FOO)} if @var{FOO} might yield a value that begins with @samp{-}.
3774 Finally, @code{AS_MKDIR_P} does not check for race condition
3775 vulnerability, whereas @code{AC_PROG_MKDIR_P} does.
3777 @samp{@@MKDIR_P@@} is special, as its value may vary for different
3778 configuration files.
3779 @end defmac
3781 @anchor{AC_PROG_LEX}
3782 @defmac AC_PROG_LEX
3783 @acindex{PROG_LEX}
3784 @ovindex LEX
3785 @ovindex LEXLIB
3786 @cvindex YYTEXT_POINTER
3787 @ovindex LEX_OUTPUT_ROOT
3788 If @code{flex} is found, set output variable @code{LEX} to @samp{flex}
3789 and @code{LEXLIB} to @option{-lfl}, if that library is in a standard
3790 place.  Otherwise set @code{LEX} to @samp{lex} and @code{LEXLIB} to
3791 @option{-ll}.
3793 Define @code{YYTEXT_POINTER} if @code{yytext} defaults to @samp{char *} instead
3794 of to @samp{char []}.  Also set output variable @code{LEX_OUTPUT_ROOT} to
3795 the base of the file name that the lexer generates; usually
3796 @file{lex.yy}, but sometimes something else.  These results vary
3797 according to whether @code{lex} or @code{flex} is being used.
3799 You are encouraged to use Flex in your sources, since it is both more
3800 pleasant to use than plain Lex and the C source it produces is portable.
3801 In order to ensure portability, however, you must either provide a
3802 function @code{yywrap} or, if you don't use it (e.g., your scanner has
3803 no @samp{#include}-like feature), simply include a @samp{%noyywrap}
3804 statement in the scanner's source.  Once this done, the scanner is
3805 portable (unless @emph{you} felt free to use nonportable constructs) and
3806 does not depend on any library.  In this case, and in this case only, it
3807 is suggested that you use this Autoconf snippet:
3809 @example
3810 AC_PROG_LEX
3811 if test "$LEX" != flex; then
3812   LEX="$SHELL $missing_dir/missing flex"
3813   AC_SUBST([LEX_OUTPUT_ROOT], [lex.yy])
3814   AC_SUBST([LEXLIB], [''])
3816 @end example
3818 The shell script @command{missing} can be found in the Automake
3819 distribution.
3821 To ensure backward compatibility, Automake's @code{AM_PROG_LEX} invokes
3822 (indirectly) this macro twice, which causes an annoying but benign
3823 ``@code{AC_PROG_LEX} invoked multiple times'' warning.  Future versions
3824 of Automake will fix this issue; meanwhile, just ignore this message.
3826 As part of running the test, this macro may delete any file in the
3827 configuration directory named @file{lex.yy.c} or @file{lexyy.c}.
3828 @end defmac
3830 @anchor{AC_PROG_LN_S}
3831 @defmac AC_PROG_LN_S
3832 @acindex{PROG_LN_S}
3833 @ovindex LN_S
3834 If @samp{ln -s} works on the current file system (the operating system
3835 and file system support symbolic links), set the output variable
3836 @code{LN_S} to @samp{ln -s}; otherwise, if @samp{ln} works, set
3837 @code{LN_S} to @samp{ln}, and otherwise set it to @samp{cp -p}.
3839 If you make a link in a directory other than the current directory, its
3840 meaning depends on whether @samp{ln} or @samp{ln -s} is used.  To safely
3841 create links using @samp{$(LN_S)}, either find out which form is used
3842 and adjust the arguments, or always invoke @code{ln} in the directory
3843 where the link is to be created.
3845 In other words, it does not work to do:
3846 @example
3847 $(LN_S) foo /x/bar
3848 @end example
3850 Instead, do:
3852 @example
3853 (cd /x && $(LN_S) foo bar)
3854 @end example
3855 @end defmac
3857 @defmac AC_PROG_RANLIB
3858 @acindex{PROG_RANLIB}
3859 @ovindex RANLIB
3860 Set output variable @code{RANLIB} to @samp{ranlib} if @code{ranlib}
3861 is found, and otherwise to @samp{:} (do nothing).
3862 @end defmac
3864 @defmac AC_PROG_SED
3865 @acindex{PROG_SED}
3866 @ovindex SED
3867 Set output variable @code{SED} to a Sed implementation that conforms to
3868 Posix and does not have arbitrary length limits.  Report an error if no
3869 acceptable Sed is found.  @xref{Limitations of Usual Tools}, for more
3870 information about portability problems with Sed.
3871 @end defmac
3873 @defmac AC_PROG_YACC
3874 @acindex{PROG_YACC}
3875 @ovindex YACC
3876 If @code{bison} is found, set output variable @code{YACC} to @samp{bison
3877 -y}.  Otherwise, if @code{byacc} is found, set @code{YACC} to
3878 @samp{byacc}.  Otherwise set @code{YACC} to @samp{yacc}.
3879 @end defmac
3881 @node Generic Programs
3882 @subsection Generic Program and File Checks
3884 These macros are used to find programs not covered by the ``particular''
3885 test macros.  If you need to check the behavior of a program as well as
3886 find out whether it is present, you have to write your own test for it
3887 (@pxref{Writing Tests}).  By default, these macros use the environment
3888 variable @env{PATH}.  If you need to check for a program that might not
3889 be in the user's @env{PATH}, you can pass a modified path to use
3890 instead, like this:
3892 @example
3893 AC_PATH_PROG([INETD], [inetd], [/usr/libexec/inetd],
3894              [$PATH$PATH_SEPARATOR/usr/libexec$PATH_SEPARATOR]dnl
3895 [/usr/sbin$PATH_SEPARATOR/usr/etc$PATH_SEPARATOR/etc])
3896 @end example
3898 You are strongly encouraged to declare the @var{variable} passed to
3899 @code{AC_CHECK_PROG} etc.@: as precious, @xref{Setting Output Variables},
3900 @code{AC_ARG_VAR}, for more details.
3902 @anchor{AC_CHECK_PROG}
3903 @defmac AC_CHECK_PROG (@var{variable}, @var{prog-to-check-for}, @
3904   @var{value-if-found}, @ovar{value-if-not-found}, @dvar{path, $PATH}, @
3905   @ovar{reject})
3906 @acindex{CHECK_PROG}
3907 Check whether program @var{prog-to-check-for} exists in @var{path}.  If
3908 it is found, set @var{variable} to @var{value-if-found}, otherwise to
3909 @var{value-if-not-found}, if given.  Always pass over @var{reject} (an
3910 absolute file name) even if it is the first found in the search path; in
3911 that case, set @var{variable} using the absolute file name of the
3912 @var{prog-to-check-for} found that is not @var{reject}.  If
3913 @var{variable} was already set, do nothing.  Calls @code{AC_SUBST} for
3914 @var{variable}.
3915 @end defmac
3917 @anchor{AC_CHECK_PROGS}
3918 @defmac AC_CHECK_PROGS (@var{variable}, @var{progs-to-check-for}, @
3919   @ovar{value-if-not-found}, @dvar{path, $PATH})
3920 @acindex{CHECK_PROGS}
3921 Check for each program in the blank-separated list
3922 @var{progs-to-check-for} existing in the @var{path}.  If one is found, set
3923 @var{variable} to the name of that program.  Otherwise, continue
3924 checking the next program in the list.  If none of the programs in the
3925 list are found, set @var{variable} to @var{value-if-not-found}; if
3926 @var{value-if-not-found} is not specified, the value of @var{variable}
3927 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3928 @end defmac
3930 @defmac AC_CHECK_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
3931   @ovar{value-if-not-found}, @dvar{path, $PATH})
3932 @acindex{CHECK_TARGET_TOOL}
3933 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3934 with a prefix of the target type as determined by
3935 @code{AC_CANONICAL_TARGET}, followed by a dash (@pxref{Canonicalizing}).
3936 If the tool cannot be found with a prefix, and if the build and target
3937 types are equal, then it is also searched for without a prefix.
3939 As noted in @ref{Specifying Names, , Specifying the system type}, the
3940 target is rarely specified, because most of the time it is the same
3941 as the host: it is the type of system for which any compiler tool in
3942 the package produces code.  What this macro looks for is,
3943 for example, @emph{a tool @r{(assembler, linker, etc.)}@: that the
3944 compiler driver @r{(@command{gcc} for the @acronym{GNU} C Compiler)}
3945 uses to produce objects, archives or executables}.
3946 @end defmac
3948 @defmac AC_CHECK_TOOL (@var{variable}, @var{prog-to-check-for}, @
3949   @ovar{value-if-not-found}, @dvar{path, $PATH})
3950 @acindex{CHECK_TOOL}
3951 Like @code{AC_CHECK_PROG}, but first looks for @var{prog-to-check-for}
3952 with a prefix of the host type as determined by
3953 @code{AC_CANONICAL_HOST}, followed by a dash (@pxref{Canonicalizing}).
3954 For example, if the user runs @samp{configure --host=i386-gnu}, then
3955 this call:
3956 @example
3957 AC_CHECK_TOOL([RANLIB], [ranlib], [:])
3958 @end example
3959 @noindent
3960 sets @code{RANLIB} to @file{i386-gnu-ranlib} if that program exists in
3961 @var{path}, or otherwise to @samp{ranlib} if that program exists in
3962 @var{path}, or to @samp{:} if neither program exists.
3964 In the future, when cross-compiling this macro will @emph{only}
3965 accept program names that are prefixed with the host type.
3966 For more information, see @ref{Specifying Names, , Specifying the
3967 system type}.
3968 @end defmac
3970 @defmac AC_CHECK_TARGET_TOOLS (@var{variable}, @var{progs-to-check-for}, @
3971   @ovar{value-if-not-found}, @dvar{path, $PATH})
3972 @acindex{CHECK_TARGET_TOOLS}
3973 Like @code{AC_CHECK_TARGET_TOOL}, each of the tools in the list
3974 @var{progs-to-check-for} are checked with a prefix of the target type as
3975 determined by @code{AC_CANONICAL_TARGET}, followed by a dash
3976 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3977 prefix, and if the build and target types are equal, then the first one
3978 without a prefix is used.  If a tool is found, set @var{variable} to
3979 the name of that program.  If none of the tools in the list are found,
3980 set @var{variable} to @var{value-if-not-found}; if @var{value-if-not-found}
3981 is not specified, the value of @var{variable} is not changed.  Calls
3982 @code{AC_SUBST} for @var{variable}.
3983 @end defmac
3985 @defmac AC_CHECK_TOOLS (@var{variable}, @var{progs-to-check-for}, @
3986   @ovar{value-if-not-found}, @dvar{path, $PATH})
3987 @acindex{CHECK_TOOLS}
3988 Like @code{AC_CHECK_TOOL}, each of the tools in the list
3989 @var{progs-to-check-for} are checked with a prefix of the host type as
3990 determined by @code{AC_CANONICAL_HOST}, followed by a dash
3991 (@pxref{Canonicalizing}).  If none of the tools can be found with a
3992 prefix, then the first one without a prefix is used.  If a tool is found,
3993 set @var{variable} to the name of that program.  If none of the tools in
3994 the list are found, set @var{variable} to @var{value-if-not-found}; if
3995 @var{value-if-not-found} is not specified, the value of @var{variable}
3996 is not changed.  Calls @code{AC_SUBST} for @var{variable}.
3998 In the future, when cross-compiling this macro will @emph{not}
3999 accept program names that are not prefixed with the host type.
4000 @end defmac
4002 @anchor{AC_PATH_PROG}
4003 @defmac AC_PATH_PROG (@var{variable}, @var{prog-to-check-for}, @
4004   @ovar{value-if-not-found}, @dvar{path, $PATH})
4005 @acindex{PATH_PROG}
4006 Like @code{AC_CHECK_PROG}, but set @var{variable} to the absolute
4007 name of @var{prog-to-check-for} if found.
4008 @end defmac
4010 @anchor{AC_PATH_PROGS}
4011 @defmac AC_PATH_PROGS (@var{variable}, @var{progs-to-check-for}, @
4012   @ovar{value-if-not-found}, @dvar{path, $PATH})
4013 @acindex{PATH_PROGS}
4014 Like @code{AC_CHECK_PROGS}, but if any of @var{progs-to-check-for}
4015 are found, set @var{variable} to the absolute name of the program
4016 found.
4017 @end defmac
4019 @defmac AC_PATH_PROGS_FEATURE_CHECK (@var{variable}, @
4020   @var{progs-to-check-for}, @var{feature-test}, @
4021   @ovar{action-if-not-found}, @dvar{path, $PATH})
4022 @acindex{PATH_PROGS_FEATURE_CHECK}
4023 This macro was introduced in Autoconf 2.62.  If @var{variable} is not
4024 empty, then set the cache variable @code{$ac_cv_path_@var{variable}} to
4025 its value.  Otherwise, check for each program in the blank-separated
4026 list @var{progs-to-check-for} existing in @var{path}.  For each program
4027 found, execute @var{feature-test} with @code{$ac_path_@var{variable}}
4028 set to the absolute name of the candidate program.  If no invocation of
4029 @var{feature-test} sets the shell variable
4030 @code{$ac_cv_path_@var{variable}}, then @var{action-if-not-found} is
4031 executed.  @var{feature-test} will be run even when
4032 @code{ac_cv_path_@var{variable}} is set, to provide the ability to
4033 choose a better candidate found later in @var{path}; to accept the
4034 current setting and bypass all futher checks, @var{feature-test} can
4035 execute @code{ac_path_@var{variable}_found=:}.
4037 Note that this macro has some subtle differences from
4038 @code{AC_CHECK_PROGS}.  It is designed to be run inside
4039 @code{AC_CACHE_VAL}, therefore, it should have no side effects.  In
4040 particular, @var{variable} is not set to the final value of
4041 @code{ac_cv_path_@var{variable}}, nor is @code{AC_SUBST} automatically
4042 run.  Also, on failure, any action can be performed, whereas
4043 @code{AC_CHECK_PROGS} only performs
4044 @code{@var{variable}=@var{value-if-not-found}}.
4046 Here is an example, similar to what Autoconf uses in its own configure
4047 script.  It will search for an implementation of @command{m4} that
4048 supports the @code{indir} builtin, even if it goes by the name
4049 @command{gm4} or is not the first implementation on @env{PATH}.
4051 @example
4052 AC_CACHE_CHECK([for m4 that supports indir], [ac_cv_path_M4],
4053   [AC_PATH_PROGS_FEATURE_CHECK([M4], [m4 gm4],
4054     [[m4out=`echo 'changequote([,])indir([divnum])' | $ac_path_M4`
4055       test "x$m4out" = x0 \
4056       && ac_cv_path_M4=$ac_path_M4 ac_path_M4_found=:]],
4057     [AC_MSG_ERROR([could not find m4 that supports indir])])])
4058 AC_SUBST([M4], [$ac_cv_path_M4])
4059 @end example
4060 @end defmac
4062 @defmac AC_PATH_TARGET_TOOL (@var{variable}, @var{prog-to-check-for}, @
4063   @ovar{value-if-not-found}, @dvar{path, $PATH})
4064 @acindex{PATH_TARGET_TOOL}
4065 Like @code{AC_CHECK_TARGET_TOOL}, but set @var{variable} to the absolute
4066 name of the program if it is found.
4067 @end defmac
4069 @defmac AC_PATH_TOOL (@var{variable}, @var{prog-to-check-for}, @
4070   @ovar{value-if-not-found}, @dvar{path, $PATH})
4071 @acindex{PATH_TOOL}
4072 Like @code{AC_CHECK_TOOL}, but set @var{variable} to the absolute
4073 name of the program if it is found.
4075 In the future, when cross-compiling this macro will @emph{not}
4076 accept program names that are not prefixed with the host type.
4077 @end defmac
4080 @node Files
4081 @section Files
4082 @cindex File, checking
4084 You might also need to check for the existence of files.  Before using
4085 these macros, ask yourself whether a runtime test might not be a better
4086 solution.  Be aware that, like most Autoconf macros, they test a feature
4087 of the host machine, and therefore, they die when cross-compiling.
4089 @defmac AC_CHECK_FILE (@var{file}, @ovar{action-if-found}, @
4090   @ovar{action-if-not-found})
4091 @acindex{CHECK_FILE}
4092 Check whether file @var{file} exists on the native system.  If it is
4093 found, execute @var{action-if-found}, otherwise do
4094 @var{action-if-not-found}, if given.
4095 @end defmac
4097 @defmac AC_CHECK_FILES (@var{files}, @ovar{action-if-found}, @
4098   @ovar{action-if-not-found})
4099 @acindex{CHECK_FILES}
4100 Executes @code{AC_CHECK_FILE} once for each file listed in @var{files}.
4101 Additionally, defines @samp{HAVE_@var{file}} (@pxref{Standard Symbols})
4102 for each file found.
4103 @end defmac
4106 @node Libraries
4107 @section Library Files
4108 @cindex Library, checking
4110 The following macros check for the presence of certain C, C++, or Fortran
4111 library archive files.
4113 @anchor{AC_CHECK_LIB}
4114 @defmac AC_CHECK_LIB (@var{library}, @var{function}, @
4115   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4116 @acindex{CHECK_LIB}
4117 Test whether the library @var{library} is available by trying to link
4118 a test program that calls function @var{function} with the library.
4119 @var{function} should be a function provided by the library.
4120 Use the base
4121 name of the library; e.g., to check for @option{-lmp}, use @samp{mp} as
4122 the @var{library} argument.
4124 @var{action-if-found} is a list of shell commands to run if the link
4125 with the library succeeds; @var{action-if-not-found} is a list of shell
4126 commands to run if the link fails.  If @var{action-if-found} is not
4127 specified, the default action prepends @option{-l@var{library}} to
4128 @code{LIBS} and defines @samp{HAVE_LIB@var{library}} (in all
4129 capitals).  This macro is intended to support building @code{LIBS} in
4130 a right-to-left (least-dependent to most-dependent) fashion such that
4131 library dependencies are satisfied as a natural side effect of
4132 consecutive tests.  Linkers are sensitive to library ordering
4133 so the order in which @code{LIBS} is generated is important to reliable
4134 detection of libraries.
4136 If linking with @var{library} results in unresolved symbols that would
4137 be resolved by linking with additional libraries, give those libraries
4138 as the @var{other-libraries} argument, separated by spaces:
4139 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4140 that @var{library} is present, because linking the test program
4141 always fails with unresolved symbols.  The @var{other-libraries} argument
4142 should be limited to cases where it is desirable to test for one library
4143 in the presence of another that is not already in @code{LIBS}.
4145 @code{AC_CHECK_LIB} requires some care in usage, and should be avoided
4146 in some common cases.  Many standard functions like @code{gethostbyname}
4147 appear in the standard C library on some hosts, and in special libraries
4148 like @code{nsl} on other hosts.  On some hosts the special libraries
4149 contain variant implementations that you may not want to use.  These
4150 days it is normally better to use @code{AC_SEARCH_LIBS([gethostbyname],
4151 [nsl])} instead of @code{AC_CHECK_LIB([nsl], [gethostbyname])}.
4152 @end defmac
4154 @anchor{AC_SEARCH_LIBS}
4155 @defmac AC_SEARCH_LIBS (@var{function}, @var{search-libs}, @
4156   @ovar{action-if-found}, @ovar{action-if-not-found}, @ovar{other-libraries})
4157 @acindex{SEARCH_LIBS}
4158 Search for a library defining @var{function} if it's not already
4159 available.  This equates to calling
4160 @samp{AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])])} first with
4161 no libraries, then for each library listed in @var{search-libs}.
4163 Add @option{-l@var{library}} to @code{LIBS} for the first library found
4164 to contain @var{function}, and run @var{action-if-found}.  If the
4165 function is not found, run @var{action-if-not-found}.
4167 If linking with @var{library} results in unresolved symbols that would
4168 be resolved by linking with additional libraries, give those libraries
4169 as the @var{other-libraries} argument, separated by spaces:
4170 e.g., @option{-lXt -lX11}.  Otherwise, this macro fails to detect
4171 that @var{function} is present, because linking the test program
4172 always fails with unresolved symbols.
4173 @end defmac
4177 @node Library Functions
4178 @section Library Functions
4180 The following macros check for particular C library functions.
4181 If there is no macro specifically defined to check for a function you need,
4182 and you don't need to check for any special properties of
4183 it, then you can use one of the general function-check macros.
4185 @menu
4186 * Function Portability::        Pitfalls with usual functions
4187 * Particular Functions::        Special handling to find certain functions
4188 * Generic Functions::           How to find other functions
4189 @end menu
4191 @node Function Portability
4192 @subsection Portability of C Functions
4193 @cindex Portability of C functions
4194 @cindex C function portability
4196 Most usual functions can either be missing, or be buggy, or be limited
4197 on some architectures.  This section tries to make an inventory of these
4198 portability issues.  By definition, this list always requires
4199 additions.  Please help us keeping it as complete as possible.
4201 @table @asis
4202 @item @code{exit}
4203 @c @fuindex exit
4204 @prindex @code{exit}
4205 On ancient hosts, @code{exit} returned @code{int}.
4206 This is because @code{exit} predates @code{void}, and there was a long
4207 tradition of it returning @code{int}.
4209 On current hosts, the problem more likely is that @code{exit} is not
4210 declared, due to C++ problems of some sort or another.  For this reason
4211 we suggest that test programs not invoke @code{exit}, but return from
4212 @code{main} instead.
4214 @item @code{free}
4215 @c @fuindex free
4216 @prindex @code{free}
4217 The C standard says a call @code{free (NULL)} does nothing, but
4218 some old systems don't support this (e.g., NextStep).
4220 @item @code{isinf}
4221 @itemx @code{isnan}
4222 @c @fuindex isinf
4223 @c @fuindex isnan
4224 @prindex @code{isinf}
4225 @prindex @code{isnan}
4226 The C99 standard says that @code{isinf} and @code{isnan} are
4227 macros.  On some systems just macros are available
4228 (e.g., @acronym{HP-UX} and Solaris 10), on
4229 some systems both macros and functions (e.g., glibc 2.3.2), and on some
4230 systems only functions (e.g., IRIX 6 and Solaris 9).  In some cases
4231 these functions are declared in nonstandard headers like
4232 @code{<sunmath.h>} and defined in non-default libraries like
4233 @option{-lm} or @option{-lsunmath}.
4235 The C99 @code{isinf} and @code{isnan} macros work correctly with
4236 @code{long double} arguments, but pre-C99 systems that use functions
4237 typically assume @code{double} arguments.  On such a system,
4238 @code{isinf} incorrectly returns true for a finite @code{long double}
4239 argument that is outside the range of @code{double}.
4241 To work around this porting mess, you can use code like the following.
4243 @smallexample
4244 #include <math.h>
4246 #ifndef isnan
4247 # define isnan(x) \
4248     (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
4249      : sizeof (x) == sizeof (double) ? isnan_d (x) \
4250      : isnan_f (x))
4251 static inline int isnan_f  (float       x) @{ return x != x; @}
4252 static inline int isnan_d  (double      x) @{ return x != x; @}
4253 static inline int isnan_ld (long double x) @{ return x != x; @}
4254 #endif
4256 #ifndef isinf
4257 # define isinf(x) \
4258     (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
4259      : sizeof (x) == sizeof (double) ? isinf_d (x) \
4260      : isinf_f (x))
4261 static inline int isinf_f  (float       x) @{ return isnan (x - x); @}
4262 static inline int isinf_d  (double      x) @{ return isnan (x - x); @}
4263 static inline int isinf_ld (long double x) @{ return isnan (x - x); @}
4264 #endif
4265 @end smallexample
4267 Use @code{AC_C_INLINE} (@pxref{C Compiler}) so that this code works on
4268 compilers that lack the @code{inline} keyword.  Some optimizing
4269 compilers mishandle these definitions, but systems with that bug
4270 typically have missing or broken @code{isnan} functions anyway, so it's
4271 probably not worth worrying about.
4273 @item @code{malloc}
4274 @c @fuindex malloc
4275 @prindex @code{malloc}
4276 The C standard says a call @code{malloc (0)} is implementation
4277 dependent.  It can return either @code{NULL} or a new non-null pointer.
4278 The latter is more common (e.g., the @acronym{GNU} C Library) but is by
4279 no means universal.  @code{AC_FUNC_MALLOC}
4280 can be used to insist on non-@code{NULL} (@pxref{Particular Functions}).
4282 @item @code{putenv}
4283 @c @fuindex putenv
4284 @prindex @code{putenv}
4285 Posix prefers @code{setenv} to @code{putenv}; among other things,
4286 @code{putenv} is not required of all Posix implementations, but
4287 @code{setenv} is.
4289 Posix specifies that @code{putenv} puts the given string directly in
4290 @code{environ}, but some systems make a copy of it instead (e.g.,
4291 glibc 2.0, or @acronym{BSD}).  And when a copy is made, @code{unsetenv} might
4292 not free it, causing a memory leak (e.g., Free@acronym{BSD} 4).
4294 On some systems @code{putenv ("FOO")} removes @samp{FOO} from the
4295 environment, but this is not standard usage and it dumps core
4296 on some systems (e.g., AIX).
4298 On MinGW, a call @code{putenv ("FOO=")} removes @samp{FOO} from the
4299 environment, rather than inserting it with an empty value.
4301 @item @code{realloc}
4302 @c @fuindex realloc
4303 @prindex @code{realloc}
4304 The C standard says a call @code{realloc (NULL, size)} is equivalent
4305 to @code{malloc (size)}, but some old systems don't support this (e.g.,
4306 NextStep).
4308 @item @code{signal} handler
4309 @c @fuindex signal
4310 @prindex @code{signal}
4311 Normally @code{signal} takes a handler function with a return type of
4312 @code{void}, but some old systems required @code{int} instead.  Any
4313 actual @code{int} value returned is not used; this is only a
4314 difference in the function prototype demanded.
4316 All systems we know of in current use return @code{void}.  The
4317 @code{int} was to support K&R C, where of course @code{void} is not
4318 available.  @code{AC_TYPE_SIGNAL} (@pxref{Particular Types}) can be
4319 used to establish the correct type in all cases.
4321 @item @code{snprintf}
4322 @c @fuindex snprintf
4323 @prindex @code{snprintf}
4324 @c @fuindex vsnprintf
4325 @prindex @code{vsnprintf}
4326 The C99 standard says that if the output array isn't big enough
4327 and if no other errors occur, @code{snprintf} and @code{vsnprintf}
4328 truncate the output and return the number of bytes that ought to have
4329 been produced.  Some older systems return the truncated length (e.g.,
4330 @acronym{GNU} C Library 2.0.x or @sc{irix} 6.5), some a negative value
4331 (e.g., earlier @acronym{GNU} C Library versions), and some the buffer
4332 length without truncation (e.g., 32-bit Solaris 7).  Also, some buggy
4333 older systems ignore the length and overrun the buffer (e.g., 64-bit
4334 Solaris 7).
4336 @item @code{sprintf}
4337 @c @fuindex sprintf
4338 @prindex @code{sprintf}
4339 @c @fuindex vsprintf
4340 @prindex @code{vsprintf}
4341 The C standard says @code{sprintf} and @code{vsprintf} return the
4342 number of bytes written.  On some ancient systems (SunOS 4 for
4343 instance) they return the buffer pointer instead, but these no
4344 longer need to be worried about.
4346 @item @code{sscanf}
4347 @c @fuindex sscanf
4348 @prindex @code{sscanf}
4349 On various old systems, e.g., @acronym{HP-UX} 9, @code{sscanf} requires
4350 that its
4351 input string be writable (though it doesn't actually change it).  This
4352 can be a problem when using @command{gcc} since it normally puts
4353 constant strings in read-only memory (@pxref{Incompatibilities,
4354 Incompatibilities of @acronym{GCC}, , gcc, Using and
4355 Porting the @acronym{GNU} Compiler Collection}).  Apparently in some cases even
4356 having format strings read-only can be a problem.
4358 @item @code{strerror_r}
4359 @c @fuindex strerror_r
4360 @prindex @code{strerror_r}
4361 Posix specifies that @code{strerror_r} returns an @code{int}, but many
4362 systems (e.g., @acronym{GNU} C Library version 2.2.4) provide a
4363 different version returning a @code{char *}.  @code{AC_FUNC_STRERROR_R}
4364 can detect which is in use (@pxref{Particular Functions}).
4366 @item @code{strnlen}
4367 @c @fuindex strnlen
4368 @prindex @code{strnlen}
4369 @acronym{AIX} 4.3 provides a broken version which produces the
4370 following results:
4372 @example
4373 strnlen ("foobar", 0) = 0
4374 strnlen ("foobar", 1) = 3
4375 strnlen ("foobar", 2) = 2
4376 strnlen ("foobar", 3) = 1
4377 strnlen ("foobar", 4) = 0
4378 strnlen ("foobar", 5) = 6
4379 strnlen ("foobar", 6) = 6
4380 strnlen ("foobar", 7) = 6
4381 strnlen ("foobar", 8) = 6
4382 strnlen ("foobar", 9) = 6
4383 @end example
4385 @item @code{sysconf}
4386 @c @fuindex sysconf
4387 @prindex @code{sysconf}
4388 @code{_SC_PAGESIZE} is standard, but some older systems (e.g., @acronym{HP-UX}
4389 9) have @code{_SC_PAGE_SIZE} instead.  This can be tested with
4390 @code{#ifdef}.
4392 @item @code{unlink}
4393 @c @fuindex unlink
4394 @prindex @code{unlink}
4395 The Posix spec says that @code{unlink} causes the given file to be
4396 removed only after there are no more open file handles for it.  Some
4397 non-Posix hosts have trouble with this requirement, though,
4398 and some @acronym{DOS} variants even corrupt the file system.
4400 @item @code{unsetenv}
4401 @c @fuindex unsetenv
4402 @prindex @code{unsetenv}
4403 On MinGW, @code{unsetenv} is not available, but a variable @samp{FOO}
4404 can be removed with a call @code{putenv ("FOO=")}, as described under
4405 @code{putenv} above.
4407 @item @code{va_copy}
4408 @c @fuindex va_copy
4409 @prindex @code{va_copy}
4410 The C99 standard provides @code{va_copy} for copying
4411 @code{va_list} variables.  It may be available in older environments
4412 too, though possibly as @code{__va_copy} (e.g., @command{gcc} in strict
4413 pre-C99 mode).  These can be tested with @code{#ifdef}.  A fallback to
4414 @code{memcpy (&dst, &src, sizeof (va_list))} gives maximum
4415 portability.
4417 @item @code{va_list}
4418 @c @fuindex va_list
4419 @prindex @code{va_list}
4420 @code{va_list} is not necessarily just a pointer.  It can be a
4421 @code{struct} (e.g., @command{gcc} on Alpha), which means @code{NULL} is
4422 not portable.  Or it can be an array (e.g., @command{gcc} in some
4423 PowerPC configurations), which means as a function parameter it can be
4424 effectively call-by-reference and library routines might modify the
4425 value back in the caller (e.g., @code{vsnprintf} in the @acronym{GNU} C Library
4426 2.1).
4428 @item Signed @code{>>}
4429 Normally the C @code{>>} right shift of a signed type replicates the
4430 high bit, giving a so-called ``arithmetic'' shift.  But care should be
4431 taken since Standard C doesn't require that behavior.  On those
4432 few processors without a native arithmetic shift (for instance Cray
4433 vector systems) zero bits may be shifted in, the same as a shift of an
4434 unsigned type.
4436 @item Integer @code{/}
4437 C divides signed integers by truncating their quotient toward zero,
4438 yielding the same result as Fortran.  However, before C99 the standard
4439 allowed C implementations to take the floor or ceiling of the quotient
4440 in some cases.  Hardly any implementations took advantage of this
4441 freedom, though, and it's probably not worth worrying about this issue
4442 nowadays.
4443 @end table
4446 @node Particular Functions
4447 @subsection Particular Function Checks
4448 @cindex Function, checking
4450 These macros check for particular C functions---whether they exist, and
4451 in some cases how they respond when given certain arguments.
4453 @anchor{AC_FUNC_ALLOCA}
4454 @defmac AC_FUNC_ALLOCA
4455 @acindex{FUNC_ALLOCA}
4456 @cvindex C_ALLOCA
4457 @cvindex HAVE_ALLOCA_H
4458 @ovindex ALLOCA
4459 @c @fuindex alloca
4460 @prindex @code{alloca}
4461 @hdrindex{alloca.h}
4462 Check how to get @code{alloca}.  Tries to get a builtin version by
4463 checking for @file{alloca.h} or the predefined C preprocessor macros
4464 @code{__GNUC__} and @code{_AIX}.  If this macro finds @file{alloca.h},
4465 it defines @code{HAVE_ALLOCA_H}.
4467 If those attempts fail, it looks for the function in the standard C
4468 library.  If any of those methods succeed, it defines
4469 @code{HAVE_ALLOCA}.  Otherwise, it sets the output variable
4470 @code{ALLOCA} to @samp{$@{LIBOBJDIR@}alloca.o} and defines
4471 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
4472 garbage collect).  This variable is separate from @code{LIBOBJS} so
4473 multiple programs can share the value of @code{ALLOCA} without needing
4474 to create an actual library, in case only some of them use the code in
4475 @code{LIBOBJS}.  The @samp{$@{LIBOBJDIR@}} prefix serves the same
4476 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
4478 This macro does not try to get @code{alloca} from the System V R3
4479 @file{libPW} or the System V R4 @file{libucb} because those libraries
4480 contain some incompatible functions that cause trouble.  Some versions
4481 do not even contain @code{alloca} or contain a buggy version.  If you
4482 still want to use their @code{alloca}, use @code{ar} to extract
4483 @file{alloca.o} from them instead of compiling @file{alloca.c}.
4485 Source files that use @code{alloca} should start with a piece of code
4486 like the following, to declare it properly.
4488 @example
4489 @group
4490 #ifdef HAVE_ALLOCA_H
4491 # include <alloca.h>
4492 #elif defined __GNUC__
4493 # define alloca __builtin_alloca
4494 #elif defined _AIX
4495 # define alloca __alloca
4496 #elif defined _MSC_VER
4497 # include <malloc.h>
4498 # define alloca _alloca
4499 #else
4500 # include <stddef.h>
4501 # ifdef  __cplusplus
4502 extern "C"
4503 # endif
4504 void *alloca (size_t);
4505 #endif
4506 @end group
4507 @end example
4508 @end defmac
4510 @defmac AC_FUNC_CHOWN
4511 @acindex{FUNC_CHOWN}
4512 @cvindex HAVE_CHOWN
4513 @c @fuindex chown
4514 @prindex @code{chown}
4515 If the @code{chown} function is available and works (in particular, it
4516 should accept @option{-1} for @code{uid} and @code{gid}), define
4517 @code{HAVE_CHOWN}.
4518 @end defmac
4520 @anchor{AC_FUNC_CLOSEDIR_VOID}
4521 @defmac AC_FUNC_CLOSEDIR_VOID
4522 @acindex{FUNC_CLOSEDIR_VOID}
4523 @cvindex CLOSEDIR_VOID
4524 @c @fuindex closedir
4525 @prindex @code{closedir}
4526 If the @code{closedir} function does not return a meaningful value,
4527 define @code{CLOSEDIR_VOID}.  Otherwise, callers ought to check its
4528 return value for an error indicator.
4530 Currently this test is implemented by running a test program.  When
4531 cross compiling the pessimistic assumption that @code{closedir} does not
4532 return a meaningful value is made.
4534 This macro is obsolescent, as @code{closedir} returns a meaningful value
4535 on current systems.  New programs need not use this macro.
4536 @end defmac
4538 @defmac AC_FUNC_ERROR_AT_LINE
4539 @acindex{FUNC_ERROR_AT_LINE}
4540 @c @fuindex error_at_line
4541 @prindex @code{error_at_line}
4542 If the @code{error_at_line} function is not found, require an
4543 @code{AC_LIBOBJ} replacement of @samp{error}.
4544 @end defmac
4546 @defmac AC_FUNC_FNMATCH
4547 @acindex{FUNC_FNMATCH}
4548 @c @fuindex fnmatch
4549 @prindex @code{fnmatch}
4550 If the @code{fnmatch} function conforms to Posix, define
4551 @code{HAVE_FNMATCH}.  Detect common implementation bugs, for example,
4552 the bugs in Solaris 2.4.
4554 Unlike the other specific
4555 @code{AC_FUNC} macros, @code{AC_FUNC_FNMATCH} does not replace a
4556 broken/missing @code{fnmatch}.  This is for historical reasons.
4557 See @code{AC_REPLACE_FNMATCH} below.
4559 This macro is obsolescent.  New programs should use Gnulib's
4560 @code{fnmatch-posix} module.  @xref{Gnulib}.
4561 @end defmac
4563 @defmac AC_FUNC_FNMATCH_GNU
4564 @acindex{FUNC_FNMATCH_GNU}
4565 @c @fuindex fnmatch
4566 @prindex @code{fnmatch}
4567 Behave like @code{AC_REPLACE_FNMATCH} (@emph{replace}) but also test
4568 whether @code{fnmatch} supports @acronym{GNU} extensions.  Detect common
4569 implementation bugs, for example, the bugs in the @acronym{GNU} C
4570 Library 2.1.
4572 This macro is obsolescent.  New programs should use Gnulib's
4573 @code{fnmatch-gnu} module.  @xref{Gnulib}.
4574 @end defmac
4576 @anchor{AC_FUNC_FORK}
4577 @defmac AC_FUNC_FORK
4578 @acindex{FUNC_FORK}
4579 @cvindex HAVE_VFORK_H
4580 @cvindex HAVE_WORKING_FORK
4581 @cvindex HAVE_WORKING_VFORK
4582 @cvindex vfork
4583 @c @fuindex fork
4584 @prindex @code{fork}
4585 @c @fuindex vfork
4586 @prindex @code{vfork}
4587 @hdrindex{vfork.h}
4588 This macro checks for the @code{fork} and @code{vfork} functions.  If a
4589 working @code{fork} is found, define @code{HAVE_WORKING_FORK}.  This macro
4590 checks whether @code{fork} is just a stub by trying to run it.
4592 If @file{vfork.h} is found, define @code{HAVE_VFORK_H}.  If a working
4593 @code{vfork} is found, define @code{HAVE_WORKING_VFORK}.  Otherwise,
4594 define @code{vfork} to be @code{fork} for backward compatibility with
4595 previous versions of @command{autoconf}.  This macro checks for several known
4596 errors in implementations of @code{vfork} and considers the system to not
4597 have a working @code{vfork} if it detects any of them.  It is not considered
4598 to be an implementation error if a child's invocation of @code{signal}
4599 modifies the parent's signal handler, since child processes rarely change
4600 their signal handlers.
4602 Since this macro defines @code{vfork} only for backward compatibility with
4603 previous versions of @command{autoconf} you're encouraged to define it
4604 yourself in new code:
4605 @example
4606 @group
4607 #ifndef HAVE_WORKING_VFORK
4608 # define vfork fork
4609 #endif
4610 @end group
4611 @end example
4612 @end defmac
4614 @defmac AC_FUNC_FSEEKO
4615 @acindex{FUNC_FSEEKO}
4616 @cvindex _LARGEFILE_SOURCE
4617 @cvindex HAVE_FSEEKO
4618 @c @fuindex fseeko
4619 @prindex @code{fseeko}
4620 If the @code{fseeko} function is available, define @code{HAVE_FSEEKO}.
4621 Define @code{_LARGEFILE_SOURCE} if necessary to make the prototype
4622 visible on some systems (e.g., glibc 2.2).  Otherwise linkage problems
4623 may occur when compiling with @code{AC_SYS_LARGEFILE} on
4624 largefile-sensitive systems where @code{off_t} does not default to a
4625 64bit entity.
4626 @end defmac
4628 @defmac AC_FUNC_GETGROUPS
4629 @acindex{FUNC_GETGROUPS}
4630 @cvindex HAVE_GETGROUPS
4631 @ovindex GETGROUPS_LIBS
4632 @c @fuindex getgroups
4633 @prindex @code{getgroups}
4634 If the @code{getgroups} function is available and works (unlike on
4635 Ultrix 4.3, where @samp{getgroups (0, 0)} always fails), define
4636 @code{HAVE_GETGROUPS}.  Set @code{GETGROUPS_LIBS} to any libraries
4637 needed to get that function.  This macro runs @code{AC_TYPE_GETGROUPS}.
4638 @end defmac
4640 @anchor{AC_FUNC_GETLOADAVG}
4641 @defmac AC_FUNC_GETLOADAVG
4642 @acindex{FUNC_GETLOADAVG}
4643 @cvindex SVR4
4644 @cvindex DGUX
4645 @cvindex UMAX
4646 @cvindex UMAX4_3
4647 @cvindex HAVE_NLIST_H
4648 @cvindex NLIST_NAME_UNION
4649 @cvindex GETLOADAVG_PRIVILEGED
4650 @cvindex NEED_SETGID
4651 @cvindex C_GETLOADAVG
4652 @ovindex LIBOBJS
4653 @ovindex NEED_SETGID
4654 @ovindex KMEM_GROUP
4655 @ovindex GETLOADAVG_LIBS
4656 @c @fuindex getloadavg
4657 @prindex @code{getloadavg}
4658 Check how to get the system load averages.  To perform its tests
4659 properly, this macro needs the file @file{getloadavg.c}; therefore, be
4660 sure to set the @code{AC_LIBOBJ} replacement directory properly (see
4661 @ref{Generic Functions}, @code{AC_CONFIG_LIBOBJ_DIR}).
4663 If the system has the @code{getloadavg} function, define
4664 @code{HAVE_GETLOADAVG}, and set @code{GETLOADAVG_LIBS} to any libraries
4665 necessary to get that function.  Also add @code{GETLOADAVG_LIBS} to
4666 @code{LIBS}.  Otherwise, require an @code{AC_LIBOBJ} replacement for
4667 @samp{getloadavg} with source code in @file{@var{dir}/getloadavg.c}, and
4668 possibly define several other C preprocessor macros and output
4669 variables:
4671 @enumerate
4672 @item
4673 Define @code{C_GETLOADAVG}.
4675 @item
4676 Define @code{SVR4}, @code{DGUX}, @code{UMAX}, or @code{UMAX4_3} if on
4677 those systems.
4679 @item
4680 @hdrindex{nlist.h}
4681 If @file{nlist.h} is found, define @code{HAVE_NLIST_H}.
4683 @item
4684 If @samp{struct nlist} has an @samp{n_un.n_name} member, define
4685 @code{HAVE_STRUCT_NLIST_N_UN_N_NAME}.  The obsolete symbol
4686 @code{NLIST_NAME_UNION} is still defined, but do not depend upon it.
4688 @item
4689 Programs may need to be installed set-group-ID (or set-user-ID) for
4690 @code{getloadavg} to work.  In this case, define
4691 @code{GETLOADAVG_PRIVILEGED}, set the output variable @code{NEED_SETGID}
4692 to @samp{true} (and otherwise to @samp{false}), and set
4693 @code{KMEM_GROUP} to the name of the group that should own the installed
4694 program.
4695 @end enumerate
4697 The @code{AC_FUNC_GETLOADAVG} macro is obsolescent.  New programs should
4698 use Gnulib's @code{getloadavg} module.  @xref{Gnulib}.
4699 @end defmac
4701 @anchor{AC_FUNC_GETMNTENT}
4702 @defmac AC_FUNC_GETMNTENT
4703 @acindex{FUNC_GETMNTENT}
4704 @cvindex HAVE_GETMNTENT
4705 @c @fuindex getmntent
4706 @prindex @code{getmntent}
4707 Check for @code{getmntent} in the standard C library, and then in the
4708 @file{sun}, @file{seq}, and @file{gen} libraries, for @sc{unicos},
4709 @sc{irix} 4, @sc{ptx}, and UnixWare, respectively.  Then, if
4710 @code{getmntent} is available, define @code{HAVE_GETMNTENT}.
4711 @end defmac
4713 @defmac AC_FUNC_GETPGRP
4714 @acindex{FUNC_GETPGRP}
4715 @cvindex GETPGRP_VOID
4716 @c @fuindex getpgid
4717 @c @fuindex getpgrp
4718 @prindex @code{getpgid}
4719 @prindex @code{getpgrp}
4720 Define @code{GETPGRP_VOID} if it is an error to pass 0 to
4721 @code{getpgrp}; this is the Posix behavior.  On older @acronym{BSD}
4722 systems, you must pass 0 to @code{getpgrp}, as it takes an argument and
4723 behaves like Posix's @code{getpgid}.
4725 @example
4726 #ifdef GETPGRP_VOID
4727   pid = getpgrp ();
4728 #else
4729   pid = getpgrp (0);
4730 #endif
4731 @end example
4733 This macro does not check whether
4734 @code{getpgrp} exists at all; if you need to work in that situation,
4735 first call @code{AC_CHECK_FUNC} for @code{getpgrp}.
4737 This macro is obsolescent, as current systems have a @code{getpgrp}
4738 whose signature conforms to Posix.  New programs need not use this macro.
4739 @end defmac
4741 @defmac AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
4742 @acindex{FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK}
4743 @cvindex LSTAT_FOLLOWS_SLASHED_SYMLINK
4744 @c @fuindex lstat
4745 @prindex @code{lstat}
4746 If @file{link} is a symbolic link, then @code{lstat} should treat
4747 @file{link/} the same as @file{link/.}.  However, many older
4748 @code{lstat} implementations incorrectly ignore trailing slashes.
4750 It is safe to assume that if @code{lstat} incorrectly ignores
4751 trailing slashes, then other symbolic-link-aware functions like
4752 @code{unlink} also incorrectly ignore trailing slashes.
4754 If @code{lstat} behaves properly, define
4755 @code{LSTAT_FOLLOWS_SLASHED_SYMLINK}, otherwise require an
4756 @code{AC_LIBOBJ} replacement of @code{lstat}.
4757 @end defmac
4759 @defmac AC_FUNC_MALLOC
4760 @acindex{FUNC_MALLOC}
4761 @cvindex HAVE_MALLOC
4762 @cvindex malloc
4763 @c @fuindex malloc
4764 @prindex @code{malloc}
4765 If the @code{malloc} function is compatible with the @acronym{GNU} C
4766 library @code{malloc} (i.e., @samp{malloc (0)} returns a valid
4767 pointer), define @code{HAVE_MALLOC} to 1.  Otherwise define
4768 @code{HAVE_MALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4769 @samp{malloc}, and define @code{malloc} to @code{rpl_malloc} so that the
4770 native @code{malloc} is not used in the main project.
4772 Typically, the replacement file @file{malloc.c} should look like (note
4773 the @samp{#undef malloc}):
4775 @verbatim
4776 #ifdef HAVE_CONFIG_H
4777 # include <config.h>
4778 #endif
4779 #undef malloc
4781 #include <sys/types.h>
4783 void *malloc ();
4785 /* Allocate an N-byte block of memory from the heap.
4786    If N is zero, allocate a 1-byte block.  */
4788 void *
4789 rpl_malloc (size_t n)
4791   if (n == 0)
4792     n = 1;
4793   return malloc (n);
4795 @end verbatim
4796 @end defmac
4798 @defmac AC_FUNC_MEMCMP
4799 @acindex{FUNC_MEMCMP}
4800 @ovindex LIBOBJS
4801 @c @fuindex memcmp
4802 @prindex @code{memcmp}
4803 If the @code{memcmp} function is not available, or does not work on
4804 8-bit data (like the one on SunOS 4.1.3), or fails when comparing 16
4805 bytes or more and with at least one buffer not starting on a 4-byte
4806 boundary (such as the one on NeXT x86 OpenStep), require an
4807 @code{AC_LIBOBJ} replacement for @samp{memcmp}.
4809 This macro is obsolescent, as current systems have a working
4810 @code{memcmp}.  New programs need not use this macro.
4811 @end defmac
4813 @defmac AC_FUNC_MBRTOWC
4814 @acindex{FUNC_MBRTOWC}
4815 @cvindex HAVE_MBRTOWC
4816 @c @fuindex mbrtowc
4817 @prindex @code{mbrtowc}
4818 Define @code{HAVE_MBRTOWC} to 1 if the function @code{mbrtowc} and the
4819 type @code{mbstate_t} are properly declared.
4820 @end defmac
4822 @defmac AC_FUNC_MKTIME
4823 @acindex{FUNC_MKTIME}
4824 @ovindex LIBOBJS
4825 @c @fuindex mktime
4826 @prindex @code{mktime}
4827 If the @code{mktime} function is not available, or does not work
4828 correctly, require an @code{AC_LIBOBJ} replacement for @samp{mktime}.
4829 For the purposes of this test, @code{mktime} should conform to the
4830 Posix standard and should be the inverse of
4831 @code{localtime}.
4832 @end defmac
4834 @anchor{AC_FUNC_MMAP}
4835 @defmac AC_FUNC_MMAP
4836 @acindex{FUNC_MMAP}
4837 @cvindex HAVE_MMAP
4838 @c @fuindex mmap
4839 @prindex @code{mmap}
4840 If the @code{mmap} function exists and works correctly, define
4841 @code{HAVE_MMAP}.  This checks only private fixed mapping of already-mapped
4842 memory.
4843 @end defmac
4845 @defmac AC_FUNC_OBSTACK
4846 @acindex{FUNC_OBSTACK}
4847 @cvindex HAVE_OBSTACK
4848 @cindex obstack
4849 If the obstacks are found, define @code{HAVE_OBSTACK}, else require an
4850 @code{AC_LIBOBJ} replacement for @samp{obstack}.
4851 @end defmac
4853 @defmac AC_FUNC_REALLOC
4854 @acindex{FUNC_REALLOC}
4855 @cvindex HAVE_REALLOC
4856 @cvindex realloc
4857 @c @fuindex realloc
4858 @prindex @code{realloc}
4859 If the @code{realloc} function is compatible with the @acronym{GNU} C
4860 library @code{realloc} (i.e., @samp{realloc (NULL, 0)} returns a
4861 valid pointer), define @code{HAVE_REALLOC} to 1.  Otherwise define
4862 @code{HAVE_REALLOC} to 0, ask for an @code{AC_LIBOBJ} replacement for
4863 @samp{realloc}, and define @code{realloc} to @code{rpl_realloc} so that
4864 the native @code{realloc} is not used in the main project.  See
4865 @code{AC_FUNC_MALLOC} for details.
4866 @end defmac
4868 @defmac AC_FUNC_SELECT_ARGTYPES
4869 @acindex{FUNC_SELECT_ARGTYPES}
4870 @cvindex SELECT_TYPE_ARG1
4871 @cvindex SELECT_TYPE_ARG234
4872 @cvindex SELECT_TYPE_ARG5
4873 @c @fuindex select
4874 @prindex @code{select}
4875 Determines the correct type to be passed for each of the
4876 @code{select} function's arguments, and defines those types
4877 in @code{SELECT_TYPE_ARG1}, @code{SELECT_TYPE_ARG234}, and
4878 @code{SELECT_TYPE_ARG5} respectively.  @code{SELECT_TYPE_ARG1} defaults
4879 to @samp{int}, @code{SELECT_TYPE_ARG234} defaults to @samp{int *},
4880 and @code{SELECT_TYPE_ARG5} defaults to @samp{struct timeval *}.
4882 This macro is obsolescent, as current systems have a @code{select} whose
4883 signature conforms to Posix.  New programs need not use this macro.
4884 @end defmac
4886 @defmac AC_FUNC_SETPGRP
4887 @acindex{FUNC_SETPGRP}
4888 @cvindex SETPGRP_VOID
4889 @c @fuindex setpgrp
4890 @prindex @code{setpgrp}
4891 If @code{setpgrp} takes no argument (the Posix version), define
4892 @code{SETPGRP_VOID}.  Otherwise, it is the @acronym{BSD} version, which takes
4893 two process IDs as arguments.  This macro does not check whether
4894 @code{setpgrp} exists at all; if you need to work in that situation,
4895 first call @code{AC_CHECK_FUNC} for @code{setpgrp}.
4897 This macro is obsolescent, as current systems have a @code{setpgrp}
4898 whose signature conforms to Posix.  New programs need not use this macro.
4899 @end defmac
4901 @defmac AC_FUNC_STAT
4902 @defmacx AC_FUNC_LSTAT
4903 @acindex{FUNC_STAT}
4904 @acindex{FUNC_LSTAT}
4905 @cvindex HAVE_STAT_EMPTY_STRING_BUG
4906 @cvindex HAVE_LSTAT_EMPTY_STRING_BUG
4907 @c @fuindex stat
4908 @prindex @code{stat}
4909 @c @fuindex lstat
4910 @prindex @code{lstat}
4911 Determine whether @code{stat} or @code{lstat} have the bug that it
4912 succeeds when given the zero-length file name as argument.  The @code{stat}
4913 and @code{lstat} from SunOS 4.1.4 and the Hurd (as of 1998-11-01) do
4914 this.
4916 If it does, then define @code{HAVE_STAT_EMPTY_STRING_BUG} (or
4917 @code{HAVE_LSTAT_EMPTY_STRING_BUG}) and ask for an @code{AC_LIBOBJ}
4918 replacement of it.
4920 These macros are obsolescent, as no current systems have the bug.
4921 New programs need not use these macros.
4922 @end defmac
4924 @anchor{AC_FUNC_STRCOLL}
4925 @defmac AC_FUNC_STRCOLL
4926 @acindex{FUNC_STRCOLL}
4927 @cvindex HAVE_STRCOLL
4928 @c @fuindex strcoll
4929 @prindex @code{strcoll}
4930 If the @code{strcoll} function exists and works correctly, define
4931 @code{HAVE_STRCOLL}.  This does a bit more than
4932 @samp{AC_CHECK_FUNCS(strcoll)}, because some systems have incorrect
4933 definitions of @code{strcoll} that should not be used.
4934 @end defmac
4936 @defmac AC_FUNC_STRERROR_R
4937 @acindex{FUNC_STRERROR_R}
4938 @cvindex HAVE_STRERROR_R
4939 @cvindex HAVE_DECL_STRERROR_R
4940 @cvindex STRERROR_R_CHAR_P
4941 @c @fuindex strerror_r
4942 @prindex @code{strerror_r}
4943 If @code{strerror_r} is available, define @code{HAVE_STRERROR_R}, and if
4944 it is declared, define @code{HAVE_DECL_STRERROR_R}.  If it returns a
4945 @code{char *} message, define @code{STRERROR_R_CHAR_P}; otherwise it
4946 returns an @code{int} error number.  The Thread-Safe Functions option of
4947 Posix requires @code{strerror_r} to return @code{int}, but
4948 many systems (including, for example, version 2.2.4 of the @acronym{GNU} C
4949 Library) return a @code{char *} value that is not necessarily equal to
4950 the buffer argument.
4951 @end defmac
4953 @anchor{AC_FUNC_STRFTIME}
4954 @defmac AC_FUNC_STRFTIME
4955 @acindex{FUNC_STRFTIME}
4956 @cvindex HAVE_STRFTIME
4957 @c @fuindex strftime
4958 @prindex @code{strftime}
4959 Check for @code{strftime} in the @file{intl} library, for SCO Unix.
4960 Then, if @code{strftime} is available, define @code{HAVE_STRFTIME}.
4962 This macro is obsolescent, as no current systems require the @file{intl}
4963 library for @code{strftime}.  New programs need not use this macro.
4964 @end defmac
4966 @defmac AC_FUNC_STRTOD
4967 @acindex{FUNC_STRTOD}
4968 @ovindex POW_LIB
4969 @c @fuindex strtod
4970 @prindex @code{strtod}
4971 If the @code{strtod} function does not exist or doesn't work correctly,
4972 ask for an @code{AC_LIBOBJ} replacement of @samp{strtod}.  In this case,
4973 because @file{strtod.c} is likely to need @samp{pow}, set the output
4974 variable @code{POW_LIB} to the extra library needed.
4975 @end defmac
4977 @defmac AC_FUNC_STRTOLD
4978 @acindex{FUNC_STRTOLD}
4979 @cvindex HAVE_STRTOLD
4980 @prindex @code{strtold}
4981 If the @code{strtold} function exists and conforms to C99, define
4982 @code{HAVE_STRTOLD}.
4983 @end defmac
4985 @defmac AC_FUNC_STRNLEN
4986 @acindex{FUNC_STRNLEN}
4987 @cvindex HAVE_STRNLEN
4988 @c @fuindex strnlen
4989 @prindex @code{strnlen}
4990 If the @code{strnlen} function is not available, or is buggy (like the one
4991 from @acronym{AIX} 4.3), require an @code{AC_LIBOBJ} replacement for it.
4992 @end defmac
4994 @anchor{AC_FUNC_UTIME_NULL}
4995 @defmac AC_FUNC_UTIME_NULL
4996 @acindex{FUNC_UTIME_NULL}
4997 @cvindex HAVE_UTIME_NULL
4998 @c @fuindex utime
4999 @prindex @code{utime}
5000 If @samp{utime (@var{file}, NULL)} sets @var{file}'s timestamp to
5001 the present, define @code{HAVE_UTIME_NULL}.
5003 This macro is obsolescent, as all current systems have a @code{utime}
5004 that behaves this way.  New programs need not use this macro.
5005 @end defmac
5007 @anchor{AC_FUNC_VPRINTF}
5008 @defmac AC_FUNC_VPRINTF
5009 @acindex{FUNC_VPRINTF}
5010 @cvindex HAVE_VPRINTF
5011 @cvindex HAVE_DOPRNT
5012 @c @fuindex vprintf
5013 @prindex @code{vprintf}
5014 If @code{vprintf} is found, define @code{HAVE_VPRINTF}.  Otherwise, if
5015 @code{_doprnt} is found, define @code{HAVE_DOPRNT}.  (If @code{vprintf}
5016 is available, you may assume that @code{vfprintf} and @code{vsprintf}
5017 are also available.)
5019 This macro is obsolescent, as all current systems have @code{vprintf}.
5020 New programs need not use this macro.
5021 @end defmac
5023 @defmac AC_REPLACE_FNMATCH
5024 @acindex{REPLACE_FNMATCH}
5025 @c @fuindex fnmatch
5026 @prindex @code{fnmatch}
5027 @hdrindex{fnmatch.h}
5028 If the @code{fnmatch} function does not conform to Posix (see
5029 @code{AC_FUNC_FNMATCH}), ask for its @code{AC_LIBOBJ} replacement.
5031 The files @file{fnmatch.c}, @file{fnmatch_loop.c}, and @file{fnmatch_.h}
5032 in the @code{AC_LIBOBJ} replacement directory are assumed to contain a
5033 copy of the source code of @acronym{GNU} @code{fnmatch}.  If necessary,
5034 this source code is compiled as an @code{AC_LIBOBJ} replacement, and the
5035 @file{fnmatch_.h} file is linked to @file{fnmatch.h} so that it can be
5036 included in place of the system @code{<fnmatch.h>}.
5038 This macro is obsolescent, as it assumes the use of particular source
5039 files.  New programs should use Gnulib's @code{fnmatch-posix} module,
5040 which provides this macro along with the source files.  @xref{Gnulib}.
5041 @end defmac
5045 @node Generic Functions
5046 @subsection Generic Function Checks
5048 These macros are used to find functions not covered by the ``particular''
5049 test macros.  If the functions might be in libraries other than the
5050 default C library, first call @code{AC_CHECK_LIB} for those libraries.
5051 If you need to check the behavior of a function as well as find out
5052 whether it is present, you have to write your own test for
5053 it (@pxref{Writing Tests}).
5055 @anchor{AC_CHECK_FUNC}
5056 @defmac AC_CHECK_FUNC (@var{function}, @ovar{action-if-found}, @
5057   @ovar{action-if-not-found})
5058 @acindex{CHECK_FUNC}
5059 If C function @var{function} is available, run shell commands
5060 @var{action-if-found}, otherwise @var{action-if-not-found}.  If you just
5061 want to define a symbol if the function is available, consider using
5062 @code{AC_CHECK_FUNCS} instead.  This macro checks for functions with C
5063 linkage even when @code{AC_LANG(C++)} has been called, since C is more
5064 standardized than C++.  (@pxref{Language Choice}, for more information
5065 about selecting the language for checks.)
5066 @end defmac
5068 @anchor{AC_CHECK_FUNCS}
5069 @defmac AC_CHECK_FUNCS (@var{function}@dots{}, @ovar{action-if-found}, @
5070   @ovar{action-if-not-found})
5071 @acindex{CHECK_FUNCS}
5072 @cvindex HAVE_@var{function}
5073 For each @var{function} enumerated in the blank-or-newline-separated argument
5074 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5075 If @var{action-if-found} is given, it is additional shell code to
5076 execute when one of the functions is found.  You can give it a value of
5077 @samp{break} to break out of the loop on the first match.  If
5078 @var{action-if-not-found} is given, it is executed when one of the
5079 functions is not found.
5080 @end defmac
5082 @defmac AC_CHECK_FUNCS_ONCE (@var{function}@dots{})
5083 @acindex{CHECK_FUNCS_ONCE}
5084 @cvindex HAVE_@var{function}
5085 For each @var{function} enumerated in the blank-or-newline-separated argument
5086 list, define @code{HAVE_@var{function}} (in all capitals) if it is available.
5087 This is a once-only variant of @code{AC_CHECK_FUNCS}.  It generates the
5088 checking code at most once, so that @command{configure} is smaller and
5089 faster; but the checks cannot be conditionalized and are always done once,
5090 early during the @command{configure} run.
5091 @end defmac
5093 @sp 1
5095 Autoconf follows a philosophy that was formed over the years by those
5096 who have struggled for portability: isolate the portability issues in
5097 specific files, and then program as if you were in a Posix
5098 environment.  Some functions may be missing or unfixable, and your
5099 package must be ready to replace them.
5101 Suitable replacements for many such problem functions are available from
5102 Gnulib (@pxref{Gnulib}).
5104 @defmac AC_LIBOBJ (@var{function})
5105 @acindex{LIBOBJ}
5106 @ovindex LIBOBJS
5107 Specify that @samp{@var{function}.c} must be included in the executables
5108 to replace a missing or broken implementation of @var{function}.
5110 Technically, it adds @samp{@var{function}.$ac_objext} to the output
5111 variable @code{LIBOBJS} if it is not already in, and calls
5112 @code{AC_LIBSOURCE} for @samp{@var{function}.c}.  You should not
5113 directly change @code{LIBOBJS}, since this is not traceable.
5114 @end defmac
5116 @defmac AC_LIBSOURCE (@var{file})
5117 @acindex{LIBSOURCE}
5118 Specify that @var{file} might be needed to compile the project.  If you
5119 need to know what files might be needed by a @file{configure.ac}, you
5120 should trace @code{AC_LIBSOURCE}.  @var{file} must be a literal.
5122 This macro is called automatically from @code{AC_LIBOBJ}, but you must
5123 call it explicitly if you pass a shell variable to @code{AC_LIBOBJ}.  In
5124 that case, since shell variables cannot be traced statically, you must
5125 pass to @code{AC_LIBSOURCE} any possible files that the shell variable
5126 might cause @code{AC_LIBOBJ} to need.  For example, if you want to pass
5127 a variable @code{$foo_or_bar} to @code{AC_LIBOBJ} that holds either
5128 @code{"foo"} or @code{"bar"}, you should do:
5130 @example
5131 AC_LIBSOURCE([foo.c])
5132 AC_LIBSOURCE([bar.c])
5133 AC_LIBOBJ([$foo_or_bar])
5134 @end example
5136 @noindent
5137 There is usually a way to avoid this, however, and you are encouraged to
5138 simply call @code{AC_LIBOBJ} with literal arguments.
5140 Note that this macro replaces the obsolete @code{AC_LIBOBJ_DECL}, with
5141 slightly different semantics: the old macro took the function name,
5142 e.g., @code{foo}, as its argument rather than the file name.
5143 @end defmac
5145 @defmac AC_LIBSOURCES (@var{files})
5146 @acindex{LIBSOURCES}
5147 Like @code{AC_LIBSOURCE}, but accepts one or more @var{files} in a
5148 comma-separated M4 list.  Thus, the above example might be rewritten:
5150 @example
5151 AC_LIBSOURCES([foo.c, bar.c])
5152 AC_LIBOBJ([$foo_or_bar])
5153 @end example
5154 @end defmac
5156 @defmac AC_CONFIG_LIBOBJ_DIR (@var{directory})
5157 @acindex{CONFIG_LIBOBJ_DIR}
5158 Specify that @code{AC_LIBOBJ} replacement files are to be found in
5159 @var{directory}, a name relative to the top level of the
5160 source tree.  The replacement directory defaults to @file{.}, the top
5161 level directory, and the most typical value is @file{lib}, corresponding
5162 to @samp{AC_CONFIG_LIBOBJ_DIR([lib])}.
5164 @command{configure} might need to know the replacement directory for the
5165 following reasons: (i) some checks use the replacement files, (ii) some
5166 macros bypass broken system headers by installing links to the
5167 replacement headers (iii) when used in conjunction with Automake,
5168 within each makefile, @var{directory} is used as a relative path
5169 from @code{$(top_srcdir)} to each object named in @code{LIBOBJS} and
5170 @code{LTLIBOBJS}, etc.
5171 @end defmac
5173 @sp 1
5175 It is common to merely check for the existence of a function, and ask
5176 for its @code{AC_LIBOBJ} replacement if missing.  The following macro is
5177 a convenient shorthand.
5179 @defmac AC_REPLACE_FUNCS (@var{function}@dots{})
5180 @acindex{REPLACE_FUNCS}
5181 @cvindex HAVE_@var{function}
5182 @ovindex LIBOBJS
5183 Like @code{AC_CHECK_FUNCS}, but uses @samp{AC_LIBOBJ(@var{function})} as
5184 @var{action-if-not-found}.  You can declare your replacement function by
5185 enclosing the prototype in @samp{#ifndef HAVE_@var{function}}.  If the
5186 system has the function, it probably declares it in a header file you
5187 should be including, so you shouldn't redeclare it lest your declaration
5188 conflict.
5189 @end defmac
5191 @node Header Files
5192 @section Header Files
5193 @cindex Header, checking
5195 The following macros check for the presence of certain C header files.
5196 If there is no macro specifically defined to check for a header file you need,
5197 and you don't need to check for any special properties of
5198 it, then you can use one of the general header-file check macros.
5200 @menu
5201 * Header Portability::          Collected knowledge on common headers
5202 * Particular Headers::          Special handling to find certain headers
5203 * Generic Headers::             How to find other headers
5204 @end menu
5206 @node Header Portability
5207 @subsection Portability of Headers
5208 @cindex Portability of headers
5209 @cindex Header portability
5211 This section tries to collect knowledge about common headers, and the
5212 problems they cause.  By definition, this list always requires
5213 additions.  Please help us keeping it as complete as possible.
5215 @table @asis
5217 @item @file{limits.h}
5218 C99 says that @file{limits.h} defines @code{LLONG_MIN},
5219 @code{LLONG_MAX}, and @code{ULLONG_MAX}, but many almost-C99
5220 environments (e.g., default @acronym{GCC} 4.0.2 + glibc 2.4) do not
5221 define them.
5223 @item @file{inttypes.h} vs.@: @file{stdint.h}
5224 @hdrindex{inttypes.h}
5225 @hdrindex{stdint.h}
5226 The C99 standard says that @file{inttypes.h} includes
5227 @file{stdint.h}, so there's no need to include @file{stdint.h}
5228 separately in a standard environment.  Some implementations have
5229 @file{inttypes.h} but not @file{stdint.h} (e.g., Solaris 7), but we don't
5230 know of any implementation that has @file{stdint.h} but not
5231 @file{inttypes.h}.
5233 @item @file{linux/irda.h}
5234 @hdrindex{linux/irda.h}
5235 It requires @file{linux/types.h} and @file{sys/socket.h}.
5237 @item @file{linux/random.h}
5238 @hdrindex{linux/random.h}
5239 It requires @file{linux/types.h}.
5241 @item @file{net/if.h}
5242 @hdrindex{net/if.h}
5243 On Darwin, this file requires that @file{sys/socket.h} be included
5244 beforehand.  One should run:
5246 @example
5247 AC_CHECK_HEADERS([sys/socket.h])
5248 AC_CHECK_HEADERS([net/if.h], [], [],
5249 [#include <stdio.h>
5250 #ifdef STDC_HEADERS
5251 # include <stdlib.h>
5252 # include <stddef.h>
5253 #else
5254 # ifdef HAVE_STDLIB_H
5255 #  include <stdlib.h>
5256 # endif
5257 #endif
5258 #ifdef HAVE_SYS_SOCKET_H
5259 # include <sys/socket.h>
5260 #endif
5262 @end example
5264 @item @file{netinet/if_ether.h}
5265 @hdrindex{netinet/if_ether.h}
5266 On Darwin, this file requires that @file{stdio.h} and
5267 @file{sys/socket.h} be included beforehand.  One should run:
5269 @example
5270 AC_CHECK_HEADERS([sys/socket.h])
5271 AC_CHECK_HEADERS([netinet/if_ether.h], [], [],
5272 [#include <stdio.h>
5273 #ifdef STDC_HEADERS
5274 # include <stdlib.h>
5275 # include <stddef.h>
5276 #else
5277 # ifdef HAVE_STDLIB_H
5278 #  include <stdlib.h>
5279 # endif
5280 #endif
5281 #ifdef HAVE_SYS_SOCKET_H
5282 # include <sys/socket.h>
5283 #endif
5285 @end example
5287 @item @file{stdint.h}
5288 See above, item @file{inttypes.h} vs.@: @file{stdint.h}.
5290 @item @file{stdlib.h}
5291 @hdrindex{stdlib.h}
5292 On many systems (e.g., Darwin), @file{stdio.h} is a prerequisite.
5294 @item @file{sys/mount.h}
5295 @hdrindex{sys/mount.h}
5296 On Free@acronym{BSD} 4.8 on ia32 and using gcc version 2.95.4,
5297 @file{sys/params.h} is a prerequisite.
5299 @item @file{sys/ptem.h}
5300 @hdrindex{sys/ptem.h}
5301 On Solaris 8, @file{sys/stream.h} is a prerequisite.
5303 @item @file{sys/socket.h}
5304 @hdrindex{sys/socket.h}
5305 On Darwin, @file{stdlib.h} is a prerequisite.
5307 @item @file{sys/ucred.h}
5308 @hdrindex{sys/ucred.h}
5309 On Tru64 5.1, @file{sys/types.h} is a prerequisite.
5311 @item @file{X11/extensions/scrnsaver.h}
5312 @hdrindex{X11/extensions/scrnsaver.h}
5313 Using XFree86, this header requires @file{X11/Xlib.h}, which is probably
5314 so required that you might not even consider looking for it.
5316 @example
5317 AC_CHECK_HEADERS([X11/extensions/scrnsaver.h], [], [],
5318 [[#include <X11/Xlib.h>
5320 @end example
5321 @end table
5324 @node Particular Headers
5325 @subsection Particular Header Checks
5327 These macros check for particular system header files---whether they
5328 exist, and in some cases whether they declare certain symbols.
5330 @defmac AC_HEADER_ASSERT
5331 @acindex{HEADER_ASSERT}
5332 @cvindex NDEBUG
5333 @hdrindex{assert.h}
5334 Check whether to enable assertions in the style of @file{assert.h}.
5335 Assertions are enabled by default, but the user can override this by
5336 invoking @command{configure} with the @option{--disable-assert} option.
5337 @end defmac
5339 @anchor{AC_HEADER_DIRENT}
5340 @defmac AC_HEADER_DIRENT
5341 @acindex{HEADER_DIRENT}
5342 @cvindex HAVE_DIRENT_H
5343 @cvindex HAVE_NDIR_H
5344 @cvindex HAVE_SYS_DIR_H
5345 @cvindex HAVE_SYS_NDIR_H
5346 @hdrindex{dirent.h}
5347 @hdrindex{sys/ndir.h}
5348 @hdrindex{sys/dir.h}
5349 @hdrindex{ndir.h}
5350 Check for the following header files.  For the first one that is
5351 found and defines @samp{DIR}, define the listed C preprocessor macro:
5353 @multitable {@file{sys/ndir.h}} {@code{HAVE_SYS_NDIR_H}}
5354 @item @file{dirent.h}   @tab @code{HAVE_DIRENT_H}
5355 @item @file{sys/ndir.h} @tab @code{HAVE_SYS_NDIR_H}
5356 @item @file{sys/dir.h}  @tab @code{HAVE_SYS_DIR_H}
5357 @item @file{ndir.h}     @tab @code{HAVE_NDIR_H}
5358 @end multitable
5360 The directory-library declarations in your source code should look
5361 something like the following:
5363 @example
5364 @group
5365 #include <sys/types.h>
5366 #ifdef HAVE_DIRENT_H
5367 # include <dirent.h>
5368 # define NAMLEN(dirent) strlen ((dirent)->d_name)
5369 #else
5370 # define dirent direct
5371 # define NAMLEN(dirent) ((dirent)->d_namlen)
5372 # ifdef HAVE_SYS_NDIR_H
5373 #  include <sys/ndir.h>
5374 # endif
5375 # ifdef HAVE_SYS_DIR_H
5376 #  include <sys/dir.h>
5377 # endif
5378 # ifdef HAVE_NDIR_H
5379 #  include <ndir.h>
5380 # endif
5381 #endif
5382 @end group
5383 @end example
5385 Using the above declarations, the program would declare variables to be
5386 of type @code{struct dirent}, not @code{struct direct}, and would access
5387 the length of a directory entry name by passing a pointer to a
5388 @code{struct dirent} to the @code{NAMLEN} macro.
5390 This macro also checks for the SCO Xenix @file{dir} and @file{x} libraries.
5392 This macro is obsolescent, as all current systems with directory
5393 libraries have @code{<dirent.h>}.  New programs need not use this macro.
5395 Also see @code{AC_STRUCT_DIRENT_D_INO} and
5396 @code{AC_STRUCT_DIRENT_D_TYPE} (@pxref{Particular Structures}).
5397 @end defmac
5399 @anchor{AC_HEADER_MAJOR}
5400 @defmac AC_HEADER_MAJOR
5401 @acindex{HEADER_MAJOR}
5402 @cvindex MAJOR_IN_MKDEV
5403 @cvindex MAJOR_IN_SYSMACROS
5404 @hdrindex{sys/mkdev.h}
5405 @hdrindex{sys/sysmacros.h}
5406 If @file{sys/types.h} does not define @code{major}, @code{minor}, and
5407 @code{makedev}, but @file{sys/mkdev.h} does, define
5408 @code{MAJOR_IN_MKDEV}; otherwise, if @file{sys/sysmacros.h} does, define
5409 @code{MAJOR_IN_SYSMACROS}.
5410 @end defmac
5412 @defmac AC_HEADER_RESOLV
5413 @acindex{HEADER_RESOLV}
5414 @cvindex HAVE_RESOLV_H
5415 @hdrindex{resolv.h}
5416 Checks for header @file{resolv.h}, checking for prerequisites first.
5417 To properly use @file{resolv.h}, your code should contain something like
5418 the following:
5420 @verbatim
5421 #ifdef HAVE_SYS_TYPES_H
5422 #  include <sys/types.h>
5423 #endif
5424 #ifdef HAVE_NETINET_IN_H
5425 #  include <netinet/in.h>   /* inet_ functions / structs */
5426 #endif
5427 #ifdef HAVE_ARPA_NAMESER_H
5428 #  include <arpa/nameser.h> /* DNS HEADER struct */
5429 #endif
5430 #ifdef HAVE_NETDB_H
5431 #  include <netdb.h>
5432 #endif
5433 #include <resolv.h>
5434 @end verbatim
5435 @end defmac
5437 @anchor{AC_HEADER_STAT}
5438 @defmac AC_HEADER_STAT
5439 @acindex{HEADER_STAT}
5440 @cvindex STAT_MACROS_BROKEN
5441 @hdrindex{sys/stat.h}
5442 If the macros @code{S_ISDIR}, @code{S_ISREG}, etc.@: defined in
5443 @file{sys/stat.h} do not work properly (returning false positives),
5444 define @code{STAT_MACROS_BROKEN}.  This is the case on Tektronix UTekV,
5445 Amdahl UTS and Motorola System V/88.
5447 This macro is obsolescent, as no current systems have the bug.
5448 New programs need not use this macro.
5449 @end defmac
5451 @defmac AC_HEADER_STDBOOL
5452 @acindex{HEADER_STDBOOL}
5453 @cvindex HAVE_STDBOOL_H
5454 @cvindex HAVE__BOOL
5455 @hdrindex{stdbool.h}
5456 @hdrindex{system.h}
5457 If @file{stdbool.h} exists and conforms to C99, define
5458 @code{HAVE_STDBOOL_H} to 1; if the type @code{_Bool} is defined, define
5459 @code{HAVE__BOOL} to 1.  To fulfill the C99 requirements, your
5460 @file{system.h} could contain the following code:
5462 @verbatim
5463 #ifdef HAVE_STDBOOL_H
5464 # include <stdbool.h>
5465 #else
5466 # ifndef HAVE__BOOL
5467 #  ifdef __cplusplus
5468 typedef bool _Bool;
5469 #  else
5470 #   define _Bool signed char
5471 #  endif
5472 # endif
5473 # define bool _Bool
5474 # define false 0
5475 # define true 1
5476 # define __bool_true_false_are_defined 1
5477 #endif
5478 @end verbatim
5480 Alternatively you can use the @samp{stdbool} package of Gnulib
5481 (@pxref{Gnulib}); it packages the above code into a replacement header
5482 and contains a few other bells and whistles.
5484 @end defmac
5486 @anchor{AC_HEADER_STDC}
5487 @defmac AC_HEADER_STDC
5488 @acindex{HEADER_STDC}
5489 @cvindex STDC_HEADERS
5490 @hdrindex{stdlib.h}
5491 @hdrindex{stdarg.h}
5492 @hdrindex{string.h}
5493 @hdrindex{float.h}
5494 @hdrindex{ctype.h}
5495 Define @code{STDC_HEADERS} if the system has C header files
5496 conforming to @acronym{ANSI} C89 (@acronym{ISO} C90).
5497 Specifically, this macro checks for @file{stdlib.h}, @file{stdarg.h},
5498 @file{string.h}, and @file{float.h}; if the system has those, it
5499 probably has the rest of the C89 header files.  This macro also
5500 checks whether @file{string.h} declares @code{memchr} (and thus
5501 presumably the other @code{mem} functions), whether @file{stdlib.h}
5502 declare @code{free} (and thus presumably @code{malloc} and other related
5503 functions), and whether the @file{ctype.h} macros work on characters
5504 with the high bit set, as the C standard requires.
5506 If you use this macro, your code can refer to @code{STDC_HEADERS} to
5507 determine whether the system has conforming header files (and probably C
5508 library functions).
5510 This macro is obsolescent, as current systems have conforming header
5511 files.  New programs need not use this macro.
5513 @hdrindex{string.h}
5514 @hdrindex{strings.h}
5515 Nowadays @file{string.h} is part of the C standard and declares functions like
5516 @code{strcpy}, and @file{strings.h} is standardized by Posix and declares
5517 @acronym{BSD} functions like @code{bcopy}; but
5518 historically, string functions were a major sticking point in this area.
5519 If you still want to worry about portability to ancient systems without
5520 standard headers, there is so much variation
5521 that it is probably easier to declare the functions you use than to
5522 figure out exactly what the system header files declare.  Some ancient systems
5523 contained a mix of functions from the C standard and from @acronym{BSD};
5524 some were mostly standard but lacked @samp{memmove}; some defined the
5525 @acronym{BSD} functions as macros in @file{string.h} or
5526 @file{strings.h}; some had only the @acronym{BSD} functions but
5527 @file{string.h}; some declared the memory functions in @file{memory.h},
5528 some in @file{string.h}; etc.  It is probably sufficient to check for
5529 one string function and one memory function; if the library had the
5530 standard versions of those then it probably had most of the others.
5531 If you put the following in @file{configure.ac}:
5533 @example
5534 # This example is obsolescent.
5535 # Nowadays you can omit these macro calls.
5536 AC_HEADER_STDC
5537 AC_CHECK_FUNCS([strchr memcpy])
5538 @end example
5540 @noindent
5541 then, in your code, you can use declarations like this:
5543 @example
5544 @group
5545 /* This example is obsolescent.
5546    Nowadays you can just #include <string.h>.  */
5547 #ifdef STDC_HEADERS
5548 # include <string.h>
5549 #else
5550 # ifndef HAVE_STRCHR
5551 #  define strchr index
5552 #  define strrchr rindex
5553 # endif
5554 char *strchr (), *strrchr ();
5555 # ifndef HAVE_MEMCPY
5556 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
5557 #  define memmove(d, s, n) bcopy ((s), (d), (n))
5558 # endif
5559 #endif
5560 @end group
5561 @end example
5563 @noindent
5564 If you use a function like @code{memchr}, @code{memset}, @code{strtok},
5565 or @code{strspn}, which have no @acronym{BSD} equivalent, then macros don't
5566 suffice to port to ancient hosts; you must provide an implementation of
5567 each function.  An easy
5568 way to incorporate your implementations only when needed (since the ones
5569 in system C libraries may be hand optimized) is to, taking @code{memchr}
5570 for example, put it in @file{memchr.c} and use
5571 @samp{AC_REPLACE_FUNCS([memchr])}.
5572 @end defmac
5574 @defmac AC_HEADER_SYS_WAIT
5575 @acindex{HEADER_SYS_WAIT}
5576 @cvindex HAVE_SYS_WAIT_H
5577 @hdrindex{sys/wait.h}
5578 If @file{sys/wait.h} exists and is compatible with Posix, define
5579 @code{HAVE_SYS_WAIT_H}.  Incompatibility can occur if @file{sys/wait.h}
5580 does not exist, or if it uses the old @acronym{BSD} @code{union wait} instead
5581 of @code{int} to store a status value.  If @file{sys/wait.h} is not
5582 Posix compatible, then instead of including it, define the
5583 Posix macros with their usual interpretations.  Here is an
5584 example:
5586 @example
5587 @group
5588 #include <sys/types.h>
5589 #ifdef HAVE_SYS_WAIT_H
5590 # include <sys/wait.h>
5591 #endif
5592 #ifndef WEXITSTATUS
5593 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
5594 #endif
5595 #ifndef WIFEXITED
5596 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
5597 #endif
5598 @end group
5599 @end example
5601 @noindent
5602 This macro is obsolescent, as current systems are compatible with Posix.
5603 New programs need not use this macro.
5604 @end defmac
5606 @cvindex _POSIX_VERSION
5607 @hdrindex{unistd.h}
5608 @code{_POSIX_VERSION} is defined when @file{unistd.h} is included on
5609 Posix systems.  If there is no @file{unistd.h}, it is definitely
5610 not a Posix system.  However, some non-Posix systems do
5611 have @file{unistd.h}.
5613 The way to check whether the system supports Posix is:
5615 @example
5616 @group
5617 #ifdef HAVE_UNISTD_H
5618 # include <sys/types.h>
5619 # include <unistd.h>
5620 #endif
5622 #ifdef _POSIX_VERSION
5623 /* Code for Posix systems.  */
5624 #endif
5625 @end group
5626 @end example
5628 @anchor{AC_HEADER_TIME}
5629 @defmac AC_HEADER_TIME
5630 @acindex{HEADER_TIME}
5631 @cvindex TIME_WITH_SYS_TIME
5632 @hdrindex{time.h}
5633 @hdrindex{sys/time.h}
5634 If a program may include both @file{time.h} and @file{sys/time.h},
5635 define @code{TIME_WITH_SYS_TIME}.  On some ancient systems,
5636 @file{sys/time.h} included @file{time.h}, but @file{time.h} was not
5637 protected against multiple inclusion, so programs could not explicitly
5638 include both files.  This macro is useful in programs that use, for
5639 example, @code{struct timeval} as well as
5640 @code{struct tm}.  It is best used in conjunction with
5641 @code{HAVE_SYS_TIME_H}, which can be checked for using
5642 @code{AC_CHECK_HEADERS([sys/time.h])}.
5644 @example
5645 @group
5646 #ifdef TIME_WITH_SYS_TIME
5647 # include <sys/time.h>
5648 # include <time.h>
5649 #else
5650 # ifdef HAVE_SYS_TIME_H
5651 #  include <sys/time.h>
5652 # else
5653 #  include <time.h>
5654 # endif
5655 #endif
5656 @end group
5657 @end example
5659 @noindent
5660 This macro is obsolescent, as current systems can include both files
5661 when they exist.  New programs need not use this macro.
5662 @end defmac
5665 @defmac AC_HEADER_TIOCGWINSZ
5666 @acindex{HEADER_TIOCGWINSZ}
5667 @cvindex GWINSZ_IN_SYS_IOCTL
5668 @hdrindex{sys/ioctl.h}
5669 @hdrindex{termios.h}
5670 @c FIXME: I need clarifications from Jim.
5671 If the use of @code{TIOCGWINSZ} requires @file{<sys/ioctl.h>}, then
5672 define @code{GWINSZ_IN_SYS_IOCTL}.  Otherwise @code{TIOCGWINSZ} can be
5673 found in @file{<termios.h>}.
5675 Use:
5677 @example
5678 @group
5679 #ifdef HAVE_TERMIOS_H
5680 # include <termios.h>
5681 #endif
5683 #ifdef GWINSZ_IN_SYS_IOCTL
5684 # include <sys/ioctl.h>
5685 #endif
5686 @end group
5687 @end example
5688 @end defmac
5690 @node Generic Headers
5691 @subsection Generic Header Checks
5693 These macros are used to find system header files not covered by the
5694 ``particular'' test macros.  If you need to check the contents of a header
5695 as well as find out whether it is present, you have to write your own
5696 test for it (@pxref{Writing Tests}).
5698 @anchor{AC_CHECK_HEADER}
5699 @defmac AC_CHECK_HEADER (@var{header-file}, @ovar{action-if-found}, @
5700   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5701 @acindex{CHECK_HEADER}
5702 If the system header file @var{header-file} is compilable, execute shell
5703 commands @var{action-if-found}, otherwise execute
5704 @var{action-if-not-found}.  If you just want to define a symbol if the
5705 header file is available, consider using @code{AC_CHECK_HEADERS}
5706 instead.
5708 For compatibility issues with older versions of Autoconf, please read
5709 below.
5710 @end defmac
5712 @anchor{AC_CHECK_HEADERS}
5713 @defmac AC_CHECK_HEADERS (@var{header-file}@dots{}, @
5714   @ovar{action-if-found}, @ovar{action-if-not-found}, @
5715   @dvar{includes, default-includes})
5716 @acindex{CHECK_HEADERS}
5717 @cvindex HAVE_@var{header}
5718 For each given system header file @var{header-file} in the
5719 blank-separated argument list that exists, define
5720 @code{HAVE_@var{header-file}} (in all capitals).  If @var{action-if-found}
5721 is given, it is additional shell code to execute when one of the header
5722 files is found.  You can give it a value of @samp{break} to break out of
5723 the loop on the first match.  If @var{action-if-not-found} is given, it
5724 is executed when one of the header files is not found.
5726 For compatibility issues with older versions of Autoconf, please read
5727 below.
5728 @end defmac
5730 Previous versions of Autoconf merely checked whether the header was
5731 accepted by the preprocessor.  This was changed because the old test was
5732 inappropriate for typical uses.  Headers are typically used to compile,
5733 not merely to preprocess, and the old behavior sometimes accepted
5734 headers that clashed at compile-time.  If you need to check whether a
5735 header is preprocessable, you can use @code{AC_PREPROC_IFELSE}
5736 (@pxref{Running the Preprocessor}).
5738 This scheme, which improves the robustness of the test, also requires
5739 that you make sure that headers that must be included before the
5740 @var{header-file} be part of the @var{includes}, (@pxref{Default
5741 Includes}).  If looking for @file{bar.h}, which requires that
5742 @file{foo.h} be included before if it exists, we suggest the following
5743 scheme:
5745 @verbatim
5746 AC_CHECK_HEADERS([foo.h])
5747 AC_CHECK_HEADERS([bar.h], [], [],
5748 [#ifdef HAVE_FOO_H
5749 # include <foo.h>
5750 # endif
5752 @end verbatim
5754 The following variant generates smaller, faster @command{configure}
5755 files if you do not need the full power of @code{AC_CHECK_HEADERS}.
5757 @defmac AC_CHECK_HEADERS_ONCE (@var{header-file}@dots{})
5758 @acindex{CHECK_HEADERS_ONCE}
5759 @cvindex HAVE_@var{header}
5760 For each given system header file @var{header-file} in the
5761 blank-separated argument list that exists, define
5762 @code{HAVE_@var{header-file}} (in all capitals).
5763 This is a once-only variant of @code{AC_CHECK_HEADERS}.  It generates the
5764 checking code at most once, so that @command{configure} is smaller and
5765 faster; but the checks cannot be conditionalized and are always done once,
5766 early during the @command{configure} run.
5767 @end defmac
5769 @node Declarations
5770 @section Declarations
5771 @cindex Declaration, checking
5773 The following macros check for the declaration of variables and
5774 functions.  If there is no macro specifically defined to check for a
5775 symbol you need, then you can use the general macros (@pxref{Generic
5776 Declarations}) or, for more complex tests, you may use
5777 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5779 @menu
5780 * Particular Declarations::     Macros to check for certain declarations
5781 * Generic Declarations::        How to find other declarations
5782 @end menu
5784 @node Particular Declarations
5785 @subsection Particular Declaration Checks
5787 There are no specific macros for declarations.
5789 @node Generic Declarations
5790 @subsection Generic Declaration Checks
5792 These macros are used to find declarations not covered by the ``particular''
5793 test macros.
5795 @defmac AC_CHECK_DECL (@var{symbol}, @ovar{action-if-found}, @
5796   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5797 @acindex{CHECK_DECL}
5798 If @var{symbol} (a function, variable, or constant) is not declared in
5799 @var{includes} and a declaration is needed, run the shell commands
5800 @var{action-if-not-found}, otherwise @var{action-if-found}.  If no
5801 @var{includes} are specified, the default includes are used
5802 (@pxref{Default Includes}).
5804 This macro actually tests whether @var{symbol} is defined as a macro or
5805 can be used as an r-value, not whether it is really declared, because it
5806 is much safer to avoid
5807 introducing extra declarations when they are not needed.
5808 @end defmac
5810 @anchor{AC_CHECK_DECLS}
5811 @defmac AC_CHECK_DECLS (@var{symbols}, @ovar{action-if-found}, @
5812   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5813 @acindex{CHECK_DECLS}
5814 @cvindex HAVE_DECL_@var{symbol}
5815 For each of the @var{symbols} (@emph{comma}-separated list), define
5816 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5817 @var{symbol} is declared, otherwise to @samp{0}.  If
5818 @var{action-if-not-found} is given, it is additional shell code to
5819 execute when one of the function declarations is needed, otherwise
5820 @var{action-if-found} is executed.
5822 This macro uses an M4 list as first argument:
5823 @example
5824 AC_CHECK_DECLS([strdup])
5825 AC_CHECK_DECLS([strlen])
5826 AC_CHECK_DECLS([malloc, realloc, calloc, free])
5827 @end example
5829 Unlike the other @samp{AC_CHECK_*S} macros, when a @var{symbol} is not
5830 declared, @code{HAVE_DECL_@var{symbol}} is defined to @samp{0} instead
5831 of leaving @code{HAVE_DECL_@var{symbol}} undeclared.  When you are
5832 @emph{sure} that the check was performed, use
5833 @code{HAVE_DECL_@var{symbol}} in @code{#if}:
5835 @example
5836 #if !HAVE_DECL_SYMBOL
5837 extern char *symbol;
5838 #endif
5839 @end example
5841 @noindent
5842 If the test may have not been performed, however, because it is safer
5843 @emph{not} to declare a symbol than to use a declaration that conflicts
5844 with the system's one, you should use:
5846 @example
5847 #if defined HAVE_DECL_MALLOC && !HAVE_DECL_MALLOC
5848 void *malloc (size_t *s);
5849 #endif
5850 @end example
5852 @noindent
5853 You fall into the second category only in extreme situations: either
5854 your files may be used without being configured, or they are used during
5855 the configuration.  In most cases the traditional approach is enough.
5856 @end defmac
5858 @defmac AC_CHECK_DECLS_ONCE (@var{symbols})
5859 @acindex{CHECK_DECLS_ONCE}
5860 @cvindex HAVE_DECL_@var{symbol}
5861 For each of the @var{symbols} (@emph{comma}-separated list), define
5862 @code{HAVE_DECL_@var{symbol}} (in all capitals) to @samp{1} if
5863 @var{symbol} is declared in the default include files, otherwise to
5864 @samp{0}.  This is a once-only variant of @code{AC_CHECK_DECLS}.  It
5865 generates the checking code at most once, so that @command{configure} is
5866 smaller and faster; but the checks cannot be conditionalized and are
5867 always done once, early during the @command{configure} run.
5868 @end defmac
5871 @node Structures
5872 @section Structures
5873 @cindex Structure, checking
5875 The following macros check for the presence of certain members in C
5876 structures.  If there is no macro specifically defined to check for a
5877 member you need, then you can use the general structure-member macros
5878 (@pxref{Generic Structures}) or, for more complex tests, you may use
5879 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}).
5881 @menu
5882 * Particular Structures::       Macros to check for certain structure members
5883 * Generic Structures::          How to find other structure members
5884 @end menu
5886 @node Particular Structures
5887 @subsection Particular Structure Checks
5889 The following macros check for certain structures or structure members.
5891 @defmac AC_STRUCT_DIRENT_D_INO
5892 @acindex{STRUCT_DIRENT_D_INO}
5893 @cvindex HAVE_STRUCT_DIRENT_D_INO
5894 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5895 Headers}).  Then, if @code{struct dirent} contains a @code{d_ino}
5896 member, define @code{HAVE_STRUCT_DIRENT_D_INO}.
5898 @code{HAVE_STRUCT_DIRENT_D_INO} indicates only the presence of
5899 @code{d_ino}, not whether its contents are always reliable.
5900 Traditionally, a zero @code{d_ino} indicated a deleted directory entry,
5901 though current systems hide this detail from the user and never return
5902 zero @code{d_ino} values.
5903 Many current systems report an incorrect @code{d_ino} for a directory
5904 entry that is a mount point.
5905 @end defmac
5907 @defmac AC_STRUCT_DIRENT_D_TYPE
5908 @acindex{STRUCT_DIRENT_D_TYPE}
5909 @cvindex HAVE_STRUCT_DIRENT_D_TYPE
5910 Perform all the actions of @code{AC_HEADER_DIRENT} (@pxref{Particular
5911 Headers}).  Then, if @code{struct dirent} contains a @code{d_type}
5912 member, define @code{HAVE_STRUCT_DIRENT_D_TYPE}.
5913 @end defmac
5915 @anchor{AC_STRUCT_ST_BLOCKS}
5916 @defmac AC_STRUCT_ST_BLOCKS
5917 @acindex{STRUCT_ST_BLOCKS}
5918 @cvindex HAVE_STRUCT_STAT_ST_BLOCKS
5919 @cvindex HAVE_ST_BLOCKS
5920 @ovindex LIBOBJS
5921 If @code{struct stat} contains an @code{st_blocks} member, define
5922 @code{HAVE_STRUCT_STAT_ST_BLOCKS}.  Otherwise, require an
5923 @code{AC_LIBOBJ} replacement of @samp{fileblocks}.  The former name,
5924 @code{HAVE_ST_BLOCKS} is to be avoided, as its support will cease in the
5925 future.
5926 @end defmac
5928 @defmac AC_STRUCT_TM
5929 @acindex{STRUCT_TM}
5930 @cvindex TM_IN_SYS_TIME
5931 @hdrindex{time.h}
5932 @hdrindex{sys/time.h}
5933 If @file{time.h} does not define @code{struct tm}, define
5934 @code{TM_IN_SYS_TIME}, which means that including @file{sys/time.h}
5935 had better define @code{struct tm}.
5937 This macro is obsolescent, as @file{time.h} defines @code{struct tm} in
5938 current systems.  New programs need not use this macro.
5939 @end defmac
5941 @anchor{AC_STRUCT_TIMEZONE}
5942 @defmac AC_STRUCT_TIMEZONE
5943 @acindex{STRUCT_TIMEZONE}
5944 @cvindex HAVE_DECL_TZNAME
5945 @cvindex HAVE_STRUCT_TM_TM_ZONE
5946 @cvindex HAVE_TM_ZONE
5947 @cvindex HAVE_TZNAME
5948 Figure out how to get the current timezone.  If @code{struct tm} has a
5949 @code{tm_zone} member, define @code{HAVE_STRUCT_TM_TM_ZONE} (and the
5950 obsoleted @code{HAVE_TM_ZONE}).  Otherwise, if the external array
5951 @code{tzname} is found, define @code{HAVE_TZNAME}; if it is declared,
5952 define @code{HAVE_DECL_TZNAME}.
5953 @end defmac
5955 @node Generic Structures
5956 @subsection Generic Structure Checks
5958 These macros are used to find structure members not covered by the
5959 ``particular'' test macros.
5961 @defmac AC_CHECK_MEMBER (@var{aggregate}.@var{member}, @
5962   @ovar{action-if-found}, @ovar{action-if-not-found}, @
5963   @dvar{includes, default-includes})
5964 @acindex{CHECK_MEMBER}
5965 Check whether @var{member} is a member of the aggregate @var{aggregate}.
5966 If no @var{includes} are specified, the default includes are used
5967 (@pxref{Default Includes}).
5969 @example
5970 AC_CHECK_MEMBER([struct passwd.pw_gecos], [],
5971                 [AC_MSG_ERROR([We need `passwd.pw_gecos'!])],
5972                 [#include <pwd.h>])
5973 @end example
5975 You can use this macro for submembers:
5977 @example
5978 AC_CHECK_MEMBER(struct top.middle.bot)
5979 @end example
5980 @end defmac
5982 @anchor{AC_CHECK_MEMBERS}
5983 @defmac AC_CHECK_MEMBERS (@var{members}, @ovar{action-if-found}, @
5984   @ovar{action-if-not-found}, @dvar{includes, default-includes})
5985 @acindex{CHECK_MEMBERS}
5986 @cvindex HAVE_@var{aggregate}_@var{member}
5987 Check for the existence of each @samp{@var{aggregate}.@var{member}} of
5988 @var{members} using the previous macro.  When @var{member} belongs to
5989 @var{aggregate}, define @code{HAVE_@var{aggregate}_@var{member}} (in all
5990 capitals, with spaces and dots replaced by underscores).  If
5991 @var{action-if-found} is given, it is executed for each of the found
5992 members.  If @var{action-if-not-found} is given, it is executed for each
5993 of the members that could not be found.
5995 This macro uses M4 lists:
5996 @example
5997 AC_CHECK_MEMBERS([struct stat.st_rdev, struct stat.st_blksize])
5998 @end example
5999 @end defmac
6002 @node Types
6003 @section Types
6004 @cindex Types
6005 @cindex C types
6007 The following macros check for C types, either builtin or typedefs.  If
6008 there is no macro specifically defined to check for a type you need, and
6009 you don't need to check for any special properties of it, then you can
6010 use a general type-check macro.
6012 @menu
6013 * Particular Types::            Special handling to find certain types
6014 * Generic Types::               How to find other types
6015 @end menu
6017 @node Particular Types
6018 @subsection Particular Type Checks
6020 @hdrindex{sys/types.h}
6021 @hdrindex{stdlib.h}
6022 @hdrindex{stdint.h}
6023 @hdrindex{inttypes.h}
6024 These macros check for particular C types in @file{sys/types.h},
6025 @file{stdlib.h}, @file{stdint.h}, @file{inttypes.h} and others, if they
6026 exist.
6028 The Gnulib @code{stdint} module is an alternate way to define many of
6029 these symbols; it is useful if you prefer your code to assume a
6030 C99-or-better environment.  @xref{Gnulib}.
6032 @anchor{AC_TYPE_GETGROUPS}
6033 @defmac AC_TYPE_GETGROUPS
6034 @acindex{TYPE_GETGROUPS}
6035 @cvindex GETGROUPS_T
6036 Define @code{GETGROUPS_T} to be whichever of @code{gid_t} or @code{int}
6037 is the base type of the array argument to @code{getgroups}.
6038 @end defmac
6040 @defmac AC_TYPE_INT8_T
6041 @acindex{TYPE_INT8_T}
6042 @cvindex HAVE_INT8_T
6043 @cvindex int8_t
6044 If @file{stdint.h} or @file{inttypes.h} does not define the type
6045 @code{int8_t}, define @code{int8_t} to a signed
6046 integer type that is exactly 8 bits wide and that uses two's complement
6047 representation, if such a type exists.
6048 If you are worried about porting to hosts that lack such a type, you can
6049 use the results of this macro in C89-or-later code as follows:
6051 @example
6052 #if HAVE_STDINT_H
6053 # include <stdint.h>
6054 #endif
6055 #if defined INT8_MAX || defined int8_t
6056  @emph{code using int8_t}
6057 #else
6058  @emph{complicated alternative using >8-bit 'signed char'}
6059 #endif
6060 @end example
6061 @end defmac
6063 @defmac AC_TYPE_INT16_T
6064 @acindex{TYPE_INT16_T}
6065 @cvindex HAVE_INT16_T
6066 @cvindex int16_t
6067 This is like @code{AC_TYPE_INT8_T}, except for 16-bit integers.
6068 @end defmac
6070 @defmac AC_TYPE_INT32_T
6071 @acindex{TYPE_INT32_T}
6072 @cvindex HAVE_INT32_T
6073 @cvindex int32_t
6074 This is like @code{AC_TYPE_INT8_T}, except for 32-bit integers.
6075 @end defmac
6077 @defmac AC_TYPE_INT64_T
6078 @acindex{TYPE_INT64_T}
6079 @cvindex HAVE_INT64_T
6080 @cvindex int64_t
6081 This is like @code{AC_TYPE_INT8_T}, except for 64-bit integers.
6082 @end defmac
6084 @defmac AC_TYPE_INTMAX_T
6085 @acindex{TYPE_INTMAX_T}
6086 @cvindex HAVE_INTMAX_T
6087 @cvindex intmax_t
6088 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intmax_t},
6089 define @code{HAVE_INTMAX_T}.  Otherwise, define @code{intmax_t} to the
6090 widest signed integer type.
6091 @end defmac
6093 @defmac AC_TYPE_INTPTR_T
6094 @acindex{TYPE_INTPTR_T}
6095 @cvindex HAVE_INTPTR_T
6096 @cvindex intptr_t
6097 If @file{stdint.h} or @file{inttypes.h} defines the type @code{intptr_t},
6098 define @code{HAVE_INTPTR_T}.  Otherwise, define @code{intptr_t} to a
6099 signed integer type wide enough to hold a pointer, if such a type
6100 exists.
6101 @end defmac
6103 @defmac AC_TYPE_LONG_DOUBLE
6104 @acindex{TYPE_LONG_DOUBLE}
6105 @cvindex HAVE_LONG_DOUBLE
6106 If the C compiler supports a working @code{long double} type, define
6107 @code{HAVE_LONG_DOUBLE}.  The @code{long double} type might have the
6108 same range and precision as @code{double}.
6110 This macro is obsolescent, as current C compilers support @code{long
6111 double}.  New programs need not use this macro.
6112 @end defmac
6114 @defmac AC_TYPE_LONG_DOUBLE_WIDER
6115 @acindex{TYPE_LONG_DOUBLE_WIDER}
6116 @cvindex HAVE_LONG_DOUBLE_WIDER
6117 If the C compiler supports a working @code{long double} type with more
6118 range or precision than the @code{double} type, define
6119 @code{HAVE_LONG_DOUBLE_WIDER}.
6120 @end defmac
6122 @defmac AC_TYPE_LONG_LONG_INT
6123 @acindex{TYPE_LONG_LONG_INT}
6124 @cvindex HAVE_LONG_LONG_INT
6125 If the C compiler supports a working @code{long long int} type, define
6126 @code{HAVE_LONG_LONG_INT}.  This test also validates that the
6127 preprocessor can handle integers with the @samp{LL} suffix.
6128 @end defmac
6130 @defmac AC_TYPE_MBSTATE_T
6131 @acindex{TYPE_MBSTATE_T}
6132 @cvindex mbstate_t
6133 @hdrindex{wchar.h}
6134 Define @code{HAVE_MBSTATE_T} if @code{<wchar.h>} declares the
6135 @code{mbstate_t} type.  Also, define @code{mbstate_t} to be a type if
6136 @code{<wchar.h>} does not declare it.
6137 @end defmac
6139 @anchor{AC_TYPE_MODE_T}
6140 @defmac AC_TYPE_MODE_T
6141 @acindex{TYPE_MODE_T}
6142 @cvindex mode_t
6143 Define @code{mode_t} to a suitable type, if standard headers do not
6144 define it.
6145 @end defmac
6147 @anchor{AC_TYPE_OFF_T}
6148 @defmac AC_TYPE_OFF_T
6149 @acindex{TYPE_OFF_T}
6150 @cvindex off_t
6151 Define @code{off_t} to a suitable type, if standard headers do not
6152 define it.
6153 @end defmac
6155 @anchor{AC_TYPE_PID_T}
6156 @defmac AC_TYPE_PID_T
6157 @acindex{TYPE_PID_T}
6158 @cvindex pid_t
6159 Define @code{pid_t} to a suitable type, if standard headers do not
6160 define it.
6161 @end defmac
6163 @anchor{AC_TYPE_SIGNAL}
6164 @defmac AC_TYPE_SIGNAL
6165 @acindex{TYPE_SIGNAL}
6166 @cvindex RETSIGTYPE
6167 @hdrindex{signal.h}
6168 If @file{signal.h} declares @code{signal} as returning a pointer to a
6169 function returning @code{void}, define @code{RETSIGTYPE} to be
6170 @code{void}; otherwise, define it to be @code{int}.
6172 Define signal handlers as returning type @code{RETSIGTYPE}:
6174 @example
6175 @group
6176 RETSIGTYPE
6177 hup_handler ()
6179 @dots{}
6181 @end group
6182 @end example
6183 @end defmac
6185 @anchor{AC_TYPE_SIZE_T}
6186 @defmac AC_TYPE_SIZE_T
6187 @acindex{TYPE_SIZE_T}
6188 @cvindex size_t
6189 Define @code{size_t} to a suitable type, if standard headers do not
6190 define it.
6191 @end defmac
6193 @defmac AC_TYPE_SSIZE_T
6194 @acindex{TYPE_SSIZE_T}
6195 @cvindex ssize_t
6196 Define @code{ssize_t} to a suitable type, if standard headers do not
6197 define it.
6198 @end defmac
6200 @anchor{AC_TYPE_UID_T}
6201 @defmac AC_TYPE_UID_T
6202 @acindex{TYPE_UID_T}
6203 @cvindex uid_t
6204 @cvindex gid_t
6205 Define @code{uid_t} and @code{gid_t} to suitable types, if standard
6206 headers do not define them.
6207 @end defmac
6209 @defmac AC_TYPE_UINT8_T
6210 @acindex{TYPE_UINT8_T}
6211 @cvindex HAVE_UINT8_T
6212 @cvindex uint8_t
6213 If @file{stdint.h} or @file{inttypes.h} does not define the type
6214 @code{uint8_t}, define @code{uint8_t} to an
6215 unsigned integer type that is exactly 8 bits wide, if such a type
6216 exists.
6217 This is like @code{AC_TYPE_INT8_T}, except for unsigned integers.
6218 @end defmac
6220 @defmac AC_TYPE_UINT16_T
6221 @acindex{TYPE_UINT16_T}
6222 @cvindex HAVE_UINT16_T
6223 @cvindex uint16_t
6224 This is like @code{AC_TYPE_UINT8_T}, except for 16-bit integers.
6225 @end defmac
6227 @defmac AC_TYPE_UINT32_T
6228 @acindex{TYPE_UINT32_T}
6229 @cvindex HAVE_UINT32_T
6230 @cvindex uint32_t
6231 This is like @code{AC_TYPE_UINT8_T}, except for 32-bit integers.
6232 @end defmac
6234 @defmac AC_TYPE_UINT64_T
6235 @acindex{TYPE_UINT64_T}
6236 @cvindex HAVE_UINT64_T
6237 @cvindex uint64_t
6238 This is like @code{AC_TYPE_UINT8_T}, except for 64-bit integers.
6239 @end defmac
6241 @defmac AC_TYPE_UINTMAX_T
6242 @acindex{TYPE_UINTMAX_T}
6243 @cvindex HAVE_UINTMAX_T
6244 @cvindex uintmax_t
6245 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintmax_t},
6246 define @code{HAVE_UINTMAX_T}.  Otherwise, define @code{uintmax_t} to the
6247 widest unsigned integer type.
6248 @end defmac
6250 @defmac AC_TYPE_UINTPTR_T
6251 @acindex{TYPE_UINTPTR_T}
6252 @cvindex HAVE_UINTPTR_T
6253 @cvindex uintptr_t
6254 If @file{stdint.h} or @file{inttypes.h} defines the type @code{uintptr_t},
6255 define @code{HAVE_UINTPTR_T}.  Otherwise, define @code{uintptr_t} to an
6256 unsigned integer type wide enough to hold a pointer, if such a type
6257 exists.
6258 @end defmac
6260 @defmac AC_TYPE_UNSIGNED_LONG_LONG_INT
6261 @acindex{TYPE_UNSIGNED_LONG_LONG_INT}
6262 @cvindex HAVE_UNSIGNED_LONG_LONG_INT
6263 If the C compiler supports a working @code{unsigned long long int} type,
6264 define @code{HAVE_UNSIGNED_LONG_LONG_INT}.  This test also validates
6265 that the preprocessor can handle integers with the @samp{ULL} suffix.
6266 However, portable code cannot mix @code{unsigned long int} and
6267 @code{unsigned long long int} types in preprocessor expressions,
6268 since the @acronym{HP-UX} 11.00 preprocessor does not use consistent
6269 promotion rules.
6270 @end defmac
6272 @node Generic Types
6273 @subsection Generic Type Checks
6275 These macros are used to check for types not covered by the ``particular''
6276 test macros.
6278 @defmac AC_CHECK_TYPE (@var{type}, @ovar{action-if-found}, @
6279   @ovar{action-if-not-found}, @dvar{includes, default-includes})
6280 @acindex{CHECK_TYPE}
6281 Check whether @var{type} is defined.  It may be a compiler builtin type
6282 or defined by the @var{includes} (@pxref{Default Includes}).
6284 In C, @var{type} must be a type-name, so that the expression @samp{sizeof
6285 (@var{type})} is valid (but @samp{sizeof ((@var{type}))} is not).  The
6286 same test is applied when compiling for C++, which means that in C++
6287 @var{type} should be a type-id and should not be an anonymous
6288 @samp{struct} or @samp{union}.
6289 @end defmac
6292 @defmac AC_CHECK_TYPES (@var{types}, @ovar{action-if-found}, @
6293   @ovar{action-if-not-found}, @dvar{includes, default-includes})
6294 @acindex{CHECK_TYPES}
6295 @cvindex HAVE_@var{type}
6296 For each @var{type} of the @var{types} that is defined, define
6297 @code{HAVE_@var{type}} (in all capitals).  Each @var{type} must follow
6298 the rules of @code{AC_CHECK_TYPE}.  If no @var{includes} are
6299 specified, the default includes are used (@pxref{Default Includes}).  If
6300 @var{action-if-found} is given, it is additional shell code to execute
6301 when one of the types is found.  If @var{action-if-not-found} is given,
6302 it is executed when one of the types is not found.
6304 This macro uses M4 lists:
6305 @example
6306 AC_CHECK_TYPES([ptrdiff_t])
6307 AC_CHECK_TYPES([unsigned long long int, uintmax_t])
6308 @end example
6310 @end defmac
6312 Autoconf, up to 2.13, used to provide to another version of
6313 @code{AC_CHECK_TYPE}, broken by design.  In order to keep backward
6314 compatibility, a simple heuristic, quite safe but not totally, is
6315 implemented.  In case of doubt, read the documentation of the former
6316 @code{AC_CHECK_TYPE}, see @ref{Obsolete Macros}.
6319 @node Compilers and Preprocessors
6320 @section Compilers and Preprocessors
6321 @cindex Compilers
6322 @cindex Preprocessors
6324 @ovindex EXEEXT
6325 All the tests for compilers (@code{AC_PROG_CC}, @code{AC_PROG_CXX},
6326 @code{AC_PROG_F77}) define the output variable @code{EXEEXT} based on
6327 the output of the compiler, typically to the empty string if
6328 Posix and @samp{.exe} if a @acronym{DOS} variant.
6330 @ovindex OBJEXT
6331 They also define the output variable @code{OBJEXT} based on the
6332 output of the compiler, after @file{.c} files have been excluded, typically
6333 to @samp{o} if Posix, @samp{obj} if a @acronym{DOS} variant.
6335 If the compiler being used does not produce executables, the tests fail.  If
6336 the executables can't be run, and cross-compilation is not enabled, they
6337 fail too.  @xref{Manual Configuration}, for more on support for cross
6338 compiling.
6340 @menu
6341 * Specific Compiler Characteristics::  Some portability issues
6342 * Generic Compiler Characteristics::  Language independent tests and features
6343 * C Compiler::                  Checking its characteristics
6344 * C++ Compiler::                Likewise
6345 * Objective C Compiler::        Likewise
6346 * Erlang Compiler and Interpreter::  Likewise
6347 * Fortran Compiler::            Likewise
6348 @end menu
6350 @node Specific Compiler Characteristics
6351 @subsection Specific Compiler Characteristics
6353 Some compilers exhibit different behaviors.
6355 @table @asis
6356 @item Static/Dynamic Expressions
6357 Autoconf relies on a trick to extract one bit of information from the C
6358 compiler: using negative array sizes.  For instance the following
6359 excerpt of a C source demonstrates how to test whether @samp{int} objects are 4
6360 bytes wide:
6362 @example
6363 static int test_array[sizeof (int) == 4 ? 1 : -1];
6364 @end example
6366 @noindent
6367 To our knowledge, there is a single compiler that does not support this
6368 trick: the @acronym{HP} C compilers (the real ones, not only the
6369 ``bundled'') on @acronym{HP-UX} 11.00.
6370 They incorrectly reject the above program with the diagnostic
6371 ``Variable-length arrays cannot have static storage.''
6372 This bug comes from @acronym{HP} compilers' mishandling of @code{sizeof (int)},
6373 not from the @code{? 1 : -1}, and
6374 Autoconf works around this problem by casting @code{sizeof (int)} to
6375 @code{long int} before comparing it.
6376 @end table
6378 @node Generic Compiler Characteristics
6379 @subsection Generic Compiler Characteristics
6381 @anchor{AC_CHECK_SIZEOF}
6382 @defmac AC_CHECK_SIZEOF (@var{type-or-expr}, @ovar{unused}, @
6383   @dvar{includes, default-includes})
6384 @acindex{CHECK_SIZEOF}
6385 @cvindex SIZEOF_@var{type-or-expr}
6386 Define @code{SIZEOF_@var{type-or-expr}} (@pxref{Standard Symbols}) to be
6387 the size in bytes of @var{type-or-expr}, which may be either a type or
6388 an expression returning a value that has a size.  If the expression
6389 @samp{sizeof (@var{type-or-expr})} is invalid, the result is 0.  If no
6390 @var{includes} are specified, the default includes are used
6391 (@pxref{Default Includes}).
6393 This macro now works even when cross-compiling.  The @var{unused}
6394 argument was used when cross-compiling.
6396 For example, the call
6398 @example
6399 AC_CHECK_SIZEOF([int *])
6400 @end example
6402 @noindent
6403 defines @code{SIZEOF_INT_P} to be 8 on DEC Alpha AXP systems.
6404 @end defmac
6406 @defmac AC_CHECK_ALIGNOF (@var{type}, @dvar{includes, default-includes})
6407 @acindex{CHECK_ALIGNOF}
6408 @cvindex ALIGNOF_@var{type}
6409 Define @code{ALIGNOF_@var{type}} (@pxref{Standard Symbols}) to be the
6410 alignment in bytes of @var{type}.  @samp{@var{type} y;} must be valid as
6411 a structure member declaration.  If @samp{type} is unknown, the result
6412 is 0.  If no @var{includes} are specified, the default includes are used
6413 (@pxref{Default Includes}).
6414 @end defmac
6416 @defmac AC_COMPUTE_INT (@var{var}, @var{expression}, @
6417   @dvar{includes, default-includes}, @ovar{action-if-fails})
6418 @acindex{COMPUTE_INT}
6419 Store into the shell variable @var{var} the value of the integer
6420 @var{expression}.  The
6421 value should fit in an initializer in a C variable of type @code{signed
6422 long}.  To support cross compilation (in which case, the macro only works on
6423 hosts that use twos-complement arithmetic), it should be possible to evaluate
6424 the expression at compile-time.  If no @var{includes} are specified, the
6425 default includes are used (@pxref{Default Includes}).
6427 Execute @var{action-if-fails} if the value cannot be determined correctly.
6428 @end defmac
6430 @defmac AC_LANG_WERROR
6431 @acindex{LANG_WERROR}
6432 Normally Autoconf ignores warnings generated by the compiler, linker, and
6433 preprocessor.  If this macro is used, warnings count as fatal
6434 errors for the current language.  This macro is useful when the
6435 results of configuration are used where warnings are unacceptable; for
6436 instance, if parts of a program are built with the @acronym{GCC}
6437 @option{-Werror}
6438 option.  If the whole program is built using @option{-Werror} it is
6439 often simpler to put @option{-Werror} in the compiler flags (@code{CFLAGS},
6440 etc.).
6441 @end defmac
6443 @defmac AC_OPENMP
6444 @acindex{OPENMP}
6445 @cvindex _OPENMP
6446 @ovindex OPENMP_CFLAGS
6447 @ovindex OPENMP_CXXFLAGS
6448 @ovindex OPENMP_FFLAGS
6449 @ovindex OPENMP_FCFLAGS
6450 OpenMP (@url{http://www.openmp.org/}) specifies extensions of C, C++,
6451 and Fortran that simplify optimization of shared memory parallelism,
6452 which is a common problem on multicore CPUs.
6454 If the current language is C, the macro @code{AC_OPENMP} sets the
6455 variable @code{OPENMP_CFLAGS} to the C compiler flags needed for
6456 supporting OpenMP@.  @code{OPENMP_CFLAGS} is set to empty if the
6457 compiler already supports OpenMP, if it has no way to activate OpenMP
6458 support, or if the user rejects OpenMP support by invoking
6459 @samp{configure} with the @samp{--disable-openmp} option.
6461 @code{OPENMP_CFLAGS} needs to be used when compiling programs, when
6462 preprocessing program source, and when linking programs.  Therefore you
6463 need to add @code{$(OPENMP_CFLAGS)} to the @code{CFLAGS} of C programs
6464 that use OpenMP@.  If you preprocess OpenMP-specific C code, you also
6465 need to add @code{$(OPENMP_CFLAGS)} to @code{CPPFLAGS}.  The presence of
6466 OpenMP support is revealed at compile time by the preprocessor macro
6467 @code{_OPENMP}.
6469 Linking a program with @code{OPENMP_CFLAGS} typically adds one more
6470 shared library to the program's dependencies, so its use is recommended
6471 only on programs that actually require OpenMP.
6473 If the current language is C++, @code{AC_OPENMP} sets the variable
6474 @code{OPENMP_CXXFLAGS}, suitably for the C++ compiler.  The same remarks
6475 hold as for C.
6477 If the current language is Fortran 77 or Fortran, @code{AC_OPENMP} sets
6478 the variable @code{OPENMP_FFLAGS} or @code{OPENMP_FCFLAGS},
6479 respectively.  Similar remarks as for C hold, except that
6480 @code{CPPFLAGS} is not used for Fortran, and no preprocessor macro
6481 signals OpenMP support.
6482 @end defmac
6484 @node C Compiler
6485 @subsection C Compiler Characteristics
6487 The following macros provide ways to find and exercise a C Compiler.
6488 There are a few constructs that ought to be avoided, but do not deserve
6489 being checked for, since they can easily be worked around.
6491 @table @asis
6492 @item Don't use lines containing solitary backslashes
6493 They tickle a bug in the @acronym{HP-UX} C compiler (checked on
6494 @acronym{HP-UX} 10.20,
6495 11.00, and 11i).  When given the following source:
6497 @example
6498 #ifdef __STDC__
6500 * A comment with backslash-newlines in it.  %@{ %@} *\
6503 char str[] = "\\
6504 " A string with backslash-newlines in it %@{ %@} \\
6506 char apostrophe = '\\
6510 #endif
6511 @end example
6513 @noindent
6514 the compiler incorrectly fails with the diagnostics ``Non-terminating
6515 comment at end of file'' and ``Missing @samp{#endif} at end of file.''
6516 Removing the lines with solitary backslashes solves the problem.
6518 @item Don't compile several files at once if output matters to you
6519 Some compilers, such as @acronym{HP}'s, report names of files being
6520 compiled when given more than one file operand.  For instance:
6522 @example
6523 $ @kbd{cc a.c b.c}
6524 a.c:
6525 b.c:
6526 @end example
6528 @noindent
6529 This can cause problems if you observe the output of the compiler to
6530 detect failures.  Invoking @samp{cc -c a.c && cc -c b.c && cc -o c a.o
6531 b.o} solves the issue.
6533 @item Don't rely on @code{#error} failing
6534 The @sc{irix} C compiler does not fail when #error is preprocessed; it
6535 simply emits a diagnostic and continues, exiting successfully.  So,
6536 instead of an error directive like @code{#error "Unsupported word size"}
6537 it is more portable to use an invalid directive like @code{#Unsupported
6538 word size} in Autoconf tests.  In ordinary source code, @code{#error} is
6539 OK, since installers with inadequate compilers like @sc{irix} can simply
6540 examine these compilers' diagnostic output.
6542 @item Don't rely on correct @code{#line} support
6543 On Solaris, @command{c89} (at least Sun C 5.3 through 5.8)
6544 diagnoses @code{#line} directives whose line
6545 numbers are greater than 32767.  Nothing in Posix
6546 makes this invalid.  That is why Autoconf stopped issuing
6547 @code{#line} directives.
6548 @end table
6550 @defmac AC_PROG_CC (@ovar{compiler-search-list})
6551 @acindex{PROG_CC}
6552 @ovindex CC
6553 @ovindex CFLAGS
6554 Determine a C compiler to use.  If @code{CC} is not already set in the
6555 environment, check for @code{gcc} and @code{cc}, then for other C
6556 compilers.  Set output variable @code{CC} to the name of the compiler
6557 found.
6559 This macro may, however, be invoked with an optional first argument
6560 which, if specified, must be a blank-separated list of C compilers to
6561 search for.  This just gives the user an opportunity to specify an
6562 alternative search list for the C compiler.  For example, if you didn't
6563 like the default order, then you could invoke @code{AC_PROG_CC} like
6564 this:
6566 @example
6567 AC_PROG_CC([gcc cl cc])
6568 @end example
6570 If the C compiler does not handle function prototypes correctly by
6571 default, try to add an option to output variable @code{CC} to make it
6572 so.  This macro tries various options that select standard-conformance
6573 modes on various systems.
6575 After calling this macro you can check whether the C compiler has been
6576 set to accept @acronym{ANSI} C89 (@acronym{ISO} C90); if not, the shell
6577 variable
6578 @code{ac_cv_prog_cc_c89} is set to @samp{no}.  See also
6579 @code{AC_C_PROTOTYPES} below.
6581 If using the @acronym{GNU} C compiler, set shell variable @code{GCC} to
6582 @samp{yes}.  If output variable @code{CFLAGS} was not already set, set
6583 it to @option{-g -O2} for the @acronym{GNU} C compiler (@option{-O2} on systems
6584 where @acronym{GCC} does not accept @option{-g}), or @option{-g} for
6585 other compilers.
6586 @end defmac
6588 @anchor{AC_PROG_CC_C_O}
6589 @defmac AC_PROG_CC_C_O
6590 @acindex{PROG_CC_C_O}
6591 @cvindex NO_MINUS_C_MINUS_O
6592 If the C compiler does not accept the @option{-c} and @option{-o} options
6593 simultaneously, define @code{NO_MINUS_C_MINUS_O}.  This macro actually
6594 tests both the compiler found by @code{AC_PROG_CC}, and, if different,
6595 the first @code{cc} in the path.  The test fails if one fails.  This
6596 macro was created for @acronym{GNU} Make to choose the default C compilation
6597 rule.
6598 @end defmac
6601 @defmac AC_PROG_CPP
6602 @acindex{PROG_CPP}
6603 @ovindex CPP
6604 Set output variable @code{CPP} to a command that runs the
6605 C preprocessor.  If @samp{$CC -E} doesn't work, @file{/lib/cpp} is used.
6606 It is only portable to run @code{CPP} on files with a @file{.c}
6607 extension.
6609 Some preprocessors don't indicate missing include files by the error
6610 status.  For such preprocessors an internal variable is set that causes
6611 other macros to check the standard error from the preprocessor and
6612 consider the test failed if any warnings have been reported.
6613 For most preprocessors, though, warnings do not cause include-file
6614 tests to fail unless @code{AC_PROG_CPP_WERROR} is also specified.
6615 @end defmac
6617 @defmac AC_PROG_CPP_WERROR
6618 @acindex{PROG_CPP_WERROR}
6619 @ovindex CPP
6620 This acts like @code{AC_PROG_CPP}, except it treats warnings from the
6621 preprocessor as errors even if the preprocessor exit status indicates
6622 success.  This is useful for avoiding headers that generate mandatory
6623 warnings, such as deprecation notices.
6624 @end defmac
6627 The following macros check for C compiler or machine architecture
6628 features.  To check for characteristics not listed here, use
6629 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
6630 @code{AC_RUN_IFELSE} (@pxref{Runtime}).
6632 @defmac AC_PROG_CC_STDC
6633 @acindex{PROG_CC_STDC}
6634 If the C compiler cannot compile @acronym{ISO} Standard C (currently
6635 C99), try to add an option to output variable @code{CC} to make it work.
6636 If the compiler does not support C99, fall back to supporting
6637 @acronym{ANSI} C89 (@acronym{ISO} C90).
6639 After calling this macro you can check whether the C compiler has been
6640 set to accept Standard C; if not, the shell variable
6641 @code{ac_cv_prog_cc_stdc} is set to @samp{no}.
6642 @end defmac
6644 @defmac AC_PROG_CC_C89
6645 @acindex{PROG_CC_C89}
6646 If the C compiler is not in @acronym{ANSI} C89 (@acronym{ISO} C90) mode by
6647 default, try to add an option to output variable @code{CC} to make it
6648 so.  This macro tries various options that select @acronym{ANSI} C89 on
6649 some system or another.  It considers the compiler to be in
6650 @acronym{ANSI} C89 mode if it handles function prototypes correctly.
6652 After calling this macro you can check whether the C compiler has been
6653 set to accept @acronym{ANSI} C89; if not, the shell variable
6654 @code{ac_cv_prog_cc_c89} is set to @samp{no}.
6656 This macro is called automatically by @code{AC_PROG_CC}.
6657 @end defmac
6659 @defmac AC_PROG_CC_C99
6660 @acindex{PROG_CC_C99}
6661 If the C compiler is not in C99 mode by default, try to add an
6662 option to output variable @code{CC} to make it so.  This macro tries
6663 various options that select C99 on some system or another.  It
6664 considers the compiler to be in C99 mode if it handles @code{_Bool},
6665 @code{//} comments, flexible array members, @code{inline}, signed and
6666 unsigned @code{long long int}, mixed code and declarations, named
6667 initialization of structs,
6668 @code{restrict}, @code{va_copy}, varargs macros, variable declarations
6669 in @code{for} loops, and variable length arrays.
6671 After calling this macro you can check whether the C compiler has been
6672 set to accept C99; if not, the shell variable
6673 @code{ac_cv_prog_cc_c99} is set to @samp{no}.
6674 @end defmac
6676 @defmac AC_C_BACKSLASH_A
6677 @acindex{C_BACKSLASH_A}
6678 @cvindex HAVE_C_BACKSLASH_A
6679 Define @samp{HAVE_C_BACKSLASH_A} to 1 if the C compiler understands
6680 @samp{\a}.
6682 This macro is obsolescent, as current C compilers understand @samp{\a}.
6683 New programs need not use this macro.
6684 @end defmac
6686 @anchor{AC_C_BIGENDIAN}
6687 @defmac AC_C_BIGENDIAN (@ovar{action-if-true}, @ovar{action-if-false}, @
6688   @ovar{action-if-unknown}, @ovar{action-if-universal})
6689 @acindex{C_BIGENDIAN}
6690 @cvindex WORDS_BIGENDIAN
6691 @cindex Endianness
6692 If words are stored with the most significant byte first (like Motorola
6693 and SPARC CPUs), execute @var{action-if-true}.  If words are stored with
6694 the least significant byte first (like Intel and VAX CPUs), execute
6695 @var{action-if-false}.
6697 This macro runs a test-case if endianness cannot be determined from the
6698 system header files.  When cross-compiling, the test-case is not run but
6699 grep'ed for some magic values.  @var{action-if-unknown} is executed if
6700 the latter case fails to determine the byte sex of the host system.
6702 In some cases a single run of a compiler can generate code for multiple
6703 architectures.  This can happen, for example, when generating Mac OS X
6704 universal binary files, which work on both PowerPC and Intel
6705 architectures.  In this case, the different variants might be for
6706 different architectures whose endiannesses differ.  If
6707 @command{configure} detects this, it executes @var{action-if-universal}
6708 instead of @var{action-if-unknown}.
6710 The default for @var{action-if-true} is to define
6711 @samp{WORDS_BIGENDIAN}.  The default for @var{action-if-false} is to do
6712 nothing.  The default for @var{action-if-unknown} is to
6713 abort configure and tell the installer how to bypass this test.
6714 And finally, the default for @var{action-if-universal} is to define
6715 @samp{WORDS_BIGENDIAN} or not, depending on the architecture that the
6716 code is being generated for.
6718 If you use this macro without specifying @var{action-if-universal}, you
6719 should also use @code{AC_CONFIG_HEADERS}; otherwise
6720 @samp{WORDS_BIGENDIAN} may be set incorrectly for Mac OS X universal
6721 binary files.
6722 @end defmac
6724 @anchor{AC_C_CONST}
6725 @defmac AC_C_CONST
6726 @acindex{C_CONST}
6727 @cvindex const
6728 If the C compiler does not fully support the @code{const} keyword,
6729 define @code{const} to be empty.  Some C compilers that do
6730 not define @code{__STDC__} do support @code{const}; some compilers that
6731 define @code{__STDC__} do not completely support @code{const}.  Programs
6732 can simply use @code{const} as if every C compiler supported it; for
6733 those that don't, the makefile or configuration header file
6734 defines it as empty.
6736 Occasionally installers use a C++ compiler to compile C code, typically
6737 because they lack a C compiler.  This causes problems with @code{const},
6738 because C and C++ treat @code{const} differently.  For example:
6740 @example
6741 const int foo;
6742 @end example
6744 @noindent
6745 is valid in C but not in C++.  These differences unfortunately cannot be
6746 papered over by defining @code{const} to be empty.
6748 If @command{autoconf} detects this situation, it leaves @code{const} alone,
6749 as this generally yields better results in practice.  However, using a
6750 C++ compiler to compile C code is not recommended or supported, and
6751 installers who run into trouble in this area should get a C compiler
6752 like @acronym{GCC} to compile their C code.
6754 This macro is obsolescent, as current C compilers support @code{const}.
6755 New programs need not use this macro.
6756 @end defmac
6758 @defmac AC_C_RESTRICT
6759 @acindex{C_RESTRICT}
6760 @cvindex restrict
6761 If the C compiler recognizes a variant spelling for the @code{restrict}
6762 keyword (@code{__restrict}, @code{__restrict__}, or @code{_Restrict}),
6763 then define @code{restrict} to that; this is more likely to do the right
6764 thing with compilers that support language variants where plain
6765 @code{restrict} is not a keyword.  Otherwise, if the C compiler
6766 recognizes the @code{restrict} keyword, don't do anything.
6767 Otherwise, define @code{restrict} to be empty.
6768 Thus, programs may simply use @code{restrict} as if every C compiler
6769 supported it; for those that do not, the makefile
6770 or configuration header defines it away.
6772 Although support in C++ for the @code{restrict} keyword is not
6773 required, several C++ compilers do accept the keyword.
6774 This macro works for them, too.
6775 @end defmac
6777 @defmac AC_C_VOLATILE
6778 @acindex{C_VOLATILE}
6779 @cvindex volatile
6780 If the C compiler does not understand the keyword @code{volatile},
6781 define @code{volatile} to be empty.  Programs can simply use
6782 @code{volatile} as if every C compiler supported it; for those that do
6783 not, the makefile or configuration header defines it as
6784 empty.
6786 If the correctness of your program depends on the semantics of
6787 @code{volatile}, simply defining it to be empty does, in a sense, break
6788 your code.  However, given that the compiler does not support
6789 @code{volatile}, you are at its mercy anyway.  At least your
6790 program compiles, when it wouldn't before.
6791 @xref{Volatile Objects}, for more about @code{volatile}.
6793 In general, the @code{volatile} keyword is a standard C feature, so
6794 you might expect that @code{volatile} is available only when
6795 @code{__STDC__} is defined.  However, Ultrix 4.3's native compiler does
6796 support volatile, but does not define @code{__STDC__}.
6798 This macro is obsolescent, as current C compilers support @code{volatile}.
6799 New programs need not use this macro.
6800 @end defmac
6802 @anchor{AC_C_INLINE}
6803 @defmac AC_C_INLINE
6804 @acindex{C_INLINE}
6805 @cvindex inline
6806 If the C compiler supports the keyword @code{inline}, do nothing.
6807 Otherwise define @code{inline} to @code{__inline__} or @code{__inline}
6808 if it accepts one of those, otherwise define @code{inline} to be empty.
6809 @end defmac
6811 @anchor{AC_C_CHAR_UNSIGNED}
6812 @defmac AC_C_CHAR_UNSIGNED
6813 @acindex{C_CHAR_UNSIGNED}
6814 @cvindex __CHAR_UNSIGNED__
6815 If the C type @code{char} is unsigned, define @code{__CHAR_UNSIGNED__},
6816 unless the C compiler predefines it.
6817 @end defmac
6819 @defmac AC_C_STRINGIZE
6820 @acindex{C_STRINGIZE}
6821 @cvindex HAVE_STRINGIZE
6822 If the C preprocessor supports the stringizing operator, define
6823 @code{HAVE_STRINGIZE}.  The stringizing operator is @samp{#} and is
6824 found in macros such as this:
6826 @example
6827 #define x(y) #y
6828 @end example
6830 This macro is obsolescent, as current C compilers support the
6831 stringizing operator.  New programs need not use this macro.
6832 @end defmac
6834 @defmac AC_C_FLEXIBLE_ARRAY_MEMBER
6835 @acindex{C_FLEXIBLE_ARRAY_MEMBER}
6836 @cvindex FLEXIBLE_ARRAY_MEMBER
6837 If the C compiler supports flexible array members, define
6838 @code{FLEXIBLE_ARRAY_MEMBER} to nothing; otherwise define it to 1.
6839 That way, a declaration like this:
6841 @example
6842 struct s
6843   @{
6844     size_t n_vals;
6845     double val[FLEXIBLE_ARRAY_MEMBER];
6846   @};
6847 @end example
6849 @noindent
6850 will let applications use the ``struct hack'' even with compilers that
6851 do not support flexible array members.  To allocate and use such an
6852 object, you can use code like this:
6854 @example
6855 size_t i;
6856 size_t n = compute_value_count ();
6857 struct s *p =
6858    malloc (offsetof (struct s, val)
6859            + n * sizeof (double));
6860 p->n_vals = n;
6861 for (i = 0; i < n; i++)
6862   p->val[i] = compute_value (i);
6863 @end example
6864 @end defmac
6866 @defmac AC_C_VARARRAYS
6867 @acindex{C_VARARRAYS}
6868 @cvindex HAVE_C_VARARRAYS
6869 If the C compiler supports variable-length arrays, define
6870 @code{HAVE_C_VARARRAYS}.  A variable-length array is an array of automatic
6871 storage duration whose length is determined at run time, when the array
6872 is declared.
6873 @end defmac
6875 @defmac AC_C_TYPEOF
6876 @acindex{C_TYPEOF}
6877 @cvindex HAVE_TYPEOF
6878 @cvindex typeof
6879 If the C compiler supports @acronym{GCC}'s @code{typeof} syntax either
6880 directly or
6881 through a different spelling of the keyword (e.g., @code{__typeof__}),
6882 define @code{HAVE_TYPEOF}.  If the support is available only through a
6883 different spelling, define @code{typeof} to that spelling.
6884 @end defmac
6886 @defmac AC_C_PROTOTYPES
6887 @acindex{C_PROTOTYPES}
6888 @cvindex PROTOTYPES
6889 @cvindex __PROTOTYPES
6890 @cvindex PARAMS
6891 If function prototypes are understood by the compiler (as determined by
6892 @code{AC_PROG_CC}), define @code{PROTOTYPES} and @code{__PROTOTYPES}.
6893 Defining @code{__PROTOTYPES} is for the benefit of
6894 header files that cannot use macros that infringe on user name space.
6896 This macro is obsolescent, as current C compilers support prototypes.
6897 New programs need not use this macro.
6898 @end defmac
6900 @anchor{AC_PROG_GCC_TRADITIONAL}
6901 @defmac AC_PROG_GCC_TRADITIONAL
6902 @acindex{PROG_GCC_TRADITIONAL}
6903 @ovindex CC
6904 Add @option{-traditional} to output variable @code{CC} if using the
6905 @acronym{GNU} C compiler and @code{ioctl} does not work properly without
6906 @option{-traditional}.  That usually happens when the fixed header files
6907 have not been installed on an old system.
6909 This macro is obsolescent, since current versions of the @acronym{GNU} C
6910 compiler fix the header files automatically when installed.
6911 @end defmac
6914 @node C++ Compiler
6915 @subsection C++ Compiler Characteristics
6918 @defmac AC_PROG_CXX (@ovar{compiler-search-list})
6919 @acindex{PROG_CXX}
6920 @ovindex CXX
6921 @ovindex CXXFLAGS
6922 Determine a C++ compiler to use.  Check whether the environment variable
6923 @code{CXX} or @code{CCC} (in that order) is set; if so, then set output
6924 variable @code{CXX} to its value.
6926 Otherwise, if the macro is invoked without an argument, then search for
6927 a C++ compiler under the likely names (first @code{g++} and @code{c++}
6928 then other names).  If none of those checks succeed, then as a last
6929 resort set @code{CXX} to @code{g++}.
6931 This macro may, however, be invoked with an optional first argument
6932 which, if specified, must be a blank-separated list of C++ compilers to
6933 search for.  This just gives the user an opportunity to specify an
6934 alternative search list for the C++ compiler.  For example, if you
6935 didn't like the default order, then you could invoke @code{AC_PROG_CXX}
6936 like this:
6938 @example
6939 AC_PROG_CXX([gcc cl KCC CC cxx cc++ xlC aCC c++ g++])
6940 @end example
6942 If using the @acronym{GNU} C++ compiler, set shell variable @code{GXX} to
6943 @samp{yes}.  If output variable @code{CXXFLAGS} was not already set, set
6944 it to @option{-g -O2} for the @acronym{GNU} C++ compiler (@option{-O2} on
6945 systems where G++ does not accept @option{-g}), or @option{-g} for other
6946 compilers.
6947 @end defmac
6949 @defmac AC_PROG_CXXCPP
6950 @acindex{PROG_CXXCPP}
6951 @ovindex CXXCPP
6952 Set output variable @code{CXXCPP} to a command that runs the C++
6953 preprocessor.  If @samp{$CXX -E} doesn't work, @file{/lib/cpp} is used.
6954 It is portable to run @code{CXXCPP} only on files with a @file{.c},
6955 @file{.C}, @file{.cc}, or @file{.cpp} extension.
6957 Some preprocessors don't indicate missing include files by the error
6958 status.  For such preprocessors an internal variable is set that causes
6959 other macros to check the standard error from the preprocessor and
6960 consider the test failed if any warnings have been reported.  However,
6961 it is not known whether such broken preprocessors exist for C++.
6962 @end defmac
6964 @defmac AC_PROG_CXX_C_O
6965 @acindex{PROG_CXX_C_O}
6966 @cvindex CXX_NO_MINUS_C_MINUS_O
6967 Test whether the C++ compiler accepts the options @option{-c} and
6968 @option{-o} simultaneously, and define @code{CXX_NO_MINUS_C_MINUS_O},
6969 if it does not.
6970 @end defmac
6973 @node Objective C Compiler
6974 @subsection Objective C Compiler Characteristics
6977 @defmac AC_PROG_OBJC (@ovar{compiler-search-list})
6978 @acindex{PROG_OBJC}
6979 @ovindex OBJC
6980 @ovindex OBJCFLAGS
6981 Determine an Objective C compiler to use.  If @code{OBJC} is not already
6982 set in the environment, check for Objective C compilers.  Set output
6983 variable @code{OBJC} to the name of the compiler found.
6985 This macro may, however, be invoked with an optional first argument
6986 which, if specified, must be a blank-separated list of Objective C compilers to
6987 search for.  This just gives the user an opportunity to specify an
6988 alternative search list for the Objective C compiler.  For example, if you
6989 didn't like the default order, then you could invoke @code{AC_PROG_OBJC}
6990 like this:
6992 @example
6993 AC_PROG_OBJC([gcc objcc objc])
6994 @end example
6996 If using the @acronym{GNU} Objective C compiler, set shell variable
6997 @code{GOBJC} to @samp{yes}.  If output variable @code{OBJCFLAGS} was not
6998 already set, set it to @option{-g -O2} for the @acronym{GNU} Objective C
6999 compiler (@option{-O2} on systems where @command{gcc} does not accept
7000 @option{-g}), or @option{-g} for other compilers.
7001 @end defmac
7003 @defmac AC_PROG_OBJCPP
7004 @acindex{PROG_OBJCPP}
7005 @ovindex OBJCPP
7006 Set output variable @code{OBJCPP} to a command that runs the Objective C
7007 preprocessor.  If @samp{$OBJC -E} doesn't work, @file{/lib/cpp} is used.
7008 @end defmac
7011 @node Erlang Compiler and Interpreter
7012 @subsection Erlang Compiler and Interpreter Characteristics
7013 @cindex Erlang
7015 Autoconf defines the following macros for determining paths to the essential
7016 Erlang/OTP programs:
7018 @defmac AC_ERLANG_PATH_ERLC (@ovar{value-if-not-found}, @dvar{path, $PATH})
7019 @acindex{ERLANG_PATH_ERLC}
7020 @ovindex ERLC
7021 @ovindex ERLCFLAGS
7022 Determine an Erlang compiler to use.  If @code{ERLC} is not already set in the
7023 environment, check for @command{erlc}.  Set output variable @code{ERLC} to the
7024 complete path of the compiler command found.  In addition, if @code{ERLCFLAGS}
7025 is not set in the environment, set it to an empty value.
7027 The two optional arguments have the same meaning as the two last arguments of
7028 macro @code{AC_PROG_PATH} for looking for the @command{erlc} program.  For
7029 example, to look for @command{erlc} only in the @file{/usr/lib/erlang/bin}
7030 directory:
7032 @example
7033 AC_ERLANG_PATH_ERLC([not found], [/usr/lib/erlang/bin])
7034 @end example
7035 @end defmac
7037 @defmac AC_ERLANG_NEED_ERLC (@dvar{path, $PATH})
7038 @acindex{ERLANG_NEED_ERLC}
7039 A simplified variant of the @code{AC_ERLANG_PATH_ERLC} macro, that prints an
7040 error message and exits the @command{configure} script if the @command{erlc}
7041 program is not found.
7042 @end defmac
7044 @defmac AC_ERLANG_PATH_ERL (@ovar{value-if-not-found}, @dvar{path, $PATH})
7045 @acindex{ERLANG_PATH_ERL}
7046 @ovindex ERL
7047 Determine an Erlang interpreter to use.  If @code{ERL} is not already
7048 set in the
7049 environment, check for @command{erl}.  Set output variable @code{ERL} to the
7050 complete path of the interpreter command found.
7052 The two optional arguments have the same meaning as the two last arguments of
7053 macro @code{AC_PROG_PATH} for looking for the @command{erl} program.  For
7054 example, to look for @command{erl} only in the @file{/usr/lib/erlang/bin}
7055 directory:
7057 @example
7058 AC_ERLANG_PATH_ERL([not found], [/usr/lib/erlang/bin])
7059 @end example
7060 @end defmac
7062 @defmac AC_ERLANG_NEED_ERL (@dvar{path, $PATH})
7063 @acindex{ERLANG_NEED_ERL}
7064 A simplified variant of the @code{AC_ERLANG_PATH_ERL} macro, that prints an
7065 error message and exits the @command{configure} script if the @command{erl}
7066 program is not found.
7067 @end defmac
7070 @node Fortran Compiler
7071 @subsection Fortran Compiler Characteristics
7072 @cindex Fortran
7073 @cindex F77
7075 The Autoconf Fortran support is divided into two categories: legacy
7076 Fortran 77 macros (@code{F77}), and modern Fortran macros (@code{FC}).
7077 The former are intended for traditional Fortran 77 code, and have output
7078 variables like @code{F77}, @code{FFLAGS}, and @code{FLIBS}.  The latter
7079 are for newer programs that can (or must) compile under the newer
7080 Fortran standards, and have output variables like @code{FC},
7081 @code{FCFLAGS}, and @code{FCLIBS}.
7083 Except for two new macros @code{AC_FC_SRCEXT} and
7084 @code{AC_FC_FREEFORM} (see below), the @code{FC} and @code{F77} macros
7085 behave almost identically, and so they are documented together in this
7086 section.
7089 @defmac AC_PROG_F77 (@ovar{compiler-search-list})
7090 @acindex{PROG_F77}
7091 @ovindex F77
7092 @ovindex FFLAGS
7093 Determine a Fortran 77 compiler to use.  If @code{F77} is not already
7094 set in the environment, then check for @code{g77} and @code{f77}, and
7095 then some other names.  Set the output variable @code{F77} to the name
7096 of the compiler found.
7098 This macro may, however, be invoked with an optional first argument
7099 which, if specified, must be a blank-separated list of Fortran 77
7100 compilers to search for.  This just gives the user an opportunity to
7101 specify an alternative search list for the Fortran 77 compiler.  For
7102 example, if you didn't like the default order, then you could invoke
7103 @code{AC_PROG_F77} like this:
7105 @example
7106 AC_PROG_F77([fl32 f77 fort77 xlf g77 f90 xlf90])
7107 @end example
7109 If using @code{g77} (the @acronym{GNU} Fortran 77 compiler), then
7110 set the shell variable @code{G77} to @samp{yes}.
7111 If the output variable @code{FFLAGS} was not already set in the
7112 environment, then set it to @option{-g -02} for @code{g77} (or @option{-O2}
7113 where @code{g77} does not accept @option{-g}).  Otherwise, set
7114 @code{FFLAGS} to @option{-g} for all other Fortran 77 compilers.
7115 @end defmac
7117 @defmac AC_PROG_FC (@ovar{compiler-search-list}, @ovar{dialect})
7118 @acindex{PROG_FC}
7119 @ovindex FC
7120 @ovindex FCFLAGS
7121 Determine a Fortran compiler to use.  If @code{FC} is not already set in
7122 the environment, then @code{dialect} is a hint to indicate what Fortran
7123 dialect to search for; the default is to search for the newest available
7124 dialect.  Set the output variable @code{FC} to the name of the compiler
7125 found.
7127 By default, newer dialects are preferred over older dialects, but if
7128 @code{dialect} is specified then older dialects are preferred starting
7129 with the specified dialect.  @code{dialect} can currently be one of
7130 Fortran 77, Fortran 90, or Fortran 95.  However, this is only a hint of
7131 which compiler @emph{name} to prefer (e.g., @code{f90} or @code{f95}),
7132 and no attempt is made to guarantee that a particular language standard
7133 is actually supported.  Thus, it is preferable that you avoid the
7134 @code{dialect} option, and use AC_PROG_FC only for code compatible with
7135 the latest Fortran standard.
7137 This macro may, alternatively, be invoked with an optional first argument
7138 which, if specified, must be a blank-separated list of Fortran
7139 compilers to search for, just as in @code{AC_PROG_F77}.
7141 If the output variable @code{FCFLAGS} was not already set in the
7142 environment, then set it to @option{-g -02} for @acronym{GNU} @code{g77} (or
7143 @option{-O2} where @code{g77} does not accept @option{-g}).  Otherwise,
7144 set @code{FCFLAGS} to @option{-g} for all other Fortran compilers.
7145 @end defmac
7147 @defmac AC_PROG_F77_C_O
7148 @defmacx AC_PROG_FC_C_O
7149 @acindex{PROG_F77_C_O}
7150 @acindex{PROG_FC_C_O}
7151 @cvindex F77_NO_MINUS_C_MINUS_O
7152 @cvindex FC_NO_MINUS_C_MINUS_O
7153 Test whether the Fortran compiler accepts the options @option{-c} and
7154 @option{-o} simultaneously, and define @code{F77_NO_MINUS_C_MINUS_O} or
7155 @code{FC_NO_MINUS_C_MINUS_O}, respectively, if it does not.
7156 @end defmac
7158 The following macros check for Fortran compiler characteristics.
7159 To check for characteristics not listed here, use
7160 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}) or
7161 @code{AC_RUN_IFELSE} (@pxref{Runtime}), making sure to first set the
7162 current language to Fortran 77 or Fortran via @code{AC_LANG([Fortran 77])}
7163 or @code{AC_LANG(Fortran)} (@pxref{Language Choice}).
7166 @defmac AC_F77_LIBRARY_LDFLAGS
7167 @defmacx AC_FC_LIBRARY_LDFLAGS
7168 @acindex{F77_LIBRARY_LDFLAGS}
7169 @ovindex FLIBS
7170 @acindex{FC_LIBRARY_LDFLAGS}
7171 @ovindex FCLIBS
7172 Determine the linker flags (e.g., @option{-L} and @option{-l}) for the
7173 @dfn{Fortran intrinsic and runtime libraries} that are required to
7174 successfully link a Fortran program or shared library.  The output
7175 variable @code{FLIBS} or @code{FCLIBS} is set to these flags (which
7176 should be included after @code{LIBS} when linking).
7178 This macro is intended to be used in those situations when it is
7179 necessary to mix, e.g., C++ and Fortran source code in a single
7180 program or shared library (@pxref{Mixing Fortran 77 With C and C++, , ,
7181 automake, @acronym{GNU} Automake}).
7183 For example, if object files from a C++ and Fortran compiler must be
7184 linked together, then the C++ compiler/linker must be used for linking
7185 (since special C++-ish things need to happen at link time like calling
7186 global constructors, instantiating templates, enabling exception
7187 support, etc.).
7189 However, the Fortran intrinsic and runtime libraries must be linked in
7190 as well, but the C++ compiler/linker doesn't know by default how to add
7191 these Fortran 77 libraries.  Hence, this macro was created to determine
7192 these Fortran libraries.
7194 The macros @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7195 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} are probably also necessary to
7196 link C/C++ with Fortran; see below.
7197 @end defmac
7199 @defmac AC_F77_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7200 @defmacx AC_FC_DUMMY_MAIN (@ovar{action-if-found}, @ovar{action-if-not-found})
7201 @acindex{F77_DUMMY_MAIN}
7202 @cvindex F77_DUMMY_MAIN
7203 With many compilers, the Fortran libraries detected by
7204 @code{AC_F77_LIBRARY_LDFLAGS} or @code{AC_FC_LIBRARY_LDFLAGS} provide
7205 their own @code{main} entry function that initializes things like
7206 Fortran I/O, and which then calls a user-provided entry function named
7207 (say) @code{MAIN__} to run the user's program.  The
7208 @code{AC_F77_DUMMY_MAIN} and @code{AC_FC_DUMMY_MAIN} or
7209 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros figure out how to deal with
7210 this interaction.
7212 When using Fortran for purely numerical functions (no I/O, etc.)@: often
7213 one prefers to provide one's own @code{main} and skip the Fortran
7214 library initializations.  In this case, however, one may still need to
7215 provide a dummy @code{MAIN__} routine in order to prevent linking errors
7216 on some systems.  @code{AC_F77_DUMMY_MAIN} or @code{AC_FC_DUMMY_MAIN}
7217 detects whether any such routine is @emph{required} for linking, and
7218 what its name is; the shell variable @code{F77_DUMMY_MAIN} or
7219 @code{FC_DUMMY_MAIN} holds this name, @code{unknown} when no solution
7220 was found, and @code{none} when no such dummy main is needed.
7222 By default, @var{action-if-found} defines @code{F77_DUMMY_MAIN} or
7223 @code{FC_DUMMY_MAIN} to the name of this routine (e.g., @code{MAIN__})
7224 @emph{if} it is required.  @var{action-if-not-found} defaults to
7225 exiting with an error.
7227 In order to link with Fortran routines, the user's C/C++ program should
7228 then include the following code to define the dummy main if it is
7229 needed:
7231 @example
7232 #ifdef F77_DUMMY_MAIN
7233 #  ifdef __cplusplus
7234      extern "C"
7235 #  endif
7236    int F77_DUMMY_MAIN() @{ return 1; @}
7237 #endif
7238 @end example
7240 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7242 Note that this macro is called automatically from @code{AC_F77_WRAPPERS}
7243 or @code{AC_FC_WRAPPERS}; there is generally no need to call it
7244 explicitly unless one wants to change the default actions.
7245 @end defmac
7247 @defmac AC_F77_MAIN
7248 @defmacx AC_FC_MAIN
7249 @acindex{F77_MAIN}
7250 @cvindex F77_MAIN
7251 @acindex{FC_MAIN}
7252 @cvindex FC_MAIN
7253 As discussed above, many Fortran libraries allow you to provide an entry
7254 point called (say) @code{MAIN__} instead of the usual @code{main}, which
7255 is then called by a @code{main} function in the Fortran libraries that
7256 initializes things like Fortran I/O@.  The
7257 @code{AC_F77_MAIN} and @code{AC_FC_MAIN} macros detect whether it is
7258 @emph{possible} to utilize such an alternate main function, and defines
7259 @code{F77_MAIN} and @code{FC_MAIN} to the name of the function.  (If no
7260 alternate main function name is found, @code{F77_MAIN} and @code{FC_MAIN} are
7261 simply defined to @code{main}.)
7263 Thus, when calling Fortran routines from C that perform things like I/O,
7264 one should use this macro and declare the "main" function like so:
7266 @example
7267 #ifdef __cplusplus
7268   extern "C"
7269 #endif
7270 int F77_MAIN(int argc, char *argv[]);
7271 @end example
7273 (Again, replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7274 @end defmac
7276 @defmac AC_F77_WRAPPERS
7277 @defmacx AC_FC_WRAPPERS
7278 @acindex{F77_WRAPPERS}
7279 @cvindex F77_FUNC
7280 @cvindex F77_FUNC_
7281 @acindex{FC_WRAPPERS}
7282 @cvindex FC_FUNC
7283 @cvindex FC_FUNC_
7284 Defines C macros @code{F77_FUNC (name, NAME)}, @code{FC_FUNC (name, NAME)},
7285 @code{F77_FUNC_(name, NAME)}, and @code{FC_FUNC_(name, NAME)} to properly
7286 mangle the names of C/C++ identifiers, and identifiers with underscores,
7287 respectively, so that they match the name-mangling scheme used by the
7288 Fortran compiler.
7290 Fortran is case-insensitive, and in order to achieve this the Fortran
7291 compiler converts all identifiers into a canonical case and format.  To
7292 call a Fortran subroutine from C or to write a C function that is
7293 callable from Fortran, the C program must explicitly use identifiers in
7294 the format expected by the Fortran compiler.  In order to do this, one
7295 simply wraps all C identifiers in one of the macros provided by
7296 @code{AC_F77_WRAPPERS} or @code{AC_FC_WRAPPERS}.  For example, suppose
7297 you have the following Fortran 77 subroutine:
7299 @example
7300       subroutine foobar (x, y)
7301       double precision x, y
7302       y = 3.14159 * x
7303       return
7304       end
7305 @end example
7307 You would then declare its prototype in C or C++ as:
7309 @example
7310 #define FOOBAR_F77 F77_FUNC (foobar, FOOBAR)
7311 #ifdef __cplusplus
7312 extern "C"  /* prevent C++ name mangling */
7313 #endif
7314 void FOOBAR_F77(double *x, double *y);
7315 @end example
7317 Note that we pass both the lowercase and uppercase versions of the
7318 function name to @code{F77_FUNC} so that it can select the right one.
7319 Note also that all parameters to Fortran 77 routines are passed as
7320 pointers (@pxref{Mixing Fortran 77 With C and C++, , , automake, @acronym{GNU}
7321 Automake}).
7323 (Replace @code{F77} with @code{FC} for Fortran instead of Fortran 77.)
7325 Although Autoconf tries to be intelligent about detecting the
7326 name-mangling scheme of the Fortran compiler, there may be Fortran
7327 compilers that it doesn't support yet.  In this case, the above code
7328 generates a compile-time error, but some other behavior
7329 (e.g., disabling Fortran-related features) can be induced by checking
7330 whether @code{F77_FUNC} or @code{FC_FUNC} is defined.
7332 Now, to call that routine from a C program, we would do something like:
7334 @example
7336     double x = 2.7183, y;
7337     FOOBAR_F77 (&x, &y);
7339 @end example
7341 If the Fortran identifier contains an underscore (e.g., @code{foo_bar}),
7342 you should use @code{F77_FUNC_} or @code{FC_FUNC_} instead of
7343 @code{F77_FUNC} or @code{FC_FUNC} (with the same arguments).  This is
7344 because some Fortran compilers mangle names differently if they contain
7345 an underscore.
7346 @end defmac
7348 @defmac AC_F77_FUNC (@var{name}, @ovar{shellvar})
7349 @defmacx AC_FC_FUNC (@var{name}, @ovar{shellvar})
7350 @acindex{F77_FUNC}
7351 @acindex{FC_FUNC}
7352 Given an identifier @var{name}, set the shell variable @var{shellvar} to
7353 hold the mangled version @var{name} according to the rules of the
7354 Fortran linker (see also @code{AC_F77_WRAPPERS} or
7355 @code{AC_FC_WRAPPERS}).  @var{shellvar} is optional; if it is not
7356 supplied, the shell variable is simply @var{name}.  The purpose of
7357 this macro is to give the caller a way to access the name-mangling
7358 information other than through the C preprocessor as above, for example,
7359 to call Fortran routines from some language other than C/C++.
7360 @end defmac
7362 @defmac AC_FC_SRCEXT (@var{ext}, @ovar{action-if-success}, @
7363   @ovar{action-if-failure})
7364 @acindex{FC_SRCEXT}
7365 By default, the @code{FC} macros perform their tests using a @file{.f}
7366 extension for source-code files.  Some compilers, however, only enable
7367 newer language features for appropriately named files, e.g., Fortran 90
7368 features only for @file{.f90} files.  On the other hand, some other
7369 compilers expect all source files to end in @file{.f} and require
7370 special flags to support other file name extensions.  The
7371 @code{AC_FC_SRCEXT} macro deals with both of these issues.
7373 The @code{AC_FC_SRCEXT} tries to get the @code{FC} compiler to accept files
7374 ending with the extension .@var{ext} (i.e., @var{ext} does @emph{not}
7375 contain the dot).  If any special compiler flags are needed for this, it
7376 stores them in the output variable @code{FCFLAGS_}@var{ext}.  This
7377 extension and these flags are then used for all subsequent @code{FC} tests
7378 (until @code{AC_FC_SRCEXT} is called again).
7380 For example, you would use @code{AC_FC_SRCEXT(f90)} to employ the
7381 @file{.f90} extension in future tests, and it would set a
7382 @code{FCFLAGS_f90} output variable with any extra flags that are needed
7383 to compile such files.
7385 The @code{FCFLAGS_}@var{ext} can @emph{not} be simply absorbed into
7386 @code{FCFLAGS}, for two reasons based on the limitations of some
7387 compilers.  First, only one @code{FCFLAGS_}@var{ext} can be used at a
7388 time, so files with different extensions must be compiled separately.
7389 Second, @code{FCFLAGS_}@var{ext} must appear @emph{immediately} before
7390 the source-code file name when compiling.  So, continuing the example
7391 above, you might compile a @file{foo.f90} file in your makefile with the
7392 command:
7394 @example
7395 foo.o: foo.f90
7396      $(FC) -c $(FCFLAGS) $(FCFLAGS_f90) '$(srcdir)/foo.f90'
7397 @end example
7399 If @code{AC_FC_SRCEXT} succeeds in compiling files with the @var{ext}
7400 extension, it calls @var{action-if-success} (defaults to nothing).  If
7401 it fails, and cannot find a way to make the @code{FC} compiler accept such
7402 files, it calls @var{action-if-failure} (defaults to exiting with an
7403 error message).
7405 @end defmac
7407 @defmac AC_FC_FREEFORM (@ovar{action-if-success}, @ovar{action-if-failure})
7408 @acindex{FC_FREEFORM}
7410 The @code{AC_FC_FREEFORM} tries to ensure that the Fortran compiler
7411 (@code{$FC}) allows free-format source code (as opposed to the older
7412 fixed-format style from Fortran 77).  If necessary, it may add some
7413 additional flags to @code{FCFLAGS}.
7415 This macro is most important if you are using the default @file{.f}
7416 extension, since many compilers interpret this extension as indicating
7417 fixed-format source unless an additional flag is supplied.  If you
7418 specify a different extension with @code{AC_FC_SRCEXT}, such as
7419 @file{.f90} or @file{.f95}, then @code{AC_FC_FREEFORM} ordinarily
7420 succeeds without modifying @code{FCFLAGS}.
7422 If @code{AC_FC_FREEFORM} succeeds in compiling free-form source, it
7423 calls @var{action-if-success} (defaults to nothing).  If it fails, it
7424 calls @var{action-if-failure} (defaults to exiting with an error
7425 message).
7426 @end defmac
7428 @node System Services
7429 @section System Services
7431 The following macros check for operating system services or capabilities.
7433 @anchor{AC_PATH_X}
7434 @defmac AC_PATH_X
7435 @acindex{PATH_X}
7436 @evindex XMKMF
7437 @cindex X Window System
7438 Try to locate the X Window System include files and libraries.  If the
7439 user gave the command line options @option{--x-includes=@var{dir}} and
7440 @option{--x-libraries=@var{dir}}, use those directories.
7442 If either or both were not given, get the missing values by running
7443 @code{xmkmf} (or an executable pointed to by the @code{XMKMF}
7444 environment variable) on a trivial @file{Imakefile} and examining the
7445 makefile that it produces.  Setting @code{XMKMF} to @samp{false}
7446 disables this method.
7448 If this method fails to find the X Window System, @command{configure}
7449 looks for the files in several directories where they often reside.
7450 If either method is successful, set the shell variables
7451 @code{x_includes} and @code{x_libraries} to their locations, unless they
7452 are in directories the compiler searches by default.
7454 If both methods fail, or the user gave the command line option
7455 @option{--without-x}, set the shell variable @code{no_x} to @samp{yes};
7456 otherwise set it to the empty string.
7457 @end defmac
7459 @anchor{AC_PATH_XTRA}
7460 @defmac AC_PATH_XTRA
7461 @acindex{PATH_XTRA}
7462 @ovindex X_CFLAGS
7463 @ovindex X_LIBS
7464 @ovindex X_EXTRA_LIBS
7465 @ovindex X_PRE_LIBS
7466 @cvindex X_DISPLAY_MISSING
7467 An enhanced version of @code{AC_PATH_X}.  It adds the C compiler flags
7468 that X needs to output variable @code{X_CFLAGS}, and the X linker flags
7469 to @code{X_LIBS}.  Define @code{X_DISPLAY_MISSING} if X is not
7470 available.
7472 This macro also checks for special libraries that some systems need in
7473 order to compile X programs.  It adds any that the system needs to
7474 output variable @code{X_EXTRA_LIBS}.  And it checks for special X11R6
7475 libraries that need to be linked with before @option{-lX11}, and adds
7476 any found to the output variable @code{X_PRE_LIBS}.
7478 @c This is an incomplete kludge.  Make a real way to do it.
7479 @c If you need to check for other X functions or libraries yourself, then
7480 @c after calling this macro, add the contents of @code{X_EXTRA_LIBS} to
7481 @c @code{LIBS} temporarily, like this: (FIXME - add example)
7482 @end defmac
7484 @anchor{AC_SYS_INTERPRETER}
7485 @defmac AC_SYS_INTERPRETER
7486 @acindex{SYS_INTERPRETER}
7487 Check whether the system supports starting scripts with a line of the
7488 form @samp{#!/bin/sh} to select the interpreter to use for the script.
7489 After running this macro, shell code in @file{configure.ac} can check
7490 the shell variable @code{interpval}; it is set to @samp{yes}
7491 if the system supports @samp{#!}, @samp{no} if not.
7492 @end defmac
7494 @defmac AC_SYS_LARGEFILE
7495 @acindex{SYS_LARGEFILE}
7496 @cvindex _FILE_OFFSET_BITS
7497 @cvindex _LARGE_FILES
7498 @ovindex CC
7499 @cindex Large file support
7500 @cindex LFS
7501 Arrange for
7502 @uref{http://www.unix-systems.org/@/version2/@/whatsnew/@/lfs20mar.html,
7503 large-file support}.  On some hosts, one must use special compiler
7504 options to build programs that can access large files.  Append any such
7505 options to the output variable @code{CC}.  Define
7506 @code{_FILE_OFFSET_BITS} and @code{_LARGE_FILES} if necessary.
7508 Large-file support can be disabled by configuring with the
7509 @option{--disable-largefile} option.
7511 If you use this macro, check that your program works even when
7512 @code{off_t} is wider than @code{long int}, since this is common when
7513 large-file support is enabled.  For example, it is not correct to print
7514 an arbitrary @code{off_t} value @code{X} with @code{printf ("%ld",
7515 (long int) X)}.
7517 The LFS introduced the @code{fseeko} and @code{ftello} functions to
7518 replace their C counterparts @code{fseek} and @code{ftell} that do not
7519 use @code{off_t}.  Take care to use @code{AC_FUNC_FSEEKO} to make their
7520 prototypes available when using them and large-file support is
7521 enabled.
7522 @end defmac
7524 @anchor{AC_SYS_LONG_FILE_NAMES}
7525 @defmac AC_SYS_LONG_FILE_NAMES
7526 @acindex{SYS_LONG_FILE_NAMES}
7527 @cvindex HAVE_LONG_FILE_NAMES
7528 If the system supports file names longer than 14 characters, define
7529 @code{HAVE_LONG_FILE_NAMES}.
7530 @end defmac
7532 @defmac AC_SYS_POSIX_TERMIOS
7533 @acindex{SYS_POSIX_TERMIOS}
7534 @cindex Posix termios headers
7535 @cindex termios Posix headers
7536 Check to see if the Posix termios headers and functions are available on the
7537 system.  If so, set the shell variable @code{ac_cv_sys_posix_termios} to
7538 @samp{yes}.  If not, set the variable to @samp{no}.
7539 @end defmac
7541 @node Posix Variants
7542 @section Posix Variants
7544 The following macro makes it possible to use features of Posix that are
7545 extensions to C, as well as platform extensions not defined by Posix.
7547 @anchor{AC_USE_SYSTEM_EXTENSIONS}
7548 @defmac AC_USE_SYSTEM_EXTENSIONS
7549 @acindex{USE_SYSTEM_EXTENSIONS}
7550 @cvindex _ALL_SOURCE
7551 @cvindex _GNU_SOURCE
7552 @cvindex _MINIX
7553 @cvindex _POSIX_1_SOURCE
7554 @cvindex _POSIX_PTHREAD_SEMANTICS
7555 @cvindex _POSIX_SOURCE
7556 @cvindex _TANDEM_SOURCE
7557 @cvindex __EXTENSIONS__
7558 This macro was introduced in Autoconf 2.60.  If possible, enable
7559 extensions to C or Posix on hosts that normally disable the extensions,
7560 typically due to standards-conformance namespace issues.  This should be
7561 called before any macros that run the C compiler.  The following
7562 preprocessor macros are defined where appropriate:
7564 @table @code
7565 @item _GNU_SOURCE
7566 Enable extensions on @acronym{GNU}/Linux.
7567 @item __EXTENSIONS__
7568 Enable general extensions on Solaris.
7569 @item _POSIX_PTHREAD_SEMANTICS
7570 Enable threading extensions on Solaris.
7571 @item _TANDEM_SOURCE
7572 Enable extensions for the @acronym{HP} NonStop platform.
7573 @item _ALL_SOURCE
7574 Enable extensions for @acronym{AIX} 3, and for Interix.
7575 @item _POSIX_SOURCE
7576 Enable Posix functions for Minix.
7577 @item _POSIX_1_SOURCE
7578 Enable additional Posix functions for Minix.
7579 @item _MINIX
7580 Identify Minix platform.  This particular preprocessor macro is
7581 obsolescent, and may be removed in a future release of Autoconf.
7582 @end table
7583 @end defmac
7586 @node Erlang Libraries
7587 @section Erlang Libraries
7588 @cindex Erlang, Library, checking
7590 The following macros check for an installation of Erlang/OTP, and for the
7591 presence of certain Erlang libraries.  All those macros require the
7592 configuration of an Erlang interpreter and an Erlang compiler
7593 (@pxref{Erlang Compiler and Interpreter}).
7595 @defmac AC_ERLANG_SUBST_ROOT_DIR
7596 @acindex{ERLANG_SUBST_ROOT_DIR}
7597 @ovindex ERLANG_ROOT_DIR
7599 Set the output variable @code{ERLANG_ROOT_DIR} to the path to the base
7600 directory in which Erlang/OTP is installed (as returned by Erlang's
7601 @code{code:root_dir/0} function).  The result of this test is cached if
7602 caching is enabled when running @command{configure}.
7603 @end defmac
7605 @defmac AC_ERLANG_SUBST_LIB_DIR
7606 @acindex{ERLANG_SUBST_LIB_DIR}
7607 @ovindex ERLANG_LIB_DIR
7609 Set the output variable @code{ERLANG_LIB_DIR} to the path of the library
7610 directory of Erlang/OTP (as returned by Erlang's
7611 @code{code:lib_dir/0} function), which subdirectories each contain an installed
7612 Erlang/OTP library.  The result of this test is cached if caching is enabled
7613 when running @command{configure}.
7614 @end defmac
7616 @defmac AC_ERLANG_CHECK_LIB (@var{library}, @ovar{action-if-found}, @
7617   @ovar{action-if-not-found})
7618 @acindex{ERLANG_CHECK_LIB}
7619 @ovindex ERLANG_LIB_DIR_@var{library}
7620 @ovindex ERLANG_LIB_VER_@var{library}
7622 Test whether the Erlang/OTP library @var{library} is installed by
7623 calling Erlang's @code{code:lib_dir/1} function.  The result of this
7624 test is cached if caching is enabled when running @command{configure}.
7625 @var{action-if-found} is a list of shell commands to run if the library
7626 is installed; @var{action-if-not-found} is a list of shell commands to
7627 run if it is not.  Additionally, if the library is installed, the output
7628 variable @samp{ERLANG_LIB_DIR_@var{library}} is set to the path to the
7629 library installation directory, and the output variable
7630 @samp{ERLANG_LIB_VER_@var{library}} is set to the version number that is
7631 part of the subdirectory name, if it is in the standard form
7632 (@code{@var{library}-@var{version}}).  If the directory name does not
7633 have a version part, @samp{ERLANG_LIB_VER_@var{library}} is set to the
7634 empty string.  If the library is not installed,
7635 @samp{ERLANG_LIB_DIR_@var{library}} and
7636 @samp{ERLANG_LIB_VER_@var{library}} are set to @code{"not found"}.  For
7637 example, to check if library @code{stdlib} is installed:
7639 @example
7640 AC_ERLANG_CHECK_LIB([stdlib],
7641   [echo "stdlib version \"$ERLANG_LIB_VER_stdlib\""
7642    echo "is installed in \"$ERLANG_LIB_DIR_stdlib\""],
7643   [AC_MSG_ERROR([stdlib was not found!])])
7644 @end example
7645 @end defmac
7647 In addition to the above macros, which test installed Erlang libraries, the
7648 following macros determine the paths to the directories into which newly built
7649 Erlang libraries are to be installed:
7651 @defmac AC_ERLANG_SUBST_INSTALL_LIB_DIR
7652 @acindex{ERLANG_SUBST_INSTALL_LIB_DIR}
7653 @ovindex ERLANG_INSTALL_LIB_DIR
7655 Set the @code{ERLANG_INSTALL_LIB_DIR} output variable to the directory into
7656 which every built Erlang library should be installed in a separate
7657 subdirectory.
7658 If this variable is not set in the environment when @command{configure} runs,
7659 its default value is @code{$ERLANG_LIB_DIR}, which value is set by the
7660 @code{AC_ERLANG_SUBST_LIB_DIR} macro.
7661 @end defmac
7663 @defmac AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR (@var{library}, @var{version})
7664 @acindex{ERLANG_SUBST_INSTALL_LIB_SUBDIR}
7665 @ovindex ERLANG_INSTALL_LIB_DIR_@var{library}
7667 Set the @samp{ERLANG_INSTALL_LIB_DIR_@var{library}} output variable to the
7668 directory into which the built Erlang library @var{library} version
7669 @var{version} should be installed.  If this variable is not set in the
7670 environment when @command{configure} runs, its default value is
7671 @samp{$ERLANG_INSTALL_LIB_DIR/@var{library}-@var{version}}, the value of the
7672 @code{ERLANG_INSTALL_LIB_DIR} variable being set by the
7673 @code{AC_ERLANG_SUBST_INSTALL_LIB_DIR} macro.
7674 @end defmac
7680 @c ========================================================= Writing Tests
7682 @node Writing Tests
7683 @chapter Writing Tests
7685 If the existing feature tests don't do something you need, you have to
7686 write new ones.  These macros are the building blocks.  They provide
7687 ways for other macros to check whether various kinds of features are
7688 available and report the results.
7690 This chapter contains some suggestions and some of the reasons why the
7691 existing tests are written the way they are.  You can also learn a lot
7692 about how to write Autoconf tests by looking at the existing ones.  If
7693 something goes wrong in one or more of the Autoconf tests, this
7694 information can help you understand the assumptions behind them, which
7695 might help you figure out how to best solve the problem.
7697 These macros check the output of the compiler system of the current
7698 language (@pxref{Language Choice}).  They do not cache the results of
7699 their tests for future use (@pxref{Caching Results}), because they don't
7700 know enough about the information they are checking for to generate a
7701 cache variable name.  They also do not print any messages, for the same
7702 reason.  The checks for particular kinds of features call these macros
7703 and do cache their results and print messages about what they're
7704 checking for.
7706 When you write a feature test that could be applicable to more than one
7707 software package, the best thing to do is encapsulate it in a new macro.
7708 @xref{Writing Autoconf Macros}, for how to do that.
7710 @menu
7711 * Language Choice::             Selecting which language to use for testing
7712 * Writing Test Programs::       Forging source files for compilers
7713 * Running the Preprocessor::    Detecting preprocessor symbols
7714 * Running the Compiler::        Detecting language or header features
7715 * Running the Linker::          Detecting library features
7716 * Runtime::                     Testing for runtime features
7717 * Systemology::                 A zoology of operating systems
7718 * Multiple Cases::              Tests for several possible values
7719 @end menu
7721 @node Language Choice
7722 @section Language Choice
7723 @cindex Language
7725 Autoconf-generated @command{configure} scripts check for the C compiler and
7726 its features by default.  Packages that use other programming languages
7727 (maybe more than one, e.g., C and C++) need to test features of the
7728 compilers for the respective languages.  The following macros determine
7729 which programming language is used in the subsequent tests in
7730 @file{configure.ac}.
7732 @anchor{AC_LANG}
7733 @defmac AC_LANG (@var{language})
7734 Do compilation tests using the compiler, preprocessor, and file
7735 extensions for the specified @var{language}.
7737 Supported languages are:
7739 @table @samp
7740 @item C
7741 Do compilation tests using @code{CC} and @code{CPP} and use extension
7742 @file{.c} for test programs.  Use compilation flags: @code{CPPFLAGS} with
7743 @code{CPP}, and both @code{CPPFLAGS} and @code{CFLAGS} with @code{CC}.
7745 @item C++
7746 Do compilation tests using @code{CXX} and @code{CXXCPP} and use
7747 extension @file{.C} for test programs.  Use compilation flags:
7748 @code{CPPFLAGS} with @code{CXXCPP}, and both @code{CPPFLAGS} and
7749 @code{CXXFLAGS} with @code{CXX}.
7751 @item Fortran 77
7752 Do compilation tests using @code{F77} and use extension @file{.f} for
7753 test programs.  Use compilation flags: @code{FFLAGS}.
7755 @item Fortran
7756 Do compilation tests using @code{FC} and use extension @file{.f} (or
7757 whatever has been set by @code{AC_FC_SRCEXT}) for test programs.  Use
7758 compilation flags: @code{FCFLAGS}.
7760 @item Erlang
7761 @ovindex ERLC
7762 @ovindex ERL
7763 @ovindex ERLCFLAGS
7764 Compile and execute tests using @code{ERLC} and @code{ERL} and use extension
7765 @file{.erl} for test Erlang modules.  Use compilation flags: @code{ERLCFLAGS}.
7767 @item Objective C
7768 Do compilation tests using @code{OBJC} and @code{OBJCPP} and use
7769 extension @file{.m} for test programs.  Use compilation flags:
7770 @code{CPPFLAGS} with @code{OBJCPP}, and both @code{CPPFLAGS} and
7771 @code{OBJCFLAGS} with @code{OBJC}.
7772 @end table
7773 @end defmac
7775 @anchor{AC_LANG_PUSH}
7776 @defmac AC_LANG_PUSH (@var{language})
7777 @acindex{LANG_PUSH}
7778 Remember the current language (as set by @code{AC_LANG}) on a stack, and
7779 then select the @var{language}.  Use this macro and @code{AC_LANG_POP}
7780 in macros that need to temporarily switch to a particular language.
7781 @end defmac
7783 @defmac AC_LANG_POP (@ovar{language})
7784 @acindex{LANG_POP}
7785 Select the language that is saved on the top of the stack, as set by
7786 @code{AC_LANG_PUSH}, and remove it from the stack.
7788 If given, @var{language} specifies the language we just @emph{quit}.  It
7789 is a good idea to specify it when it's known (which should be the
7790 case@dots{}), since Autoconf detects inconsistencies.
7792 @example
7793 AC_LANG_PUSH([Fortran 77])
7794 # Perform some tests on Fortran 77.
7795 # @dots{}
7796 AC_LANG_POP([Fortran 77])
7797 @end example
7798 @end defmac
7800 @defmac AC_LANG_ASSERT (@var{language})
7801 @acindex{LANG_ASSERT} Check statically that the current language is
7802 @var{language}.  You should use this in your language specific macros
7803 to avoid that they be called with an inappropriate language.
7805 This macro runs only at @command{autoconf} time, and incurs no cost at
7806 @command{configure} time.  Sadly enough and because Autoconf is a two
7807 layer language @footnote{Because M4 is not aware of Sh code,
7808 especially conditionals, some optimizations that look nice statically
7809 may produce incorrect results at runtime.}, the macros
7810 @code{AC_LANG_PUSH} and @code{AC_LANG_POP} cannot be ``optimizing'',
7811 therefore as much as possible you ought to avoid using them to wrap
7812 your code, rather, require from the user to run the macro with a
7813 correct current language, and check it with @code{AC_LANG_ASSERT}.
7814 And anyway, that may help the user understand she is running a Fortran
7815 macro while expecting a result about her Fortran 77 compiler@dots{}
7816 @end defmac
7819 @defmac AC_REQUIRE_CPP
7820 @acindex{REQUIRE_CPP}
7821 Ensure that whichever preprocessor would currently be used for tests has
7822 been found.  Calls @code{AC_REQUIRE} (@pxref{Prerequisite Macros}) with an
7823 argument of either @code{AC_PROG_CPP} or @code{AC_PROG_CXXCPP},
7824 depending on which language is current.
7825 @end defmac
7828 @node Writing Test Programs
7829 @section Writing Test Programs
7831 Autoconf tests follow a common scheme: feed some program with some
7832 input, and most of the time, feed a compiler with some source file.
7833 This section is dedicated to these source samples.
7835 @menu
7836 * Guidelines::                  General rules for writing test programs
7837 * Test Functions::              Avoiding pitfalls in test programs
7838 * Generating Sources::          Source program boilerplate
7839 @end menu
7841 @node Guidelines
7842 @subsection Guidelines for Test Programs
7844 The most important rule to follow when writing testing samples is:
7846 @center @emph{Look for realism.}
7848 This motto means that testing samples must be written with the same
7849 strictness as real programs are written.  In particular, you should
7850 avoid ``shortcuts'' and simplifications.
7852 Don't just play with the preprocessor if you want to prepare a
7853 compilation.  For instance, using @command{cpp} to check whether a header is
7854 functional might let your @command{configure} accept a header which
7855 causes some @emph{compiler} error.  Do not hesitate to check a header with
7856 other headers included before, especially required headers.
7858 Make sure the symbols you use are properly defined, i.e., refrain for
7859 simply declaring a function yourself instead of including the proper
7860 header.
7862 Test programs should not write to standard output.  They
7863 should exit with status 0 if the test succeeds, and with status 1
7864 otherwise, so that success
7865 can be distinguished easily from a core dump or other failure;
7866 segmentation violations and other failures produce a nonzero exit
7867 status.  Unless you arrange for @code{exit} to be declared, test
7868 programs should @code{return}, not @code{exit}, from @code{main},
7869 because on many systems @code{exit} is not declared by default.
7871 Test programs can use @code{#if} or @code{#ifdef} to check the values of
7872 preprocessor macros defined by tests that have already run.  For
7873 example, if you call @code{AC_HEADER_STDBOOL}, then later on in
7874 @file{configure.ac} you can have a test program that includes
7875 @file{stdbool.h} conditionally:
7877 @example
7878 @group
7879 #ifdef HAVE_STDBOOL_H
7880 # include <stdbool.h>
7881 #endif
7882 @end group
7883 @end example
7885 Both @code{#if HAVE_STDBOOL_H} and @code{#ifdef HAVE_STDBOOL_H} will
7886 work with any standard C compiler.  Some developers prefer @code{#if}
7887 because it is easier to read, while others prefer @code{#ifdef} because
7888 it avoids diagnostics with picky compilers like @acronym{GCC} with the
7889 @option{-Wundef} option.
7891 If a test program needs to use or create a data file, give it a name
7892 that starts with @file{conftest}, such as @file{conftest.data}.  The
7893 @command{configure} script cleans up by running @samp{rm -f -r conftest*}
7894 after running test programs and if the script is interrupted.
7896 @node Test Functions
7897 @subsection Test Functions
7899 These days it's safe to assume support for function prototypes
7900 (introduced in C89).
7902 Functions that test programs declare should also be conditionalized for
7903 C++, which requires @samp{extern "C"} prototypes.  Make sure to not
7904 include any header files containing clashing prototypes.
7906 @example
7907 #ifdef __cplusplus
7908 extern "C"
7909 #endif
7910 void *valloc (size_t);
7911 @end example
7913 If a test program calls a function with invalid parameters (just to see
7914 whether it exists), organize the program to ensure that it never invokes
7915 that function.  You can do this by calling it in another function that is
7916 never invoked.  You can't do it by putting it after a call to
7917 @code{exit}, because @acronym{GCC} version 2 knows that @code{exit}
7918 never returns
7919 and optimizes out any code that follows it in the same block.
7921 If you include any header files, be sure to call the functions
7922 relevant to them with the correct number of arguments, even if they are
7923 just 0, to avoid compilation errors due to prototypes.  @acronym{GCC}
7924 version 2
7925 has internal prototypes for several functions that it automatically
7926 inlines; for example, @code{memcpy}.  To avoid errors when checking for
7927 them, either pass them the correct number of arguments or redeclare them
7928 with a different return type (such as @code{char}).
7931 @node Generating Sources
7932 @subsection Generating Sources
7934 Autoconf provides a set of macros that can be used to generate test
7935 source files.  They are written to be language generic, i.e., they
7936 actually depend on the current language (@pxref{Language Choice}) to
7937 ``format'' the output properly.
7940 @defmac AC_LANG_CONFTEST (@var{source})
7941 @acindex{LANG_CONFTEST}
7942 Save the @var{source} text in the current test source file:
7943 @file{conftest.@var{extension}} where the @var{extension} depends on the
7944 current language.
7946 Note that the @var{source} is evaluated exactly once, like regular
7947 Autoconf macro arguments, and therefore (i) you may pass a macro
7948 invocation, (ii) if not, be sure to double quote if needed.
7949 @end defmac
7951 @defmac AC_LANG_SOURCE (@var{source})
7952 @acindex{LANG_SOURCE}
7953 Expands into the @var{source}, with the definition of
7954 all the @code{AC_DEFINE} performed so far.
7955 @end defmac
7957 For instance executing (observe the double quotation!):
7959 @example
7960 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
7961 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
7962   [Greetings string.])
7963 AC_LANG(C)
7964 AC_LANG_CONFTEST(
7965    [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
7966 gcc -E -dD -o - conftest.c
7967 @end example
7969 @noindent
7970 results in:
7972 @example
7973 @dots{}
7974 # 1 "conftest.c"
7976 #define PACKAGE_NAME "Hello"
7977 #define PACKAGE_TARNAME "hello"
7978 #define PACKAGE_VERSION "1.0"
7979 #define PACKAGE_STRING "Hello 1.0"
7980 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
7981 #define HELLO_WORLD "Hello, World\n"
7983 const char hw[] = "Hello, World\n";
7984 @end example
7986 When the test language is Fortran or Erlang, the @code{AC_DEFINE} definitions
7987 are not automatically translated into constants in the source code by this
7988 macro.
7990 @defmac AC_LANG_PROGRAM (@var{prologue}, @var{body})
7991 @acindex{LANG_PROGRAM}
7992 Expands into a source file which consists of the @var{prologue}, and
7993 then @var{body} as body of the main function (e.g., @code{main} in
7994 C).  Since it uses @code{AC_LANG_SOURCE}, the features of the latter are
7995 available.
7996 @end defmac
7998 For instance:
8000 @example
8001 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8002 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
8003   [Greetings string.])
8004 AC_LANG_CONFTEST(
8005 [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
8006                  [[fputs (hw, stdout);]])])
8007 gcc -E -dD -o - conftest.c
8008 @end example
8010 @noindent
8011 results in:
8013 @example
8014 @dots{}
8015 # 1 "conftest.c"
8017 #define PACKAGE_NAME "Hello"
8018 #define PACKAGE_TARNAME "hello"
8019 #define PACKAGE_VERSION "1.0"
8020 #define PACKAGE_STRING "Hello 1.0"
8021 #define PACKAGE_BUGREPORT "bug-hello@@example.org"
8022 #define HELLO_WORLD "Hello, World\n"
8024 const char hw[] = "Hello, World\n";
8026 main ()
8028 fputs (hw, stdout);
8029   ;
8030   return 0;
8032 @end example
8034 In Erlang tests, the created source file is that of an Erlang module called
8035 @code{conftest} (@file{conftest.erl}).  This module defines and exports
8036 at least
8037 one @code{start/0} function, which is called to perform the test.  The
8038 @var{prologue} is optional code that is inserted between the module header and
8039 the @code{start/0} function definition.  @var{body} is the body of the
8040 @code{start/0} function without the final period (@pxref{Runtime}, about
8041 constraints on this function's behavior).
8043 For instance:
8045 @example
8046 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8047 AC_LANG(Erlang)
8048 AC_LANG_CONFTEST(
8049 [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
8050                  [[io:format("~s~n", [?HELLO_WORLD])]])])
8051 cat conftest.erl
8052 @end example
8054 @noindent
8055 results in:
8057 @example
8058 -module(conftest).
8059 -export([start/0]).
8060 -define(HELLO_WORLD, "Hello, world!").
8061 start() ->
8062 io:format("~s~n", [?HELLO_WORLD])
8064 @end example
8066 @defmac AC_LANG_CALL (@var{prologue}, @var{function})
8067 @acindex{LANG_CALL}
8068 Expands into a source file which consists of the @var{prologue}, and
8069 then a call to the @var{function} as body of the main function (e.g.,
8070 @code{main} in C).  Since it uses @code{AC_LANG_PROGRAM}, the feature
8071 of the latter are available.
8073 This function will probably be replaced in the future by a version
8074 which would enable specifying the arguments.  The use of this macro is
8075 not encouraged, as it violates strongly the typing system.
8077 This macro cannot be used for Erlang tests.
8078 @end defmac
8080 @defmac AC_LANG_FUNC_LINK_TRY (@var{function})
8081 @acindex{LANG_FUNC_LINK_TRY}
8082 Expands into a source file which uses the @var{function} in the body of
8083 the main function (e.g., @code{main} in C).  Since it uses
8084 @code{AC_LANG_PROGRAM}, the features of the latter are available.
8086 As @code{AC_LANG_CALL}, this macro is documented only for completeness.
8087 It is considered to be severely broken, and in the future will be
8088 removed in favor of actual function calls (with properly typed
8089 arguments).
8091 This macro cannot be used for Erlang tests.
8092 @end defmac
8094 @node Running the Preprocessor
8095 @section Running the Preprocessor
8097 Sometimes one might need to run the preprocessor on some source file.
8098 @emph{Usually it is a bad idea}, as you typically need to @emph{compile}
8099 your project, not merely run the preprocessor on it; therefore you
8100 certainly want to run the compiler, not the preprocessor.  Resist the
8101 temptation of following the easiest path.
8103 Nevertheless, if you need to run the preprocessor, then use
8104 @code{AC_PREPROC_IFELSE}.
8106 The macros described in this section cannot be used for tests in Erlang or
8107 Fortran, since those languages require no preprocessor.
8109 @anchor{AC_PREPROC_IFELSE}
8110 @defmac AC_PREPROC_IFELSE (@var{input}, @ovar{action-if-true}, @
8111   @ovar{action-if-false})
8112 @acindex{PREPROC_IFELSE}
8113 Run the preprocessor of the current language (@pxref{Language Choice})
8114 on the @var{input}, run the shell commands @var{action-if-true} on
8115 success, @var{action-if-false} otherwise.  The @var{input} can be made
8116 by @code{AC_LANG_PROGRAM} and friends.
8118 This macro uses @code{CPPFLAGS}, but not @code{CFLAGS}, because
8119 @option{-g}, @option{-O}, etc.@: are not valid options to many C
8120 preprocessors.
8122 It is customary to report unexpected failures with
8123 @code{AC_MSG_FAILURE}.
8124 @end defmac
8126 For instance:
8128 @example
8129 AC_INIT([Hello], [1.0], [bug-hello@@example.org])
8130 AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
8131   [Greetings string.])
8132 AC_PREPROC_IFELSE(
8133    [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
8134                     [[fputs (hw, stdout);]])],
8135    [AC_MSG_RESULT([OK])],
8136    [AC_MSG_FAILURE([unexpected preprocessor failure])])
8137 @end example
8139 @noindent
8140 results in:
8142 @example
8143 checking for gcc... gcc
8144 checking for C compiler default output file name... a.out
8145 checking whether the C compiler works... yes
8146 checking whether we are cross compiling... no
8147 checking for suffix of executables...
8148 checking for suffix of object files... o
8149 checking whether we are using the GNU C compiler... yes
8150 checking whether gcc accepts -g... yes
8151 checking for gcc option to accept ISO C89... none needed
8152 checking how to run the C preprocessor... gcc -E
8154 @end example
8156 @sp 1
8158 The macro @code{AC_TRY_CPP} (@pxref{Obsolete Macros}) used to play the
8159 role of @code{AC_PREPROC_IFELSE}, but double quotes its argument, making
8160 it impossible to use it to elaborate sources.  You are encouraged to
8161 get rid of your old use of the macro @code{AC_TRY_CPP} in favor of
8162 @code{AC_PREPROC_IFELSE}, but, in the first place, are you sure you need
8163 to run the @emph{preprocessor} and not the compiler?
8165 @anchor{AC_EGREP_HEADER}
8166 @defmac AC_EGREP_HEADER (@var{pattern}, @var{header-file}, @
8167   @var{action-if-found}, @ovar{action-if-not-found})
8168 @acindex{EGREP_HEADER}
8169 If the output of running the preprocessor on the system header file
8170 @var{header-file} matches the extended regular expression
8171 @var{pattern}, execute shell commands @var{action-if-found}, otherwise
8172 execute @var{action-if-not-found}.
8173 @end defmac
8175 @anchor{AC_EGREP_CPP}
8176 @defmac AC_EGREP_CPP (@var{pattern}, @var{program}, @
8177   @ovar{action-if-found}, @ovar{action-if-not-found})
8178 @acindex{EGREP_CPP}
8179 @var{program} is the text of a C or C++ program, on which shell
8180 variable, back quote, and backslash substitutions are performed.  If the
8181 output of running the preprocessor on @var{program} matches the
8182 extended regular expression @var{pattern}, execute shell commands
8183 @var{action-if-found}, otherwise execute @var{action-if-not-found}.
8184 @end defmac
8188 @node Running the Compiler
8189 @section Running the Compiler
8191 To check for a syntax feature of the current language's (@pxref{Language
8192 Choice}) compiler, such as whether it recognizes a certain keyword, or
8193 simply to try some library feature, use @code{AC_COMPILE_IFELSE} to try
8194 to compile a small program that uses that feature.
8196 @defmac AC_COMPILE_IFELSE (@var{input}, @ovar{action-if-true}, @
8197   @ovar{action-if-false})
8198 @acindex{COMPILE_IFELSE}
8199 Run the compiler and compilation flags of the current language
8200 (@pxref{Language Choice}) on the @var{input}, run the shell commands
8201 @var{action-if-true} on success, @var{action-if-false} otherwise.  The
8202 @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
8204 It is customary to report unexpected failures with
8205 @code{AC_MSG_FAILURE}.  This macro does not try to link; use
8206 @code{AC_LINK_IFELSE} if you need to do that (@pxref{Running the
8207 Linker}).
8208 @end defmac
8210 @ovindex ERL
8211 For tests in Erlang, the @var{input} must be the source code of a module named
8212 @code{conftest}.  @code{AC_COMPILE_IFELSE} generates a @file{conftest.beam}
8213 file that can be interpreted by the Erlang virtual machine (@code{ERL}).  It is
8214 recommended to use @code{AC_LANG_PROGRAM} to specify the test program,
8215 to ensure that the Erlang module has the right name.
8217 @node Running the Linker
8218 @section Running the Linker
8220 To check for a library, a function, or a global variable, Autoconf
8221 @command{configure} scripts try to compile and link a small program that
8222 uses it.  This is unlike Metaconfig, which by default uses @code{nm} or
8223 @code{ar} on the C library to try to figure out which functions are
8224 available.  Trying to link with the function is usually a more reliable
8225 approach because it avoids dealing with the variations in the options
8226 and output formats of @code{nm} and @code{ar} and in the location of the
8227 standard libraries.  It also allows configuring for cross-compilation or
8228 checking a function's runtime behavior if needed.  On the other hand,
8229 it can be slower than scanning the libraries once, but accuracy is more
8230 important than speed.
8232 @code{AC_LINK_IFELSE} is used to compile test programs to test for
8233 functions and global variables.  It is also used by @code{AC_CHECK_LIB}
8234 to check for libraries (@pxref{Libraries}), by adding the library being
8235 checked for to @code{LIBS} temporarily and trying to link a small
8236 program.
8238 @anchor{AC_LINK_IFELSE}
8239 @defmac AC_LINK_IFELSE (@var{input}, @ovar{action-if-true}, @
8240   @ovar{action-if-false})
8241 @acindex{LINK_IFELSE}
8242 Run the compiler (and compilation flags) and the linker of the current
8243 language (@pxref{Language Choice}) on the @var{input}, run the shell
8244 commands @var{action-if-true} on success, @var{action-if-false}
8245 otherwise.  The @var{input} can be made by @code{AC_LANG_PROGRAM} and
8246 friends.
8248 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
8249 current compilation flags.
8251 It is customary to report unexpected failures with
8252 @code{AC_MSG_FAILURE}.  This macro does not try to execute the program;
8253 use @code{AC_RUN_IFELSE} if you need to do that (@pxref{Runtime}).
8254 @end defmac
8256 The @code{AC_LINK_IFELSE} macro cannot be used for Erlang tests, since Erlang
8257 programs are interpreted and do not require linking.
8261 @node Runtime
8262 @section Checking Runtime Behavior
8264 Sometimes you need to find out how a system performs at runtime, such
8265 as whether a given function has a certain capability or bug.  If you
8266 can, make such checks when your program runs instead of when it is
8267 configured.  You can check for things like the machine's endianness when
8268 your program initializes itself.
8270 If you really need to test for a runtime behavior while configuring,
8271 you can write a test program to determine the result, and compile and
8272 run it using @code{AC_RUN_IFELSE}.  Avoid running test programs if
8273 possible, because this prevents people from configuring your package for
8274 cross-compiling.
8276 @anchor{AC_RUN_IFELSE}
8277 @defmac AC_RUN_IFELSE (@var{input}, @ovar{action-if-true}, @
8278   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
8279 @acindex{RUN_IFELSE}
8280 If @var{program} compiles and links successfully and returns an exit
8281 status of 0 when executed, run shell commands @var{action-if-true}.
8282 Otherwise, run shell commands @var{action-if-false}.
8284 The @var{input} can be made by @code{AC_LANG_PROGRAM} and friends.
8285 @code{LDFLAGS} and @code{LIBS} are used for linking, in addition to the
8286 compilation flags of the current language (@pxref{Language Choice}).
8288 If the compiler being used does not produce executables that run on the
8289 system where @command{configure} is being run, then the test program is
8290 not run.  If the optional shell commands @var{action-if-cross-compiling}
8291 are given, they are run instead.  Otherwise, @command{configure} prints
8292 an error message and exits.
8294 In the @var{action-if-false} section, the failing exit status is
8295 available in the shell variable @samp{$?}.  This exit status might be
8296 that of a failed compilation, or it might be that of a failed program
8297 execution.
8299 It is customary to report unexpected failures with
8300 @code{AC_MSG_FAILURE}.
8301 @end defmac
8303 Try to provide a pessimistic default value to use when cross-compiling
8304 makes runtime tests impossible.  You do this by passing the optional
8305 last argument to @code{AC_RUN_IFELSE}.  @command{autoconf} prints a
8306 warning message when creating @command{configure} each time it
8307 encounters a call to @code{AC_RUN_IFELSE} with no
8308 @var{action-if-cross-compiling} argument given.  You may ignore the
8309 warning, though users cannot configure your package for
8310 cross-compiling.  A few of the macros distributed with Autoconf produce
8311 this warning message.
8313 To configure for cross-compiling you can also choose a value for those
8314 parameters based on the canonical system name (@pxref{Manual
8315 Configuration}).  Alternatively, set up a test results cache file with
8316 the correct values for the host system (@pxref{Caching Results}).
8318 @ovindex cross_compiling
8319 To provide a default for calls of @code{AC_RUN_IFELSE} that are embedded
8320 in other macros, including a few of the ones that come with Autoconf,
8321 you can test whether the shell variable @code{cross_compiling} is set to
8322 @samp{yes}, and then use an alternate method to get the results instead
8323 of calling the macros.
8325 A C or C++ runtime test should be portable.
8326 @xref{Portable C and C++}.
8328 Erlang tests must exit themselves the Erlang VM by calling the @code{halt/1}
8329 function: the given status code is used to determine the success of the test
8330 (status is @code{0}) or its failure (status is different than @code{0}), as
8331 explained above.  It must be noted that data output through the standard output
8332 (e.g., using @code{io:format/2}) may be truncated when halting the VM.
8333 Therefore, if a test must output configuration information, it is recommended
8334 to create and to output data into the temporary file named @file{conftest.out},
8335 using the functions of module @code{file}.  The @code{conftest.out} file is
8336 automatically deleted by the @code{AC_RUN_IFELSE} macro.  For instance, a
8337 simplified implementation of Autoconf's @code{AC_ERLANG_SUBST_LIB_DIR}
8338 macro is:
8340 @example
8341 AC_INIT([LibdirTest], [1.0], [bug-libdirtest@@example.org])
8342 AC_ERLANG_NEED_ERL
8343 AC_LANG(Erlang)
8344 AC_RUN_IFELSE(
8345   [AC_LANG_PROGRAM([], [dnl
8346     file:write_file("conftest.out", code:lib_dir()),
8347     halt(0)])],
8348   [echo "code:lib_dir() returned: `cat conftest.out`"],
8349   [AC_MSG_FAILURE([test Erlang program execution failed])])
8350 @end example
8353 @node Systemology
8354 @section Systemology
8355 @cindex Systemology
8357 This section aims at presenting some systems and pointers to
8358 documentation.  It may help you addressing particular problems reported
8359 by users.
8361 @uref{http://www.opengroup.org/susv3, Posix-conforming systems} are
8362 derived from the @uref{http://www.bell-labs.com/history/unix/, Unix
8363 operating system}.
8365 The @uref{http://bhami.com/rosetta.html, Rosetta Stone for Unix}
8366 contains a table correlating the features of various Posix-conforming
8367 systems.  @uref{http://www.levenez.com/unix/, Unix History} is a
8368 simplified diagram of how many Unix systems were derived from each
8369 other.
8371 @uref{http://heirloom.sourceforge.net/, The Heirloom Project}
8372 provides some variants of traditional implementations of Unix utilities.
8374 @table @asis
8375 @item Darwin
8376 @cindex Darwin
8377 Darwin is also known as Mac OS X@.  Beware that the file system @emph{can} be
8378 case-preserving, but case insensitive.  This can cause nasty problems,
8379 since for instance the installation attempt for a package having an
8380 @file{INSTALL} file can result in @samp{make install} report that
8381 nothing was to be done!
8383 That's all dependent on whether the file system is a UFS (case
8384 sensitive) or HFS+ (case preserving).  By default Apple wants you to
8385 install the OS on HFS+.  Unfortunately, there are some pieces of
8386 software which really need to be built on UFS@.  We may want to rebuild
8387 Darwin to have both UFS and HFS+ available (and put the /local/build
8388 tree on the UFS).
8390 @item @acronym{QNX} 4.25
8391 @cindex @acronym{QNX} 4.25
8392 @c FIXME: Please, if you feel like writing something more precise,
8393 @c it'd be great.  In particular, I can't understand the difference with
8394 @c QNX Neutrino.
8395 @acronym{QNX} is a realtime operating system running on Intel architecture
8396 meant to be scalable from the small embedded systems to the hundred
8397 processor super-computer.  It claims to be Posix certified.  More
8398 information is available on the
8399 @uref{http://www.qnx.com/, @acronym{QNX} home page}.
8401 @item Tru64
8402 @cindex Tru64
8403 @uref{http://h30097.www3.hp.com/@/docs/,
8404 Documentation of several versions of Tru64} is available in different
8405 formats.
8407 @item Unix version 7
8408 @cindex Unix version 7
8409 @cindex V7
8410 Officially this was called the ``Seventh Edition'' of ``the @sc{unix}
8411 time-sharing system'' but we use the more-common name ``Unix version 7''.
8412 Documentation is available in the
8413 @uref{http://plan9.bell-labs.com/@/7thEdMan/, Unix Seventh Edition Manual}.
8414 Previous versions of Unix are called ``Unix version 6'', etc., but
8415 they were not as widely used.
8416 @end table
8419 @node Multiple Cases
8420 @section Multiple Cases
8422 Some operations are accomplished in several possible ways, depending on
8423 the OS variant.  Checking for them essentially requires a ``case
8424 statement''.  Autoconf does not directly provide one; however, it is
8425 easy to simulate by using a shell variable to keep track of whether a
8426 way to perform the operation has been found yet.
8428 Here is an example that uses the shell variable @code{fstype} to keep
8429 track of whether the remaining cases need to be checked.
8431 @example
8432 @group
8433 AC_MSG_CHECKING([how to get file system type])
8434 fstype=no
8435 # The order of these tests is important.
8436 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statvfs.h>
8437 #include <sys/fstyp.h>]])],
8438                   [AC_DEFINE([FSTYPE_STATVFS], [1],
8439                      [Define if statvfs exists.])
8440                    fstype=SVR4])
8441 if test $fstype = no; then
8442   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8443 #include <sys/fstyp.h>]])],
8444                   [AC_DEFINE([FSTYPE_USG_STATFS], [1],
8445                      [Define if USG statfs.])
8446                    fstype=SVR3])
8448 if test $fstype = no; then
8449   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/statfs.h>
8450 #include <sys/vmount.h>]])]),
8451                   [AC_DEFINE([FSTYPE_AIX_STATFS], [1],
8452                      [Define if AIX statfs.])
8453                    fstype=AIX])
8455 # (more cases omitted here)
8456 AC_MSG_RESULT([$fstype])
8457 @end group
8458 @end example
8460 @c ====================================================== Results of Tests.
8462 @node Results
8463 @chapter Results of Tests
8465 Once @command{configure} has determined whether a feature exists, what can
8466 it do to record that information?  There are four sorts of things it can
8467 do: define a C preprocessor symbol, set a variable in the output files,
8468 save the result in a cache file for future @command{configure} runs, and
8469 print a message letting the user know the result of the test.
8471 @menu
8472 * Defining Symbols::            Defining C preprocessor symbols
8473 * Setting Output Variables::    Replacing variables in output files
8474 * Special Chars in Variables::  Characters to beware of in variables
8475 * Caching Results::             Speeding up subsequent @command{configure} runs
8476 * Printing Messages::           Notifying @command{configure} users
8477 @end menu
8479 @node Defining Symbols
8480 @section Defining C Preprocessor Symbols
8482 A common action to take in response to a feature test is to define a C
8483 preprocessor symbol indicating the results of the test.  That is done by
8484 calling @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}.
8486 By default, @code{AC_OUTPUT} places the symbols defined by these macros
8487 into the output variable @code{DEFS}, which contains an option
8488 @option{-D@var{symbol}=@var{value}} for each symbol defined.  Unlike in
8489 Autoconf version 1, there is no variable @code{DEFS} defined while
8490 @command{configure} is running.  To check whether Autoconf macros have
8491 already defined a certain C preprocessor symbol, test the value of the
8492 appropriate cache variable, as in this example:
8494 @example
8495 AC_CHECK_FUNC([vprintf], [AC_DEFINE([HAVE_VPRINTF], [1],
8496                           [Define if vprintf exists.])])
8497 if test "$ac_cv_func_vprintf" != yes; then
8498   AC_CHECK_FUNC([_doprnt], [AC_DEFINE([HAVE_DOPRNT], [1],
8499                             [Define if _doprnt exists.])])
8501 @end example
8503 If @code{AC_CONFIG_HEADERS} has been called, then instead of creating
8504 @code{DEFS}, @code{AC_OUTPUT} creates a header file by substituting the
8505 correct values into @code{#define} statements in a template file.
8506 @xref{Configuration Headers}, for more information about this kind of
8507 output.
8509 @defmac AC_DEFINE (@var{variable}, @var{value}, @ovar{description})
8510 @defmacx AC_DEFINE (@var{variable})
8511 @cvindex @var{variable}
8512 @acindex{DEFINE}
8513 Define @var{variable} to @var{value} (verbatim), by defining a C
8514 preprocessor macro for @var{variable}.  @var{variable} should be a C
8515 identifier, optionally suffixed by a parenthesized argument list to
8516 define a C preprocessor macro with arguments.  The macro argument list,
8517 if present, should be a comma-separated list of C identifiers, possibly
8518 terminated by an ellipsis @samp{...} if C99 syntax is employed.
8519 @var{variable} should not contain comments, white space, trigraphs,
8520 backslash-newlines, universal character names, or non-@acronym{ASCII}
8521 characters.
8523 @var{value} may contain backslash-escaped newlines, which will be
8524 preserved if you use @code{AC_CONFIG_HEADERS} but flattened if passed
8525 via @code{@@DEFS@@} (with no effect on the compilation, since the
8526 preprocessor sees only one line in the first place).  @var{value} should
8527 not contain raw newlines.  If you are not using
8528 @code{AC_CONFIG_HEADERS}, @var{value} should not contain any @samp{#}
8529 characters, as @command{make} tends to eat them.  To use a shell
8530 variable, use @code{AC_DEFINE_UNQUOTED} instead.
8532 @var{description} is only useful if you are using
8533 @code{AC_CONFIG_HEADERS}.  In this case, @var{description} is put into
8534 the generated @file{config.h.in} as the comment before the macro define.
8535 The following example defines the C preprocessor variable
8536 @code{EQUATION} to be the string constant @samp{"$a > $b"}:
8538 @example
8539 AC_DEFINE([EQUATION], ["$a > $b"],
8540   [Equation string.])
8541 @end example
8543 If neither @var{value} nor @var{description} are given, then
8544 @var{value} defaults to 1 instead of to the empty string.  This is for
8545 backwards compatibility with older versions of Autoconf, but this usage
8546 is obsolescent and may be withdrawn in future versions of Autoconf.
8548 If the @var{variable} is a literal string, it is passed to
8549 @code{m4_pattern_allow} (@pxref{Forbidden Patterns}).
8551 If multiple @code{AC_DEFINE} statements are executed for the same
8552 @var{variable} name (not counting any parenthesized argument list),
8553 the last one wins.
8554 @end defmac
8556 @defmac AC_DEFINE_UNQUOTED (@var{variable}, @var{value}, @ovar{description})
8557 @defmacx AC_DEFINE_UNQUOTED (@var{variable})
8558 @acindex{DEFINE_UNQUOTED}
8559 @cvindex @var{variable}
8560 Like @code{AC_DEFINE}, but three shell expansions are
8561 performed---once---on @var{variable} and @var{value}: variable expansion
8562 (@samp{$}), command substitution (@samp{`}), and backslash escaping
8563 (@samp{\}).  Single and double quote characters in the value have no
8564 special meaning.  Use this macro instead of @code{AC_DEFINE} when
8565 @var{variable} or @var{value} is a shell variable.  Examples:
8567 @example
8568 AC_DEFINE_UNQUOTED([config_machfile], ["$machfile"],
8569   [Configuration machine file.])
8570 AC_DEFINE_UNQUOTED([GETGROUPS_T], [$ac_cv_type_getgroups],
8571   [getgroups return type.])
8572 AC_DEFINE_UNQUOTED([$ac_tr_hdr], [1],
8573   [Translated header name.])
8574 @end example
8575 @end defmac
8577 Due to a syntactical bizarreness of the Bourne shell, do not use
8578 semicolons to separate @code{AC_DEFINE} or @code{AC_DEFINE_UNQUOTED}
8579 calls from other macro calls or shell code; that can cause syntax errors
8580 in the resulting @command{configure} script.  Use either blanks or
8581 newlines.  That is, do this:
8583 @example
8584 AC_CHECK_HEADER([elf.h],
8585   [AC_DEFINE([SVR4], [1], [System V Release 4]) LIBS="-lelf $LIBS"])
8586 @end example
8588 @noindent
8589 or this:
8591 @example
8592 AC_CHECK_HEADER([elf.h],
8593   [AC_DEFINE([SVR4], [1], [System V Release 4])
8594    LIBS="-lelf $LIBS"])
8595 @end example
8597 @noindent
8598 instead of this:
8600 @example
8601 AC_CHECK_HEADER([elf.h],
8602   [AC_DEFINE([SVR4], [1], [System V Release 4]); LIBS="-lelf $LIBS"])
8603 @end example
8605 @node Setting Output Variables
8606 @section Setting Output Variables
8607 @cindex Output variables
8609 Another way to record the results of tests is to set @dfn{output
8610 variables}, which are shell variables whose values are substituted into
8611 files that @command{configure} outputs.  The two macros below create new
8612 output variables.  @xref{Preset Output Variables}, for a list of output
8613 variables that are always available.
8615 @defmac AC_SUBST (@var{variable}, @ovar{value})
8616 @acindex{SUBST}
8617 Create an output variable from a shell variable.  Make @code{AC_OUTPUT}
8618 substitute the variable @var{variable} into output files (typically one
8619 or more makefiles).  This means that @code{AC_OUTPUT}
8620 replaces instances of @samp{@@@var{variable}@@} in input files with the
8621 value that the shell variable @var{variable} has when @code{AC_OUTPUT}
8622 is called.  The value can contain any non-@code{NUL} character, including
8623 newline.
8624 Variable occurrences should not overlap: e.g., an input file should
8625 not contain @samp{@@@var{var1}@@@var{var2}@@} if @var{var1} and @var{var2}
8626 are variable names.
8627 The substituted value is not rescanned for more output variables;
8628 occurrences of @samp{@@@var{variable}@@} in the value are inserted
8629 literally into the output file.  (The algorithm uses the special marker
8630 @code{|#_!!_#|} internally, so neither the substituted value nor the
8631 output file may contain @code{|#_!!_#|}.)
8633 If @var{value} is given, in addition assign it to @var{variable}.
8635 The string @var{variable} is passed to @code{m4_pattern_allow}
8636 (@pxref{Forbidden Patterns}).
8637 @end defmac
8639 @defmac AC_SUBST_FILE (@var{variable})
8640 @acindex{SUBST_FILE}
8641 Another way to create an output variable from a shell variable.  Make
8642 @code{AC_OUTPUT} insert (without substitutions) the contents of the file
8643 named by shell variable @var{variable} into output files.  This means
8644 that @code{AC_OUTPUT} replaces instances of
8645 @samp{@@@var{variable}@@} in output files (such as @file{Makefile.in})
8646 with the contents of the file that the shell variable @var{variable}
8647 names when @code{AC_OUTPUT} is called.  Set the variable to
8648 @file{/dev/null} for cases that do not have a file to insert.
8649 This substitution occurs only when the @samp{@@@var{variable}@@} is on a
8650 line by itself, optionally surrounded by spaces and tabs.  The
8651 substitution replaces the whole line, including the spaces, tabs, and
8652 the terminating newline.
8654 This macro is useful for inserting makefile fragments containing
8655 special dependencies or other @code{make} directives for particular host
8656 or target types into makefiles.  For example, @file{configure.ac}
8657 could contain:
8659 @example
8660 AC_SUBST_FILE([host_frag])
8661 host_frag=$srcdir/conf/sun4.mh
8662 @end example
8664 @noindent
8665 and then a @file{Makefile.in} could contain:
8667 @example
8668 @@host_frag@@
8669 @end example
8671 The string @var{variable} is passed to @code{m4_pattern_allow}
8672 (@pxref{Forbidden Patterns}).
8673 @end defmac
8675 @cindex Precious Variable
8676 @cindex Variable, Precious
8677 Running @command{configure} in varying environments can be extremely
8678 dangerous.  If for instance the user runs @samp{CC=bizarre-cc
8679 ./configure}, then the cache, @file{config.h}, and many other output
8680 files depend upon @command{bizarre-cc} being the C compiler.  If
8681 for some reason the user runs @command{./configure} again, or if it is
8682 run via @samp{./config.status --recheck}, (@xref{Automatic Remaking},
8683 and @pxref{config.status Invocation}), then the configuration can be
8684 inconsistent, composed of results depending upon two different
8685 compilers.
8687 Environment variables that affect this situation, such as @samp{CC}
8688 above, are called @dfn{precious variables}, and can be declared as such
8689 by @code{AC_ARG_VAR}.
8691 @defmac AC_ARG_VAR (@var{variable}, @var{description})
8692 @acindex{ARG_VAR}
8693 Declare @var{variable} is a precious variable, and include its
8694 @var{description} in the variable section of @samp{./configure --help}.
8696 Being precious means that
8697 @itemize @minus
8698 @item
8699 @var{variable} is substituted via @code{AC_SUBST}.
8701 @item
8702 The value of @var{variable} when @command{configure} was launched is
8703 saved in the cache, including if it was not specified on the command
8704 line but via the environment.  Indeed, while @command{configure} can
8705 notice the definition of @code{CC} in @samp{./configure CC=bizarre-cc},
8706 it is impossible to notice it in @samp{CC=bizarre-cc ./configure},
8707 which, unfortunately, is what most users do.
8709 We emphasize that it is the @emph{initial} value of @var{variable} which
8710 is saved, not that found during the execution of @command{configure}.
8711 Indeed, specifying @samp{./configure FOO=foo} and letting
8712 @samp{./configure} guess that @code{FOO} is @code{foo} can be two
8713 different things.
8715 @item
8716 @var{variable} is checked for consistency between two
8717 @command{configure} runs.  For instance:
8719 @example
8720 $ @kbd{./configure --silent --config-cache}
8721 $ @kbd{CC=cc ./configure --silent --config-cache}
8722 configure: error: `CC' was not set in the previous run
8723 configure: error: changes in the environment can compromise \
8724 the build
8725 configure: error: run `make distclean' and/or \
8726 `rm config.cache' and start over
8727 @end example
8729 @noindent
8730 and similarly if the variable is unset, or if its content is changed.
8733 @item
8734 @var{variable} is kept during automatic reconfiguration
8735 (@pxref{config.status Invocation}) as if it had been passed as a command
8736 line argument, including when no cache is used:
8738 @example
8739 $ @kbd{CC=/usr/bin/cc ./configure var=raboof --silent}
8740 $ @kbd{./config.status --recheck}
8741 running CONFIG_SHELL=/bin/sh /bin/sh ./configure var=raboof \
8742   CC=/usr/bin/cc  --no-create --no-recursion
8743 @end example
8744 @end itemize
8745 @end defmac
8747 @node Special Chars in Variables
8748 @section Special Characters in Output Variables
8749 @cindex Output variables, special characters in
8751 Many output variables are intended to be evaluated both by
8752 @command{make} and by the shell.  Some characters are expanded
8753 differently in these two contexts, so to avoid confusion these
8754 variables' values should not contain any of the following characters:
8756 @example
8757 " # $ & ' ( ) * ; < > ? [ \ ^ ` |
8758 @end example
8760 Also, these variables' values should neither contain newlines, nor start
8761 with @samp{~}, nor contain white space or @samp{:} immediately followed
8762 by @samp{~}.  The values can contain nonempty sequences of white space
8763 characters like tabs and spaces, but each such sequence might
8764 arbitrarily be replaced by a single space during substitution.
8766 These restrictions apply both to the values that @command{configure}
8767 computes, and to the values set directly by the user.  For example, the
8768 following invocations of @command{configure} are problematic, since they
8769 attempt to use special characters within @code{CPPFLAGS} and white space
8770 within @code{$(srcdir)}:
8772 @example
8773 CPPFLAGS='-DOUCH="&\"#$*?"' '../My Source/ouch-1.0/configure'
8775 '../My Source/ouch-1.0/configure' CPPFLAGS='-DOUCH="&\"#$*?"'
8776 @end example
8778 @node Caching Results
8779 @section Caching Results
8780 @cindex Cache
8782 To avoid checking for the same features repeatedly in various
8783 @command{configure} scripts (or in repeated runs of one script),
8784 @command{configure} can optionally save the results of many checks in a
8785 @dfn{cache file} (@pxref{Cache Files}).  If a @command{configure} script
8786 runs with caching enabled and finds a cache file, it reads the results
8787 of previous runs from the cache and avoids rerunning those checks.  As a
8788 result, @command{configure} can then run much faster than if it had to
8789 perform all of the checks every time.
8791 @defmac AC_CACHE_VAL (@var{cache-id}, @var{commands-to-set-it})
8792 @acindex{CACHE_VAL}
8793 Ensure that the results of the check identified by @var{cache-id} are
8794 available.  If the results of the check were in the cache file that was
8795 read, and @command{configure} was not given the @option{--quiet} or
8796 @option{--silent} option, print a message saying that the result was
8797 cached; otherwise, run the shell commands @var{commands-to-set-it}.  If
8798 the shell commands are run to determine the value, the value is
8799 saved in the cache file just before @command{configure} creates its output
8800 files.  @xref{Cache Variable Names}, for how to choose the name of the
8801 @var{cache-id} variable.
8803 The @var{commands-to-set-it} @emph{must have no side effects} except for
8804 setting the variable @var{cache-id}, see below.
8805 @end defmac
8807 @defmac AC_CACHE_CHECK (@var{message}, @var{cache-id}, @
8808   @var{commands-to-set-it})
8809 @acindex{CACHE_CHECK}
8810 A wrapper for @code{AC_CACHE_VAL} that takes care of printing the
8811 messages.  This macro provides a convenient shorthand for the most
8812 common way to use these macros.  It calls @code{AC_MSG_CHECKING} for
8813 @var{message}, then @code{AC_CACHE_VAL} with the @var{cache-id} and
8814 @var{commands} arguments, and @code{AC_MSG_RESULT} with @var{cache-id}.
8816 The @var{commands-to-set-it} @emph{must have no side effects} except for
8817 setting the variable @var{cache-id}, see below.
8818 @end defmac
8820 It is common to find buggy macros using @code{AC_CACHE_VAL} or
8821 @code{AC_CACHE_CHECK}, because people are tempted to call
8822 @code{AC_DEFINE} in the @var{commands-to-set-it}.  Instead, the code that
8823 @emph{follows} the call to @code{AC_CACHE_VAL} should call
8824 @code{AC_DEFINE}, by examining the value of the cache variable.  For
8825 instance, the following macro is broken:
8827 @example
8828 @group
8829 AC_DEFUN([AC_SHELL_TRUE],
8830 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
8831                 [my_cv_shell_true_works=no
8832                  (true) 2>/dev/null && my_cv_shell_true_works=yes
8833                  if test "$my_cv_shell_true_works" = yes; then
8834                    AC_DEFINE([TRUE_WORKS], [1],
8835                              [Define if `true(1)' works properly.])
8836                  fi])
8838 @end group
8839 @end example
8841 @noindent
8842 This fails if the cache is enabled: the second time this macro is run,
8843 @code{TRUE_WORKS} @emph{will not be defined}.  The proper implementation
8846 @example
8847 @group
8848 AC_DEFUN([AC_SHELL_TRUE],
8849 [AC_CACHE_CHECK([whether true(1) works], [my_cv_shell_true_works],
8850                 [my_cv_shell_true_works=no
8851                  (true) 2>/dev/null && my_cv_shell_true_works=yes])
8852  if test "$my_cv_shell_true_works" = yes; then
8853    AC_DEFINE([TRUE_WORKS], [1],
8854              [Define if `true(1)' works properly.])
8855  fi
8857 @end group
8858 @end example
8860 Also, @var{commands-to-set-it} should not print any messages, for
8861 example with @code{AC_MSG_CHECKING}; do that before calling
8862 @code{AC_CACHE_VAL}, so the messages are printed regardless of whether
8863 the results of the check are retrieved from the cache or determined by
8864 running the shell commands.
8866 @menu
8867 * Cache Variable Names::        Shell variables used in caches
8868 * Cache Files::                 Files @command{configure} uses for caching
8869 * Cache Checkpointing::         Loading and saving the cache file
8870 @end menu
8872 @node Cache Variable Names
8873 @subsection Cache Variable Names
8874 @cindex Cache variable
8876 The names of cache variables should have the following format:
8878 @example
8879 @var{package-prefix}_cv_@var{value-type}_@var{specific-value}_@ovar{additional-options}
8880 @end example
8882 @noindent
8883 for example, @samp{ac_cv_header_stat_broken} or
8884 @samp{ac_cv_prog_gcc_traditional}.  The parts of the variable name are:
8886 @table @asis
8887 @item @var{package-prefix}
8888 An abbreviation for your package or organization; the same prefix you
8889 begin local Autoconf macros with, except lowercase by convention.
8890 For cache values used by the distributed Autoconf macros, this value is
8891 @samp{ac}.
8893 @item @code{_cv_}
8894 Indicates that this shell variable is a cache value.  This string
8895 @emph{must} be present in the variable name, including the leading
8896 underscore.
8898 @item @var{value-type}
8899 A convention for classifying cache values, to produce a rational naming
8900 system.  The values used in Autoconf are listed in @ref{Macro Names}.
8902 @item @var{specific-value}
8903 Which member of the class of cache values this test applies to.
8904 For example, which function (@samp{alloca}), program (@samp{gcc}), or
8905 output variable (@samp{INSTALL}).
8907 @item @var{additional-options}
8908 Any particular behavior of the specific member that this test applies to.
8909 For example, @samp{broken} or @samp{set}.  This part of the name may
8910 be omitted if it does not apply.
8911 @end table
8913 The values assigned to cache variables may not contain newlines.
8914 Usually, their values are Boolean (@samp{yes} or @samp{no}) or the
8915 names of files or functions; so this is not an important restriction.
8917 @node Cache Files
8918 @subsection Cache Files
8920 A cache file is a shell script that caches the results of configure
8921 tests run on one system so they can be shared between configure scripts
8922 and configure runs.  It is not useful on other systems.  If its contents
8923 are invalid for some reason, the user may delete or edit it.
8925 By default, @command{configure} uses no cache file,
8926 to avoid problems caused by accidental
8927 use of stale cache files.
8929 To enable caching, @command{configure} accepts @option{--config-cache} (or
8930 @option{-C}) to cache results in the file @file{config.cache}.
8931 Alternatively, @option{--cache-file=@var{file}} specifies that
8932 @var{file} be the cache file.  The cache file is created if it does not
8933 exist already.  When @command{configure} calls @command{configure} scripts in
8934 subdirectories, it uses the @option{--cache-file} argument so that they
8935 share the same cache.  @xref{Subdirectories}, for information on
8936 configuring subdirectories with the @code{AC_CONFIG_SUBDIRS} macro.
8938 @file{config.status} only pays attention to the cache file if it is
8939 given the @option{--recheck} option, which makes it rerun
8940 @command{configure}.
8942 It is wrong to try to distribute cache files for particular system types.
8943 There is too much room for error in doing that, and too much
8944 administrative overhead in maintaining them.  For any features that
8945 can't be guessed automatically, use the standard method of the canonical
8946 system type and linking files (@pxref{Manual Configuration}).
8948 The site initialization script can specify a site-wide cache file to
8949 use, instead of the usual per-program cache.  In this case, the cache
8950 file gradually accumulates information whenever someone runs a new
8951 @command{configure} script.  (Running @command{configure} merges the new cache
8952 results with the existing cache file.)  This may cause problems,
8953 however, if the system configuration (e.g., the installed libraries or
8954 compilers) changes and the stale cache file is not deleted.
8956 @node Cache Checkpointing
8957 @subsection Cache Checkpointing
8959 If your configure script, or a macro called from @file{configure.ac}, happens
8960 to abort the configure process, it may be useful to checkpoint the cache
8961 a few times at key points using @code{AC_CACHE_SAVE}.  Doing so
8962 reduces the amount of time it takes to rerun the configure script with
8963 (hopefully) the error that caused the previous abort corrected.
8965 @c FIXME: Do we really want to document this guy?
8966 @defmac AC_CACHE_LOAD
8967 @acindex{CACHE_LOAD}
8968 Loads values from existing cache file, or creates a new cache file if a
8969 cache file is not found.  Called automatically from @code{AC_INIT}.
8970 @end defmac
8972 @defmac AC_CACHE_SAVE
8973 @acindex{CACHE_SAVE}
8974 Flushes all cached values to the cache file.  Called automatically from
8975 @code{AC_OUTPUT}, but it can be quite useful to call
8976 @code{AC_CACHE_SAVE} at key points in @file{configure.ac}.
8977 @end defmac
8979 For instance:
8981 @example
8982 @r{ @dots{} AC_INIT, etc. @dots{}}
8983 @group
8984 # Checks for programs.
8985 AC_PROG_CC
8986 AC_PROG_AWK
8987 @r{ @dots{} more program checks @dots{}}
8988 AC_CACHE_SAVE
8989 @end group
8991 @group
8992 # Checks for libraries.
8993 AC_CHECK_LIB([nsl], [gethostbyname])
8994 AC_CHECK_LIB([socket], [connect])
8995 @r{ @dots{} more lib checks @dots{}}
8996 AC_CACHE_SAVE
8997 @end group
8999 @group
9000 # Might abort@dots{}
9001 AM_PATH_GTK([1.0.2], [], [AC_MSG_ERROR([GTK not in path])])
9002 AM_PATH_GTKMM([0.9.5], [], [AC_MSG_ERROR([GTK not in path])])
9003 @end group
9004 @r{ @dots{} AC_OUTPUT, etc. @dots{}}
9005 @end example
9007 @node Printing Messages
9008 @section Printing Messages
9009 @cindex Messages, from @command{configure}
9011 @command{configure} scripts need to give users running them several kinds
9012 of information.  The following macros print messages in ways appropriate
9013 for each kind.  The arguments to all of them get enclosed in shell
9014 double quotes, so the shell performs variable and back-quote
9015 substitution on them.
9017 These macros are all wrappers around the @command{echo} shell command.
9018 They direct output to the appropriate file descriptor (@pxref{File
9019 Descriptor Macros}).
9020 @command{configure} scripts should rarely need to run @command{echo} directly
9021 to print messages for the user.  Using these macros makes it easy to
9022 change how and when each kind of message is printed; such changes need
9023 only be made to the macro definitions and all the callers change
9024 automatically.
9026 To diagnose static issues, i.e., when @command{autoconf} is run, see
9027 @ref{Diagnostic Macros}.
9029 @defmac AC_MSG_CHECKING (@var{feature-description})
9030 @acindex{MSG_CHECKING}
9031 Notify the user that @command{configure} is checking for a particular
9032 feature.  This macro prints a message that starts with @samp{checking }
9033 and ends with @samp{...} and no newline.  It must be followed by a call
9034 to @code{AC_MSG_RESULT} to print the result of the check and the
9035 newline.  The @var{feature-description} should be something like
9036 @samp{whether the Fortran compiler accepts C++ comments} or @samp{for
9037 c89}.
9039 This macro prints nothing if @command{configure} is run with the
9040 @option{--quiet} or @option{--silent} option.
9041 @end defmac
9043 @anchor{AC_MSG_RESULT}
9044 @defmac AC_MSG_RESULT (@var{result-description})
9045 @acindex{MSG_RESULT}
9046 Notify the user of the results of a check.  @var{result-description} is
9047 almost always the value of the cache variable for the check, typically
9048 @samp{yes}, @samp{no}, or a file name.  This macro should follow a call
9049 to @code{AC_MSG_CHECKING}, and the @var{result-description} should be
9050 the completion of the message printed by the call to
9051 @code{AC_MSG_CHECKING}.
9053 This macro prints nothing if @command{configure} is run with the
9054 @option{--quiet} or @option{--silent} option.
9055 @end defmac
9057 @anchor{AC_MSG_NOTICE}
9058 @defmac AC_MSG_NOTICE (@var{message})
9059 @acindex{MSG_NOTICE}
9060 Deliver the @var{message} to the user.  It is useful mainly to print a
9061 general description of the overall purpose of a group of feature checks,
9062 e.g.,
9064 @example
9065 AC_MSG_NOTICE([checking if stack overflow is detectable])
9066 @end example
9068 This macro prints nothing if @command{configure} is run with the
9069 @option{--quiet} or @option{--silent} option.
9070 @end defmac
9072 @anchor{AC_MSG_ERROR}
9073 @defmac AC_MSG_ERROR (@var{error-description}, @ovar{exit-status})
9074 @acindex{MSG_ERROR}
9075 Notify the user of an error that prevents @command{configure} from
9076 completing.  This macro prints an error message to the standard error
9077 output and exits @command{configure} with @var{exit-status} (1 by default).
9078 @var{error-description} should be something like @samp{invalid value
9079 $HOME for \$HOME}.
9081 The @var{error-description} should start with a lower-case letter, and
9082 ``cannot'' is preferred to ``can't''.
9083 @end defmac
9085 @defmac AC_MSG_FAILURE (@var{error-description}, @ovar{exit-status})
9086 @acindex{MSG_FAILURE}
9087 This @code{AC_MSG_ERROR} wrapper notifies the user of an error that
9088 prevents @command{configure} from completing @emph{and} that additional
9089 details are provided in @file{config.log}.  This is typically used when
9090 abnormal results are found during a compilation.
9091 @end defmac
9093 @anchor{AC_MSG_WARN}
9094 @defmac AC_MSG_WARN (@var{problem-description})
9095 @acindex{MSG_WARN}
9096 Notify the @command{configure} user of a possible problem.  This macro
9097 prints the message to the standard error output; @command{configure}
9098 continues running afterward, so macros that call @code{AC_MSG_WARN} should
9099 provide a default (back-up) behavior for the situations they warn about.
9100 @var{problem-description} should be something like @samp{ln -s seems to
9101 make hard links}.
9102 @end defmac
9106 @c ====================================================== Programming in M4.
9108 @node Programming in M4
9109 @chapter Programming in M4
9110 @cindex M4
9112 Autoconf is written on top of two layers: @dfn{M4sugar}, which provides
9113 convenient macros for pure M4 programming, and @dfn{M4sh}, which
9114 provides macros dedicated to shell script generation.
9116 As of this version of Autoconf, these two layers still contain
9117 experimental macros, whose interface might change in the future.  As a
9118 matter of fact, @emph{anything that is not documented must not be used}.
9120 @menu
9121 * M4 Quotation::                Protecting macros from unwanted expansion
9122 * Using autom4te::              The Autoconf executables backbone
9123 * Programming in M4sugar::      Convenient pure M4 macros
9124 * Programming in M4sh::         Common shell Constructs
9125 * File Descriptor Macros::      File descriptor macros for input and output
9126 @end menu
9128 @node M4 Quotation
9129 @section M4 Quotation
9130 @cindex M4 quotation
9131 @cindex quotation
9133 The most common problem with existing macros is an improper quotation.
9134 This section, which users of Autoconf can skip, but which macro writers
9135 @emph{must} read, first justifies the quotation scheme that was chosen
9136 for Autoconf and then ends with a rule of thumb.  Understanding the
9137 former helps one to follow the latter.
9139 @menu
9140 * Active Characters::           Characters that change the behavior of M4
9141 * One Macro Call::              Quotation and one macro call
9142 * Quoting and Parameters::      M4 vs. shell parameters
9143 * Quotation and Nested Macros::  Macros calling macros
9144 * Changequote is Evil::         Worse than INTERCAL: M4 + changequote
9145 * Quadrigraphs::                Another way to escape special characters
9146 * Quotation Rule Of Thumb::     One parenthesis, one quote
9147 @end menu
9149 @node Active Characters
9150 @subsection Active Characters
9152 To fully understand where proper quotation is important, you first need
9153 to know what the special characters are in Autoconf: @samp{#} introduces
9154 a comment inside which no macro expansion is performed, @samp{,}
9155 separates arguments, @samp{[} and @samp{]} are the quotes themselves,
9156 @samp{(} and @samp{)} (which M4 tries to match by pairs), and finally
9157 @samp{$} inside a macro definition.
9159 In order to understand the delicate case of macro calls, we first have
9160 to present some obvious failures.  Below they are ``obvious-ified'',
9161 but when you find them in real life, they are usually in disguise.
9163 Comments, introduced by a hash and running up to the newline, are opaque
9164 tokens to the top level: active characters are turned off, and there is
9165 no macro expansion:
9167 @example
9168 # define([def], ine)
9169 @result{}# define([def], ine)
9170 @end example
9172 Each time there can be a macro expansion, there is a quotation
9173 expansion, i.e., one level of quotes is stripped:
9175 @example
9176 int tab[10];
9177 @result{}int tab10;
9178 [int tab[10];]
9179 @result{}int tab[10];
9180 @end example
9182 Without this in mind, the reader might try hopelessly to use her macro
9183 @code{array}:
9185 @example
9186 define([array], [int tab[10];])
9187 array
9188 @result{}int tab10;
9189 [array]
9190 @result{}array
9191 @end example
9193 @noindent
9194 How can you correctly output the intended results@footnote{Using
9195 @code{defn}.}?
9198 @node One Macro Call
9199 @subsection One Macro Call
9201 Let's proceed on the interaction between active characters and macros
9202 with this small macro, which just returns its first argument:
9204 @example
9205 define([car], [$1])
9206 @end example
9208 @noindent
9209 The two pairs of quotes above are not part of the arguments of
9210 @code{define}; rather, they are understood by the top level when it
9211 tries to find the arguments of @code{define}.  Therefore, assuming
9212 @code{car} is not already defined, it is equivalent to write:
9214 @example
9215 define(car, $1)
9216 @end example
9218 @noindent
9219 But, while it is acceptable for a @file{configure.ac} to avoid unnecessary
9220 quotes, it is bad practice for Autoconf macros which must both be more
9221 robust and also advocate perfect style.
9223 At the top level, there are only two possibilities: either you
9224 quote or you don't:
9226 @example
9227 car(foo, bar, baz)
9228 @result{}foo
9229 [car(foo, bar, baz)]
9230 @result{}car(foo, bar, baz)
9231 @end example
9233 Let's pay attention to the special characters:
9235 @example
9236 car(#)
9237 @error{}EOF in argument list
9238 @end example
9240 The closing parenthesis is hidden in the comment; with a hypothetical
9241 quoting, the top level understood it this way:
9243 @example
9244 car([#)]
9245 @end example
9247 @noindent
9248 Proper quotation, of course, fixes the problem:
9250 @example
9251 car([#])
9252 @result{}#
9253 @end example
9255 Here are more examples:
9257 @example
9258 car(foo, bar)
9259 @result{}foo
9260 car([foo, bar])
9261 @result{}foo, bar
9262 car((foo, bar))
9263 @result{}(foo, bar)
9264 car([(foo], [bar)])
9265 @result{}(foo
9266 define([a], [b])
9267 @result{}
9268 car(a)
9269 @result{}b
9270 car([a])
9271 @result{}b
9272 car([[a]])
9273 @result{}a
9274 car([[[a]]])
9275 @result{}[a]
9276 @end example
9278 @node Quoting and Parameters
9279 @subsection
9281 When M4 encounters @samp{$} within a macro definition, followed
9282 immediately by a character it recognizes (@samp{0}@dots{}@samp{9},
9283 @samp{#}, @samp{@@}, or @samp{*}), it will perform M4 parameter
9284 expansion.  This happens regardless of how many layers of quotes the
9285 parameter expansion is nested within, or even if it occurs in text that
9286 will be rescanned as a comment.
9288 @example
9289 define([none], [$1])
9290 @result{}
9291 define([one], [[$1]])
9292 @result{}
9293 define([two], [[[$1]]])
9294 @result{}
9295 define([comment], [# $1])
9296 @result{}
9297 define([active], [ACTIVE])
9298 @result{}
9299 none([active])
9300 @result{}ACTIVE
9301 one([active])
9302 @result{}active
9303 two([active])
9304 @result{}[active]
9305 comment([active])
9306 @result{}# active
9307 @end example
9309 On the other hand, since autoconf generates shell code, you often want
9310 to output shell variable expansion, rather than performing M4 parameter
9311 expansion.  To do this, you must use M4 quoting to separate the @samp{$}
9312 from the next character in the definition of your macro.  If the macro
9313 definition occurs in single-quoted text, then insert another level of
9314 quoting; if the usage is already inside a double-quoted string, then
9315 split it into concatenated strings.
9317 @example
9318 define([single], [a single-quoted $[]1 definition])
9319 @result{}
9320 define([double], [[a double-quoted $][1 definition]])
9321 @result{}
9322 single
9323 @result{}a single-quoted $1 definition
9324 double
9325 @result{}a double-quoted $1 definition
9326 @end example
9328 Posix states that M4 implementations are free to provide implementation
9329 extensions when @samp{$@{} is encountered in a macro definition.
9330 Autoconf reserves the longer sequence @samp{$@{@{} for use with planned
9331 extensions that will be available in the future @acronym{GNU} M4 2.0,
9332 but guarantees that all other instances of @samp{$@{} will be output
9333 literally.  Therefore, this idiom can also be used to output shell code
9334 parameter references:
9336 @example
9337 define([first], [$@{1@}])first
9338 @result{}$@{1@}
9339 @end example
9341 Posix also states that @samp{$11} should expand to the first parameter
9342 concatenated with a literal @samp{1}, although some versions of
9343 @acronym{GNU} M4 expand the eleventh parameter instead.  For
9344 portability, you should only use single-digit M4 parameter expansion.
9346 With this in mind, we can explore the cases where macros invoke
9347 macros@enddots{}
9349 @node Quotation and Nested Macros
9350 @subsection Quotation and Nested Macros
9352 The examples below use the following macros:
9354 @example
9355 define([car], [$1])
9356 define([active], [ACT, IVE])
9357 define([array], [int tab[10]])
9358 @end example
9360 Each additional embedded macro call introduces other possible
9361 interesting quotations:
9363 @example
9364 car(active)
9365 @result{}ACT
9366 car([active])
9367 @result{}ACT, IVE
9368 car([[active]])
9369 @result{}active
9370 @end example
9372 In the first case, the top level looks for the arguments of @code{car},
9373 and finds @samp{active}.  Because M4 evaluates its arguments
9374 before applying the macro, @samp{active} is expanded, which results in:
9376 @example
9377 car(ACT, IVE)
9378 @result{}ACT
9379 @end example
9381 @noindent
9382 In the second case, the top level gives @samp{active} as first and only
9383 argument of @code{car}, which results in:
9385 @example
9386 active
9387 @result{}ACT, IVE
9388 @end example
9390 @noindent
9391 i.e., the argument is evaluated @emph{after} the macro that invokes it.
9392 In the third case, @code{car} receives @samp{[active]}, which results in:
9394 @example
9395 [active]
9396 @result{}active
9397 @end example
9399 @noindent
9400 exactly as we already saw above.
9402 The example above, applied to a more realistic example, gives:
9404 @example
9405 car(int tab[10];)
9406 @result{}int tab10;
9407 car([int tab[10];])
9408 @result{}int tab10;
9409 car([[int tab[10];]])
9410 @result{}int tab[10];
9411 @end example
9413 @noindent
9414 Huh?  The first case is easily understood, but why is the second wrong,
9415 and the third right?  To understand that, you must know that after
9416 M4 expands a macro, the resulting text is immediately subjected
9417 to macro expansion and quote removal.  This means that the quote removal
9418 occurs twice---first before the argument is passed to the @code{car}
9419 macro, and second after the @code{car} macro expands to the first
9420 argument.
9422 As the author of the Autoconf macro @code{car}, you then consider it to
9423 be incorrect that your users have to double-quote the arguments of
9424 @code{car}, so you ``fix'' your macro.  Let's call it @code{qar} for
9425 quoted car:
9427 @example
9428 define([qar], [[$1]])
9429 @end example
9431 @noindent
9432 and check that @code{qar} is properly fixed:
9434 @example
9435 qar([int tab[10];])
9436 @result{}int tab[10];
9437 @end example
9439 @noindent
9440 Ahhh!  That's much better.
9442 But note what you've done: now that the result of @code{qar} is always
9443 a literal string, the only time a user can use nested macros is if she
9444 relies on an @emph{unquoted} macro call:
9446 @example
9447 qar(active)
9448 @result{}ACT
9449 qar([active])
9450 @result{}active
9451 @end example
9453 @noindent
9454 leaving no way for her to reproduce what she used to do with @code{car}:
9456 @example
9457 car([active])
9458 @result{}ACT, IVE
9459 @end example
9461 @noindent
9462 Worse yet: she wants to use a macro that produces a set of @code{cpp}
9463 macros:
9465 @example
9466 define([my_includes], [#include <stdio.h>])
9467 car([my_includes])
9468 @result{}#include <stdio.h>
9469 qar(my_includes)
9470 @error{}EOF in argument list
9471 @end example
9473 This macro, @code{qar}, because it double quotes its arguments, forces
9474 its users to leave their macro calls unquoted, which is dangerous.
9475 Commas and other active symbols are interpreted by M4 before
9476 they are given to the macro, often not in the way the users expect.
9477 Also, because @code{qar} behaves differently from the other macros,
9478 it's an exception that should be avoided in Autoconf.
9480 @node Changequote is Evil
9481 @subsection @code{changequote} is Evil
9482 @cindex @code{changequote}
9484 The temptation is often high to bypass proper quotation, in particular
9485 when it's late at night.  Then, many experienced Autoconf hackers
9486 finally surrender to the dark side of the force and use the ultimate
9487 weapon: @code{changequote}.
9489 The M4 builtin @code{changequote} belongs to a set of primitives that
9490 allow one to adjust the syntax of the language to adjust it to one's
9491 needs.  For instance, by default M4 uses @samp{`} and @samp{'} as
9492 quotes, but in the context of shell programming (and actually of most
9493 programming languages), that's about the worst choice one can make:
9494 because of strings and back-quoted expressions in shell code (such as
9495 @samp{'this'} and @samp{`that`}), and because of literal characters in usual
9496 programming languages (as in @samp{'0'}), there are many unbalanced
9497 @samp{`} and @samp{'}.  Proper M4 quotation then becomes a nightmare, if
9498 not impossible.  In order to make M4 useful in such a context, its
9499 designers have equipped it with @code{changequote}, which makes it
9500 possible to choose another pair of quotes.  M4sugar, M4sh, Autoconf, and
9501 Autotest all have chosen to use @samp{[} and @samp{]}.  Not especially
9502 because they are unlikely characters, but @emph{because they are
9503 characters unlikely to be unbalanced}.
9505 There are other magic primitives, such as @code{changecom} to specify
9506 what syntactic forms are comments (it is common to see
9507 @samp{changecom(<!--, -->)} when M4 is used to produce HTML pages),
9508 @code{changeword} and @code{changesyntax} to change other syntactic
9509 details (such as the character to denote the @var{n}th argument, @samp{$} by
9510 default, the parentheses around arguments, etc.).
9512 These primitives are really meant to make M4 more useful for specific
9513 domains: they should be considered like command line options:
9514 @option{--quotes}, @option{--comments}, @option{--words}, and
9515 @option{--syntax}.  Nevertheless, they are implemented as M4 builtins, as
9516 it makes M4 libraries self contained (no need for additional options).
9518 There lies the problem@enddots{}
9520 @sp 1
9522 The problem is that it is then tempting to use them in the middle of an
9523 M4 script, as opposed to its initialization.  This, if not carefully
9524 thought out, can lead to disastrous effects: @emph{you are changing the
9525 language in the middle of the execution}.  Changing and restoring the
9526 syntax is often not enough: if you happened to invoke macros in between,
9527 these macros are lost, as the current syntax is probably not
9528 the one they were implemented with.
9530 @c FIXME: I've been looking for a short, real case example, but I
9531 @c lost them all :(
9534 @node Quadrigraphs
9535 @subsection Quadrigraphs
9536 @cindex quadrigraphs
9537 @cindex @samp{@@S|@@}
9538 @cindex @samp{@@&t@@}
9539 @c Info cannot handle `:' in index entries.
9540 @c @cindex @samp{@@<:@@}
9541 @c @cindex @samp{@@:>@@}
9542 @c @cindex @samp{@@%:@@}
9544 When writing an Autoconf macro you may occasionally need to generate
9545 special characters that are difficult to express with the standard
9546 Autoconf quoting rules.  For example, you may need to output the regular
9547 expression @samp{[^[]}, which matches any character other than @samp{[}.
9548 This expression contains unbalanced brackets so it cannot be put easily
9549 into an M4 macro.
9551 You can work around this problem by using one of the following
9552 @dfn{quadrigraphs}:
9554 @table @samp
9555 @item @@<:@@
9556 @samp{[}
9557 @item @@:>@@
9558 @samp{]}
9559 @item @@S|@@
9560 @samp{$}
9561 @item @@%:@@
9562 @samp{#}
9563 @item @@&t@@
9564 Expands to nothing.
9565 @end table
9567 Quadrigraphs are replaced at a late stage of the translation process,
9568 after @command{m4} is run, so they do not get in the way of M4 quoting.
9569 For example, the string @samp{^@@<:@@}, independently of its quotation,
9570 appears as @samp{^[} in the output.
9572 The empty quadrigraph can be used:
9574 @itemize @minus
9575 @item to mark trailing spaces explicitly
9577 Trailing spaces are smashed by @command{autom4te}.  This is a feature.
9579 @item to produce other quadrigraphs
9581 For instance @samp{@@<@@&t@@:@@} produces @samp{@@<:@@}.
9583 @item to escape @emph{occurrences} of forbidden patterns
9585 For instance you might want to mention @code{AC_FOO} in a comment, while
9586 still being sure that @command{autom4te} still catches unexpanded
9587 @samp{AC_*}.  Then write @samp{AC@@&t@@_FOO}.
9588 @end itemize
9590 The name @samp{@@&t@@} was suggested by Paul Eggert:
9592 @quotation
9593 I should give some credit to the @samp{@@&t@@} pun.  The @samp{&} is my
9594 own invention, but the @samp{t} came from the source code of the
9595 @sc{algol68c} compiler, written by Steve Bourne (of Bourne shell fame),
9596 and which used @samp{mt} to denote the empty string.  In C, it would
9597 have looked like something like:
9599 @example
9600 char const mt[] = "";
9601 @end example
9603 @noindent
9604 but of course the source code was written in Algol 68.
9606 I don't know where he got @samp{mt} from: it could have been his own
9607 invention, and I suppose it could have been a common pun around the
9608 Cambridge University computer lab at the time.
9609 @end quotation
9611 @node Quotation Rule Of Thumb
9612 @subsection Quotation Rule Of Thumb
9614 To conclude, the quotation rule of thumb is:
9616 @center @emph{One pair of quotes per pair of parentheses.}
9618 Never over-quote, never under-quote, in particular in the definition of
9619 macros.  In the few places where the macros need to use brackets
9620 (usually in C program text or regular expressions), properly quote
9621 @emph{the arguments}!
9623 It is common to read Autoconf programs with snippets like:
9625 @example
9626 AC_TRY_LINK(
9627 changequote(<<, >>)dnl
9628 <<#include <time.h>
9629 #ifndef tzname /* For SGI.  */
9630 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9631 #endif>>,
9632 changequote([, ])dnl
9633 [atoi (*tzname);], ac_cv_var_tzname=yes, ac_cv_var_tzname=no)
9634 @end example
9636 @noindent
9637 which is incredibly useless since @code{AC_TRY_LINK} is @emph{already}
9638 double quoting, so you just need:
9640 @example
9641 AC_TRY_LINK(
9642 [#include <time.h>
9643 #ifndef tzname /* For SGI.  */
9644 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9645 #endif],
9646             [atoi (*tzname);],
9647             [ac_cv_var_tzname=yes],
9648             [ac_cv_var_tzname=no])
9649 @end example
9651 @noindent
9652 The M4-fluent reader might note that these two examples are rigorously
9653 equivalent, since M4 swallows both the @samp{changequote(<<, >>)}
9654 and @samp{<<} @samp{>>} when it @dfn{collects} the arguments: these
9655 quotes are not part of the arguments!
9657 Simplified, the example above is just doing this:
9659 @example
9660 changequote(<<, >>)dnl
9661 <<[]>>
9662 changequote([, ])dnl
9663 @end example
9665 @noindent
9666 instead of simply:
9668 @example
9669 [[]]
9670 @end example
9672 With macros that do not double quote their arguments (which is the
9673 rule), double-quote the (risky) literals:
9675 @example
9676 AC_LINK_IFELSE([AC_LANG_PROGRAM(
9677 [[#include <time.h>
9678 #ifndef tzname /* For SGI.  */
9679 extern char *tzname[]; /* RS6000 and others reject char **tzname.  */
9680 #endif]],
9681                                 [atoi (*tzname);])],
9682                [ac_cv_var_tzname=yes],
9683                [ac_cv_var_tzname=no])
9684 @end example
9686 Please note that the macro @code{AC_TRY_LINK} is obsolete, so you really
9687 should be using @code{AC_LINK_IFELSE} instead.
9689 @xref{Quadrigraphs}, for what to do if you run into a hopeless case
9690 where quoting does not suffice.
9692 When you create a @command{configure} script using newly written macros,
9693 examine it carefully to check whether you need to add more quotes in
9694 your macros.  If one or more words have disappeared in the M4
9695 output, you need more quotes.  When in doubt, quote.
9697 However, it's also possible to put on too many layers of quotes.  If
9698 this happens, the resulting @command{configure} script may contain
9699 unexpanded macros.  The @command{autoconf} program checks for this problem
9700 by looking for the string @samp{AC_} in @file{configure}.  However, this
9701 heuristic does not work in general: for example, it does not catch
9702 overquoting in @code{AC_DEFINE} descriptions.
9705 @c ---------------------------------------- Using autom4te
9707 @node Using autom4te
9708 @section Using @command{autom4te}
9710 The Autoconf suite, including M4sugar, M4sh, and Autotest, in addition
9711 to Autoconf per se, heavily rely on M4.  All these different uses
9712 revealed common needs factored into a layer over M4:
9713 @command{autom4te}@footnote{
9715 Yet another great name from Lars J. Aas.
9719 @command{autom4te} is a preprocessor that is like @command{m4}.
9720 It supports M4 extensions designed for use in tools like Autoconf.
9722 @menu
9723 * autom4te Invocation::         A @acronym{GNU} M4 wrapper
9724 * Customizing autom4te::        Customizing the Autoconf package
9725 @end menu
9727 @node autom4te Invocation
9728 @subsection Invoking @command{autom4te}
9730 The command line arguments are modeled after M4's:
9732 @example
9733 autom4te @var{options} @var{files}
9734 @end example
9736 @noindent
9737 @evindex M4
9738 where the @var{files} are directly passed to @command{m4}.  By default,
9739 @acronym{GNU} M4 is found during configuration, but the environment
9740 variable
9741 @env{M4} can be set to tell @command{autom4te} where to look.  In addition
9742 to the regular expansion, it handles the replacement of the quadrigraphs
9743 (@pxref{Quadrigraphs}), and of @samp{__oline__}, the current line in the
9744 output.  It supports an extended syntax for the @var{files}:
9746 @table @file
9747 @item @var{file}.m4f
9748 This file is an M4 frozen file.  Note that @emph{all the previous files
9749 are ignored}.  See the option @option{--melt} for the rationale.
9751 @item @var{file}?
9752 If found in the library path, the @var{file} is included for expansion,
9753 otherwise it is ignored instead of triggering a failure.
9754 @end table
9756 @sp 1
9758 Of course, it supports the Autoconf common subset of options:
9760 @table @option
9761 @item --help
9762 @itemx -h
9763 Print a summary of the command line options and exit.
9765 @item --version
9766 @itemx -V
9767 Print the version number of Autoconf and exit.
9769 @item --verbose
9770 @itemx -v
9771 Report processing steps.
9773 @item --debug
9774 @itemx -d
9775 Don't remove the temporary files and be even more verbose.
9777 @item --include=@var{dir}
9778 @itemx -I @var{dir}
9779 Also look for input files in @var{dir}.  Multiple invocations
9780 accumulate.
9782 @item --output=@var{file}
9783 @itemx -o @var{file}
9784 Save output (script or trace) to @var{file}.  The file @option{-} stands
9785 for the standard output.
9786 @end table
9788 @sp 1
9790 As an extension of @command{m4}, it includes the following options:
9792 @table @option
9793 @item --warnings=@var{category}
9794 @itemx -W @var{category}
9795 @evindex WARNINGS
9796 @c FIXME: Point to the M4sugar macros, not Autoconf's.
9797 Report the warnings related to @var{category} (which can actually be a
9798 comma separated list).  @xref{Reporting Messages}, macro
9799 @code{AC_DIAGNOSE}, for a comprehensive list of categories.  Special
9800 values include:
9802 @table @samp
9803 @item all
9804 report all the warnings
9806 @item none
9807 report none
9809 @item error
9810 treats warnings as errors
9812 @item no-@var{category}
9813 disable warnings falling into @var{category}
9814 @end table
9816 Warnings about @samp{syntax} are enabled by default, and the environment
9817 variable @env{WARNINGS}, a comma separated list of categories, is
9818 honored.  @samp{autom4te -W @var{category}} actually
9819 behaves as if you had run:
9821 @example
9822 autom4te --warnings=syntax,$WARNINGS,@var{category}
9823 @end example
9825 @noindent
9826 For example, if you want to disable defaults and @env{WARNINGS}
9827 of @command{autom4te}, but enable the warnings about obsolete
9828 constructs, you would use @option{-W none,obsolete}.
9830 @cindex Back trace
9831 @cindex Macro invocation stack
9832 @command{autom4te} displays a back trace for errors, but not for
9833 warnings; if you want them, just pass @option{-W error}.
9835 @item --melt
9836 @itemx -M
9837 Do not use frozen files.  Any argument @code{@var{file}.m4f} is
9838 replaced by @code{@var{file}.m4}.  This helps tracing the macros which
9839 are executed only when the files are frozen, typically
9840 @code{m4_define}.  For instance, running:
9842 @example
9843 autom4te --melt 1.m4 2.m4f 3.m4 4.m4f input.m4
9844 @end example
9846 @noindent
9847 is roughly equivalent to running:
9849 @example
9850 m4 1.m4 2.m4 3.m4 4.m4 input.m4
9851 @end example
9853 @noindent
9854 while
9856 @example
9857 autom4te 1.m4 2.m4f 3.m4 4.m4f input.m4
9858 @end example
9860 @noindent
9861 is equivalent to:
9863 @example
9864 m4 --reload-state=4.m4f input.m4
9865 @end example
9867 @item --freeze
9868 @itemx -f
9869 Produce a frozen state file.  @command{autom4te} freezing is stricter
9870 than M4's: it must produce no warnings, and no output other than empty
9871 lines (a line with white space is @emph{not} empty) and comments
9872 (starting with @samp{#}).  Unlike @command{m4}'s similarly-named option,
9873 this option takes no argument:
9875 @example
9876 autom4te 1.m4 2.m4 3.m4 --freeze --output=3.m4f
9877 @end example
9879 @noindent
9880 corresponds to
9882 @example
9883 m4 1.m4 2.m4 3.m4 --freeze-state=3.m4f
9884 @end example
9886 @item --mode=@var{octal-mode}
9887 @itemx -m @var{octal-mode}
9888 Set the mode of the non-traces output to @var{octal-mode}; by default
9889 @samp{0666}.
9890 @end table
9892 @sp 1
9894 @cindex @file{autom4te.cache}
9895 As another additional feature over @command{m4}, @command{autom4te}
9896 caches its results.  @acronym{GNU} M4 is able to produce a regular
9897 output and traces at the same time.  Traces are heavily used in the
9898 @acronym{GNU} Build System: @command{autoheader} uses them to build
9899 @file{config.h.in}, @command{autoreconf} to determine what
9900 @acronym{GNU} Build System components are used, @command{automake} to
9901 ``parse'' @file{configure.ac} etc.  To avoid recomputation,
9902 traces are cached while performing regular expansion,
9903 and conversely.  This cache is (actually, the caches are) stored in
9904 the directory @file{autom4te.cache}.  @emph{It can safely be removed}
9905 at any moment (especially if for some reason @command{autom4te}
9906 considers it is trashed).
9908 @table @option
9909 @item --cache=@var{directory}
9910 @itemx -C @var{directory}
9911 Specify the name of the directory where the result should be cached.
9912 Passing an empty value disables caching.  Be sure to pass a relative
9913 file name, as for the time being, global caches are not supported.
9915 @item --no-cache
9916 Don't cache the results.
9918 @item --force
9919 @itemx -f
9920 If a cache is used, consider it obsolete (but update it anyway).
9921 @end table
9923 @sp 1
9925 Because traces are so important to the @acronym{GNU} Build System,
9926 @command{autom4te} provides high level tracing features as compared to
9927 M4, and helps exploiting the cache:
9929 @table @option
9930 @item --trace=@var{macro}[:@var{format}]
9931 @itemx -t @var{macro}[:@var{format}]
9932 Trace the invocations of @var{macro} according to the @var{format}.
9933 Multiple @option{--trace} arguments can be used to list several macros.
9934 Multiple @option{--trace} arguments for a single macro are not
9935 cumulative; instead, you should just make @var{format} as long as
9936 needed.
9938 The @var{format} is a regular string, with newlines if desired, and
9939 several special escape codes.  It defaults to @samp{$f:$l:$n:$%}.  It can
9940 use the following special escapes:
9942 @table @samp
9943 @item $$
9944 The character @samp{$}.
9946 @item $f
9947 The file name from which @var{macro} is called.
9949 @item $l
9950 The line number from which @var{macro} is called.
9952 @item $d
9953 The depth of the @var{macro} call.  This is an M4 technical detail that
9954 you probably don't want to know about.
9956 @item $n
9957 The name of the @var{macro}.
9959 @item $@var{num}
9960 The @var{num}th argument of the call to @var{macro}.
9962 @item $@@
9963 @itemx $@var{sep}@@
9964 @itemx $@{@var{separator}@}@@
9965 All the arguments passed to @var{macro}, separated by the character
9966 @var{sep} or the string @var{separator} (@samp{,} by default).  Each
9967 argument is quoted, i.e., enclosed in a pair of square brackets.
9969 @item $*
9970 @itemx $@var{sep}*
9971 @itemx $@{@var{separator}@}*
9972 As above, but the arguments are not quoted.
9974 @item $%
9975 @itemx $@var{sep}%
9976 @itemx $@{@var{separator}@}%
9977 As above, but the arguments are not quoted, all new line characters in
9978 the arguments are smashed, and the default separator is @samp{:}.
9980 The escape @samp{$%} produces single-line trace outputs (unless you put
9981 newlines in the @samp{separator}), while @samp{$@@} and @samp{$*} do
9982 not.
9983 @end table
9985 @xref{autoconf Invocation}, for examples of trace uses.
9987 @item --preselect=@var{macro}
9988 @itemx -p @var{macro}
9989 Cache the traces of @var{macro}, but do not enable traces.  This is
9990 especially important to save CPU cycles in the future.  For instance,
9991 when invoked, @command{autoconf} preselects all the macros that
9992 @command{autoheader}, @command{automake}, @command{autoreconf}, etc.,
9993 trace, so that running @command{m4} is not needed to trace them: the
9994 cache suffices.  This results in a huge speed-up.
9995 @end table
9997 @sp 1
9999 @cindex Autom4te Library
10000 Finally, @command{autom4te} introduces the concept of @dfn{Autom4te
10001 libraries}.  They consists in a powerful yet extremely simple feature:
10002 sets of combined command line arguments:
10004 @table @option
10005 @item --language=@var{language}
10006 @itemx -l @var{language}
10007 Use the @var{language} Autom4te library.  Current languages include:
10009 @table @code
10010 @item M4sugar
10011 create M4sugar output.
10013 @item M4sh
10014 create M4sh executable shell scripts.
10016 @item Autotest
10017 create Autotest executable test suites.
10019 @item Autoconf-without-aclocal-m4
10020 create Autoconf executable configure scripts without
10021 reading @file{aclocal.m4}.
10023 @item Autoconf
10024 create Autoconf executable configure scripts.  This language inherits
10025 all the characteristics of @code{Autoconf-without-aclocal-m4} and
10026 additionally reads @file{aclocal.m4}.
10027 @end table
10029 @item --prepend-include=@var{dir}
10030 @item -B @var{dir}
10031 Prepend directory @var{dir} to the search path.  This is used to include
10032 the language-specific files before any third-party macros.
10034 @end table
10036 @cindex @file{autom4te.cfg}
10037 As an example, if Autoconf is installed in its default location,
10038 @file{/usr/local}, the command @samp{autom4te -l m4sugar foo.m4} is
10039 strictly equivalent to the command:
10041 @example
10042 autom4te --prepend-include /usr/local/share/autoconf \
10043   m4sugar/m4sugar.m4f --warnings syntax foo.m4
10044 @end example
10046 @noindent
10047 Recursive expansion applies here: the command @samp{autom4te -l m4sh foo.m4}
10048 is the same as @samp{autom4te --language M4sugar m4sugar/m4sh.m4f
10049 foo.m4}, i.e.:
10051 @example
10052 autom4te --prepend-include /usr/local/share/autoconf \
10053   m4sugar/m4sugar.m4f m4sugar/m4sh.m4f --mode 777 foo.m4
10054 @end example
10056 @noindent
10057 The definition of the languages is stored in @file{autom4te.cfg}.
10059 @node Customizing autom4te
10060 @subsection Customizing @command{autom4te}
10062 One can customize @command{autom4te} via @file{~/.autom4te.cfg} (i.e.,
10063 as found in the user home directory), and @file{./.autom4te.cfg} (i.e.,
10064 as found in the directory from which @command{autom4te} is run).  The
10065 order is first reading @file{autom4te.cfg}, then @file{~/.autom4te.cfg},
10066 then @file{./.autom4te.cfg}, and finally the command line arguments.
10068 In these text files, comments are introduced with @code{#}, and empty
10069 lines are ignored.  Customization is performed on a per-language basis,
10070 wrapped in between a @samp{begin-language: "@var{language}"},
10071 @samp{end-language: "@var{language}"} pair.
10073 Customizing a language stands for appending options (@pxref{autom4te
10074 Invocation}) to the current definition of the language.  Options, and
10075 more generally arguments, are introduced by @samp{args:
10076 @var{arguments}}.  You may use the traditional shell syntax to quote the
10077 @var{arguments}.
10079 As an example, to disable Autoconf caches (@file{autom4te.cache})
10080 globally, include the following lines in @file{~/.autom4te.cfg}:
10082 @verbatim
10083 ## ------------------ ##
10084 ## User Preferences.  ##
10085 ## ------------------ ##
10087 begin-language: "Autoconf-without-aclocal-m4"
10088 args: --no-cache
10089 end-language: "Autoconf-without-aclocal-m4"
10090 @end verbatim
10093 @node Programming in M4sugar
10094 @section Programming in M4sugar
10096 @cindex M4sugar
10097 M4 by itself provides only a small, but sufficient, set of all-purpose
10098 macros.  M4sugar introduces additional generic macros.  Its name was
10099 coined by Lars J. Aas: ``Readability And Greater Understanding Stands 4
10100 M4sugar''.
10102 M4sugar reserves the macro namespace @samp{^_m4_} for internal use, and
10103 the macro namespace @samp{^m4_} for M4sugar macros.  You should not
10104 define your own macros into these namespaces.
10106 @menu
10107 * Redefined M4 Macros::         M4 builtins changed in M4sugar
10108 * Diagnostic Macros::           Diagnostic messages from M4sugar
10109 * Diversion support::           Diversions in M4sugar
10110 * Conditional constructs::      Conditions in M4
10111 * Looping constructs::          Iteration in M4
10112 * Evaluation Macros::           More quotation and evaluation control
10113 * Text processing Macros::      String manipulation in M4
10114 * Number processing Macros::    Arithmetic computation in M4
10115 * Forbidden Patterns::          Catching unexpanded macros
10116 @end menu
10118 @node Redefined M4 Macros
10119 @subsection Redefined M4 Macros
10121 @msindex{builtin}
10122 @msindex{changecom}
10123 @msindex{changequote}
10124 @msindex{debugfile}
10125 @msindex{debugmode}
10126 @msindex{decr}
10127 @msindex{define}
10128 @msindex{divnum}
10129 @msindex{dumpdef}
10130 @msindex{errprint}
10131 @msindex{esyscmd}
10132 @msindex{eval}
10133 @msindex{format}
10134 @msindex{ifdef}
10135 @msindex{incr}
10136 @msindex{index}
10137 @msindex{indir}
10138 @msindex{len}
10139 @msindex{pushdef}
10140 @msindex{shift}
10141 @msindex{substr}
10142 @msindex{syscmd}
10143 @msindex{sysval}
10144 @msindex{traceoff}
10145 @msindex{traceon}
10146 @msindex{translit}
10147 With a few exceptions, all the M4 native macros are moved in the
10148 @samp{m4_} pseudo-namespace, e.g., M4sugar renames @code{define} as
10149 @code{m4_define} etc.
10151 The list of macros unchanged from M4, except for their name, is:
10152 @itemize @minus
10153 @item m4_builtin
10154 @item m4_changecom
10155 @item m4_changequote
10156 @item m4_debugfile
10157 @item m4_debugmode
10158 @item m4_decr
10159 @item m4_define
10160 @item m4_divnum
10161 @item m4_dumpdef
10162 @item m4_errprint
10163 @item m4_esyscmd
10164 @item m4_eval
10165 @item m4_format
10166 @item m4_ifdef
10167 @item m4_incr
10168 @item m4_index
10169 @item m4_indir
10170 @item m4_len
10171 @item m4_pushdef
10172 @item m4_shift
10173 @item m4_substr
10174 @item m4_syscmd
10175 @item m4_sysval
10176 @item m4_traceoff
10177 @item m4_traceon
10178 @item m4_translit
10179 @end itemize
10181 Some M4 macros are redefined, and are slightly incompatible with their
10182 native equivalent.
10184 @defmac __file__
10185 @defmacx __line__
10186 @MSindex __file__
10187 @MSindex __line__
10188 All M4 macros starting with @samp{__} retain their original name: for
10189 example, no @code{m4__file__} is defined.
10190 @end defmac
10192 @defmac __oline__
10193 @MSindex __oline__
10194 This is not technically a macro, but a feature of Autom4te.  The
10195 sequence @code{__oline__} can be used similarly to the other m4sugar
10196 location macros, but rather than expanding to the location of the input
10197 file, it is translated to the line number where it appears in the output
10198 file after all other M4 expansions.
10199 @end defmac
10201 @defmac dnl
10202 @MSindex dnl
10203 This macro kept its original name: no @code{m4_dnl} is defined.
10204 @end defmac
10206 @defmac m4_bpatsubst (@var{string}, @var{regexp}, @ovar{replacement})
10207 @msindex{bpatsubst}
10208 This macro corresponds to @code{patsubst}.  The name @code{m4_patsubst}
10209 is kept for future versions of M4sugar, once @acronym{GNU} M4 2.0 is
10210 released and supports extended regular expression syntax.
10211 @end defmac
10213 @defmac m4_bregexp (@var{string}, @var{regexp}, @ovar{replacement})
10214 @msindex{bregexp}
10215 This macro corresponds to @code{regexp}.  The name @code{m4_regexp}
10216 is kept for future versions of M4sugar, once @acronym{GNU} M4 2.0 is
10217 released and supports extended regular expression syntax.
10218 @end defmac
10220 @defmac m4_defn (@var{macro})
10221 @msindex{defn}
10222 Unlike the M4 builtin, this macro fails if @var{macro} is not
10223 defined.  Also, while newer M4 can concatenate multiple definitions,
10224 this version currently only supports a single @var{macro}.  See
10225 @code{m4_undefine}.
10226 @end defmac
10228 @defmac m4_divert (@var{diversion})
10229 @msindex{divert}
10230 M4sugar relies heavily on diversions, so rather than behaving as a
10231 primitive, @code{m4_divert} behaves like:
10232 @example
10233 m4_divert_pop()m4_divert_push([@var{diversion}])
10234 @end example
10235 @noindent
10236 @xref{Diversion support}, for more details about the use of the
10237 diversion stack.
10238 @end defmac
10240 @defmac m4_exit (@var{exit-status})
10241 @msindex{exit}
10242 This macro corresponds to @code{m4exit}.
10243 @end defmac
10245 @defmac m4_if (@var{comment})
10246 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @ovar{not-equal})
10247 @defmacx m4_if (@var{string-1}, @var{string-2}, @var{equal}, @dots{})
10248 @msindex{if}
10249 This macro corresponds to @code{ifelse}.  @var{string-1} and
10250 @var{string-2} are compared literally, so usually one of the two
10251 arguments is passed unquoted.  @xref{Conditional constructs}, for more
10252 conditional idioms.
10253 @end defmac
10255 @defmac m4_include (@var{file})
10256 @defmacx m4_sinclude (@var{file})
10257 @msindex{include}
10258 @msindex{sinclude}
10259 Like the M4 builtins, but warn against multiple inclusions of @var{file}.
10260 @end defmac
10262 @defmac m4_mkstemp (@var{template})
10263 @defmacx m4_maketemp (@var{template})
10264 @msindex{maketemp}
10265 @msindex{mkstemp}
10266 Posix requires @code{maketemp} to replace the trailing @samp{X}
10267 characters in @var{template} with the process id, without regards to the
10268 existence of a file by that name, but this a security hole.  When this
10269 was pointed out to the Posix folks, they agreed to invent a new macro
10270 @code{mkstemp} that always creates a uniquely named file, but not all
10271 versions of @acronym{GNU} M4 support the new macro.  In M4sugar,
10272 @code{m4_maketemp} and @code{m4_mkstemp} are synonyms for each other,
10273 and both have the secure semantics regardless of which macro the
10274 underlying M4 provides.
10275 @end defmac
10277 @defmac m4_popdef (@var{macro})
10278 @msindex{popdef}
10279 Unlike the M4 builtin, this macro fails if @var{macro} is not
10280 defined.  Also, while newer M4 can pop multiple definitions at once,
10281 this version currently only supports a single @var{macro}.  See
10282 @code{m4_undefine}.
10283 @end defmac
10285 @defmac m4_undefine (@var{macro})
10286 @msindex{undefine}
10287 Unlike the M4 builtin, this macro fails if @var{macro} is not
10288 defined.  Also, while newer M4 can undefine multiple definitions at
10289 once, this version currently only supports a single @var{macro}.  Use
10291 @example
10292 m4_ifdef([@var{macro}], [m4_undefine([@var{macro}])])
10293 @end example
10295 @noindent
10296 to recover the behavior of the builtin.
10297 @end defmac
10299 @defmac m4_undivert (@var{diversion})
10300 @msindex{undivert}
10301 Unlike the M4 builtin, only one diversion can be undiverted per
10302 invocation.  Also, since the M4sugar diversion stack prefers named
10303 diversions, the use of @code{m4_undivert} to include files is risky.
10304 @xref{Diversion support}, for more details about the use of the
10305 diversion stack.
10306 @end defmac
10308 @defmac m4_wrap (@var{text})
10309 @msindex{wrap}
10310 This macro corresponds to @code{m4wrap}.
10312 Posix requires arguments of multiple @code{m4wrap} calls to be
10313 reprocessed at @acronym{EOF} in the same order as the original calls.
10314 @acronym{GNU} M4 versions through 1.4.x, however, reprocess them in
10315 reverse order.  Your code should not depend on the order.
10317 Also, Posix requires @code{m4wrap} to ignore its second and succeeding
10318 arguments, but @acronym{GNU} M4 versions through 1.4.x concatenate the
10319 arguments with intervening spaces.  Your code should not pass more than
10320 one argument.
10322 You are encouraged to end @var{text} with @samp{[]}, to avoid unexpected
10323 token pasting between consecutive invocations of @code{m4_wrap}, as in:
10325 @example
10326 m4_define([foo], [bar])
10327 m4_define([foofoo], [OUCH])
10328 m4_wrap([foo])
10329 m4_wrap([foo])
10330 @result{}OUCH
10331 @end example
10332 @end defmac
10335 @node Diagnostic Macros
10336 @subsection Diagnostic messages from M4sugar
10337 @cindex Messages, from @command{M4sugar}
10339 When macros statically diagnose abnormal situations, benign or fatal,
10340 they should report them using these macros.  For issuing dynamic issues,
10341 i.e., when @command{configure} is run, see @ref{Printing Messages}.
10343 @defmac m4_assert (@var{expression}, @dvar{exit-status, 1})
10344 @msindex{assert}
10345 Assert that the arithmetic @var{expression} evaluates to non-zero.
10346 Otherwise, issue a fatal error, and exit @command{autom4te} with
10347 @var{exit-status}.
10348 @end defmac
10350 @defmac m4_errprintn (@var{message})
10351 @msindex{errprintn}
10352 Similar to the builtin @code{m4_errprint}, except that a newline is
10353 guaranteed after @var{message}.
10354 @end defmac
10356 @anchor{m4_fatal}
10357 @defmac m4_fatal
10358 @msindex{fatal}
10359 Report a severe error @var{message} prefixed with the current location,
10360 and have @command{autom4te} die.
10361 @end defmac
10363 @defmac m4_location
10364 @msindex{location}
10365 Useful as a prefix in a message line.  Short for:
10366 @example
10367 __file__:__line__
10368 @end example
10369 @end defmac
10371 @anchor{m4_warn}
10372 @defmac m4_warn (@var{category}, @var{message})
10373 @msindex{warn}
10374 Report @var{message} as a warning (or as an error if requested by the
10375 user) if warnings of the @var{category} are turned on.  If the message
10376 is emitted, it is prefixed with the current location, and followed by a
10377 call trace of all macros defined via @code{AC_DEFUN} used to get to the
10378 current expansion.  You are encouraged to use standard categories, which
10379 currently include:
10381 @table @samp
10382 @item all
10383 messages that don't fall into one of the following categories.  Use of an
10384 empty @var{category} is equivalent.
10386 @item cross
10387 related to cross compilation issues.
10389 @item obsolete
10390 use of an obsolete construct.
10392 @item syntax
10393 dubious syntactic constructs, incorrectly ordered macro calls.
10394 @end table
10395 @end defmac
10398 @node Diversion support
10399 @subsection Diversion support
10401 M4sugar makes heavy use of diversions, because it is often the case that
10402 text that must appear early in the output is not discovered until late
10403 in the input.  Additionally, some of the topological sorting algorithms
10404 used in resolving macro dependencies use diversions.  Therefore, most
10405 macros should not need to change diversions directly, but rather rely on
10406 higher-level M4sugar macros to manage diversions transparently.
10408 To make diversion management easier, M4sugar uses the concept of named
10409 diversions.  Rather than using diversion numbers directly, it is nicer
10410 to associate a name with each diversion; the diversion number associated
10411 with a particular diversion name is an implementation detail, so you
10412 should only use diversion names.
10414 M4sugar defines two named diversions.
10415 @table @code
10416 @item KILL
10417 Text written to this diversion is discarded.  This is the default
10418 diversion once M4sugar is initialized.
10419 @item GROW
10420 This diversion is used behind the scenes by topological sorting macros,
10421 such as @code{AC_REQUIRE}.
10422 @end table
10424 M4sh adds several more named diversions.
10425 @table @code
10426 @item BINSH
10427 This diversion is reserved for the @samp{#!} interpreter line.
10428 @item HEADER-REVISION
10429 This diversion holds text from @code{AC_REVISION}.
10430 @item HEADER-COMMENT
10431 This diversion holds comments about the purpose of a file.
10432 @item HEADER-COPYRIGHT
10433 This diversion is managed by @code{AC_COPYRIGHT}.
10434 @item M4SH-SANITIZE
10435 This diversion contains M4sh sanitization code, used to ensure M4sh is
10436 executing in a reasonable shell environment.
10437 @item M4SH-INIT
10438 This diversion contains M4sh initialization code, initializing variables
10439 that are required by other M4sh macros.
10440 @item BODY
10441 This diversion contains the body of the shell code, and is the default
10442 diversion once M4sh is initialized.
10443 @end table
10445 For now, the named diversions of Autoconf, Autoheader, and Autotest are
10446 not documented.
10448 @defmac m4_divert_once (@var{diversion}, @ovar{content})
10449 @msindex{divert_once}
10450 Similar to @code{m4_divert_text}, except that @var{content} is only
10451 output to @var{diversion} if this is the first time that
10452 @code{m4_divert_once} has been called with its particular arguments.
10453 @end defmac
10455 @defmac m4_divert_pop (@ovar{diversion})
10456 @msindex{divert_pop}
10457 If provided, check that the current diversion is indeed @var{diversion}.
10458 Then change to the diversion located earlier on the stack, giving an
10459 error if an attempt is made to pop beyond the initial m4sugar diversion
10460 of @code{KILL}.
10461 @end defmac
10463 @defmac m4_divert_push (@var{diversion})
10464 @msindex{divert_push}
10465 Remember the former diversion on the diversion stack, and output
10466 subsequent text into @var{diversion}.  M4sugar maintains a diversion
10467 stack, and issues an error if there is not a matching pop for every
10468 push.
10469 @end defmac
10471 @defmac m4_divert_text (@var{diversion}, @ovar{content})
10472 @msindex{divert_text}
10473 Output @var{content} and a newline into @var{diversion}, without
10474 affecting the current diversion.  Shorthand for:
10475 @example
10476 m4_divert_push([@var{diversion}])@var{content}
10477 m4_divert_pop([@var{diversion}])dnl
10478 @end example
10479 @end defmac
10481 @node Conditional constructs
10482 @subsection Conditional constructs
10484 The following macros provide additional conditional contructs, as
10485 convenience wrappers around @code{m4_if}.
10487 @defmac m4_bmatch (@var{string}, @var{regex-1}, @var{value-1}, @
10488   @ovar{regex-2}, @ovar{value-2}, @dots{}, @ovar{default})
10489 @msindex{bmatch}
10490 The string @var{string} is repeatedly compared against a series of
10491 @var{regex} arguments; if a match is found, the expansion is the
10492 corresponding @var{value}, otherwise, the macro moves on to the next
10493 @var{regex}.  If no @var{regex} match, then the result is the optional
10494 @var{default}, or nothing.
10495 @end defmac
10497 @defmac m4_bpatsubsts (@var{string}, @var{regex-1}, @var{subst-1}, @
10498   @ovar{regex-2}, @ovar{subst-2}, @dots{})
10499 @msindex{bpatsubsts}
10500 The string @var{string} is altered by @var{regex-1} and @var{subst-1},
10501 as if by:
10502 @example
10503 m4_bpatsubst([[@var{string}]], [@var{regex}], [@var{subst}])
10504 @end example
10506 @noindent
10507 The result of the substitution is then passed through the next set of
10508 @var{regex} and @var{subst}, and so forth.  An empty @var{subst} implies
10509 deletion of any matched portions in the current string.  Note that this
10510 macro over-quotes @var{string}; this behavior is intentional, so that
10511 the result of each step of the recursion remains as a quoted string.
10512 However, it means that anchors (@samp{^} and @samp{$} in the @var{regex}
10513 will line up with the extra quotations, and not the characters of the
10514 original string.
10515 @end defmac
10517 @defmac m4_case (@var{string}, @var{value-1}, @var{if-value-1}, @
10518   @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
10519 @msindex{case}
10520 Test @var{string} against multiple @var{value} possibilities, resulting
10521 in the first @var{if-value} for a match, or in the optional
10522 @var{default}.  This is shorthand for:
10523 @example
10524 m4_if([@var{string}], [@var{value-1}], [@var{if-value-1}],
10525       [@var{string}], [@var{value-2}], [@var{if-value-2}], @dots{},
10526       [@var{default}])
10527 @end example
10528 @end defmac
10530 @defmac m4_cond (@var{test-1}, @var{value-1}, @var{if-value-1}, @
10531   @ovar{test-2}, @ovar{value-2}, @ovar{if-value-2}, @dots{}, @ovar{default})
10532 @msindex{cond}
10533 This macro was introduced in Autoconf 2.62.  Similar to @code{m4_if},
10534 except that each @var{test} is expanded only when it is encountered.
10535 This is useful for short-circuiting expensive tests; while @code{m4_if}
10536 requires all its strings to be expanded up front before doing
10537 comparisons, @code{m4_cond} only expands a @var{test} when all earlier
10538 tests have failed.
10540 For an example, these two sequences give the same result, but in the
10541 case where @samp{$1} does not contain a backslash, the @code{m4_cond}
10542 version only expands @code{m4_index} once, instead of five times, for
10543 faster computation if this is a common case for @samp{$1}.  Notice that
10544 every third argument is unquoted for @code{m4_if}, and quoted for
10545 @code{m4_cond}:
10547 @example
10548 m4_if(m4_index([$1], [\]), [-1], [$2],
10549       m4_eval(m4_index([$1], [\\]) >= 0), [1], [$2],
10550       m4_eval(m4_index([$1], [\$]) >= 0), [1], [$2],
10551       m4_eval(m4_index([$1], [\`]) >= 0), [1], [$3],
10552       m4_eval(m4_index([$1], [\"]) >= 0), [1], [$3],
10553       [$2])
10554 m4_cond([m4_index([$1], [\])], [-1], [$2],
10555         [m4_eval(m4_index([$1], [\\]) >= 0)], [1], [$2],
10556         [m4_eval(m4_index([$1], [\$]) >= 0)], [1], [$2],
10557         [m4_eval(m4_index([$1], [\`]) >= 0)], [1], [$3],
10558         [m4_eval(m4_index([$1], [\"]) >= 0)], [1], [$3],
10559         [$2])
10560 @end example
10561 @end defmac
10563 @defmac m4_default (@var{expr-1}, @var{expr-2})
10564 @msindex{default}
10565 If @var{expr-1} is not empty, use it.  Otherwise, expand to
10566 @var{expr-2}.  Useful for providing a fixed default if the expression
10567 that results in @var{expr-1} would otherwise be empty.
10568 @end defmac
10570 @defmac m4_ifndef (@var{macro}, @var{if-not-defined}, @ovar{if-defined})
10571 @msindex{ifndef}
10572 This is shorthand for:
10573 @example
10574 m4_ifdef([@var{macro}], [@var{if-defined}], [@var{if-not-defined}])
10575 @end example
10576 @end defmac
10578 @defmac m4_ifset (@var{macro}, @ovar{if-true}, @ovar{if-false})
10579 @msindex{ifset}
10580 If @var{macro} is undefined, or is defined as the empty string, expand
10581 to @var{if-false}.  Otherwise, expands to @var{if-true}.  Similar to:
10582 @example
10583 m4_ifval(m4_defn([@var{macro}]), [@var{if-true}], [@var{if-false}])
10584 @end example
10585 @noindent
10586 except that it is not an error if @var{macro} is undefined.
10587 @end defmac
10589 @defmac m4_ifval (@var{cond}, @ovar{if-true}, @ovar{if-false})
10590 @msindex{ifval}
10591 Expands to @var{if-true} if @var{cond} is not empty, otherwise to
10592 @var{if-false}.  This is shorthand for:
10593 @example
10594 m4_if([@var{cond}], [], [@var{if-true}], [@var{if-false}])
10595 @end example
10596 @end defmac
10598 @defmac m4_ifvaln (@var{cond}, @ovar{if-true}, @ovar{if-false})
10599 @msindex{ifvaln}
10600 Similar to @code{m4_ifval}, except guarantee that a newline is present
10601 after any non-empty expansion.
10602 @end defmac
10604 @defmac m4_n (@var{text})
10605 @msindex{n}
10606 Expand to @var{text}, and add a newline if @var{text} is not empty.
10607 @end defmac
10610 @node Looping constructs
10611 @subsection Looping constructs
10613 The following macros are useful in implementing recursive algorithms in
10614 M4, including loop operations.  An M4 list is formed by quoting a list
10615 of quoted elements; generally the lists are comma-separated, although
10616 @code{m4_foreach_w} is whitespace-separated.  For example, the list
10617 @samp{[[a], [b,c]]} contains two elements: @samp{[a]} and @samp{[b,c]}.
10618 It is common to see lists with unquoted elements when those elements are
10619 not likely to be macro names, as in @samp{[fputc_unlocked,
10620 fgetc_unlocked]}.
10622 @defmac m4_car (@var{list})
10623 @msindex{car}
10624 Expands to the quoted first element of the comma-separated quoted
10625 @var{list}.  Often used with @code{m4_cdr} to recursively iterate
10626 through a list.  Generally, when using quoted lists of quoted elements,
10627 @code{m4_car} should be called without any extra quotes.
10628 @end defmac
10630 @defmac m4_cdr (@var{list})
10631 @msindex{cdr}
10632 Expands to a quoted list of all but the first element of the
10633 comma-separated quoted @var{list}, or the empty string if @var{list} had
10634 only one element.  Generally, when using quoted lists of quoted
10635 elements, @code{m4_cdr} should be called without any extra quotes.
10637 For example, this is a simple implementation of @code{m4_map}; note how
10638 each iteration checks for the end of recursion, then merely applies the
10639 first argument to the first element of the list, then recurses with the
10640 rest of the list.  (The actual implementation in M4sugar is a bit more
10641 involved, to gain some speed and share code with @code{m4_map_sep}).
10642 @example
10643 m4_define([m4_map], [m4_ifval([$2],
10644   [m4_apply([$1], m4_car($2))[]$0([$1], m4_cdr($2))])])dnl
10645 m4_map([ m4_eval], [[[1]], [[1+1]], [[10],[16]]])
10646 @result{} 1 2 a
10647 @end example
10648 @end defmac
10650 @defmac m4_for (@var{var}, @var{first}, @var{last}, @ovar{step}, @
10651   @var{expression})
10652 @msindex{for}
10653 Loop over the numeric values between @var{first} and @var{last}
10654 including bounds by increments of @var{step}.  For each iteration,
10655 expand @var{expression} with the numeric value assigned to @var{var}.
10656 If @var{step} is omitted, it defaults to @samp{1} or @samp{-1} depending
10657 on the order of the limits.  If given, @var{step} has to match this
10658 order.
10659 @end defmac
10661 @defmac m4_foreach (@var{var}, @var{list}, @var{expression})
10662 @msindex{foreach}
10663 Loop over the comma-separated M4 list @var{list}, assigning each value
10664 to @var{var}, and expand @var{expression}.  The following example
10665 outputs two lines:
10667 @example
10668 m4_foreach([myvar], [[foo], [bar, baz]],
10669            [echo myvar
10670 ])dnl
10671 @result{}echo foo
10672 @result{}echo bar, baz
10673 @end example
10674 @end defmac
10676 @anchor{m4_foreach_w}
10677 @defmac m4_foreach_w (@var{var}, @var{list}, @var{expression})
10678 @msindex{foreach_w}
10679 Loop over the white-space-separated list @var{list}, assigning each value
10680 to @var{var}, and expand @var{expression}.
10682 The deprecated macro @code{AC_FOREACH} is an alias of
10683 @code{m4_foreach_w}.
10684 @end defmac
10686 @defmac m4_map (@var{macro}, @var{list})
10687 @defmacx m4_map_sep (@var{macro}, @var{separator}, @var{list})
10688 Loop over the comma separated quoted list of argument descriptions in
10689 @var{list}, and invoke @var{macro} with the arguments.  An argument
10690 description is in turn a comma-separated quoted list of quoted elements,
10691 suitable for @code{m4_apply}, making it possible to invoke @var{macro}
10692 without arguments if an argument description is empty.
10693 @code{m4_map_sep} additionally outputs @var{separator} between macro
10694 invocations, with no additional expansion of the separator.
10695 @example
10696 m4_map([m4_count], [])
10697 @result{}
10698 m4_map([ m4_count], [[],
10699                      [[1]],
10700                      [[1], [2]]])
10701 @result{} 0 1 2
10702 m4_map_sep([m4_eval], [,], [[[1+2]],
10703                             [[10], [16]]])
10704 @result{}3,a
10705 @end example
10706 @end defmac
10708 @defmac m4_shiftn (@var{count}, @dots{})
10709 @defmacx m4_shift2 (@dots{})
10710 @defmacx m4_shift3 (@dots{})
10711 @msindex{shift2}
10712 @msindex{shift3}
10713 @msindex{shiftn}
10714 @code{m4_shiftn} performs @var{count} iterations of @code{m4_shift},
10715 along with validation that enough arguments were passed in to match the
10716 shift count, and that the count is positive.  @code{m4_shift2} and
10717 @code{m4_shift3} are specializations
10718 of @code{m4_shiftn}, introduced in Autoconf 2.62, and are more efficient
10719 for two and three shifts, respectively.
10720 @end defmac
10723 @node Evaluation Macros
10724 @subsection Evaluation Macros
10726 The following macros give some control over the order of the evaluation
10727 by adding or removing levels of quotes.
10729 @defmac m4_apply (@var{macro}, @var{list})
10730 @msindex apply
10731 Apply the elements of the quoted, comma-separated @var{list} as the
10732 arguments to @var{macro}.  If @var{list} is empty, invoke @var{macro}
10733 without arguments.  Note the difference between @code{m4_indir}, which
10734 expects its first argument to be a macro name but can use names that are
10735 otherwise invalid, and @code{m4_apply}, where @var{macro} can contain
10736 other text, but must end in a valid macro name.
10737 @example
10738 m4_apply([m4_count], [])
10739 @result{}0
10740 m4_apply([m4_count], [[]])
10741 @result{}1
10742 m4_apply([m4_count], [[1], [2]])
10743 @result{}2
10744 m4_apply([m4_join], [[|], [1], [2]])
10745 @result{}1|2
10746 @end example
10747 @end defmac
10749 @defmac m4_count (@var{arg1}, @dots{})
10750 @msindex{count}
10751 This macro returns the decimal count of the number of arguments it was
10752 passed.
10753 @end defmac
10755 @defmac m4_do (@var{arg1}, @dots{})
10756 @msindex{do}
10757 This macro loops over its arguments and expands each @var{arg} in
10758 sequence.  Its main use is for readability; it allows the use of
10759 indentation and fewer @code{dnl} to result in the same expansion.
10760 @end defmac
10762 @defmac m4_dquote (@var{arg1}, @dots{})
10763 @msindex{dquote}
10764 Return the arguments as a quoted list of quoted arguments.
10765 Conveniently, if there is just one @var{arg}, this effectively adds a
10766 level of quoting.
10767 @end defmac
10769 @defmac m4_dquote_elt (@var{arg1}, @dots{})
10770 @msindex{dquote_elt}
10771 Return the arguments as a series of double-quoted arguments.  Whereas
10772 @code{m4_dquote} returns a single argument, @code{m4_dquote_elt} returns
10773 as many arguments as it was passed.
10774 @end defmac
10776 @defmac m4_echo (@var{arg1}, @dots{})
10777 @msindex{echo}
10778 Return the arguments, with the same level of quoting.  Other than
10779 discarding whitespace after unquoted commas, this macro is a no-op.
10780 @end defmac
10782 @defmac m4_expand (@var{arg})
10783 @msindex{expand}
10784 Return the expansion of @var{arg} as a quoted string.  Whereas
10785 @code{m4_quote} is designed to collect expanded text into a single
10786 argument, @code{m4_expand} is designed to perform one level of expansion
10787 on quoted text.  The distinction is in the treatment of whitespace
10788 following a comma in the original @var{arg}.  Any time multiple
10789 arguments are collected into one with @code{m4_quote}, the M4 argument
10790 collection rules discard the whitespace.  However, with @code{m4_expand},
10791 whitespace is preserved, even after the expansion of macros contained in
10792 @var{arg}.
10794 Note that @code{m4_expand} cannot parse everything.  The expansion of
10795 @var{arg} must not contain unbalanced quotes (although quadrigraphs can
10796 get around this), nor unbalanced parentheses (portable shell @code{case}
10797 statements are a major culprit here, but creative shell comments can get
10798 around this).
10800 @example
10801 m4_define([active], [ACT, IVE])dnl
10802 m4_define([active2], [[ACT, IVE]])dnl
10803 m4_quote(active, active)
10804 @result{}ACT,IVE,ACT,IVE
10805 m4_expand([active, active])
10806 @result{}ACT, IVE, ACT, IVE
10807 m4_quote(active2, active2)
10808 @result{}ACT, IVE,ACT, IVE
10809 m4_expand([active2, active2])
10810 @result{}ACT, IVE, ACT, IVE
10811 @end example
10812 @end defmac
10814 @defmac m4_ignore (@dots{})
10815 @msindex{ignore}
10816 This macro was introduced in Autoconf 2.62.  Expands to nothing,
10817 ignoring all of its arguments.  By itself, this isn't very useful.
10818 However, it can be used to conditionally ignore an arbitrary number of
10819 arguments, by making a decision about which macro name to apply to a
10820 list of arguments.
10821 @example
10822 dnl foo outputs a message only if [debug] is defined.
10823 m4_define([foo],
10824 [m4_ifdef([debug],[AC_MSG_NOTICE],[m4_ignore])([debug message])])
10825 @end example
10827 Note that for earlier versions of Autoconf, the macro @code{__gnu__} can
10828 serve the same purpose, although it is less readable.
10829 @end defmac
10831 @defmac m4_make_list (@var{arg1}, @dots{})
10832 @msindex{make_list}
10833 This macro exists to aid debugging of M4sugar algorithms.  Its net
10834 effect is similar to @code{m4_dquote}---it produces a quoted list of
10835 quoted arguments, for each @var{arg}.  The difference is that this
10836 version uses a comma-newline separator instead of just comma, to improve
10837 readability of the list; with the result that it is less efficient than
10838 @code{m4_dquote}.
10839 @example
10840 m4_define([zero],[0])m4_define([one],[1])m4_define([two],[2])dnl
10841 m4_dquote(zero, [one], [[two]])
10842 @result{}[0],[one],[[two]]
10843 m4_make_list(zero, [one], [[two]])
10844 @result{}[0],
10845 @result{}[one],
10846 @result{}[[two]]
10847 m4_foreach([number], m4_dquote(zero, [one], [[two]]), [ number])
10848 @result{} 0 1 two
10849 m4_foreach([number], m4_make_list(zero, [one], [[two]]), [ number])
10850 @result{} 0 1 two
10851 @end example
10852 @end defmac
10854 @c m4_noquote is too dangerous to document - it invokes macros that
10855 @c probably rely on @samp{[]} nested quoting for proper operation.  The
10856 @c user should generally prefer m4_unquote instead.
10858 @defmac m4_quote (@var{arg1}, @dots{})
10859 @msindex{quote}
10860 Return the arguments as a single entity, i.e., wrap them into a pair of
10861 quotes.  This effectively collapses multiple arguments into one,
10862 although it loses whitespace after unquoted commas in the process.
10863 @end defmac
10865 @defmac m4_unquote (@var{arg1}, @dots{})
10866 @msindex{unquote}
10867 This macro was introduced in Autoconf 2.62.  Expand each argument,
10868 separated by commas.  For a single @var{arg}, this effectively removes a
10869 layer of quoting, and @code{m4_unquote([@var{arg}])} is more efficient
10870 than the equivalent @code{m4_do([@var{arg}])}.  For multiple arguments,
10871 this results in an unquoted list of expansions.  This is commonly used
10872 with @code{m4_split}, in order to convert a single quoted list into a
10873 series of quoted elements.
10874 @end defmac
10876 The following example aims at emphasizing the difference between several
10877 scenarios: not using these macros, using @code{m4_defn}, using
10878 @code{m4_quote}, using @code{m4_dquote}, and using @code{m4_expand}.
10880 @example
10881 $ @kbd{cat example.m4}
10882 # Overquote, so that quotes are visible.
10883 m4_define([show], [$[]1 = [$1], $[]@@ = [$@@]])
10884 m4_define([a], [A])
10885 m4_define([mkargs], [1, 2[,] 3])
10886 m4_define([arg1], [[$1]])
10887 m4_divert([0])dnl
10888 show(a, b)
10889 show([a, b])
10890 show(m4_quote(a, b))
10891 show(m4_dquote(a, b))
10892 show(m4_expand([a, b]))
10894 arg1(mkargs)
10895 arg1([mkargs])
10896 arg1(m4_defn([mkargs]))
10897 arg1(m4_quote(mkargs))
10898 arg1(m4_dquote(mkargs))
10899 arg1(m4_expand([mkargs]))
10900 $ @kbd{autom4te -l m4sugar example.m4}
10901 $1 = A, $@@ = [A],[b]
10902 $1 = a, b, $@@ = [a, b]
10903 $1 = A,b, $@@ = [A,b]
10904 $1 = [A],[b], $@@ = [[A],[b]]
10905 $1 = A, b, $@@ = [A, b]
10908 mkargs
10909 1, 2[,] 3
10910 1,2, 3
10911 [1],[2, 3]
10912 1,2, 3
10913 @end example
10916 @node Text processing Macros
10917 @subsection String manipulation in M4
10919 The following macros may be used to manipulate strings in M4.  Many of
10920 the macros in this section intentionally result in quoted strings as
10921 output, rather than subjecting the arguments to further expansions.  As
10922 a result, if you are manipulating text that contains active M4
10923 characters, the arguments are passed with single quoting rather than
10924 double.
10926 @defmac m4_append (@var{macro-name}, @var{string}, @ovar{separator})
10927 @defmacx m4_append_uniq (@var{macro-name}, @var{string}, @ovar{separator} @
10928   @ovar{if-uniq}, @ovar{if-duplicate})
10929 @msindex{append}
10930 @msindex{append_uniq}
10931 Redefine @var{macro-name} to its former contents with @var{separator}
10932 and @var{string} added at the end.  If @var{macro-name} was undefined
10933 before (but not if it was defined but empty), then no @var{separator} is
10934 added.  As of Autoconf 2.62, neither @var{string} nor @var{separator}
10935 are expanded during this macro; instead, they are expanded when
10936 @var{macro-name} is invoked.
10938 @code{m4_append} can be used to grow strings, and @code{m4_append_uniq}
10939 to grow strings without duplicating substrings.  Additionally,
10940 @code{m4_append_uniq} takes two optional parameters as of Autoconf 2.62;
10941 @var{if-uniq} is expanded if @var{string} was appended, and
10942 @var{if-duplicate} is expanded if @var{string} was already present.
10943 Also, @code{m4_append_uniq} warns if @var{separator} is not empty, but
10944 occurs within @var{string}, since that can lead to duplicates.
10946 @example
10947 m4_define([active], [ACTIVE])dnl
10948 m4_append([sentence], [This is an])dnl
10949 m4_append([sentence], [ active ])dnl
10950 m4_append([sentence], [symbol.])dnl
10951 sentence
10952 @result{}This is an ACTIVE symbol.
10953 m4_undefine([active])dnl
10954 @result{}This is an active symbol.
10955 m4_append_uniq([list], [one], [, ], [new], [existing])
10956 @result{}new
10957 m4_append_uniq([list], [one], [, ], [new], [existing])
10958 @result{}existing
10959 m4_append_uniq([list], [two], [, ], [new], [existing])
10960 @result{}new
10961 m4_append_uniq([list], [three], [, ], [new], [existing])
10962 @result{}new
10963 m4_append_uniq([list], [two], [, ], [new], [existing])
10964 @result{}existing
10965 list
10966 @result{}one, two, three
10967 m4_dquote(list)
10968 @result{}[one],[two],[three]
10969 m4_append([list2], [one], [[, ]])dnl
10970 m4_append_uniq([list2], [two], [[, ]])dnl
10971 m4_append([list2], [three], [[, ]])dnl
10972 list2
10973 @result{}one, two, three
10974 m4_dquote(list2)
10975 @result{}[one, two, three]
10976 @end example
10977 @end defmac
10979 @defmac m4_append_uniq_w (@var{macro-name}, @var{strings})
10980 @msindex{append_uniq_w}
10981 This macro was introduced in Autoconf 2.62.  It is similar to
10982 @code{m4_append_uniq}, but treats @var{strings} as a whitespace
10983 separated list of words to append, and only appends unique words.
10984 @var{macro-name} is updated with a single space between new words.
10985 @example
10986 m4_append_uniq_w([numbers], [1 1 2])dnl
10987 m4_append_uniq_w([numbers], [ 2 3 ])dnl
10988 numbers
10989 @result{}1 2 3
10990 @end example
10991 @end defmac
10993 @defmac m4_combine (@ovar{separator}, @var{prefix-list}, @ovar{infix}, @
10994   @var{suffix-1}, @dots{})
10995 @msindex{combine}
10996 This macro produces a quoted string containing the pairwise combination
10997 of every element of the quoted, comma-separated @var{prefix-list}, and
10998 every element from the @var{suffix} arguments.  Each pairwise
10999 combination is joined with @var{infix} in the middle, and successive
11000 pairs are joined by @var{separator}.  No expansion occurs on any of the
11001 arguments.  No output occurs if either the @var{prefix} or @var{suffix}
11002 list is empty, but the lists can contain empty elements.
11003 @example
11004 m4_define([a], [oops])dnl
11005 m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
11006 @result{}a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
11007 m4_combine([, ], [[a], [b]], [-])
11008 @result{}
11009 m4_combine([, ], [[a], [b]], [-], [])
11010 @result{}a-, b-
11011 m4_combine([, ], [], [-], [1], [2])
11012 @result{}
11013 m4_combine([, ], [[]], [-], [1], [2])
11014 @result{}-1, -2
11015 @end example
11016 @end defmac
11018 @defmac m4_flatten (@var{string})
11019 @msindex{flatten}
11020 Flatten @var{string} into a single line.  Delete all backslash-newline
11021 pairs, and replace all remaining newlines with a space.  The result is
11022 still a quoted string.
11023 @end defmac
11025 @defmac m4_join (@ovar{separator}, @var{args}@dots{})
11026 @msindex{join}
11027 Concatenate each @var{arg}, separated by @var{separator}, with the
11028 exception that no back-to-back separators are issued for empty
11029 arguments.  The result is a quoted string.
11030 @example
11031 m4_define([active], [ACTIVE])dnl
11032 m4_join([|], [one], [], [active], [two])
11033 @result{}one|active|two
11034 @end example
11036 Note that if all you intend to do is join @var{args} with commas between
11037 them, to form a quoted list suitable for @code{m4_foreach}, it is more
11038 efficient to use @code{m4_dquote}.
11039 @end defmac
11041 @defmac m4_newline
11042 @msindex{newline}
11043 This macro was introduced in Autoconf 2.62, and expands to a newline.
11044 It is primarily useful for maintaining macro formatting, and ensuring
11045 that M4 does not discard leading whitespace during argument collection.
11046 @end defmac
11048 @defmac m4_normalize (@var{string})
11049 @msindex{normalize}
11050 Remove leading and trailing spaces and tabs, sequences of
11051 backslash-then-newline, and replace multiple spaces, tabs, and newlines
11052 with a single space.  This is a combination of @code{m4_flatten} and
11053 @code{m4_strip}.
11054 @end defmac
11056 @defmac m4_re_escape (@var{string})
11057 @msindex{re_escape}
11058 Backslash-escape all characters in @var{string} that are active in
11059 regexps.
11060 @end defmac
11062 @defmac m4_split (@var{string}, @dvar{regexp, [\t ]+})
11063 @msindex{split}
11064 Split @var{string} into an M4 list of elements quoted by @samp{[} and
11065 @samp{]}, while keeping white space at the beginning and at the end.
11066 If @var{regexp} is given, use it instead of @samp{[\t ]+} for splitting.
11067 If @var{string} is empty, the result is an empty list.
11068 @end defmac
11070 @defmac m4_strip (@var{string})
11071 @msindex{strip}
11072 Strip whitespace from @var{string}.  Sequences of spaces and tabs are
11073 reduced to a single space, then leading and trailing spaces are removed.
11074 The result is still a quoted string.  Note that this does not interfere
11075 with newlines; if you want newlines stripped as well, consider
11076 @code{m4_flatten}, or do it all at once with @code{m4_normalize}.
11077 @end defmac
11079 @defmac m4_text_box (@var{message}, @dvar{frame, -})
11080 @msindex{text_box}
11081 Add a text box around @var{message}, using @var{frame} as the border
11082 character above and below the message.  The frame correctly accounts for
11083 the subsequent expansion of @var{message}.  For example:
11084 @example
11085 m4_define([macro], [abc])dnl
11086 m4_text_box([macro])
11087 @result{}## --- ##
11088 @result{}## abc ##
11089 @result{}## --- ##
11090 @end example
11091 @end defmac
11093 @defmac m4_text_wrap (@var{string}, @ovar{prefix}, @
11094   @dvar{prefix1, @var{prefix}}, @dvar{width, 79})
11095 @msindex{text_wrap}
11096 Break @var{string} into a series of whitespace-separated words, then
11097 output those words separated by spaces, and wrapping lines any time the
11098 output would exceed @var{width} columns.  If given, @var{prefix1} begins
11099 the first line, and @var{prefix} begins all wrapped lines.  If
11100 @var{prefix1} is longer than @var{prefix}, then the first line consists
11101 of just @var{prefix1}.  If @var{prefix} is longer than @var{prefix1},
11102 padding is inserted so that the first word of @var{string} begins at the
11103 same indentation as all wrapped lines.  Note that using literal tab
11104 characters in any of the arguments will interfere with the calculation
11105 of width.  No expansions occur on @var{prefix}, @var{prefix1}, or the
11106 words of @var{string}, although quadrigraphs are recognized.
11108 For some examples:
11109 @example
11110 m4_text_wrap([Short string */], [   ], [/* ], [20])
11111 @result{}/* Short string */
11112 m4_text_wrap([Much longer string */], [   ], [/* ], [20])
11113 @result{}/* Much longer
11114 @result{}   string */
11115 m4_text_wrap([Short doc.], [          ], [  --short ], [30])
11116 @result{}  --short Short doc.
11117 m4_text_wrap([Short doc.], [          ], [  --too-wide ], [30])
11118 @result{}  --too-wide
11119 @result{}          Short doc.
11120 m4_text_wrap([Super long documentation.], [     ],
11121              [  --too-wide ], 30)
11122 @result{}  --too-wide
11123 @result{}     Super long
11124 @result{}     documentation.
11125 @end example
11126 @end defmac
11128 @defmac m4_tolower (@var{string})
11129 @defmacx m4_toupper (@var{string})
11130 @msindex{tolower}
11131 @msindex{toupper}
11132 Return @var{string} with letters converted to upper or lower case,
11133 respectively.
11134 @end defmac
11136 @node Number processing Macros
11137 @subsection Arithmetic computation in M4
11139 The following macros make it easier to deal with arithmetic operations.
11140 Where a parameter is documented as taking an arithmetic expression, you
11141 can use anything that can be parsed by @code{m4_eval}.
11143 @defmac m4_cmp (@var{expr-1}, @var{expr-2})
11144 @msindex{cmp}
11145 Compare the arithmetic expressions @var{expr-1} and @var{expr-2}, and
11146 expand to @samp{-1} if @var{expr-1} is smaller, @samp{0} if they are
11147 equal, and @samp{1} if @var{expr-1} is larger.
11148 @end defmac
11150 @defmac m4_list_cmp (@var{list-1}, @var{list-2})
11151 @msindex{list_cmp}
11152 Compare the two M4 lists consisting of comma-separated arithmetic
11153 expressions, left to right.  Expand to @samp{-1} for the first element
11154 pairing where the value from @var{list-1} is smaller, @samp{1} where the
11155 value from @var{list-2} is smaller, or @samp{0} if both lists have the
11156 same values.  If one list is shorter than the other, the remaining
11157 elements of the longer list are compared against 0.
11158 @example
11159 m4_list_cmp([1, 0],       [1])
11160 @result{}0
11161 m4_list_cmp([1, [1 * 0]], [1, 0])
11162 @result{}0
11163 m4_list_cmp([1, 2],       [1, 0])
11164 @result{}1
11165 m4_list_cmp([1, [1+1], 3],[1, 2])
11166 @result{}1
11167 m4_list_cmp([1, 2, -3],   [1, 2])
11168 @result{}-1
11169 m4_list_cmp([1, 0],       [1, 2])
11170 @result{}-1
11171 m4_list_cmp([1],          [1, 2])
11172 @result{}-1
11173 @end example
11174 @end defmac
11176 @defmac m4_max (@var{arg1}, @dots{})
11177 @msindex{max}
11178 This macro was introduced in Autoconf 2.62.  Expand to the decimal value
11179 of the maximum arithmetic expression among all the arguments.
11180 @end defmac
11182 @defmac m4_min (@var{arg1}, @dots{})
11183 @msindex{min}
11184 This macro was introduced in Autoconf 2.62.  Expand to the decimal value
11185 of the minimum arithmetic expression among all the arguments.
11186 @end defmac
11188 @defmac m4_sign (@var{expr})
11189 @msindex{sign}
11190 Expand to @samp{-1} if the arithmetic expression @var{expr} is negative,
11191 @samp{1} if it is positive, and @samp{0} if it is zero.
11192 @end defmac
11194 @anchor{m4_version_compare}
11195 @defmac m4_version_compare (@var{version-1}, @var{version-2})
11196 @msindex{version_compare}
11197 This macro was introduced in Autoconf 2.53, but had a number of
11198 usability limitations that were not lifted until Autoconf 2.62.  Compare
11199 the version strings @var{version-1} and @var{version-2}, and expand to
11200 @samp{-1} if @var{version-1} is smaller, @samp{0} if they are the same,
11201 or @samp{1} @var{version-2} is smaller.  Version strings must be a list
11202 of elements separated by @samp{.}, @samp{,} or @samp{-}, where each
11203 element is a number along with optional case-insensitive letters
11204 designating beta releases.  The comparison stops at the leftmost element
11205 that contains a difference, although a 0 element compares equal to a
11206 missing element.
11208 It is permissible to include commit identifiers in @var{version}, such
11209 as an abbreviated SHA1 of the commit, provided there is still a
11210 monotonically increasing prefix to allow for accurate version-based
11211 comparisons.  For example, this paragraph was written when the
11212 development snapshot of autoconf claimed to be at version
11213 @samp{2.61a-248-dc51}, or 248 commits after the 2.61a release, with an
11214 abbreviated commit identification of @samp{dc51}.
11216 @example
11217 m4_version_compare([1.1], [2.0])
11218 @result{}-1
11219 m4_version_compare([2.0b], [2.0a])
11220 @result{}1
11221 m4_version_compare([1.1.1], [1.1.1a])
11222 @result{}-1
11223 m4_version_compare([1.2], [1.1.1a])
11224 @result{}1
11225 m4_version_compare([1.0], [1])
11226 @result{}0
11227 m4_version_compare([1.1pre], [1.1PRE])
11228 @result{}0
11229 m4_version_compare([1.1a], [1,10])
11230 @result{}-1
11231 m4_version_compare([2.61a], [2.61a-248-dc51])
11232 @result{}-1
11233 m4_version_compare([2.61b], [2.61a-248-dc51])
11234 @result{}1
11235 @end example
11236 @end defmac
11239 @node Forbidden Patterns
11240 @subsection Forbidden Patterns
11241 @cindex Forbidden patterns
11242 @cindex Patterns, forbidden
11244 M4sugar provides a means to define suspicious patterns, patterns
11245 describing tokens which should not be found in the output.  For
11246 instance, if an Autoconf @file{configure} script includes tokens such as
11247 @samp{AC_DEFINE}, or @samp{dnl}, then most probably something went
11248 wrong (typically a macro was not evaluated because of overquotation).
11250 M4sugar forbids all the tokens matching @samp{^_?m4_} and @samp{^dnl$}.
11251 Additional layers, such as M4sh and Autoconf, add additional forbidden
11252 patterns to the list.
11254 @defmac m4_pattern_forbid (@var{pattern})
11255 @msindex{pattern_forbid}
11256 Declare that no token matching @var{pattern} must be found in the output.
11257 Comments are not checked; this can be a problem if, for instance, you
11258 have some macro left unexpanded after an @samp{#include}.  No consensus
11259 is currently found in the Autoconf community, as some people consider it
11260 should be valid to name macros in comments (which doesn't make sense to
11261 the authors of this documentation: input, such as macros, should be
11262 documented by @samp{dnl} comments; reserving @samp{#}-comments to
11263 document the output).
11264 @end defmac
11266 Of course, you might encounter exceptions to these generic rules, for
11267 instance you might have to refer to @samp{$m4_flags}.
11269 @defmac m4_pattern_allow (@var{pattern})
11270 @msindex{pattern_allow}
11271 Any token matching @var{pattern} is allowed, including if it matches an
11272 @code{m4_pattern_forbid} pattern.
11273 @end defmac
11275 @node Programming in M4sh
11276 @section Programming in M4sh
11278 @c FIXME: Eventually will become a chapter, as it is not related to
11279 @c programming in M4 per se.
11281 M4sh, pronounced ``mash'', is aiming at producing portable Bourne shell
11282 scripts.  This name was coined by Lars J. Aas, who notes that,
11283 according to the Webster's Revised Unabridged Dictionary (1913):
11285 @quotation
11286 Mash \Mash\, n.  [Akin to G. meisch, maisch, meische, maische, mash,
11287 wash, and prob.@: to AS. miscian to mix.  See ``Mix''.]
11289 @enumerate 1
11290 @item
11291 A mass of mixed ingredients reduced to a soft pulpy state by beating or
11292 pressure@enddots{}
11294 @item
11295 A mixture of meal or bran and water fed to animals.
11297 @item
11298 A mess; trouble.  [Obs.] --Beau.@: & Fl.
11299 @end enumerate
11300 @end quotation
11303 For the time being, it is not mature enough to be widely used.
11305 M4sh reserves the M4 macro namespace @samp{^_AS_} for internal use, and
11306 the namespace @samp{^AS_} for M4sh macros.  It also reserves the shell
11307 and environment variable namespace @samp{^as_}, and the here-doc
11308 delimiter namespace @samp{^_AS[A-Z]} in the output file.  You should not
11309 define your own macros or output shell code that conflicts with these
11310 namespaces.
11312 M4sh provides portable alternatives for some common shell constructs
11313 that unfortunately are not portable in practice.
11315 @c Deprecated, to be replaced by a better API
11316 @ignore
11317 @defmac AS_BASENAME (@var{file-name})
11318 @asindex{BASENAME}
11319 Output the non-directory portion of @var{file-name}.  For example,
11320 if @code{$file} is @samp{/one/two/three}, the command
11321 @code{base=`AS_BASENAME(["$file"])`} sets @code{base} to @samp{three}.
11322 @end defmac
11323 @end ignore
11325 @defmac AS_BOURNE_COMPATIBLE
11326 @asindex{BOURNE_COMPATIBLE}
11327 Set up the shell to be more compatible with the Bourne shell as
11328 standardized by Posix, if possible.  This may involve setting
11329 environment variables, or setting options, or similar
11330 implementation-specific actions.
11331 @end defmac
11333 @defmac AS_CASE (@var{word}, @ovar{pattern1}, @ovar{if-matched1}, @
11334   @dots{}, @ovar{default})
11335 @asindex{CASE}
11336 Expand into a shell @samp{case} statement, where @var{word} is matched
11337 against one or more patterns.  @var{if-matched} is run if the
11338 corresponding pattern matched @var{word}, else @var{default} is run.
11339 @end defmac
11341 @defmac AS_DIRNAME (@var{file-name})
11342 @asindex{DIRNAME}
11343 Output the directory portion of @var{file-name}.  For example,
11344 if @code{$file} is @samp{/one/two/three}, the command
11345 @code{dir=`AS_DIRNAME(["$file"])`} sets @code{dir} to @samp{/one/two}.
11346 @end defmac
11348 @defmac AS_IF (@var{test1}, @ovar{run-if-true1}, @dots{}, @ovar{run-if-false})
11349 @asindex{IF}
11350 Run shell code @var{test1}.  If @var{test1} exits with a zero status then
11351 run shell code @var{run-if-true1}, else examine further tests.  If no test
11352 exits with a zero status, run shell code @var{run-if-false}, with
11353 simplifications if either @var{run-if-true1} or @var{run-if-false1}
11354 is empty.  For example,
11356 @example
11357 AS_IF([test "$foo" = yes], [HANDLE_FOO([yes])],
11358       [test "$foo" != no], [HANDLE_FOO([maybe])],
11359       [echo foo not specified])
11360 @end example
11362 @noindent
11363 ensures any required macros of @code{HANDLE_FOO}
11364 are expanded before the first test.
11365 @end defmac
11367 @defmac AS_MKDIR_P (@var{file-name})
11368 @asindex{MKDIR_P}
11369 Make the directory @var{file-name}, including intervening directories
11370 as necessary.  This is equivalent to @samp{mkdir -p @var{file-name}},
11371 except that it is portable to older versions of @command{mkdir} that
11372 lack support for the @option{-p} option.  Also, @code{AS_MKDIR_P}
11373 succeeds if @var{file-name} is a symbolic link to an existing directory,
11374 even though Posix is unclear whether @samp{mkdir -p} should
11375 succeed in that case.  If creation of @var{file-name} fails, exit the
11376 script.
11378 Also see the @code{AC_PROG_MKDIR_P} macro (@pxref{Particular Programs}).
11379 @end defmac
11381 @defmac AS_SHELL_SANITIZE
11382 @asindex{SHELL_SANITIZE}
11383 Initialize the shell suitably for @code{configure} scripts.  This has
11384 the effect of @code{AS_BOURNE_COMPATIBLE}, and sets some other
11385 environment variables for predictable results from configuration tests.
11386 For example, it sets @env{LC_ALL} to change to the default C locale.
11387 @xref{Special Shell Variables}.
11388 @end defmac
11390 @defmac AS_TR_CPP (@var{expression})
11391 @asindex{TR_CPP}
11392 Transform @var{expression} into a valid right-hand side for a C @code{#define}.
11393 For example:
11395 @example
11396 # This outputs "#define HAVE_CHAR_P 1".
11397 type="char *"
11398 echo "#define AS_TR_CPP([HAVE_$type]) 1"
11399 @end example
11400 @end defmac
11402 @defmac AS_TR_SH (@var{expression})
11403 @asindex{TR_SH}
11404 Transform @var{expression} into a valid shell variable name.  For example:
11406 @example
11407 # This outputs "Have it!".
11408 header="sys/some file.h"
11409 AS_TR_SH([HAVE_$header])=yes
11410 if test "$HAVE_sys_some_file_h" = yes; then echo "Have it!"; fi
11411 @end example
11412 @end defmac
11414 @defmac AS_SET_CATFILE (@var{var}, @var{dir}, @var{file})
11415 @asindex{SET_CATFILE}
11416 Set the shell variable @var{var} to @var{dir}/@var{file}, but
11417 optimizing the common cases (@var{dir} or @var{file} is @samp{.},
11418 @var{file} is absolute, etc.).
11419 @end defmac
11422 @node File Descriptor Macros
11423 @section File Descriptor Macros
11424 @cindex input
11425 @cindex standard input
11426 @cindex file descriptors
11427 @cindex descriptors
11428 @cindex low-level output
11429 @cindex output, low-level
11431 The following macros define file descriptors used to output messages
11432 (or input values) from @file{configure} scripts.
11433 For example:
11435 @example
11436 echo "$wombats found" >&AS_MESSAGE_LOG_FD
11437 echo 'Enter desired kangaroo count:' >&AS_MESSAGE_FD
11438 read kangaroos <&AS_ORIGINAL_STDIN_FD`
11439 @end example
11441 @noindent
11442 However doing so is seldom needed, because Autoconf provides higher
11443 level macros as described below.
11445 @defmac AS_MESSAGE_FD
11446 @asindex{MESSAGE_FD}
11447 The file descriptor for @samp{checking for...}  messages and results.
11448 Normally this directs messages to the standard output, however when
11449 @command{configure} is run with the @option{-q} option, messages sent to
11450 @code{AS_MESSAGE_FD} are discarded.
11452 If you want to display some messages, consider using one of the printing
11453 macros (@pxref{Printing Messages}) instead.  Copies of messages output
11454 via these macros are also recorded in @file{config.log}.
11455 @end defmac
11457 @defmac AS_MESSAGE_LOG_FD
11458 @asindex{MESSAGE_LOG_FD}
11460 The file descriptor for messages logged to @file{config.log}.  Macros
11461 that run tools, like @code{AC_COMPILE_IFELSE} (@pxref{Running the
11462 Compiler}), redirect all output to this descriptor.  You may want to do
11463 so if you develop such a low-level macro.
11464 @end defmac
11466 @defmac AS_ORIGINAL_STDIN_FD
11467 @asindex{ORIGINAL_STDIN_FD}
11468 The file descriptor for the original standard input.
11470 When @command{configure} runs, it may accidentally execute an
11471 interactive command that has the same name as the non-interactive meant
11472 to be used or checked.  If the standard input was the terminal, such
11473 interactive programs would cause @command{configure} to stop, pending
11474 some user input.  Therefore @command{configure} redirects its standard
11475 input from @file{/dev/null} during its initialization.  This is not
11476 normally a problem, since @command{configure} normally does not need
11477 user input.
11479 In the extreme case where your @file{configure} script really needs to
11480 obtain some values from the original standard input, you can read them
11481 explicitly from @code{AS_ORIGINAL_STDIN_FD}.
11482 @end defmac
11485 @c =================================================== Writing Autoconf Macros.
11487 @node Writing Autoconf Macros
11488 @chapter Writing Autoconf Macros
11490 When you write a feature test that could be applicable to more than one
11491 software package, the best thing to do is encapsulate it in a new macro.
11492 Here are some instructions and guidelines for writing Autoconf macros.
11494 @menu
11495 * Macro Definitions::           Basic format of an Autoconf macro
11496 * Macro Names::                 What to call your new macros
11497 * Reporting Messages::          Notifying @command{autoconf} users
11498 * Dependencies Between Macros::  What to do when macros depend on other macros
11499 * Obsoleting Macros::           Warning about old ways of doing things
11500 * Coding Style::                Writing Autoconf macros @`a la Autoconf
11501 @end menu
11503 @node Macro Definitions
11504 @section Macro Definitions
11506 @acindex{DEFUN}
11507 Autoconf macros are defined using the @code{AC_DEFUN} macro, which is
11508 similar to the M4 builtin @code{m4_define} macro.  In addition to
11509 defining a macro, @code{AC_DEFUN} adds to it some code that is used to
11510 constrain the order in which macros are called (@pxref{Prerequisite
11511 Macros}).
11513 An Autoconf macro definition looks like this:
11515 @example
11516 AC_DEFUN(@var{macro-name}, @var{macro-body})
11517 @end example
11519 You can refer to any arguments passed to the macro as @samp{$1},
11520 @samp{$2}, etc.  @xref{Definitions, , How to define new macros, m4.info,
11521 @acronym{GNU} M4}, for more complete information on writing M4 macros.
11523 Be sure to properly quote both the @var{macro-body} @emph{and} the
11524 @var{macro-name} to avoid any problems if the macro happens to have
11525 been previously defined.
11527 Each macro should have a header comment that gives its prototype, and a
11528 brief description.  When arguments have default values, display them in
11529 the prototype.  For example:
11531 @example
11532 # AC_MSG_ERROR(ERROR, [EXIT-STATUS = 1])
11533 # --------------------------------------
11534 m4_define([AC_MSG_ERROR],
11535   [@{ AS_MESSAGE([error: $1], [2])
11536      exit m4_default([$2], [1]); @}])
11537 @end example
11539 Comments about the macro should be left in the header comment.  Most
11540 other comments make their way into @file{configure}, so just keep
11541 using @samp{#} to introduce comments.
11543 @cindex @code{dnl}
11544 If you have some special comments about pure M4 code, comments
11545 that make no sense in @file{configure} and in the header comment, then
11546 use the builtin @code{dnl}: it causes M4 to discard the text
11547 through the next newline.
11549 Keep in mind that @code{dnl} is rarely needed to introduce comments;
11550 @code{dnl} is more useful to get rid of the newlines following macros
11551 that produce no output, such as @code{AC_REQUIRE}.
11554 @node Macro Names
11555 @section Macro Names
11557 All of the public Autoconf macros have all-uppercase names in the
11558 namespace @samp{^AC_} to prevent them from accidentally conflicting with
11559 other text; Autoconf also reserves the namespace @samp{^_AC_} for
11560 internal macros.  All shell variables that they use for internal
11561 purposes have mostly-lowercase names starting with @samp{ac_}.  Autoconf
11562 also uses here-doc delimiters in the namespace @samp{^_AC[A-Z]}.  During
11563 @command{configure}, files produced by Autoconf make heavy use of the
11564 file system namespace @samp{^conf}.
11566 Since Autoconf is built on top of M4sugar (@pxref{Programming in
11567 M4sugar}) and M4sh (@pxref{Programming in M4sh}), you must also be aware
11568 of those namespaces (@samp{^_?\(m4\|AS\)_}).  And since
11569 @file{configure.ac} is also designed to be scanned by Autoheader,
11570 Autoscan, Autoupdate, and Automake, you should be aware of the
11571 @samp{^_?A[HNUM]_} namespaces.  In general, you @emph{should not use}
11572 the namespace of a package that does not own the macro or shell code you
11573 are writing.
11575 To ensure that your macros don't conflict with present or future
11576 Autoconf macros, you should prefix your own macro names and any shell
11577 variables they use with some other sequence.  Possibilities include your
11578 initials, or an abbreviation for the name of your organization or
11579 software package.  Historically, people have not always followed the
11580 rule of using a namespace appropriate for their package, and this has
11581 made it difficult for determining the origin of a macro (and where to
11582 report bugs about that macro), as well as difficult for the true
11583 namespace owner to add new macros without interference from pre-existing
11584 uses of third-party macros.  Perhaps the best example of this confusion
11585 is the @code{AM_GNU_GETTEXT} macro, which belongs, not to Automake, but
11586 to Gettext.
11588 Most of the Autoconf macros' names follow a structured naming convention
11589 that indicates the kind of feature check by the name.  The macro names
11590 consist of several words, separated by underscores, going from most
11591 general to most specific.  The names of their cache variables use the
11592 same convention (@pxref{Cache Variable Names}, for more information on
11593 them).
11595 The first word of the name after the namepace initials (such as
11596 @samp{AC_}) usually tells the category
11597 of the feature being tested.  Here are the categories used in Autoconf for
11598 specific test macros, the kind of macro that you are more likely to
11599 write.  They are also used for cache variables, in all-lowercase.  Use
11600 them where applicable; where they're not, invent your own categories.
11602 @table @code
11603 @item C
11604 C language builtin features.
11605 @item DECL
11606 Declarations of C variables in header files.
11607 @item FUNC
11608 Functions in libraries.
11609 @item GROUP
11610 Posix group owners of files.
11611 @item HEADER
11612 Header files.
11613 @item LIB
11614 C libraries.
11615 @item PROG
11616 The base names of programs.
11617 @item MEMBER
11618 Members of aggregates.
11619 @item SYS
11620 Operating system features.
11621 @item TYPE
11622 C builtin or declared types.
11623 @item VAR
11624 C variables in libraries.
11625 @end table
11627 After the category comes the name of the particular feature being
11628 tested.  Any further words in the macro name indicate particular aspects
11629 of the feature.  For example, @code{AC_PROG_CC_STDC} checks whether the
11630 C compiler supports @acronym{ISO} Standard C.
11632 An internal macro should have a name that starts with an underscore;
11633 Autoconf internals should therefore start with @samp{_AC_}.
11634 Additionally, a macro that is an internal subroutine of another macro
11635 should have a name that starts with an underscore and the name of that
11636 other macro, followed by one or more words saying what the internal
11637 macro does.  For example, @code{AC_PATH_X} has internal macros
11638 @code{_AC_PATH_X_XMKMF} and @code{_AC_PATH_X_DIRECT}.
11640 @node Reporting Messages
11641 @section Reporting Messages
11642 @cindex Messages, from @command{autoconf}
11644 When macros statically diagnose abnormal situations, benign or fatal, it
11645 is possible to make @command{autoconf} detect the problem, and refuse to
11646 create @file{configure} in the case of an error.  The macros in this
11647 section are considered obsolescent, and new code should use M4sugar
11648 macros for this purpose, see @ref{Diagnostic Macros}.
11650 On the other hand, it is possible to want to detect errors when
11651 @command{configure} is run, which are dependent on the environment of
11652 the user rather than the maintainer.  For dynamic diagnostics, see
11653 @ref{Printing Messages}.
11655 @defmac AC_DIAGNOSE (@var{category}, @var{message})
11656 @acindex{DIAGNOSE}
11657 Report @var{message} as a warning (or as an error if requested by the
11658 user) if warnings of the @var{category} are turned on.  This macro is
11659 obsolescent; you are encouraged to use:
11660 @example
11661 m4_warn([@var{category}], [@var{message}])
11662 @end example
11663 @noindent
11664 instead.  @xref{m4_warn}, for more details, including valid
11665 @var{category} names.
11666 @end defmac
11668 @defmac AC_WARNING (@var{message})
11669 @acindex{WARNING}
11670 Report @var{message} as a syntax warning.  This macro is obsolescent;
11671 you are encouraged to use:
11672 @example
11673 m4_warn([syntax], [@var{message}])
11674 @end example
11675 @noindent
11676 instead.  @xref{m4_warn}, for more details, as well as better
11677 finer-grained categories of warnings (not all problems have to do with
11678 syntax).
11679 @end defmac
11681 @defmac AC_FATAL (@var{message})
11682 @acindex{FATAL}
11683 Report a severe error @var{message}, and have @command{autoconf} die.
11684 This macro is obsolescent; you are encouraged to use:
11685 @example
11686 m4_fatal([@var{message}])
11687 @end example
11688 @noindent
11689 instead.  @xref{m4_fatal}, for more details.
11690 @end defmac
11692 When the user runs @samp{autoconf -W error}, warnings from
11693 @code{m4_warn} (including those issued through @code{AC_DIAGNOSE} and
11694 @code{AC_WARNING}) are reported as errors, see @ref{autoconf Invocation}.
11696 @node Dependencies Between Macros
11697 @section Dependencies Between Macros
11698 @cindex Dependencies between macros
11700 Some Autoconf macros depend on other macros having been called first in
11701 order to work correctly.  Autoconf provides a way to ensure that certain
11702 macros are called if needed and a way to warn the user if macros are
11703 called in an order that might cause incorrect operation.
11705 @menu
11706 * Prerequisite Macros::         Ensuring required information
11707 * Suggested Ordering::          Warning about possible ordering problems
11708 * One-Shot Macros::             Ensuring a macro is called only once
11709 @end menu
11711 @node Prerequisite Macros
11712 @subsection Prerequisite Macros
11713 @cindex Prerequisite macros
11714 @cindex Macros, prerequisites
11716 A macro that you write might need to use values that have previously
11717 been computed by other macros.  For example, @code{AC_DECL_YYTEXT}
11718 examines the output of @code{flex} or @code{lex}, so it depends on
11719 @code{AC_PROG_LEX} having been called first to set the shell variable
11720 @code{LEX}.
11722 Rather than forcing the user of the macros to keep track of the
11723 dependencies between them, you can use the @code{AC_REQUIRE} macro to do
11724 it automatically.  @code{AC_REQUIRE} can ensure that a macro is only
11725 called if it is needed, and only called once.
11727 @defmac AC_REQUIRE (@var{macro-name})
11728 @acindex{REQUIRE}
11729 If the M4 macro @var{macro-name} has not already been called, call it
11730 (without any arguments).  Make sure to quote @var{macro-name} with
11731 square brackets.  @var{macro-name} must have been defined using
11732 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
11733 that it has been called.
11735 @code{AC_REQUIRE} must be used inside a macro defined by @code{AC_DEFUN}; it
11736 must not be called from the top level.
11737 @end defmac
11739 @code{AC_REQUIRE} is often misunderstood.  It really implements
11740 dependencies between macros in the sense that if one macro depends upon
11741 another, the latter is expanded @emph{before} the body of the
11742 former.  To be more precise, the required macro is expanded before
11743 the outermost defined macro in the current expansion stack.
11744 In particular, @samp{AC_REQUIRE([FOO])} is not replaced with the body of
11745 @code{FOO}.  For instance, this definition of macros:
11747 @example
11748 @group
11749 AC_DEFUN([TRAVOLTA],
11750 [test "$body_temperature_in_celsius" -gt "38" &&
11751   dance_floor=occupied])
11752 AC_DEFUN([NEWTON_JOHN],
11753 [test "$hair_style" = "curly" &&
11754   dance_floor=occupied])
11755 @end group
11757 @group
11758 AC_DEFUN([RESERVE_DANCE_FLOOR],
11759 [if date | grep '^Sat.*pm' >/dev/null 2>&1; then
11760   AC_REQUIRE([TRAVOLTA])
11761   AC_REQUIRE([NEWTON_JOHN])
11762 fi])
11763 @end group
11764 @end example
11766 @noindent
11767 with this @file{configure.ac}
11769 @example
11770 AC_INIT([Dance Manager], [1.0], [bug-dance@@example.org])
11771 RESERVE_DANCE_FLOOR
11772 if test "$dance_floor" = occupied; then
11773   AC_MSG_ERROR([cannot pick up here, let's move])
11775 @end example
11777 @noindent
11778 does not leave you with a better chance to meet a kindred soul at
11779 other times than Saturday night since it expands into:
11781 @example
11782 @group
11783 test "$body_temperature_in_Celsius" -gt "38" &&
11784   dance_floor=occupied
11785 test "$hair_style" = "curly" &&
11786   dance_floor=occupied
11788 if date | grep '^Sat.*pm' >/dev/null 2>&1; then
11792 @end group
11793 @end example
11795 This behavior was chosen on purpose: (i) it prevents messages in
11796 required macros from interrupting the messages in the requiring macros;
11797 (ii) it avoids bad surprises when shell conditionals are used, as in:
11799 @example
11800 @group
11801 if @dots{}; then
11802   AC_REQUIRE([SOME_CHECK])
11804 @dots{}
11805 SOME_CHECK
11806 @end group
11807 @end example
11809 The helper macros @code{AS_IF} and @code{AS_CASE} may be used to
11810 enforce expansion of required macros outside of shell conditional
11811 constructs.  You are furthermore encouraged to put all @code{AC_REQUIRE} calls
11812 at the beginning of a macro.  You can use @code{dnl} to avoid the empty
11813 lines they leave.
11815 @node Suggested Ordering
11816 @subsection Suggested Ordering
11817 @cindex Macros, ordering
11818 @cindex Ordering macros
11820 Some macros should be run before another macro if both are called, but
11821 neither @emph{requires} that the other be called.  For example, a macro
11822 that changes the behavior of the C compiler should be called before any
11823 macros that run the C compiler.  Many of these dependencies are noted in
11824 the documentation.
11826 Autoconf provides the @code{AC_BEFORE} macro to warn users when macros
11827 with this kind of dependency appear out of order in a
11828 @file{configure.ac} file.  The warning occurs when creating
11829 @command{configure} from @file{configure.ac}, not when running
11830 @command{configure}.
11832 For example, @code{AC_PROG_CPP} checks whether the C compiler
11833 can run the C preprocessor when given the @option{-E} option.  It should
11834 therefore be called after any macros that change which C compiler is
11835 being used, such as @code{AC_PROG_CC}.  So @code{AC_PROG_CC} contains:
11837 @example
11838 AC_BEFORE([$0], [AC_PROG_CPP])dnl
11839 @end example
11841 @noindent
11842 This warns the user if a call to @code{AC_PROG_CPP} has already occurred
11843 when @code{AC_PROG_CC} is called.
11845 @defmac AC_BEFORE (@var{this-macro-name}, @var{called-macro-name})
11846 @acindex{BEFORE}
11847 Make M4 print a warning message to the standard error output if
11848 @var{called-macro-name} has already been called.  @var{this-macro-name}
11849 should be the name of the macro that is calling @code{AC_BEFORE}.  The
11850 macro @var{called-macro-name} must have been defined using
11851 @code{AC_DEFUN} or else contain a call to @code{AC_PROVIDE} to indicate
11852 that it has been called.
11853 @end defmac
11855 @node One-Shot Macros
11856 @subsection One-Shot Macros
11857 @cindex One-shot macros
11858 @cindex Macros, called once
11860 Some macros should be called only once, either because calling them
11861 multiple time is unsafe, or because it is bad style.  For instance
11862 Autoconf ensures that @code{AC_CANONICAL_BUILD} and cousins
11863 (@pxref{Canonicalizing}) are evaluated only once, because it makes no
11864 sense to run these expensive checks more than once.  Such one-shot
11865 macros can be defined using @code{AC_DEFUN_ONCE}.
11867 @defmac AC_DEFUN_ONCE (@var{macro-name}, @var{macro-body})
11868 @acindex{DEFUN_ONCE}
11870 Declare macro @var{macro-name} like @code{AC_DEFUN} would (@pxref{Macro
11871 Definitions}), and emit a warning any time the macro is called more than
11872 once.
11873 @end defmac
11875 Obviously it is not sensible to evaluate a macro defined by
11876 @code{AC_DEFUN_ONCE} in a macro defined by @code{AC_DEFUN}.
11877 Most of the time you want to use @code{AC_REQUIRE} (@pxref{Prerequisite
11878 Macros}).
11880 @node Obsoleting Macros
11881 @section Obsoleting Macros
11882 @cindex Obsoleting macros
11883 @cindex Macros, obsoleting
11885 Configuration and portability technology has evolved over the years.
11886 Often better ways of solving a particular problem are developed, or
11887 ad-hoc approaches are systematized.  This process has occurred in many
11888 parts of Autoconf.  One result is that some of the macros are now
11889 considered @dfn{obsolete}; they still work, but are no longer considered
11890 the best thing to do, hence they should be replaced with more modern
11891 macros.  Ideally, @command{autoupdate} should replace the old macro calls
11892 with their modern implementation.
11894 Autoconf provides a simple means to obsolete a macro.
11896 @anchor{AU_DEFUN}
11897 @defmac AU_DEFUN (@var{old-macro}, @var{implementation}, @ovar{message})
11898 @auindex{DEFUN}
11899 Define @var{old-macro} as @var{implementation}.  The only difference
11900 with @code{AC_DEFUN} is that the user is warned that
11901 @var{old-macro} is now obsolete.
11903 If she then uses @command{autoupdate}, the call to @var{old-macro} is
11904 replaced by the modern @var{implementation}.  @var{message} should
11905 include information on what to do after running @command{autoupdate};
11906 @command{autoupdate} prints it as a warning, and includes it
11907 in the updated @file{configure.ac} file.
11909 The details of this macro are hairy: if @command{autoconf} encounters an
11910 @code{AU_DEFUN}ed macro, all macros inside its second argument are expanded
11911 as usual.  However, when @command{autoupdate} is run, only M4 and M4sugar
11912 macros are expanded here, while all other macros are disabled and
11913 appear literally in the updated @file{configure.ac}.
11914 @end defmac
11916 @defmac AU_ALIAS (@var{old-name}, @var{new-name})
11917 @auindex{ALIAS}
11918 Used if the @var{old-name} is to be replaced by a call to @var{new-macro}
11919 with the same parameters.  This happens for example if the macro was renamed.
11920 @end defmac
11922 @node Coding Style
11923 @section Coding Style
11924 @cindex Coding style
11926 The Autoconf macros follow a strict coding style.  You are encouraged to
11927 follow this style, especially if you intend to distribute your macro,
11928 either by contributing it to Autoconf itself, or via other means.
11930 The first requirement is to pay great attention to the quotation.  For
11931 more details, see @ref{Autoconf Language}, and @ref{M4 Quotation}.
11933 Do not try to invent new interfaces.  It is likely that there is a macro
11934 in Autoconf that resembles the macro you are defining: try to stick to
11935 this existing interface (order of arguments, default values, etc.).  We
11936 @emph{are} conscious that some of these interfaces are not perfect;
11937 nevertheless, when harmless, homogeneity should be preferred over
11938 creativity.
11940 Be careful about clashes both between M4 symbols and between shell
11941 variables.
11943 If you stick to the suggested M4 naming scheme (@pxref{Macro Names}),
11944 you are unlikely to generate conflicts.  Nevertheless, when you need to
11945 set a special value, @emph{avoid using a regular macro name}; rather,
11946 use an ``impossible'' name.  For instance, up to version 2.13, the macro
11947 @code{AC_SUBST} used to remember what @var{symbol} macros were already defined
11948 by setting @code{AC_SUBST_@var{symbol}}, which is a regular macro name.
11949 But since there is a macro named @code{AC_SUBST_FILE}, it was just
11950 impossible to @samp{AC_SUBST(FILE)}!  In this case,
11951 @code{AC_SUBST(@var{symbol})} or @code{_AC_SUBST(@var{symbol})} should
11952 have been used (yes, with the parentheses).
11953 @c or better yet, high-level macros such as @code{m4_expand_once}
11955 No Autoconf macro should ever enter the user-variable name space; i.e.,
11956 except for the variables that are the actual result of running the
11957 macro, all shell variables should start with @code{ac_}.  In
11958 addition, small macros or any macro that is likely to be embedded in
11959 other macros should be careful not to use obvious names.
11961 @cindex @code{dnl}
11962 Do not use @code{dnl} to introduce comments: most of the comments you
11963 are likely to write are either header comments which are not output
11964 anyway, or comments that should make their way into @file{configure}.
11965 There are exceptional cases where you do want to comment special M4
11966 constructs, in which case @code{dnl} is right, but keep in mind that it
11967 is unlikely.
11969 M4 ignores the leading blanks and newlines before each argument.
11970 Use this feature to
11971 indent in such a way that arguments are (more or less) aligned with the
11972 opening parenthesis of the macro being called.  For instance, instead of
11974 @example
11975 AC_CACHE_CHECK(for EMX OS/2 environment,
11976 ac_cv_emxos2,
11977 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [return __EMX__;])],
11978 [ac_cv_emxos2=yes], [ac_cv_emxos2=no])])
11979 @end example
11981 @noindent
11982 write
11984 @example
11985 AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
11986 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
11987                    [ac_cv_emxos2=yes],
11988                    [ac_cv_emxos2=no])])
11989 @end example
11991 @noindent
11992 or even
11994 @example
11995 AC_CACHE_CHECK([for EMX OS/2 environment],
11996                [ac_cv_emxos2],
11997                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
11998                                                    [return __EMX__;])],
11999                                   [ac_cv_emxos2=yes],
12000                                   [ac_cv_emxos2=no])])
12001 @end example
12003 When using @code{AC_RUN_IFELSE} or any macro that cannot work when
12004 cross-compiling, provide a pessimistic value (typically @samp{no}).
12006 Feel free to use various tricks to prevent auxiliary tools, such as
12007 syntax-highlighting editors, from behaving improperly.  For instance,
12008 instead of:
12010 @example
12011 m4_bpatsubst([$1], [$"])
12012 @end example
12014 @noindent
12017 @example
12018 m4_bpatsubst([$1], [$""])
12019 @end example
12021 @noindent
12022 so that Emacsen do not open an endless ``string'' at the first quote.
12023 For the same reasons, avoid:
12025 @example
12026 test $[#] != 0
12027 @end example
12029 @noindent
12030 and use:
12032 @example
12033 test $[@@%:@@] != 0
12034 @end example
12036 @noindent
12037 Otherwise, the closing bracket would be hidden inside a @samp{#}-comment,
12038 breaking the bracket-matching highlighting from Emacsen.  Note the
12039 preferred style to escape from M4: @samp{$[1]}, @samp{$[@@]}, etc.  Do
12040 not escape when it is unnecessary.  Common examples of useless quotation
12041 are @samp{[$]$1} (write @samp{$$1}), @samp{[$]var} (use @samp{$var}),
12042 etc.  If you add portability issues to the picture, you'll prefer
12043 @samp{$@{1+"$[@@]"@}} to @samp{"[$]@@"}, and you'll prefer do something
12044 better than hacking Autoconf @code{:-)}.
12046 When using @command{sed}, don't use @option{-e} except for indenting
12047 purposes.  With the @code{s} and @code{y} commands, the preferred
12048 separator is @samp{/} unless @samp{/} itself might appear in the pattern
12049 or replacement, in which case you should use @samp{|}, or optionally
12050 @samp{,} if you know the pattern and replacement cannot contain a file
12051 name.  If none of these characters will do, choose a printable character
12052 that cannot appear in the pattern or replacement.  Characters from the
12053 set @samp{"#$&'()*;<=>?`|~} are good choices if the pattern or
12054 replacement might contain a file name, since they have special meaning
12055 to the shell and are less likely to occur in file names.
12057 @xref{Macro Definitions}, for details on how to define a macro.  If a
12058 macro doesn't use @code{AC_REQUIRE}, is expected to never be the object
12059 of an @code{AC_REQUIRE} directive, and macros required by other macros
12060 inside arguments do not need to be expanded before this macro, then
12061 use @code{m4_define}.  In case of doubt, use @code{AC_DEFUN}.
12062 All the @code{AC_REQUIRE} statements should be at the beginning of the
12063 macro, and each statement should be followed by @code{dnl}.
12065 You should not rely on the number of arguments: instead of checking
12066 whether an argument is missing, test that it is not empty.  It provides
12067 both a simpler and a more predictable interface to the user, and saves
12068 room for further arguments.
12070 Unless the macro is short, try to leave the closing @samp{])} at the
12071 beginning of a line, followed by a comment that repeats the name of the
12072 macro being defined.  This introduces an additional newline in
12073 @command{configure}; normally, that is not a problem, but if you want to
12074 remove it you can use @samp{[]dnl} on the last line.  You can similarly
12075 use @samp{[]dnl} after a macro call to remove its newline.  @samp{[]dnl}
12076 is recommended instead of @samp{dnl} to ensure that M4 does not
12077 interpret the @samp{dnl} as being attached to the preceding text or
12078 macro output.  For example, instead of:
12080 @example
12081 AC_DEFUN([AC_PATH_X],
12082 [AC_MSG_CHECKING([for X])
12083 AC_REQUIRE_CPP()
12084 @r{# @dots{}omitted@dots{}}
12085   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
12086 fi])
12087 @end example
12089 @noindent
12090 you would write:
12092 @example
12093 AC_DEFUN([AC_PATH_X],
12094 [AC_REQUIRE_CPP()[]dnl
12095 AC_MSG_CHECKING([for X])
12096 @r{# @dots{}omitted@dots{}}
12097   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
12098 fi[]dnl
12099 ])# AC_PATH_X
12100 @end example
12102 If the macro is long, try to split it into logical chunks.  Typically,
12103 macros that check for a bug in a function and prepare its
12104 @code{AC_LIBOBJ} replacement should have an auxiliary macro to perform
12105 this setup.  Do not hesitate to introduce auxiliary macros to factor
12106 your code.
12108 In order to highlight the recommended coding style, here is a macro
12109 written the old way:
12111 @example
12112 dnl Check for EMX on OS/2.
12113 dnl _AC_EMXOS2
12114 AC_DEFUN(_AC_EMXOS2,
12115 [AC_CACHE_CHECK(for EMX OS/2 environment, ac_cv_emxos2,
12116 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, return __EMX__;)],
12117 ac_cv_emxos2=yes, ac_cv_emxos2=no)])
12118 test "$ac_cv_emxos2" = yes && EMXOS2=yes])
12119 @end example
12121 @noindent
12122 and the new way:
12124 @example
12125 # _AC_EMXOS2
12126 # ----------
12127 # Check for EMX on OS/2.
12128 m4_define([_AC_EMXOS2],
12129 [AC_CACHE_CHECK([for EMX OS/2 environment], [ac_cv_emxos2],
12130 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return __EMX__;])],
12131                    [ac_cv_emxos2=yes],
12132                    [ac_cv_emxos2=no])])
12133 test "$ac_cv_emxos2" = yes && EMXOS2=yes[]dnl
12134 ])# _AC_EMXOS2
12135 @end example
12140 @c ============================================= Portable Shell Programming
12142 @node Portable Shell
12143 @chapter Portable Shell Programming
12144 @cindex Portable shell programming
12146 When writing your own checks, there are some shell-script programming
12147 techniques you should avoid in order to make your code portable.  The
12148 Bourne shell and upward-compatible shells like the Korn shell and Bash
12149 have evolved over the years, but to prevent trouble, do not take
12150 advantage of features that were added after Unix version 7, circa
12151 1977 (@pxref{Systemology}).
12153 You should not use aliases, negated character classes, or other features
12154 that are not found in all Bourne-compatible shells; restrict yourself
12155 to the lowest common denominator.  Even @code{unset} is not supported
12156 by all shells!
12158 Shell functions are considered portable nowadays, though Autoconf still
12159 does not use them (Autotest does).  However, inside a shell function,
12160 you should not rely on the error status of a subshell if the last
12161 command of that subshell was @code{exit} or @code{trap}, as this
12162 triggers bugs in zsh 4.x; while Autoconf tries to find a shell that
12163 does not exhibit the bug, zsh might be the only shell present on the
12164 user's machine.
12166 Some ancient systems have quite
12167 small limits on the length of the @samp{#!} line; for instance, 32
12168 bytes (not including the newline) on SunOS 4.
12169 A few ancient 4.2@acronym{BSD} based systems (such as Dynix circa 1984)
12170 required a single space between the @samp{#!} and the @samp{/}.
12171 However, these ancient systems are no longer of practical concern.
12173 The set of external programs you should run in a @command{configure} script
12174 is fairly small.  @xref{Utilities in Makefiles, , Utilities in
12175 Makefiles, standards, @acronym{GNU} Coding Standards}, for the list.  This
12176 restriction allows users to start out with a fairly small set of
12177 programs and build the rest, avoiding too many interdependencies between
12178 packages.
12180 Some of these external utilities have a portable subset of features; see
12181 @ref{Limitations of Usual Tools}.
12183 There are other sources of documentation about shells.  The
12184 specification for the Posix
12185 @uref{http://www.opengroup.org/@/susv3/@/utilities/@/xcu_chap02.html, Shell
12186 Command Language}, though more generous than the restrictive shell
12187 subset described above, is fairly portable nowadays.  Also please see
12188 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/, the Shell FAQs}.
12190 @menu
12191 * Shellology::                  A zoology of shells
12192 * Here-Documents::              Quirks and tricks
12193 * File Descriptors::            FDs and redirections
12194 * File System Conventions::     File names
12195 * Shell Pattern Matching::      Pattern matching
12196 * Shell Substitutions::         Variable and command expansions
12197 * Assignments::                 Varying side effects of assignments
12198 * Parentheses::                 Parentheses in shell scripts
12199 * Slashes::                     Slashes in shell scripts
12200 * Special Shell Variables::     Variables you should not change
12201 * Limitations of Builtins::     Portable use of not so portable /bin/sh
12202 * Limitations of Usual Tools::  Portable use of portable tools
12203 @end menu
12205 @node Shellology
12206 @section Shellology
12207 @cindex Shellology
12209 There are several families of shells, most prominently the Bourne family
12210 and the C shell family which are deeply incompatible.  If you want to
12211 write portable shell scripts, avoid members of the C shell family.  The
12212 @uref{http://www.faqs.org/@/faqs/@/unix-faq/@/shell/@/shell-differences/, the
12213 Shell difference FAQ} includes a small history of Posix shells, and a
12214 comparison between several of them.
12216 Below we describe some of the members of the Bourne shell family.
12218 @table @asis
12219 @item Ash
12220 @cindex Ash
12221 Ash is often used on @acronym{GNU}/Linux and @acronym{BSD}
12222 systems as a light-weight Bourne-compatible shell.  Ash 0.2 has some
12223 bugs that are fixed in the 0.3.x series, but portable shell scripts
12224 should work around them, since version 0.2 is still shipped with many
12225 @acronym{GNU}/Linux distributions.
12227 To be compatible with Ash 0.2:
12229 @itemize @minus
12230 @item
12231 don't use @samp{$?} after expanding empty or unset variables,
12232 or at the start of an @command{eval}:
12234 @example
12235 foo=
12236 false
12237 $foo
12238 echo "Do not use it: $?"
12239 false
12240 eval 'echo "Do not use it: $?"'
12241 @end example
12243 @item
12244 don't use command substitution within variable expansion:
12246 @example
12247 cat $@{FOO=`bar`@}
12248 @end example
12250 @item
12251 beware that single builtin substitutions are not performed by a
12252 subshell, hence their effect applies to the current shell!  @xref{Shell
12253 Substitutions}, item ``Command Substitution''.
12254 @end itemize
12256 @item Bash
12257 @cindex Bash
12258 To detect whether you are running Bash, test whether
12259 @code{BASH_VERSION} is set.  To require
12260 Posix compatibility, run @samp{set -o posix}.  @xref{Bash POSIX
12261 Mode, , Bash Posix Mode, bash, The @acronym{GNU} Bash Reference
12262 Manual}, for details.
12264 @item Bash 2.05 and later
12265 @cindex Bash 2.05 and later
12266 Versions 2.05 and later of Bash use a different format for the
12267 output of the @command{set} builtin, designed to make evaluating its
12268 output easier.  However, this output is not compatible with earlier
12269 versions of Bash (or with many other shells, probably).  So if
12270 you use Bash 2.05 or higher to execute @command{configure},
12271 you'll need to use Bash 2.05 for all other build tasks as well.
12273 @item Ksh
12274 @cindex Ksh
12275 @cindex Korn shell
12276 @prindex @samp{ksh}
12277 @prindex @samp{ksh88}
12278 @prindex @samp{ksh93}
12279 The Korn shell is compatible with the Bourne family and it mostly
12280 conforms to Posix.  It has two major variants commonly
12281 called @samp{ksh88} and @samp{ksh93}, named after the years of initial
12282 release.  It is usually called @command{ksh}, but is called @command{sh}
12283 on some hosts if you set your path appropriately.
12285 Solaris systems have three variants:
12286 @prindex @command{/usr/bin/ksh} on Solaris
12287 @command{/usr/bin/ksh} is @samp{ksh88}; it is
12288 standard on Solaris 2.0 and later.
12289 @prindex @command{/usr/xpg4/bin/sh} on Solaris
12290 @command{/usr/xpg4/bin/sh} is a Posix-compliant variant of
12291 @samp{ksh88}; it is standard on Solaris 9 and later.
12292 @prindex @command{/usr/dt/bin/dtksh} on Solaris
12293 @command{/usr/dt/bin/dtksh} is @samp{ksh93}.
12294 Variants that are not standard may be parts of optional
12295 packages.  There is no extra charge for these packages, but they are
12296 not part of a minimal OS install and therefore some installations may
12297 not have it.
12299 Starting with Tru64 Version 4.0, the Korn shell @command{/usr/bin/ksh}
12300 is also available as @command{/usr/bin/posix/sh}.  If the environment
12301 variable @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
12302 the standard shell conform to Posix.
12304 @item Pdksh
12305 @prindex @samp{pdksh}
12306 A public-domain clone of the Korn shell called @command{pdksh} is widely
12307 available: it has most of the @samp{ksh88} features along with a few of
12308 its own.  It usually sets @code{KSH_VERSION}, except if invoked as
12309 @command{/bin/sh} on Open@acronym{BSD}, and similarly to Bash you can require
12310 Posix compatibility by running @samp{set -o posix}.  Unfortunately, with
12311 @command{pdksh} 5.2.14 (the latest stable version as of January 2007)
12312 Posix mode is buggy and causes @command{pdksh} to depart from Posix in
12313 at least one respect:
12315 @example
12316 $ @kbd{echo "`echo \"hello\"`"}
12317 hello
12318 $ @kbd{set -o posix}
12319 $ @kbd{echo "`echo \"hello\"`"}
12320 "hello"
12321 @end example
12323 The last line of output contains spurious quotes.  This is yet another
12324 reason why portable shell code should not contain
12325 @code{"`@dots{}\"@dots{}\"@dots{}`"} constructs (@pxref{Shell
12326 Substitutions}).
12328 @item Zsh
12329 @cindex Zsh
12330 To detect whether you are running @command{zsh}, test whether
12331 @code{ZSH_VERSION} is set.  By default @command{zsh} is @emph{not}
12332 compatible with the Bourne shell: you must execute @samp{emulate sh},
12333 and for @command{zsh} versions before 3.1.6-dev-18 you must also
12334 set @code{NULLCMD} to @samp{:}.  @xref{Compatibility, , Compatibility,
12335 zsh, The Z Shell Manual}, for details.
12337 The default Mac OS X @command{sh} was originally Zsh; it was changed to
12338 Bash in Mac OS X 10.2.
12339 @end table
12341 The following discussion between Russ Allbery and Robert Lipe is worth
12342 reading:
12344 @noindent
12345 Russ Allbery:
12347 @quotation
12348 The @acronym{GNU} assumption that @command{/bin/sh} is the one and only shell
12349 leads to a permanent deadlock.  Vendors don't want to break users'
12350 existing shell scripts, and there are some corner cases in the Bourne
12351 shell that are not completely compatible with a Posix shell.  Thus,
12352 vendors who have taken this route will @emph{never} (OK@dots{}``never say
12353 never'') replace the Bourne shell (as @command{/bin/sh}) with a
12354 Posix shell.
12355 @end quotation
12357 @noindent
12358 Robert Lipe:
12360 @quotation
12361 This is exactly the problem.  While most (at least most System V's) do
12362 have a Bourne shell that accepts shell functions most vendor
12363 @command{/bin/sh} programs are not the Posix shell.
12365 So while most modern systems do have a shell @emph{somewhere} that meets the
12366 Posix standard, the challenge is to find it.
12367 @end quotation
12369 @node Here-Documents
12370 @section Here-Documents
12371 @cindex Here-documents
12372 @cindex Shell here-documents
12374 Don't rely on @samp{\} being preserved just because it has no special
12375 meaning together with the next symbol.  In the native @command{sh}
12376 on Open@acronym{BSD} 2.7 @samp{\"} expands to @samp{"} in here-documents with
12377 unquoted delimiter.  As a general rule, if @samp{\\} expands to @samp{\}
12378 use @samp{\\} to get @samp{\}.
12380 With Open@acronym{BSD} 2.7's @command{sh}
12382 @example
12383 @group
12384 $ @kbd{cat <<EOF
12385 > \" \\
12386 > EOF}
12387 " \
12388 @end group
12389 @end example
12391 @noindent
12392 and with Bash:
12394 @example
12395 @group
12396 bash-2.04$ @kbd{cat <<EOF
12397 > \" \\
12398 > EOF}
12399 \" \
12400 @end group
12401 @end example
12403 Some shells mishandle large here-documents: for example,
12404 Solaris 10 @command{dtksh} and the UnixWare 7.1.1 Posix shell, which are
12405 derived from Korn shell version M-12/28/93d, mishandle braced variable
12406 expansion that crosses a 1024- or 4096-byte buffer boundary
12407 within a here-document.  Only the part of the variable name after the boundary
12408 is used.  For example, @code{$@{variable@}} could be replaced by the expansion
12409 of @code{$@{ble@}}.  If the end of the variable name is aligned with the block
12410 boundary, the shell reports an error, as if you used @code{$@{@}}.
12411 Instead of @code{$@{variable-default@}}, the shell may expand
12412 @code{$@{riable-default@}}, or even @code{$@{fault@}}.  This bug can often
12413 be worked around by omitting the braces: @code{$variable}.  The bug was
12414 fixed in
12415 @samp{ksh93g} (1998-04-30) but as of 2006 many operating systems were
12416 still shipping older versions with the bug.
12418 Many shells (including the Bourne shell) implement here-documents
12419 inefficiently.  In particular, some shells can be extremely inefficient when
12420 a single statement contains many here-documents.  For instance if your
12421 @file{configure.ac} includes something like:
12423 @example
12424 @group
12425 if <cross_compiling>; then
12426   assume this and that
12427 else
12428   check this
12429   check that
12430   check something else
12431   @dots{}
12432   on and on forever
12433   @dots{}
12435 @end group
12436 @end example
12438 A shell parses the whole @code{if}/@code{fi} construct, creating
12439 temporary files for each here-document in it.  Some shells create links
12440 for such here-documents on every @code{fork}, so that the clean-up code
12441 they had installed correctly removes them.  It is creating the links
12442 that can take the shell forever.
12444 Moving the tests out of the @code{if}/@code{fi}, or creating multiple
12445 @code{if}/@code{fi} constructs, would improve the performance
12446 significantly.  Anyway, this kind of construct is not exactly the
12447 typical use of Autoconf.  In fact, it's even not recommended, because M4
12448 macros can't look into shell conditionals, so we may fail to expand a
12449 macro when it was expanded before in a conditional path, and the
12450 condition turned out to be false at runtime, and we end up not
12451 executing the macro at all.
12453 @node File Descriptors
12454 @section File Descriptors
12455 @cindex Descriptors
12456 @cindex File descriptors
12457 @cindex Shell file descriptors
12459 Most shells, if not all (including Bash, Zsh, Ash), output traces on
12460 stderr, even for subshells.  This might result in undesirable content
12461 if you meant to capture the standard-error output of the inner command:
12463 @example
12464 $ @kbd{ash -x -c '(eval "echo foo >&2") 2>stderr'}
12465 $ @kbd{cat stderr}
12466 + eval echo foo >&2
12467 + echo foo
12469 $ @kbd{bash -x -c '(eval "echo foo >&2") 2>stderr'}
12470 $ @kbd{cat stderr}
12471 + eval 'echo foo >&2'
12472 ++ echo foo
12474 $ @kbd{zsh -x -c '(eval "echo foo >&2") 2>stderr'}
12475 @i{# Traces on startup files deleted here.}
12476 $ @kbd{cat stderr}
12477 +zsh:1> eval echo foo >&2
12478 +zsh:1> echo foo
12480 @end example
12482 @noindent
12483 One workaround is to grep out uninteresting lines, hoping not to remove
12484 good ones.
12486 If you intend to redirect both standard error and standard output,
12487 redirect standard output first.  This works better with @acronym{HP-UX},
12488 since its shell mishandles tracing if standard error is redirected
12489 first:
12491 @example
12492 $ @kbd{sh -x -c ': 2>err >out'}
12493 + :
12494 + 2> err $ @kbd{cat err}
12495 1> out
12496 @end example
12498 Don't try to redirect the standard error of a command substitution.  It
12499 must be done @emph{inside} the command substitution.  When running
12500 @samp{: `cd /zorglub` 2>/dev/null} expect the error message to
12501 escape, while @samp{: `cd /zorglub 2>/dev/null`} works properly.
12503 It is worth noting that Zsh (but not Ash nor Bash) makes it possible
12504 in assignments though: @samp{foo=`cd /zorglub` 2>/dev/null}.
12506 When catering to old systems, don't redirect the same file descriptor
12507 several times, as you are doomed to failure under Ultrix.
12509 @example
12510 ULTRIX V4.4 (Rev. 69) System #31: Thu Aug 10 19:42:23 GMT 1995
12511 UWS V4.4 (Rev. 11)
12512 $ @kbd{eval 'echo matter >fullness' >void}
12513 illegal io
12514 $ @kbd{eval '(echo matter >fullness)' >void}
12515 illegal io
12516 $ @kbd{(eval '(echo matter >fullness)') >void}
12517 Ambiguous output redirect.
12518 @end example
12520 @noindent
12521 In each case the expected result is of course @file{fullness} containing
12522 @samp{matter} and @file{void} being empty.  However, this bug is
12523 probably not of practical concern to modern platforms.
12525 Don't rely on file descriptors 0, 1, and 2 remaining closed in a
12526 subsidiary program.  If any of these descriptors is closed, the
12527 operating system may open an unspecified file for the descriptor in the
12528 new process image.  Posix says this may be done only if the subsidiary
12529 program is set-user-ID or set-group-ID, but @acronym{HP-UX} 11.23 does
12530 it even for ordinary programs.
12532 Don't rely on open file descriptors being open in child processes.  In
12533 @command{ksh}, file descriptors above 2 which are opened using
12534 @samp{exec @var{n}>file} are closed by a subsequent @samp{exec} (such as
12535 that involved in the fork-and-exec which runs a program or script).
12536 Thus, using @command{sh}, we have:
12538 @example
12539 $ @kbd{cat ./descrips}
12540 #!/bin/sh -
12541 echo hello >&5
12542 $ @kbd{exec 5>t}
12543 $ @kbd{./descrips}
12544 $ @kbd{cat t}
12545 hello
12547 @end example
12549 @noindent
12550 But using ksh:
12552 @example
12553 $ @kbd{exec 5>t}
12554 $ @kbd{./descrips}
12555 hello
12556 $ @kbd{cat t}
12558 @end example
12560 @noindent
12561 Within the process which runs the @samp{descrips} script, file
12562 descriptor 5 is closed.
12564 @acronym{DOS} variants cannot rename or remove open files, such as in
12565 @samp{mv foo bar >foo} or @samp{rm foo >foo}, even though this is
12566 perfectly portable among Posix hosts.
12568 A few ancient systems reserved some file descriptors.  By convention,
12569 file descriptor 3 was opened to @file{/dev/tty} when you logged into
12570 Eighth Edition (1985) through Tenth Edition Unix (1989).  File
12571 descriptor 4 had a special use on the Stardent/Kubota Titan (circa
12572 1990), though we don't now remember what it was.  Both these systems are
12573 obsolete, so it's now safe to treat file descriptors 3 and 4 like any
12574 other file descriptors.
12576 @node File System Conventions
12577 @section File System Conventions
12578 @cindex File system conventions
12580 Autoconf uses shell-script processing extensively, so the file names
12581 that it processes should not contain characters that are special to the
12582 shell.  Special characters include space, tab, newline, @sc{nul}, and
12583 the following:
12585 @example
12586 " # $ & ' ( ) * ; < = > ? [ \ ` |
12587 @end example
12589 Also, file names should not begin with @samp{~} or @samp{-}, and should
12590 contain neither @samp{-} immediately after @samp{/} nor @samp{~}
12591 immediately after @samp{:}.  On Posix-like platforms, directory names
12592 should not contain @samp{:}, as this runs afoul of @samp{:} used as the
12593 path separator.
12595 These restrictions apply not only to the files that you distribute, but
12596 also to the absolute file names of your source, build, and destination
12597 directories.
12599 On some Posix-like platforms, @samp{!} and @samp{^} are special too, so
12600 they should be avoided.
12602 Posix lets implementations treat leading @file{//} specially, but
12603 requires leading @file{///} and beyond to be equivalent to @file{/}.
12604 Most Unix variants treat @file{//} like @file{/}.  However, some treat
12605 @file{//} as a ``super-root'' that can provide access to files that are
12606 not otherwise reachable from @file{/}.  The super-root tradition began
12607 with Apollo Domain/OS, which died out long ago, but unfortunately Cygwin
12608 has revived it.
12610 While @command{autoconf} and friends are usually run on some Posix
12611 variety, they can be used on other systems, most notably @acronym{DOS}
12612 variants.  This impacts several assumptions regarding file names.
12614 @noindent
12615 For example, the following code:
12617 @example
12618 case $foo_dir in
12619   /*) # Absolute
12620      ;;
12621   *)
12622      foo_dir=$dots$foo_dir ;;
12623 esac
12624 @end example
12626 @noindent
12627 fails to properly detect absolute file names on those systems, because
12628 they can use a drivespec, and usually use a backslash as directory
12629 separator.  If you want to be portable to @acronym{DOS} variants (at the
12630 price of rejecting valid but oddball Posix file names like @file{a:\b}),
12631 you can check for absolute file names like this:
12633 @cindex absolute file names, detect
12634 @example
12635 case $foo_dir in
12636   [\\/]* | ?:[\\/]* ) # Absolute
12637      ;;
12638   *)
12639      foo_dir=$dots$foo_dir ;;
12640 esac
12641 @end example
12643 @noindent
12644 Make sure you quote the brackets if appropriate and keep the backslash as
12645 first character (@pxref{Limitations of Builtins}).
12647 Also, because the colon is used as part of a drivespec, these systems don't
12648 use it as path separator.  When creating or accessing paths, you can use the
12649 @code{PATH_SEPARATOR} output variable instead.  @command{configure} sets this
12650 to the appropriate value for the build system (@samp{:} or @samp{;}) when it
12651 starts up.
12653 File names need extra care as well.  While @acronym{DOS} variants
12654 that are Posixy enough to run @command{autoconf} (such as @acronym{DJGPP})
12655 are usually able to handle long file names properly, there are still
12656 limitations that can seriously break packages.  Several of these issues
12657 can be easily detected by the
12658 @uref{ftp://ftp.gnu.org/gnu/non-gnu/doschk/doschk-1.1.tar.gz, doschk}
12659 package.
12661 A short overview follows; problems are marked with @sc{sfn}/@sc{lfn} to
12662 indicate where they apply: @sc{sfn} means the issues are only relevant to
12663 plain @acronym{DOS}, not to @acronym{DOS} under Microsoft Windows
12664 variants, while @sc{lfn} identifies problems that exist even under
12665 Microsoft Windows variants.
12667 @table @asis
12668 @item No multiple dots (@sc{sfn})
12669 @acronym{DOS} cannot handle multiple dots in file names.  This is an especially
12670 important thing to remember when building a portable configure script,
12671 as @command{autoconf} uses a .in suffix for template files.
12673 This is perfectly OK on Posix variants:
12675 @example
12676 AC_CONFIG_HEADERS([config.h])
12677 AC_CONFIG_FILES([source.c foo.bar])
12678 AC_OUTPUT
12679 @end example
12681 @noindent
12682 but it causes problems on @acronym{DOS}, as it requires @samp{config.h.in},
12683 @samp{source.c.in} and @samp{foo.bar.in}.  To make your package more portable
12684 to @acronym{DOS}-based environments, you should use this instead:
12686 @example
12687 AC_CONFIG_HEADERS([config.h:config.hin])
12688 AC_CONFIG_FILES([source.c:source.cin foo.bar:foobar.in])
12689 AC_OUTPUT
12690 @end example
12692 @item No leading dot (@sc{sfn})
12693 @acronym{DOS} cannot handle file names that start with a dot.  This is usually
12694 not important for @command{autoconf}.
12696 @item Case insensitivity (@sc{lfn})
12697 @acronym{DOS} is case insensitive, so you cannot, for example, have both a
12698 file called @samp{INSTALL} and a directory called @samp{install}.  This
12699 also affects @command{make}; if there's a file called @samp{INSTALL} in
12700 the directory, @samp{make install} does nothing (unless the
12701 @samp{install} target is marked as PHONY).
12703 @item The 8+3 limit (@sc{sfn})
12704 Because the @acronym{DOS} file system only stores the first 8 characters of
12705 the file name and the first 3 of the extension, those must be unique.
12706 That means that @file{foobar-part1.c}, @file{foobar-part2.c} and
12707 @file{foobar-prettybird.c} all resolve to the same file name
12708 (@file{FOOBAR-P.C}).  The same goes for @file{foo.bar} and
12709 @file{foo.bartender}.
12711 The 8+3 limit is not usually a problem under Microsoft Windows, as it
12712 uses numeric
12713 tails in the short version of file names to make them unique.  However, a
12714 registry setting can turn this behavior off.  While this makes it
12715 possible to share file trees containing long file names between @sc{sfn}
12716 and @sc{lfn} environments, it also means the above problem applies there
12717 as well.
12719 @item Invalid characters (@sc{lfn})
12720 Some characters are invalid in @acronym{DOS} file names, and should therefore
12721 be avoided.  In a @sc{lfn} environment, these are @samp{/}, @samp{\},
12722 @samp{?}, @samp{*}, @samp{:}, @samp{<}, @samp{>}, @samp{|} and @samp{"}.
12723 In a @sc{sfn} environment, other characters are also invalid.  These
12724 include @samp{+}, @samp{,}, @samp{[} and @samp{]}.
12726 @item Invalid names (@sc{lfn})
12727 Some @acronym{DOS} file names are reserved, and cause problems if you
12728 try to use files with those names.  These names include @file{CON},
12729 @file{AUX}, @file{COM1}, @file{COM2}, @file{COM3}, @file{COM4},
12730 @file{LPT1}, @file{LPT2}, @file{LPT3}, @file{NUL}, and @file{PRN}.
12731 File names are case insensitive, so even names like
12732 @file{aux/config.guess} are disallowed.
12734 @end table
12736 @node Shell Pattern Matching
12737 @section Shell Pattern Matching
12738 @cindex Shell pattern matching
12740 Nowadays portable patterns can use negated character classes like
12741 @samp{[!-aeiou]}.  The older syntax @samp{[^-aeiou]} is supported by
12742 some shells but not others; hence portable scripts should never use
12743 @samp{^} as the first character of a bracket pattern.
12745 Outside the C locale, patterns like @samp{[a-z]} are problematic since
12746 they may match characters that are not lower-case letters.
12748 @node Shell Substitutions
12749 @section Shell Substitutions
12750 @cindex Shell substitutions
12752 Contrary to a persistent urban legend, the Bourne shell does not
12753 systematically split variables and back-quoted expressions, in particular
12754 on the right-hand side of assignments and in the argument of @code{case}.
12755 For instance, the following code:
12757 @example
12758 case "$given_srcdir" in
12759 .)  top_srcdir="`echo "$dots" | sed 's|/$||'`" ;;
12760 *)  top_srcdir="$dots$given_srcdir" ;;
12761 esac
12762 @end example
12764 @noindent
12765 is more readable when written as:
12767 @example
12768 case $given_srcdir in
12769 .)  top_srcdir=`echo "$dots" | sed 's|/$||'` ;;
12770 *)  top_srcdir=$dots$given_srcdir ;;
12771 esac
12772 @end example
12774 @noindent
12775 and in fact it is even @emph{more} portable: in the first case of the
12776 first attempt, the computation of @code{top_srcdir} is not portable,
12777 since not all shells properly understand @code{"`@dots{}"@dots{}"@dots{}`"}.
12778 Worse yet, not all shells understand @code{"`@dots{}\"@dots{}\"@dots{}`"}
12779 the same way.  There is just no portable way to use double-quoted
12780 strings inside double-quoted back-quoted expressions (pfew!).
12782 @table @code
12783 @item $@@
12784 @cindex @samp{"$@@"}
12785 One of the most famous shell-portability issues is related to
12786 @samp{"$@@"}.  When there are no positional arguments, Posix says
12787 that @samp{"$@@"} is supposed to be equivalent to nothing, but the
12788 original Unix version 7 Bourne shell treated it as equivalent to
12789 @samp{""} instead, and this behavior survives in later implementations
12790 like Digital Unix 5.0.
12792 The traditional way to work around this portability problem is to use
12793 @samp{$@{1+"$@@"@}}.  Unfortunately this method does not work with
12794 Zsh (3.x and 4.x), which is used on Mac OS X@.  When emulating
12795 the Bourne shell, Zsh performs word splitting on @samp{$@{1+"$@@"@}}:
12797 @example
12798 zsh $ @kbd{emulate sh}
12799 zsh $ @kbd{for i in "$@@"; do echo $i; done}
12800 Hello World
12802 zsh $ @kbd{for i in $@{1+"$@@"@}; do echo $i; done}
12803 Hello
12804 World
12806 @end example
12808 @noindent
12809 Zsh handles plain @samp{"$@@"} properly, but we can't use plain
12810 @samp{"$@@"} because of the portability problems mentioned above.
12811 One workaround relies on Zsh's ``global aliases'' to convert
12812 @samp{$@{1+"$@@"@}} into @samp{"$@@"} by itself:
12814 @example
12815 test "$@{ZSH_VERSION+set@}" = set && alias -g '$@{1+"$@@"@}'='"$@@"'
12816 @end example
12818 Zsh only recognizes this alias when a shell word matches it exactly;
12819 @samp{"foo"$@{1+"$@@"@}} remains subject to word splitting.  Since this
12820 case always yields at least one shell word, use plain @samp{"$@@"}.
12822 A more conservative workaround is to avoid @samp{"$@@"} if it is
12823 possible that there may be no positional arguments.  For example,
12824 instead of:
12826 @example
12827 cat conftest.c "$@@"
12828 @end example
12830 you can use this instead:
12832 @example
12833 case $# in
12834 0) cat conftest.c;;
12835 *) cat conftest.c "$@@";;
12836 esac
12837 @end example
12839 Autoconf macros often use the @command{set} command to update
12840 @samp{$@@}, so if you are writing shell code intended for
12841 @command{configure} you should not assume that the value of @samp{$@@}
12842 persists for any length of time.
12845 @item $@{10@}
12846 @cindex positional parameters
12847 The 10th, 11th, @dots{} positional parameters can be accessed only after
12848 a @code{shift}.  The 7th Edition shell reported an error if given
12849 @code{$@{10@}}, and
12850 Solaris 10 @command{/bin/sh} still acts that way:
12852 @example
12853 $ @kbd{set 1 2 3 4 5 6 7 8 9 10}
12854 $ @kbd{echo $@{10@}}
12855 bad substitution
12856 @end example
12858 @item $@{@var{var}:-@var{value}@}
12859 @c Info cannot handle `:' in index entries.
12860 @c @cindex $@{@var{var}:-@var{value}@}
12861 Old @acronym{BSD} shells, including the Ultrix @code{sh}, don't accept the
12862 colon for any shell substitution, and complain and die.
12863 Similarly for $@{@var{var}:=@var{value}@}, $@{@var{var}:?@var{value}@}, etc.
12865 @item $@{@var{var}=@var{literal}@}
12866 @cindex $@{@var{var}=@var{literal}@}
12867 Be sure to quote:
12869 @example
12870 : $@{var='Some words'@}
12871 @end example
12873 @noindent
12874 otherwise some shells, such as on Digital Unix V 5.0, die because
12875 of a ``bad substitution''.
12877 @sp 1
12879 Solaris @command{/bin/sh} has a frightening bug in its interpretation
12880 of this.  Imagine you need set a variable to a string containing
12881 @samp{@}}.  This @samp{@}} character confuses Solaris @command{/bin/sh}
12882 when the affected variable was already set.  This bug can be exercised
12883 by running:
12885 @example
12886 $ @kbd{unset foo}
12887 $ @kbd{foo=$@{foo='@}'@}}
12888 $ @kbd{echo $foo}
12890 $ @kbd{foo=$@{foo='@}'   # no error; this hints to what the bug is}
12891 $ @kbd{echo $foo}
12893 $ @kbd{foo=$@{foo='@}'@}}
12894 $ @kbd{echo $foo}
12895 @}@}
12896  ^ ugh!
12897 @end example
12899 It seems that @samp{@}} is interpreted as matching @samp{$@{}, even
12900 though it is enclosed in single quotes.  The problem doesn't happen
12901 using double quotes.
12903 @item $@{@var{var}=@var{expanded-value}@}
12904 @cindex $@{@var{var}=@var{expanded-value}@}
12905 On Ultrix,
12906 running
12908 @example
12909 default="yu,yaa"
12910 : $@{var="$default"@}
12911 @end example
12913 @noindent
12914 sets @var{var} to @samp{M-yM-uM-,M-yM-aM-a}, i.e., the 8th bit of
12915 each char is set.  You don't observe the phenomenon using a simple
12916 @samp{echo $var} since apparently the shell resets the 8th bit when it
12917 expands $var.  Here are two means to make this shell confess its sins:
12919 @example
12920 $ @kbd{cat -v <<EOF
12921 $var
12922 EOF}
12923 @end example
12925 @noindent
12928 @example
12929 $ @kbd{set | grep '^var=' | cat -v}
12930 @end example
12932 One classic incarnation of this bug is:
12934 @example
12935 default="a b c"
12936 : $@{list="$default"@}
12937 for c in $list; do
12938   echo $c
12939 done
12940 @end example
12942 @noindent
12943 You'll get @samp{a b c} on a single line.  Why?  Because there are no
12944 spaces in @samp{$list}: there are @samp{M- }, i.e., spaces with the 8th
12945 bit set, hence no IFS splitting is performed!!!
12947 One piece of good news is that Ultrix works fine with @samp{:
12948 $@{list=$default@}}; i.e., if you @emph{don't} quote.  The bad news is
12949 then that @acronym{QNX} 4.25 then sets @var{list} to the @emph{last} item of
12950 @var{default}!
12952 The portable way out consists in using a double assignment, to switch
12953 the 8th bit twice on Ultrix:
12955 @example
12956 list=$@{list="$default"@}
12957 @end example
12959 @noindent
12960 @dots{}but beware of the @samp{@}} bug from Solaris (see above).  For safety,
12961 use:
12963 @example
12964 test "$@{var+set@}" = set || var=@var{@{value@}}
12965 @end example
12967 @item $@{#@var{var}@}
12968 @itemx $@{@var{var}%@var{word}@}
12969 @itemx $@{@var{var}%%@var{word}@}
12970 @itemx $@{@var{var}#@var{word}@}
12971 @itemx $@{@var{var}##@var{word}@}
12972 @cindex $@{#@var{var}@}
12973 @cindex $@{@var{var}%@var{word}@}
12974 @cindex $@{@var{var}%%@var{word}@}
12975 @cindex $@{@var{var}#@var{word}@}
12976 @cindex $@{@var{var}##@var{word}@}
12977 Posix requires support for these usages, but they do not work with many
12978 traditional shells, e.g., Solaris 10 @command{/bin/sh}.
12980 Also, @command{pdksh} 5.2.14 mishandles some @var{word} forms.  For
12981 example if @samp{$1} is @samp{a/b} and @samp{$2} is @samp{a}, then
12982 @samp{$@{1#$2@}} should yield @samp{/b}, but with @command{pdksh} it
12983 yields the empty string.
12986 @item `@var{commands}`
12987 @cindex `@var{commands}`
12988 @cindex Command Substitution
12989 Posix requires shells to trim all trailing newlines from command
12990 output before substituting it, so assignments like
12991 @samp{dir=`echo "$file" | tr a A`} do not work as expected if
12992 @samp{$file} ends in a newline.
12994 While in general it makes no sense, do not substitute a single builtin
12995 with side effects, because Ash 0.2, trying to optimize, does not fork a
12996 subshell to perform the command.
12998 For instance, if you wanted to check that @command{cd} is silent, do not
12999 use @samp{test -z "`cd /`"} because the following can happen:
13001 @example
13002 $ @kbd{pwd}
13003 /tmp
13004 $ @kbd{test -z "`cd /`" && pwd}
13006 @end example
13008 @noindent
13009 The result of @samp{foo=`exit 1`} is left as an exercise to the reader.
13011 The MSYS shell leaves a stray byte in the expansion of a double-quoted
13012 command substitution of a native program, if the end of the substitution
13013 is not aligned with the end of the double quote.  This may be worked
13014 around by inserting another pair of quotes:
13016 @example
13017 $ @kbd{echo "`printf 'foo\r\n'` bar" > broken}
13018 $ @kbd{echo "`printf 'foo\r\n'`"" bar" | cmp - broken}
13019 - broken differ: char 4, line 1
13020 @end example
13023 @item $(@var{commands})
13024 @cindex $(@var{commands})
13025 This construct is meant to replace @samp{`@var{commands}`},
13026 and it has most of the problems listed under @code{`@var{commands}`}.
13028 This construct can be
13029 nested while this is impossible to do portably with back quotes.
13030 Unfortunately it is not yet universally supported.  Most notably, even recent
13031 releases of Solaris don't support it:
13033 @example
13034 $ @kbd{showrev -c /bin/sh | grep version}
13035 Command version: SunOS 5.10 Generic 121005-03 Oct 2006
13036 $ @kbd{echo $(echo blah)}
13037 syntax error: `(' unexpected
13038 @end example
13040 @noindent
13041 nor does @sc{irix} 6.5's Bourne shell:
13042 @example
13043 $ @kbd{uname -a}
13044 IRIX firebird-image 6.5 07151432 IP22
13045 $ @kbd{echo $(echo blah)}
13046 $(echo blah)
13047 @end example
13049 If you do use @samp{$(@var{commands})}, make sure that the commands
13050 do not start with a parenthesis, as that would cause confusion with
13051 a different notation @samp{$((@var{expression}))} that in modern
13052 shells is an arithmetic expression not a command.  To avoid the
13053 confusion, insert a space between the two opening parentheses.
13055 Avoid @var{commands} that contain unbalanced parentheses in
13056 here-documents, comments, or case statement patterns, as many shells
13057 mishandle them.  For example, Bash 3.1, @samp{ksh88}, @command{pdksh}
13058 5.2.14, and Zsh 4.2.6 all mishandle the following valid command:
13060 @example
13061 echo $(case x in x) echo hello;; esac)
13062 @end example
13064 @item ^
13065 @cindex ^ quoting
13066 Always quote @samp{^}, otherwise traditional shells such as
13067 @command{/bin/sh} on Solaris 10 treat this like @samp{|}.
13069 @end table
13072 @node Assignments
13073 @section Assignments
13074 @cindex Shell assignments
13076 When setting several variables in a row, be aware that the order of the
13077 evaluation is undefined.  For instance @samp{foo=1 foo=2; echo $foo}
13078 gives @samp{1} with Solaris @command{/bin/sh}, but @samp{2} with Bash.
13079 You must use
13080 @samp{;} to enforce the order: @samp{foo=1; foo=2; echo $foo}.
13082 Don't rely on the following to find @file{subdir/program}:
13084 @example
13085 PATH=subdir$PATH_SEPARATOR$PATH program
13086 @end example
13088 @noindent
13089 as this does not work with Zsh 3.0.6.  Use something like this
13090 instead:
13092 @example
13093 (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)
13094 @end example
13096 Don't rely on the exit status of an assignment: Ash 0.2 does not change
13097 the status and propagates that of the last statement:
13099 @example
13100 $ @kbd{false || foo=bar; echo $?}
13102 $ @kbd{false || foo=`:`; echo $?}
13104 @end example
13106 @noindent
13107 and to make things even worse, @acronym{QNX} 4.25 just sets the exit status
13108 to 0 in any case:
13110 @example
13111 $ @kbd{foo=`exit 1`; echo $?}
13113 @end example
13115 To assign default values, follow this algorithm:
13117 @enumerate
13118 @item
13119 If the default value is a literal and does not contain any closing
13120 brace, use:
13122 @example
13123 : $@{var='my literal'@}
13124 @end example
13126 @item
13127 If the default value contains no closing brace, has to be expanded, and
13128 the variable being initialized is not intended to be IFS-split
13129 (i.e., it's not a list), then use:
13131 @example
13132 : $@{var="$default"@}
13133 @end example
13135 @item
13136 If the default value contains no closing brace, has to be expanded, and
13137 the variable being initialized is intended to be IFS-split (i.e., it's a list),
13138 then use:
13140 @example
13141 var=$@{var="$default"@}
13142 @end example
13144 @item
13145 If the default value contains a closing brace, then use:
13147 @example
13148 test "$@{var+set@}" = set || var="has a '@}'"
13149 @end example
13150 @end enumerate
13152 In most cases @samp{var=$@{var="$default"@}} is fine, but in case of
13153 doubt, just use the last form.  @xref{Shell Substitutions}, items
13154 @samp{$@{@var{var}:-@var{value}@}} and @samp{$@{@var{var}=@var{value}@}}
13155 for the rationale.
13157 @node Parentheses
13158 @section Parentheses in Shell Scripts
13159 @cindex Shell parentheses
13161 Beware of two opening parentheses in a row, as many shell
13162 implementations treat them specially.  Posix requires that the command
13163 @samp{((cat))} must behave like @samp{(cat)}, but many shells, including
13164 Bash and the Korn shell, treat @samp{((cat))} as an arithmetic
13165 expression equivalent to @samp{let "cat"}, and may or may not report an
13166 error when they detect that @samp{cat} is not a number.  As another
13167 example, @samp{pdksh} 5.2.14 misparses the following code:
13169 @example
13170 if ((true) || false); then
13171   echo ok
13173 @end example
13175 @noindent
13176 To work around this problem, insert a space between the two opening
13177 parentheses.  There is a similar problem and workaround with
13178 @samp{$((}; see @ref{Shell Substitutions}.
13180 @node Slashes
13181 @section Slashes in Shell Scripts
13182 @cindex Shell slashes
13184 Unpatched Tru64 5.1 @command{sh} omits the last slash of command-line
13185 arguments that contain two trailing slashes:
13187 @example
13188 $ @kbd{echo / // /// //// .// //.}
13189 / / // /// ./ //.
13190 $ @kbd{x=//}
13191 $ @kbd{eval "echo \$x"}
13193 $ @kbd{set -x}
13194 $ @kbd{echo abc | tr -t ab //}
13195 + echo abc
13196 + tr -t ab /
13198 @end example
13200 Unpatched Tru64 4.0 @command{sh} adds a slash after @samp{"$var"} if the
13201 variable is empty and the second double-quote is followed by a word that
13202 begins and ends with slash:
13204 @example
13205 $ @kbd{sh -xc 'p=; echo "$p"/ouch/'}
13207 + echo //ouch/
13208 //ouch/
13209 @end example
13211 However, our understanding is that patches are available, so perhaps
13212 it's not worth worrying about working around these horrendous bugs.
13214 @node Special Shell Variables
13215 @section Special Shell Variables
13216 @cindex Shell variables
13217 @cindex Special shell variables
13219 Some shell variables should not be used, since they can have a deep
13220 influence on the behavior of the shell.  In order to recover a sane
13221 behavior from the shell, some variables should be unset, but
13222 @command{unset} is not portable (@pxref{Limitations of Builtins}) and a
13223 fallback value is needed.
13225 As a general rule, shell variable names containing a lower-case letter
13226 are safe; you can define and use these variables without worrying about
13227 their effect on the underlying system, and without worrying about
13228 whether the shell changes them unexpectedly.  (The exception is the
13229 shell variable @code{status}, as described below.)
13231 Here is a list of names that are known to cause trouble.  This list is
13232 not exhaustive, but you should be safe if you avoid the name
13233 @code{status} and names containing only upper-case letters and
13234 underscores.
13236 @c Alphabetical order, case insensitive, `A' before `a'.
13237 @table @code
13238 @item _
13239 Many shells reserve @samp{$_} for various purposes, e.g., the name of
13240 the last command executed.
13242 @item BIN_SH
13243 @evindex BIN_SH
13244 In Tru64, if @env{BIN_SH} is set to @code{xpg4}, subsidiary invocations of
13245 the standard shell conform to Posix.
13247 @item CDPATH
13248 @evindex CDPATH
13249 When this variable is set it specifies a list of directories to search
13250 when invoking @code{cd} with a relative file name that did not start
13251 with @samp{./} or @samp{../}.  Posix
13252 1003.1-2001 says that if a nonempty directory name from @env{CDPATH}
13253 is used successfully, @code{cd} prints the resulting absolute
13254 file name.  Unfortunately this output can break idioms like
13255 @samp{abs=`cd src && pwd`} because @code{abs} receives the name twice.
13256 Also, many shells do not conform to this part of Posix; for
13257 example, @command{zsh} prints the result only if a directory name
13258 other than @file{.} was chosen from @env{CDPATH}.
13260 In practice the shells that have this problem also support
13261 @command{unset}, so you can work around the problem as follows:
13263 @example
13264 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
13265 @end example
13267 You can also avoid output by ensuring that your directory name is
13268 absolute or anchored at @samp{./}, as in @samp{abs=`cd ./src && pwd`}.
13270 Autoconf-generated scripts automatically unset @env{CDPATH} if
13271 possible, so you need not worry about this problem in those scripts.
13273 @item DUALCASE
13274 @evindex DUALCASE
13275 In the MKS shell, case statements and file name generation are
13276 case-insensitive unless @env{DUALCASE} is nonzero.
13277 Autoconf-generated scripts export this variable when they start up.
13279 @item ENV
13280 @itemx MAIL
13281 @itemx MAILPATH
13282 @itemx PS1
13283 @itemx PS2
13284 @itemx PS4
13285 @evindex ENV
13286 @evindex MAIL
13287 @evindex MAILPATH
13288 @evindex PS1
13289 @evindex PS2
13290 @evindex PS4
13291 These variables should not matter for shell scripts, since they are
13292 supposed to affect only interactive shells.  However, at least one
13293 shell (the pre-3.0 @sc{uwin} Korn shell) gets confused about
13294 whether it is interactive, which means that (for example) a @env{PS1}
13295 with a side effect can unexpectedly modify @samp{$?}.  To work around
13296 this bug, Autoconf-generated scripts do something like this:
13298 @example
13299 (unset ENV) >/dev/null 2>&1 && unset ENV MAIL MAILPATH
13300 PS1='$ '
13301 PS2='> '
13302 PS4='+ '
13303 @end example
13305 @item FPATH
13306 The Korn shell uses @env{FPATH} to find shell functions, so avoid
13307 @env{FPATH} in portable scripts.  @env{FPATH} is consulted after
13308 @env{PATH}, but you still need to be wary of tests that use @env{PATH}
13309 to find whether a command exists, since they might report the wrong
13310 result if @env{FPATH} is also set.
13312 @item IFS
13313 @evindex IFS
13314 Long ago, shell scripts inherited @env{IFS} from the environment,
13315 but this caused many problems so modern shells ignore any environment
13316 settings for @env{IFS}.
13318 Don't set the first character of @code{IFS} to backslash.  Indeed,
13319 Bourne shells use the first character (backslash) when joining the
13320 components in @samp{"$@@"} and some shells then reinterpret (!)@: the
13321 backslash escapes, so you can end up with backspace and other strange
13322 characters.
13324 The proper value for @code{IFS} (in regular code, not when performing
13325 splits) is @samp{@key{SPC}@key{TAB}@key{RET}}.  The first character is
13326 especially important, as it is used to join the arguments in @samp{$*};
13327 however, note that traditional shells, but also bash-2.04, fail to adhere
13328 to this and join with a space anyway.
13330 @item LANG
13331 @itemx LC_ALL
13332 @itemx LC_COLLATE
13333 @itemx LC_CTYPE
13334 @itemx LC_MESSAGES
13335 @itemx LC_MONETARY
13336 @itemx LC_NUMERIC
13337 @itemx LC_TIME
13338 @evindex LANG
13339 @evindex LC_ALL
13340 @evindex LC_COLLATE
13341 @evindex LC_CTYPE
13342 @evindex LC_MESSAGES
13343 @evindex LC_MONETARY
13344 @evindex LC_NUMERIC
13345 @evindex LC_TIME
13347 Autoconf-generated scripts normally set all these variables to
13348 @samp{C} because so much configuration code assumes the C locale and
13349 Posix requires that locale environment variables be set to
13350 @samp{C} if the C locale is desired.  However, some older, nonstandard
13351 systems (notably @acronym{SCO}) break if locale environment variables
13352 are set to @samp{C}, so when running on these systems
13353 Autoconf-generated scripts unset the variables instead.
13355 @item LANGUAGE
13356 @evindex LANGUAGE
13358 @env{LANGUAGE} is not specified by Posix, but it is a @acronym{GNU}
13359 extension that overrides @env{LC_ALL} in some cases, so
13360 Autoconf-generated scripts set it too.
13362 @item LC_ADDRESS
13363 @itemx LC_IDENTIFICATION
13364 @itemx LC_MEASUREMENT
13365 @itemx LC_NAME
13366 @itemx LC_PAPER
13367 @itemx LC_TELEPHONE
13368 @evindex LC_ADDRESS
13369 @evindex LC_IDENTIFICATION
13370 @evindex LC_MEASUREMENT
13371 @evindex LC_NAME
13372 @evindex LC_PAPER
13373 @evindex LC_TELEPHONE
13375 These locale environment variables are @acronym{GNU} extensions.  They
13376 are treated like their Posix brethren (@env{LC_COLLATE},
13377 etc.)@: as described above.
13379 @item LINENO
13380 Most modern shells provide the current line number in @code{LINENO}.
13381 Its value is the line number of the beginning of the current command.
13382 Autoconf attempts to execute @command{configure} with a shell that
13383 supports @code{LINENO}.
13384 If no such shell is available, it attempts to implement @code{LINENO}
13385 with a Sed prepass that replaces each instance of the string
13386 @code{$LINENO} (not followed by an alphanumeric character) with the
13387 line's number.
13389 You should not rely on @code{LINENO} within @command{eval}, as the
13390 behavior differs in practice.  Also, the possibility of the Sed
13391 prepass means that you should not rely on @code{$LINENO} when quoted,
13392 when in here-documents, or when in long commands that cross line
13393 boundaries.  Subshells should be OK, though.  In the following
13394 example, lines 1, 6, and 9 are portable, but the other instances of
13395 @code{LINENO} are not:
13397 @example
13398 @group
13399 $ @kbd{cat lineno}
13400 echo 1. $LINENO
13401 cat <<EOF
13402 3. $LINENO
13403 4. $LINENO
13405 ( echo 6. $LINENO )
13406 eval 'echo 7. $LINENO'
13407 echo 8. '$LINENO'
13408 echo 9. $LINENO '
13409 10.' $LINENO
13410 @end group
13411 @group
13412 $ @kbd{bash-2.05 lineno}
13413 1. 1
13414 3. 2
13415 4. 2
13416 6. 6
13417 7. 1
13418 8. $LINENO
13419 9. 9
13420 10. 9
13421 @end group
13422 @group
13423 $ @kbd{zsh-3.0.6 lineno}
13424 1. 1
13425 3. 2
13426 4. 2
13427 6. 6
13428 7. 7
13429 8. $LINENO
13430 9. 9
13431 10. 9
13432 @end group
13433 @group
13434 $ @kbd{pdksh-5.2.14 lineno}
13435 1. 1
13436 3. 2
13437 4. 2
13438 6. 6
13439 7. 0
13440 8. $LINENO
13441 9. 9
13442 10. 9
13443 @end group
13444 @group
13445 $ @kbd{sed '=' <lineno |}
13446 > @kbd{  sed '}
13447 > @kbd{    N}
13448 > @kbd{    s,$,-,}
13449 > @kbd{    t loop}
13450 > @kbd{    :loop}
13451 > @kbd{    s,^\([0-9]*\)\(.*\)[$]LINENO\([^a-zA-Z0-9_]\),\1\2\1\3,}
13452 > @kbd{    t loop}
13453 > @kbd{    s,-$,,}
13454 > @kbd{    s,^[0-9]*\n,,}
13455 > @kbd{  ' |}
13456 > @kbd{  sh}
13457 1. 1
13458 3. 3
13459 4. 4
13460 6. 6
13461 7. 7
13462 8. 8
13463 9. 9
13464 10. 10
13465 @end group
13466 @end example
13468 @item NULLCMD
13469 @evindex NULLCMD
13470 When executing the command @samp{>foo}, @command{zsh} executes
13471 @samp{$NULLCMD >foo} unless it is operating in Bourne shell
13472 compatibility mode and the @command{zsh} version is newer
13473 than 3.1.6-dev-18.  If you are using an older @command{zsh}
13474 and forget to set @env{NULLCMD},
13475 your script might be suspended waiting for data on its standard input.
13477 @item PATH_SEPARATOR
13478 @evindex PATH_SEPARATOR
13479 On @acronym{DJGPP} systems, the @env{PATH_SEPARATOR} environment
13480 variable can be set to either @samp{:} or @samp{;} to control the path
13481 separator Bash uses to set up certain environment variables (such as
13482 @env{PATH}).  You can set this variable to @samp{;} if you want
13483 @command{configure} to use @samp{;} as a separator; this might be useful
13484 if you plan to use non-Posix shells to execute files.  @xref{File System
13485 Conventions}, for more information about @code{PATH_SEPARATOR}.
13487 @item PWD
13488 @evindex PWD
13489 Posix 1003.1-2001 requires that @command{cd} and
13490 @command{pwd} must update the @env{PWD} environment variable to point
13491 to the logical name of the current directory, but traditional shells
13492 do not support this.  This can cause confusion if one shell instance
13493 maintains @env{PWD} but a subsidiary and different shell does not know
13494 about @env{PWD} and executes @command{cd}; in this case @env{PWD}
13495 points to the wrong directory.  Use @samp{`pwd`} rather than
13496 @samp{$PWD}.
13498 @item RANDOM
13499 Many shells provide @code{RANDOM}, a variable that returns a different
13500 integer each time it is used.  Most of the time, its value does not
13501 change when it is not used, but on @sc{irix} 6.5 the value changes all
13502 the time.  This can be observed by using @command{set}.  It is common
13503 practice to use @code{$RANDOM} as part of a file name, but code
13504 shouldn't rely on @code{$RANDOM} expanding to a nonempty string.
13506 @item status
13507 This variable is an alias to @samp{$?} for @code{zsh} (at least 3.1.6),
13508 hence read-only.  Do not use it.
13509 @end table
13511 @node Limitations of Builtins
13512 @section Limitations of Shell Builtins
13513 @cindex Shell builtins
13514 @cindex Limitations of shell builtins
13516 No, no, we are serious: some shells do have limitations!  :)
13518 You should always keep in mind that any builtin or command may support
13519 options, and therefore differ in behavior with arguments
13520 starting with a dash.  For instance, the innocent @samp{echo "$word"}
13521 can give unexpected results when @code{word} starts with a dash.  It is
13522 often possible to avoid this problem using @samp{echo "x$word"}, taking
13523 the @samp{x} into account later in the pipe.
13525 @table @asis
13526 @item @command{.}
13527 @c --------------
13528 @prindex @command{.}
13529 Use @command{.} only with regular files (use @samp{test -f}).  Bash
13530 2.03, for instance, chokes on @samp{. /dev/null}.  Also, remember that
13531 @command{.} uses @env{PATH} if its argument contains no slashes, so if
13532 you want to use @command{.} on a file @file{foo} in the current
13533 directory, you must use @samp{. ./foo}.
13535 @item @command{!}
13536 @c --------------
13537 @prindex @command{!}
13538 The Unix version 7 shell did not support
13539 negating the exit status of commands with @command{!}, and this feature
13540 is still absent from some shells (e.g., Solaris @command{/bin/sh}).
13541 Shell code like this:
13543 @example
13544 if ! cmp file1 file2 >/dev/null 2>&1; then
13545   echo files differ or trouble
13547 @end example
13549 is therefore not portable in practice.  Typically it is easy to rewrite
13550 such code, e.g.:
13552 @example
13553 cmp file1 file2 >/dev/null 2>&1 ||
13554   echo files differ or trouble
13555 @end example
13557 More generally, one can always rewrite @samp{! @var{command}} as:
13559 @example
13560 if @var{command}; then (exit 1); else :; fi
13561 @end example
13564 @item @command{@{...@}}
13565 @c --------------------
13566 @prindex @command{@{...@}}
13567 As recently as GNU Bash 3.2, some shells do not properly set @samp{$?}
13568 when failing to write redirected output of any compound command other
13569 than a subshell group, when that compound command is the first thing
13570 executed.  This is most commonly observed with @{...@}, but also affects
13571 other compound commands.
13573 @example
13574 $ @kbd{bash -c '@{ echo foo; @} >/bad; echo $?'}
13575 bash: line 1: /bad: Permission denied
13577 $ @kbd{bash -c 'while :; do echo; done >/bad; echo $?'}
13578 bash: line 1: /bad: Permission denied
13580 @end example
13582 The workaround is simple: prime bash with a simple command before any
13583 compound command with redirection.
13585 @example
13586 $ @kbd{bash -c ':; @{ echo foo; @} >/bad; echo $?'}
13587 bash: line 1: /bad: Permission denied
13589 $ @kbd{bash -c ':; while :; do echo; done >/bad; echo $?'}
13590 bash: line 1: /bad: Permission denied
13592 @end example
13595 @item @command{break}
13596 @c ------------------
13597 @prindex @command{break}
13598 The use of @samp{break 2} etc.@: is safe.
13601 @item @command{case}
13602 @c -----------------
13603 @prindex @command{case}
13604 You don't need to quote the argument; no splitting is performed.
13606 You don't need the final @samp{;;}, but you should use it.
13608 Posix requires support for @code{case} patterns with opening
13609 parentheses like this:
13611 @example
13612 case $file_name in
13613 (*.c) echo "C source code";;
13614 esac
13615 @end example
13617 @noindent
13618 but the @code{(} in this example is not portable to many Bourne
13619 shell implementations.  It can be omitted safely.
13621 Zsh handles pattern fragments derived from parameter expansions or
13622 command substitutions as though quoted:
13624 @example
13625 $ pat=\?; case aa in ?$pat) echo match;; esac
13626 $ pat=\?; case a? in ?$pat) echo match;; esac
13627 match
13628 @end example
13630 @noindent
13631 Because of a bug in its @code{fnmatch}, Bash fails to properly
13632 handle backslashes in character classes:
13634 @example
13635 bash-2.02$ @kbd{case /tmp in [/\\]*) echo OK;; esac}
13636 bash-2.02$
13637 @end example
13639 @noindent
13640 This is extremely unfortunate, since you are likely to use this code to
13641 handle Posix or @sc{ms-dos} absolute file names.  To work around this
13642 bug, always put the backslash first:
13644 @example
13645 bash-2.02$ @kbd{case '\TMP' in [\\/]*) echo OK;; esac}
13647 bash-2.02$ @kbd{case /tmp in [\\/]*) echo OK;; esac}
13649 @end example
13651 Many Bourne shells cannot handle closing brackets in character classes
13652 correctly.
13654 Some shells also have problems with backslash escaping in case you do not want
13655 to match the backslash: both a backslash and the escaped character match this
13656 pattern.  To work around this, specify the character class in a variable, so
13657 that quote removal does not apply afterwards, and the special characters don't
13658 have to be backslash-escaped:
13660 @example
13661 $ @kbd{case '\' in [\<]) echo OK;; esac}
13663 $ @kbd{scanset='[<]'; case '\' in $scanset) echo OK;; esac}
13665 @end example
13667 Even with this, Solaris @command{ksh} matches a backslash if the set
13668 contains any
13669 of the characters @samp{|}, @samp{&}, @samp{(}, or @samp{)}.
13671 Conversely, Tru64 @command{ksh} (circa 2003) erroneously always matches
13672 a closing parenthesis if not specified in a character class:
13674 @example
13675 $ @kbd{case foo in *\)*) echo fail ;; esac}
13676 fail
13677 $ @kbd{case foo in *')'*) echo fail ;; esac}
13678 fail
13679 @end example
13681 Some shells, such as Ash 0.3.8, are confused by an empty
13682 @code{case}/@code{esac}:
13684 @example
13685 ash-0.3.8 $ @kbd{case foo in esac;}
13686 @error{}Syntax error: ";" unexpected (expecting ")")
13687 @end example
13689 Many shells still do not support parenthesized cases, which is a pity
13690 for those of us using tools that rely on balanced parentheses.  For
13691 instance, Solaris @command{/bin/sh}:
13693 @example
13694 $ @kbd{case foo in (foo) echo foo;; esac}
13695 @error{}syntax error: `(' unexpected
13696 @end example
13699 @item @command{cd}
13700 @c ---------------
13701 @prindex @command{cd}
13702 Posix 1003.1-2001 requires that @command{cd} must support
13703 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
13704 with @option{-L} being the default.  However, traditional shells do
13705 not support these options, and their @command{cd} command has the
13706 @option{-P} behavior.
13708 Portable scripts should assume neither option is supported, and should
13709 assume neither behavior is the default.  This can be a bit tricky,
13710 since the Posix default behavior means that, for example,
13711 @samp{ls ..} and @samp{cd ..} may refer to different directories if
13712 the current logical directory is a symbolic link.  It is safe to use
13713 @command{cd @var{dir}} if @var{dir} contains no @file{..} components.
13714 Also, Autoconf-generated scripts check for this problem when computing
13715 variables like @code{ac_top_srcdir} (@pxref{Configuration Actions}),
13716 so it is safe to @command{cd} to these variables.
13718 See @xref{Special Shell Variables}, for portability problems involving
13719 @command{cd} and the @env{CDPATH} environment variable.
13720 Also please see the discussion of the @command{pwd} command.
13723 @item @command{echo}
13724 @c -----------------
13725 @prindex @command{echo}
13726 The simple @command{echo} is probably the most surprising source of
13727 portability troubles.  It is not possible to use @samp{echo} portably
13728 unless both options and escape sequences are omitted.  New applications
13729 which are not aiming at portability should use @samp{printf} instead of
13730 @samp{echo}.
13732 Don't expect any option.  @xref{Preset Output Variables}, @code{ECHO_N}
13733 etc.@: for a means to simulate @option{-n}.
13735 Do not use backslashes in the arguments, as there is no consensus on
13736 their handling.  For @samp{echo '\n' | wc -l}, the @command{sh} of
13737 Solaris outputs 2, but Bash and Zsh (in @command{sh} emulation mode) output 1.
13738 The problem is truly @command{echo}: all the shells
13739 understand @samp{'\n'} as the string composed of a backslash and an
13740 @samp{n}.
13742 Because of these problems, do not pass a string containing arbitrary
13743 characters to @command{echo}.  For example, @samp{echo "$foo"} is safe
13744 if you know that @var{foo}'s value cannot contain backslashes and cannot
13745 start with @samp{-}, but otherwise you should use a here-document like
13746 this:
13748 @example
13749 cat <<EOF
13750 $foo
13752 @end example
13755 @item @command{eval}
13756 @c -----------------
13757 @prindex @command{eval}
13758 The @command{eval} command is useful in limited circumstances, e.g.,
13759 using commands like @samp{eval table_$key=\$value} and @samp{eval
13760 value=table_$key} to simulate a hash table when the key is known to be
13761 alphanumeric.  However, @command{eval} is tricky to use on arbitrary
13762 arguments, even when it is implemented correctly.
13764 It is obviously unwise to use @samp{eval $cmd} if the string value of
13765 @samp{cmd} was derived from an untrustworthy source.  But even if the
13766 string value is valid, @samp{eval $cmd} might not work as intended,
13767 since it causes field splitting and file name expansion to occur twice,
13768 once for the @command{eval} and once for the command itself.  It is
13769 therefore safer to use @samp{eval "$cmd"}.  For example, if @var{cmd}
13770 has the value @samp{cat test?.c}, @samp{eval $cmd} might expand to the
13771 equivalent of @samp{cat test;.c} if there happens to be a file named
13772 @file{test;.c} in the current directory; and this in turn
13773 mistakenly attempts to invoke @command{cat} on the file @file{test} and
13774 then execute the command @command{.c}.  To avoid this problem, use
13775 @samp{eval "$cmd"} rather than @samp{eval $cmd}.
13777 However, suppose that you want to output the text of the evaluated
13778 command just before executing it.  Assuming the previous example,
13779 @samp{echo "Executing: $cmd"} outputs @samp{Executing: cat test?.c}, but
13780 this output doesn't show the user that @samp{test;.c} is the actual name
13781 of the copied file.  Conversely, @samp{eval "echo Executing: $cmd"}
13782 works on this example, but it fails with @samp{cmd='cat foo >bar'},
13783 since it mistakenly replaces the contents of @file{bar} by the
13784 string @samp{cat foo}.  No simple, general, and portable solution to
13785 this problem is known.
13787 You should also be wary of common bugs in @command{eval} implementations.
13788 In some shell implementations (e.g., older @command{ash}, Open@acronym{BSD} 3.8
13789 @command{sh}, @command{pdksh} v5.2.14 99/07/13.2, and @command{zsh}
13790 4.2.5), the arguments of @samp{eval} are evaluated in a context where
13791 @samp{$?} is 0, so they exhibit behavior like this:
13793 @example
13794 $ @kbd{false; eval 'echo $?'}
13796 @end example
13798 The correct behavior here is to output a nonzero value,
13799 but portable scripts should not rely on this.
13801 You should not rely on @code{LINENO} within @command{eval}.
13802 @xref{Special Shell Variables}.
13804 @item @command{exit}
13805 @c -----------------
13806 @prindex @command{exit}
13807 The default value of @command{exit} is supposed to be @code{$?};
13808 unfortunately, some shells, such as the @acronym{DJGPP} port of Bash 2.04, just
13809 perform @samp{exit 0}.
13811 @example
13812 bash-2.04$ @kbd{foo=`exit 1` || echo fail}
13813 fail
13814 bash-2.04$ @kbd{foo=`(exit 1)` || echo fail}
13815 fail
13816 bash-2.04$ @kbd{foo=`(exit 1); exit` || echo fail}
13817 bash-2.04$
13818 @end example
13820 Using @samp{exit $?} restores the expected behavior.
13822 Some shell scripts, such as those generated by @command{autoconf}, use a
13823 trap to clean up before exiting.  If the last shell command exited with
13824 nonzero status, the trap also exits with nonzero status so that the
13825 invoker can tell that an error occurred.
13827 Unfortunately, in some shells, such as Solaris @command{/bin/sh}, an exit
13828 trap ignores the @code{exit} command's argument.  In these shells, a trap
13829 cannot determine whether it was invoked by plain @code{exit} or by
13830 @code{exit 1}.  Instead of calling @code{exit} directly, use the
13831 @code{AC_MSG_ERROR} macro that has a workaround for this problem.
13834 @item @command{export}
13835 @c -------------------
13836 @prindex @command{export}
13837 The builtin @command{export} dubs a shell variable @dfn{environment
13838 variable}.  Each update of exported variables corresponds to an update
13839 of the environment variables.  Conversely, each environment variable
13840 received by the shell when it is launched should be imported as a shell
13841 variable marked as exported.
13843 Alas, many shells, such as Solaris @command{/bin/sh},
13844 @sc{irix} 6.3, @sc{irix} 5.2,
13845 @acronym{AIX} 4.1.5, and Digital Unix 4.0, forget to
13846 @command{export} the environment variables they receive.  As a result,
13847 two variables coexist: the environment variable and the shell
13848 variable.  The following code demonstrates this failure:
13850 @example
13851 #!/bin/sh
13852 echo $FOO
13853 FOO=bar
13854 echo $FOO
13855 exec /bin/sh $0
13856 @end example
13858 @noindent
13859 when run with @samp{FOO=foo} in the environment, these shells print
13860 alternately @samp{foo} and @samp{bar}, although they should print only
13861 @samp{foo} and then a sequence of @samp{bar}s.
13863 Therefore you should @command{export} again each environment variable
13864 that you update.
13867 @item @command{false}
13868 @c ------------------
13869 @prindex @command{false}
13870 Don't expect @command{false} to exit with status 1: in native
13871 Solaris @file{/bin/false} exits with status 255.
13874 @item @command{for}
13875 @c ----------------
13876 @prindex @command{for}
13877 To loop over positional arguments, use:
13879 @example
13880 for arg
13882   echo "$arg"
13883 done
13884 @end example
13886 @noindent
13887 You may @emph{not} leave the @code{do} on the same line as @code{for},
13888 since some shells improperly grok:
13890 @example
13891 for arg; do
13892   echo "$arg"
13893 done
13894 @end example
13896 If you want to explicitly refer to the positional arguments, given the
13897 @samp{$@@} bug (@pxref{Shell Substitutions}), use:
13899 @example
13900 for arg in $@{1+"$@@"@}; do
13901   echo "$arg"
13902 done
13903 @end example
13905 @noindent
13906 But keep in mind that Zsh, even in Bourne shell emulation mode, performs
13907 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
13908 item @samp{$@@}, for more.
13911 @item @command{if}
13912 @c ---------------
13913 @prindex @command{if}
13914 Using @samp{!} is not portable.  Instead of:
13916 @example
13917 if ! cmp -s file file.new; then
13918   mv file.new file
13920 @end example
13922 @noindent
13923 use:
13925 @example
13926 if cmp -s file file.new; then :; else
13927   mv file.new file
13929 @end example
13931 There are shells that do not reset the exit status from an @command{if}:
13933 @example
13934 $ @kbd{if (exit 42); then true; fi; echo $?}
13936 @end example
13938 @noindent
13939 whereas a proper shell should have printed @samp{0}.  This is especially
13940 bad in makefiles since it produces false failures.  This is why properly
13941 written makefiles, such as Automake's, have such hairy constructs:
13943 @example
13944 if test -f "$file"; then
13945   install "$file" "$dest"
13946 else
13947   :
13949 @end example
13952 @item @command{printf}
13953 @c ------------------
13954 @prindex @command{printf}
13955 A format string starting with a @samp{-} can cause problems.
13956 Bash interprets it as an option and
13957 gives an error.  And @samp{--} to mark the end of options is not good
13958 in the Net@acronym{BSD} Almquist shell (e.g., 0.4.6) which takes that
13959 literally as the format string.  Putting the @samp{-} in a @samp{%c}
13960 or @samp{%s} is probably easiest:
13962 @example
13963 printf %s -foo
13964 @end example
13966 Bash 2.03 mishandles an escape sequence that happens to evaluate to @samp{%}:
13968 @example
13969 $ @kbd{printf '\045'}
13970 bash: printf: `%': missing format character
13971 @end example
13973 Large outputs may cause trouble.  On Solaris 2.5.1 through 10, for
13974 example, @file{/usr/bin/printf} is buggy, so when using
13975 @command{/bin/sh} the command @samp{printf %010000x 123} normally dumps
13976 core.
13979 @item @command{read}
13980 @c ------------------
13981 @prindex @command{read}
13982 Not all shells support @option{-r} (Solaris @command{/bin/sh} for example).
13985 @item @command{pwd}
13986 @c ----------------
13987 @prindex @command{pwd}
13988 With modern shells, plain @command{pwd} outputs a ``logical''
13989 directory name, some of whose components may be symbolic links.  These
13990 directory names are in contrast to ``physical'' directory names, whose
13991 components are all directories.
13993 Posix 1003.1-2001 requires that @command{pwd} must support
13994 the @option{-L} (``logical'') and @option{-P} (``physical'') options,
13995 with @option{-L} being the default.  However, traditional shells do
13996 not support these options, and their @command{pwd} command has the
13997 @option{-P} behavior.
13999 Portable scripts should assume neither option is supported, and should
14000 assume neither behavior is the default.  Also, on many hosts
14001 @samp{/bin/pwd} is equivalent to @samp{pwd -P}, but Posix
14002 does not require this behavior and portable scripts should not rely on
14005 Typically it's best to use plain @command{pwd}.  On modern hosts this
14006 outputs logical directory names, which have the following advantages:
14008 @itemize @bullet
14009 @item
14010 Logical names are what the user specified.
14011 @item
14012 Physical names may not be portable from one installation
14013 host to another due to network file system gymnastics.
14014 @item
14015 On modern hosts @samp{pwd -P} may fail due to lack of permissions to
14016 some parent directory, but plain @command{pwd} cannot fail for this
14017 reason.
14018 @end itemize
14020 Also please see the discussion of the @command{cd} command.
14023 @item @command{set}
14024 @c ----------------
14025 @prindex @command{set}
14026 With the Free@acronym{BSD} 6.0 shell, the @command{set} command (without
14027 any options) does not sort its output.
14029 The @command{set} builtin faces the usual problem with arguments
14030 starting with a
14031 dash.  Modern shells such as Bash or Zsh understand @option{--} to specify
14032 the end of the options (any argument after @option{--} is a parameter,
14033 even @samp{-x} for instance), but many traditional shells (e.g., Solaris
14034 10 @command{/bin/sh}) simply stop option
14035 processing as soon as a non-option argument is found.  Therefore, use
14036 @samp{dummy} or simply @samp{x} to end the option processing, and use
14037 @command{shift} to pop it out:
14039 @example
14040 set x $my_list; shift
14041 @end example
14043 Avoid @samp{set -}, e.g., @samp{set - $my_list}.  Posix no
14044 longer requires support for this command, and in traditional shells
14045 @samp{set - $my_list} resets the @option{-v} and @option{-x} options, which
14046 makes scripts harder to debug.
14048 Some nonstandard shells do not recognize more than one option
14049 (e.g., @samp{set -e -x} assigns @samp{-x} to the command line).  It is
14050 better to combine them:
14052 @example
14053 set -ex
14054 @end example
14056 The @acronym{BSD} shell has had several problems with the @option{-e}
14057 option, partly because @acronym{BSD} @command{make} traditionally used
14058 @option{-e} even though this was incompatible with Posix
14059 (@pxref{Failure in Make Rules}).  Older versions of the @acronym{BSD}
14060 shell (circa 1990) mishandled @samp{&&}, @samp{||}, @samp{if}, and
14061 @samp{case} when @option{-e} was in effect, causing the shell to exit
14062 unexpectedly in some cases.  This was particularly a problem with
14063 makefiles, and led to circumlocutions like @samp{sh -c 'test -f file ||
14064 touch file'}, where the seemingly-unnecessary @samp{sh -c '@dots{}'}
14065 wrapper works around the bug.
14067 Even relatively-recent versions of the @acronym{BSD} shell (e.g.,
14068 Open@acronym{BSD} 3.4) wrongly exit with @option{-e} if a command within
14069 @samp{&&} fails inside a compound statement.  For example:
14071 @example
14072 #! /bin/sh
14073 set -e
14074 foo=''
14075 test -n "$foo" && exit 1
14076 echo one
14077 if :; then
14078   test -n "$foo" && exit 1
14080 echo two
14081 @end example
14083 @noindent
14084 does not print @samp{two}.  One workaround is to use @samp{if test -n
14085 "$foo"; then exit 1; fi} rather than @samp{test -n "$foo" && exit 1}.
14086 Another possibility is to warn @acronym{BSD} users not to use @samp{sh -e}.
14089 @item @command{shift}
14090 @c ------------------
14091 @prindex @command{shift}
14092 Not only is @command{shift}ing a bad idea when there is nothing left to
14093 shift, but in addition it is not portable: the shell of @acronym{MIPS
14094 RISC/OS} 4.52 refuses to do it.
14096 Don't use @samp{shift 2} etc.; it was not in the 7th Edition Bourne shell,
14097 and it is also absent in many pre-Posix shells.
14100 @item @command{source}
14101 @c -------------------
14102 @prindex @command{source}
14103 This command is not portable, as Posix does not require it; use
14104 @command{.} instead.
14107 @item @command{test}
14108 @c -----------------
14109 @prindex @command{test}
14110 The @code{test} program is the way to perform many file and string
14111 tests.  It is often invoked by the alternate name @samp{[}, but using
14112 that name in Autoconf code is asking for trouble since it is an M4 quote
14113 character.
14115 The @option{-a}, @option{-o}, @samp{(}, and @samp{)} operands are not
14116 portable and should be avoided.  Thus, portable uses of @command{test}
14117 should never have more than four arguments, and scripts should use shell
14118 constructs like @samp{&&} and @samp{||} instead.  If you combine
14119 @samp{&&} and @samp{||} in the same statement, keep in mind that they
14120 have equal precedence, so it is often better to parenthesize even when
14121 this is redundant.  For example:
14123 @smallexample
14124 # Not portable:
14125 test "X$a" = "X$b" -a \
14126   '(' "X$c" != "X$d" -o "X$e" = "X$f" ')'
14128 # Portable:
14129 test "X$a" = "X$b" &&
14130   @{ test "X$c" != "X$d" || test "X$e" = "X$f"; @}
14131 @end smallexample
14133 @command{test} does not process options like most other commands do; for
14134 example, it does not recognize the @option{--} argument as marking the
14135 end of options.
14137 It is safe to use @samp{!} as a @command{test} operator.  For example,
14138 @samp{if test ! -d foo; @dots{}} is portable even though @samp{if ! test
14139 -d foo; @dots{}} is not.
14142 @item @command{test} (files)
14143 @c -------------------------
14144 To enable @command{configure} scripts to support cross-compilation, they
14145 shouldn't do anything that tests features of the build system instead of
14146 the host system.  But occasionally you may find it necessary to check
14147 whether some arbitrary file exists.  To do so, use @samp{test -f} or
14148 @samp{test -r}.  Do not use @samp{test -x}, because 4.3@acronym{BSD} does not
14149 have it.  Do not use @samp{test -e} either, because Solaris @command{/bin/sh}
14150 lacks it.  To test for symbolic links on systems that have them, use
14151 @samp{test -h} rather than @samp{test -L}; either form conforms to
14152 Posix 1003.1-2001, but older shells like Solaris 8
14153 @code{/bin/sh} support only @option{-h}.
14155 @item @command{test} (strings)
14156 @c ---------------------------
14157 Posix says that @samp{test "@var{string}"} succeeds if @var{string} is
14158 not null, but this usage is not portable to traditional platforms like
14159 Solaris 10 @command{/bin/sh}, which mishandle strings like @samp{!} and
14160 @samp{-n}.
14162 Posix also says that @samp{test ! "@var{string}"},
14163 @samp{test -n "@var{string}"} and
14164 @samp{test -z "@var{string}"} work with any string, but many
14165 shells (such as Solaris, @acronym{AIX} 3.2, @sc{unicos} 10.0.0.6,
14166 Digital Unix 4, etc.)@: get confused if
14167 @var{string} looks like an operator:
14169 @example
14170 $ @kbd{test -n =}
14171 test: argument expected
14172 $ @kbd{test ! -n}
14173 test: argument expected
14174 @end example
14176 Similarly, Posix says that both @samp{test "@var{string1}" = "@var{string2"}}
14177 and @samp{test "@var{string1}" != "@var{string2"}} work for any pairs of
14178 strings, but in practice this is not true for troublesome strings that
14179 look like operators or parentheses, or that begin with @samp{-}.
14181 It is best to protect such strings with a leading @samp{X}, e.g.,
14182 @samp{test "X@var{string}" != X} rather than @samp{test -n
14183 "@var{string}"} or @samp{test ! "@var{string}"}.
14185 It is common to find variations of the following idiom:
14187 @example
14188 test -n "`echo $ac_feature | sed 's/[-a-zA-Z0-9_]//g'`" &&
14189   @var{action}
14190 @end example
14192 @noindent
14193 to take an action when a token matches a given pattern.  Such constructs
14194 should be avoided by using:
14196 @example
14197 case $ac_feature in
14198   *[!-a-zA-Z0-9_]*) @var{action};;
14199 esac
14200 @end example
14202 If the pattern is a complicated regular expression that cannot be
14203 expressed as a shell pattern, use something like this instead:
14205 @example
14206 expr "X$ac_feature" : 'X.*[^-a-zA-Z0-9_]' >/dev/null &&
14207   @var{action}
14208 @end example
14210 @samp{expr "X@var{foo}" : "X@var{bar}"} is more robust than @samp{echo
14211 "X@var{foo}" | grep "^X@var{bar}"}, because it avoids problems when
14212 @samp{@var{foo}} contains backslashes.
14215 @item @command{trap}
14216 @c -----------------
14217 @prindex @command{trap}
14218 It is safe to trap at least the signals 1, 2, 13, and 15.  You can also
14219 trap 0, i.e., have the @command{trap} run when the script ends (either via an
14220 explicit @command{exit}, or the end of the script).  The trap for 0 should be
14221 installed outside of a shell function, or @acronym{AIX} 5.3 @command{/bin/sh}
14222 will invoke the trap at the end of this function.
14224 Posix says that @samp{trap - 1 2 13 15} resets the traps for the
14225 specified signals to their default values, but many common shells (e.g.,
14226 Solaris @command{/bin/sh}) misinterpret this and attempt to execute a
14227 ``command'' named @command{-} when the specified conditions arise.
14228 There is no portable workaround, except for @samp{trap - 0}, for which
14229 @samp{trap '' 0} is a portable substitute.
14231 Although Posix is not absolutely clear on this point, it is widely
14232 admitted that when entering the trap @samp{$?} should be set to the exit
14233 status of the last command run before the trap.  The ambiguity can be
14234 summarized as: ``when the trap is launched by an @command{exit}, what is
14235 the @emph{last} command run: that before @command{exit}, or
14236 @command{exit} itself?''
14238 Bash considers @command{exit} to be the last command, while Zsh and
14239 Solaris @command{/bin/sh} consider that when the trap is run it is
14240 @emph{still} in the @command{exit}, hence it is the previous exit status
14241 that the trap receives:
14243 @example
14244 $ @kbd{cat trap.sh}
14245 trap 'echo $?' 0
14246 (exit 42); exit 0
14247 $ @kbd{zsh trap.sh}
14249 $ @kbd{bash trap.sh}
14251 @end example
14253 The portable solution is then simple: when you want to @samp{exit 42},
14254 run @samp{(exit 42); exit 42}, the first @command{exit} being used to
14255 set the exit status to 42 for Zsh, and the second to trigger the trap
14256 and pass 42 as exit status for Bash.
14258 The shell in Free@acronym{BSD} 4.0 has the following bug: @samp{$?} is
14259 reset to 0 by empty lines if the code is inside @command{trap}.
14261 @example
14262 $ @kbd{trap 'false}
14264 echo $?' 0
14265 $ @kbd{exit}
14267 @end example
14269 @noindent
14270 Fortunately, this bug only affects @command{trap}.
14272 @item @command{true}
14273 @c -----------------
14274 @prindex @command{true}
14275 @c Info cannot handle `:' in index entries.
14276 @c @prindex @command{:}
14277 Don't worry: as far as we know @command{true} is portable.
14278 Nevertheless, it's not always a builtin (e.g., Bash 1.x), and the
14279 portable shell community tends to prefer using @command{:}.  This has a
14280 funny side effect: when asked whether @command{false} is more portable
14281 than @command{true} Alexandre Oliva answered:
14283 @quotation
14284 In a sense, yes, because if it doesn't exist, the shell will produce an
14285 exit status of failure, which is correct for @command{false}, but not
14286 for @command{true}.
14287 @end quotation
14290 @item @command{unset}
14291 @c ------------------
14292 @prindex @command{unset}
14293 In some nonconforming shells (e.g., Bash 2.05a), @code{unset FOO} fails
14294 when @code{FOO} is not set.  Also, Bash 2.01 mishandles @code{unset
14295 MAIL} in some cases and dumps core.
14297 A few ancient shells lack @command{unset} entirely.  Nevertheless, because
14298 it is extremely useful to disable embarrassing variables such as
14299 @code{PS1}, you can test for its existence and use
14300 it @emph{provided} you give a neutralizing value when @command{unset} is
14301 not supported:
14303 @smallexample
14304 # "|| exit" suppresses any "Segmentation fault" message.
14305 if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
14306   unset=unset
14307 else
14308   unset=false
14310 $unset PS1 || PS1='$ '
14311 @end smallexample
14313 @noindent
14314 @xref{Special Shell Variables}, for some neutralizing values.  Also, see
14315 @ref{Limitations of Builtins}, documentation of @command{export}, for
14316 the case of environment variables.
14317 @end table
14319 @node Limitations of Usual Tools
14320 @section Limitations of Usual Tools
14321 @cindex Limitations of usual tools
14323 The small set of tools you can expect to find on any machine can still
14324 include some limitations you should be aware of.
14326 @table @asis
14327 @item Awk
14328 @c ------
14329 @prindex Awk
14330 Don't leave white space before the opening parenthesis in a user function call.
14331 Posix does not allow this and @acronym{GNU} Awk rejects it:
14333 @example
14334 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
14335         BEGIN @{ die () @}'}
14336 gawk: cmd. line:2:         BEGIN @{ die () @}
14337 gawk: cmd. line:2:                      ^ parse error
14338 $ @kbd{gawk 'function die () @{ print "Aaaaarg!"  @}
14339         BEGIN @{ die() @}'}
14340 Aaaaarg!
14341 @end example
14343 Posix says that if a program contains only @samp{BEGIN} actions, and
14344 contains no instances of @code{getline}, then the program merely
14345 executes the actions without reading input.  However, traditional Awk
14346 implementations (such as Solaris 10 @command{awk}) read and discard
14347 input in this case.  Portable scripts can redirect input from
14348 @file{/dev/null} to work around the problem.  For example:
14350 @example
14351 awk 'BEGIN @{print "hello world"@}' </dev/null
14352 @end example
14354 Posix says that in an @samp{END} action, @samp{$NF} (and presumably,
14355 @samp{$1}) retain their value from the last record read, if no
14356 intervening @samp{getline} occurred.  However, some implementations
14357 (such as Solaris 10 @samp{/usr/bin/awk}, @samp{nawk}, or Darwin
14358 @samp{awk}) reset these variables.  A workaround is to use an
14359 intermediate variable prior to the @samp{END} block.  For example:
14361 @example
14362 $ @kbd{cat end.awk}
14363 @{ tmp = $1 @}
14364 END @{ print "a", $1, $NF, "b", tmp @}
14365 $ @kbd{echo 1 | awk -f end.awk}
14366 a   b 1
14367 $ @kbd{echo 1 | gawk -f end.awk}
14368 a 1 1 b 1
14369 @end example
14371 If you want your program to be deterministic, don't depend on @code{for}
14372 on arrays:
14374 @example
14375 $ @kbd{cat for.awk}
14376 END @{
14377   arr["foo"] = 1
14378   arr["bar"] = 1
14379   for (i in arr)
14380     print i
14382 $ @kbd{gawk -f for.awk </dev/null}
14385 $ @kbd{nawk -f for.awk </dev/null}
14388 @end example
14390 Some Awk implementations, such as @acronym{HP-UX} 11.0's native one,
14391 mishandle anchors:
14393 @example
14394 $ @kbd{echo xfoo | $AWK '/foo|^bar/ @{ print @}'}
14395 $ @kbd{echo bar | $AWK '/foo|^bar/ @{ print @}'}
14397 $ @kbd{echo xfoo | $AWK '/^bar|foo/ @{ print @}'}
14398 xfoo
14399 $ @kbd{echo bar | $AWK '/^bar|foo/ @{ print @}'}
14401 @end example
14403 @noindent
14404 Either do not depend on such patterns (i.e., use @samp{/^(.*foo|bar)/},
14405 or use a simple test to reject such implementations.
14407 @acronym{AIX} version 5.2 has an arbitrary limit of 399 on the
14408 length of regular expressions and literal strings in an Awk program.
14410 Traditional Awk implementations derived from Unix version 7, such as
14411 Solaris @command{/bin/awk}, have many limitations and do not
14412 conform to Posix.  Nowadays @code{AC_PROG_AWK} (@pxref{Particular
14413 Programs}) finds you an Awk that doesn't have these problems, but if
14414 for some reason you prefer not to use @code{AC_PROG_AWK} you may need to
14415 address them.
14417 Traditional Awk does not support multidimensional arrays or user-defined
14418 functions.
14420 Traditional Awk does not support the @option{-v} option.  You can use
14421 assignments after the program instead, e.g., @command{$AWK '@{print v
14422 $1@}' v=x}; however, don't forget that such assignments are not
14423 evaluated until they are encountered (e.g., after any @code{BEGIN}
14424 action).
14426 Traditional Awk does not support the keywords @code{delete} or @code{do}.
14428 Traditional Awk does not support the expressions
14429 @code{@var{a}?@var{b}:@var{c}}, @code{!@var{a}}, @code{@var{a}^@var{b}},
14430 or @code{@var{a}^=@var{b}}.
14432 Traditional Awk does not support the predefined @code{CONVFMT} variable.
14434 Traditional Awk supports only the predefined functions @code{exp}, @code{index},
14435 @code{int}, @code{length}, @code{log}, @code{split}, @code{sprintf},
14436 @code{sqrt}, and @code{substr}.
14438 Traditional Awk @code{getline} is not at all compatible with Posix;
14439 avoid it.
14441 Traditional Awk has @code{for (i in a) @dots{}} but no other uses of the
14442 @code{in} keyword.  For example, it lacks @code{if (i in a) @dots{}}.
14444 In code portable to both traditional and modern Awk, @code{FS} must be a
14445 string containing just one ordinary character, and similarly for the
14446 field-separator argument to @code{split}.
14448 Traditional Awk has a limit of 99
14449 fields in a record.  You may be able to circumvent this problem by using
14450 @code{split}.
14452 Traditional Awk has a limit of at most 99 bytes in a number formatted by
14453 @code{OFMT}; for example, @code{OFMT="%.300e"; print 0.1;} typically
14454 dumps core.
14456 The original version of Awk had a limit of at most 99 bytes per
14457 @code{split} field, 99 bytes per @code{substr} substring, and 99 bytes
14458 per run of non-special characters in a @code{printf} format, but these
14459 bugs have been fixed on all practical hosts that we know of.
14461 @item @command{basename}
14462 @c ---------------------
14463 @prindex @command{basename}
14464 Not all hosts have a working @command{basename}.
14465 You can use @command{expr} instead.
14467 @c AS_BASENAME is to be replaced by a better API.
14468 @ignore
14469 Not all hosts have a working @command{basename}, and you should instead
14470 use @code{AS_BASENAME} (@pxref{Programming in M4sh}), followed by
14471 @command{expr} if you need to strip a suffix.  For example:
14473 @example
14474 a=`basename "$aname"`       # This is not portable.
14475 a=`AS_BASENAME(["$aname"])` # This is more portable.
14477 # This is not portable.
14478 c=`basename "$cname" .c`
14480 # This is more portable.
14481 c=`AS_BASENAME(["$cname"])`
14482 case $c in
14483 ?*.c) c=`expr "X$c" : 'X\(.*\)\.c'`;;
14484 esac
14485 @end example
14486 @end ignore
14489 @item @command{cat}
14490 @c ----------------
14491 @prindex @command{cat}
14492 Don't rely on any option.
14495 @item @command{cc}
14496 @c ---------------
14497 @prindex @command{cc}
14498 The command @samp{cc -c foo.c} traditionally produces an object file
14499 named @file{foo.o}.  Most compilers allow @option{-c} to be combined
14500 with @option{-o} to specify a different object file name, but
14501 Posix does not require this combination and a few compilers
14502 lack support for it.  @xref{C Compiler}, for how @acronym{GNU} Make
14503 tests for this feature with @code{AC_PROG_CC_C_O}.
14505 When a compilation such as @samp{cc -o foo foo.c} fails, some compilers
14506 (such as @sc{cds} on Reliant Unix) leave a @file{foo.o}.
14508 @acronym{HP-UX} @command{cc} doesn't accept @file{.S} files to preprocess and
14509 assemble.  @samp{cc -c foo.S} appears to succeed, but in fact does
14510 nothing.
14512 The default executable, produced by @samp{cc foo.c}, can be
14514 @itemize
14515 @item @file{a.out} --- usual Posix convention.
14516 @item @file{b.out} --- i960 compilers (including @command{gcc}).
14517 @item @file{a.exe} --- @acronym{DJGPP} port of @command{gcc}.
14518 @item @file{a_out.exe} --- GNV @command{cc} wrapper for DEC C on OpenVMS.
14519 @item @file{foo.exe} --- various MS-DOS compilers.
14520 @end itemize
14522 The C compiler's traditional name is @command{cc}, but other names like
14523 @command{gcc} are common.  Posix 1003.1-2001 specifies the
14524 name @command{c99}, but older Posix editions specified
14525 @command{c89} and anyway these standard names are rarely used in
14526 practice.  Typically the C compiler is invoked from makefiles that use
14527 @samp{$(CC)}, so the value of the @samp{CC} make variable selects the
14528 compiler name.
14531 @item @command{chmod}
14532 @c ------------------
14533 @prindex @command{chmod}
14534 Avoid usages like @samp{chmod -w file}; use @samp{chmod a-w file}
14535 instead, for two reasons.  First, plain @option{-w} does not necessarily
14536 make the file unwritable, since it does not affect mode bits that
14537 correspond to bits in the file mode creation mask.  Second,
14538 Posix says that the @option{-w} might be interpreted as an
14539 implementation-specific option, not as a mode; Posix suggests
14540 using @samp{chmod -- -w file} to avoid this confusion, but unfortunately
14541 @samp{--} does not work on some older hosts.
14544 @item @command{cmp}
14545 @c ----------------
14546 @prindex @command{cmp}
14547 @command{cmp} performs a raw data comparison of two files, while
14548 @command{diff} compares two text files.  Therefore, if you might compare
14549 DOS files, even if only checking whether two files are different, use
14550 @command{diff} to avoid spurious differences due to differences of
14551 newline encoding.
14554 @item @command{cp}
14555 @c ---------------
14556 @prindex @command{cp}
14557 Avoid the @option{-r} option, since Posix 1003.1-2004 marks it as
14558 obsolescent and its behavior on special files is implementation-defined.
14559 Use @option{-R} instead.  On @acronym{GNU} hosts the two options
14560 are equivalent, but on Solaris hosts (for example) @command{cp -r}
14561 reads from pipes instead of replicating them.
14563 Some @command{cp} implementations (e.g., @acronym{BSD/OS} 4.2) do not allow
14564 trailing slashes at the end of nonexistent destination directories.  To
14565 avoid this problem, omit the trailing slashes.  For example, use
14566 @samp{cp -R source /tmp/newdir} rather than @samp{cp -R source
14567 /tmp/newdir/} if @file{/tmp/newdir} does not exist.
14569 @c This is thanks to Ian.
14570 The ancient SunOS 4 @command{cp} does not support @option{-f}, although
14571 its @command{mv} does.
14573 @cindex timestamp resolution
14574 Traditionally, file timestamps had 1-second resolution, and @samp{cp
14575 -p} copied the timestamps exactly.  However, many modern file systems
14576 have timestamps with 1-nanosecond resolution.  Unfortunately, @samp{cp
14577 -p} implementations truncate timestamps when copying files, so this
14578 can result in the destination file appearing to be older than the
14579 source.  The exact amount of truncation depends on the resolution of
14580 the system calls that @command{cp} uses; traditionally this was
14581 @code{utime}, which has 1-second resolution, but some newer
14582 @command{cp} implementations use @code{utimes}, which has
14583 1-microsecond resolution.  These newer implementations include @acronym{GNU}
14584 Core Utilities 5.0.91 or later, and Solaris 8 (sparc) patch 109933-02 or
14585 later.  Unfortunately as of January 2006 there is still no system
14586 call to set timestamps to the full nanosecond resolution.
14588 Bob Proulx notes that @samp{cp -p} always @emph{tries} to copy
14589 ownerships.  But whether it actually does copy ownerships or not is a
14590 system dependent policy decision implemented by the kernel.  If the
14591 kernel allows it then it happens.  If the kernel does not allow it then
14592 it does not happen.  It is not something @command{cp} itself has control
14593 over.
14595 In Unix System V any user can chown files to any other user, and System
14596 V also has a non-sticky @file{/tmp}.  That probably derives from the
14597 heritage of System V in a business environment without hostile users.
14598 @acronym{BSD} changed this
14599 to be a more secure model where only root can @command{chown} files and
14600 a sticky @file{/tmp} is used.  That undoubtedly derives from the heritage
14601 of @acronym{BSD} in a campus environment.
14603 @acronym{GNU}/Linux and Solaris by default follow @acronym{BSD}, but
14604 can be configured to allow a System V style @command{chown}.  On the
14605 other hand, @acronym{HP-UX} follows System V, but can
14606 be configured to use the modern security model and disallow
14607 @command{chown}.  Since it is an administrator-configurable parameter
14608 you can't use the name of the kernel as an indicator of the behavior.
14612 @item @command{date}
14613 @c -----------------
14614 @prindex @command{date}
14615 Some versions of @command{date} do not recognize special @samp{%} directives,
14616 and unfortunately, instead of complaining, they just pass them through,
14617 and exit with success:
14619 @example
14620 $ @kbd{uname -a}
14621 OSF1 medusa.sis.pasteur.fr V5.1 732 alpha
14622 $ @kbd{date "+%s"}
14624 @end example
14627 @item @command{diff}
14628 @c -----------------
14629 @prindex @command{diff}
14630 Option @option{-u} is nonportable.
14632 Some implementations, such as Tru64's, fail when comparing to
14633 @file{/dev/null}.  Use an empty file instead.
14636 @item @command{dirname}
14637 @c --------------------
14638 @prindex @command{dirname}
14639 Not all hosts have a working @command{dirname}, and you should instead
14640 use @code{AS_DIRNAME} (@pxref{Programming in M4sh}).  For example:
14642 @example
14643 dir=`dirname "$file"`       # This is not portable.
14644 dir=`AS_DIRNAME(["$file"])` # This is more portable.
14645 @end example
14648 @item @command{egrep}
14649 @c ------------------
14650 @prindex @command{egrep}
14651 Posix 1003.1-2001 no longer requires @command{egrep},
14652 but many hosts do not yet support the Posix
14653 replacement @code{grep -E}.  Also, some traditional implementations do
14654 not work on long input lines.  To work around these problems, invoke
14655 @code{AC_PROG_EGREP} and then use @code{$EGREP}.
14657 Portable extended regular expressions should use @samp{\} only to escape
14658 characters in the string @samp{$()*+.?[\^@{|}.  For example, @samp{\@}}
14659 is not portable, even though it typically matches @samp{@}}.
14661 The empty alternative is not portable.  Use @samp{?} instead.  For
14662 instance with Digital Unix v5.0:
14664 @example
14665 > printf "foo\n|foo\n" | $EGREP '^(|foo|bar)$'
14666 |foo
14667 > printf "bar\nbar|\n" | $EGREP '^(foo|bar|)$'
14668 bar|
14669 > printf "foo\nfoo|\n|bar\nbar\n" | $EGREP '^(foo||bar)$'
14671 |bar
14672 @end example
14674 @command{$EGREP} also suffers the limitations of @command{grep}.
14676 @item @command{expr}
14677 @c -----------------
14678 @prindex @command{expr}
14679 No @command{expr} keyword starts with @samp{X}, so use @samp{expr
14680 X"@var{word}" : 'X@var{regex}'} to keep @command{expr} from
14681 misinterpreting @var{word}.
14683 Don't use @code{length}, @code{substr}, @code{match} and @code{index}.
14685 @item @command{expr} (@samp{|})
14686 @prindex @command{expr} (@samp{|})
14687 You can use @samp{|}.  Although Posix does require that @samp{expr
14688 ''} return the empty string, it does not specify the result when you
14689 @samp{|} together the empty string (or zero) with the empty string.  For
14690 example:
14692 @example
14693 expr '' \| ''
14694 @end example
14696 Posix 1003.2-1992 returns the empty string
14697 for this case, but traditional Unix returns @samp{0} (Solaris is
14698 one such example).  In Posix 1003.1-2001, the specification was
14699 changed to match traditional Unix's behavior (which is
14700 bizarre, but it's too late to fix this).  Please note that the same
14701 problem does arise when the empty string results from a computation,
14702 as in:
14704 @example
14705 expr bar : foo \| foo : bar
14706 @end example
14708 @noindent
14709 Avoid this portability problem by avoiding the empty string.
14712 @item @command{expr} (@samp{:})
14713 @c ----------------------------
14714 @prindex @command{expr}
14715 Portable @command{expr} regular expressions should use @samp{\} to
14716 escape only characters in the string @samp{$()*.0123456789[\^n@{@}}.
14717 For example, alternation, @samp{\|}, is common but Posix does not
14718 require its support, so it should be avoided in portable scripts.
14719 Similarly, @samp{\+} and @samp{\?} should be avoided.
14721 Portable @command{expr} regular expressions should not begin with
14722 @samp{^}.  Patterns are automatically anchored so leading @samp{^} is
14723 not needed anyway.
14725 The Posix standard is ambiguous as to whether
14726 @samp{expr 'a' : '\(b\)'} outputs @samp{0} or the empty string.
14727 In practice, it outputs the empty string on most platforms, but portable
14728 scripts should not assume this.  For instance, the @acronym{QNX} 4.25 native
14729 @command{expr} returns @samp{0}.
14731 One might think that a way to get a uniform behavior would be to use
14732 the empty string as a default value:
14734 @example
14735 expr a : '\(b\)' \| ''
14736 @end example
14738 @noindent
14739 Unfortunately this behaves exactly as the original expression; see the
14740 @command{expr} (@samp{|}) entry for more information.
14742 Some ancient @command{expr} implementations (e.g., SunOS 4 @command{expr} and
14743 Solaris 8 @command{/usr/ucb/expr}) have a silly length limit that causes
14744 @command{expr} to fail if the matched substring is longer than 120
14745 bytes.  In this case, you might want to fall back on @samp{echo|sed} if
14746 @command{expr} fails.  Nowadays this is of practical importance only for
14747 the rare installer who mistakenly puts @file{/usr/ucb} before
14748 @file{/usr/bin} in @env{PATH}.
14750 On Mac OS X 10.4, @command{expr} mishandles the pattern @samp{[^-]} in
14751 some cases.  For example, the command
14752 @example
14753 expr Xpowerpc-apple-darwin8.1.0 : 'X[^-]*-[^-]*-\(.*\)'
14754 @end example
14756 @noindent
14757 outputs @samp{apple-darwin8.1.0} rather than the correct @samp{darwin8.1.0}.
14758 This particular case can be worked around by substituting @samp{[^--]}
14759 for @samp{[^-]}.
14761 Don't leave, there is some more!
14763 The @acronym{QNX} 4.25 @command{expr}, in addition of preferring @samp{0} to
14764 the empty string, has a funny behavior in its exit status: it's always 1
14765 when parentheses are used!
14767 @example
14768 $ @kbd{val=`expr 'a' : 'a'`; echo "$?: $val"}
14769 0: 1
14770 $ @kbd{val=`expr 'a' : 'b'`; echo "$?: $val"}
14771 1: 0
14773 $ @kbd{val=`expr 'a' : '\(a\)'`; echo "?: $val"}
14774 1: a
14775 $ @kbd{val=`expr 'a' : '\(b\)'`; echo "?: $val"}
14776 1: 0
14777 @end example
14779 @noindent
14780 In practice this can be a big problem if you are ready to catch failures
14781 of @command{expr} programs with some other method (such as using
14782 @command{sed}), since you may get twice the result.  For instance
14784 @example
14785 $ @kbd{expr 'a' : '\(a\)' || echo 'a' | sed 's/^\(a\)$/\1/'}
14786 @end example
14788 @noindent
14789 outputs @samp{a} on most hosts, but @samp{aa} on @acronym{QNX} 4.25.  A
14790 simple workaround consists of testing @command{expr} and using a variable
14791 set to @command{expr} or to @command{false} according to the result.
14793 Tru64 @command{expr} incorrectly treats the result as a number, if it
14794 can be interpreted that way:
14796 @example
14797 $ @kbd{expr 00001 : '.*\(...\)'}
14799 @end example
14802 @item @command{fgrep}
14803 @c ------------------
14804 @prindex @command{fgrep}
14805 Posix 1003.1-2001 no longer requires @command{fgrep},
14806 but many hosts do not yet support the Posix
14807 replacement @code{grep -F}.  Also, some traditional implementations do
14808 not work on long input lines.  To work around these problems, invoke
14809 @code{AC_PROG_FGREP} and then use @code{$FGREP}.
14812 @item @command{find}
14813 @c -----------------
14814 @prindex @command{find}
14815 The option @option{-maxdepth} seems to be @acronym{GNU} specific.
14816 Tru64 v5.1, Net@acronym{BSD} 1.5 and Solaris @command{find}
14817 commands do not understand it.
14819 The replacement of @samp{@{@}} is guaranteed only if the argument is
14820 exactly @emph{@{@}}, not if it's only a part of an argument.  For
14821 instance on DU, and @acronym{HP-UX} 10.20 and @acronym{HP-UX} 11:
14823 @example
14824 $ @kbd{touch foo}
14825 $ @kbd{find . -name foo -exec echo "@{@}-@{@}" \;}
14826 @{@}-@{@}
14827 @end example
14829 @noindent
14830 while @acronym{GNU} @command{find} reports @samp{./foo-./foo}.
14833 @item @command{grep}
14834 @c -----------------
14835 @prindex @command{grep}
14836 Portable scripts can rely on the @command{grep} options @option{-c},
14837 @option{-l}, @option{-n}, and @option{-v}, but should avoid other
14838 options.  For example, don't use @option{-w}, as Posix does not require
14839 it and Irix 6.5.16m's @command{grep} does not support it.  Also,
14840 portable scripts should not combine @option{-c} with @option{-l},
14841 as Posix does not allow this.
14843 Some of the options required by Posix are not portable in practice.
14844 Don't use @samp{grep -q} to suppress output, because many @command{grep}
14845 implementations (e.g., Solaris) do not support @option{-q}.
14846 Don't use @samp{grep -s} to suppress output either, because Posix
14847 says @option{-s} does not suppress output, only some error messages;
14848 also, the @option{-s} option of traditional @command{grep} behaved
14849 like @option{-q} does in most modern implementations.  Instead,
14850 redirect the standard output and standard error (in case the file
14851 doesn't exist) of @code{grep} to @file{/dev/null}.  Check the exit
14852 status of @code{grep} to determine whether it found a match.
14854 Some traditional @command{grep} implementations do not work on long
14855 input lines.  On AIX the default @code{grep} silently truncates long
14856 lines on the input before matching.
14858 Also, many implementations do not support multiple regexps
14859 with @option{-e}: they either reject @option{-e} entirely (e.g., Solaris)
14860 or honor only the last pattern (e.g., @acronym{IRIX} 6.5 and NeXT).  To
14861 work around these problems, invoke @code{AC_PROG_GREP} and then use
14862 @code{$GREP}.
14864 Another possible workaround for the multiple @option{-e} problem is to
14865 separate the patterns by newlines, for example:
14867 @example
14868 grep 'foo
14869 bar' in.txt
14870 @end example
14872 @noindent
14873 except that this fails with traditional @command{grep}
14874 implementations and with Open@acronym{BSD} 3.8 @command{grep}.
14876 Traditional @command{grep} implementations (e.g., Solaris) do not
14877 support the @option{-E} or @option{-F} options.  To work around these
14878 problems, invoke @code{AC_PROG_EGREP} and then use @code{$EGREP}, and
14879 similarly for @code{AC_PROG_FGREP} and @code{$FGREP}.  Even if you are
14880 willing to require support for Posix @command{grep}, your script should
14881 not use both @option{-E} and @option{-F}, since Posix does not allow
14882 this combination.
14884 Portable @command{grep} regular expressions should use @samp{\} only to
14885 escape characters in the string @samp{$()*.0123456789[\^@{@}}.  For example,
14886 alternation, @samp{\|}, is common but Posix does not require its
14887 support in basic regular expressions, so it should be avoided in
14888 portable scripts.  Solaris and HP-UX @command{grep} do not support it.
14889 Similarly, the following escape sequences should also be avoided:
14890 @samp{\<}, @samp{\>}, @samp{\+}, @samp{\?}, @samp{\`}, @samp{\'},
14891 @samp{\B}, @samp{\b}, @samp{\S}, @samp{\s}, @samp{\W}, and @samp{\w}.
14894 @item @command{join}
14895 @c -----------------
14896 @prindex @command{join}
14897 Solaris 8 @command{join} has bugs when the second operand is standard
14898 input, and when standard input is a pipe.  For example, the following
14899 shell script causes Solaris 8 @command{join} to loop forever:
14901 @example
14902 cat >file <<'EOF'
14903 1 x
14904 2 y
14906 cat file | join file -
14907 @end example
14909 Use @samp{join - file} instead.
14912 @item @command{ln}
14913 @c ---------------
14914 @prindex @command{ln}
14915 @cindex Symbolic links
14916 Don't rely on @command{ln} having a @option{-f} option.  Symbolic links
14917 are not available on old systems; use @samp{$(LN_S)} as a portable substitute.
14919 For versions of the @acronym{DJGPP} before 2.04,
14920 @command{ln} emulates symbolic links
14921 to executables by generating a stub that in turn calls the real
14922 program.  This feature also works with nonexistent files like in the
14923 Posix spec.  So @samp{ln -s file link} generates @file{link.exe},
14924 which attempts to call @file{file.exe} if run.  But this feature only
14925 works for executables, so @samp{cp -p} is used instead for these
14926 systems.  @acronym{DJGPP} versions 2.04 and later have full support
14927 for symbolic links.
14930 @item @command{ls}
14931 @c ---------------
14932 @prindex @command{ls}
14933 @cindex Listing directories
14934 The portable options are @option{-acdilrtu}.  Current practice is for
14935 @option{-l} to output both owner and group, even though ancient versions
14936 of @command{ls} omitted the group.
14938 On ancient hosts, @samp{ls foo} sent the diagnostic @samp{foo not found}
14939 to standard output if @file{foo} did not exist.  Hence a shell command
14940 like @samp{sources=`ls *.c 2>/dev/null`} did not always work, since it
14941 was equivalent to @samp{sources='*.c not found'} in the absence of
14942 @samp{.c} files.  This is no longer a practical problem, since current
14943 @command{ls} implementations send diagnostics to standard error.
14945 @item @command{mkdir}
14946 @c ------------------
14947 @prindex @command{mkdir}
14948 @cindex Making directories
14949 No @command{mkdir} option is portable to older systems.  Instead of
14950 @samp{mkdir -p @var{file-name}}, you should use
14951 @code{AS_MKDIR_P(@var{file-name})} (@pxref{Programming in M4sh})
14952 or @code{AC_PROG_MKDIR_P} (@pxref{Particular Programs}).
14954 Combining the @option{-m} and @option{-p} options, as in @samp{mkdir -m
14955 go-w -p @var{dir}}, often leads to trouble.  Free@acronym{BSD}
14956 @command{mkdir} incorrectly attempts to change the permissions of
14957 @var{dir} even if it already exists.  @acronym{HP-UX} 11.23 and
14958 @acronym{IRIX} 6.5 @command{mkdir} often assign the wrong permissions to
14959 any newly-created parents of @var{dir}.
14961 Posix does not clearly specify whether @samp{mkdir -p foo}
14962 should succeed when @file{foo} is a symbolic link to an already-existing
14963 directory.  The @acronym{GNU} Core Utilities 5.1.0 @command{mkdir}
14964 succeeds, but Solaris @command{mkdir} fails.
14966 Traditional @code{mkdir -p} implementations suffer from race conditions.
14967 For example, if you invoke @code{mkdir -p a/b} and @code{mkdir -p a/c}
14968 at the same time, both processes might detect that @file{a} is missing,
14969 one might create @file{a}, then the other might try to create @file{a}
14970 and fail with a @code{File exists} diagnostic.  The @acronym{GNU} Core
14971 Utilities (@samp{fileutils} version 4.1), Free@acronym{BSD} 5.0,
14972 Net@acronym{BSD} 2.0.2, and Open@acronym{BSD} 2.4 are known to be
14973 race-free when two processes invoke @code{mkdir -p} simultaneously, but
14974 earlier versions are vulnerable.  Solaris @command{mkdir} is still
14975 vulnerable as of Solaris 10, and other traditional Unix systems are
14976 probably vulnerable too.  This possible race is harmful in parallel
14977 builds when several Make rules call @code{mkdir -p} to
14978 construct directories.  You may use
14979 @code{install-sh -d} as a safe replacement, provided this script is
14980 recent enough; the copy shipped with Autoconf 2.60 and Automake 1.10 is
14981 OK, but copies from older versions are vulnerable.
14984 @item @command{mktemp}
14985 @c -------------------
14986 @prindex @command{mktemp}
14987 @cindex Creating temporary files
14988 Shell scripts can use temporary files safely with @command{mktemp}, but
14989 it does not exist on all systems.  A portable way to create a safe
14990 temporary file name is to create a temporary directory with mode 700 and
14991 use a file inside this directory.  Both methods prevent attackers from
14992 gaining control, though @command{mktemp} is far less likely to fail
14993 gratuitously under attack.
14995 Here is sample code to create a new temporary directory safely:
14997 @example
14998 # Create a temporary directory $tmp in $TMPDIR (default /tmp).
14999 # Use mktemp if possible; otherwise fall back on mkdir,
15000 # with $RANDOM to make collisions less likely.
15001 : $@{TMPDIR=/tmp@}
15003   tmp=`
15004     (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
15005   ` &&
15006   test -n "$tmp" && test -d "$tmp"
15007 @} || @{
15008   tmp=$TMPDIR/foo$$-$RANDOM
15009   (umask 077 && mkdir "$tmp")
15010 @} || exit $?
15011 @end example
15014 @item @command{mv}
15015 @c ---------------
15016 @prindex @command{mv}
15017 @cindex Moving open files
15018 The only portable options are @option{-f} and @option{-i}.
15020 Moving individual files between file systems is portable (it was in Unix
15021 version 6),
15022 but it is not always atomic: when doing @samp{mv new existing}, there's
15023 a critical section where neither the old nor the new version of
15024 @file{existing} actually exists.
15026 On some systems moving files from @file{/tmp} can sometimes cause
15027 undesirable (but perfectly valid) warnings, even if you created these
15028 files.  This is because @file{/tmp} belongs to a group that ordinary
15029 users are not members of, and files created in @file{/tmp} inherit
15030 the group of @file{/tmp}.  When the file is copied, @command{mv} issues
15031 a diagnostic without failing:
15033 @smallexample
15034 $ @kbd{touch /tmp/foo}
15035 $ @kbd{mv /tmp/foo .}
15036 @error{}mv: ./foo: set owner/group (was: 100/0): Operation not permitted
15037 $ @kbd{echo $?}
15039 $ @kbd{ls foo}
15041 @end smallexample
15043 @noindent
15044 This annoying behavior conforms to Posix, unfortunately.
15046 Moving directories across mount points is not portable, use @command{cp}
15047 and @command{rm}.
15049 @acronym{DOS} variants cannot rename or remove open files, and do not
15050 support commands like @samp{mv foo bar >foo}, even though this is
15051 perfectly portable among Posix hosts.
15054 @item @command{od}
15055 @c ---------------
15056 @prindex @command{od}
15058 In Mac OS X 10.3, @command{od} does not support the
15059 standard Posix options @option{-A}, @option{-j}, @option{-N}, or
15060 @option{-t}, or the @acronym{XSI} option @option{-s}.  The only
15061 supported Posix option is @option{-v}, and the only supported
15062 @acronym{XSI} options are those in @option{-bcdox}.  The @acronym{BSD}
15063 @command{hexdump} program can be used instead.
15065 This problem no longer exists in Mac OS X 10.4.3.
15068 @item @command{rm}
15069 @c ---------------
15070 @prindex @command{rm}
15071 The @option{-f} and @option{-r} options are portable.
15073 It is not portable to invoke @command{rm} without operands.  For
15074 example, on many systems @samp{rm -f -r} (with no other arguments)
15075 silently succeeds without doing anything, but it fails with a diagnostic
15076 on Net@acronym{BSD} 2.0.2.
15078 A file might not be removed even if its parent directory is writable
15079 and searchable.  Many Posix hosts cannot remove a mount point, a named
15080 stream, a working directory, or a last link to a file that is being
15081 executed.
15083 @acronym{DOS} variants cannot rename or remove open files, and do not
15084 support commands like @samp{rm foo >foo}, even though this is
15085 perfectly portable among Posix hosts.
15088 @item @command{sed}
15089 @c ----------------
15090 @prindex @command{sed}
15091 Patterns should not include the separator (unless escaped), even as part
15092 of a character class.  In conformance with Posix, the Cray
15093 @command{sed} rejects @samp{s/[^/]*$//}: use @samp{s,[^/]*$,,}.
15095 Avoid empty patterns within parentheses (i.e., @samp{\(\)}).  Posix does
15096 not require support for empty patterns, and Unicos 9 @command{sed} rejects
15097 them.
15099 Unicos 9 @command{sed} loops endlessly on patterns like @samp{.*\n.*}.
15101 Sed scripts should not use branch labels longer than 7 characters and
15102 should not contain comments.  @acronym{HP-UX} sed has a limit of 99 commands
15103 (not counting @samp{:} commands) and
15104 48 labels, which can not be circumvented by using more than one script
15105 file.  It can execute up to 19 reads with the @samp{r} command per cycle.
15106 Solaris @command{/usr/ucb/sed} rejects usages that exceed an limit of
15107 about 6000 bytes for the internal representation of commands.
15109 Avoid redundant @samp{;}, as some @command{sed} implementations, such as
15110 Net@acronym{BSD} 1.4.2's, incorrectly try to interpret the second
15111 @samp{;} as a command:
15113 @example
15114 $ @kbd{echo a | sed 's/x/x/;;s/x/x/'}
15115 sed: 1: "s/x/x/;;s/x/x/": invalid command code ;
15116 @end example
15118 Input should not have unreasonably long lines, since some @command{sed}
15119 implementations have an input buffer limited to 4000 bytes.
15121 Portable @command{sed} regular expressions should use @samp{\} only to escape
15122 characters in the string @samp{$()*.0123456789[\^n@{@}}.  For example,
15123 alternation, @samp{\|}, is common but Posix does not require its
15124 support, so it should be avoided in portable scripts.  Solaris
15125 @command{sed} does not support alternation; e.g., @samp{sed '/a\|b/d'}
15126 deletes only lines that contain the literal string @samp{a|b}.
15127 Similarly, @samp{\+} and @samp{\?} should be avoided.
15129 Anchors (@samp{^} and @samp{$}) inside groups are not portable.
15131 Nested parentheses in patterns (e.g., @samp{\(\(a*\)b*)\)}) are
15132 quite portable to current hosts, but was not supported by some ancient
15133 @command{sed} implementations like SVR3.
15135 Some @command{sed} implementations, e.g., Solaris,
15136 restrict the special role of the asterisk to one-character regular expressions.
15137 This may lead to unexpected behavior:
15139 @example
15140 $ @kbd{echo '1*23*4' | /usr/bin/sed 's/\(.\)*/x/g'}
15141 x2x4
15142 $ @kbd{echo '1*23*4' | /usr/xpg4/bin/sed 's/\(.\)*/x/g'}
15144 @end example
15146 The @option{-e} option is mostly portable.
15147 However, its argument
15148 cannot start with @samp{a}, @samp{c}, or @samp{i},
15149 as this runs afoul of a Tru64 5.1 bug.
15150 Also, its argument cannot be empty, as this fails on @acronym{AIX} 5.3.
15151 Some people prefer to use @samp{-e}:
15153 @example
15154 sed -e '@var{command-1}' \
15155     -e '@var{command-2}'
15156 @end example
15158 @noindent
15159 as opposed to the equivalent:
15161 @example
15162 sed '
15163   @var{command-1}
15164   @var{command-2}
15166 @end example
15168 @noindent
15169 The following usage is sometimes equivalent:
15171 @example
15172 sed '@var{command-1};@var{command-2}'
15173 @end example
15175 but Posix says that this use of a semicolon has undefined effect if
15176 @var{command-1}'s verb is @samp{@{}, @samp{a}, @samp{b}, @samp{c},
15177 @samp{i}, @samp{r}, @samp{t}, @samp{w}, @samp{:}, or @samp{#}, so you
15178 should use semicolon only with simple scripts that do not use these
15179 verbs.
15181 Commands inside @{ @} brackets are further restricted.  Posix says that
15182 they cannot be preceded by addresses, @samp{!}, or @samp{;}, and that
15183 each command must be followed immediately by a newline, without any
15184 intervening blanks or semicolons.  The closing bracket must be alone on
15185 a line, other than white space preceding or following it.
15187 Contrary to yet another urban legend, you may portably use @samp{&} in
15188 the replacement part of the @code{s} command to mean ``what was
15189 matched''.  All descendants of Unix version 7 @command{sed}
15190 (at least; we
15191 don't have first hand experience with older @command{sed} implementations) have
15192 supported it.
15194 Posix requires that you must not have any white space between
15195 @samp{!} and the following command.  It is OK to have blanks between
15196 the address and the @samp{!}.  For instance, on Solaris:
15198 @example
15199 $ @kbd{echo "foo" | sed -n '/bar/ ! p'}
15200 @error{}Unrecognized command: /bar/ ! p
15201 $ @kbd{echo "foo" | sed -n '/bar/! p'}
15202 @error{}Unrecognized command: /bar/! p
15203 $ @kbd{echo "foo" | sed -n '/bar/ !p'}
15205 @end example
15207 Posix also says that you should not combine @samp{!} and @samp{;}.  If
15208 you use @samp{!}, it is best to put it on a command that is delimited by
15209 newlines rather than @samp{;}.
15211 Also note that Posix requires that the @samp{b}, @samp{t}, @samp{r}, and
15212 @samp{w} commands be followed by exactly one space before their argument.
15213 On the other hand, no white space is allowed between @samp{:} and the
15214 subsequent label name.
15216 If a sed script is specified on the command line and ends in an
15217 @samp{a}, @samp{c}, or @samp{i} command, the last line of inserted text
15218 should be followed by a newline.  Otherwise some @command{sed}
15219 implementations (e.g., Open@acronym{BSD} 3.9) do not append a newline to the
15220 inserted text.
15222 Many @command{sed} implementations (e.g., MacOS X 10.4,
15223 Open@acronym{BSD} 3.9, Solaris 10
15224 @command{/usr/ucb/sed}) strip leading white space from the text of
15225 @samp{a}, @samp{c}, and @samp{i} commands.  Prepend a backslash to
15226 work around this incompatibility with Posix:
15228 @example
15229 $ @kbd{echo flushleft | sed 'a\}
15230 > @kbd{   indented}
15231 > @kbd{'}
15232 flushleft
15233 indented
15234 $ @kbd{echo foo | sed 'a\}
15235 > @kbd{\   indented}
15236 > @kbd{'}
15237 flushleft
15238    indented
15239 @end example
15242 @item @command{sed} (@samp{t})
15243 @c ---------------------------
15244 @prindex @command{sed} (@samp{t})
15245 Some old systems have @command{sed} that ``forget'' to reset their
15246 @samp{t} flag when starting a new cycle.  For instance on @acronym{MIPS
15247 RISC/OS}, and on @sc{irix} 5.3, if you run the following @command{sed}
15248 script (the line numbers are not actual part of the texts):
15250 @example
15251 s/keep me/kept/g  # a
15252 t end             # b
15253 s/.*/deleted/g    # c
15254 :end              # d
15255 @end example
15257 @noindent
15260 @example
15261 delete me         # 1
15262 delete me         # 2
15263 keep me           # 3
15264 delete me         # 4
15265 @end example
15267 @noindent
15268 you get
15270 @example
15271 deleted
15272 delete me
15273 kept
15274 deleted
15275 @end example
15277 @noindent
15278 instead of
15280 @example
15281 deleted
15282 deleted
15283 kept
15284 deleted
15285 @end example
15287 Why?  When processing line 1, (c) matches, therefore sets the @samp{t}
15288 flag, and the output is produced.  When processing
15289 line 2, the @samp{t} flag is still set (this is the bug).  Command (a)
15290 fails to match, but @command{sed} is not supposed to clear the @samp{t}
15291 flag when a substitution fails.  Command (b) sees that the flag is set,
15292 therefore it clears it, and jumps to (d), hence you get @samp{delete me}
15293 instead of @samp{deleted}.  When processing line (3), @samp{t} is clear,
15294 (a) matches, so the flag is set, hence (b) clears the flags and jumps.
15295 Finally, since the flag is clear, line 4 is processed properly.
15297 There are two things one should remember about @samp{t} in @command{sed}.
15298 Firstly, always remember that @samp{t} jumps if @emph{some} substitution
15299 succeeded, not only the immediately preceding substitution.  Therefore,
15300 always use a fake @samp{t clear} followed by a @samp{:clear} on the next
15301 line, to reset the @samp{t} flag where needed.
15303 Secondly, you cannot rely on @command{sed} to clear the flag at each new
15304 cycle.
15306 One portable implementation of the script above is:
15308 @example
15309 t clear
15310 :clear
15311 s/keep me/kept/g
15312 t end
15313 s/.*/deleted/g
15314 :end
15315 @end example
15317 @item @command{touch}
15318 @c ------------------
15319 @prindex @command{touch}
15320 @cindex timestamp resolution
15321 If you specify the desired timestamp (e.g., with the @option{-r}
15322 option), @command{touch} typically uses the @code{utime} or
15323 @code{utimes} system call, which can result in the same kind of
15324 timestamp truncation problems that @samp{cp -p} has.
15326 On ancient @acronym{BSD} systems, @command{touch} or any command that
15327 results in an empty file does not update the timestamps, so use a
15328 command like @command{echo} as a workaround.
15329 Also,
15330 @acronym{GNU} @command{touch} 3.16r (and presumably all before that)
15331 fails to work on SunOS 4.1.3 when the empty file is on an
15332 @acronym{NFS}-mounted 4.2 volume.
15333 However, these problems are no longer of practical concern.
15335 @end table
15338 @node Portable Make
15339 @chapter Portable Make Programming
15340 @prindex @command{make}
15341 @cindex Limitations of @command{make}
15343 Writing portable makefiles is an art.  Since a makefile's commands are
15344 executed by the shell, you must consider the shell portability issues
15345 already mentioned.  However, other issues are specific to @command{make}
15346 itself.
15348 @menu
15349 * $< in Ordinary Make Rules::   $< in ordinary rules
15350 * Failure in Make Rules::       Failing portably in rules
15351 * Special Chars in Names::      Special Characters in Macro Names
15352 * Backslash-Newline-Newline::   Empty last lines in macro definitions
15353 * Backslash-Newline Comments::  Spanning comments across line boundaries
15354 * Long Lines in Makefiles::     Line length limitations
15355 * Macros and Submakes::         @code{make macro=value} and submakes
15356 * The Make Macro MAKEFLAGS::    @code{$(MAKEFLAGS)} portability issues
15357 * The Make Macro SHELL::        @code{$(SHELL)} portability issues
15358 * Comments in Make Rules::      Other problems with Make comments
15359 * obj/ and Make::               Don't name a subdirectory @file{obj}
15360 * make -k Status::              Exit status of @samp{make -k}
15361 * VPATH and Make::              @code{VPATH} woes
15362 * Single Suffix Rules::         Single suffix rules and separated dependencies
15363 * Timestamps and Make::         Subsecond timestamp resolution
15364 @end menu
15366 @node $< in Ordinary Make Rules
15367 @section @code{$<} in Ordinary Make Rules
15369 Posix says that the @samp{$<} construct in makefiles can be
15370 used only in inference rules and in the @samp{.DEFAULT} rule; its
15371 meaning in ordinary rules is unspecified.  Solaris @command{make}
15372 for instance replaces it with the empty string.  Open@acronym{BSD} (3.0 and
15373 later) @command{make} diagnoses these uses and errors out.
15375 @node Failure in Make Rules
15376 @section Failure in Make Rules
15378 Since 1992 Posix has required that @command{make} must invoke
15379 each command with the equivalent of a @samp{sh -c} subshell.  However,
15380 many @command{make} implementations, including @acronym{BSD} make through 2004,
15381 use @samp{sh -e -c} instead, and the @option{-e} option causes the
15382 subshell to exit immediately if a subsidiary simple-command fails.  For
15383 example, the command @samp{touch T; rm -f U} always attempts to
15384 remove @file{U} with Posix make, but incompatible
15385 @command{make} implementations skip the @command{rm} if the
15386 @command{touch} fails.  One way to work around this is to reword the
15387 affected simple-commands so that they always succeed, e.g., @samp{touch
15388 T || :; rm -f U}.
15389 However, even this approach can run into common bugs in @acronym{BSD}
15390 implementations of the @option{-e} option of @command{sh} and
15391 @command{set} (@pxref{Limitations of Builtins}), so if you are worried
15392 about porting to buggy @acronym{BSD} shells it may be simpler to migrate
15393 complicated @command{make} actions into separate scripts.
15395 @node Special Chars in Names
15396 @section Special Characters in Make Macro Names
15398 Posix limits macro names to nonempty strings containing only
15399 @acronym{ASCII} letters and digits, @samp{.}, and @samp{_}.  Many
15400 @command{make} implementations allow a wider variety of characters, but
15401 portable makefiles should avoid them.  It is portable to start a name
15402 with a special character, e.g., @samp{$(.FOO)}.
15404 Some ancient @command{make} implementations don't support leading
15405 underscores in macro names.  An example is @acronym{NEWS-OS} 4.2R.
15407 @example
15408 $ @kbd{cat Makefile}
15409 _am_include = #
15410 _am_quote =
15411 all:; @@echo this is test
15412 $ @kbd{make}
15413 Make: Must be a separator on rules line 2.  Stop.
15414 $ @kbd{cat Makefile2}
15415 am_include = #
15416 am_quote =
15417 all:; @@echo this is test
15418 $ @kbd{make -f Makefile2}
15419 this is test
15420 @end example
15422 @noindent
15423 However, this problem is no longer of practical concern.
15425 @node Backslash-Newline-Newline
15426 @section Backslash-Newline-Newline in Make Macro Values
15428 @c  This has been seen on ia64 hpux 11.20, and on one hppa hpux 10.20,
15429 @c  but another hppa hpux 10.20 didn't have it.  Bob Proulx
15430 @c  <bob@proulx.com> thinks it was in hpux 8.0 too.
15431 On some versions of @acronym{HP-UX}, @command{make} reads multiple newlines
15432 following a backslash, continuing to the next non-empty line.  For
15433 example,
15435 @example
15436 FOO = one \
15438 BAR = two
15440 test:
15441         : FOO is "$(FOO)"
15442         : BAR is "$(BAR)"
15443 @end example
15445 @noindent
15446 shows @code{FOO} equal to @code{one BAR = two}.  Other implementations
15447 sensibly let a backslash continue only to the immediately following
15448 line.
15450 @node Backslash-Newline Comments
15451 @section Backslash-Newline in Make Comments
15453 According to Posix, Make comments start with @code{#}
15454 and continue until an unescaped newline is reached.
15456 @example
15457 $ @kbd{cat Makefile}
15458 # A = foo \
15459       bar \
15460       baz
15462 all:
15463         @@echo ok
15464 $ @kbd{make}   # GNU make
15466 @end example
15468 @noindent
15469 However this is not always the case.  Some implementations
15470 discard everything from @code{#} through the end of the line, ignoring any
15471 trailing backslash.
15473 @example
15474 $ @kbd{pmake}  # BSD make
15475 "Makefile", line 3: Need an operator
15476 Fatal errors encountered -- cannot continue
15477 @end example
15479 @noindent
15480 Therefore, if you want to comment out a multi-line definition, prefix each
15481 line with @code{#}, not only the first.
15483 @example
15484 # A = foo \
15485 #     bar \
15486 #     baz
15487 @end example
15489 @node Long Lines in Makefiles
15490 @section Long Lines in Makefiles
15492 Tru64 5.1's @command{make} has been reported to crash when given a
15493 makefile with lines longer than around 20 kB.  Earlier versions are
15494 reported to exit with @code{Line too long} diagnostics.
15496 @node Macros and Submakes
15497 @section @code{make macro=value} and Submakes
15499 A command-line variable definition such as @code{foo=bar} overrides any
15500 definition of @code{foo} in a makefile.  Some @command{make}
15501 implementations (such as @acronym{GNU} @command{make}) propagate this
15502 override to subsidiary invocations of @command{make}.  Some other
15503 implementations do not pass the substitution along to submakes.
15505 @example
15506 $ @kbd{cat Makefile}
15507 foo = foo
15508 one:
15509         @@echo $(foo)
15510         $(MAKE) two
15511 two:
15512         @@echo $(foo)
15513 $ @kbd{make foo=bar}            # GNU make 3.79.1
15515 make two
15516 make[1]: Entering directory `/home/adl'
15518 make[1]: Leaving directory `/home/adl'
15519 $ @kbd{pmake foo=bar}           # BSD make
15521 pmake two
15523 @end example
15525 You have a few possibilities if you do want the @code{foo=bar} override
15526 to propagate to submakes.  One is to use the @option{-e}
15527 option, which causes all environment variables to have precedence over
15528 the makefile macro definitions, and declare foo as an environment
15529 variable:
15531 @example
15532 $ @kbd{env foo=bar make -e}
15533 @end example
15535 The @option{-e} option is propagated to submakes automatically,
15536 and since the environment is inherited between @command{make}
15537 invocations, the @code{foo} macro is overridden in
15538 submakes as expected.
15540 This syntax (@code{foo=bar make -e}) is portable only when used
15541 outside of a makefile, for instance from a script or from the
15542 command line.  When run inside a @command{make} rule, @acronym{GNU}
15543 @command{make} 3.80 and prior versions forget to propagate the
15544 @option{-e} option to submakes.
15546 Moreover, using @option{-e} could have unexpected side effects if your
15547 environment contains some other macros usually defined by the
15548 makefile.  (See also the note about @code{make -e} and @code{SHELL}
15549 below.)
15551 Another way to propagate overrides to submakes is to do it
15552 manually, from your makefile:
15554 @example
15555 foo = foo
15556 one:
15557         @@echo $(foo)
15558         $(MAKE) foo=$(foo) two
15559 two:
15560         @@echo $(foo)
15561 @end example
15563 You need to foresee all macros that a user might want to override if
15564 you do that.
15566 @node The Make Macro MAKEFLAGS
15567 @section The Make Macro MAKEFLAGS
15568 @cindex @code{MAKEFLAGS} and @command{make}
15569 @cindex @command{make} and @code{MAKEFLAGS}
15571 Posix requires @command{make} to use @code{MAKEFLAGS} to affect the
15572 current and recursive invocations of make, but allows implementations
15573 several formats for the variable.  It is tricky to parse
15574 @code{$MAKEFLAGS} to determine whether @option{-s} for silent execution
15575 or @option{-k} for continued execution are in effect.  For example, you
15576 cannot assume that the first space-separated word in @code{$MAKEFLAGS}
15577 contains single-letter options, since in the Cygwin version of
15578 @acronym{GNU} @command{make} it is either @option{--unix} or
15579 @option{--win32} with the second word containing single-letter options.
15581 @example
15582 $ @kbd{cat Makefile}
15583 all:
15584         @@echo MAKEFLAGS = $(MAKEFLAGS)
15585 $ @kbd{make}
15586 MAKEFLAGS = --unix
15587 $ @kbd{make -k}
15588 MAKEFLAGS = --unix -k
15589 @end example
15591 @node The Make Macro SHELL
15592 @section The Make Macro @code{SHELL}
15593 @cindex @code{SHELL} and @command{make}
15594 @cindex @command{make} and @code{SHELL}
15596 Posix-compliant @command{make} internally uses the @code{$(SHELL)}
15597 macro to spawn shell processes and execute Make rules.  This
15598 is a builtin macro supplied by @command{make}, but it can be modified
15599 by a makefile or by a command-line argument.
15601 Not all @command{make} implementations define this @code{SHELL} macro.
15602 Tru64
15603 @command{make} is an example; this implementation always uses
15604 @code{/bin/sh}.  So it's a good idea to always define @code{SHELL} in
15605 your makefiles.  If you use Autoconf, do
15607 @example
15608 SHELL = @@SHELL@@
15609 @end example
15611 Do not force @code{SHELL = /bin/sh} because that is not correct
15612 everywhere.  For instance @acronym{DJGPP} lacks @code{/bin/sh}, and when
15613 its @acronym{GNU} @code{make} port sees such a setting it enters a special
15614 emulation mode where features like pipes and redirections are emulated
15615 on top of DOS's @command{command.com}.  Unfortunately this emulation is
15616 incomplete; for instance it does not handle command substitutions.
15617 On @acronym{DJGPP} @code{SHELL} should point to Bash.
15619 Posix-compliant @command{make} should never acquire the value of
15620 $(SHELL) from the environment, even when @code{make -e} is used
15621 (otherwise, think about what would happen to your rules if
15622 @code{SHELL=/bin/tcsh}).
15624 However not all @command{make} implementations have this exception.
15625 For instance it's not surprising that Tru64 @command{make} doesn't
15626 protect @code{SHELL}, since it doesn't use it.
15628 @example
15629 $ @kbd{cat Makefile}
15630 SHELL = /bin/sh
15631 FOO = foo
15632 all:
15633         @@echo $(SHELL)
15634         @@echo $(FOO)
15635 $ @kbd{env SHELL=/bin/tcsh FOO=bar make -e}   # Tru64 Make
15636 /bin/tcsh
15638 $ @kbd{env SHELL=/bin/tcsh FOO=bar gmake -e}  # GNU make
15639 /bin/sh
15641 @end example
15643 @node Comments in Make Rules
15644 @section Comments in Make Rules
15645 @cindex Comments in @file{Makefile} rules
15646 @cindex @file{Makefile} rules and comments
15648 Never put comments in a rule.
15650 Some @command{make} treat anything starting with a tab as a command for
15651 the current rule, even if the tab is immediately followed by a @code{#}.
15652 The @command{make} from Tru64 Unix V5.1 is one of them.  The following
15653 makefile runs @code{# foo} through the shell.
15655 @example
15656 all:
15657         # foo
15658 @end example
15660 @node obj/ and Make
15661 @section The @file{obj/} Subdirectory and Make
15662 @cindex @file{obj/}, subdirectory
15663 @cindex @acronym{BSD} @command{make} and @file{obj/}
15665 Never name one of your subdirectories @file{obj/} if you don't like
15666 surprises.
15668 If an @file{obj/} directory exists, @acronym{BSD} @command{make} enters it
15669 before reading the makefile.  Hence the makefile in the
15670 current directory is not read.
15672 @example
15673 $ @kbd{cat Makefile}
15674 all:
15675         echo Hello
15676 $ @kbd{cat obj/Makefile}
15677 all:
15678         echo World
15679 $ @kbd{make}      # GNU make
15680 echo Hello
15681 Hello
15682 $ @kbd{pmake}     # BSD make
15683 echo World
15684 World
15685 @end example
15687 @node make -k Status
15688 @section Exit Status of @code{make -k}
15689 @cindex @code{make -k}
15691 Do not rely on the exit status of @code{make -k}.  Some implementations
15692 reflect whether they encountered an error in their exit status; other
15693 implementations always succeed.
15695 @example
15696 $ @kbd{cat Makefile}
15697 all:
15698         false
15699 $ @kbd{make -k; echo exit status: $?}    # GNU make
15700 false
15701 make: *** [all] Error 1
15702 exit status: 2
15703 $ @kbd{pmake -k; echo exit status: $?}   # BSD make
15704 false
15705 *** Error code 1 (continuing)
15706 exit status: 0
15707 @end example
15709 @node VPATH and Make
15710 @section @code{VPATH} and Make
15711 @cindex @code{VPATH}
15713 Posix does not specify the semantics of @code{VPATH}.  Typically,
15714 @command{make} supports @code{VPATH}, but its implementation is not
15715 consistent.
15717 Autoconf and Automake support makefiles whose usages of @code{VPATH} are
15718 portable to recent-enough popular implementations of @command{make}, but
15719 to keep the resulting makefiles portable, a package's makefile
15720 prototypes must take the following issues into account.  These issues
15721 are complicated and are often poorly understood, and installers who use
15722 @code{VPATH} should expect to find many bugs in this area.  If you use
15723 @code{VPATH}, the simplest way to avoid these portability bugs is to
15724 stick with @acronym{GNU} @command{make}, since it is the most
15725 commonly-used @command{make} among Autoconf users.
15727 Here are some known issues with some @code{VPATH}
15728 implementations.
15730 @menu
15731 * VPATH and Double-colon::      Problems with @samp{::} on ancient hosts
15732 * $< in Explicit Rules::        @code{$<} does not work in ordinary rules
15733 * Automatic Rule Rewriting::    @code{VPATH} goes wild on Solaris
15734 * Tru64 Directory Magic::       @command{mkdir} goes wild on Tru64
15735 * Make Target Lookup::          More details about @code{VPATH} lookup
15736 @end menu
15738 @node VPATH and Double-colon
15739 @subsection @code{VPATH} and Double-colon Rules
15740 @cindex @code{VPATH} and double-colon rules
15741 @cindex double-colon rules and @code{VPATH}
15743 With ancient versions of Sun @command{make},
15744 any assignment to @code{VPATH} causes @command{make} to execute only
15745 the first set of double-colon rules.
15746 However, this problem is no longer of practical concern.
15748 @node $< in Explicit Rules
15749 @subsection @code{$<} Not Supported in Explicit Rules
15750 @cindex explicit rules, @code{$<}, and @code{VPATH}
15751 @cindex @code{$<}, explicit rules, and @code{VPATH}
15752 @cindex @code{VPATH}, explicit rules, and @code{$<}
15754 Using @code{$<} in explicit rules is not portable.
15755 The prerequisite file must be named explicitly in the rule.  If you want
15756 to find the prerequisite via a @code{VPATH} search, you have to code the
15757 whole thing manually.  @xref{Build Directories}.
15759 @node Automatic Rule Rewriting
15760 @subsection Automatic Rule Rewriting
15761 @cindex @code{VPATH} and automatic rule rewriting
15762 @cindex automatic rule rewriting and @code{VPATH}
15764 Some @command{make} implementations, such as Solaris and Tru64,
15765 search for prerequisites in @code{VPATH} and
15766 then rewrite each occurrence as a plain word in the rule.
15767 For instance:
15769 @example
15770 # This isn't portable to GNU make.
15771 VPATH = ../pkg/src
15772 f.c: if.c
15773         cp if.c f.c
15774 @end example
15776 @noindent
15777 executes @code{cp ../pkg/src/if.c f.c} if @file{if.c} is
15778 found in @file{../pkg/src}.
15780 However, this rule leads to real problems in practice.  For example, if
15781 the source directory contains an ordinary file named @file{test} that is
15782 used in a dependency, Solaris @command{make} rewrites commands like
15783 @samp{if test -r foo; @dots{}} to @samp{if ../pkg/src/test -r foo;
15784 @dots{}}, which is typically undesirable.  To avoid this problem,
15785 portable makefiles should never mention a source file whose name is that
15786 of a shell keyword like @file{until} or a shell command like
15787 @command{cat} or @command{gcc} or @command{test}.
15789 Because of these problems @acronym{GNU} @command{make} and many other
15790 @command{make} implementations do not rewrite commands, so portable
15791 makefiles should
15792 search @code{VPATH} manually.  It is tempting to write this:
15794 @smallexample
15795 # This isn't portable to Solaris make.
15796 VPATH = ../pkg/src
15797 f.c: if.c
15798         cp `test -f if.c || echo $(VPATH)/`if.c f.c
15799 @end smallexample
15801 @noindent
15802 However, the ``prerequisite rewriting'' still applies here.  So if
15803 @file{if.c} is in @file{../pkg/src}, Solaris and Tru64 @command{make}
15804 execute
15806 @smallexample
15807 cp `test -f ../pkg/src/if.c || echo ../pkg/src/`if.c f.c
15808 @end smallexample
15810 @noindent
15811 which reduces to
15813 @example
15814 cp if.c f.c
15815 @end example
15817 @noindent
15818 and thus fails.  Oops.
15820 A simple workaround, and good practice anyway, is to use @samp{$?} and
15821 @samp{$@@} when possible:
15823 @smallexample
15824 VPATH = ../pkg/src
15825 f.c: if.c
15826         cp $? $@@
15827 @end smallexample
15829 @noindent
15830 but this does not generalize well to commands with multiple
15831 prerequisites.  A more general workaround is to rewrite the rule so that
15832 the prerequisite @file{if.c} never appears as a plain word.  For
15833 example, these three rules would be safe, assuming @file{if.c} is in
15834 @file{../pkg/src} and the other files are in the working directory:
15836 @smallexample
15837 VPATH = ../pkg/src
15838 f.c: if.c f1.c
15839         cat `test -f ./if.c || echo $(VPATH)/`if.c f1.c >$@@
15840 g.c: if.c g1.c
15841         cat `test -f 'if.c' || echo $(VPATH)/`if.c g1.c >$@@
15842 h.c: if.c h1.c
15843         cat `test -f "if.c" || echo $(VPATH)/`if.c h1.c >$@@
15844 @end smallexample
15846 Things get worse when your prerequisites are in a macro.
15848 @example
15849 VPATH = ../pkg/src
15850 HEADERS = f.h g.h h.h
15851 install-HEADERS: $(HEADERS)
15852         for i in $(HEADERS); do \
15853           $(INSTALL) -m 644 \
15854             `test -f $$i || echo $(VPATH)/`$$i \
15855             $(DESTDIR)$(includedir)/$$i; \
15856         done
15857 @end example
15859 The above @code{install-HEADERS} rule is not Solaris-proof because @code{for
15860 i in $(HEADERS);} is expanded to @code{for i in f.h g.h h.h;}
15861 where @code{f.h} and @code{g.h} are plain words and are hence
15862 subject to @code{VPATH} adjustments.
15864 If the three files are in @file{../pkg/src}, the rule is run as:
15866 @example
15867 for i in ../pkg/src/f.h ../pkg/src/g.h h.h; do \
15868   install -m 644 \
15869      `test -f $i || echo ../pkg/src/`$i \
15870      /usr/local/include/$i; \
15871 done
15872 @end example
15874 where the two first @command{install} calls fail.  For instance,
15875 consider the @code{f.h} installation:
15877 @example
15878 install -m 644 \
15879   `test -f ../pkg/src/f.h || \
15880     echo ../pkg/src/ \
15881   `../pkg/src/f.h \
15882   /usr/local/include/../pkg/src/f.h;
15883 @end example
15885 @noindent
15886 It reduces to:
15888 @example
15889 install -m 644 \
15890   ../pkg/src/f.h \
15891   /usr/local/include/../pkg/src/f.h;
15892 @end example
15894 Note that the manual @code{VPATH} search did not cause any problems here;
15895 however this command installs @file{f.h} in an incorrect directory.
15897 Trying to quote @code{$(HEADERS)} in some way, as we did for
15898 @code{foo.c} a few makefiles ago, does not help:
15900 @example
15901 install-HEADERS: $(HEADERS)
15902         headers='$(HEADERS)'; \
15903         for i in $$headers; do \
15904           $(INSTALL) -m 644 \
15905             `test -f $$i || echo $(VPATH)/`$$i \
15906             $(DESTDIR)$(includedir)/$$i; \
15907         done
15908 @end example
15910 Now, @code{headers='$(HEADERS)'} macro-expands to:
15912 @example
15913 headers='f.h g.h h.h'
15914 @end example
15916 @noindent
15917 but @code{g.h} is still a plain word.  (As an aside, the idiom
15918 @code{headers='$(HEADERS)'; for i in $$headers;} is a good
15919 idea if @code{$(HEADERS)} can be empty, because some shells diagnose a
15920 syntax error on @code{for i in;}.)
15922 One workaround is to strip this unwanted @file{../pkg/src/} prefix manually:
15924 @example
15925 VPATH = ../pkg/src
15926 HEADERS = f.h g.h h.h
15927 install-HEADERS: $(HEADERS)
15928         headers='$(HEADERS)'; \
15929         for i in $$headers; do \
15930           i=`expr "$$i" : '$(VPATH)/\(.*\)'`;
15931           $(INSTALL) -m 644 \
15932             `test -f $$i || echo $(VPATH)/`$$i \
15933             $(DESTDIR)$(includedir)/$$i; \
15934         done
15935 @end example
15937 Automake does something similar.  However the above hack works only if
15938 the files listed in @code{HEADERS} are in the current directory or a
15939 subdirectory; they should not be in an enclosing directory.  If we had
15940 @code{HEADERS = ../f.h}, the above fragment would fail in a VPATH
15941 build with Tru64 @command{make}.  The reason is that not only does
15942 Tru64 @command{make} rewrite dependencies, but it also simplifies
15943 them.  Hence @code{../f.h} becomes @code{../pkg/f.h} instead of
15944 @code{../pkg/src/../f.h}.  This obviously defeats any attempt to strip
15945 a leading @file{../pkg/src/} component.
15947 The following example makes the behavior of Tru64 @command{make}
15948 more apparent.
15950 @example
15951 $ @kbd{cat Makefile}
15952 VPATH = sub
15953 all: ../foo
15954         echo ../foo
15955 $ @kbd{ls}
15956 Makefile foo
15957 $ @kbd{make}
15958 echo foo
15960 @end example
15962 @noindent
15963 Dependency @file{../foo} was found in @file{sub/../foo}, but Tru64
15964 @command{make} simplified it as @file{foo}.  (Note that the @file{sub/}
15965 directory does not even exist, this just means that the simplification
15966 occurred before the file was checked for.)
15968 For the record here is how SunOS 4 @command{make} behaves on this
15969 example.
15971 @smallexample
15972 $ @kbd{make}
15973 make: Fatal error: Don't know how to make target `../foo'
15974 $ @kbd{mkdir sub}
15975 $ @kbd{make}
15976 echo sub/../foo
15977 sub/../foo
15978 @end smallexample
15981 @node Tru64 Directory Magic
15982 @subsection Tru64 @command{make} Creates Prerequisite Directories Magically
15983 @cindex @code{VPATH} and prerequisite directories
15984 @cindex prerequisite directories and @code{VPATH}
15986 When a prerequisite is a subdirectory of @code{VPATH}, Tru64
15987 @command{make} creates it in the current directory.
15989 @example
15990 $ @kbd{mkdir -p foo/bar build}
15991 $ @kbd{cd build}
15992 $ @kbd{cat >Makefile <<END
15993 VPATH = ..
15994 all: foo/bar
15995 END}
15996 $ @kbd{make}
15997 mkdir foo
15998 mkdir foo/bar
15999 @end example
16001 This can yield unexpected results if a rule uses a manual @code{VPATH}
16002 search as presented before.
16004 @example
16005 VPATH = ..
16006 all : foo/bar
16007         command `test -d foo/bar || echo ../`foo/bar
16008 @end example
16010 The above @command{command} is run on the empty @file{foo/bar}
16011 directory that was created in the current directory.
16013 @node Make Target Lookup
16014 @subsection Make Target Lookup
16015 @cindex @code{VPATH}, resolving target pathnames
16017 @acronym{GNU} @command{make} uses a complex algorithm to decide when it
16018 should use files found via a @code{VPATH} search.  @xref{Search
16019 Algorithm, , How Directory Searches are Performed, make, The @acronym{GNU} Make
16020 Manual}.
16022 If a target needs to be rebuilt, @acronym{GNU} @command{make} discards the
16023 file name found during the @code{VPATH} search for this target, and
16024 builds the file locally using the file name given in the makefile.
16025 If a target does not need to be rebuilt, @acronym{GNU} @command{make} uses the
16026 file name found during the @code{VPATH} search.
16028 Other @command{make} implementations, like Net@acronym{BSD} @command{make}, are
16029 easier to describe: the file name found during the @code{VPATH} search
16030 is used whether the target needs to be rebuilt or not.  Therefore
16031 new files are created locally, but existing files are updated at their
16032 @code{VPATH} location.
16034 Open@acronym{BSD} and Free@acronym{BSD} @command{make}, however,
16035 never perform a
16036 @code{VPATH} search for a dependency that has an explicit rule.
16037 This is extremely annoying.
16039 When attempting a @code{VPATH} build for an autoconfiscated package
16040 (e.g., @code{mkdir build && cd build && ../configure}), this means
16041 @acronym{GNU}
16042 @command{make} builds everything locally in the @file{build}
16043 directory, while @acronym{BSD} @command{make} builds new files locally and
16044 updates existing files in the source directory.
16046 @example
16047 $ @kbd{cat Makefile}
16048 VPATH = ..
16049 all: foo.x bar.x
16050 foo.x bar.x: newer.x
16051         @@echo Building $@@
16052 $ @kbd{touch ../bar.x}
16053 $ @kbd{touch ../newer.x}
16054 $ @kbd{make}        # GNU make
16055 Building foo.x
16056 Building bar.x
16057 $ @kbd{pmake}       # NetBSD make
16058 Building foo.x
16059 Building ../bar.x
16060 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
16061 Building foo.x
16062 Building bar.x
16063 $ @kbd{tmake}       # Tru64 make
16064 Building foo.x
16065 Building bar.x
16066 $ @kbd{touch ../bar.x}
16067 $ @kbd{make}        # GNU make
16068 Building foo.x
16069 $ @kbd{pmake}       # NetBSD make
16070 Building foo.x
16071 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
16072 Building foo.x
16073 Building bar.x
16074 $ @kbd{tmake}       # Tru64 make
16075 Building foo.x
16076 Building bar.x
16077 @end example
16079 Note how Net@acronym{BSD} @command{make} updates @file{../bar.x} in its
16080 VPATH location, and how Free@acronym{BSD}, Open@acronym{BSD}, and Tru64
16081 @command{make} always
16082 update @file{bar.x}, even when @file{../bar.x} is up to date.
16084 Another point worth mentioning is that once @acronym{GNU} @command{make} has
16085 decided to ignore a @code{VPATH} file name (e.g., it ignored
16086 @file{../bar.x} in the above example) it continues to ignore it when
16087 the target occurs as a prerequisite of another rule.
16089 The following example shows that @acronym{GNU} @command{make} does not look up
16090 @file{bar.x} in @code{VPATH} before performing the @code{.x.y} rule,
16091 because it ignored the @code{VPATH} result of @file{bar.x} while running
16092 the @code{bar.x: newer.x} rule.
16094 @example
16095 $ @kbd{cat Makefile}
16096 VPATH = ..
16097 all: bar.y
16098 bar.x: newer.x
16099         @@echo Building $@@
16100 .SUFFIXES: .x .y
16101 .x.y:
16102         cp $< $@@
16103 $ @kbd{touch ../bar.x}
16104 $ @kbd{touch ../newer.x}
16105 $ @kbd{make}        # GNU make
16106 Building bar.x
16107 cp bar.x bar.y
16108 cp: cannot stat `bar.x': No such file or directory
16109 make: *** [bar.y] Error 1
16110 $ @kbd{pmake}       # NetBSD make
16111 Building ../bar.x
16112 cp ../bar.x bar.y
16113 $ @kbd{rm bar.y}
16114 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
16115 echo Building bar.x
16116 cp bar.x bar.y
16117 cp: cannot stat `bar.x': No such file or directory
16118 *** Error code 1
16119 $ @kbd{tmake}       # Tru64 make
16120 Building bar.x
16121 cp: bar.x: No such file or directory
16122 *** Exit 1
16123 @end example
16125 Note that if you drop away the command from the @code{bar.x: newer.x}
16126 rule, @acronym{GNU} @command{make} magically starts to work: it
16127 knows that @code{bar.x} hasn't been updated, therefore it doesn't
16128 discard the result from @code{VPATH} (@file{../bar.x}) in succeeding
16129 uses.  Tru64 also works, but Free@acronym{BSD} and Open@acronym{BSD}
16130 still don't.
16132 @example
16133 $ @kbd{cat Makefile}
16134 VPATH = ..
16135 all: bar.y
16136 bar.x: newer.x
16137 .SUFFIXES: .x .y
16138 .x.y:
16139         cp $< $@@
16140 $ @kbd{touch ../bar.x}
16141 $ @kbd{touch ../newer.x}
16142 $ @kbd{make}        # GNU make
16143 cp ../bar.x bar.y
16144 $ @kbd{rm bar.y}
16145 $ @kbd{pmake}       # NetBSD make
16146 cp ../bar.x bar.y
16147 $ @kbd{rm bar.y}
16148 $ @kbd{fmake}       # FreeBSD make, OpenBSD make
16149 cp bar.x bar.y
16150 cp: cannot stat `bar.x': No such file or directory
16151 *** Error code 1
16152 $ @kbd{tmake}       # Tru64 make
16153 cp ../bar.x bar.y
16154 @end example
16156 It seems the sole solution that would please every @command{make}
16157 implementation is to never rely on @code{VPATH} searches for targets.
16158 In other words, @code{VPATH} should be reserved to unbuilt sources.
16161 @node Single Suffix Rules
16162 @section Single Suffix Rules and Separated Dependencies
16163 @cindex Single Suffix Inference Rule
16164 @cindex Rule, Single Suffix Inference
16165 A @dfn{Single Suffix Rule} is basically a usual suffix (inference) rule
16166 (@samp{.from.to:}), but which @emph{destination} suffix is empty
16167 (@samp{.from:}).
16169 @cindex Separated Dependencies
16170 @dfn{Separated dependencies} simply refers to listing the prerequisite
16171 of a target, without defining a rule.  Usually one can list on the one
16172 hand side, the rules, and on the other hand side, the dependencies.
16174 Solaris @command{make} does not support separated dependencies for
16175 targets defined by single suffix rules:
16177 @example
16178 $ @kbd{cat Makefile}
16179 .SUFFIXES: .in
16180 foo: foo.in
16181 .in:
16182         cp $< $@@
16183 $ @kbd{touch foo.in}
16184 $ @kbd{make}
16185 $ @kbd{ls}
16186 Makefile  foo.in
16187 @end example
16189 @noindent
16190 while @acronym{GNU} Make does:
16192 @example
16193 $ @kbd{gmake}
16194 cp foo.in foo
16195 $ @kbd{ls}
16196 Makefile  foo       foo.in
16197 @end example
16199 Note it works without the @samp{foo: foo.in} dependency.
16201 @example
16202 $ @kbd{cat Makefile}
16203 .SUFFIXES: .in
16204 .in:
16205         cp $< $@@
16206 $ @kbd{make foo}
16207 cp foo.in foo
16208 @end example
16210 @noindent
16211 and it works with double suffix inference rules:
16213 @example
16214 $ @kbd{cat Makefile}
16215 foo.out: foo.in
16216 .SUFFIXES: .in .out
16217 .in.out:
16218         cp $< $@@
16219 $ @kbd{make}
16220 cp foo.in foo.out
16221 @end example
16223 As a result, in such a case, you have to write target rules.
16225 @node Timestamps and Make
16226 @section Timestamp Resolution and Make
16227 @cindex timestamp resolution
16228 Traditionally, file timestamps had 1-second resolution, and
16229 @command{make} used those timestamps to determine whether one file was
16230 newer than the other.  However, many modern file systems have
16231 timestamps with 1-nanosecond resolution.  Some @command{make}
16232 implementations look at the entire timestamp; others ignore the
16233 fractional part, which can lead to incorrect results.  Normally this
16234 is not a problem, but in some extreme cases you may need to use tricks
16235 like @samp{sleep 1} to work around timestamp truncation bugs.
16237 Commands like @samp{cp -p} and @samp{touch -r} typically do not copy
16238 file timestamps to their full resolutions (@pxref{Limitations of Usual
16239 Tools}).  Hence you should be wary of rules like this:
16241 @example
16242 dest: src
16243         cp -p src dest
16244 @end example
16246 as @file{dest} often appears to be older than @file{src} after the
16247 timestamp is truncated, and this can cause @command{make} to do
16248 needless rework the next time it is invoked.  To work around this
16249 problem, you can use a timestamp file, e.g.:
16251 @example
16252 dest-stamp: src
16253         cp -p src dest
16254         date >dest-stamp
16255 @end example
16260 @c ======================================== Portable C and C++ Programming
16262 @node Portable C and C++
16263 @chapter Portable C and C++ Programming
16264 @cindex Portable C and C++ programming
16266 C and C++ programs often use low-level features of the underlying
16267 system, and therefore are often more difficult to make portable to other
16268 platforms.
16270 Several standards have been developed to help make your programs more
16271 portable.  If you write programs with these standards in mind, you can
16272 have greater confidence that your programs work on a wide variety
16273 of systems.  @xref{Standards, , Language Standards Supported by
16274 @acronym{GCC}, gcc, Using the @acronym{GNU} Compiler Collection
16275 (@acronym{GCC})}, for a list of C-related
16276 standards.  Many programs also assume the
16277 @uref{http://www.opengroup.org/susv3, Posix standard}.
16279 Some old code is written to be portable to K&R C, which predates any C
16280 standard.  K&R C compilers are no longer of practical interest, though,
16281 and the rest of section assumes at least C89, the first C standard.
16283 Program portability is a huge topic, and this section can only briefly
16284 introduce common pitfalls.  @xref{System Portability, , Portability
16285 between System Types, standards, @acronym{GNU} Coding Standards}, for
16286 more information.
16288 @menu
16289 * Varieties of Unportability::  How to make your programs unportable
16290 * Integer Overflow::            When integers get too large
16291 * Null Pointers::               Properties of null pointers
16292 * Buffer Overruns::             Subscript errors and the like
16293 * Volatile Objects::            @code{volatile} and signals
16294 * Floating Point Portability::  Portable floating-point arithmetic
16295 * Exiting Portably::            Exiting and the exit status
16296 @end menu
16298 @node Varieties of Unportability
16299 @section Varieties of Unportability
16300 @cindex portability
16302 Autoconf tests and ordinary programs often need to test what is allowed
16303 on a system, and therefore they may need to deliberately exceed the
16304 boundaries of what the standards allow, if only to see whether an
16305 optional feature is present.  When you write such a program, you should
16306 keep in mind the difference between constraints, unspecified behavior,
16307 and undefined behavior.
16309 In C, a @dfn{constraint} is a rule that the compiler must enforce.  An
16310 example constraint is that C programs must not declare a bit-field with
16311 negative width.  Tests can therefore reliably assume that programs with
16312 negative-width bit-fields are rejected by a compiler that conforms
16313 to the standard.
16315 @dfn{Unspecified behavior} is valid behavior, where the standard allows
16316 multiple possibilities.  For example, the order of evaluation of
16317 function arguments is unspecified.  Some unspecified behavior is
16318 @dfn{implementation-defined}, i.e., documented by the implementation,
16319 but since Autoconf tests cannot read the documentation they cannot
16320 distinguish between implementation-defined and other unspecified
16321 behavior.  It is common for Autoconf tests to probe implementations to
16322 determine otherwise-unspecified behavior.
16324 @dfn{Undefined behavior} is invalid behavior, where the standard allows
16325 the implementation to do anything it pleases.  For example,
16326 dereferencing a null pointer leads to undefined behavior.  If possible,
16327 test programs should avoid undefined behavior, since a program with
16328 undefined behavior might succeed on a test that should fail.
16330 The above rules apply to programs that are intended to conform to the
16331 standard.  However, strictly-conforming programs are quite rare, since
16332 the standards are so limiting.  A major goal of Autoconf is to support
16333 programs that use implementation features not described by the standard,
16334 and it is fairly common for test programs to violate the above rules, if
16335 the programs work well enough in practice.
16337 @node Integer Overflow
16338 @section Integer Overflow
16339 @cindex integer overflow
16340 @cindex overflow, signed integer
16341 @cindex signed integer overflow
16342 @cindex wraparound arithmetic
16344 In practice many portable C programs assume that signed integer overflow wraps
16345 around reliably using two's complement arithmetic.  Yet the C standard
16346 says that program behavior is undefined on overflow, and in a few cases
16347 C programs do not work on some modern implementations because their
16348 overflows do not wrap around as their authors expected.  Conversely, in
16349 signed integer remainder, the C standard requires overflow
16350 behavior that is commonly not implemented.
16352 @menu
16353 * Integer Overflow Basics::      Why integer overflow is a problem
16354 * Signed Overflow Examples::     Examples of code assuming wraparound
16355 * Optimization and Wraparound::  Optimizations that break uses of wraparound
16356 * Signed Overflow Advice::       Practical advice for signed overflow issues
16357 * Signed Integer Division::      @code{INT_MIN / -1} and @code{INT_MIN % -1}
16358 @end menu
16360 @node Integer Overflow Basics
16361 @subsection Basics of Integer Overflow
16362 @cindex integer overflow
16363 @cindex overflow, signed integer
16364 @cindex signed integer overflow
16365 @cindex wraparound arithmetic
16367 In languages like C, unsigned integer overflow reliably wraps around;
16368 e.g., @code{UINT_MAX + 1} yields zero.
16369 This is guaranteed by the C standard and is
16370 portable in practice, unless you specify aggressive,
16371 nonstandard optimization options
16372 suitable only for special applications.
16374 In contrast, the C standard says that signed integer overflow leads to
16375 undefined behavior where a program can do anything, including dumping
16376 core or overrunning a buffer.  The misbehavior can even precede the
16377 overflow.  Such an overflow can occur during addition, subtraction,
16378 multiplication, division, and left shift.
16380 Despite this requirement of the standard, many C programs and Autoconf
16381 tests assume that signed integer overflow silently wraps around modulo a
16382 power of two, using two's complement arithmetic, so long as you cast the
16383 resulting value to a signed integer type or store it into a signed
16384 integer variable.  If you use conservative optimization flags, such
16385 programs are generally portable to the vast majority of modern
16386 platforms, with a few exceptions discussed later.
16388 For historical reasons the C standard also allows implementations with
16389 ones' complement or signed magnitude arithmetic, but it is safe to
16390 assume two's complement nowadays.
16392 Also, overflow can occur when converting an out-of-range value to a
16393 signed integer type.  Here a standard implementation must define what
16394 happens, but this might include raising an exception.  In practice all
16395 known implementations support silent wraparound in this case, so you need
16396 not worry about other possibilities.
16398 @node Signed Overflow Examples
16399 @subsection Examples of Code Assuming Wraparound Overflow
16400 @cindex integer overflow
16401 @cindex overflow, signed integer
16402 @cindex signed integer overflow
16403 @cindex wraparound arithmetic
16405 There has long been a tension between what the C standard requires for
16406 signed integer overflow, and what C programs commonly assume.  The
16407 standard allows aggressive optimizations based on assumptions that
16408 overflow never occurs, but many practical C programs rely on overflow
16409 wrapping around.  These programs do not conform to the standard, but
16410 they commonly work in practice because compiler writers are
16411 understandably reluctant to implement optimizations that would break
16412 many programs, unless perhaps a user specifies aggressive optimization.
16414 The C Standard says that if a program has signed integer overflow its
16415 behavior is undefined, and the undefined behavior can even precede the
16416 overflow.  To take an extreme example:
16418 @c Inspired by Robert Dewar's example in
16419 @c <http://gcc.gnu.org/ml/gcc/2007-01/msg00038.html> (2007-01-01).
16420 @example
16421 if (password == expected_password)
16422   allow_superuser_privileges ();
16423 else if (counter++ == INT_MAX)
16424   abort ();
16425 else
16426   printf ("%d password mismatches\n", counter);
16427 @end example
16429 @noindent
16430 If the @code{int} variable @code{counter} equals @code{INT_MAX},
16431 @code{counter++} must overflow and the behavior is undefined, so the C
16432 standard allows the compiler to optimize away the test against
16433 @code{INT_MAX} and the @code{abort} call.
16434 Worse, if an earlier bug in the program lets the compiler deduce that
16435 @code{counter == INT_MAX} or that @code{counter} previously overflowed,
16436 the C standard allows the compiler to optimize away the password test
16437 and generate code that allows superuser privileges unconditionally.
16439 Despite this requirement by the standard, it has long been common for C
16440 code to assume wraparound arithmetic after signed overflow, and all
16441 known practical C implementations support some C idioms that assume
16442 wraparound signed arithmetic, even if the idioms do not conform
16443 strictly to the standard.  If your code looks like the following
16444 examples it will almost surely work with real-world compilers.
16446 Here is an example derived from the 7th Edition Unix implementation of
16447 @code{atoi} (1979-01-10):
16449 @example
16450 char *p;
16451 int f, n;
16452 @dots{}
16453 while (*p >= '0' && *p <= '9')
16454   n = n * 10 + *p++ - '0';
16455 return (f ? -n : n);
16456 @end example
16458 @noindent
16459 Even if the input string is in range, on most modern machines this has
16460 signed overflow when computing the most negative integer (the @code{-n}
16461 overflows) or a value near an extreme integer (the first @code{+}
16462 overflows).
16464 Here is another example, derived from the 7th Edition implementation of
16465 @code{rand} (1979-01-10).  Here the programmer expects both
16466 multiplication and addition to wrap on overflow:
16468 @example
16469 static long int randx = 1;
16470 @dots{}
16471 randx = randx * 1103515245 + 12345;
16472 return (randx >> 16) & 077777;
16473 @end example
16475 In the following example, derived from the @acronym{GNU} C Library 2.5
16476 implementation of @code{mktime} (2006-09-09), the code assumes
16477 wraparound arithmetic in @code{+} to detect signed overflow:
16479 @example
16480 time_t t, t1, t2;
16481 int sec_requested, sec_adjustment;
16482 @dots{}
16483 t1 = t + sec_requested;
16484 t2 = t1 + sec_adjustment;
16485 if (((t1 < t) != (sec_requested < 0))
16486     | ((t2 < t1) != (sec_adjustment < 0)))
16487   return -1;
16488 @end example
16490 If your code looks like these examples, it is probably safe even though
16491 it does not strictly conform to the C standard.  This might lead one to
16492 believe that one can generally assume wraparound on overflow, but that
16493 is not always true, as can be seen in the next section.
16495 @node Optimization and Wraparound
16496 @subsection Optimizations That Break Wraparound Arithmetic
16497 @cindex loop induction
16499 Compilers sometimes generate code that is incompatible with wraparound
16500 integer arithmetic.  A simple example is an algebraic simplification: a
16501 compiler might translate @code{(i * 2000) / 1000} to @code{i * 2}
16502 because it assumes that @code{i * 2000} does not overflow.  The
16503 translation is not equivalent to the original when overflow occurs:
16504 e.g., in the typical case of 32-bit signed two's complement wraparound
16505 @code{int}, if @code{i} has type @code{int} and value @code{1073742},
16506 the original expression returns @minus{}2147483 but the optimized
16507 version returns the mathematically correct value 2147484.
16509 More subtly, loop induction optimizations often exploit the undefined
16510 behavior of signed overflow.  Consider the following contrived function
16511 @code{sumc}:
16513 @example
16515 sumc (int lo, int hi)
16517   int sum = 0;
16518   int i;
16519   for (i = lo; i <= hi; i++)
16520     sum ^= i * 53;
16521   return sum;
16523 @end example
16525 @noindent
16526 To avoid multiplying by 53 each time through the loop, an optimizing
16527 compiler might internally transform @code{sumc} to the equivalent of the
16528 following:
16530 @example
16532 transformed_sumc (int lo, int hi)
16534   int sum = 0;
16535   int hic = hi * 53;
16536   int ic;
16537   for (ic = lo * 53; ic <= hic; ic += 53)
16538     sum ^= ic;
16539   return sum;
16541 @end example
16543 @noindent
16544 This transformation is allowed by the C standard, but it is invalid for
16545 wraparound arithmetic when @code{INT_MAX / 53 < hi}, because then the
16546 overflow in computing expressions like @code{hi * 53} can cause the
16547 expression @code{i <= hi} to yield a different value from the
16548 transformed expression @code{ic <= hic}.
16550 For this reason, compilers that use loop induction and similar
16551 techniques often do not support reliable wraparound arithmetic when a
16552 loop induction variable like @code{ic} is involved.  Since loop
16553 induction variables are generated by the compiler, and are not visible
16554 in the source code, it is not always trivial to say whether the problem
16555 affects your code.
16557 Hardly any code actually depends on wraparound arithmetic in cases like
16558 these, so in practice these loop induction optimizations are almost
16559 always useful.  However, edge cases in this area can cause problems.
16560 For example:
16562 @example
16563 int j;
16564 for (j = 1; 0 < j; j *= 2)
16565   test (j);
16566 @end example
16568 @noindent
16569 Here, the loop attempts to iterate through all powers of 2 that
16570 @code{int} can represent, but the C standard allows a compiler to
16571 optimize away the comparison and generate an infinite loop,
16572 under the argument that behavior is undefined on overflow.  As of this
16573 writing this optimization is not done by any production version of
16574 @acronym{GCC} with @option{-O2}, but it might be performed by other
16575 compilers, or by more aggressive @acronym{GCC} optimization options,
16576 and the @acronym{GCC} developers have not decided whether it will
16577 continue to work with @acronym{GCC} and @option{-O2}.
16579 @node Signed Overflow Advice
16580 @subsection Practical Advice for Signed Overflow Issues
16581 @cindex integer overflow
16582 @cindex overflow, signed integer
16583 @cindex signed integer overflow
16584 @cindex wraparound arithmetic
16586 Ideally the safest approach is to avoid signed integer overflow
16587 entirely.  For example, instead of multiplying two signed integers, you
16588 can convert them to unsigned integers, multiply the unsigned values,
16589 then test whether the result is in signed range.
16591 Rewriting code in this way will be inconvenient, though, particularly if
16592 the signed values might be negative.  Also, it may hurt
16593 performance.  Using unsigned arithmetic to check for overflow is
16594 particularly painful to do portably and efficiently when dealing with an
16595 integer type like @code{uid_t} whose width and signedness vary from
16596 platform to platform.
16598 Furthermore, many C applications pervasively assume wraparound behavior
16599 and typically it is not easy to find and remove all these assumptions.
16600 Hence it is often useful to maintain nonstandard code that assumes
16601 wraparound on overflow, instead of rewriting the code.  The rest of this
16602 section attempts to give practical advice for this situation.
16604 If your code wants to detect signed integer overflow in @code{sum = a +
16605 b}, it is generally safe to use an expression like @code{(sum < a) != (b
16606 < 0)}.
16608 If your code uses a signed loop index, make sure that the index cannot
16609 overflow, along with all signed expressions derived from the index.
16610 Here is a contrived example of problematic code with two instances of
16611 overflow.
16613 @example
16614 for (i = INT_MAX - 10; i <= INT_MAX; i++)
16615   if (i + 1 < 0)
16616     @{
16617       report_overflow ();
16618       break;
16619     @}
16620 @end example
16622 @noindent
16623 Because of the two overflows, a compiler might optimize away or
16624 transform the two comparisons in a way that is incompatible with the
16625 wraparound assumption.
16627 If your code uses an expression like @code{(i * 2000) / 1000} and you
16628 actually want the multiplication to wrap around on overflow, use
16629 unsigned arithmetic
16630 to do it, e.g., @code{((int) (i * 2000u)) / 1000}.
16632 If your code assumes wraparound behavior and you want to insulate it
16633 against any @acronym{GCC} optimizations that would fail to support that
16634 behavior, you should use @acronym{GCC}'s @option{-fwrapv} option, which
16635 causes signed overflow to wrap around reliably (except for division and
16636 remainder, as discussed in the next section).
16638 If you need to port to platforms where signed integer overflow does not
16639 reliably wrap around (e.g., due to hardware overflow checking, or to
16640 highly aggressive optimizations), you should consider debugging with
16641 @acronym{GCC}'s @option{-ftrapv} option, which causes signed overflow to
16642 raise an exception.
16644 @node Signed Integer Division
16645 @subsection Signed Integer Division and Integer Overflow
16646 @cindex division, integer
16648 Overflow in signed
16649 integer division is not always harmless: for example, on CPUs of the
16650 i386 family, dividing @code{INT_MIN} by @code{-1} yields a SIGFPE signal
16651 which by default terminates the program.  Worse, taking the remainder
16652 of these two values typically yields the same signal on these CPUs,
16653 even though the C standard requires @code{INT_MIN % -1} to yield zero
16654 because the expression does not overflow.
16656 @node Null Pointers
16657 @section Properties of Null Pointers
16658 @cindex null pointers
16660 Most modern hosts reliably fail when you attempt to dereference a null
16661 pointer.
16663 On almost all modern hosts, null pointers use an all-bits-zero internal
16664 representation, so you can reliably use @code{memset} with 0 to set all
16665 the pointers in an array to null values.
16667 If @code{p} is a null pointer to an object type, the C expression
16668 @code{p + 0} always evaluates to @code{p} on modern hosts, even though
16669 the standard says that it has undefined behavior.
16671 @node Buffer Overruns
16672 @section Buffer Overruns and Subscript Errors
16673 @cindex buffer overruns
16675 Buffer overruns and subscript errors are the most common dangerous
16676 errors in C programs.  They result in undefined behavior because storing
16677 outside an array typically modifies storage that is used by some other
16678 object, and most modern systems lack runtime checks to catch these
16679 errors.  Programs should not rely on buffer overruns being caught.
16681 There is one exception to the usual rule that a portable program cannot
16682 address outside an array.  In C, it is valid to compute the address just
16683 past an object, e.g., @code{&a[N]} where @code{a} has @code{N} elements,
16684 so long as you do not dereference the resulting pointer.  But it is not
16685 valid to compute the address just before an object, e.g., @code{&a[-1]};
16686 nor is it valid to compute two past the end, e.g., @code{&a[N+1]}.  On
16687 most platforms @code{&a[-1] < &a[0] && &a[N] < &a[N+1]}, but this is not
16688 reliable in general, and it is usually easy enough to avoid the
16689 potential portability problem, e.g., by allocating an extra unused array
16690 element at the start or end.
16692 @uref{http://valgrind.org/, Valgrind} can catch many overruns.
16693 @acronym{GCC}
16694 users might also consider using the @option{-fmudflap} option to catch
16695 overruns.
16697 Buffer overruns are usually caused by off-by-one errors, but there are
16698 more subtle ways to get them.
16700 Using @code{int} values to index into an array or compute array sizes
16701 causes problems on typical 64-bit hosts where an array index might
16702 be @math{2^31} or larger.  Index values of type @code{size_t} avoid this
16703 problem, but cannot be negative.  Index values of type @code{ptrdiff_t}
16704 are signed, and are wide enough in practice.
16706 If you add or multiply two numbers to calculate an array size, e.g.,
16707 @code{malloc (x * sizeof y + z)}, havoc ensues if the addition or
16708 multiplication overflows.
16710 Many implementations of the @code{alloca} function silently misbehave
16711 and can generate buffer overflows if given sizes that are too large.
16712 The size limits are implementation dependent, but are at least 4000
16713 bytes on all platforms that we know about.
16715 The standard functions @code{asctime}, @code{asctime_r}, @code{ctime},
16716 @code{ctime_r}, and @code{gets} are prone to buffer overflows, and
16717 portable code should not use them unless the inputs are known to be
16718 within certain limits.  The time-related functions can overflow their
16719 buffers if given timestamps out of range (e.g., a year less than -999
16720 or greater than 9999).  Time-related buffer overflows cannot happen with
16721 recent-enough versions of the @acronym{GNU} C library, but are possible
16722 with other
16723 implementations.  The @code{gets} function is the worst, since it almost
16724 invariably overflows its buffer when presented with an input line larger
16725 than the buffer.
16727 @node Volatile Objects
16728 @section Volatile Objects
16729 @cindex volatile objects
16731 The keyword @code{volatile} is often misunderstood in portable code.
16732 Its use inhibits some memory-access optimizations, but programmers often
16733 wish that it had a different meaning than it actually does.
16735 @code{volatile} was designed for code that accesses special objects like
16736 memory-mapped device registers whose contents spontaneously change.
16737 Such code is inherently low-level, and it is difficult to specify
16738 portably what @code{volatile} means in these cases.  The C standard
16739 says, ``What constitutes an access to an object that has
16740 volatile-qualified type is implementation-defined,'' so in theory each
16741 implementation is supposed to fill in the gap by documenting what
16742 @code{volatile} means for that implementation.  In practice, though,
16743 this documentation is usually absent or incomplete.
16745 One area of confusion is the distinction between objects defined with
16746 volatile types, and volatile lvalues.  From the C standard's point of
16747 view, an object defined with a volatile type has externally visible
16748 behavior.  You can think of such objects as having little oscilloscope
16749 probes attached to them, so that the user can observe some properties of
16750 accesses to them, just as the user can observe data written to output
16751 files.  However, the standard does not make it clear whether users can
16752 observe accesses by volatile lvalues to ordinary objects.  For example:
16754 @example
16755 /* Declare and access a volatile object.
16756    Accesses to X are "visible" to users.  */
16757 static int volatile x;
16758 x = 1;
16760 /* Access two ordinary objects via a volatile lvalue.
16761    It's not clear whether accesses to *P are "visible".  */
16762 int y;
16763 int *z = malloc (sizeof (int));
16764 int volatile *p;
16765 p = &y;
16766 *p = 1;
16767 p = z;
16768 *p = 1;
16769 @end example
16771 Programmers often wish that @code{volatile} meant ``Perform the memory
16772 access here and now, without merging several memory accesses, without
16773 changing the memory word size, and without reordering.''  But the C
16774 standard does not require this.  For objects defined with a volatile
16775 type, accesses must be done before the next sequence point; but
16776 otherwise merging, reordering, and word-size change is allowed.  Worse,
16777 it is not clear from the standard whether volatile lvalues provide more
16778 guarantees in general than nonvolatile lvalues, if the underlying
16779 objects are ordinary.
16781 Even when accessing objects defined with a volatile type,
16782 the C standard allows only
16783 extremely limited signal handlers: the behavior is undefined if a signal
16784 handler reads any nonlocal object, or writes to any nonlocal object
16785 whose type is not @code{sig_atomic_t volatile}, or calls any standard
16786 library function other than @code{abort}, @code{signal}, and (if C99)
16787 @code{_Exit}.  Hence C compilers need not worry about a signal handler
16788 disturbing ordinary computation, unless the computation accesses a
16789 @code{sig_atomic_t volatile} lvalue that is not a local variable.
16790 (There is an obscure exception for accesses via a pointer to a volatile
16791 character, since it may point into part of a @code{sig_atomic_t
16792 volatile} object.)  Posix
16793 adds to the list of library functions callable from a portable signal
16794 handler, but otherwise is like the C standard in this area.
16796 Some C implementations allow memory-access optimizations within each
16797 translation unit, such that actual behavior agrees with the behavior
16798 required by the standard only when calling a function in some other
16799 translation unit, and a signal handler acts like it was called from a
16800 different translation unit.  The C standard hints that in these
16801 implementations, objects referred to by signal handlers ``would require
16802 explicit specification of @code{volatile} storage, as well as other
16803 implementation-defined restrictions.''  But unfortunately even for this
16804 special case these other restrictions are often not documented well.
16805 @xref{Volatiles, , When is a Volatile Object Accessed?, gcc, Using the
16806 @acronym{GNU} Compiler Collection (@acronym{GCC})}, for some
16807 restrictions imposed by @acronym{GCC}.  @xref{Defining Handlers, ,
16808 Defining Signal Handlers, libc, The @acronym{GNU} C Library}, for some
16809 restrictions imposed by the @acronym{GNU} C library.  Restrictions
16810 differ on other platforms.
16812 If possible, it is best to use a signal handler that fits within the
16813 limits imposed by the C and Posix standards.
16815 If this is not practical, you can try the following rules of thumb.  A
16816 signal handler should access only volatile lvalues, preferably lvalues
16817 that refer to objects defined with a volatile type, and should not
16818 assume that the accessed objects have an internally consistent state
16819 if they are larger than a machine word.  Furthermore, installers
16820 should employ compilers and compiler options that are commonly used
16821 for building operating system kernels, because kernels often need more
16822 from @code{volatile} than the C Standard requires, and installers who
16823 compile an application in a similar environment can sometimes benefit
16824 from the extra constraints imposed by kernels on compilers.
16825 Admittedly we are handwaving somewhat here, as there are few
16826 guarantees in this area; the rules of thumb may help to fix some bugs
16827 but there is a good chance that they will not fix them all.
16829 For @code{volatile}, C++ has the same problems that C does.
16830 Multithreaded applications have even more problems with @code{volatile},
16831 but they are beyond the scope of this section.
16833 The bottom line is that using @code{volatile} typically hurts
16834 performance but should not hurt correctness.  In some cases its use
16835 does help correctness, but these cases are often so poorly understood
16836 that all too often adding @code{volatile} to a data structure merely
16837 alleviates some symptoms of a bug while not fixing the bug in general.
16839 @node Floating Point Portability
16840 @section Floating Point Portability
16841 @cindex floating point
16843 Almost all modern systems use IEEE-754 floating point, and it is safe to
16844 assume IEEE-754 in most portable code these days.  For more information,
16845 please see David Goldberg's classic paper
16846 @uref{http://www.validlab.com/goldberg/paper.pdf, What Every Computer
16847 Scientist Should Know About Floating-Point Arithmetic}.
16849 @node Exiting Portably
16850 @section Exiting Portably
16851 @cindex exiting portably
16853 A C or C++ program can exit with status @var{N} by returning
16854 @var{N} from the @code{main} function.  Portable programs are supposed
16855 to exit either with status 0 or @code{EXIT_SUCCESS} to succeed, or with
16856 status @code{EXIT_FAILURE} to fail, but in practice it is portable to
16857 fail by exiting with status 1, and test programs that assume Posix can
16858 fail by exiting with status values from 1 through 255.  Programs on
16859 SunOS 2.0 (1985) through 3.5.2 (1988) incorrectly exited with zero
16860 status when @code{main} returned nonzero, but ancient systems like these
16861 are no longer of practical concern.
16863 A program can also exit with status @var{N} by passing @var{N} to the
16864 @code{exit} function, and a program can fail by calling the @code{abort}
16865 function.  If a program is specialized to just some platforms, it can fail
16866 by calling functions specific to those platforms, e.g., @code{_exit}
16867 (Posix) and @code{_Exit} (C99).  However, like other functions, an exit
16868 function should be declared, typically by including a header.  For
16869 example, if a C program calls @code{exit}, it should include @file{stdlib.h}
16870 either directly or via the default includes (@pxref{Default Includes}).
16872 A program can fail due to undefined behavior such as dereferencing a null
16873 pointer, but this is not recommended as undefined behavior allows an
16874 implementation to do whatever it pleases and this includes exiting
16875 successfully.
16878 @c ================================================== Manual Configuration
16880 @node Manual Configuration
16881 @chapter Manual Configuration
16883 A few kinds of features can't be guessed automatically by running test
16884 programs.  For example, the details of the object-file format, or
16885 special options that need to be passed to the compiler or linker.  You
16886 can check for such features using ad-hoc means, such as having
16887 @command{configure} check the output of the @code{uname} program, or
16888 looking for libraries that are unique to particular systems.  However,
16889 Autoconf provides a uniform method for handling unguessable features.
16891 @menu
16892 * Specifying Names::            Specifying the system type
16893 * Canonicalizing::              Getting the canonical system type
16894 * Using System Type::           What to do with the system type
16895 @end menu
16897 @node Specifying Names
16898 @section Specifying the System Type
16899 @cindex System type
16901 Autoconf-generated
16902 @command{configure} scripts can make decisions based on a canonical name
16903 for the system type, which has the form:
16904 @samp{@var{cpu}-@var{vendor}-@var{os}}, where @var{os} can be
16905 @samp{@var{system}} or @samp{@var{kernel}-@var{system}}
16907 @command{configure} can usually guess the canonical name for the type of
16908 system it's running on.  To do so it runs a script called
16909 @command{config.guess}, which infers the name using the @code{uname}
16910 command or symbols predefined by the C preprocessor.
16912 Alternately, the user can specify the system type with command line
16913 arguments to @command{configure}.  Doing so is necessary when
16914 cross-compiling.  In the most complex case of cross-compiling, three
16915 system types are involved.  The options to specify them are:
16917 @table @option
16918 @item --build=@var{build-type}
16919 the type of system on which the package is being configured and
16920 compiled.  It defaults to the result of running @command{config.guess}.
16922 @item --host=@var{host-type}
16923 the type of system on which the package runs.  By default it is the
16924 same as the build machine.  Specifying it enables the cross-compilation
16925 mode.
16927 @item --target=@var{target-type}
16928 the type of system for which any compiler tools in the package
16929 produce code (rarely needed).  By default, it is the same as host.
16930 @end table
16932 If you mean to override the result of @command{config.guess}, use
16933 @option{--build}, not @option{--host}, since the latter enables
16934 cross-compilation.  For historical reasons,
16935 whenever you specify @option{--host},
16936 be sure to specify @option{--build} too; this will be fixed in the
16937 future.  So, to enter cross-compilation mode, use a command like this
16939 @example
16940 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
16941 @end example
16943 @noindent
16944 Note that if you do not specify @option{--host}, @command{configure}
16945 fails if it can't run the code generated by the specified compiler.  For
16946 example, configuring as follows fails:
16948 @example
16949 ./configure CC=m68k-coff-gcc
16950 @end example
16952 In the future, when cross-compiling Autoconf will @emph{not}
16953 accept tools (compilers, linkers, assemblers) whose name is not
16954 prefixed with the host type.  The only case when this may be
16955 useful is when you really are not cross-compiling, but only
16956 building for a least-common-denominator architecture: an example
16957 is building for @code{i386-pc-linux-gnu} while running on an
16958 @code{i686-pc-linux-gnu} architecture.  In this case, some particular
16959 pairs might be similar enough to let you get away with the system
16960 compilers, but in general the compiler might make bogus assumptions
16961 on the host: if you know what you are doing, please create symbolic
16962 links from the host compiler to the build compiler.
16964 @cindex @command{config.sub}
16965 @command{configure} recognizes short aliases for many system types; for
16966 example, @samp{decstation} can be used instead of
16967 @samp{mips-dec-ultrix4.2}.  @command{configure} runs a script called
16968 @command{config.sub} to canonicalize system type aliases.
16970 This section deliberately omits the description of the obsolete
16971 interface; see @ref{Hosts and Cross-Compilation}.
16974 @node Canonicalizing
16975 @section Getting the Canonical System Type
16976 @cindex System type
16977 @cindex Canonical system type
16979 The following macros make the system type available to @command{configure}
16980 scripts.
16982 @ovindex build_alias
16983 @ovindex host_alias
16984 @ovindex target_alias
16986 The variables @samp{build_alias}, @samp{host_alias}, and
16987 @samp{target_alias} are always exactly the arguments of @option{--build},
16988 @option{--host}, and @option{--target}; in particular, they are left empty
16989 if the user did not use them, even if the corresponding
16990 @code{AC_CANONICAL} macro was run.  Any configure script may use these
16991 variables anywhere.  These are the variables that should be used when in
16992 interaction with the user.
16994 If you need to recognize some special environments based on their system
16995 type, run the following macros to get canonical system names.  These
16996 variables are not set before the macro call.
16998 If you use these macros, you must distribute @command{config.guess} and
16999 @command{config.sub} along with your source code.  @xref{Output}, for
17000 information about the @code{AC_CONFIG_AUX_DIR} macro which you can use
17001 to control in which directory @command{configure} looks for those scripts.
17004 @defmac AC_CANONICAL_BUILD
17005 @acindex{CANONICAL_BUILD}
17006 @ovindex build
17007 @ovindex build_cpu
17008 @ovindex build_vendor
17009 @ovindex build_os
17010 Compute the canonical build-system type variable, @code{build}, and its
17011 three individual parts @code{build_cpu}, @code{build_vendor}, and
17012 @code{build_os}.
17014 If @option{--build} was specified, then @code{build} is the
17015 canonicalization of @code{build_alias} by @command{config.sub},
17016 otherwise it is determined by the shell script @command{config.guess}.
17017 @end defmac
17019 @defmac AC_CANONICAL_HOST
17020 @acindex{CANONICAL_HOST}
17021 @ovindex host
17022 @ovindex host_cpu
17023 @ovindex host_vendor
17024 @ovindex host_os
17025 Compute the canonical host-system type variable, @code{host}, and its
17026 three individual parts @code{host_cpu}, @code{host_vendor}, and
17027 @code{host_os}.
17029 If @option{--host} was specified, then @code{host} is the
17030 canonicalization of @code{host_alias} by @command{config.sub},
17031 otherwise it defaults to @code{build}.
17032 @end defmac
17034 @defmac AC_CANONICAL_TARGET
17035 @acindex{CANONICAL_TARGET}
17036 @ovindex target
17037 @ovindex target_cpu
17038 @ovindex target_vendor
17039 @ovindex target_os
17040 Compute the canonical target-system type variable, @code{target}, and its
17041 three individual parts @code{target_cpu}, @code{target_vendor}, and
17042 @code{target_os}.
17044 If @option{--target} was specified, then @code{target} is the
17045 canonicalization of @code{target_alias} by @command{config.sub},
17046 otherwise it defaults to @code{host}.
17047 @end defmac
17049 Note that there can be artifacts due to the backward compatibility
17050 code.  See @xref{Hosts and Cross-Compilation}, for more.
17052 @node Using System Type
17053 @section Using the System Type
17055 In @file{configure.ac} the system type is generally used by one or more
17056 @code{case} statements to select system-specifics.  Shell wildcards can
17057 be used to match a group of system types.
17059 For example, an extra assembler code object file could be chosen, giving
17060 access to a CPU cycle counter register.  @code{$(CYCLE_OBJ)} in the
17061 following would be used in a makefile to add the object to a
17062 program or library.
17064 @example
17065 case $host in
17066   alpha*-*-*) CYCLE_OBJ=rpcc.o ;;
17067   i?86-*-*)   CYCLE_OBJ=rdtsc.o ;;
17068   *)          CYCLE_OBJ= ;;
17069 esac
17070 AC_SUBST([CYCLE_OBJ])
17071 @end example
17073 @code{AC_CONFIG_LINKS} (@pxref{Configuration Links}) is another good way
17074 to select variant source files, for example optimized code for some
17075 CPUs.  The configured CPU type doesn't always indicate exact CPU types,
17076 so some runtime capability checks may be necessary too.
17078 @example
17079 case $host in
17080   alpha*-*-*)   AC_CONFIG_LINKS([dither.c:alpha/dither.c]) ;;
17081   powerpc*-*-*) AC_CONFIG_LINKS([dither.c:powerpc/dither.c]) ;;
17082   *-*-*)        AC_CONFIG_LINKS([dither.c:generic/dither.c]) ;;
17083 esac
17084 @end example
17086 The host system type can also be used to find cross-compilation tools
17087 with @code{AC_CHECK_TOOL} (@pxref{Generic Programs}).
17089 The above examples all show @samp{$host}, since this is where the code
17090 is going to run.  Only rarely is it necessary to test @samp{$build}
17091 (which is where the build is being done).
17093 Whenever you're tempted to use @samp{$host} it's worth considering
17094 whether some sort of probe would be better.  New system types come along
17095 periodically or previously missing features are added.  Well-written
17096 probes can adapt themselves to such things, but hard-coded lists of
17097 names can't.  Here are some guidelines,
17099 @itemize @bullet
17100 @item
17101 Availability of libraries and library functions should always be checked
17102 by probing.
17103 @item
17104 Variant behavior of system calls is best identified with runtime tests
17105 if possible, but bug workarounds or obscure difficulties might have to
17106 be driven from @samp{$host}.
17107 @item
17108 Assembler code is inevitably highly CPU-specific and is best selected
17109 according to @samp{$host_cpu}.
17110 @item
17111 Assembler variations like underscore prefix on globals or ELF versus
17112 COFF type directives are however best determined by probing, perhaps
17113 even examining the compiler output.
17114 @end itemize
17116 @samp{$target} is for use by a package creating a compiler or similar.
17117 For ordinary packages it's meaningless and should not be used.  It
17118 indicates what the created compiler should generate code for, if it can
17119 cross-compile.  @samp{$target} generally selects various hard-coded CPU
17120 and system conventions, since usually the compiler or tools under
17121 construction themselves determine how the target works.
17124 @c ===================================================== Site Configuration.
17126 @node Site Configuration
17127 @chapter Site Configuration
17129 @command{configure} scripts support several kinds of local configuration
17130 decisions.  There are ways for users to specify where external software
17131 packages are, include or exclude optional features, install programs
17132 under modified names, and set default values for @command{configure}
17133 options.
17135 @menu
17136 * Help Formatting::             Customizing @samp{configure --help}
17137 * External Software::           Working with other optional software
17138 * Package Options::             Selecting optional features
17139 * Pretty Help Strings::         Formatting help string
17140 * Option Checking::             Controlling checking of @command{configure} options
17141 * Site Details::                Configuring site details
17142 * Transforming Names::          Changing program names when installing
17143 * Site Defaults::               Giving @command{configure} local defaults
17144 @end menu
17146 @node Help Formatting
17147 @section Controlling Help Output
17149 Users consult @samp{configure --help} to learn of configuration
17150 decisions specific to your package.  By default, @command{configure}
17151 breaks this output into sections for each type of option; within each
17152 section, help strings appear in the order @file{configure.ac} defines
17153 them:
17155 @example
17156 Optional Features:
17157   @dots{}
17158   --enable-bar            include bar
17160 Optional Packages:
17161   @dots{}
17162   --with-foo              use foo
17163 @end example
17165 @defmac AC_PRESERVE_HELP_ORDER
17166 @acindex{PRESERVE_HELP_ORDER}
17168 Request an alternate @option{--help} format, in which options of all
17169 types appear together, in the order defined.  Call this macro before any
17170 @code{AC_ARG_ENABLE} or @code{AC_ARG_WITH}.
17172 @example
17173 Optional Features and Packages:
17174   @dots{}
17175   --enable-bar            include bar
17176   --with-foo              use foo
17177 @end example
17179 @end defmac
17181 @node External Software
17182 @section Working With External Software
17183 @cindex External software
17185 Some packages require, or can optionally use, other software packages
17186 that are already installed.  The user can give @command{configure}
17187 command line options to specify which such external software to use.
17188 The options have one of these forms:
17190 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
17191 @c awful.
17192 @example
17193 --with-@var{package}[=@var{arg}]
17194 --without-@var{package}
17195 @end example
17197 For example, @option{--with-gnu-ld} means work with the @acronym{GNU} linker
17198 instead of some other linker.  @option{--with-x} means work with The X
17199 Window System.
17201 The user can give an argument by following the package name with
17202 @samp{=} and the argument.  Giving an argument of @samp{no} is for
17203 packages that are used by default; it says to @emph{not} use the
17204 package.  An argument that is neither @samp{yes} nor @samp{no} could
17205 include a name or number of a version of the other package, to specify
17206 more precisely which other package this program is supposed to work
17207 with.  If no argument is given, it defaults to @samp{yes}.
17208 @option{--without-@var{package}} is equivalent to
17209 @option{--with-@var{package}=no}.
17211 Normally @command{configure} scripts complain about
17212 @option{--with-@var{package}} options that they do not support.
17213 @xref{Option Checking}, for details, and for how to override the
17214 defaults.
17216 For each external software package that may be used, @file{configure.ac}
17217 should call @code{AC_ARG_WITH} to detect whether the @command{configure}
17218 user asked to use it.  Whether each package is used or not by default,
17219 and which arguments are valid, is up to you.
17221 @anchor{AC_ARG_WITH}
17222 @defmac AC_ARG_WITH (@var{package}, @var{help-string}, @
17223   @ovar{action-if-given}, @ovar{action-if-not-given})
17224 @acindex{ARG_WITH}
17225 If the user gave @command{configure} the option @option{--with-@var{package}}
17226 or @option{--without-@var{package}}, run shell commands
17227 @var{action-if-given}.  If neither option was given, run shell commands
17228 @var{action-if-not-given}.  The name @var{package} indicates another
17229 software package that this program should work with.  It should consist
17230 only of alphanumeric characters, dashes, and dots.
17232 The option's argument is available to the shell commands
17233 @var{action-if-given} in the shell variable @code{withval}, which is
17234 actually just the value of the shell variable named
17235 @code{with_@var{package}}, with any non-alphanumeric characters in
17236 @var{package} changed into @samp{_}.  You may use that variable instead,
17237 if you wish.
17239 The argument @var{help-string} is a description of the option that
17240 looks like this:
17241 @example
17242   --with-readline         support fancy command line editing
17243 @end example
17245 @noindent
17246 @var{help-string} may be more than one line long, if more detail is
17247 needed.  Just make sure the columns line up in @samp{configure
17248 --help}.  Avoid tabs in the help string.  You'll need to enclose the
17249 help string in @samp{[} and @samp{]} in order to produce the leading
17250 blanks.
17252 You should format your @var{help-string} with the macro
17253 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
17255 The following example shows how to use the @code{AC_ARG_WITH} macro in
17256 a common situation.  You want to let the user decide whether to enable
17257 support for an external library (e.g., the readline library); if the user
17258 specified neither @option{--with-readline} nor @option{--without-readline},
17259 you want to enable support for readline only if the library is available
17260 on the system.
17262 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
17263 @example
17264 AC_ARG_WITH([readline],
17265   [AS_HELP_STRING([--with-readline],
17266     [support fancy command line editing @@<:@@default=check@@:>@@])],
17267   [],
17268   [with_readline=check])
17270 LIBREADLINE=
17271 AS_IF([test "x$with_readline" != xno],
17272   [AC_CHECK_LIB([readline], [main],
17273     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
17274      AC_DEFINE([HAVE_LIBREADLINE], [1],
17275                [Define if you have libreadline])
17276     ],
17277     [if test "x$with_readline" != xcheck; then
17278        AC_MSG_FAILURE(
17279          [--with-readline was given, but test for readline failed])
17280      fi
17281     ], -lncurses)])
17282 @end example
17284 The next example shows how to use @code{AC_ARG_WITH} to give the user the
17285 possibility to enable support for the readline library, in case it is still
17286 experimental and not well tested, and is therefore disabled by default.
17288 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
17289 @example
17290 AC_ARG_WITH([readline],
17291   [AS_HELP_STRING([--with-readline],
17292     [enable experimental support for readline])],
17293   [],
17294   [with_readline=no])
17296 LIBREADLINE=
17297 AS_IF([test "x$with_readline" != xno],
17298   [AC_CHECK_LIB([readline], [main],
17299     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
17300      AC_DEFINE([HAVE_LIBREADLINE], [1],
17301                [Define if you have libreadline])
17302     ],
17303     [AC_MSG_FAILURE(
17304        [--with-readline was given, but test for readline failed])],
17305     [-lncurses])])
17306 @end example
17308 The last example shows how to use @code{AC_ARG_WITH} to give the user the
17309 possibility to disable support for the readline library, given that it is
17310 an important feature and that it should be enabled by default.
17312 @c FIXME: Remove AS_IF when the problem of AC_REQUIRE within `if' is solved.
17313 @example
17314 AC_ARG_WITH([readline],
17315   [AS_HELP_STRING([--without-readline],
17316     [disable support for readline])],
17317   [],
17318   [with_readline=yes])
17320 LIBREADLINE=
17321 AS_IF([test "x$with_readline" != xno],
17322   [AC_CHECK_LIB([readline], [main],
17323     [AC_SUBST([LIBREADLINE], ["-lreadline -lncurses"])
17324      AC_DEFINE([HAVE_LIBREADLINE], [1],
17325                [Define if you have libreadline])
17326     ],
17327     [AC_MSG_FAILURE(
17328        [readline test failed (--without-readline to disable)])],
17329     [-lncurses])])
17330 @end example
17332 These three examples can be easily adapted to the case where
17333 @code{AC_ARG_ENABLE} should be preferred to @code{AC_ARG_WITH} (see
17334 @ref{Package Options}).
17335 @end defmac
17337 @node Package Options
17338 @section Choosing Package Options
17339 @cindex Package options
17340 @cindex Options, package
17342 If a software package has optional compile-time features, the user can
17343 give @command{configure} command line options to specify whether to
17344 compile them.  The options have one of these forms:
17346 @c FIXME: Can't use @ovar here, Texinfo 4.0 goes lunatic and emits something
17347 @c awful.
17348 @example
17349 --enable-@var{feature}[=@var{arg}]
17350 --disable-@var{feature}
17351 @end example
17353 These options allow users to choose which optional features to build and
17354 install.  @option{--enable-@var{feature}} options should never make a
17355 feature behave differently or cause one feature to replace another.
17356 They should only cause parts of the program to be built rather than left
17357 out.
17359 The user can give an argument by following the feature name with
17360 @samp{=} and the argument.  Giving an argument of @samp{no} requests
17361 that the feature @emph{not} be made available.  A feature with an
17362 argument looks like @option{--enable-debug=stabs}.  If no argument is
17363 given, it defaults to @samp{yes}.  @option{--disable-@var{feature}} is
17364 equivalent to @option{--enable-@var{feature}=no}.
17366 Normally @command{configure} scripts complain about
17367 @option{--enable-@var{package}} options that they do not support.
17368 @xref{Option Checking}, for details, and for how to override the
17369 defaults.
17371 For each optional feature, @file{configure.ac} should call
17372 @code{AC_ARG_ENABLE} to detect whether the @command{configure} user asked
17373 to include it.  Whether each feature is included or not by default, and
17374 which arguments are valid, is up to you.
17376 @anchor{AC_ARG_ENABLE}
17377 @defmac AC_ARG_ENABLE (@var{feature}, @var{help-string}, @
17378   @ovar{action-if-given}, @ovar{action-if-not-given})
17379 @acindex{ARG_ENABLE}
17380 If the user gave @command{configure} the option
17381 @option{--enable-@var{feature}} or @option{--disable-@var{feature}}, run
17382 shell commands @var{action-if-given}.  If neither option was given, run
17383 shell commands @var{action-if-not-given}.  The name @var{feature}
17384 indicates an optional user-level facility.  It should consist only of
17385 alphanumeric characters, dashes, and dots.
17387 The option's argument is available to the shell commands
17388 @var{action-if-given} in the shell variable @code{enableval}, which is
17389 actually just the value of the shell variable named
17390 @code{enable_@var{feature}}, with any non-alphanumeric characters in
17391 @var{feature} changed into @samp{_}.  You may use that variable instead,
17392 if you wish.  The @var{help-string} argument is like that of
17393 @code{AC_ARG_WITH} (@pxref{External Software}).
17395 You should format your @var{help-string} with the macro
17396 @code{AS_HELP_STRING} (@pxref{Pretty Help Strings}).
17398 See the examples suggested with the definition of @code{AC_ARG_WITH}
17399 (@pxref{External Software}) to get an idea of possible applications of
17400 @code{AC_ARG_ENABLE}.
17401 @end defmac
17403 @node Pretty Help Strings
17404 @section Making Your Help Strings Look Pretty
17405 @cindex Help strings
17407 Properly formatting the @samp{help strings} which are used in
17408 @code{AC_ARG_WITH} (@pxref{External Software}) and @code{AC_ARG_ENABLE}
17409 (@pxref{Package Options}) can be challenging.  Specifically, you want
17410 your own @samp{help strings} to line up in the appropriate columns of
17411 @samp{configure --help} just like the standard Autoconf @samp{help
17412 strings} do.  This is the purpose of the @code{AS_HELP_STRING} macro.
17414 @anchor{AS_HELP_STRING}
17415 @defmac AS_HELP_STRING (@var{left-hand-side}, @var{right-hand-side} @
17416   @dvar{indent-column, 26}, @dvar{wrap-column, 79})
17417 @asindex{HELP_STRING}
17419 Expands into an help string that looks pretty when the user executes
17420 @samp{configure --help}.  It is typically used in @code{AC_ARG_WITH}
17421 (@pxref{External Software}) or @code{AC_ARG_ENABLE} (@pxref{Package
17422 Options}).  The following example makes this clearer.
17424 @example
17425 AC_ARG_WITH([foo],
17426   [AS_HELP_STRING([--with-foo],
17427      [use foo (default is no)])],
17428   [use_foo=$withval],
17429   [use_foo=no])
17430 @end example
17432 Then the last few lines of @samp{configure --help} appear like
17433 this:
17435 @example
17436 --enable and --with options recognized:
17437   --with-foo              use foo (default is no)
17438 @end example
17440 Macro expansion is performed on the first argument.  However, the second
17441 argument of @code{AS_HELP_STRING} is treated as a whitespace separated
17442 list of text to be reformatted, and is not subject to macro expansion.
17443 Since it is not expanded, it should not be double quoted.
17444 @xref{Autoconf Language}, for a more detailed explanation.
17446 The @code{AS_HELP_STRING} macro is particularly helpful when the
17447 @var{left-hand-side} and/or @var{right-hand-side} are composed of macro
17448 arguments, as shown in the following example.  Be aware that
17449 @var{left-hand-side} may not contain unbalanced quotes or parentheses.
17451 @example
17452 AC_DEFUN([MY_ARG_WITH],
17453   [AC_ARG_WITH(m4_translit([[$1]], [_], [-]),
17454      [AS_HELP_STRING([--with-m4_translit([$1], [_], [-])],
17455                      [use $1 (default is $2)])],
17456      [use_[]$1=$withval],
17457      [use_[]$1=$2])])
17458 MY_ARG_WITH([a_b], [no])
17459 @end example
17460 @noindent
17461 Here, the last few lines of @samp{configure --help} will include:
17463 @example
17464 --enable and --with options recognized:
17465   --with-a-b              use a_b (default is no)
17466 @end example
17468 The parameters @var{indent-column} and @var{wrap-column} were introduced
17469 in Autoconf 2.62.  Generally, they should not be specified; they exist
17470 for fine-tuning of the wrapping.
17471 @example
17472 AS_HELP_STRING([--option], [description of option])
17473 @result{}  --option                description of option
17474 AS_HELP_STRING([--option], [description of option], [15], [30])
17475 @result{}  --option     description of
17476 @result{}               option
17477 @end example
17478 @end defmac
17481 @node Option Checking
17482 @section Controlling Checking of @command{configure} Options
17483 @cindex Options, Package
17485 The @command{configure} script checks its command-line options against a
17486 list of known options, like @option{--help} or @option{--config-cache}.
17487 An unknown option ordinarily indicates a mistake by the user and
17488 @command{configure} halts with an error.  However, by default unknown
17489 @option{--with-@var{package}} and @option{--enable-@var{feature}}
17490 options elicit only a warning, to support configuring entire source
17491 trees.
17493 Source trees often contain multiple packages with a top-level
17494 @command{configure} script that uses the @code{AC_CONFIG_SUBDIRS} macro
17495 (@pxref{Subdirectories}).  Because the packages generally support
17496 different @option{--with-@var{package}} and
17497 @option{--enable-@var{feature}} options, the @acronym{GNU} Coding
17498 Standards say they must accept unrecognized options without halting.
17499 Even a warning message is undesirable here, so @code{AC_CONFIG_SUBDIRS}
17500 automatically disables the warnings.
17502 This default behavior may be modified in two ways.  First, the installer
17503 can invoke @command{configure --disable-option-checking} to disable
17504 these warnings, or invoke @command{configure --enable-option-checking=fatal}
17505 options to turn them into fatal errors, respectively.  Second, the
17506 maintainer can use @code{AC_DISABLE_OPTION_CHECKING}.
17508 @defmac AC_DISABLE_OPTION_CHECKING
17509 @acindex{DISABLE_OPTION_CHECKING}
17511 By default, disable warnings related to any unrecognized
17512 @option{--with-@var{package}} or @option{--enable-@var{feature}}
17513 options.  This is implied by @code{AC_CONFIG_SUBDIRS}.
17515 The installer can override this behavior by passing
17516 @option{--enable-option-checking} (enable warnings) or
17517 @option{--enable-option-checking=fatal} (enable errors) to
17518 @command{configure}.
17519 @end defmac
17522 @node Site Details
17523 @section Configuring Site Details
17524 @cindex Site details
17526 Some software packages require complex site-specific information.  Some
17527 examples are host names to use for certain services, company names, and
17528 email addresses to contact.  Since some configuration scripts generated
17529 by Metaconfig ask for such information interactively, people sometimes
17530 wonder how to get that information in Autoconf-generated configuration
17531 scripts, which aren't interactive.
17533 Such site configuration information should be put in a file that is
17534 edited @emph{only by users}, not by programs.  The location of the file
17535 can either be based on the @code{prefix} variable, or be a standard
17536 location such as the user's home directory.  It could even be specified
17537 by an environment variable.  The programs should examine that file at
17538 runtime, rather than at compile time.  Runtime configuration is more
17539 convenient for users and makes the configuration process simpler than
17540 getting the information while configuring.  @xref{Directory Variables, ,
17541 Variables for Installation Directories, standards, @acronym{GNU} Coding
17542 Standards}, for more information on where to put data files.
17544 @node Transforming Names
17545 @section Transforming Program Names When Installing
17546 @cindex Transforming program names
17547 @cindex Program names, transforming
17549 Autoconf supports changing the names of programs when installing them.
17550 In order to use these transformations, @file{configure.ac} must call the
17551 macro @code{AC_ARG_PROGRAM}.
17553 @defmac AC_ARG_PROGRAM
17554 @acindex{ARG_PROGRAM}
17555 @ovindex program_transform_name
17556 Place in output variable @code{program_transform_name} a sequence of
17557 @code{sed} commands for changing the names of installed programs.
17559 If any of the options described below are given to @command{configure},
17560 program names are transformed accordingly.  Otherwise, if
17561 @code{AC_CANONICAL_TARGET} has been called and a @option{--target} value
17562 is given, the target type followed by a dash is used as a prefix.
17563 Otherwise, no program name transformation is done.
17564 @end defmac
17566 @menu
17567 * Transformation Options::      @command{configure} options to transform names
17568 * Transformation Examples::     Sample uses of transforming names
17569 * Transformation Rules::        Makefile uses of transforming names
17570 @end menu
17572 @node Transformation Options
17573 @subsection Transformation Options
17575 You can specify name transformations by giving @command{configure} these
17576 command line options:
17578 @table @option
17579 @item --program-prefix=@var{prefix}
17580 prepend @var{prefix} to the names;
17582 @item --program-suffix=@var{suffix}
17583 append @var{suffix} to the names;
17585 @item --program-transform-name=@var{expression}
17586 perform @code{sed} substitution @var{expression} on the names.
17587 @end table
17589 @node Transformation Examples
17590 @subsection Transformation Examples
17592 These transformations are useful with programs that can be part of a
17593 cross-compilation development environment.  For example, a
17594 cross-assembler running on a Sun 4 configured with
17595 @option{--target=i960-vxworks} is normally installed as
17596 @file{i960-vxworks-as}, rather than @file{as}, which could be confused
17597 with a native Sun 4 assembler.
17599 You can force a program name to begin with @file{g}, if you don't want
17600 @acronym{GNU} programs installed on your system to shadow other programs with
17601 the same name.  For example, if you configure @acronym{GNU} @code{diff} with
17602 @option{--program-prefix=g}, then when you run @samp{make install} it is
17603 installed as @file{/usr/local/bin/gdiff}.
17605 As a more sophisticated example, you could use
17607 @example
17608 --program-transform-name='s/^/g/; s/^gg/g/; s/^gless/less/'
17609 @end example
17610 @noindent
17612 to prepend @samp{g} to most of the program names in a source tree,
17613 excepting those like @code{gdb} that already have one and those like
17614 @code{less} and @code{lesskey} that aren't @acronym{GNU} programs.  (That is
17615 assuming that you have a source tree containing those programs that is
17616 set up to use this feature.)
17618 One way to install multiple versions of some programs simultaneously is
17619 to append a version number to the name of one or both.  For example, if
17620 you want to keep Autoconf version 1 around for awhile, you can configure
17621 Autoconf version 2 using @option{--program-suffix=2} to install the
17622 programs as @file{/usr/local/bin/autoconf2},
17623 @file{/usr/local/bin/autoheader2}, etc.  Nevertheless, pay attention
17624 that only the binaries are renamed, therefore you'd have problems with
17625 the library files which might overlap.
17627 @node Transformation Rules
17628 @subsection Transformation Rules
17630 Here is how to use the variable @code{program_transform_name} in a
17631 @file{Makefile.in}:
17633 @example
17634 PROGRAMS = cp ls rm
17635 transform = @@program_transform_name@@
17636 install:
17637         for p in $(PROGRAMS); do \
17638           $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/`echo $$p | \
17639                                               sed '$(transform)'`; \
17640         done
17642 uninstall:
17643         for p in $(PROGRAMS); do \
17644           rm -f $(DESTDIR)$(bindir)/`echo $$p | sed '$(transform)'`; \
17645         done
17646 @end example
17648 It is guaranteed that @code{program_transform_name} is never empty, and
17649 that there are no useless separators.  Therefore you may safely embed
17650 @code{program_transform_name} within a sed program using @samp{;}:
17652 @example
17653 transform = @@program_transform_name@@
17654 transform_exe = s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/
17655 @end example
17657 Whether to do the transformations on documentation files (Texinfo or
17658 @code{man}) is a tricky question; there seems to be no perfect answer,
17659 due to the several reasons for name transforming.  Documentation is not
17660 usually particular to a specific architecture, and Texinfo files do not
17661 conflict with system documentation.  But they might conflict with
17662 earlier versions of the same files, and @code{man} pages sometimes do
17663 conflict with system documentation.  As a compromise, it is probably
17664 best to do name transformations on @code{man} pages but not on Texinfo
17665 manuals.
17667 @node Site Defaults
17668 @section Setting Site Defaults
17669 @cindex Site defaults
17671 Autoconf-generated @command{configure} scripts allow your site to provide
17672 default values for some configuration values.  You do this by creating
17673 site- and system-wide initialization files.
17675 @evindex CONFIG_SITE
17676 If the environment variable @code{CONFIG_SITE} is set, @command{configure}
17677 uses its value as the name of a shell script to read.  Otherwise, it
17678 reads the shell script @file{@var{prefix}/share/config.site} if it exists,
17679 then @file{@var{prefix}/etc/config.site} if it exists.  Thus,
17680 settings in machine-specific files override those in machine-independent
17681 ones in case of conflict.
17683 Site files can be arbitrary shell scripts, but only certain kinds of
17684 code are really appropriate to be in them.  Because @command{configure}
17685 reads any cache file after it has read any site files, a site file can
17686 define a default cache file to be shared between all Autoconf-generated
17687 @command{configure} scripts run on that system (@pxref{Cache Files}).  If
17688 you set a default cache file in a site file, it is a good idea to also
17689 set the output variable @code{CC} in that site file, because the cache
17690 file is only valid for a particular compiler, but many systems have
17691 several available.
17693 You can examine or override the value set by a command line option to
17694 @command{configure} in a site file; options set shell variables that have
17695 the same names as the options, with any dashes turned into underscores.
17696 The exceptions are that @option{--without-} and @option{--disable-} options
17697 are like giving the corresponding @option{--with-} or @option{--enable-}
17698 option and the value @samp{no}.  Thus, @option{--cache-file=localcache}
17699 sets the variable @code{cache_file} to the value @samp{localcache};
17700 @option{--enable-warnings=no} or @option{--disable-warnings} sets the variable
17701 @code{enable_warnings} to the value @samp{no}; @option{--prefix=/usr} sets the
17702 variable @code{prefix} to the value @samp{/usr}; etc.
17704 Site files are also good places to set default values for other output
17705 variables, such as @code{CFLAGS}, if you need to give them non-default
17706 values: anything you would normally do, repetitively, on the command
17707 line.  If you use non-default values for @var{prefix} or
17708 @var{exec_prefix} (wherever you locate the site file), you can set them
17709 in the site file if you specify it with the @code{CONFIG_SITE}
17710 environment variable.
17712 You can set some cache values in the site file itself.  Doing this is
17713 useful if you are cross-compiling, where it is impossible to check features
17714 that require running a test program.  You could ``prime the cache'' by
17715 setting those values correctly for that system in
17716 @file{@var{prefix}/etc/config.site}.  To find out the names of the cache
17717 variables you need to set, look for shell variables with @samp{_cv_} in
17718 their names in the affected @command{configure} scripts, or in the Autoconf
17719 M4 source code for those macros.
17721 The cache file is careful to not override any variables set in the site
17722 files.  Similarly, you should not override command-line options in the
17723 site files.  Your code should check that variables such as @code{prefix}
17724 and @code{cache_file} have their default values (as set near the top of
17725 @command{configure}) before changing them.
17727 Here is a sample file @file{/usr/share/local/gnu/share/config.site}.  The
17728 command @samp{configure --prefix=/usr/share/local/gnu} would read this
17729 file (if @code{CONFIG_SITE} is not set to a different file).
17731 @example
17732 # config.site for configure
17734 # Change some defaults.
17735 test "$prefix" = NONE && prefix=/usr/share/local/gnu
17736 test "$exec_prefix" = NONE && exec_prefix=/usr/local/gnu
17737 test "$sharedstatedir" = '$prefix/com' && sharedstatedir=/var
17738 test "$localstatedir" = '$prefix/var' && localstatedir=/var
17740 # Give Autoconf 2.x generated configure scripts a shared default
17741 # cache file for feature test results, architecture-specific.
17742 if test "$cache_file" = /dev/null; then
17743   cache_file="$prefix/var/config.cache"
17744   # A cache file is only valid for one C compiler.
17745   CC=gcc
17747 @end example
17750 @c ============================================== Running configure Scripts.
17752 @node Running configure Scripts
17753 @chapter Running @command{configure} Scripts
17754 @cindex @command{configure}
17756 Below are instructions on how to configure a package that uses a
17757 @command{configure} script, suitable for inclusion as an @file{INSTALL}
17758 file in the package.  A plain-text version of @file{INSTALL} which you
17759 may use comes with Autoconf.
17761 @menu
17762 * Basic Installation::          Instructions for typical cases
17763 * Compilers and Options::       Selecting compilers and optimization
17764 * Multiple Architectures::      Compiling for multiple architectures at once
17765 * Installation Names::          Installing in different directories
17766 * Optional Features::           Selecting optional features
17767 * System Type::                 Specifying the system type
17768 * Sharing Defaults::            Setting site-wide defaults for @command{configure}
17769 * Defining Variables::          Specifying the compiler etc.
17770 * configure Invocation::        Changing how @command{configure} runs
17771 @end menu
17773 @set autoconf
17774 @include install.texi
17777 @c ============================================== config.status Invocation
17779 @node config.status Invocation
17780 @chapter config.status Invocation
17781 @cindex @command{config.status}
17783 The @command{configure} script creates a file named @file{config.status},
17784 which actually configures, @dfn{instantiates}, the template files.  It
17785 also records the configuration options that were specified when the
17786 package was last configured in case reconfiguring is needed.
17788 Synopsis:
17789 @example
17790 ./config.status @var{option}@dots{} [@var{file}@dots{}]
17791 @end example
17793 It configures the @var{files}; if none are specified, all the templates
17794 are instantiated.  The files must be specified without their
17795 dependencies, as in
17797 @example
17798 ./config.status foobar
17799 @end example
17801 @noindent
17804 @example
17805 ./config.status foobar:foo.in:bar.in
17806 @end example
17808 The supported options are:
17810 @table @option
17811 @item --help
17812 @itemx -h
17813 Print a summary of the command line options, the list of the template
17814 files, and exit.
17816 @item --version
17817 @itemx -V
17818 Print the version number of Autoconf and the configuration settings,
17819 and exit.
17821 @item --silent
17822 @itemx --quiet
17823 @itemx -q
17824 Do not print progress messages.
17826 @item --debug
17827 @itemx -d
17828 Don't remove the temporary files.
17830 @item --file=@var{file}[:@var{template}]
17831 Require that @var{file} be instantiated as if
17832 @samp{AC_CONFIG_FILES(@var{file}:@var{template})} was used.  Both
17833 @var{file} and @var{template} may be @samp{-} in which case the standard
17834 output and/or standard input, respectively, is used.  If a
17835 @var{template} file name is relative, it is first looked for in the build
17836 tree, and then in the source tree.  @xref{Configuration Actions}, for
17837 more details.
17839 This option and the following ones provide one way for separately
17840 distributed packages to share the values computed by @command{configure}.
17841 Doing so can be useful if some of the packages need a superset of the
17842 features that one of them, perhaps a common library, does.  These
17843 options allow a @file{config.status} file to create files other than the
17844 ones that its @file{configure.ac} specifies, so it can be used for a
17845 different package.
17847 @item --header=@var{file}[:@var{template}]
17848 Same as @option{--file} above, but with @samp{AC_CONFIG_HEADERS}.
17850 @item --recheck
17851 Ask @file{config.status} to update itself and exit (no instantiation).
17852 This option is useful if you change @command{configure}, so that the
17853 results of some tests might be different from the previous run.  The
17854 @option{--recheck} option reruns @command{configure} with the same arguments
17855 you used before, plus the @option{--no-create} option, which prevents
17856 @command{configure} from running @file{config.status} and creating
17857 @file{Makefile} and other files, and the @option{--no-recursion} option,
17858 which prevents @command{configure} from running other @command{configure}
17859 scripts in subdirectories.  (This is so other Make rules can
17860 run @file{config.status} when it changes; @pxref{Automatic Remaking},
17861 for an example).
17862 @end table
17864 @file{config.status} checks several optional environment variables that
17865 can alter its behavior:
17867 @defvar CONFIG_SHELL
17868 @evindex CONFIG_SHELL
17869 The shell with which to run @command{configure} for the @option{--recheck}
17870 option.  It must be Bourne-compatible.  The default is a shell that
17871 supports @code{LINENO} if available, and @file{/bin/sh} otherwise.
17872 Invoking @command{configure} by hand bypasses this setting, so you may
17873 need to use a command like @samp{CONFIG_SHELL=/bin/bash /bin/bash ./configure}
17874 to insure that the same shell is used everywhere.  The absolute name of the
17875 shell should be passed.
17876 @end defvar
17878 @defvar CONFIG_STATUS
17879 @evindex CONFIG_STATUS
17880 The file name to use for the shell script that records the
17881 configuration.  The default is @file{./config.status}.  This variable is
17882 useful when one package uses parts of another and the @command{configure}
17883 scripts shouldn't be merged because they are maintained separately.
17884 @end defvar
17886 You can use @file{./config.status} in your makefiles.  For example, in
17887 the dependencies given above (@pxref{Automatic Remaking}),
17888 @file{config.status} is run twice when @file{configure.ac} has changed.
17889 If that bothers you, you can make each run only regenerate the files for
17890 that rule:
17891 @example
17892 @group
17893 config.h: stamp-h
17894 stamp-h: config.h.in config.status
17895         ./config.status config.h
17896         echo > stamp-h
17898 Makefile: Makefile.in config.status
17899         ./config.status Makefile
17900 @end group
17901 @end example
17903 The calling convention of @file{config.status} has changed; see
17904 @ref{Obsolete config.status Use}, for details.
17907 @c =================================================== Obsolete Constructs
17909 @node Obsolete Constructs
17910 @chapter Obsolete Constructs
17911 @cindex Obsolete constructs
17913 Autoconf changes, and throughout the years some constructs have been
17914 obsoleted.  Most of the changes involve the macros, but in some cases
17915 the tools themselves, or even some concepts, are now considered
17916 obsolete.
17918 You may completely skip this chapter if you are new to Autoconf.  Its
17919 intention is mainly to help maintainers updating their packages by
17920 understanding how to move to more modern constructs.
17922 @menu
17923 * Obsolete config.status Use::  Obsolete convention for @command{config.status}
17924 * acconfig Header::             Additional entries in @file{config.h.in}
17925 * autoupdate Invocation::       Automatic update of @file{configure.ac}
17926 * Obsolete Macros::             Backward compatibility macros
17927 * Autoconf 1::                  Tips for upgrading your files
17928 * Autoconf 2.13::               Some fresher tips
17929 @end menu
17931 @node Obsolete config.status Use
17932 @section Obsolete @file{config.status} Invocation
17934 @file{config.status} now supports arguments to specify the files to
17935 instantiate; see @ref{config.status Invocation}, for more details.
17936 Before, environment variables had to be used.
17938 @defvar CONFIG_COMMANDS
17939 @evindex CONFIG_COMMANDS
17940 The tags of the commands to execute.  The default is the arguments given
17941 to @code{AC_OUTPUT} and @code{AC_CONFIG_COMMANDS} in
17942 @file{configure.ac}.
17943 @end defvar
17945 @defvar CONFIG_FILES
17946 @evindex CONFIG_FILES
17947 The files in which to perform @samp{@@@var{variable}@@} substitutions.
17948 The default is the arguments given to @code{AC_OUTPUT} and
17949 @code{AC_CONFIG_FILES} in @file{configure.ac}.
17950 @end defvar
17952 @defvar CONFIG_HEADERS
17953 @evindex CONFIG_HEADERS
17954 The files in which to substitute C @code{#define} statements.  The
17955 default is the arguments given to @code{AC_CONFIG_HEADERS}; if that
17956 macro was not called, @file{config.status} ignores this variable.
17957 @end defvar
17959 @defvar CONFIG_LINKS
17960 @evindex CONFIG_LINKS
17961 The symbolic links to establish.  The default is the arguments given to
17962 @code{AC_CONFIG_LINKS}; if that macro was not called,
17963 @file{config.status} ignores this variable.
17964 @end defvar
17966 In @ref{config.status Invocation}, using this old interface, the example
17967 would be:
17969 @example
17970 @group
17971 config.h: stamp-h
17972 stamp-h: config.h.in config.status
17973         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_FILES= \
17974           CONFIG_HEADERS=config.h ./config.status
17975         echo > stamp-h
17977 Makefile: Makefile.in config.status
17978         CONFIG_COMMANDS= CONFIG_LINKS= CONFIG_HEADERS= \
17979           CONFIG_FILES=Makefile ./config.status
17980 @end group
17981 @end example
17983 @noindent
17984 (If @file{configure.ac} does not call @code{AC_CONFIG_HEADERS}, there is
17985 no need to set @code{CONFIG_HEADERS} in the @code{make} rules.  Equally
17986 for @code{CONFIG_COMMANDS}, etc.)
17989 @node acconfig Header
17990 @section @file{acconfig.h}
17992 @cindex @file{acconfig.h}
17993 @cindex @file{config.h.top}
17994 @cindex @file{config.h.bot}
17996 In order to produce @file{config.h.in}, @command{autoheader} needs to
17997 build or to find templates for each symbol.  Modern releases of Autoconf
17998 use @code{AH_VERBATIM} and @code{AH_TEMPLATE} (@pxref{Autoheader
17999 Macros}), but in older releases a file, @file{acconfig.h}, contained the
18000 list of needed templates.  @command{autoheader} copied comments and
18001 @code{#define} and @code{#undef} statements from @file{acconfig.h} in
18002 the current directory, if present.  This file used to be mandatory if
18003 you @code{AC_DEFINE} any additional symbols.
18005 Modern releases of Autoconf also provide @code{AH_TOP} and
18006 @code{AH_BOTTOM} if you need to prepend/append some information to
18007 @file{config.h.in}.  Ancient versions of Autoconf had a similar feature:
18008 if @file{./acconfig.h} contains the string @samp{@@TOP@@},
18009 @command{autoheader} copies the lines before the line containing
18010 @samp{@@TOP@@} into the top of the file that it generates.  Similarly,
18011 if @file{./acconfig.h} contains the string @samp{@@BOTTOM@@},
18012 @command{autoheader} copies the lines after that line to the end of the
18013 file it generates.  Either or both of those strings may be omitted.  An
18014 even older alternate way to produce the same effect in ancient versions
18015 of Autoconf is to create the files @file{@var{file}.top} (typically
18016 @file{config.h.top}) and/or @file{@var{file}.bot} in the current
18017 directory.  If they exist, @command{autoheader} copies them to the
18018 beginning and end, respectively, of its output.
18020 In former versions of Autoconf, the files used in preparing a software
18021 package for distribution were:
18022 @example
18023 @group
18024 configure.ac --.   .------> autoconf* -----> configure
18025                +---+
18026 [aclocal.m4] --+   `---.
18027 [acsite.m4] ---'       |
18028                        +--> [autoheader*] -> [config.h.in]
18029 [acconfig.h] ----.     |
18030                  +-----'
18031 [config.h.top] --+
18032 [config.h.bot] --'
18033 @end group
18034 @end example
18036 Using only the @code{AH_} macros, @file{configure.ac} should be
18037 self-contained, and should not depend upon @file{acconfig.h} etc.
18040 @node autoupdate Invocation
18041 @section Using @command{autoupdate} to Modernize @file{configure.ac}
18042 @cindex @command{autoupdate}
18044 The @command{autoupdate} program updates a @file{configure.ac} file that
18045 calls Autoconf macros by their old names to use the current macro names.
18046 In version 2 of Autoconf, most of the macros were renamed to use a more
18047 uniform and descriptive naming scheme.  @xref{Macro Names}, for a
18048 description of the new scheme.  Although the old names still work
18049 (@pxref{Obsolete Macros}, for a list of the old macros and the corresponding
18050 new names), you can make your @file{configure.ac} files more readable
18051 and make it easier to use the current Autoconf documentation if you
18052 update them to use the new macro names.
18054 @evindex SIMPLE_BACKUP_SUFFIX
18055 If given no arguments, @command{autoupdate} updates @file{configure.ac},
18056 backing up the original version with the suffix @file{~} (or the value
18057 of the environment variable @code{SIMPLE_BACKUP_SUFFIX}, if that is
18058 set).  If you give @command{autoupdate} an argument, it reads that file
18059 instead of @file{configure.ac} and writes the updated file to the
18060 standard output.
18062 @noindent
18063 @command{autoupdate} accepts the following options:
18065 @table @option
18066 @item --help
18067 @itemx -h
18068 Print a summary of the command line options and exit.
18070 @item --version
18071 @itemx -V
18072 Print the version number of Autoconf and exit.
18074 @item --verbose
18075 @itemx -v
18076 Report processing steps.
18078 @item --debug
18079 @itemx -d
18080 Don't remove the temporary files.
18082 @item --force
18083 @itemx -f
18084 Force the update even if the file has not changed.  Disregard the cache.
18086 @item --include=@var{dir}
18087 @itemx -I @var{dir}
18088 Also look for input files in @var{dir}.  Multiple invocations accumulate.
18089 Directories are browsed from last to first.
18090 @end table
18092 @node Obsolete Macros
18093 @section Obsolete Macros
18095 Several macros are obsoleted in Autoconf, for various reasons (typically
18096 they failed to quote properly, couldn't be extended for more recent
18097 issues, etc.).  They are still supported, but deprecated: their use
18098 should be avoided.
18100 During the jump from Autoconf version 1 to version 2, most of the
18101 macros were renamed to use a more uniform and descriptive naming scheme,
18102 but their signature did not change.  @xref{Macro Names}, for a
18103 description of the new naming scheme.  Below, if there is just the mapping
18104 from old names to new names for these macros, the reader is invited to
18105 refer to the definition of the new macro for the signature and the
18106 description.
18108 @defmac AC_AIX
18109 @acindex{AIX}
18110 @cvindex _ALL_SOURCE
18111 This macro is a platform-specific subset of
18112 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
18113 @end defmac
18115 @defmac AC_ALLOCA
18116 @acindex{ALLOCA}
18117 Replaced by @code{AC_FUNC_ALLOCA} (@pxref{AC_FUNC_ALLOCA}).
18118 @end defmac
18120 @defmac AC_ARG_ARRAY
18121 @acindex{ARG_ARRAY}
18122 Removed because of limited usefulness.
18123 @end defmac
18125 @defmac AC_C_CROSS
18126 @acindex{C_CROSS}
18127 This macro is obsolete; it does nothing.
18128 @end defmac
18130 @defmac AC_C_LONG_DOUBLE
18131 @acindex{C_LONG_DOUBLE}
18132 @cvindex HAVE_LONG_DOUBLE
18133 If the C compiler supports a working @code{long double} type with more
18134 range or precision than the @code{double} type, define
18135 @code{HAVE_LONG_DOUBLE}.
18137 You should use @code{AC_TYPE_LONG_DOUBLE} or
18138 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
18139 @end defmac
18141 @defmac AC_CANONICAL_SYSTEM
18142 @acindex{CANONICAL_SYSTEM}
18143 Determine the system type and set output variables to the names of the
18144 canonical system types.  @xref{Canonicalizing}, for details about the
18145 variables this macro sets.
18147 The user is encouraged to use either @code{AC_CANONICAL_BUILD}, or
18148 @code{AC_CANONICAL_HOST}, or @code{AC_CANONICAL_TARGET}, depending on
18149 the needs.  Using @code{AC_CANONICAL_TARGET} is enough to run the two
18150 other macros (@pxref{Canonicalizing}).
18151 @end defmac
18153 @defmac AC_CHAR_UNSIGNED
18154 @acindex{CHAR_UNSIGNED}
18155 Replaced by @code{AC_C_CHAR_UNSIGNED} (@pxref{AC_C_CHAR_UNSIGNED}).
18156 @end defmac
18158 @defmac AC_CHECK_TYPE (@var{type}, @var{default})
18159 @acindex{CHECK_TYPE}
18160 Autoconf, up to 2.13, used to provide this version of
18161 @code{AC_CHECK_TYPE}, deprecated because of its flaws.  First, although
18162 it is a member of the @code{CHECK} clan, it does
18163 more than just checking.  Secondly, missing types are defined
18164 using @code{#define}, not @code{typedef}, and this can lead to
18165 problems in the case of pointer types.
18167 This use of @code{AC_CHECK_TYPE} is obsolete and discouraged; see
18168 @ref{Generic Types}, for the description of the current macro.
18170 If the type @var{type} is not defined, define it to be the C (or C++)
18171 builtin type @var{default}, e.g., @samp{short int} or @samp{unsigned int}.
18173 This macro is equivalent to:
18175 @example
18176 AC_CHECK_TYPE([@var{type}], [],
18177   [AC_DEFINE_UNQUOTED([@var{type}], [@var{default}],
18178      [Define to `@var{default}'
18179       if <sys/types.h> does not define.])])
18180 @end example
18182 In order to keep backward compatibility, the two versions of
18183 @code{AC_CHECK_TYPE} are implemented, selected using these heuristics:
18185 @enumerate
18186 @item
18187 If there are three or four arguments, the modern version is used.
18189 @item
18190 If the second argument appears to be a C or C++ type, then the
18191 obsolete version is used.  This happens if the argument is a C or C++
18192 @emph{builtin} type or a C identifier ending in @samp{_t}, optionally
18193 followed by one of @samp{[(* } and then by a string of zero or more
18194 characters taken from the set @samp{[]()* _a-zA-Z0-9}.
18196 @item
18197 If the second argument is spelled with the alphabet of valid C and C++
18198 types, the user is warned and the modern version is used.
18200 @item
18201 Otherwise, the modern version is used.
18202 @end enumerate
18204 @noindent
18205 You are encouraged either to use a valid builtin type, or to use the
18206 equivalent modern code (see above), or better yet, to use
18207 @code{AC_CHECK_TYPES} together with
18209 @example
18210 #ifndef HAVE_LOFF_T
18211 typedef loff_t off_t;
18212 #endif
18213 @end example
18214 @end defmac
18215 @c end of AC_CHECK_TYPE
18217 @defmac AC_CHECKING (@var{feature-description})
18218 @acindex{CHECKING}
18219 Same as
18221 @example
18222 AC_MSG_NOTICE([checking @var{feature-description}@dots{}]
18223 @end example
18225 @noindent
18226 @xref{AC_MSG_NOTICE}.
18227 @end defmac
18229 @defmac AC_COMPILE_CHECK (@var{echo-text}, @var{includes}, @
18230   @var{function-body}, @var{action-if-true}, @ovar{action-if-false})
18231 @acindex{COMPILE_CHECK}
18232 This is an obsolete version of @code{AC_TRY_COMPILE} itself replaced by
18233 @code{AC_COMPILE_IFELSE} (@pxref{Running the Compiler}), with the
18234 addition that it prints @samp{checking for @var{echo-text}} to the
18235 standard output first, if @var{echo-text} is non-empty.  Use
18236 @code{AC_MSG_CHECKING} and @code{AC_MSG_RESULT} instead to print
18237 messages (@pxref{Printing Messages}).
18238 @end defmac
18240 @defmac AC_CONST
18241 @acindex{CONST}
18242 Replaced by @code{AC_C_CONST} (@pxref{AC_C_CONST}).
18243 @end defmac
18245 @defmac AC_CROSS_CHECK
18246 @acindex{CROSS_CHECK}
18247 Same as @code{AC_C_CROSS}, which is obsolete too, and does nothing
18248 @code{:-)}.
18249 @end defmac
18251 @defmac AC_CYGWIN
18252 @acindex{CYGWIN}
18253 @evindex CYGWIN
18254 Check for the Cygwin environment in which case the shell variable
18255 @code{CYGWIN} is set to @samp{yes}.  Don't use this macro, the dignified
18256 means to check the nature of the host is using @code{AC_CANONICAL_HOST}
18257 (@pxref{Canonicalizing}).  As a matter of fact this macro is defined as:
18259 @example
18260 AC_REQUIRE([AC_CANONICAL_HOST])[]dnl
18261 case $host_os in
18262   *cygwin* ) CYGWIN=yes;;
18263          * ) CYGWIN=no;;
18264 esac
18265 @end example
18267 Beware that the variable @env{CYGWIN} has a special meaning when
18268 running Cygwin, and should not be changed.  That's yet another reason
18269 not to use this macro.
18270 @end defmac
18272 @defmac AC_DECL_SYS_SIGLIST
18273 @acindex{DECL_SYS_SIGLIST}
18274 @cvindex SYS_SIGLIST_DECLARED
18275 Same as:
18277 @example
18278 AC_CHECK_DECLS([sys_siglist], [], [],
18279 [#include <signal.h>
18280 /* NetBSD declares sys_siglist in unistd.h.  */
18281 #ifdef HAVE_UNISTD_H
18282 # include <unistd.h>
18283 #endif
18285 @end example
18287 @noindent
18288 @xref{AC_CHECK_DECLS}.
18289 @end defmac
18291 @defmac AC_DECL_YYTEXT
18292 @acindex{DECL_YYTEXT}
18293 Does nothing, now integrated in @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
18294 @end defmac
18296 @defmac AC_DIR_HEADER
18297 @acindex{DIR_HEADER}
18298 @cvindex DIRENT
18299 @cvindex SYSNDIR
18300 @cvindex SYSDIR
18301 @cvindex NDIR
18302 Like calling @code{AC_FUNC_CLOSEDIR_VOID}
18303 (@pxref{AC_FUNC_CLOSEDIR_VOID}) and @code{AC_HEADER_DIRENT}
18304 (@pxref{AC_HEADER_DIRENT}),
18305 but defines a different set of C preprocessor macros to indicate which
18306 header file is found:
18308 @multitable {@file{sys/ndir.h}} {Old Symbol} {@code{HAVE_SYS_NDIR_H}}
18309 @item Header            @tab Old Symbol     @tab New Symbol
18310 @item @file{dirent.h}   @tab @code{DIRENT}  @tab @code{HAVE_DIRENT_H}
18311 @item @file{sys/ndir.h} @tab @code{SYSNDIR} @tab @code{HAVE_SYS_NDIR_H}
18312 @item @file{sys/dir.h}  @tab @code{SYSDIR}  @tab @code{HAVE_SYS_DIR_H}
18313 @item @file{ndir.h}     @tab @code{NDIR}    @tab @code{HAVE_NDIR_H}
18314 @end multitable
18315 @end defmac
18317 @defmac AC_DYNIX_SEQ
18318 @acindex{DYNIX_SEQ}
18319 If on DYNIX/ptx, add @option{-lseq} to output variable
18320 @code{LIBS}.  This macro used to be defined as
18322 @example
18323 AC_CHECK_LIB([seq], [getmntent], [LIBS="-lseq $LIBS"])
18324 @end example
18326 @noindent
18327 now it is just @code{AC_FUNC_GETMNTENT} (@pxref{AC_FUNC_GETMNTENT}).
18328 @end defmac
18330 @defmac AC_EXEEXT
18331 @acindex{EXEEXT}
18332 @ovindex EXEEXT
18333 Defined the output variable @code{EXEEXT} based on the output of the
18334 compiler, which is now done automatically.  Typically set to empty
18335 string if Posix and @samp{.exe} if a @acronym{DOS} variant.
18336 @end defmac
18338 @defmac AC_EMXOS2
18339 @acindex{EMXOS2}
18340 Similar to @code{AC_CYGWIN} but checks for the EMX environment on OS/2
18341 and sets @code{EMXOS2}.  Don't use this macro, the dignified means to
18342 check the nature of the host is using @code{AC_CANONICAL_HOST}
18343 (@pxref{Canonicalizing}).
18344 @end defmac
18346 @defmac AC_ENABLE (@var{feature}, @var{action-if-given}, @
18347   @ovar{action-if-not-given})
18348 @acindex{ENABLE}
18349 This is an obsolete version of @code{AC_ARG_ENABLE} that does not
18350 support providing a help string (@pxref{AC_ARG_ENABLE}).
18351 @end defmac
18353 @defmac AC_ERROR
18354 @acindex{ERROR}
18355 Replaced by @code{AC_MSG_ERROR} (@pxref{AC_MSG_ERROR}).
18356 @end defmac
18358 @defmac AC_FIND_X
18359 @acindex{FIND_X}
18360 Replaced by @code{AC_PATH_X} (@pxref{AC_PATH_X}).
18361 @end defmac
18363 @defmac AC_FIND_XTRA
18364 @acindex{FIND_XTRA}
18365 Replaced by @code{AC_PATH_XTRA} (@pxref{AC_PATH_XTRA}).
18366 @end defmac
18368 @defmac AC_FOREACH
18369 @acindex{FOREACH}
18370 Replaced by @code{m4_foreach_w} (@pxref{m4_foreach_w}).
18371 @end defmac
18373 @defmac AC_FUNC_CHECK
18374 @acindex{FUNC_CHECK}
18375 Replaced by @code{AC_CHECK_FUNC} (@pxref{AC_CHECK_FUNC}).
18376 @end defmac
18378 @anchor{AC_FUNC_SETVBUF_REVERSED}
18379 @defmac AC_FUNC_SETVBUF_REVERSED
18380 @acindex{FUNC_SETVBUF_REVERSED}
18381 @cvindex SETVBUF_REVERSED
18382 @c @fuindex setvbuf
18383 @prindex @code{setvbuf}
18384 Do nothing.  Formerly, this macro checked whether @code{setvbuf} takes
18385 the buffering type as its second argument and the buffer pointer as the
18386 third, instead of the other way around, and defined
18387 @code{SETVBUF_REVERSED}.  However, the last systems to have the problem
18388 were those based on SVR2, which became obsolete in 1987, and the macro
18389 is no longer needed.
18390 @end defmac
18392 @defmac AC_FUNC_WAIT3
18393 @acindex{FUNC_WAIT3}
18394 @cvindex HAVE_WAIT3
18395 If @code{wait3} is found and fills in the contents of its third argument
18396 (a @samp{struct rusage *}), which @acronym{HP-UX} does not do, define
18397 @code{HAVE_WAIT3}.
18399 These days portable programs should use @code{waitpid}, not
18400 @code{wait3}, as @code{wait3} has been removed from Posix.
18401 @end defmac
18403 @defmac AC_GCC_TRADITIONAL
18404 @acindex{GCC_TRADITIONAL}
18405 Replaced by @code{AC_PROG_GCC_TRADITIONAL} (@pxref{AC_PROG_GCC_TRADITIONAL}).
18406 @end defmac
18408 @defmac AC_GETGROUPS_T
18409 @acindex{GETGROUPS_T}
18410 Replaced by @code{AC_TYPE_GETGROUPS} (@pxref{AC_TYPE_GETGROUPS}).
18411 @end defmac
18413 @defmac AC_GETLOADAVG
18414 @acindex{GETLOADAVG}
18415 Replaced by @code{AC_FUNC_GETLOADAVG} (@pxref{AC_FUNC_GETLOADAVG}).
18416 @end defmac
18418 @defmac AC_GNU_SOURCE
18419 @acindex{GNU_SOURCE}
18420 @cvindex _GNU_SOURCE
18421 This macro is a platform-specific subset of
18422 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
18423 @end defmac
18425 @defmac AC_HAVE_FUNCS
18426 @acindex{HAVE_FUNCS}
18427 Replaced by @code{AC_CHECK_FUNCS} (@pxref{AC_CHECK_FUNCS}).
18428 @end defmac
18430 @defmac AC_HAVE_HEADERS
18431 @acindex{HAVE_HEADERS}
18432 Replaced by @code{AC_CHECK_HEADERS} (@pxref{AC_CHECK_HEADERS}).
18433 @end defmac
18435 @defmac AC_HAVE_LIBRARY (@var{library}, @ovar{action-if-found}, @
18436   @ovar{action-if-not-found}, @ovar{other-libraries})
18437 @acindex{HAVE_LIBRARY}
18438 This macro is equivalent to calling @code{AC_CHECK_LIB} with a
18439 @var{function} argument of @code{main}.  In addition, @var{library} can
18440 be written as any of @samp{foo}, @option{-lfoo}, or @samp{libfoo.a}.  In
18441 all of those cases, the compiler is passed @option{-lfoo}.  However,
18442 @var{library} cannot be a shell variable; it must be a literal name.
18443 @xref{AC_CHECK_LIB}.
18444 @end defmac
18446 @defmac AC_HAVE_POUNDBANG
18447 @acindex{HAVE_POUNDBANG}
18448 Replaced by @code{AC_SYS_INTERPRETER} (@pxref{AC_SYS_INTERPRETER}).
18449 @end defmac
18451 @defmac AC_HEADER_CHECK
18452 @acindex{HEADER_CHECK}
18453 Replaced by @code{AC_CHECK_HEADER} (@pxref{AC_CHECK_HEADER}).
18454 @end defmac
18456 @defmac AC_HEADER_EGREP
18457 @acindex{HEADER_EGREP}
18458 Replaced by @code{AC_EGREP_HEADER} (@pxref{AC_EGREP_HEADER}).
18459 @end defmac
18461 @defmac AC_HELP_STRING
18462 @acindex{HELP_STRING}
18463 Replaced by @code{AS_HELP_STRING} (@pxref{AS_HELP_STRING}).
18464 @end defmac
18466 @defmac AC_INIT (@var{unique-file-in-source-dir})
18467 @acindex{INIT}
18468 Formerly @code{AC_INIT} used to have a single argument, and was
18469 equivalent to:
18471 @example
18472 AC_INIT
18473 AC_CONFIG_SRCDIR(@var{unique-file-in-source-dir})
18474 @end example
18475 See @ref{AC_INIT} and @ref{AC_CONFIG_SRCDIR}.
18476 @end defmac
18478 @defmac AC_INLINE
18479 @acindex{INLINE}
18480 Replaced by @code{AC_C_INLINE} (@pxref{AC_C_INLINE}).
18481 @end defmac
18483 @defmac AC_INT_16_BITS
18484 @acindex{INT_16_BITS}
18485 @cvindex INT_16_BITS
18486 If the C type @code{int} is 16 bits wide, define @code{INT_16_BITS}.
18487 Use @samp{AC_CHECK_SIZEOF(int)} instead (@pxref{AC_CHECK_SIZEOF}).
18488 @end defmac
18490 @defmac AC_IRIX_SUN
18491 @acindex{IRIX_SUN}
18492 If on @sc{irix} (Silicon Graphics Unix), add @option{-lsun} to output
18493 @code{LIBS}.  If you were using it to get @code{getmntent}, use
18494 @code{AC_FUNC_GETMNTENT} instead.  If you used it for the NIS versions
18495 of the password and group functions, use @samp{AC_CHECK_LIB(sun,
18496 getpwnam)}.  Up to Autoconf 2.13, it used to be
18498 @example
18499 AC_CHECK_LIB([sun], [getmntent], [LIBS="-lsun $LIBS"])
18500 @end example
18502 @noindent
18503 now it is defined as
18505 @example
18506 AC_FUNC_GETMNTENT
18507 AC_CHECK_LIB([sun], [getpwnam])
18508 @end example
18510 @noindent
18511 See @ref{AC_FUNC_GETMNTENT} and @ref{AC_CHECK_LIB}.
18512 @end defmac
18514 @defmac AC_ISC_POSIX
18515 @acindex{ISC_POSIX}
18516 @ovindex LIBS
18517 This macro adds @option{-lcposix} to output variable @code{LIBS} if
18518 necessary for Posix facilities.  Sun dropped support for the obsolete
18519 @sc{interactive} Systems Corporation Unix on 2006-07-23.  New programs
18520 need not use this macro.  It is implemented as
18521 @code{AC_SEARCH_LIBS([strerror], [cposix])} (@pxref{AC_SEARCH_LIBS}).
18522 @end defmac
18524 @defmac AC_LANG_C
18525 @acindex{LANG_C}
18526 Same as @samp{AC_LANG([C])} (@pxref{AC_LANG}).
18527 @end defmac
18529 @defmac AC_LANG_CPLUSPLUS
18530 @acindex{LANG_CPLUSPLUS}
18531 Same as @samp{AC_LANG([C++])} (@pxref{AC_LANG}).
18532 @end defmac
18534 @defmac AC_LANG_FORTRAN77
18535 @acindex{LANG_FORTRAN77}
18536 Same as @samp{AC_LANG([Fortran 77])} (@pxref{AC_LANG}).
18537 @end defmac
18539 @defmac AC_LANG_RESTORE
18540 @acindex{LANG_RESTORE}
18541 Select the @var{language} that is saved on the top of the stack, as set
18542 by @code{AC_LANG_SAVE}, remove it from the stack, and call
18543 @code{AC_LANG(@var{language})}.  @xref{Language Choice}, for the
18544 preferred way to change languages.
18545 @end defmac
18547 @defmac AC_LANG_SAVE
18548 @acindex{LANG_SAVE}
18549 Remember the current language (as set by @code{AC_LANG}) on a stack.
18550 The current language does not change.  @code{AC_LANG_PUSH} is preferred
18551 (@pxref{AC_LANG_PUSH}).
18552 @end defmac
18554 @defmac AC_LINK_FILES (@var{source}@dots{}, @var{dest}@dots{})
18555 @acindex{LINK_FILES}
18556 This is an obsolete version of @code{AC_CONFIG_LINKS}
18557 (@pxref{AC_CONFIG_LINKS}.  An updated version of:
18559 @example
18560 AC_LINK_FILES(config/$machine.h config/$obj_format.h,
18561               host.h            object.h)
18562 @end example
18564 @noindent
18567 @example
18568 AC_CONFIG_LINKS([host.h:config/$machine.h
18569                 object.h:config/$obj_format.h])
18570 @end example
18571 @end defmac
18573 @defmac AC_LN_S
18574 @acindex{LN_S}
18575 Replaced by @code{AC_PROG_LN_S} (@pxref{AC_PROG_LN_S}).
18576 @end defmac
18578 @defmac AC_LONG_64_BITS
18579 @acindex{LONG_64_BITS}
18580 @cvindex LONG_64_BITS
18581 Define @code{LONG_64_BITS} if the C type @code{long int} is 64 bits wide.
18582 Use the generic macro @samp{AC_CHECK_SIZEOF([long int])} instead
18583 (@pxref{AC_CHECK_SIZEOF}).
18584 @end defmac
18586 @defmac AC_LONG_DOUBLE
18587 @acindex{LONG_DOUBLE}
18588 If the C compiler supports a working @code{long double} type with more
18589 range or precision than the @code{double} type, define
18590 @code{HAVE_LONG_DOUBLE}.
18592 You should use @code{AC_TYPE_LONG_DOUBLE} or
18593 @code{AC_TYPE_LONG_DOUBLE_WIDER} instead.  @xref{Particular Types}.
18594 @end defmac
18596 @defmac AC_LONG_FILE_NAMES
18597 @acindex{LONG_FILE_NAMES}
18598 Replaced by
18599 @example
18600 AC_SYS_LONG_FILE_NAMES
18601 @end example
18602 @noindent
18603 @xref{AC_SYS_LONG_FILE_NAMES}.
18604 @end defmac
18606 @defmac AC_MAJOR_HEADER
18607 @acindex{MAJOR_HEADER}
18608 Replaced by @code{AC_HEADER_MAJOR} (@pxref{AC_HEADER_MAJOR}).
18609 @end defmac
18611 @defmac AC_MEMORY_H
18612 @acindex{MEMORY_H}
18613 @cvindex NEED_MEMORY_H
18614 Used to define @code{NEED_MEMORY_H} if the @code{mem} functions were
18615 defined in @file{memory.h}.  Today it is equivalent to
18616 @samp{AC_CHECK_HEADERS([memory.h])} (@pxref{AC_CHECK_HEADERS}).  Adjust
18617 your code to depend upon
18618 @code{HAVE_MEMORY_H}, not @code{NEED_MEMORY_H}; see @ref{Standard
18619 Symbols}.
18620 @end defmac
18622 @defmac AC_MINGW32
18623 @acindex{MINGW32}
18624 Similar to @code{AC_CYGWIN} but checks for the MinGW compiler
18625 environment and sets @code{MINGW32}.  Don't use this macro, the
18626 dignified means to check the nature of the host is using
18627 @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
18628 @end defmac
18630 @defmac AC_MINIX
18631 @acindex{MINIX}
18632 @cvindex _MINIX
18633 @cvindex _POSIX_SOURCE
18634 @cvindex _POSIX_1_SOURCE
18635 This macro is a platform-specific subset of
18636 @code{AC_USE_SYSTEM_EXTENSIONS} (@pxref{AC_USE_SYSTEM_EXTENSIONS}).
18637 @end defmac
18639 @defmac AC_MINUS_C_MINUS_O
18640 @acindex{MINUS_C_MINUS_O}
18641 Replaced by @code{AC_PROG_CC_C_O} (@pxref{AC_PROG_CC_C_O}).
18642 @end defmac
18644 @defmac AC_MMAP
18645 @acindex{MMAP}
18646 Replaced by @code{AC_FUNC_MMAP} (@pxref{AC_FUNC_MMAP}).
18647 @end defmac
18649 @defmac AC_MODE_T
18650 @acindex{MODE_T}
18651 Replaced by @code{AC_TYPE_MODE_T} (@pxref{AC_TYPE_MODE_T}).
18652 @end defmac
18654 @defmac AC_OBJEXT
18655 @acindex{OBJEXT}
18656 @ovindex OBJEXT
18657 Defined the output variable @code{OBJEXT} based on the output of the
18658 compiler, after .c files have been excluded.  Typically set to @samp{o}
18659 if Posix, @samp{obj} if a @acronym{DOS} variant.
18660 Now the compiler checking macros handle
18661 this automatically.
18662 @end defmac
18664 @defmac AC_OBSOLETE (@var{this-macro-name}, @ovar{suggestion})
18665 @acindex{OBSOLETE}
18666 Make M4 print a message to the standard error output warning that
18667 @var{this-macro-name} is obsolete, and giving the file and line number
18668 where it was called.  @var{this-macro-name} should be the name of the
18669 macro that is calling @code{AC_OBSOLETE}.  If @var{suggestion} is given,
18670 it is printed at the end of the warning message; for example, it can be
18671 a suggestion for what to use instead of @var{this-macro-name}.
18673 For instance
18675 @example
18676 AC_OBSOLETE([$0], [; use AC_CHECK_HEADERS(unistd.h) instead])dnl
18677 @end example
18679 @noindent
18680 You are encouraged to use @code{AU_DEFUN} instead, since it gives better
18681 services to the user (@pxref{AU_DEFUN}).
18682 @end defmac
18684 @defmac AC_OFF_T
18685 @acindex{OFF_T}
18686 Replaced by @code{AC_TYPE_OFF_T} (@pxref{AC_TYPE_OFF_T}).
18687 @end defmac
18689 @defmac AC_OUTPUT (@ovar{file}@dots{}, @ovar{extra-cmds}, @ovar{init-cmds})
18690 @acindex{OUTPUT}
18691 The use of @code{AC_OUTPUT} with arguments is deprecated.  This obsoleted
18692 interface is equivalent to:
18694 @example
18695 @group
18696 AC_CONFIG_FILES(@var{file}@dots{})
18697 AC_CONFIG_COMMANDS([default],
18698                    @var{extra-cmds}, @var{init-cmds})
18699 AC_OUTPUT
18700 @end group
18701 @end example
18703 @noindent
18704 See @ref{AC_CONFIG_FILES}, @ref{AC_CONFIG_COMMANDS}, and @ref{AC_OUTPUT}.
18705 @end defmac
18707 @defmac AC_OUTPUT_COMMANDS (@var{extra-cmds}, @ovar{init-cmds})
18708 @acindex{OUTPUT_COMMANDS}
18709 Specify additional shell commands to run at the end of
18710 @file{config.status}, and shell commands to initialize any variables
18711 from @command{configure}.  This macro may be called multiple times.  It is
18712 obsolete, replaced by @code{AC_CONFIG_COMMANDS} (@pxref{AC_CONFIG_COMMANDS}).
18714 Here is an unrealistic example:
18716 @example
18717 fubar=27
18718 AC_OUTPUT_COMMANDS([echo this is extra $fubar, and so on.],
18719                    [fubar=$fubar])
18720 AC_OUTPUT_COMMANDS([echo this is another, extra, bit],
18721                    [echo init bit])
18722 @end example
18724 Aside from the fact that @code{AC_CONFIG_COMMANDS} requires an
18725 additional key, an important difference is that
18726 @code{AC_OUTPUT_COMMANDS} is quoting its arguments twice, unlike
18727 @code{AC_CONFIG_COMMANDS}.  This means that @code{AC_CONFIG_COMMANDS}
18728 can safely be given macro calls as arguments:
18730 @example
18731 AC_CONFIG_COMMANDS(foo, [my_FOO()])
18732 @end example
18734 @noindent
18735 Conversely, where one level of quoting was enough for literal strings
18736 with @code{AC_OUTPUT_COMMANDS}, you need two with
18737 @code{AC_CONFIG_COMMANDS}.  The following lines are equivalent:
18739 @example
18740 @group
18741 AC_OUTPUT_COMMANDS([echo "Square brackets: []"])
18742 AC_CONFIG_COMMANDS([default], [[echo "Square brackets: []"]])
18743 @end group
18744 @end example
18745 @end defmac
18747 @defmac AC_PID_T
18748 @acindex{PID_T}
18749 Replaced by @code{AC_TYPE_PID_T} (@pxref{AC_TYPE_PID_T}).
18750 @end defmac
18752 @defmac AC_PREFIX
18753 @acindex{PREFIX}
18754 Replaced by @code{AC_PREFIX_PROGRAM} (@pxref{AC_PREFIX_PROGRAM}).
18755 @end defmac
18757 @defmac AC_PROGRAMS_CHECK
18758 @acindex{PROGRAMS_CHECK}
18759 Replaced by @code{AC_CHECK_PROGS} (@pxref{AC_CHECK_PROGS}).
18760 @end defmac
18762 @defmac AC_PROGRAMS_PATH
18763 @acindex{PROGRAMS_PATH}
18764 Replaced by @code{AC_PATH_PROGS} (@pxref{AC_PATH_PROGS}).
18765 @end defmac
18767 @defmac AC_PROGRAM_CHECK
18768 @acindex{PROGRAM_CHECK}
18769 Replaced by @code{AC_CHECK_PROG} (@pxref{AC_CHECK_PROG}).
18770 @end defmac
18772 @defmac AC_PROGRAM_EGREP
18773 @acindex{PROGRAM_EGREP}
18774 Replaced by @code{AC_EGREP_CPP} (@pxref{AC_EGREP_CPP}).
18775 @end defmac
18777 @defmac AC_PROGRAM_PATH
18778 @acindex{PROGRAM_PATH}
18779 Replaced by @code{AC_PATH_PROG} (@pxref{AC_PATH_PROG}).
18780 @end defmac
18782 @defmac AC_REMOTE_TAPE
18783 @acindex{REMOTE_TAPE}
18784 Removed because of limited usefulness.
18785 @end defmac
18787 @defmac AC_RESTARTABLE_SYSCALLS
18788 @acindex{RESTARTABLE_SYSCALLS}
18789 This macro was renamed @code{AC_SYS_RESTARTABLE_SYSCALLS}.  However,
18790 these days portable programs should use @code{sigaction} with
18791 @code{SA_RESTART} if they want restartable system calls.  They should
18792 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
18793 system call is restartable is a dynamic issue, not a configuration-time
18794 issue.
18795 @end defmac
18797 @defmac AC_RETSIGTYPE
18798 @acindex{RETSIGTYPE}
18799 Replaced by @code{AC_TYPE_SIGNAL} (@pxref{AC_TYPE_SIGNAL}).
18800 @end defmac
18802 @defmac AC_RSH
18803 @acindex{RSH}
18804 Removed because of limited usefulness.
18805 @end defmac
18807 @defmac AC_SCO_INTL
18808 @acindex{SCO_INTL}
18809 @ovindex LIBS
18810 If on SCO Unix, add @option{-lintl} to output variable @code{LIBS}.  This
18811 macro used to do this:
18813 @example
18814 AC_CHECK_LIB([intl], [strftime], [LIBS="-lintl $LIBS"])
18815 @end example
18817 @noindent
18818 Now it just calls @code{AC_FUNC_STRFTIME} instead (@pxref{AC_FUNC_STRFTIME}).
18819 @end defmac
18821 @defmac AC_SETVBUF_REVERSED
18822 @acindex{SETVBUF_REVERSED}
18823 Replaced by
18824 @example
18825 AC_FUNC_SETVBUF_REVERSED
18826 @end example
18827 @noindent
18828 @xref{AC_FUNC_SETVBUF_REVERSED}.
18829 @end defmac
18831 @defmac AC_SET_MAKE
18832 @acindex{SET_MAKE}
18833 Replaced by @code{AC_PROG_MAKE_SET} (@pxref{AC_PROG_MAKE_SET}).
18834 @end defmac
18836 @defmac AC_SIZEOF_TYPE
18837 @acindex{SIZEOF_TYPE}
18838 Replaced by @code{AC_CHECK_SIZEOF} (@pxref{AC_CHECK_SIZEOF}).
18839 @end defmac
18841 @defmac AC_SIZE_T
18842 @acindex{SIZE_T}
18843 Replaced by @code{AC_TYPE_SIZE_T} (@pxref{AC_TYPE_SIZE_T}).
18844 @end defmac
18846 @defmac AC_STAT_MACROS_BROKEN
18847 @acindex{STAT_MACROS_BROKEN}
18848 Replaced by @code{AC_HEADER_STAT} (@pxref{AC_HEADER_STAT}).
18849 @end defmac
18851 @defmac AC_STDC_HEADERS
18852 @acindex{STDC_HEADERS}
18853 Replaced by @code{AC_HEADER_STDC} (@pxref{AC_HEADER_STDC}).
18854 @end defmac
18856 @defmac AC_STRCOLL
18857 @acindex{STRCOLL}
18858 Replaced by @code{AC_FUNC_STRCOLL} (@pxref{AC_FUNC_STRCOLL}).
18859 @end defmac
18861 @defmac AC_STRUCT_ST_BLKSIZE
18862 @acindex{STRUCT_ST_BLKSIZE}
18863 @cvindex HAVE_STRUCT_STAT_ST_BLKSIZE
18864 @cvindex HAVE_ST_BLKSIZE
18865 If @code{struct stat} contains an @code{st_blksize} member, define
18866 @code{HAVE_STRUCT_STAT_ST_BLKSIZE}.  The former name,
18867 @code{HAVE_ST_BLKSIZE} is to be avoided, as its support will cease in
18868 the future.  This macro is obsoleted, and should be replaced by
18870 @example
18871 AC_CHECK_MEMBERS([struct stat.st_blksize])
18872 @end example
18873 @noindent
18874 @xref{AC_CHECK_MEMBERS}.
18875 @end defmac
18877 @defmac AC_STRUCT_ST_RDEV
18878 @acindex{STRUCT_ST_RDEV}
18879 @cvindex HAVE_ST_RDEV
18880 @cvindex HAVE_STRUCT_STAT_ST_RDEV
18881 If @code{struct stat} contains an @code{st_rdev} member, define
18882 @code{HAVE_STRUCT_STAT_ST_RDEV}.  The former name for this macro,
18883 @code{HAVE_ST_RDEV}, is to be avoided as it will cease to be supported
18884 in the future.  Actually, even the new macro is obsolete and should be
18885 replaced by:
18886 @example
18887 AC_CHECK_MEMBERS([struct stat.st_rdev])
18888 @end example
18889 @noindent
18890 @xref{AC_CHECK_MEMBERS}.
18891 @end defmac
18893 @defmac AC_ST_BLKSIZE
18894 @acindex{ST_BLKSIZE}
18895 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
18896 @end defmac
18898 @defmac AC_ST_BLOCKS
18899 @acindex{ST_BLOCKS}
18900 Replaced by @code{AC_STRUCT_ST_BLOCKS} (@pxref{AC_STRUCT_ST_BLOCKS}).
18901 @end defmac
18903 @defmac AC_ST_RDEV
18904 @acindex{ST_RDEV}
18905 Replaced by @code{AC_CHECK_MEMBERS} (@pxref{AC_CHECK_MEMBERS}).
18906 @end defmac
18908 @defmac AC_SYS_RESTARTABLE_SYSCALLS
18909 @acindex{SYS_RESTARTABLE_SYSCALLS}
18910 @cvindex HAVE_RESTARTABLE_SYSCALLS
18911 If the system automatically restarts a system call that is interrupted
18912 by a signal, define @code{HAVE_RESTARTABLE_SYSCALLS}.  This macro does
18913 not check whether system calls are restarted in general---it checks whether a
18914 signal handler installed with @code{signal} (but not @code{sigaction})
18915 causes system calls to be restarted.  It does not check whether system calls
18916 can be restarted when interrupted by signals that have no handler.
18918 These days portable programs should use @code{sigaction} with
18919 @code{SA_RESTART} if they want restartable system calls.  They should
18920 not rely on @code{HAVE_RESTARTABLE_SYSCALLS}, since nowadays whether a
18921 system call is restartable is a dynamic issue, not a configuration-time
18922 issue.
18923 @end defmac
18925 @defmac AC_SYS_SIGLIST_DECLARED
18926 @acindex{SYS_SIGLIST_DECLARED}
18927 This macro was renamed @code{AC_DECL_SYS_SIGLIST}.  However, even that
18928 name is obsolete, as the same functionality is now acheived via
18929 @code{AC_CHECK_DECLS} (@pxref{AC_CHECK_DECLS}).
18930 @end defmac
18932 @defmac AC_TEST_CPP
18933 @acindex{TEST_CPP}
18934 This macro was renamed @code{AC_TRY_CPP}, which in turn was replaced by
18935 @code{AC_PREPROC_IFELSE} (@pxref{AC_PREPROC_IFELSE}).
18936 @end defmac
18938 @defmac AC_TEST_PROGRAM
18939 @acindex{TEST_PROGRAM}
18940 This macro was renamed @code{AC_TRY_RUN}, which in turn was replaced by
18941 @code{AC_RUN_IFELSE} (@pxref{AC_RUN_IFELSE}).
18942 @end defmac
18944 @defmac AC_TIMEZONE
18945 @acindex{TIMEZONE}
18946 Replaced by @code{AC_STRUCT_TIMEZONE} (@pxref{AC_STRUCT_TIMEZONE}).
18947 @end defmac
18949 @defmac AC_TIME_WITH_SYS_TIME
18950 @acindex{TIME_WITH_SYS_TIME}
18951 Replaced by @code{AC_HEADER_TIME} (@pxref{AC_HEADER_TIME}).
18952 @end defmac
18954 @defmac AC_TRY_COMPILE (@var{includes}, @var{function-body}, @
18955   @ovar{action-if-true}, @ovar{action-if-false})
18956 @acindex{TRY_COMPILE}
18957 Same as:
18959 @example
18960 AC_COMPILE_IFELSE(
18961   [AC_LANG_PROGRAM([[@var{includes}]],
18962      [[@var{function-body}]])],
18963   [@var{action-if-true}],
18964   [@var{action-if-false}])
18965 @end example
18967 @noindent
18968 @xref{Running the Compiler}.
18970 This macro double quotes both @var{includes} and @var{function-body}.
18972 For C and C++, @var{includes} is any @code{#include} statements needed
18973 by the code in @var{function-body} (@var{includes} is ignored if
18974 the currently selected language is Fortran or Fortran 77).  The compiler
18975 and compilation flags are determined by the current language
18976 (@pxref{Language Choice}).
18977 @end defmac
18979 @defmac AC_TRY_CPP (@var{input}, @ovar{action-if-true}, @ovar{action-if-false})
18980 @acindex{TRY_CPP}
18981 Same as:
18983 @example
18984 AC_PREPROC_IFELSE(
18985   [AC_LANG_SOURCE([[@var{input}]])],
18986   [@var{action-if-true}],
18987   [@var{action-if-false}])
18988 @end example
18990 @noindent
18991 @xref{Running the Preprocessor}.
18993 This macro double quotes the @var{input}.
18994 @end defmac
18996 @defmac AC_TRY_LINK (@var{includes}, @var{function-body}, @
18997   @ovar{action-if-true}, @ovar{action-if-false})
18998 @acindex{TRY_LINK}
18999 Same as:
19001 @example
19002 AC_LINK_IFELSE(
19003   [AC_LANG_PROGRAM([[@var{includes}]],
19004      [[@var{function-body}]])],
19005   [@var{action-if-true}],
19006   [@var{action-if-false}])
19007 @end example
19009 @noindent
19010 @xref{Running the Compiler}.
19012 This macro double quotes both @var{includes} and @var{function-body}.
19014 Depending on the current language (@pxref{Language Choice}), create a
19015 test program to see whether a function whose body consists of
19016 @var{function-body} can be compiled and linked.  If the file compiles
19017 and links successfully, run shell commands @var{action-if-found},
19018 otherwise run @var{action-if-not-found}.
19020 This macro double quotes both @var{includes} and @var{function-body}.
19022 For C and C++, @var{includes} is any @code{#include} statements needed
19023 by the code in @var{function-body} (@var{includes} is ignored if
19024 the currently selected language is Fortran or Fortran 77).  The compiler
19025 and compilation flags are determined by the current language
19026 (@pxref{Language Choice}), and in addition @code{LDFLAGS} and
19027 @code{LIBS} are used for linking.
19028 @end defmac
19030 @defmac AC_TRY_LINK_FUNC (@var{function}, @ovar{action-if-found}, @
19031   @ovar{action-if-not-found})
19032 @acindex{TRY_LINK_FUNC}
19033 This macro is equivalent to
19034 @example
19035 AC_LINK_IFELSE([AC_LANG_CALL([], [@var{function}])],
19036   [@var{action-if-found}], [@var{action-if-not-found}])
19037 @end example
19038 @noindent
19039 @xref{AC_LINK_IFELSE}.
19040 @end defmac
19042 @defmac AC_TRY_RUN (@var{program}, @ovar{action-if-true}, @
19043   @ovar{action-if-false}, @ovar{action-if-cross-compiling})
19044 @acindex{TRY_RUN}
19045 Same as:
19047 @example
19048 AC_RUN_IFELSE(
19049   [AC_LANG_SOURCE([[@var{program}]])],
19050   [@var{action-if-true}],
19051   [@var{action-if-false}],
19052   [@var{action-if-cross-compiling}])
19053 @end example
19055 @noindent
19056 @xref{Runtime}.
19057 @end defmac
19059 @defmac AC_UID_T
19060 @acindex{UID_T}
19061 Replaced by @code{AC_TYPE_UID_T} (@pxref{AC_TYPE_UID_T}).
19062 @end defmac
19064 @defmac AC_UNISTD_H
19065 @acindex{UNISTD_H}
19066 Same as @samp{AC_CHECK_HEADERS([unistd.h])} (@pxref{AC_CHECK_HEADERS}).
19067 @end defmac
19069 @defmac AC_USG
19070 @acindex{USG}
19071 @cvindex USG
19072 Define @code{USG} if the @acronym{BSD} string functions are defined in
19073 @file{strings.h}.  You should no longer depend upon @code{USG}, but on
19074 @code{HAVE_STRING_H}; see @ref{Standard Symbols}.
19075 @end defmac
19077 @defmac AC_UTIME_NULL
19078 @acindex{UTIME_NULL}
19079 Replaced by @code{AC_FUNC_UTIME_NULL} (@pxref{AC_FUNC_UTIME_NULL}).
19080 @end defmac
19082 @defmac AC_VALIDATE_CACHED_SYSTEM_TUPLE (@ovar{cmd})
19083 @acindex{VALIDATE_CACHED_SYSTEM_TUPLE}
19084 If the cache file is inconsistent with the current host, target and
19085 build system types, it used to execute @var{cmd} or print a default
19086 error message.  This is now handled by default.
19087 @end defmac
19089 @defmac AC_VERBOSE (@var{result-description})
19090 @acindex{VERBOSE}
19091 Replaced by @code{AC_MSG_RESULT} (@pxref{AC_MSG_RESULT}).
19092 @end defmac
19094 @defmac AC_VFORK
19095 @acindex{VFORK}
19096 Replaced by @code{AC_FUNC_FORK} (@pxref{AC_FUNC_FORK}).
19097 @end defmac
19099 @defmac AC_VPRINTF
19100 @acindex{VPRINTF}
19101 Replaced by @code{AC_FUNC_VPRINTF} (@pxref{AC_FUNC_VPRINTF}).
19102 @end defmac
19104 @defmac AC_WAIT3
19105 @acindex{WAIT3}
19106 This macro was renamed @code{AC_FUNC_WAIT3}.  However, these days
19107 portable programs should use @code{waitpid}, not @code{wait3}, as
19108 @code{wait3} has been removed from Posix.
19109 @end defmac
19111 @defmac AC_WARN
19112 @acindex{WARN}
19113 Replaced by @code{AC_MSG_WARN} (@pxref{AC_MSG_WARN}).
19114 @end defmac
19116 @defmac AC_WITH (@var{package}, @var{action-if-given}, @
19117   @ovar{action-if-not-given})
19118 @acindex{WITH}
19119 This is an obsolete version of @code{AC_ARG_WITH} that does not
19120 support providing a help string (@pxref{AC_ARG_WITH}).
19121 @end defmac
19123 @defmac AC_WORDS_BIGENDIAN
19124 @acindex{WORDS_BIGENDIAN}
19125 Replaced by @code{AC_C_BIGENDIAN} (@pxref{AC_C_BIGENDIAN}).
19126 @end defmac
19128 @defmac AC_XENIX_DIR
19129 @acindex{XENIX_DIR}
19130 @ovindex LIBS
19131 This macro used to add @option{-lx} to output variable @code{LIBS} if on
19132 Xenix.  Also, if @file{dirent.h} is being checked for, added
19133 @option{-ldir} to @code{LIBS}.  Now it is merely an alias of
19134 @code{AC_HEADER_DIRENT} instead, plus some code to detect whether
19135 running @sc{xenix} on which you should not depend:
19137 @example
19138 AC_MSG_CHECKING([for Xenix])
19139 AC_EGREP_CPP([yes],
19140 [#if defined M_XENIX && !defined M_UNIX
19141   yes
19142 #endif],
19143              [AC_MSG_RESULT([yes]); XENIX=yes],
19144              [AC_MSG_RESULT([no]); XENIX=])
19145 @end example
19146 @noindent
19147 Don't use this macro, the dignified means to check the nature of the
19148 host is using @code{AC_CANONICAL_HOST} (@pxref{Canonicalizing}).
19149 @end defmac
19151 @defmac AC_YYTEXT_POINTER
19152 @acindex{YYTEXT_POINTER}
19153 This macro was renamed @code{AC_DECL_YYTEXT}, which in turn was
19154 integrated into @code{AC_PROG_LEX} (@pxref{AC_PROG_LEX}).
19155 @end defmac
19157 @node Autoconf 1
19158 @section Upgrading From Version 1
19159 @cindex Upgrading autoconf
19160 @cindex Autoconf upgrading
19162 Autoconf version 2 is mostly backward compatible with version 1.
19163 However, it introduces better ways to do some things, and doesn't
19164 support some of the ugly things in version 1.  So, depending on how
19165 sophisticated your @file{configure.ac} files are, you might have to do
19166 some manual work in order to upgrade to version 2.  This chapter points
19167 out some problems to watch for when upgrading.  Also, perhaps your
19168 @command{configure} scripts could benefit from some of the new features in
19169 version 2; the changes are summarized in the file @file{NEWS} in the
19170 Autoconf distribution.
19172 @menu
19173 * Changed File Names::          Files you might rename
19174 * Changed Makefiles::           New things to put in @file{Makefile.in}
19175 * Changed Macros::              Macro calls you might replace
19176 * Changed Results::             Changes in how to check test results
19177 * Changed Macro Writing::       Better ways to write your own macros
19178 @end menu
19180 @node Changed File Names
19181 @subsection Changed File Names
19183 If you have an @file{aclocal.m4} installed with Autoconf (as opposed to
19184 in a particular package's source directory), you must rename it to
19185 @file{acsite.m4}.  @xref{autoconf Invocation}.
19187 If you distribute @file{install.sh} with your package, rename it to
19188 @file{install-sh} so @code{make} builtin rules don't inadvertently
19189 create a file called @file{install} from it.  @code{AC_PROG_INSTALL}
19190 looks for the script under both names, but it is best to use the new name.
19192 If you were using @file{config.h.top}, @file{config.h.bot}, or
19193 @file{acconfig.h}, you still can, but you have less clutter if you
19194 use the @code{AH_} macros.  @xref{Autoheader Macros}.
19196 @node Changed Makefiles
19197 @subsection Changed Makefiles
19199 Add @samp{@@CFLAGS@@}, @samp{@@CPPFLAGS@@}, and @samp{@@LDFLAGS@@} in
19200 your @file{Makefile.in} files, so they can take advantage of the values
19201 of those variables in the environment when @command{configure} is run.
19202 Doing this isn't necessary, but it's a convenience for users.
19204 Also add @samp{@@configure_input@@} in a comment to each input file for
19205 @code{AC_OUTPUT}, so that the output files contain a comment saying
19206 they were produced by @command{configure}.  Automatically selecting the
19207 right comment syntax for all the kinds of files that people call
19208 @code{AC_OUTPUT} on became too much work.
19210 Add @file{config.log} and @file{config.cache} to the list of files you
19211 remove in @code{distclean} targets.
19213 If you have the following in @file{Makefile.in}:
19215 @example
19216 prefix = /usr/local
19217 exec_prefix = $(prefix)
19218 @end example
19220 @noindent
19221 you must change it to:
19223 @example
19224 prefix = @@prefix@@
19225 exec_prefix = @@exec_prefix@@
19226 @end example
19228 @noindent
19229 The old behavior of replacing those variables without @samp{@@}
19230 characters around them has been removed.
19232 @node Changed Macros
19233 @subsection Changed Macros
19235 Many of the macros were renamed in Autoconf version 2.  You can still
19236 use the old names, but the new ones are clearer, and it's easier to find
19237 the documentation for them.  @xref{Obsolete Macros}, for a table showing the
19238 new names for the old macros.  Use the @command{autoupdate} program to
19239 convert your @file{configure.ac} to using the new macro names.
19240 @xref{autoupdate Invocation}.
19242 Some macros have been superseded by similar ones that do the job better,
19243 but are not call-compatible.  If you get warnings about calling obsolete
19244 macros while running @command{autoconf}, you may safely ignore them, but
19245 your @command{configure} script generally works better if you follow
19246 the advice that is printed about what to replace the obsolete macros with.  In
19247 particular, the mechanism for reporting the results of tests has
19248 changed.  If you were using @command{echo} or @code{AC_VERBOSE} (perhaps
19249 via @code{AC_COMPILE_CHECK}), your @command{configure} script's output
19250 looks better if you switch to @code{AC_MSG_CHECKING} and
19251 @code{AC_MSG_RESULT}.  @xref{Printing Messages}.  Those macros work best
19252 in conjunction with cache variables.  @xref{Caching Results}.
19256 @node Changed Results
19257 @subsection Changed Results
19259 If you were checking the results of previous tests by examining the
19260 shell variable @code{DEFS}, you need to switch to checking the values of
19261 the cache variables for those tests.  @code{DEFS} no longer exists while
19262 @command{configure} is running; it is only created when generating output
19263 files.  This difference from version 1 is because properly quoting the
19264 contents of that variable turned out to be too cumbersome and
19265 inefficient to do every time @code{AC_DEFINE} is called.  @xref{Cache
19266 Variable Names}.
19268 For example, here is a @file{configure.ac} fragment written for Autoconf
19269 version 1:
19271 @example
19272 AC_HAVE_FUNCS(syslog)
19273 case "$DEFS" in
19274 *-DHAVE_SYSLOG*) ;;
19275 *) # syslog is not in the default libraries.  See if it's in some other.
19276   saved_LIBS="$LIBS"
19277   for lib in bsd socket inet; do
19278     AC_CHECKING(for syslog in -l$lib)
19279     LIBS="-l$lib $saved_LIBS"
19280     AC_HAVE_FUNCS(syslog)
19281     case "$DEFS" in
19282     *-DHAVE_SYSLOG*) break ;;
19283     *) ;;
19284     esac
19285     LIBS="$saved_LIBS"
19286   done ;;
19287 esac
19288 @end example
19290 Here is a way to write it for version 2:
19292 @example
19293 AC_CHECK_FUNCS([syslog])
19294 if test $ac_cv_func_syslog = no; then
19295   # syslog is not in the default libraries.  See if it's in some other.
19296   for lib in bsd socket inet; do
19297     AC_CHECK_LIB([$lib], [syslog], [AC_DEFINE([HAVE_SYSLOG])
19298       LIBS="-l$lib $LIBS"; break])
19299   done
19301 @end example
19303 If you were working around bugs in @code{AC_DEFINE_UNQUOTED} by adding
19304 backslashes before quotes, you need to remove them.  It now works
19305 predictably, and does not treat quotes (except back quotes) specially.
19306 @xref{Setting Output Variables}.
19308 All of the Boolean shell variables set by Autoconf macros now use
19309 @samp{yes} for the true value.  Most of them use @samp{no} for false,
19310 though for backward compatibility some use the empty string instead.  If
19311 you were relying on a shell variable being set to something like 1 or
19312 @samp{t} for true, you need to change your tests.
19314 @node Changed Macro Writing
19315 @subsection Changed Macro Writing
19317 When defining your own macros, you should now use @code{AC_DEFUN}
19318 instead of @code{define}.  @code{AC_DEFUN} automatically calls
19319 @code{AC_PROVIDE} and ensures that macros called via @code{AC_REQUIRE}
19320 do not interrupt other macros, to prevent nested @samp{checking@dots{}}
19321 messages on the screen.  There's no actual harm in continuing to use the
19322 older way, but it's less convenient and attractive.  @xref{Macro
19323 Definitions}.
19325 You probably looked at the macros that came with Autoconf as a guide for
19326 how to do things.  It would be a good idea to take a look at the new
19327 versions of them, as the style is somewhat improved and they take
19328 advantage of some new features.
19330 If you were doing tricky things with undocumented Autoconf internals
19331 (macros, variables, diversions), check whether you need to change
19332 anything to account for changes that have been made.  Perhaps you can
19333 even use an officially supported technique in version 2 instead of
19334 kludging.  Or perhaps not.
19336 To speed up your locally written feature tests, add caching to them.
19337 See whether any of your tests are of general enough usefulness to
19338 encapsulate them into macros that you can share.
19341 @node Autoconf 2.13
19342 @section Upgrading From Version 2.13
19343 @cindex Upgrading autoconf
19344 @cindex Autoconf upgrading
19346 The introduction of the previous section (@pxref{Autoconf 1}) perfectly
19347 suits this section@enddots{}
19349 @quotation
19350 Autoconf version 2.50 is mostly backward compatible with version 2.13.
19351 However, it introduces better ways to do some things, and doesn't
19352 support some of the ugly things in version 2.13.  So, depending on how
19353 sophisticated your @file{configure.ac} files are, you might have to do
19354 some manual work in order to upgrade to version 2.50.  This chapter
19355 points out some problems to watch for when upgrading.  Also, perhaps
19356 your @command{configure} scripts could benefit from some of the new
19357 features in version 2.50; the changes are summarized in the file
19358 @file{NEWS} in the Autoconf distribution.
19359 @end quotation
19361 @menu
19362 * Changed Quotation::           Broken code which used to work
19363 * New Macros::                  Interaction with foreign macros
19364 * Hosts and Cross-Compilation::  Bugward compatibility kludges
19365 * AC_LIBOBJ vs LIBOBJS::        LIBOBJS is a forbidden token
19366 * AC_FOO_IFELSE vs AC_TRY_FOO::  A more generic scheme for testing sources
19367 @end menu
19369 @node Changed Quotation
19370 @subsection Changed Quotation
19372 The most important changes are invisible to you: the implementation of
19373 most macros have completely changed.  This allowed more factorization of
19374 the code, better error messages, a higher uniformity of the user's
19375 interface etc.  Unfortunately, as a side effect, some construct which
19376 used to (miraculously) work might break starting with Autoconf 2.50.
19377 The most common culprit is bad quotation.
19379 For instance, in the following example, the message is not properly
19380 quoted:
19382 @example
19383 AC_INIT
19384 AC_CHECK_HEADERS(foo.h, ,
19385   AC_MSG_ERROR(cannot find foo.h, bailing out))
19386 AC_OUTPUT
19387 @end example
19389 @noindent
19390 Autoconf 2.13 simply ignores it:
19392 @example
19393 $ @kbd{autoconf-2.13; ./configure --silent}
19394 creating cache ./config.cache
19395 configure: error: cannot find foo.h
19397 @end example
19399 @noindent
19400 while Autoconf 2.50 produces a broken @file{configure}:
19402 @example
19403 $ @kbd{autoconf-2.50; ./configure --silent}
19404 configure: error: cannot find foo.h
19405 ./configure: exit: bad non-numeric arg `bailing'
19406 ./configure: exit: bad non-numeric arg `bailing'
19408 @end example
19410 The message needs to be quoted, and the @code{AC_MSG_ERROR} invocation
19411 too!
19413 @example
19414 AC_INIT([Example], [1.0], [bug-example@@example.org])
19415 AC_CHECK_HEADERS([foo.h], [],
19416   [AC_MSG_ERROR([cannot find foo.h, bailing out])])
19417 AC_OUTPUT
19418 @end example
19420 Many many (and many more) Autoconf macros were lacking proper quotation,
19421 including no less than@dots{} @code{AC_DEFUN} itself!
19423 @example
19424 $ @kbd{cat configure.in}
19425 AC_DEFUN([AC_PROG_INSTALL],
19426 [# My own much better version
19428 AC_INIT
19429 AC_PROG_INSTALL
19430 AC_OUTPUT
19431 $ @kbd{autoconf-2.13}
19432 autoconf: Undefined macros:
19433 ***BUG in Autoconf--please report*** AC_FD_MSG
19434 ***BUG in Autoconf--please report*** AC_EPI
19435 configure.in:1:AC_DEFUN([AC_PROG_INSTALL],
19436 configure.in:5:AC_PROG_INSTALL
19437 $ @kbd{autoconf-2.50}
19439 @end example
19442 @node New Macros
19443 @subsection New Macros
19445 @cindex undefined macro
19446 @cindex @code{_m4_divert_diversion}
19448 While Autoconf was relatively dormant in the late 1990s, Automake
19449 provided Autoconf-like macros for a while.  Starting with Autoconf 2.50
19450 in 2001, Autoconf provided
19451 versions of these macros, integrated in the @code{AC_} namespace,
19452 instead of @code{AM_}.  But in order to ease the upgrading via
19453 @command{autoupdate}, bindings to such @code{AM_} macros are provided.
19455 Unfortunately older versions of Automake (e.g., Automake 1.4)
19456 did not quote the names of these macros.
19457 Therefore, when @command{m4} finds something like
19458 @samp{AC_DEFUN(AM_TYPE_PTRDIFF_T, @dots{})} in @file{aclocal.m4},
19459 @code{AM_TYPE_PTRDIFF_T} is
19460 expanded, replaced with its Autoconf definition.
19462 Fortunately Autoconf catches pre-@code{AC_INIT} expansions, and
19463 complains, in its own words:
19465 @example
19466 $ @kbd{cat configure.ac}
19467 AC_INIT([Example], [1.0], [bug-example@@example.org])
19468 AM_TYPE_PTRDIFF_T
19469 $ @kbd{aclocal-1.4}
19470 $ @kbd{autoconf}
19471 aclocal.m4:17: error: m4_defn: undefined macro: _m4_divert_diversion
19472 aclocal.m4:17: the top level
19473 autom4te: m4 failed with exit status: 1
19475 @end example
19477 Modern versions of Automake no longer define most of these
19478 macros, and properly quote the names of the remaining macros.
19479 If you must use an old Automake, do not depend upon macros from Automake
19480 as it is simply not its job
19481 to provide macros (but the one it requires itself):
19483 @example
19484 $ @kbd{cat configure.ac}
19485 AC_INIT([Example], [1.0], [bug-example@@example.org])
19486 AM_TYPE_PTRDIFF_T
19487 $ @kbd{rm aclocal.m4}
19488 $ @kbd{autoupdate}
19489 autoupdate: `configure.ac' is updated
19490 $ @kbd{cat configure.ac}
19491 AC_INIT([Example], [1.0], [bug-example@@example.org])
19492 AC_CHECK_TYPES([ptrdiff_t])
19493 $ @kbd{aclocal-1.4}
19494 $ @kbd{autoconf}
19496 @end example
19499 @node Hosts and Cross-Compilation
19500 @subsection Hosts and Cross-Compilation
19501 @cindex Cross compilation
19503 Based on the experience of compiler writers, and after long public
19504 debates, many aspects of the cross-compilation chain have changed:
19506 @itemize @minus
19507 @item
19508 the relationship between the build, host, and target architecture types,
19510 @item
19511 the command line interface for specifying them to @command{configure},
19513 @item
19514 the variables defined in @command{configure},
19516 @item
19517 the enabling of cross-compilation mode.
19518 @end itemize
19520 @sp 1
19522 The relationship between build, host, and target have been cleaned up:
19523 the chain of default is now simply: target defaults to host, host to
19524 build, and build to the result of @command{config.guess}.  Nevertheless,
19525 in order to ease the transition from 2.13 to 2.50, the following
19526 transition scheme is implemented.  @emph{Do not rely on it}, as it will
19527 be completely disabled in a couple of releases (we cannot keep it, as it
19528 proves to cause more problems than it cures).
19530 They all default to the result of running @command{config.guess}, unless
19531 you specify either @option{--build} or @option{--host}.  In this case,
19532 the default becomes the system type you specified.  If you specify both,
19533 and they're different, @command{configure} enters cross compilation
19534 mode, so it doesn't run any tests that require execution.
19536 Hint: if you mean to override the result of @command{config.guess},
19537 prefer @option{--build} over @option{--host}.  In the future,
19538 @option{--host} will not override the name of the build system type.
19539 Whenever you specify @option{--host}, be sure to specify @option{--build}
19540 too.
19542 @sp 1
19544 For backward compatibility, @command{configure} accepts a system
19545 type as an option by itself.  Such an option overrides the
19546 defaults for build, host, and target system types.  The following
19547 configure statement configures a cross toolchain that runs on
19548 Net@acronym{BSD}/alpha but generates code for @acronym{GNU} Hurd/sparc,
19549 which is also the build platform.
19551 @example
19552 ./configure --host=alpha-netbsd sparc-gnu
19553 @end example
19555 @sp 1
19557 In Autoconf 2.13 and before, the variables @code{build}, @code{host},
19558 and @code{target} had a different semantics before and after the
19559 invocation of @code{AC_CANONICAL_BUILD} etc.  Now, the argument of
19560 @option{--build} is strictly copied into @code{build_alias}, and is left
19561 empty otherwise.  After the @code{AC_CANONICAL_BUILD}, @code{build} is
19562 set to the canonicalized build type.  To ease the transition, before,
19563 its contents is the same as that of @code{build_alias}.  Do @emph{not}
19564 rely on this broken feature.
19566 For consistency with the backward compatibility scheme exposed above,
19567 when @option{--host} is specified but @option{--build} isn't, the build
19568 system is assumed to be the same as @option{--host}, and
19569 @samp{build_alias} is set to that value.  Eventually, this
19570 historically incorrect behavior will go away.
19572 @sp 1
19574 The former scheme to enable cross-compilation proved to cause more harm
19575 than good, in particular, it used to be triggered too easily, leaving
19576 regular end users puzzled in front of cryptic error messages.
19577 @command{configure} could even enter cross-compilation mode only
19578 because the compiler was not functional.  This is mainly because
19579 @command{configure} used to try to detect cross-compilation, instead of
19580 waiting for an explicit flag from the user.
19582 Now, @command{configure} enters cross-compilation mode if and only if
19583 @option{--host} is passed.
19585 That's the short documentation.  To ease the transition between 2.13 and
19586 its successors, a more complicated scheme is implemented.  @emph{Do not
19587 rely on the following}, as it will be removed in the near future.
19589 If you specify @option{--host}, but not @option{--build}, when
19590 @command{configure} performs the first compiler test it tries to run
19591 an executable produced by the compiler.  If the execution fails, it
19592 enters cross-compilation mode.  This is fragile.  Moreover, by the time
19593 the compiler test is performed, it may be too late to modify the
19594 build-system type: other tests may have already been performed.
19595 Therefore, whenever you specify @option{--host}, be sure to specify
19596 @option{--build} too.
19598 @example
19599 ./configure --build=i686-pc-linux-gnu --host=m68k-coff
19600 @end example
19602 @noindent
19603 enters cross-compilation mode.  The former interface, which
19604 consisted in setting the compiler to a cross-compiler without informing
19605 @command{configure} is obsolete.  For instance, @command{configure}
19606 fails if it can't run the code generated by the specified compiler if you
19607 configure as follows:
19609 @example
19610 ./configure CC=m68k-coff-gcc
19611 @end example
19614 @node AC_LIBOBJ vs LIBOBJS
19615 @subsection @code{AC_LIBOBJ} vs.@: @code{LIBOBJS}
19617 Up to Autoconf 2.13, the replacement of functions was triggered via the
19618 variable @code{LIBOBJS}.  Since Autoconf 2.50, the macro
19619 @code{AC_LIBOBJ} should be used instead (@pxref{Generic Functions}).
19620 Starting at Autoconf 2.53, the use of @code{LIBOBJS} is an error.
19622 This change is mandated by the unification of the @acronym{GNU} Build System
19623 components.  In particular, the various fragile techniques used to parse
19624 a @file{configure.ac} are all replaced with the use of traces.  As a
19625 consequence, any action must be traceable, which obsoletes critical
19626 variable assignments.  Fortunately, @code{LIBOBJS} was the only problem,
19627 and it can even be handled gracefully (read, ``without your having to
19628 change something'').
19630 There were two typical uses of @code{LIBOBJS}: asking for a replacement
19631 function, and adjusting @code{LIBOBJS} for Automake and/or Libtool.
19633 @sp 1
19635 As for function replacement, the fix is immediate: use
19636 @code{AC_LIBOBJ}.  For instance:
19638 @example
19639 LIBOBJS="$LIBOBJS fnmatch.o"
19640 LIBOBJS="$LIBOBJS malloc.$ac_objext"
19641 @end example
19643 @noindent
19644 should be replaced with:
19646 @example
19647 AC_LIBOBJ([fnmatch])
19648 AC_LIBOBJ([malloc])
19649 @end example
19651 @sp 1
19653 @ovindex LIBOBJDIR
19654 When used with Automake 1.10 or newer, a suitable value for
19655 @code{LIBOBJDIR} is set so that the @code{LIBOBJS} and @code{LTLIBOBJS}
19656 can be referenced from any @file{Makefile.am}.  Even without Automake,
19657 arranging for @code{LIBOBJDIR} to be set correctly enables
19658 referencing @code{LIBOBJS} and @code{LTLIBOBJS} in another directory.
19659 The @code{LIBOBJDIR} feature is experimental.
19662 @node AC_FOO_IFELSE vs AC_TRY_FOO
19663 @subsection @code{AC_FOO_IFELSE} vs.@: @code{AC_TRY_FOO}
19665 Since Autoconf 2.50, internal codes uses @code{AC_PREPROC_IFELSE},
19666 @code{AC_COMPILE_IFELSE}, @code{AC_LINK_IFELSE}, and
19667 @code{AC_RUN_IFELSE} on one hand and @code{AC_LANG_SOURCES},
19668 and @code{AC_LANG_PROGRAM} on the other hand instead of the deprecated
19669 @code{AC_TRY_CPP}, @code{AC_TRY_COMPILE}, @code{AC_TRY_LINK}, and
19670 @code{AC_TRY_RUN}.  The motivations where:
19671 @itemize @minus
19672 @item
19673 a more consistent interface: @code{AC_TRY_COMPILE} etc.@: were double
19674 quoting their arguments;
19676 @item
19677 the combinatoric explosion is solved by decomposing on the one hand the
19678 generation of sources, and on the other hand executing the program;
19680 @item
19681 this scheme helps supporting more languages than plain C and C++.
19682 @end itemize
19684 In addition to the change of syntax, the philosophy has changed too:
19685 while emphasis was put on speed at the expense of accuracy, today's
19686 Autoconf promotes accuracy of the testing framework at, ahem@dots{}, the
19687 expense of speed.
19690 As a perfect example of what is @emph{not} to be done, here is how to
19691 find out whether a header file contains a particular declaration, such
19692 as a typedef, a structure, a structure member, or a function.  Use
19693 @code{AC_EGREP_HEADER} instead of running @code{grep} directly on the
19694 header file; on some systems the symbol might be defined in another
19695 header file that the file you are checking includes.
19697 As a (bad) example, here is how you should not check for C preprocessor
19698 symbols, either defined by header files or predefined by the C
19699 preprocessor: using @code{AC_EGREP_CPP}:
19701 @example
19702 @group
19703 AC_EGREP_CPP(yes,
19704 [#ifdef _AIX
19705   yes
19706 #endif
19707 ], is_aix=yes, is_aix=no)
19708 @end group
19709 @end example
19711 The above example, properly written would (i) use
19712 @code{AC_LANG_PROGRAM}, and (ii) run the compiler:
19714 @example
19715 @group
19716 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
19717 [[#ifndef _AIX
19718  error: This isn't AIX!
19719 #endif
19720 ]])],
19721                    [is_aix=yes],
19722                    [is_aix=no])
19723 @end group
19724 @end example
19727 @c ============================= Generating Test Suites with Autotest
19729 @node Using Autotest
19730 @chapter Generating Test Suites with Autotest
19732 @cindex Autotest
19734 @display
19735 @strong{N.B.: This section describes an experimental feature which will
19736 be part of Autoconf in a forthcoming release.  Although we believe
19737 Autotest is stabilizing, this documentation describes an interface which
19738 might change in the future: do not depend upon Autotest without
19739 subscribing to the Autoconf mailing lists.}
19740 @end display
19742 It is paradoxical that portable projects depend on nonportable tools
19743 to run their test suite.  Autoconf by itself is the paragon of this
19744 problem: although it aims at perfectly portability, up to 2.13 its
19745 test suite was using Deja@acronym{GNU}, a rich and complex testing
19746 framework, but which is far from being standard on Posix systems.
19747 Worse yet, it was likely to be missing on the most fragile platforms,
19748 the very platforms that are most likely to torture Autoconf and
19749 exhibit deficiencies.
19751 To circumvent this problem, many package maintainers have developed their
19752 own testing framework, based on simple shell scripts whose sole outputs
19753 are exit status values describing whether the test succeeded.  Most of
19754 these tests share common patterns, and this can result in lots of
19755 duplicated code and tedious maintenance.
19757 Following exactly the same reasoning that yielded to the inception of
19758 Autoconf, Autotest provides a test suite generation framework, based on
19759 M4 macros building a portable shell script.  The suite itself is
19760 equipped with automatic logging and tracing facilities which greatly
19761 diminish the interaction with bug reporters, and simple timing reports.
19763 Autoconf itself has been using Autotest for years, and we do attest that
19764 it has considerably improved the strength of the test suite and the
19765 quality of bug reports.  Other projects are known to use some generation
19766 of Autotest, such as Bison, Free Recode, Free Wdiff, @acronym{GNU} Tar, each of
19767 them with different needs, and this usage has validated Autotest as a general
19768 testing framework.
19770 Nonetheless, compared to Deja@acronym{GNU}, Autotest is inadequate for
19771 interactive tool testing, which is probably its main limitation.
19773 @menu
19774 * Using an Autotest Test Suite::  Autotest and the user
19775 * Writing Testsuites::          Autotest macros
19776 * testsuite Invocation::        Running @command{testsuite} scripts
19777 * Making testsuite Scripts::    Using autom4te to create @command{testsuite}
19778 @end menu
19780 @node Using an Autotest Test Suite
19781 @section Using an Autotest Test Suite
19783 @menu
19784 * testsuite Scripts::           The concepts of Autotest
19785 * Autotest Logs::               Their contents
19786 @end menu
19788 @node testsuite Scripts
19789 @subsection @command{testsuite} Scripts
19791 @cindex @command{testsuite}
19793 Generating testing or validation suites using Autotest is rather easy.
19794 The whole validation suite is held in a file to be processed through
19795 @command{autom4te}, itself using @acronym{GNU} M4 under the scene, to
19796 produce a stand-alone Bourne shell script which then gets distributed.
19797 Neither @command{autom4te} nor @acronym{GNU} M4 are needed at
19798 the installer's end.
19800 @cindex test group
19801 Each test of the validation suite should be part of some test group.  A
19802 @dfn{test group} is a sequence of interwoven tests that ought to be
19803 executed together, usually because one test in the group creates data
19804 files than a later test in the same group needs to read.  Complex test
19805 groups make later debugging more tedious.  It is much better to
19806 keep only a few tests per test group.  Ideally there is only one test
19807 per test group.
19809 For all but the simplest packages, some file such as @file{testsuite.at}
19810 does not fully hold all test sources, as these are often easier to
19811 maintain in separate files.  Each of these separate files holds a single
19812 test group, or a sequence of test groups all addressing some common
19813 functionality in the package.  In such cases, @file{testsuite.at}
19814 merely initializes the validation suite, and sometimes does elementary
19815 health checking, before listing include statements for all other test
19816 files.  The special file @file{package.m4}, containing the
19817 identification of the package, is automatically included if found.
19819 A convenient alternative consists in moving all the global issues
19820 (local Autotest macros, elementary health checking, and @code{AT_INIT}
19821 invocation) into the file @code{local.at}, and making
19822 @file{testsuite.at} be a simple list of @code{m4_include} of sub test
19823 suites.  In such case, generating the whole test suite or pieces of it
19824 is only a matter of choosing the @command{autom4te} command line
19825 arguments.
19827 The validation scripts that Autotest produces are by convention called
19828 @command{testsuite}.  When run, @command{testsuite} executes each test
19829 group in turn, producing only one summary line per test to say if that
19830 particular test succeeded or failed.  At end of all tests, summarizing
19831 counters get printed.  One debugging directory is left for each test
19832 group which failed, if any: such directories are named
19833 @file{testsuite.dir/@var{nn}}, where @var{nn} is the sequence number of
19834 the test group, and they include:
19836 @itemize @bullet
19837 @item a debugging script named @file{run} which reruns the test in
19838 @dfn{debug mode} (@pxref{testsuite Invocation}).  The automatic generation
19839 of debugging scripts has the purpose of easing the chase for bugs.
19841 @item all the files created with @code{AT_DATA}
19843 @item a log of the run, named @file{testsuite.log}
19844 @end itemize
19846 In the ideal situation, none of the tests fail, and consequently no
19847 debugging directory is left behind for validation.
19849 It often happens in practice that individual tests in the validation
19850 suite need to get information coming out of the configuration process.
19851 Some of this information, common for all validation suites, is provided
19852 through the file @file{atconfig}, automatically created by
19853 @code{AC_CONFIG_TESTDIR}.  For configuration informations which your
19854 testing environment specifically needs, you might prepare an optional
19855 file named @file{atlocal.in}, instantiated by @code{AC_CONFIG_FILES}.
19856 The configuration process produces @file{atconfig} and @file{atlocal}
19857 out of these two input files, and these two produced files are
19858 automatically read by the @file{testsuite} script.
19860 Here is a diagram showing the relationship between files.
19862 @noindent
19863 Files used in preparing a software package for distribution:
19865 @example
19866                 [package.m4] -->.
19867                                  \
19868 subfile-1.at ->.  [local.at] ---->+
19869     ...         \                  \
19870 subfile-i.at ---->-- testsuite.at -->-- autom4te* -->testsuite
19871     ...         /
19872 subfile-n.at ->'
19873 @end example
19875 @noindent
19876 Files used in configuring a software package:
19878 @example
19879                                      .--> atconfig
19880                                     /
19881 [atlocal.in] -->  config.status* --<
19882                                     \
19883                                      `--> [atlocal]
19884 @end example
19886 @noindent
19887 Files created during the test suite execution:
19889 @example
19890 atconfig -->.                    .--> testsuite.log
19891              \                  /
19892               >-- testsuite* --<
19893              /                  \
19894 [atlocal] ->'                    `--> [testsuite.dir]
19895 @end example
19898 @node Autotest Logs
19899 @subsection Autotest Logs
19901 When run, the test suite creates a log file named after itself, e.g., a
19902 test suite named @command{testsuite} creates @file{testsuite.log}.  It
19903 contains a lot of information, usually more than maintainers actually
19904 need, but therefore most of the time it contains all that is needed:
19906 @table @asis
19907 @item command line arguments
19908 @c akim s/to consist in/to consist of/
19909 A bad but unfortunately widespread habit consists of
19910 setting environment variables before the command, such as in
19911 @samp{CC=my-home-grown-cc ./testsuite}.  The test suite does not
19912 know this change, hence (i) it cannot report it to you, and (ii)
19913 it cannot preserve the value of @code{CC} for subsequent runs.
19914 Autoconf faced exactly the same problem, and solved it by asking
19915 users to pass the variable definitions as command line arguments.
19916 Autotest requires this rule, too, but has no means to enforce it; the log
19917 then contains a trace of the variables that were changed by the user.
19919 @item @file{ChangeLog} excerpts
19920 The topmost lines of all the @file{ChangeLog} files found in the source
19921 hierarchy.  This is especially useful when bugs are reported against
19922 development versions of the package, since the version string does not
19923 provide sufficient information to know the exact state of the sources
19924 the user compiled.  Of course, this relies on the use of a
19925 @file{ChangeLog}.
19927 @item build machine
19928 Running a test suite in a cross-compile environment is not an easy task,
19929 since it would mean having the test suite run on a machine @var{build},
19930 while running programs on a machine @var{host}.  It is much simpler to
19931 run both the test suite and the programs on @var{host}, but then, from
19932 the point of view of the test suite, there remains a single environment,
19933 @var{host} = @var{build}.  The log contains relevant information on the
19934 state of the build machine, including some important environment
19935 variables.
19936 @c FIXME: How about having an M4sh macro to say `hey, log the value
19937 @c of `@dots{}'?  This would help both Autoconf and Autotest.
19939 @item tested programs
19940 The absolute file name and answers to @option{--version} of the tested
19941 programs (see @ref{Writing Testsuites}, @code{AT_TESTED}).
19943 @item configuration log
19944 The contents of @file{config.log}, as created by @command{configure},
19945 are appended.  It contains the configuration flags and a detailed report
19946 on the configuration itself.
19947 @end table
19950 @node Writing Testsuites
19951 @section Writing @file{testsuite.at}
19953 The @file{testsuite.at} is a Bourne shell script making use of special
19954 Autotest M4 macros.  It often contains a call to @code{AT_INIT} near
19955 its beginning followed by one call to @code{m4_include} per source file
19956 for tests.  Each such included file, or the remainder of
19957 @file{testsuite.at} if include files are not used, contain a sequence of
19958 test groups.  Each test group begins with a call to @code{AT_SETUP},
19959 then an arbitrary number of shell commands or calls to @code{AT_CHECK},
19960 and then completes with a call to @code{AT_CLEANUP}.  Multiple test
19961 groups can be categorized by a call to @code{AT_BANNER}.
19963 @defmac AT_INIT (@ovar{name})
19964 @atindex{INIT}
19965 @c FIXME: Not clear, plus duplication of the information.
19966 Initialize Autotest.  Giving a @var{name} to the test suite is
19967 encouraged if your package includes several test suites.  In any case,
19968 the test suite always displays the package name and version.  It also
19969 inherits the package bug report address.
19970 @end defmac
19972 @defmac AT_COPYRIGHT (@var{copyright-notice})
19973 @atindex{COPYRIGHT}
19974 @cindex Copyright Notice
19975 State that, in addition to the Free Software Foundation's copyright on
19976 the Autotest macros, parts of your test suite are covered by
19977 @var{copyright-notice}.
19979 The @var{copyright-notice} shows up in both the head of
19980 @command{testsuite} and in @samp{testsuite --version}.
19981 @end defmac
19983 @defmac AT_TESTED (@var{executables})
19984 @atindex{TESTED}
19985 Log the file name and answer to @option{--version} of each program in
19986 space-separated list @var{executables}.  Several invocations register
19987 new executables, in other words, don't fear registering one program
19988 several times.
19989 @end defmac
19991 Autotest test suites rely on @env{PATH} to find the tested program.
19992 This avoids the need to generate absolute names of the various tools, and
19993 makes it possible to test installed programs.  Therefore, knowing which
19994 programs are being exercised is crucial to understanding problems in
19995 the test suite itself, or its occasional misuses.  It is a good idea to
19996 also subscribe foreign programs you depend upon, to avoid incompatible
19997 diagnostics.
19999 @sp 1
20001 @defmac AT_BANNER (@var{test-category-name})
20002 @atindex{BANNER}
20003 This macro identifies the start of a category of related test groups.
20004 When the resulting @file{testsuite} is invoked with more than one test
20005 group to run, its output will include a banner containing
20006 @var{test-category-name} prior to any tests run from that category.  The
20007 banner should be no more than about 40 or 50 characters.  A blank banner
20008 will not print, effectively ending a category and letting subsequent
20009 test groups behave as though they are uncategorized when run in
20010 isolation.
20011 @end defmac
20013 @defmac AT_SETUP (@var{test-group-name})
20014 @atindex{SETUP}
20015 This macro starts a group of related tests, all to be executed in the
20016 same subshell.  It accepts a single argument, which holds a few words
20017 (no more than about 30 or 40 characters) quickly describing the purpose
20018 of the test group being started.  @var{test-group-name} must not contain
20019 unbalanced quotes or parentheses.
20020 @end defmac
20022 @defmac AT_KEYWORDS (@var{keywords})
20023 @atindex{KEYWORDS}
20024 Associate the space-separated list of @var{keywords} to the enclosing
20025 test group.  This makes it possible to run ``slices'' of the test suite.
20026 For instance, if some of your test groups exercise some @samp{foo}
20027 feature, then using @samp{AT_KEYWORDS(foo)} lets you run
20028 @samp{./testsuite -k foo} to run exclusively these test groups.  The
20029 @var{title} of the test group is automatically recorded to
20030 @code{AT_KEYWORDS}.
20032 Several invocations within a test group accumulate new keywords.  In
20033 other words, don't fear registering the same keyword several times in a
20034 test group.
20035 @end defmac
20037 @defmac AT_CAPTURE_FILE (@var{file})
20038 @atindex{CAPTURE_FILE}
20039 If the current test group fails, log the contents of @var{file}.
20040 Several identical calls within one test group have no additional effect.
20041 @end defmac
20043 @defmac AT_XFAIL_IF (@var{shell-condition})
20044 @atindex{XFAIL_IF}
20045 Determine whether the test is expected to fail because it is a known
20046 bug (for unsupported features, you should skip the test).
20047 @var{shell-condition} is a shell expression such as a @code{test}
20048 command; you can instantiate this macro many times from within the
20049 same test group, and one of the conditions is enough to turn
20050 the test into an expected failure.
20051 @end defmac
20053 @defmac AT_CLEANUP
20054 @atindex{CLEANUP}
20055 End the current test group.
20056 @end defmac
20058 @sp 1
20060 @defmac AT_DATA (@var{file}, @var{contents})
20061 @atindex{DATA}
20062 Initialize an input data @var{file} with given @var{contents}.  Of
20063 course, the @var{contents} have to be properly quoted between square
20064 brackets to protect against included commas or spurious M4
20065 expansion.  The contents ought to end with an end of line.
20066 @end defmac
20068 @defmac AT_CHECK (@var{commands}, @dvar{status, 0}, @dvar{stdout, }, @
20069   @dvar{stderr, }, @ovar{run-if-fail}, @ovar{run-if-pass})
20070 @atindex{CHECK}
20071 Execute a test by performing given shell @var{commands}.  These commands
20072 should normally exit with @var{status}, while producing expected
20073 @var{stdout} and @var{stderr} contents.  If @var{commands} exit with
20074 status 77, then the whole test group is skipped.  Otherwise, if this test
20075 fails, run shell commands @var{run-if-fail} or, if this test passes, run shell
20076 commands @var{run-if-pass}.
20078 @c Previously, we had this:
20079 @c  The @var{commands} @emph{must not} redirect the standard output, nor the
20080 @c  standard error.
20081 @c to prevent trigerring the double redirect bug on Ultrix, see
20082 @c `File Descriptors'.  This was too restricting, and Ultrix is pretty
20083 @c much dead, so we dropped the limitation; the obvious workaround on
20084 @c Ultrix is to use a working shell there.
20086 If @var{status}, or @var{stdout}, or @var{stderr} is @samp{ignore}, then
20087 the corresponding value is not checked.
20089 The special value @samp{expout} for @var{stdout} means the expected
20090 output of the @var{commands} is the content of the file @file{expout}.
20091 If @var{stdout} is @samp{stdout}, then the standard output of the
20092 @var{commands} is available for further tests in the file @file{stdout}.
20093 Similarly for @var{stderr} with @samp{experr} and @samp{stderr}.
20094 @end defmac
20097 @node testsuite Invocation
20098 @section Running @command{testsuite} Scripts
20099 @cindex @command{testsuite}
20101 Autotest test suites support the following arguments:
20103 @table @option
20104 @item --help
20105 @itemx -h
20106 Display the list of options and exit successfully.
20108 @item --version
20109 @itemx -V
20110 Display the version of the test suite and exit successfully.
20112 @item --clean
20113 @itemx -c
20114 Remove all the files the test suite might have created and exit.  Meant
20115 for @code{clean} Make targets.
20117 @item --list
20118 @itemx -l
20119 List all the tests (or only the selection), including their possible
20120 keywords.
20121 @end table
20123 @sp 1
20125 By default all tests are performed (or described with
20126 @option{--list}) in the default environment first silently, then
20127 verbosely, but the environment, set of tests, and verbosity level can be
20128 tuned:
20130 @table @samp
20131 @item @var{variable}=@var{value}
20132 Set the environment @var{variable} to @var{value}.  Use this rather
20133 than @samp{FOO=foo ./testsuite} as debugging scripts would then run in a
20134 different environment.
20136 @cindex @code{AUTOTEST_PATH}
20137 The variable @code{AUTOTEST_PATH} specifies the testing path to prepend
20138 to @env{PATH}.  Relative directory names (not starting with
20139 @samp{/}) are considered to be relative to the top level of the
20140 package being built.  All directories are made absolute, first
20141 starting from the top level @emph{build} tree, then from the
20142 @emph{source} tree.  For instance @samp{./testsuite
20143 AUTOTEST_PATH=tests:bin} for a @file{/src/foo-1.0} source package built
20144 in @file{/tmp/foo} results in @samp{/tmp/foo/tests:/tmp/foo/bin} and
20145 then @samp{/src/foo-1.0/tests:/src/foo-1.0/bin} being prepended to
20146 @env{PATH}.
20148 @item @var{number}
20149 @itemx @var{number}-@var{number}
20150 @itemx @var{number}-
20151 @itemx -@var{number}
20152 Add the corresponding test groups, with obvious semantics, to the
20153 selection.
20155 @item --keywords=@var{keywords}
20156 @itemx -k @var{keywords}
20157 Add to the selection the test groups with title or keywords (arguments
20158 to @code{AT_SETUP} or @code{AT_KEYWORDS}) that match @emph{all} keywords
20159 of the comma separated list @var{keywords}, case-insensitively.  Use
20160 @samp{!} immediately before the keyword to invert the selection for this
20161 keyword.  By default, the keywords match whole words; enclose them in
20162 @samp{.*} to also match parts of words.
20164 For example, running
20166 @example
20167 @kbd{./testsuite -k 'autoupdate,.*FUNC.*'}
20168 @end example
20170 @noindent
20171 selects all tests tagged @samp{autoupdate} @emph{and} with tags
20172 containing @samp{FUNC} (as in @samp{AC_CHECK_FUNC}, @samp{AC_FUNC_ALLOCA},
20173 etc.), while
20175 @example
20176 @kbd{./testsuite -k '!autoupdate' -k '.*FUNC.*'}
20177 @end example
20179 @noindent
20180 selects all tests not tagged @samp{autoupdate} @emph{or} with tags
20181 containing @samp{FUNC}.
20183 @item --errexit
20184 @itemx -e
20185 If any test fails, immediately abort testing.  It implies
20186 @option{--debug}: post test group clean up, and top-level logging
20187 are inhibited.  This option is meant for the full test
20188 suite, it is not really useful for generated debugging scripts.
20190 @item --verbose
20191 @itemx -v
20192 Force more verbosity in the detailed output of what is being done.  This
20193 is the default for debugging scripts.
20195 @item --debug
20196 @itemx -d
20197 Do not remove the files after a test group was performed ---but they are
20198 still removed @emph{before}, therefore using this option is sane when
20199 running several test groups.  Create debugging scripts.  Do not
20200 overwrite the top-level
20201 log (in order to preserve supposedly existing full log file).  This is
20202 the default for debugging scripts, but it can also be useful to debug
20203 the testsuite itself.
20205 @item --trace
20206 @itemx -x
20207 Trigger shell tracing of the test groups.
20208 @end table
20211 @node Making testsuite Scripts
20212 @section Making @command{testsuite} Scripts
20214 For putting Autotest into movement, you need some configuration and
20215 makefile machinery.  We recommend, at least if your package uses deep or
20216 shallow hierarchies, that you use @file{tests/} as the name of the
20217 directory holding all your tests and their makefile.  Here is a
20218 check list of things to do.
20220 @itemize @minus
20222 @item
20223 @cindex @file{package.m4}
20224 Make sure to create the file @file{package.m4}, which defines the
20225 identity of the package.  It must define @code{AT_PACKAGE_STRING}, the
20226 full signature of the package, and @code{AT_PACKAGE_BUGREPORT}, the
20227 address to which bug reports should be sent.  For sake of completeness,
20228 we suggest that you also define @code{AT_PACKAGE_NAME},
20229 @code{AT_PACKAGE_TARNAME}, and @code{AT_PACKAGE_VERSION}.
20230 @xref{Initializing configure}, for a description of these variables.  We
20231 suggest the following makefile excerpt:
20233 @smallexample
20234 $(srcdir)/package.m4: $(top_srcdir)/configure.ac
20235         @{                                      \
20236           echo '# Signature of the current package.'; \
20237           echo 'm4_define([AT_PACKAGE_NAME],      [@@PACKAGE_NAME@@])'; \
20238           echo 'm4_define([AT_PACKAGE_TARNAME],   [@@PACKAGE_TARNAME@@])'; \
20239           echo 'm4_define([AT_PACKAGE_VERSION],   [@@PACKAGE_VERSION@@])'; \
20240           echo 'm4_define([AT_PACKAGE_STRING],    [@@PACKAGE_STRING@@])'; \
20241           echo 'm4_define([AT_PACKAGE_BUGREPORT], [@@PACKAGE_BUGREPORT@@])'; \
20242         @} >'$(srcdir)/package.m4'
20243 @end smallexample
20245 @noindent
20246 Be sure to distribute @file{package.m4} and to put it into the source
20247 hierarchy: the test suite ought to be shipped!
20249 @item
20250 Invoke @code{AC_CONFIG_TESTDIR}.
20252 @defmac AC_CONFIG_TESTDIR (@var{directory}, @dvar{test-path, directory})
20253 @acindex{CONFIG_TESTDIR}
20254 An Autotest test suite is to be configured in @var{directory}.  This
20255 macro requires the instantiation of @file{@var{directory}/atconfig} from
20256 @file{@var{directory}/atconfig.in}, and sets the default
20257 @code{AUTOTEST_PATH} to @var{test-path} (@pxref{testsuite Invocation}).
20258 @end defmac
20260 @item
20261 Still within @file{configure.ac}, as appropriate, ensure that some
20262 @code{AC_CONFIG_FILES} command includes substitution for
20263 @file{tests/atlocal}.
20265 @item
20266 The @file{tests/Makefile.in} should be modified so the validation in
20267 your package is triggered by @samp{make check}.  An example is provided
20268 below.
20269 @end itemize
20271 With Automake, here is a minimal example about how to link @samp{make
20272 check} with a validation suite.
20274 @example
20275 EXTRA_DIST = testsuite.at $(TESTSUITE) atlocal.in
20276 TESTSUITE = $(srcdir)/testsuite
20278 check-local: atconfig atlocal $(TESTSUITE)
20279         $(SHELL) '$(TESTSUITE)' $(TESTSUITEFLAGS)
20281 installcheck-local: atconfig atlocal $(TESTSUITE)
20282         $(SHELL) '$(TESTSUITE)' AUTOTEST_PATH='$(bindir)' \
20283           $(TESTSUITEFLAGS)
20285 clean-local:
20286         test ! -f '$(TESTSUITE)' || \
20287          $(SHELL) '$(TESTSUITE)' --clean
20289 AUTOTEST = $(AUTOM4TE) --language=autotest
20290 $(TESTSUITE): $(srcdir)/testsuite.at
20291         $(AUTOTEST) -I '$(srcdir)' -o $@@.tmp $@@.at
20292         mv $@@.tmp $@@
20293 @end example
20295 You might want to list explicitly the dependencies, i.e., the list of
20296 the files @file{testsuite.at} includes.
20298 If you don't use Automake, you might need to add lines inspired from the
20299 following:
20301 @example
20302 subdir = tests
20304 atconfig: $(top_builddir)/config.status
20305         cd $(top_builddir) && \
20306            $(SHELL) ./config.status $(subdir)/$@@
20308 atlocal: $(srcdir)/atlocal.in $(top_builddir)/config.status
20309         cd $(top_builddir) && \
20310            $(SHELL) ./config.status $(subdir)/$@@
20311 @end example
20313 @noindent
20314 and manage to have @code{$(EXTRA_DIST)} distributed.
20316 If you use Automake, however, you don't need to add a rule to generate
20317 @file{atlocal}.
20319 With all this in place, and if you have not initialized @samp{TESTSUITEFLAGS}
20320 within your makefile, you can fine-tune test suite execution with this
20321 variable, for example:
20323 @example
20324 make check TESTSUITEFLAGS='-v -d -x 75 -k AC_PROG_CC CFLAGS=-g'
20325 @end example
20329 @c =============================== Frequent Autoconf Questions, with answers
20331 @node FAQ
20332 @chapter Frequent Autoconf Questions, with answers
20334 Several questions about Autoconf come up occasionally.  Here some of them
20335 are addressed.
20337 @menu
20338 * Distributing::                Distributing @command{configure} scripts
20339 * Why GNU M4::                  Why not use the standard M4?
20340 * Bootstrapping::               Autoconf and @acronym{GNU} M4 require each other?
20341 * Why Not Imake::               Why @acronym{GNU} uses @command{configure} instead of Imake
20342 * Defining Directories::        Passing @code{datadir} to program
20343 * Autom4te Cache::              What is it?  Can I remove it?
20344 * Present But Cannot Be Compiled::  Compiler and Preprocessor Disagree
20345 @end menu
20347 @node Distributing
20348 @section Distributing @command{configure} Scripts
20349 @cindex License
20351 @display
20352 What are the restrictions on distributing @command{configure}
20353 scripts that Autoconf generates?  How does that affect my
20354 programs that use them?
20355 @end display
20357 There are no restrictions on how the configuration scripts that Autoconf
20358 produces may be distributed or used.  In Autoconf version 1, they were
20359 covered by the @acronym{GNU} General Public License.  We still encourage
20360 software authors to distribute their work under terms like those of the
20361 @acronym{GPL}, but doing so is not required to use Autoconf.
20363 Of the other files that might be used with @command{configure},
20364 @file{config.h.in} is under whatever copyright you use for your
20365 @file{configure.ac}.  @file{config.sub} and @file{config.guess} have an
20366 exception to the @acronym{GPL} when they are used with an Autoconf-generated
20367 @command{configure} script, which permits you to distribute them under the
20368 same terms as the rest of your package.  @file{install-sh} is from the X
20369 Consortium and is not copyrighted.
20371 @node Why GNU M4
20372 @section Why Require @acronym{GNU} M4?
20374 @display
20375 Why does Autoconf require @acronym{GNU} M4?
20376 @end display
20378 Many M4 implementations have hard-coded limitations on the size and
20379 number of macros that Autoconf exceeds.  They also lack several
20380 builtin macros that it would be difficult to get along without in a
20381 sophisticated application like Autoconf, including:
20383 @example
20384 m4_builtin
20385 m4_indir
20386 m4_bpatsubst
20387 __file__
20388 __line__
20389 @end example
20391 Autoconf requires version 1.4.5 or later of @acronym{GNU} M4.
20393 Since only software maintainers need to use Autoconf, and since @acronym{GNU}
20394 M4 is simple to configure and install, it seems reasonable to require
20395 @acronym{GNU} M4 to be installed also.  Many maintainers of @acronym{GNU} and
20396 other free software already have most of the @acronym{GNU} utilities
20397 installed, since they prefer them.
20399 @node Bootstrapping
20400 @section How Can I Bootstrap?
20401 @cindex Bootstrap
20403 @display
20404 If Autoconf requires @acronym{GNU} M4 and @acronym{GNU} M4 has an Autoconf
20405 @command{configure} script, how do I bootstrap?  It seems like a chicken
20406 and egg problem!
20407 @end display
20409 This is a misunderstanding.  Although @acronym{GNU} M4 does come with a
20410 @command{configure} script produced by Autoconf, Autoconf is not required
20411 in order to run the script and install @acronym{GNU} M4.  Autoconf is only
20412 required if you want to change the M4 @command{configure} script, which few
20413 people have to do (mainly its maintainer).
20415 @node Why Not Imake
20416 @section Why Not Imake?
20417 @cindex Imake
20419 @display
20420 Why not use Imake instead of @command{configure} scripts?
20421 @end display
20423 Several people have written addressing this question, so I include
20424 adaptations of their explanations here.
20426 The following answer is based on one written by Richard Pixley:
20428 @quotation
20429 Autoconf generated scripts frequently work on machines that it has
20430 never been set up to handle before.  That is, it does a good job of
20431 inferring a configuration for a new system.  Imake cannot do this.
20433 Imake uses a common database of host specific data.  For X11, this makes
20434 sense because the distribution is made as a collection of tools, by one
20435 central authority who has control over the database.
20437 @acronym{GNU} tools are not released this way.  Each @acronym{GNU} tool has a
20438 maintainer; these maintainers are scattered across the world.  Using a
20439 common database would be a maintenance nightmare.  Autoconf may appear
20440 to be this kind of database, but in fact it is not.  Instead of listing
20441 host dependencies, it lists program requirements.
20443 If you view the @acronym{GNU} suite as a collection of native tools, then the
20444 problems are similar.  But the @acronym{GNU} development tools can be
20445 configured as cross tools in almost any host+target permutation.  All of
20446 these configurations can be installed concurrently.  They can even be
20447 configured to share host independent files across hosts.  Imake doesn't
20448 address these issues.
20450 Imake templates are a form of standardization.  The @acronym{GNU} coding
20451 standards address the same issues without necessarily imposing the same
20452 restrictions.
20453 @end quotation
20456 Here is some further explanation, written by Per Bothner:
20458 @quotation
20459 One of the advantages of Imake is that it easy to generate large
20460 makefiles using the @samp{#include} and macro mechanisms of @command{cpp}.
20461 However, @code{cpp} is not programmable: it has limited conditional
20462 facilities, and no looping.  And @code{cpp} cannot inspect its
20463 environment.
20465 All of these problems are solved by using @code{sh} instead of
20466 @code{cpp}.  The shell is fully programmable, has macro substitution,
20467 can execute (or source) other shell scripts, and can inspect its
20468 environment.
20469 @end quotation
20472 Paul Eggert elaborates more:
20474 @quotation
20475 With Autoconf, installers need not assume that Imake itself is already
20476 installed and working well.  This may not seem like much of an advantage
20477 to people who are accustomed to Imake.  But on many hosts Imake is not
20478 installed or the default installation is not working well, and requiring
20479 Imake to install a package hinders the acceptance of that package on
20480 those hosts.  For example, the Imake template and configuration files
20481 might not be installed properly on a host, or the Imake build procedure
20482 might wrongly assume that all source files are in one big directory
20483 tree, or the Imake configuration might assume one compiler whereas the
20484 package or the installer needs to use another, or there might be a
20485 version mismatch between the Imake expected by the package and the Imake
20486 supported by the host.  These problems are much rarer with Autoconf,
20487 where each package comes with its own independent configuration
20488 processor.
20490 Also, Imake often suffers from unexpected interactions between
20491 @command{make} and the installer's C preprocessor.  The fundamental problem
20492 here is that the C preprocessor was designed to preprocess C programs,
20493 not makefiles.  This is much less of a problem with Autoconf,
20494 which uses the general-purpose preprocessor M4, and where the
20495 package's author (rather than the installer) does the preprocessing in a
20496 standard way.
20497 @end quotation
20500 Finally, Mark Eichin notes:
20502 @quotation
20503 Imake isn't all that extensible, either.  In order to add new features to
20504 Imake, you need to provide your own project template, and duplicate most
20505 of the features of the existing one.  This means that for a sophisticated
20506 project, using the vendor-provided Imake templates fails to provide any
20507 leverage---since they don't cover anything that your own project needs
20508 (unless it is an X11 program).
20510 On the other side, though:
20512 The one advantage that Imake has over @command{configure}:
20513 @file{Imakefile} files tend to be much shorter (likewise, less redundant)
20514 than @file{Makefile.in} files.  There is a fix to this, however---at least
20515 for the Kerberos V5 tree, we've modified things to call in common
20516 @file{post.in} and @file{pre.in} makefile fragments for the
20517 entire tree.  This means that a lot of common things don't have to be
20518 duplicated, even though they normally are in @command{configure} setups.
20519 @end quotation
20522 @node Defining Directories
20523 @section How Do I @code{#define} Installation Directories?
20525 @display
20526 My program needs library files, installed in @code{datadir} and
20527 similar.  If I use
20529 @example
20530 AC_DEFINE_UNQUOTED([DATADIR], [$datadir],
20531   [Define to the read-only architecture-independent
20532    data directory.])
20533 @end example
20535 @noindent
20536 I get
20538 @example
20539 #define DATADIR "$@{prefix@}/share"
20540 @end example
20541 @end display
20543 As already explained, this behavior is on purpose, mandated by the
20544 @acronym{GNU} Coding Standards, see @ref{Installation Directory
20545 Variables}.  There are several means to achieve a similar goal:
20547 @itemize @minus
20548 @item
20549 Do not use @code{AC_DEFINE} but use your makefile to pass the
20550 actual value of @code{datadir} via compilation flags.
20551 @xref{Installation Directory Variables}, for the details.
20553 @item
20554 This solution can be simplified when compiling a program: you may either
20555 extend the @code{CPPFLAGS}:
20557 @example
20558 CPPFLAGS = -DDATADIR='"$(datadir)"' @@CPPFLAGS@@
20559 @end example
20561 @noindent
20562 If you are using Automake, you should use @code{AM_CPPFLAGS} instead:
20564 @example
20565 AM_CPPFLAGS = -DDATADIR='"$(datadir)"'
20566 @end example
20568 @noindent
20569 Alternatively, create a dedicated header file:
20571 @example
20572 DISTCLEANFILES = myprog-paths.h
20573 myprog-paths.h: Makefile
20574         echo '#define DATADIR "$(datadir)"' >$@@
20575 @end example
20577 @item
20578 Use @code{AC_DEFINE} but have @command{configure} compute the literal
20579 value of @code{datadir} and others.  Many people have wrapped macros to
20580 automate this task.  For instance, the macro @code{AC_DEFINE_DIR} from
20581 the @uref{http://autoconf-archive.cryp.to/, Autoconf Macro
20582 Archive}.
20584 This solution does not conform to the @acronym{GNU} Coding Standards.
20586 @item
20587 Note that all the previous solutions hard wire the absolute name of
20588 these directories in the executables, which is not a good property.  You
20589 may try to compute the names relative to @code{prefix}, and try to
20590 find @code{prefix} at runtime, this way your package is relocatable.
20591 @end itemize
20594 @node Autom4te Cache
20595 @section What is @file{autom4te.cache}?
20597 @display
20598 What is this directory @file{autom4te.cache}?  Can I safely remove it?
20599 @end display
20601 In the @acronym{GNU} Build System, @file{configure.ac} plays a central
20602 role and is read by many tools: @command{autoconf} to create
20603 @file{configure}, @command{autoheader} to create @file{config.h.in},
20604 @command{automake} to create @file{Makefile.in}, @command{autoscan} to
20605 check the completeness of @file{configure.ac}, @command{autoreconf} to
20606 check the @acronym{GNU} Build System components that are used.  To
20607 ``read @file{configure.ac}'' actually means to compile it with M4,
20608 which can be a long process for complex @file{configure.ac}.
20610 This is why all these tools, instead of running directly M4, invoke
20611 @command{autom4te} (@pxref{autom4te Invocation}) which, while answering to
20612 a specific demand, stores additional information in
20613 @file{autom4te.cache} for future runs.  For instance, if you run
20614 @command{autoconf}, behind the scenes, @command{autom4te} also
20615 stores information for the other tools, so that when you invoke
20616 @command{autoheader} or @command{automake} etc., reprocessing
20617 @file{configure.ac} is not needed.  The speed up is frequently of 30%,
20618 and is increasing with the size of @file{configure.ac}.
20620 But it is and remains being simply a cache: you can safely remove it.
20622 @sp 1
20624 @display
20625 Can I permanently get rid of it?
20626 @end display
20628 The creation of this cache can be disabled from
20629 @file{~/.autom4te.cfg}, see @ref{Customizing autom4te}, for more
20630 details.  You should be aware that disabling the cache slows down the
20631 Autoconf test suite by 40%.  The more @acronym{GNU} Build System
20632 components are used, the more the cache is useful; for instance
20633 running @samp{autoreconf -f} on the Core Utilities is twice slower without
20634 the cache @emph{although @option{--force} implies that the cache is
20635 not fully exploited}, and eight times slower than without
20636 @option{--force}.
20639 @node Present But Cannot Be Compiled
20640 @section Header Present But Cannot Be Compiled
20642 The most important guideline to bear in mind when checking for
20643 features is to mimic as much as possible the intended use.
20644 Unfortunately, old versions of @code{AC_CHECK_HEADER} and
20645 @code{AC_CHECK_HEADERS} failed to follow this idea, and called
20646 the preprocessor, instead of the compiler, to check for headers.  As a
20647 result, incompatibilities between headers went unnoticed during
20648 configuration, and maintainers finally had to deal with this issue
20649 elsewhere.
20651 As of Autoconf 2.56 both checks are performed, and @code{configure}
20652 complains loudly if the compiler and the preprocessor do not agree.
20653 For the time being the result used is that of the preprocessor, to give
20654 maintainers time to adjust their @file{configure.ac}, but in the
20655 future, only the compiler will be considered.
20657 Consider the following example:
20659 @smallexample
20660 $ @kbd{cat number.h}
20661 typedef int number;
20662 $ @kbd{cat pi.h}
20663 const number pi = 3;
20664 $ @kbd{cat configure.ac}
20665 AC_INIT([Example], [1.0], [bug-example@@example.org])
20666 AC_CHECK_HEADERS([pi.h])
20667 $ @kbd{autoconf -Wall}
20668 $ @kbd{./configure}
20669 checking for gcc... gcc
20670 checking for C compiler default output file name... a.out
20671 checking whether the C compiler works... yes
20672 checking whether we are cross compiling... no
20673 checking for suffix of executables...
20674 checking for suffix of object files... o
20675 checking whether we are using the GNU C compiler... yes
20676 checking whether gcc accepts -g... yes
20677 checking for gcc option to accept ISO C89... none needed
20678 checking how to run the C preprocessor... gcc -E
20679 checking for grep that handles long lines and -e... grep
20680 checking for egrep... grep -E
20681 checking for ANSI C header files... yes
20682 checking for sys/types.h... yes
20683 checking for sys/stat.h... yes
20684 checking for stdlib.h... yes
20685 checking for string.h... yes
20686 checking for memory.h... yes
20687 checking for strings.h... yes
20688 checking for inttypes.h... yes
20689 checking for stdint.h... yes
20690 checking for unistd.h... yes
20691 checking pi.h usability... no
20692 checking pi.h presence... yes
20693 configure: WARNING: pi.h: present but cannot be compiled
20694 configure: WARNING: pi.h:     check for missing prerequisite headers?
20695 configure: WARNING: pi.h: see the Autoconf documentation
20696 configure: WARNING: pi.h:     section "Present But Cannot Be Compiled"
20697 configure: WARNING: pi.h: proceeding with the preprocessor's result
20698 configure: WARNING: pi.h: in the future, the compiler will take precedence
20699 configure: WARNING:     ## -------------------------------------- ##
20700 configure: WARNING:     ## Report this to bug-example@@example.org ##
20701 configure: WARNING:     ## -------------------------------------- ##
20702 checking for pi.h... yes
20703 @end smallexample
20705 @noindent
20706 The proper way the handle this case is using the fourth argument
20707 (@pxref{Generic Headers}):
20709 @example
20710 $ @kbd{cat configure.ac}
20711 AC_INIT([Example], [1.0], [bug-example@@example.org])
20712 AC_CHECK_HEADERS([number.h pi.h], [], [],
20713 [[#ifdef HAVE_NUMBER_H
20714 # include <number.h>
20715 #endif
20717 $ @kbd{autoconf -Wall}
20718 $ @kbd{./configure}
20719 checking for gcc... gcc
20720 checking for C compiler default output... a.out
20721 checking whether the C compiler works... yes
20722 checking whether we are cross compiling... no
20723 checking for suffix of executables...
20724 checking for suffix of object files... o
20725 checking whether we are using the GNU C compiler... yes
20726 checking whether gcc accepts -g... yes
20727 checking for gcc option to accept ANSI C... none needed
20728 checking for number.h... yes
20729 checking for pi.h... yes
20730 @end example
20732 See @ref{Particular Headers}, for a list of headers with their
20733 prerequisite.
20735 @c ===================================================== History of Autoconf.
20737 @node History
20738 @chapter History of Autoconf
20739 @cindex History of autoconf
20741 You may be wondering, Why was Autoconf originally written?  How did it
20742 get into its present form?  (Why does it look like gorilla spit?)  If
20743 you're not wondering, then this chapter contains no information useful
20744 to you, and you might as well skip it.  If you @emph{are} wondering,
20745 then let there be light@enddots{}
20747 @menu
20748 * Genesis::                     Prehistory and naming of @command{configure}
20749 * Exodus::                      The plagues of M4 and Perl
20750 * Leviticus::                   The priestly code of portability arrives
20751 * Numbers::                     Growth and contributors
20752 * Deuteronomy::                 Approaching the promises of easy configuration
20753 @end menu
20755 @node Genesis
20756 @section Genesis
20758 In June 1991 I was maintaining many of the @acronym{GNU} utilities for the
20759 Free Software Foundation.  As they were ported to more platforms and
20760 more programs were added, the number of @option{-D} options that users
20761 had to select in the makefile (around 20) became burdensome.
20762 Especially for me---I had to test each new release on a bunch of
20763 different systems.  So I wrote a little shell script to guess some of
20764 the correct settings for the fileutils package, and released it as part
20765 of fileutils 2.0.  That @command{configure} script worked well enough that
20766 the next month I adapted it (by hand) to create similar @command{configure}
20767 scripts for several other @acronym{GNU} utilities packages.  Brian Berliner
20768 also adapted one of my scripts for his @acronym{CVS} revision control system.
20770 Later that summer, I learned that Richard Stallman and Richard Pixley
20771 were developing similar scripts to use in the @acronym{GNU} compiler tools;
20772 so I adapted my @command{configure} scripts to support their evolving
20773 interface: using the file name @file{Makefile.in} as the templates;
20774 adding @samp{+srcdir}, the first option (of many); and creating
20775 @file{config.status} files.
20777 @node Exodus
20778 @section Exodus
20780 As I got feedback from users, I incorporated many improvements, using
20781 Emacs to search and replace, cut and paste, similar changes in each of
20782 the scripts.  As I adapted more @acronym{GNU} utilities packages to use
20783 @command{configure} scripts, updating them all by hand became impractical.
20784 Rich Murphey, the maintainer of the @acronym{GNU} graphics utilities, sent me
20785 mail saying that the @command{configure} scripts were great, and asking if
20786 I had a tool for generating them that I could send him.  No, I thought,
20787 but I should!  So I started to work out how to generate them.  And the
20788 journey from the slavery of hand-written @command{configure} scripts to the
20789 abundance and ease of Autoconf began.
20791 Cygnus @command{configure}, which was being developed at around that time,
20792 is table driven; it is meant to deal mainly with a discrete number of
20793 system types with a small number of mainly unguessable features (such as
20794 details of the object file format).  The automatic configuration system
20795 that Brian Fox had developed for Bash takes a similar approach.  For
20796 general use, it seems to me a hopeless cause to try to maintain an
20797 up-to-date database of which features each variant of each operating
20798 system has.  It's easier and more reliable to check for most features on
20799 the fly---especially on hybrid systems that people have hacked on
20800 locally or that have patches from vendors installed.
20802 I considered using an architecture similar to that of Cygnus
20803 @command{configure}, where there is a single @command{configure} script that
20804 reads pieces of @file{configure.in} when run.  But I didn't want to have
20805 to distribute all of the feature tests with every package, so I settled
20806 on having a different @command{configure} made from each
20807 @file{configure.in} by a preprocessor.  That approach also offered more
20808 control and flexibility.
20810 I looked briefly into using the Metaconfig package, by Larry Wall,
20811 Harlan Stenn, and Raphael Manfredi, but I decided not to for several
20812 reasons.  The @command{Configure} scripts it produces are interactive,
20813 which I find quite inconvenient; I didn't like the ways it checked for
20814 some features (such as library functions); I didn't know that it was
20815 still being maintained, and the @command{Configure} scripts I had
20816 seen didn't work on many modern systems (such as System V R4 and NeXT);
20817 it wasn't flexible in what it could do in response to a feature's
20818 presence or absence; I found it confusing to learn; and it was too big
20819 and complex for my needs (I didn't realize then how much Autoconf would
20820 eventually have to grow).
20822 I considered using Perl to generate my style of @command{configure}
20823 scripts, but decided that M4 was better suited to the job of simple
20824 textual substitutions: it gets in the way less, because output is
20825 implicit.  Plus, everyone already has it.  (Initially I didn't rely on
20826 the @acronym{GNU} extensions to M4.)  Also, some of my friends at the
20827 University of Maryland had recently been putting M4 front ends on
20828 several programs, including @code{tvtwm}, and I was interested in trying
20829 out a new language.
20831 @node Leviticus
20832 @section Leviticus
20834 Since my @command{configure} scripts determine the system's capabilities
20835 automatically, with no interactive user intervention, I decided to call
20836 the program that generates them Autoconfig.  But with a version number
20837 tacked on, that name would be too long for old Unix file systems,
20838 so I shortened it to Autoconf.
20840 In the fall of 1991 I called together a group of fellow questers after
20841 the Holy Grail of portability (er, that is, alpha testers) to give me
20842 feedback as I encapsulated pieces of my handwritten scripts in M4 macros
20843 and continued to add features and improve the techniques used in the
20844 checks.  Prominent among the testers were Fran@,{c}ois Pinard, who came up
20845 with the idea of making an Autoconf shell script to run M4
20846 and check for unresolved macro calls; Richard Pixley, who suggested
20847 running the compiler instead of searching the file system to find
20848 include files and symbols, for more accurate results; Karl Berry, who
20849 got Autoconf to configure @TeX{} and added the macro index to the
20850 documentation; and Ian Lance Taylor, who added support for creating a C
20851 header file as an alternative to putting @option{-D} options in a
20852 makefile, so he could use Autoconf for his @acronym{UUCP} package.
20853 The alpha testers cheerfully adjusted their files again and again as the
20854 names and calling conventions of the Autoconf macros changed from
20855 release to release.  They all contributed many specific checks, great
20856 ideas, and bug fixes.
20858 @node Numbers
20859 @section Numbers
20861 In July 1992, after months of alpha testing, I released Autoconf 1.0,
20862 and converted many @acronym{GNU} packages to use it.  I was surprised by how
20863 positive the reaction to it was.  More people started using it than I
20864 could keep track of, including people working on software that wasn't
20865 part of the @acronym{GNU} Project (such as TCL, FSP, and Kerberos V5).
20866 Autoconf continued to improve rapidly, as many people using the
20867 @command{configure} scripts reported problems they encountered.
20869 Autoconf turned out to be a good torture test for M4 implementations.
20870 Unix M4 started to dump core because of the length of the
20871 macros that Autoconf defined, and several bugs showed up in @acronym{GNU}
20872 M4 as well.  Eventually, we realized that we needed to use some
20873 features that only @acronym{GNU} M4 has.  4.3@acronym{BSD} M4, in
20874 particular, has an impoverished set of builtin macros; the System V
20875 version is better, but still doesn't provide everything we need.
20877 More development occurred as people put Autoconf under more stresses
20878 (and to uses I hadn't anticipated).  Karl Berry added checks for X11.
20879 david zuhn contributed C++ support.  Fran@,{c}ois Pinard made it diagnose
20880 invalid arguments.  Jim Blandy bravely coerced it into configuring
20881 @acronym{GNU} Emacs, laying the groundwork for several later improvements.
20882 Roland McGrath got it to configure the @acronym{GNU} C Library, wrote the
20883 @command{autoheader} script to automate the creation of C header file
20884 templates, and added a @option{--verbose} option to @command{configure}.
20885 Noah Friedman added the @option{--autoconf-dir} option and
20886 @code{AC_MACRODIR} environment variable.  (He also coined the term
20887 @dfn{autoconfiscate} to mean ``adapt a software package to use
20888 Autoconf''.)  Roland and Noah improved the quoting protection in
20889 @code{AC_DEFINE} and fixed many bugs, especially when I got sick of
20890 dealing with portability problems from February through June, 1993.
20892 @node Deuteronomy
20893 @section Deuteronomy
20895 A long wish list for major features had accumulated, and the effect of
20896 several years of patching by various people had left some residual
20897 cruft.  In April 1994, while working for Cygnus Support, I began a major
20898 revision of Autoconf.  I added most of the features of the Cygnus
20899 @command{configure} that Autoconf had lacked, largely by adapting the
20900 relevant parts of Cygnus @command{configure} with the help of david zuhn
20901 and Ken Raeburn.  These features include support for using
20902 @file{config.sub}, @file{config.guess}, @option{--host}, and
20903 @option{--target}; making links to files; and running @command{configure}
20904 scripts in subdirectories.  Adding these features enabled Ken to convert
20905 @acronym{GNU} @code{as}, and Rob Savoye to convert Deja@acronym{GNU}, to using
20906 Autoconf.
20908 I added more features in response to other peoples' requests.  Many
20909 people had asked for @command{configure} scripts to share the results of
20910 the checks between runs, because (particularly when configuring a large
20911 source tree, like Cygnus does) they were frustratingly slow.  Mike
20912 Haertel suggested adding site-specific initialization scripts.  People
20913 distributing software that had to unpack on MS-DOS asked for a way to
20914 override the @file{.in} extension on the file names, which produced file
20915 names like @file{config.h.in} containing two dots.  Jim Avera did an
20916 extensive examination of the problems with quoting in @code{AC_DEFINE}
20917 and @code{AC_SUBST}; his insights led to significant improvements.
20918 Richard Stallman asked that compiler output be sent to @file{config.log}
20919 instead of @file{/dev/null}, to help people debug the Emacs
20920 @command{configure} script.
20922 I made some other changes because of my dissatisfaction with the quality
20923 of the program.  I made the messages showing results of the checks less
20924 ambiguous, always printing a result.  I regularized the names of the
20925 macros and cleaned up coding style inconsistencies.  I added some
20926 auxiliary utilities that I had developed to help convert source code
20927 packages to use Autoconf.  With the help of Fran@,{c}ois Pinard, I made
20928 the macros not interrupt each others' messages.  (That feature revealed
20929 some performance bottlenecks in @acronym{GNU} M4, which he hastily
20930 corrected!)  I reorganized the documentation around problems people want
20931 to solve.  And I began a test suite, because experience had shown that
20932 Autoconf has a pronounced tendency to regress when we change it.
20934 Again, several alpha testers gave invaluable feedback, especially
20935 Fran@,{c}ois Pinard, Jim Meyering, Karl Berry, Rob Savoye, Ken Raeburn,
20936 and Mark Eichin.
20938 Finally, version 2.0 was ready.  And there was much rejoicing.  (And I
20939 have free time again.  I think.  Yeah, right.)
20942 @c ========================================================== Appendices
20945 @node GNU Free Documentation License
20946 @appendix GNU Free Documentation License
20948 @include fdl.texi
20950 @node Indices
20951 @appendix Indices
20953 @menu
20954 * Environment Variable Index::  Index of environment variables used
20955 * Output Variable Index::       Index of variables set in output files
20956 * Preprocessor Symbol Index::   Index of C preprocessor symbols defined
20957 * Autoconf Macro Index::        Index of Autoconf macros
20958 * M4 Macro Index::              Index of M4, M4sugar, and M4sh macros
20959 * Autotest Macro Index::        Index of Autotest macros
20960 * Program & Function Index::    Index of those with portability problems
20961 * Concept Index::               General index
20962 @end menu
20964 @node Environment Variable Index
20965 @appendixsec Environment Variable Index
20967 This is an alphabetical list of the environment variables that Autoconf
20968 checks.
20970 @printindex ev
20972 @node Output Variable Index
20973 @appendixsec Output Variable Index
20975 This is an alphabetical list of the variables that Autoconf can
20976 substitute into files that it creates, typically one or more
20977 makefiles.  @xref{Setting Output Variables}, for more information
20978 on how this is done.
20980 @printindex ov
20982 @node Preprocessor Symbol Index
20983 @appendixsec Preprocessor Symbol Index
20985 This is an alphabetical list of the C preprocessor symbols that the
20986 Autoconf macros define.  To work with Autoconf, C source code needs to
20987 use these names in @code{#if} or @code{#ifdef} directives.
20989 @printindex cv
20991 @node Autoconf Macro Index
20992 @appendixsec Autoconf Macro Index
20994 This is an alphabetical list of the Autoconf macros.
20995 @ifset shortindexflag
20996 To make the list easier to use, the macros are listed without their
20997 preceding @samp{AC_}.
20998 @end ifset
21000 @printindex AC
21002 @node M4 Macro Index
21003 @appendixsec M4 Macro Index
21005 This is an alphabetical list of the M4, M4sugar, and M4sh macros.
21006 @ifset shortindexflag
21007 To make the list easier to use, the macros are listed without their
21008 preceding @samp{m4_} or @samp{AS_}.
21009 @end ifset
21011 @printindex MS
21013 @node Autotest Macro Index
21014 @appendixsec Autotest Macro Index
21016 This is an alphabetical list of the Autotest macros.
21017 @ifset shortindexflag
21018 To make the list easier to use, the macros are listed without their
21019 preceding @samp{AT_}.
21020 @end ifset
21022 @printindex AT
21024 @node Program & Function Index
21025 @appendixsec Program and Function Index
21027 This is an alphabetical list of the programs and functions whose
21028 portability is discussed in this document.
21030 @printindex pr
21032 @node Concept Index
21033 @appendixsec Concept Index
21035 This is an alphabetical list of the files, tools, and concepts
21036 introduced in this document.
21038 @printindex cp
21040 @bye
21042 @c  LocalWords:  texinfo setfilename autoconf texi settitle setchapternewpage
21043 @c  LocalWords:  setcontentsaftertitlepage finalout ARG ovar varname dvar acx
21044 @c  LocalWords:  makeinfo dvi defcodeindex ev ov CPP cv Autotest mv defindex fn
21045 @c  LocalWords:  shortindexflag iftex ifset acindex ACindex ifclear ahindex fu
21046 @c  LocalWords:  asindex MSindex atindex ATindex auindex hdrindex prindex FIXME
21047 @c  LocalWords:  msindex alloca fnindex Aaarg indices FSF's dircategory ifnames
21048 @c  LocalWords:  direntry autoscan autoreconf autoheader autoupdate config FDs
21049 @c  LocalWords:  testsuite titlepage Elliston Demaille vskip filll ifnottex hmm
21050 @c  LocalWords:  insertcopying Autoconf's detailmenu Automake Libtool Posix ois
21051 @c  LocalWords:  Systemology Checkpointing Changequote INTERCAL changequote dfn
21052 @c  LocalWords:  Quadrigraphs builtins Shellology acconfig Bugward LIBOBJ Imake
21053 @c  LocalWords:  LIBOBJS IFELSE cindex flushright Pinard Metaconfig uref Simons
21054 @c  LocalWords:  distclean uninstall noindent versioning Tromey dir
21055 @c  LocalWords:  SAMS samp aclocal acsite underquoted emph itemx prepend SUBST
21056 @c  LocalWords:  evindex automake Gettext autopoint gettext symlink libtoolize
21057 @c  LocalWords:  defmac INIT tarname ovindex cvindex BUGREPORT PREREQ asis PROG
21058 @c  LocalWords:  SRCDIR srcdir globbing afterwards cmds foos fooo foooo init cd
21059 @c  LocalWords:  builddir timestamp src Imakefile chmod defvar CFLAGS CPPFLAGS
21060 @c  LocalWords:  CXXFLAGS DEFS DHAVE defvarx FCFLAGS FFLAGS LDFLAGS bindir GCC
21061 @c  LocalWords:  datadir datarootdir docdir dvidir htmldir libdir ifnothtml kbd
21062 @c  LocalWords:  includedir infodir libexecdir localedir localstatedir mandir
21063 @c  LocalWords:  oldincludedir pdfdir PDF psdir PostScript sbindir sysconfdir
21064 @c  LocalWords:  sharedstatedir DDATADIR sed tmp pkgdatadir VPATH conf unistd
21065 @c  LocalWords:  undef endif builtin FUNCS ifndef STACKSEG getb GETB YMP fubar
21066 @c  LocalWords:  PRE dest SUBDIRS subdirs fi struct STDC stdlib stddef INTTYPES
21067 @c  LocalWords:  inttypes STDINT stdint AWK AIX Solaris NeXT env EGREP FGREP yy
21068 @c  LocalWords:  LEXLIB YYTEXT lfl nonportable Automake's LN RANLIB byacc INETD
21069 @c  LocalWords:  inetd prog PROGS progs ranlib lmp lXt lX nsl gethostbyname UX
21070 @c  LocalWords:  NextStep isinf isnan glibc IRIX sunmath lm lsunmath pre sizeof
21071 @c  LocalWords:  ld inline malloc putenv setenv FreeBSD realloc SunOS MinGW
21072 @c  LocalWords:  snprintf vsnprintf sprintf vsprintf sscanf gcc strerror ifdef
21073 @c  LocalWords:  strnlen sysconf PAGESIZE unsetenv va fallback memcpy dst FUNC
21074 @c  LocalWords:  PowerPC GNUC libPW pragma Olibcalls CHOWN chown CLOSEDIR VFORK
21075 @c  LocalWords:  closedir FNMATCH fnmatch vfork FSEEKO LARGEFILE fseeko SVR sc
21076 @c  LocalWords:  largefile GETGROUPS getgroups GETLOADAVG DGUX UMAX NLIST KMEM
21077 @c  LocalWords:  SETGID getloadavg nlist GETMNTENT irix
21078 @c  LocalWords:  getmntent UnixWare GETPGRP getpgid getpgrp Posix's pid LSTAT
21079 @c  LocalWords:  lstat rpl MEMCMP memcmp OpenStep MBRTOWC mbrtowc MKTIME mktime
21080 @c  LocalWords:  localtime MMAP mmap OBSTACK obstack obstacks ARGTYPES timeval
21081 @c  LocalWords:  SETPGRP setpgrp defmacx Hurd SETVBUF setvbuf STRCOLL strcoll
21082 @c  LocalWords:  STRTOD strtod DECL STRFTIME strftime SCO UTIME utime VPRINTF
21083 @c  LocalWords:  DOPRNT vprintf doprnt sp unfixable LIBSOURCE LIBSOURCES Eggert
21084 @c  LocalWords:  linux netinet ia Tru XFree DIRENT NDIR dirent ndir multitable
21085 @c  LocalWords:  NAMLEN strlen namlen MKDEV SYSMACROS makedev RESOLV resolv DNS
21086 @c  LocalWords:  inet structs NAMESER arpa NETDB netdb UTekV UTS GCC's kB
21087 @c  LocalWords:  STDBOOL BOOL stdbool conformant cplusplus bool Bool stdarg tm
21088 @c  LocalWords:  ctype strchr strrchr rindex bcopy memmove memchr WEXITSTATUS
21089 @c  LocalWords:  WIFEXITED TIOCGWINSZ GWINSZ termios preprocess preprocessable
21090 @c  LocalWords:  DECLS strdup calloc BLKSIZE blksize RDEV rdev TZNAME tzname pw
21091 @c  LocalWords:  passwd gecos pwd MBSTATE mbstate wchar RETSIGTYPE hup UID uid
21092 @c  LocalWords:  gid ptrdiff uintmax EXEEXT OBJEXT Ae conftest AXP str
21093 @c  LocalWords:  ALIGNOF WERROR Werror cpp HP's WorkShop egcs un fied stdc CXX
21094 @c  LocalWords:  varargs BIGENDIAN Endianness SPARC endianness grep'ed CONST FC
21095 @c  LocalWords:  const STRINGIZE stringizing PARAMS unprotoize protos KCC cxx
21096 @c  LocalWords:  xlC aCC CXXCPP FREEFORM xlf FLIBS FCLIBS ish SRCEXT XTRA LFS
21097 @c  LocalWords:  ISC lcposix MINIX Minix conditionalized inlines hw dD confdefs
21098 @c  LocalWords:  fputs stdout PREPROC ar UFS HFS QNX realtime fstype STATVFS se
21099 @c  LocalWords:  statvfs STATFS statfs func machfile hdr lelf raboof DEFUN GTK
21100 @c  LocalWords:  GTKMM Grmph ified ine defn baz EOF qar Ahhh changecom algol io
21101 @c  LocalWords:  changeword quadrigraphs quadrigraph dnl SGI atoi overquoting
21102 @c  LocalWords:  Aas Wcross sep args namespace undefine bpatsubst popdef dquote
21103 @c  LocalWords:  bregexp Overquote overquotation meisch maisch meische maische
21104 @c  LocalWords:  miscian DIRNAME dirname MKDIR CATFILE XMKMF TRAVOLTA celsius
21105 @c  LocalWords:  EMX emxos Emacsen Korn DYNIX subshell posix Ksh ksh Pdksh Zsh
21106 @c  LocalWords:  pdksh zsh Allbery Lipe Kubota UWS zorglub stderr eval esac lfn
21107 @c  LocalWords:  drivespec Posixy DJGPP doschk prettybird LPT pfew Zsh's yu yaa
21108 @c  LocalWords:  yM uM aM firebird IP subdir misparses ok Unpatched abc bc zA
21109 @c  LocalWords:  CDPATH DUALCASE LINENO prepass Subshells lineno NULLCMD cmp wc
21110 @c  LocalWords:  MAILPATH scanset arg NetBSD Almquist printf expr cp
21111 @c  LocalWords:  Oliva awk Aaaaarg cmd regex xfoo GNV OpenVMS VM
21112 @c  LocalWords:  sparc Proulx nbar nfoo maxdepth acdilrtu TWG mc
21113 @c  LocalWords:  mkdir exe uname OpenBSD Fileutils mktemp umask TMPDIR guid os
21114 @c  LocalWords:  fooXXXXXX Unicos utimes hpux hppa unescaped
21115 @c  LocalWords:  pmake DOS's gmake ifoo DESTDIR autoconfiscated pc coff mips gg
21116 @c  LocalWords:  dec ultrix cpu wildcards rpcc rdtsc powerpc readline
21117 @c  LocalWords:  withval vxworks gless localcache usr LOFF loff CYGWIN Cygwin
21118 @c  LocalWords:  cygwin SIGLIST siglist SYSNDIR SYSDIR ptx lseq rusage elif MSC
21119 @c  LocalWords:  lfoo POUNDBANG lsun NIS getpwnam SYSCALLS RSH INTL lintl aix
21120 @c  LocalWords:  intl lx ldir syslog bsd EPI toolchain netbsd objext de KNR nn
21121 @c  LocalWords:  fication LTLIBOBJS Wdiff TESTDIR atconfig atlocal akim XFAIL
21122 @c  LocalWords:  ChangeLog prepended errexit smallexample TESTSUITEFLAGS GPL er
21123 @c  LocalWords:  installcheck autotest indir Pixley Bothner Eichin Kerberos adl
21124 @c  LocalWords:  DISTCLEANFILES preprocessor's fileutils Stallman Murphey Stenn
21125 @c  LocalWords:  Manfredi Autoconfig TCL FSP david zuhn Blandy MACRODIR Raeburn
21126 @c  LocalWords:  autoconfiscate Savoye Haertel Avera Meyering fdl appendixsec
21127 @c  LocalWords:  printindex american LIBOBJDIR LibdirTest ERLCFLAGS OBJCFLAGS
21128 @c  LocalWords:  VER Gnulib online xyes strcpy TYPEOF typeof OBJC objcc objc ln
21129 @c  LocalWords:  GOBJC OTP ERLC erl valloc decr dumpdef errprint incr
21130 @c  LocalWords:  esyscmd len maketemp pushdef substr syscmd sysval translit txt
21131 @c  LocalWords:  sinclude foreach myvar tolower toupper uniq BASENAME STDIN
21132 @c  LocalWords:  Dynix descrips basename aname cname macroexpands xno xcheck
21133 @c  LocalWords:  LIBREADLINE lreadline lncurses libreadline
21135 @c Local Variables:
21136 @c fill-column: 72
21137 @c ispell-local-dictionary: "american"
21138 @c indent-tabs-mode: nil
21139 @c whitespace-check-buffer-indent: nil
21140 @c End: